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 #include <vigra/imageinfo.hxx>
00036 #include <lcms2.h>
00037 
00038 #include <hugin_utils/platform.h>
00039 
00040 // misc utility functions / macros
00041 
00042 // remark:
00043 // on unix_like systems don't use CurrentTime, this is defined as a macro in X.h and breaks the debug messages
00044 // on windows we can't use GetCurrentTime because this is replaced with GetTickCount
00045 
00046 #ifdef __GNUC__
00047     // the full function name is too long..
00048 //  #define DEBUG_HEADER hugin_utils::CurrentTime() <<" (" << __FILE__ << ":" << __LINE__ << ") "  << __PRETTY_FUNCTION__ << "()" << std::endl << "    "
00049     #define DEBUG_HEADER hugin_utils::GetCurrentTimeString() <<" (" << __FILE__ << ":" << __LINE__ << ") "  << __func__ << "(): "
00050 #elif _MSC_VER > 1300
00051     #define DEBUG_HEADER hugin_utils::GetCurrentTimeString() <<" (" << __FILE__ << ":" << __LINE__ << ") "  << __FUNCTION__ << "(): "
00052 #else
00053     #define DEBUG_HEADER hugin_utils::GetCurrentTimeString() <<" (" << __FILE__ << ":" << __LINE__ << ") "  << __func__ << "(): "
00054 #endif
00055 
00056 
00057 #ifdef DEBUG
00058     // debug trace
00059     #define DEBUG_TRACE(msg) { std::cerr << "TRACE " << DEBUG_HEADER << msg << std::endl; }
00060     // low level debug info
00061     #define DEBUG_DEBUG(msg) { std::cerr << "DEBUG " << DEBUG_HEADER << msg << std::endl; }
00062     // informational debug message,
00063     #define DEBUG_INFO(msg) { std::cerr << "INFO " << DEBUG_HEADER << msg << std::endl; }
00064     // major change/operation should use this
00065     #define DEBUG_NOTICE(msg) { std::cerr << "NOTICE " << DEBUG_HEADER << msg << std::endl; }
00066 #else
00067     #define DEBUG_TRACE(msg)
00068     #define DEBUG_DEBUG(msg)
00069     #define DEBUG_INFO(msg)
00070     #define DEBUG_NOTICE(msg)
00071 #endif
00072 
00073 // when an error occured, but can be handled by the same function
00074 #define DEBUG_WARN(msg) { std::cerr << "WARN: " << DEBUG_HEADER << msg << std::endl; }
00075 // an error occured, might be handled by a calling function
00076 #define DEBUG_ERROR(msg) { std::cerr << "ERROR: " << DEBUG_HEADER << msg << std::endl; }
00077 // a fatal error occured. further program execution is unlikely
00078 #define DEBUG_FATAL(msg) { std::cerr << "FATAL: " << DEBUG_HEADER << "(): " << msg << std::endl; }
00079 // C-style assertion
00080 #define DEBUG_ASSERT(cond) assert(cond)
00081 
00082 
00083 // use trace function under windows, because usually there is
00084 // no stdout under windows
00085 #ifdef __WXMSW__
00086     #ifdef DEBUG
00087         #undef DEBUG_TRACE
00088         #undef DEBUG_DEBUG
00089         #undef DEBUG_INFO
00090         #undef DEBUG_NOTICE
00091 
00092         // debug trace
00093 //      #define DEBUG_TRACE(msg) { std::stringstream o; o << "TRACE " << DEBUG_HEADER << msg; wxLogDebug(o.str().c_str());}
00094         #define DEBUG_TRACE(msg) { std::cerr << "TRACE " << DEBUG_HEADER << msg << std::endl; }
00095         // low level debug info
00096 //      #define DEBUG_DEBUG(msg) { std::stringstream o; o << "DEBUG " << DEBUG_HEADER << msg; wxLogDebug(o.str().c_str()); }
00097         #define DEBUG_DEBUG(msg) { std::cerr << "DEBUG " << DEBUG_HEADER << msg << std::endl; }
00098         // informational debug message,
00099 //      #define DEBUG_INFO(msg) { std::stringstream o; o << "INFO " << DEBUG_HEADER << msg; wxLogDebug(o.str().c_str()); }
00100         #define DEBUG_INFO(msg) { std::cerr << "INFO " << DEBUG_HEADER << msg << std::endl; }
00101         // major change/operation should use this
00102 //      #define DEBUG_NOTICE(msg) { std::stringstream o; o << "NOTICE " << DEBUG_HEADER << msg; wxLogMessage(o.str().c_str()); }
00103         #define DEBUG_NOTICE(msg) { std::cerr << "NOTICE " << DEBUG_HEADER << msg << std::endl; }
00104     #endif
00105     
00106     #undef DEBUG_WARN
00107     #undef DEBUG_ERROR
00108     #undef DEBUG_FATAL
00109     #undef DEBUG_ASSERT
00110 
00111     // when an error occured, but can be handled by the same function
00112     #define DEBUG_WARN(msg) { std::stringstream o; o << "WARN: " << DEBUG_HEADER << msg; wxLogWarning(wxString(o.str().c_str(), wxConvISO8859_1));}
00113     // an error occured, might be handled by a calling function
00114     #define DEBUG_ERROR(msg) { std::stringstream o; o << "ERROR: " << DEBUG_HEADER << msg; wxLogError(wxString(o.str().c_str(),wxConvISO8859_1));}
00115     // a fatal error occured. further program execution is unlikely
00116     #define DEBUG_FATAL(msg) { std::stringstream o; o << "FATAL: " << DEBUG_HEADER << "(): " << msg; wxLogError(wxString(o.str().c_str(),wxConvISO8859_1)); }
00117     // assertion
00118     #define DEBUG_ASSERT(cond) \
00119         do { \
00120             if (!(cond)) { \
00121                 std::stringstream o; o << "ASSERTATION: " << DEBUG_HEADER << "(): " << #cond; \
00122                     wxLogFatalError(wxString(o.str().c_str(),wxConvISO8859_1)); \
00123             } \
00124         } while(0)
00125 #endif
00126 
00127 //
00128 #define UTILS_THROW(class, msg)  { std::stringstream o; o <<  msg; throw(class(o.str().c_str())); };
00129 
00130 
00131 
00132 namespace hugin_utils
00133 {
00134     
00136     IMPEX std::string GetCurrentTimeString();
00137 
00145     IMPEX std::string doubleToString(double d, int fractionaldigits=-1);
00146 
00158     template <typename STR>
00159     bool stringToDouble(const STR & str_, double & dest)
00160     {
00161         double res=0;
00162         // set numeric locale to C, for correct number output
00163         char * old_locale = setlocale(LC_NUMERIC,NULL);
00164         old_locale = strdup(old_locale);
00165         setlocale(LC_NUMERIC,"C");
00166 
00167         STR str(str_);
00168         // replace all kommas with points, independant of the locale..
00169         for (typename STR::iterator it = str.begin(); it != str.end(); ++it) {
00170             if (*it == ',') {
00171                 *it = '.';
00172             }
00173         }
00174 
00175         const char * p = str.c_str();
00176         char * pe=0;
00177         res = strtod(p,&pe);
00178 
00179         // reset locale
00180         setlocale(LC_NUMERIC,old_locale);
00181         free(old_locale);
00182 
00183         if (pe == p) {
00184             // conversion failed.
00185             DEBUG_DEBUG("conversion failed: " << str << " to:" << dest);
00186             return false;
00187         } else {
00188             // conversion ok.
00189             dest = res;
00190     //        DEBUG_DEBUG("converted: " << str << " to:" << dest);
00191             return true;
00192         }
00193     }
00194 
00196     IMPEX bool stringToInt(const std::string& s, int& val);
00197 
00199     IMPEX bool stringToUInt(const std::string& s, unsigned int& val);
00200 
00202     IMPEX std::string getPathPrefix(const std::string & filename);
00203 
00205     IMPEX std::string getExtension(const std::string & basename);
00206 
00210     IMPEX std::string stripPath(const std::string & filename);
00211 
00213     IMPEX std::string stripExtension(const std::string & basename);
00214 
00216     IMPEX std::string StrTrim(const std::string& str);
00217 
00219     IMPEX std::vector<std::string> SplitString(const std::string& s, const std::string& sep);
00220 
00222     IMPEX void ReplaceAll(std::string& s, const std::string& oldChar, char newChar);
00223 
00224     template <class str>
00225     str QuoteStringInternal(const str & arg, const str & quotechar,
00226                             const str & replacements)
00227     {
00228         // loop over all chars..
00229         str ret(arg);
00230         size_t len = replacements.size();
00231         for (size_t i = 0; i < len; i++) {
00232             str source(replacements.substr(i,1));
00233             str dest(quotechar + source);
00234             size_t idx = 0;
00235             do {
00236                 idx = ret.find(source,idx);
00237                 if (idx != str::npos) {
00238                     ret.replace(idx, 1, dest);
00239                     // skip to next unknown char.
00240                     idx += 2;
00241                 }
00242             } while (idx != str::npos);
00243         }
00244         return ret;
00245     }
00246 
00247     IMPEX void ControlPointErrorColour(const double cperr, 
00248         double &r,double &g, double &b);
00249 
00251     IMPEX bool FileExists(const std::string& filename);
00252 
00254     IMPEX std::string GetAbsoluteFilename(const std::string& filename);
00255 
00257     IMPEX std::string GetDataDir();
00258 
00262     IMPEX std::string GetUserAppDataDir();
00263 
00268     IMPEX bool initGPU(int *argcp, char **argv);
00270     IMPEX bool wrapupGPU();
00272     IMPEX std::string GetHuginVersion();
00274     IMPEX std::string GetICCDesc(const vigra::ImageImportInfo::ICCProfile& iccProfile);
00275     IMPEX std::string GetICCDesc(const cmsHPROFILE& profile);
00276 
00277 } // namespace
00278 
00279 
00280 #endif // _HUGIN_UTILS_UTILS_H

Generated on 29 Jul 2015 for Hugintrunk by  doxygen 1.4.7