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 occurred, but can be handled by the same function
00074 #define DEBUG_WARN(msg) { std::cerr << "WARN: " << DEBUG_HEADER << msg << std::endl; }
00075 // an error occurred, might be handled by a calling function
00076 #define DEBUG_ERROR(msg) { std::cerr << "ERROR: " << DEBUG_HEADER << msg << std::endl; }
00077 // a fatal error occurred. 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 #define UTILS_THROW(class, msg)  { std::stringstream o; o <<  msg; throw(class(o.str().c_str())); };
00084 
00085 
00086 
00087 namespace hugin_utils
00088 {
00089     
00091     IMPEX std::string GetCurrentTimeString();
00092 
00100     IMPEX std::string doubleToString(double d, int fractionaldigits=-1);
00101 
00113     template <typename STR>
00114     bool stringToDouble(const STR & str_, double & dest)
00115     {
00116         double res=0;
00117         // set numeric locale to C, for correct number output
00118         char * old_locale = setlocale(LC_NUMERIC,NULL);
00119         old_locale = strdup(old_locale);
00120         setlocale(LC_NUMERIC,"C");
00121 
00122         STR str(str_);
00123         // replace all kommas with points, independant of the locale..
00124         for (typename STR::iterator it = str.begin(); it != str.end(); ++it) {
00125             if (*it == ',') {
00126                 *it = '.';
00127             }
00128         }
00129 
00130         const char * p = str.c_str();
00131         char * pe=0;
00132         res = strtod(p,&pe);
00133 
00134         // reset locale
00135         setlocale(LC_NUMERIC,old_locale);
00136         free(old_locale);
00137 
00138         if (pe == p) {
00139             // conversion failed.
00140             DEBUG_DEBUG("conversion failed: " << str << " to:" << dest);
00141             return false;
00142         } else {
00143             // conversion ok.
00144             dest = res;
00145     //        DEBUG_DEBUG("converted: " << str << " to:" << dest);
00146             return true;
00147         }
00148     }
00149 
00151     IMPEX bool stringToInt(const std::string& s, int& val);
00152 
00154     IMPEX bool stringToUInt(const std::string& s, unsigned int& val);
00155 
00157     IMPEX std::string getPathPrefix(const std::string & filename);
00158 
00160     IMPEX std::string getExtension(const std::string & basename);
00161 
00165     IMPEX std::string stripPath(const std::string & filename);
00166 
00168     IMPEX std::string stripExtension(const std::string & basename);
00169 
00171     IMPEX std::string StrTrim(const std::string& str);
00172 
00174     IMPEX std::vector<std::string> SplitString(const std::string& s, const std::string& sep);
00175 
00177     IMPEX void ReplaceAll(std::string& s, const std::string& oldChar, char newChar);
00178 
00179     template <class str>
00180     str QuoteStringInternal(const str & arg, const str & quotechar,
00181                             const str & replacements)
00182     {
00183         // loop over all chars..
00184         str ret(arg);
00185         size_t len = replacements.size();
00186         for (size_t i = 0; i < len; i++) {
00187             str source(replacements.substr(i,1));
00188             str dest(quotechar + source);
00189             size_t idx = 0;
00190             do {
00191                 idx = ret.find(source,idx);
00192                 if (idx != str::npos) {
00193                     ret.replace(idx, 1, dest);
00194                     // skip to next unknown char.
00195                     idx += 2;
00196                 }
00197             } while (idx != str::npos);
00198         }
00199         return ret;
00200     }
00201 
00202     IMPEX void ControlPointErrorColour(const double cperr, 
00203         double &r,double &g, double &b);
00204 
00206     IMPEX bool FileExists(const std::string& filename);
00207 
00209     IMPEX std::string GetAbsoluteFilename(const std::string& filename);
00210 
00212     IMPEX std::string GetDataDir();
00213 
00217     IMPEX std::string GetUserAppDataDir();
00218 
00223     IMPEX bool initGPU(int *argcp, char **argv);
00225     IMPEX bool wrapupGPU();
00227     IMPEX std::string GetHuginVersion();
00229     IMPEX std::string GetICCDesc(const vigra::ImageImportInfo::ICCProfile& iccProfile);
00230     IMPEX std::string GetICCDesc(const cmsHPROFILE& profile);
00231 
00232 } // namespace
00233 
00234 
00235 #endif // _HUGIN_UTILS_UTILS_H

Generated on 26 Sep 2016 for Hugintrunk by  doxygen 1.4.7