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 // remark:
00061 // on unix_like systems don't use CurrentTime, this is defined as a macro in X.h and breaks the debug messages
00062 // on windows we can't use GetCurrentTime because this is replaced with GetTickCount
00063 
00064 #ifdef __GNUC__
00065     // the full function name is too long..
00066 //  #define DEBUG_HEADER hugin_utils::CurrentTime() <<" (" << __FILE__ << ":" << __LINE__ << ") "  << __PRETTY_FUNCTION__ << "()" << std::endl << "    "
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     // debug trace
00077     #define DEBUG_TRACE(msg) { std::cerr << "TRACE " << DEBUG_HEADER << msg << std::endl; }
00078     // low level debug info
00079     #define DEBUG_DEBUG(msg) { std::cerr << "DEBUG " << DEBUG_HEADER << msg << std::endl; }
00080     // informational debug message,
00081     #define DEBUG_INFO(msg) { std::cerr << "INFO " << DEBUG_HEADER << msg << std::endl; }
00082     // major change/operation should use this
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 // when an error occured, but can be handled by the same function
00092 #define DEBUG_WARN(msg) { std::cerr << "WARN: " << DEBUG_HEADER << msg << std::endl; }
00093 // an error occured, might be handled by a calling function
00094 #define DEBUG_ERROR(msg) { std::cerr << "ERROR: " << DEBUG_HEADER << msg << std::endl; }
00095 // a fatal error occured. further program execution is unlikely
00096 #define DEBUG_FATAL(msg) { std::cerr << "FATAL: " << DEBUG_HEADER << "(): " << msg << std::endl; }
00097 // C-style assertion
00098 #define DEBUG_ASSERT(cond) assert(cond)
00099 
00100 
00101 // use trace function under windows, because usually there is
00102 // no stdout under windows
00103 #ifdef __WXMSW__
00104     #ifdef DEBUG
00105         #undef DEBUG_TRACE
00106         #undef DEBUG_DEBUG
00107         #undef DEBUG_INFO
00108         #undef DEBUG_NOTICE
00109 
00110         // debug trace
00111 //      #define DEBUG_TRACE(msg) { std::stringstream o; o << "TRACE " << DEBUG_HEADER << msg; wxLogDebug(o.str().c_str());}
00112         #define DEBUG_TRACE(msg) { std::cerr << "TRACE " << DEBUG_HEADER << msg << std::endl; }
00113         // low level debug info
00114 //      #define DEBUG_DEBUG(msg) { std::stringstream o; o << "DEBUG " << DEBUG_HEADER << msg; wxLogDebug(o.str().c_str()); }
00115         #define DEBUG_DEBUG(msg) { std::cerr << "DEBUG " << DEBUG_HEADER << msg << std::endl; }
00116         // informational debug message,
00117 //      #define DEBUG_INFO(msg) { std::stringstream o; o << "INFO " << DEBUG_HEADER << msg; wxLogDebug(o.str().c_str()); }
00118         #define DEBUG_INFO(msg) { std::cerr << "INFO " << DEBUG_HEADER << msg << std::endl; }
00119         // major change/operation should use this
00120 //      #define DEBUG_NOTICE(msg) { std::stringstream o; o << "NOTICE " << DEBUG_HEADER << msg; wxLogMessage(o.str().c_str()); }
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     // when an error occured, but can be handled by the same function
00130     #define DEBUG_WARN(msg) { std::stringstream o; o << "WARN: " << DEBUG_HEADER << msg; wxLogWarning(wxString(o.str().c_str(), wxConvISO8859_1));}
00131     // an error occured, might be handled by a calling function
00132     #define DEBUG_ERROR(msg) { std::stringstream o; o << "ERROR: " << DEBUG_HEADER << msg; wxLogError(wxString(o.str().c_str(),wxConvISO8859_1));}
00133     // a fatal error occured. further program execution is unlikely
00134     #define DEBUG_FATAL(msg) { std::stringstream o; o << "FATAL: " << DEBUG_HEADER << "(): " << msg; wxLogError(wxString(o.str().c_str(),wxConvISO8859_1)); }
00135     // assertion
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         // set numeric locale to C, for correct number output
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         // replace all kommas with points, independant of the locale..
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         // reset locale
00198         setlocale(LC_NUMERIC,old_locale);
00199         free(old_locale);
00200 
00201         if (pe == p) {
00202             // conversion failed.
00203             DEBUG_DEBUG("conversion failed: " << str << " to:" << dest);
00204             return false;
00205         } else {
00206             // conversion ok.
00207             dest = res;
00208     //        DEBUG_DEBUG("converted: " << str << " to:" << dest);
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             // cast error.  handle it somehow
00240             // boost guys throw an exception here
00241         };
00242 
00243         return result;
00244 
00245     }; // lexical cast
00246 
00247 
00248     template <class str>
00249     str QuoteStringInternal(const str & arg, const str & quotechar,
00250                             const str & replacements)
00251     {
00252         // loop over all chars..
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                     // skip to next unknown char.
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 } // namespace
00296 
00297 
00298 #endif // _HUGIN_UTILS_UTILS_H

Generated on Sat Jul 26 01:25:38 2014 for Hugintrunk by  doxygen 1.3.9.1