00001
00002
00025 #include "utils.h"
00026
00027 #ifdef WIN32
00028 #include <sys/utime.h>
00029 #else
00030 #include <sys/time.h>
00031 #endif
00032 #include <time.h>
00033 #include <fstream>
00034 #include <stdio.h>
00035 #include <cstdio>
00036
00037
00038 namespace hugin_utils {
00039
00040 #ifdef UNIX_LIKE
00041 std::string GetCurrentTimeString()
00042 {
00043 char tmp[100];
00044 struct tm t;
00045 struct timeval tv;
00046 gettimeofday(&tv,NULL);
00047 localtime_r((time_t*)&tv.tv_sec, &t);
00048 strftime(tmp,99,"%H:%M:%S",&t);
00049 sprintf(tmp+8,".%06ld", (long)tv.tv_usec);
00050 return tmp;
00051 }
00052 #else
00053 std::string GetCurrentTimeString()
00054 {
00055
00056 return "";
00057 }
00058 #endif
00059
00060
00061 std::string getExtension(const std::string & basename2)
00062 {
00063 std::string::size_type idx = basename2.rfind('.');
00064
00065
00066 if (idx == std::string::npos) {
00067
00068 return std::string("");
00069 }
00070 #ifdef UNIX_LIKE
00071
00072 std::string::size_type slashidx = basename2.find('/', idx);
00073 if ( slashidx == std::string::npos)
00074 {
00075 return basename2.substr(idx+1);
00076 } else {
00077 return std::string("");
00078 }
00079 #else
00080
00081 std::string::size_type slashidx = basename2.find('/', idx);
00082 std::string::size_type backslashidx = basename2.find('\\', idx);
00083 if ( slashidx == std::string::npos && backslashidx == std::string::npos)
00084 {
00085 return basename2.substr(idx+1);
00086 } else {
00087 return std::string("");
00088 }
00089 #endif
00090 }
00091
00092 std::string stripExtension(const std::string & basename2)
00093 {
00094 std::string::size_type idx = basename2.rfind('.');
00095
00096
00097 if (idx == std::string::npos) {
00098
00099 return basename2;
00100 }
00101 #ifdef UNIX_LIKE
00102 std::string::size_type slashidx = basename2.find('/', idx);
00103 if ( slashidx == std::string::npos)
00104 {
00105 return basename2.substr(0, idx);
00106 } else {
00107 return basename2;
00108 }
00109 #else
00110
00111 std::string::size_type slashidx = basename2.find('/', idx);
00112 std::string::size_type backslashidx = basename2.find('\\', idx);
00113 if ( slashidx == std::string::npos && backslashidx == std::string::npos)
00114 {
00115 return basename2.substr(0, idx);
00116 } else {
00117 return basename2;
00118 }
00119 #endif
00120 }
00121
00122 std::string stripPath(const std::string & filename)
00123 {
00124 #ifdef UNIX_LIKE
00125 std::string::size_type idx = filename.rfind('/');
00126 #else
00127 std::string::size_type idx1 = filename.rfind('\\');
00128 std::string::size_type idx2 = filename.rfind('/');
00129 std::string::size_type idx;
00130 if (idx1 == std::string::npos) {
00131 idx = idx2;
00132 } else if (idx2 == std::string::npos) {
00133 idx = idx1;
00134 } else {
00135 idx = std::max(idx1, idx2);
00136 }
00137 #endif
00138 if (idx != std::string::npos) {
00139
00140 return filename.substr(idx + 1);
00141 } else {
00142 return filename;
00143 }
00144 }
00145
00146 std::string getPathPrefix(const std::string & filename)
00147 {
00148 #ifdef UNIX_LIKE
00149 std::string::size_type idx = filename.rfind('/');
00150 #else
00151 std::string::size_type idx1 = filename.rfind('\\');
00152 std::string::size_type idx2 = filename.rfind('/');
00153 std::string::size_type idx;
00154 if (idx1 == std::string::npos) {
00155 idx = idx2;
00156 } else if (idx2 == std::string::npos) {
00157 idx = idx1;
00158 } else {
00159 idx = std::max(idx1, idx2);
00160 }
00161 #endif
00162 if (idx != std::string::npos) {
00163
00164 return filename.substr(0, idx+1);
00165 } else {
00166 return "";
00167 }
00168 }
00169
00170 std::string doubleToString(double d, int digits)
00171 {
00172 char fmt[10];
00173 if (digits < 0) {
00174 strcpy(fmt,"%f");
00175 } else {
00176 std::sprintf(fmt,"%%.%df",digits);
00177 }
00178 char c[1024];
00179 c[1023] = 0;
00180 #ifdef _MSC_VER
00181 _snprintf (c, 1023, fmt, d);
00182 #else
00183 snprintf (c, 1023, fmt, d);
00184 #endif
00185
00186 std::string number (c);
00187
00188 int l = (int)number.length()-1;
00189
00190 while ( l != 0 && number[l] == '0' ) {
00191 number.erase (l);
00192 l--;
00193 }
00194 if ( number[l] == ',' ) {
00195 number.erase (l);
00196 l--;
00197 }
00198 if ( number[l] == '.' ) {
00199 number.erase (l);
00200 l--;
00201 }
00202 return number;
00203 }
00204
00205 void ControlPointErrorColour(const double cperr,
00206 double &r,double &g, double &b)
00207 {
00208
00209 double xp1=5;
00210 double xp2=10;
00211
00212 if ( cperr<= xp1)
00213 {
00214
00215 r = cperr / xp1;
00216 g = 0.75;
00217 }
00218 else
00219 {
00220 r = 1.0;
00221 g = 0.75 * ( (1.0-std::min<double>(cperr-xp1,(xp2-xp1))/(xp2-xp1)));
00222 }
00223 b = 0.0;
00224 }
00225
00226 bool FileExists(const std::string filename)
00227 {
00228 std::ifstream ifile(filename.c_str());
00229 return !ifile.fail();
00230 }
00231
00232 std::string GetAbsoluteFilename(const std::string filename)
00233 {
00234 #ifdef _WINDOWS
00235 char fullpath[_MAX_PATH];
00236 _fullpath(fullpath,filename.c_str(),_MAX_PATH);
00237 return std::string(fullpath);
00238 #else
00239
00240
00241
00243 bool tempFileCreated=false;
00244 if(!FileExists(filename))
00245 {
00246 tempFileCreated=true;
00247 std::ofstream os(filename.c_str());
00248 os.close();
00249 };
00250 char *real_path = realpath(filename.c_str(), NULL);
00251 std::string absPath;
00252 if(real_path!=NULL)
00253 {
00254 absPath=std::string(real_path);
00255 free(real_path);
00256 }
00257 else
00258 {
00259 absPath=filename;
00260 };
00261 if(tempFileCreated)
00262 {
00263 remove(filename.c_str());
00264 };
00265 return absPath;
00266 #endif
00267 };
00268
00269 }