00001
00024 #ifndef _HUGIN_UTILS_UTILS_H
00025 #define _HUGIN_UTILS_UTILS_H
00026
00027 #include <hugin_shared.h>
00028 #include <string.h>
00029 #include <stdlib.h>
00030 #include <string>
00031 #include <vector>
00032 #include <iostream>
00033 #include <sstream>
00034 #include <cassert>
00035
00036 #include <hugin_utils/platform.h>
00037
00038 #if 0
00039
00040 #ifdef __WXMSW__
00041
00042 #include <wx/log.h>
00043 #endif
00044
00045 #ifdef WIN32
00046 #define snprintf _snprintf
00047 #endif
00048
00049 #ifdef __WXMSW__
00050
00051 #include <wx/log.h>
00052 #define snprintf _snprintf
00053 #endif
00054
00055 #endif
00056
00057
00058
00059
00060
00061
00062
00063
00064 #ifdef __GNUC__
00065
00066
00067 #define DEBUG_HEADER hugin_utils::GetCurrentTimeString() <<" (" << __FILE__ << ":" << __LINE__ << ") " << __func__ << "(): "
00068 #elif _MSC_VER > 1300
00069 #define DEBUG_HEADER hugin_utils::GetCurrentTimeString() <<" (" << __FILE__ << ":" << __LINE__ << ") " << __FUNCTION__ << "(): "
00070 #else
00071 #define DEBUG_HEADER hugin_utils::GetCurrentTimeString() <<" (" << __FILE__ << ":" << __LINE__ << ") " << __func__ << "(): "
00072 #endif
00073
00074
00075 #ifdef DEBUG
00076
00077 #define DEBUG_TRACE(msg) { std::cerr << "TRACE " << DEBUG_HEADER << msg << std::endl; }
00078
00079 #define DEBUG_DEBUG(msg) { std::cerr << "DEBUG " << DEBUG_HEADER << msg << std::endl; }
00080
00081 #define DEBUG_INFO(msg) { std::cerr << "INFO " << DEBUG_HEADER << msg << std::endl; }
00082
00083 #define DEBUG_NOTICE(msg) { std::cerr << "NOTICE " << DEBUG_HEADER << msg << std::endl; }
00084 #else
00085 #define DEBUG_TRACE(msg)
00086 #define DEBUG_DEBUG(msg)
00087 #define DEBUG_INFO(msg)
00088 #define DEBUG_NOTICE(msg)
00089 #endif
00090
00091
00092 #define DEBUG_WARN(msg) { std::cerr << "WARN: " << DEBUG_HEADER << msg << std::endl; }
00093
00094 #define DEBUG_ERROR(msg) { std::cerr << "ERROR: " << DEBUG_HEADER << msg << std::endl; }
00095
00096 #define DEBUG_FATAL(msg) { std::cerr << "FATAL: " << DEBUG_HEADER << "(): " << msg << std::endl; }
00097
00098 #define DEBUG_ASSERT(cond) assert(cond)
00099
00100
00101
00102
00103 #ifdef __WXMSW__
00104 #ifdef DEBUG
00105 #undef DEBUG_TRACE
00106 #undef DEBUG_DEBUG
00107 #undef DEBUG_INFO
00108 #undef DEBUG_NOTICE
00109
00110
00111
00112 #define DEBUG_TRACE(msg) { std::cerr << "TRACE " << DEBUG_HEADER << msg << std::endl; }
00113
00114
00115 #define DEBUG_DEBUG(msg) { std::cerr << "DEBUG " << DEBUG_HEADER << msg << std::endl; }
00116
00117
00118 #define DEBUG_INFO(msg) { std::cerr << "INFO " << DEBUG_HEADER << msg << std::endl; }
00119
00120
00121 #define DEBUG_NOTICE(msg) { std::cerr << "NOTICE " << DEBUG_HEADER << msg << std::endl; }
00122 #endif
00123
00124 #undef DEBUG_WARN
00125 #undef DEBUG_ERROR
00126 #undef DEBUG_FATAL
00127 #undef DEBUG_ASSERT
00128
00129
00130 #define DEBUG_WARN(msg) { std::stringstream o; o << "WARN: " << DEBUG_HEADER << msg; wxLogWarning(wxString(o.str().c_str(), wxConvISO8859_1));}
00131
00132 #define DEBUG_ERROR(msg) { std::stringstream o; o << "ERROR: " << DEBUG_HEADER << msg; wxLogError(wxString(o.str().c_str(),wxConvISO8859_1));}
00133
00134 #define DEBUG_FATAL(msg) { std::stringstream o; o << "FATAL: " << DEBUG_HEADER << "(): " << msg; wxLogError(wxString(o.str().c_str(),wxConvISO8859_1)); }
00135
00136 #define DEBUG_ASSERT(cond) \
00137 do { \
00138 if (!(cond)) { \
00139 std::stringstream o; o << "ASSERTATION: " << DEBUG_HEADER << "(): " << #cond; \
00140 wxLogFatalError(wxString(o.str().c_str(),wxConvISO8859_1)); \
00141 } \
00142 } while(0)
00143 #endif
00144
00145
00146 #define UTILS_THROW(class, msg) { std::stringstream o; o << msg; throw(class(o.str().c_str())); };
00147
00148
00149
00150 namespace hugin_utils
00151 {
00152
00154 IMPEX std::string GetCurrentTimeString();
00155
00163 IMPEX std::string doubleToString(double d, int fractionaldigits=-1);
00164
00176 template <typename STR>
00177 bool stringToDouble(const STR & str_, double & dest)
00178 {
00179 double res=0;
00180
00181 char * old_locale = setlocale(LC_NUMERIC,NULL);
00182 old_locale = strdup(old_locale);
00183 setlocale(LC_NUMERIC,"C");
00184
00185 STR str(str_);
00186
00187 for (typename STR::iterator it = str.begin(); it != str.end(); ++it) {
00188 if (*it == ',') {
00189 *it = '.';
00190 }
00191 }
00192
00193 const char * p = str.c_str();
00194 char * pe=0;
00195 res = strtod(p,&pe);
00196
00197
00198 setlocale(LC_NUMERIC,old_locale);
00199 free(old_locale);
00200
00201 if (pe == p) {
00202
00203 DEBUG_DEBUG("conversion failed: " << str << " to:" << dest);
00204 return false;
00205 } else {
00206
00207 dest = res;
00208
00209 return true;
00210 }
00211 }
00212
00214 IMPEX std::string getPathPrefix(const std::string & filename);
00215
00217 IMPEX std::string getExtension(const std::string & basename);
00218
00222 IMPEX std::string stripPath(const std::string & filename);
00223
00225 IMPEX std::string stripExtension(const std::string & basename);
00226
00227 template <typename Target, typename Source>
00228 Target lexical_cast(Source arg) {
00229
00230 std::stringstream interpreter;
00231
00232 Target result;
00233
00234 if (!(interpreter << arg) ||
00235 !(interpreter >> result) ||
00236 !(interpreter >> std::ws).eof()) {
00237
00238 DEBUG_ERROR("lexical cast error");
00239
00240
00241 };
00242
00243 return result;
00244
00245 };
00246
00247
00248 template <class str>
00249 str QuoteStringInternal(const str & arg, const str & quotechar,
00250 const str & replacements)
00251 {
00252
00253 str ret(arg);
00254 size_t len = replacements.size();
00255 for (size_t i = 0; i < len; i++) {
00256 str source(replacements.substr(i,1));
00257 str dest(quotechar + source);
00258 size_t idx = 0;
00259 do {
00260 idx = ret.find(source,idx);
00261 if (idx != str::npos) {
00262 ret.replace(idx, 1, dest);
00263
00264 idx += 2;
00265 }
00266 } while (idx != str::npos);
00267 }
00268 return ret;
00269 }
00270
00272 template <class str>
00273 str replaceAll(const str& arg, const str& target, const str& replacement)
00274 {
00275 str ret(arg);
00276 typename str::size_type pos = ret.find(target, 0);
00277
00278 for ( typename str::size_type n = 0 ; pos != str::npos ; pos = ret.find(target, n) )
00279 {
00280 ret.replace(pos, target.size(), replacement);
00281 n = pos + replacement.size();
00282 }
00283
00284 return ret;
00285 }
00286
00287 IMPEX void ControlPointErrorColour(const double cperr,
00288 double &r,double &g, double &b);
00289
00291 IMPEX bool FileExists(const std::string filename);
00292
00294 IMPEX std::string GetAbsoluteFilename(const std::string filename);
00295 }
00296
00297
00298 #endif // _HUGIN_UTILS_UTILS_H