Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages
hugin_base/hugin_utils/utils.h
Go to the documentation of this file.00001 // -*- c-basic-offset: 4 -*- 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 // has to be included before! 00042 #include <wx/log.h> 00043 #endif 00044 00045 #ifdef WIN32 00046 #define snprintf _snprintf 00047 #endif 00048 00049 #ifdef __WXMSW__ 00050 // has to be included before! 00051 #include <wx/log.h> 00052 #define snprintf _snprintf 00053 #endif 00054 // ?????????????????????????????????????????? 00055 #endif 00056 00057 00058 // misc utility functions / macros 00059 00060 #ifdef __GNUC__ 00061 // the full function name is too long.. 00062 // #define DEBUG_HEADER hugin_utils::CurrentTime() <<" (" << __FILE__ << ":" << __LINE__ << ") " << __PRETTY_FUNCTION__ << "()" << std::endl << " " 00063 #define DEBUG_HEADER hugin_utils::CurrentTime() <<" (" << __FILE__ << ":" << __LINE__ << ") " << __func__ << "(): " 00064 #elif _MSC_VER > 1300 00065 #define DEBUG_HEADER hugin_utils::CurrentTime() <<" (" << __FILE__ << ":" << __LINE__ << ") " << __FUNCTION__ << "(): " 00066 #else 00067 #define DEBUG_HEADER hugin_utils::CurrentTime() <<" (" << __FILE__ << ":" << __LINE__ << ") " << __func__ << "(): " 00068 #endif 00069 00070 00071 #ifdef DEBUG 00072 // debug trace 00073 #define DEBUG_TRACE(msg) { std::cerr << "TRACE " << DEBUG_HEADER << msg << std::endl; } 00074 // low level debug info 00075 #define DEBUG_DEBUG(msg) { std::cerr << "DEBUG " << DEBUG_HEADER << msg << std::endl; } 00076 // informational debug message, 00077 #define DEBUG_INFO(msg) { std::cerr << "INFO " << DEBUG_HEADER << msg << std::endl; } 00078 // major change/operation should use this 00079 #define DEBUG_NOTICE(msg) { std::cerr << "NOTICE " << DEBUG_HEADER << msg << std::endl; } 00080 #else 00081 #define DEBUG_TRACE(msg) 00082 #define DEBUG_DEBUG(msg) 00083 #define DEBUG_INFO(msg) 00084 #define DEBUG_NOTICE(msg) 00085 #endif 00086 00087 // when an error occured, but can be handled by the same function 00088 #define DEBUG_WARN(msg) { std::cerr << "WARN: " << DEBUG_HEADER << msg << std::endl; } 00089 // an error occured, might be handled by a calling function 00090 #define DEBUG_ERROR(msg) { std::cerr << "ERROR: " << DEBUG_HEADER << msg << std::endl; } 00091 // a fatal error occured. further program execution is unlikely 00092 #define DEBUG_FATAL(msg) { std::cerr << "FATAL: " << DEBUG_HEADER << "(): " << msg << std::endl; } 00093 // C-style assertion 00094 #define DEBUG_ASSERT(cond) assert(cond) 00095 00096 00097 // use trace function under windows, because usually there is 00098 // no stdout under windows 00099 #ifdef __WXMSW__ 00100 #ifdef DEBUG 00101 #undef DEBUG_TRACE 00102 #undef DEBUG_DEBUG 00103 #undef DEBUG_INFO 00104 #undef DEBUG_NOTICE 00105 00106 // debug trace 00107 // #define DEBUG_TRACE(msg) { std::stringstream o; o << "TRACE " << DEBUG_HEADER << msg; wxLogDebug(o.str().c_str());} 00108 #define DEBUG_TRACE(msg) { std::cerr << "TRACE " << DEBUG_HEADER << msg << std::endl; } 00109 // low level debug info 00110 // #define DEBUG_DEBUG(msg) { std::stringstream o; o << "DEBUG " << DEBUG_HEADER << msg; wxLogDebug(o.str().c_str()); } 00111 #define DEBUG_DEBUG(msg) { std::cerr << "DEBUG " << DEBUG_HEADER << msg << std::endl; } 00112 // informational debug message, 00113 // #define DEBUG_INFO(msg) { std::stringstream o; o << "INFO " << DEBUG_HEADER << msg; wxLogDebug(o.str().c_str()); } 00114 #define DEBUG_INFO(msg) { std::cerr << "INFO " << DEBUG_HEADER << msg << std::endl; } 00115 // major change/operation should use this 00116 // #define DEBUG_NOTICE(msg) { std::stringstream o; o << "NOTICE " << DEBUG_HEADER << msg; wxLogMessage(o.str().c_str()); } 00117 #define DEBUG_NOTICE(msg) { std::cerr << "NOTICE " << DEBUG_HEADER << msg << std::endl; } 00118 #endif 00119 00120 #undef DEBUG_WARN 00121 #undef DEBUG_ERROR 00122 #undef DEBUG_FATAL 00123 #undef DEBUG_ASSERT 00124 00125 // when an error occured, but can be handled by the same function 00126 #define DEBUG_WARN(msg) { std::stringstream o; o << "WARN: " << DEBUG_HEADER << msg; wxLogWarning(wxString(o.str().c_str(), wxConvISO8859_1));} 00127 // an error occured, might be handled by a calling function 00128 #define DEBUG_ERROR(msg) { std::stringstream o; o << "ERROR: " << DEBUG_HEADER << msg; wxLogError(wxString(o.str().c_str(),wxConvISO8859_1));} 00129 // a fatal error occured. further program execution is unlikely 00130 #define DEBUG_FATAL(msg) { std::stringstream o; o << "FATAL: " << DEBUG_HEADER << "(): " << msg; wxLogError(wxString(o.str().c_str(),wxConvISO8859_1)); } 00131 // assertion 00132 #define DEBUG_ASSERT(cond) \ 00133 do { \ 00134 if (!(cond)) { \ 00135 std::stringstream o; o << "ASSERTATION: " << DEBUG_HEADER << "(): " << #cond; \ 00136 wxLogFatalError(wxString(o.str().c_str(),wxConvISO8859_1)); \ 00137 } \ 00138 } while(0) 00139 #endif 00140 00141 // 00142 #define UTILS_THROW(class, msg) { std::stringstream o; o << msg; throw(class(o.str().c_str())); }; 00143 00144 00145 00146 namespace hugin_utils 00147 { 00148 00150 IMPEX std::string CurrentTime(); 00151 00159 IMPEX std::string doubleToString(double d, int fractionaldigits=-1); 00160 00172 template <typename STR> 00173 bool stringToDouble(const STR & str_, double & dest) 00174 { 00175 double res=0; 00176 // set numeric locale to C, for correct number output 00177 char * old_locale = setlocale(LC_NUMERIC,NULL); 00178 old_locale = strdup(old_locale); 00179 setlocale(LC_NUMERIC,"C"); 00180 00181 STR str(str_); 00182 // replace all kommas with points, independant of the locale.. 00183 for (typename STR::iterator it = str.begin(); it != str.end(); ++it) { 00184 if (*it == ',') { 00185 *it = '.'; 00186 } 00187 } 00188 00189 const char * p = str.c_str(); 00190 char * pe=0; 00191 res = strtod(p,&pe); 00192 00193 // reset locale 00194 setlocale(LC_NUMERIC,old_locale); 00195 free(old_locale); 00196 00197 if (pe == p) { 00198 // conversion failed. 00199 DEBUG_DEBUG("conversion failed: " << str << " to:" << dest); 00200 return false; 00201 } else { 00202 // conversion ok. 00203 dest = res; 00204 // DEBUG_DEBUG("converted: " << str << " to:" << dest); 00205 return true; 00206 } 00207 } 00208 00210 IMPEX std::string getPathPrefix(const std::string & filename); 00211 00213 IMPEX std::string getExtension(const std::string & basename); 00214 00218 IMPEX std::string stripPath(const std::string & filename); 00219 00221 IMPEX std::string stripExtension(const std::string & basename); 00222 00223 template <typename Target, typename Source> 00224 Target lexical_cast(Source arg) { 00225 00226 std::stringstream interpreter; 00227 00228 Target result; 00229 00230 if (!(interpreter << arg) || 00231 !(interpreter >> result) || 00232 !(interpreter >> std::ws).eof()) { 00233 00234 DEBUG_ERROR("lexical cast error"); 00235 // cast error. handle it somehow 00236 // boost guys throw an exception here 00237 }; 00238 00239 return result; 00240 00241 }; // lexical cast 00242 00243 00244 template <class str> 00245 str QuoteStringInternal(const str & arg, const str & quotechar, 00246 const str & replacements) 00247 { 00248 // loop over all chars.. 00249 str ret(arg); 00250 size_t len = replacements.size(); 00251 for (size_t i = 0; i < len; i++) { 00252 str source(replacements.substr(i,1)); 00253 str dest(quotechar + source); 00254 size_t idx = 0; 00255 do { 00256 idx = ret.find(source,idx); 00257 if (idx != str::npos) { 00258 ret.replace(idx, 1, dest); 00259 // skip to next unknown char. 00260 idx += 2; 00261 } 00262 } while (idx != str::npos); 00263 } 00264 return ret; 00265 } 00266 00268 template <class str> 00269 str replaceAll(const str& arg, const str& target, const str& replacement) 00270 { 00271 str ret(arg); 00272 typename str::size_type pos = ret.find(target, 0); 00273 00274 for ( typename str::size_type n = 0 ; pos != str::npos ; pos = ret.find(target, n) ) 00275 { 00276 ret.replace(pos, target.size(), replacement); 00277 n = pos + replacement.size(); 00278 } 00279 00280 return ret; 00281 } 00282 00283 IMPEX void ControlPointErrorColour(const double cperr, 00284 double &r,double &g, double &b); 00285 00286 } // namespace 00287 00288 00289 #endif // _HUGIN_UTILS_UTILS_H
1.3.9.1