Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
utils.h
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
24 #ifndef _HUGIN_UTILS_UTILS_H
25 #define _HUGIN_UTILS_UTILS_H
26 
27 #include <hugin_shared.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <string>
31 #include <vector>
32 #include <iostream>
33 #include <sstream>
34 #include <cassert>
35 #include <vigra/imageinfo.hxx>
36 #include <lcms2.h>
37 
38 #include <hugin_utils/platform.h>
39 
40 // misc utility functions / macros
41 
42 // remark:
43 // on unix_like systems don't use CurrentTime, this is defined as a macro in X.h and breaks the debug messages
44 // on windows we can't use GetCurrentTime because this is replaced with GetTickCount
45 
46 #ifdef __GNUC__
47  // the full function name is too long..
48 // #define DEBUG_HEADER hugin_utils::CurrentTime() <<" (" << __FILE__ << ":" << __LINE__ << ") " << __PRETTY_FUNCTION__ << "()" << std::endl << " "
49  #define DEBUG_HEADER hugin_utils::GetCurrentTimeString() <<" (" << __FILE__ << ":" << __LINE__ << ") " << __func__ << "(): "
50 #elif _MSC_VER > 1300
51  #define DEBUG_HEADER hugin_utils::GetCurrentTimeString() <<" (" << __FILE__ << ":" << __LINE__ << ") " << __FUNCTION__ << "(): "
52 #else
53  #define DEBUG_HEADER hugin_utils::GetCurrentTimeString() <<" (" << __FILE__ << ":" << __LINE__ << ") " << __func__ << "(): "
54 #endif
55 
56 
57 #ifdef DEBUG
58  // debug trace
59  #define DEBUG_TRACE(msg) { std::cerr << "TRACE " << DEBUG_HEADER << msg << std::endl; }
60  // low level debug info
61  #define DEBUG_DEBUG(msg) { std::cerr << "DEBUG " << DEBUG_HEADER << msg << std::endl; }
62  // informational debug message,
63  #define DEBUG_INFO(msg) { std::cerr << "INFO " << DEBUG_HEADER << msg << std::endl; }
64  // major change/operation should use this
65  #define DEBUG_NOTICE(msg) { std::cerr << "NOTICE " << DEBUG_HEADER << msg << std::endl; }
66 #else
67  #define DEBUG_TRACE(msg)
68  #define DEBUG_DEBUG(msg)
69  #define DEBUG_INFO(msg)
70  #define DEBUG_NOTICE(msg)
71 #endif
72 
73 // when an error occurred, but can be handled by the same function
74 #define DEBUG_WARN(msg) { std::cerr << "WARN: " << DEBUG_HEADER << msg << std::endl; }
75 // an error occurred, might be handled by a calling function
76 #define DEBUG_ERROR(msg) { std::cerr << "ERROR: " << DEBUG_HEADER << msg << std::endl; }
77 // a fatal error occurred. further program execution is unlikely
78 #define DEBUG_FATAL(msg) { std::cerr << "FATAL: " << DEBUG_HEADER << "(): " << msg << std::endl; }
79 // C-style assertion
80 #define DEBUG_ASSERT(cond) assert(cond)
81 
82 //
83 #define UTILS_THROW(class, msg) { std::stringstream o; o << msg; throw(class(o.str().c_str())); };
84 
85 
86 
87 namespace hugin_utils
88 {
89 
91  IMPEX std::string GetCurrentTimeString();
92 
100  IMPEX std::string doubleToString(double d, int fractionaldigits=-1);
101 
113  template <typename STR>
114  bool stringToDouble(const STR & str_, double & dest)
115  {
116  double res=0;
117  // set numeric locale to C, for correct number output
118  char * old_locale = setlocale(LC_NUMERIC,NULL);
119  old_locale = strdup(old_locale);
120  setlocale(LC_NUMERIC,"C");
121 
122  STR str(str_);
123  // replace all kommas with points, independant of the locale..
124  for (typename STR::iterator it = str.begin(); it != str.end(); ++it) {
125  if (*it == ',') {
126  *it = '.';
127  }
128  }
129 
130  const char * p = str.c_str();
131  char * pe=0;
132  res = strtod(p,&pe);
133 
134  // reset locale
135  setlocale(LC_NUMERIC,old_locale);
136  free(old_locale);
137 
138  if (pe == p) {
139  // conversion failed.
140  DEBUG_DEBUG("conversion failed: " << str << " to:" << dest);
141  return false;
142  } else {
143  // conversion ok.
144  dest = res;
145  // DEBUG_DEBUG("converted: " << str << " to:" << dest);
146  return true;
147  }
148  }
149 
151  IMPEX bool stringToInt(const std::string& s, int& val);
152 
154  IMPEX bool stringToUInt(const std::string& s, unsigned int& val);
155 
157  IMPEX std::string getPathPrefix(const std::string & filename);
158 
160  IMPEX std::string getExtension(const std::string & basename);
161 
165  IMPEX std::string stripPath(const std::string & filename);
166 
168  IMPEX std::string stripExtension(const std::string & basename);
169 
171  IMPEX std::string StrTrim(const std::string& str);
172 
174  IMPEX std::vector<std::string> SplitString(const std::string& s, const std::string& sep);
175 
177  IMPEX void ReplaceAll(std::string& s, const std::string& oldChar, char newChar);
178 
180  IMPEX bool StringContainsCaseInsensitive(const std::string& s1, const std::string& s2);
181 
182  template <class str>
183  str QuoteStringInternal(const str & arg, const str & quotechar,
184  const str & replacements)
185  {
186  // loop over all chars..
187  str ret(arg);
188  size_t len = replacements.size();
189  for (size_t i = 0; i < len; i++) {
190  str source(replacements.substr(i,1));
191  str dest(quotechar + source);
192  size_t idx = 0;
193  do {
194  idx = ret.find(source,idx);
195  if (idx != str::npos) {
196  ret.replace(idx, 1, dest);
197  // skip to next unknown char.
198  idx += 2;
199  }
200  } while (idx != str::npos);
201  }
202  return ret;
203  }
204 
205  IMPEX void ControlPointErrorColour(const double cperr,
206  double &r,double &g, double &b);
207 
209  IMPEX bool FileExists(const std::string& filename);
210 
212  IMPEX std::string GetAbsoluteFilename(const std::string& filename);
213 
215  IMPEX bool IsFileTypeSupported(const std::string& filename);
216 
218  IMPEX void EnforceExtension(std::string& filename, const std::string& defaultExtension);
219 
222  IMPEX std::string GetOutputFilename(const std::string& out, const std::string& in, const std::string& suffix);
223 
225  IMPEX std::string GetDataDir();
226 
230  IMPEX std::string GetUserAppDataDir();
231 
236  IMPEX bool initGPU(int *argcp, char **argv);
238  IMPEX bool wrapupGPU();
240  IMPEX std::string GetHuginVersion();
242  IMPEX std::string GetICCDesc(const vigra::ImageImportInfo::ICCProfile& iccProfile);
243  IMPEX std::string GetICCDesc(const cmsHPROFILE& profile);
244 
246  IMPEX std::vector<std::string> GetRawExtensions();
248  IMPEX bool IsRawExtension(const std::string testExt);
249 
250 } // namespace
251 
252 
253 #endif // _HUGIN_UTILS_UTILS_H
std::string GetICCDesc(const vigra::ImageImportInfo::ICCProfile &iccProfile)
returns description of given icc profile
Definition: utils.cpp:925
str QuoteStringInternal(const str &arg, const str &quotechar, const str &replacements)
Definition: utils.h:183
bool wrapupGPU()
cleanup GPU settings
Definition: utils.cpp:914
bool FileExists(const std::string &filename)
checks if file exists
Definition: utils.cpp:362
std::string StrTrim(const std::string &str)
remove trailing and leading white spaces and tabs
Definition: utils.cpp:208
std::string GetCurrentTimeString()
current time as a string
Definition: utils.cpp:91
std::string GetOutputFilename(const std::string &out, const std::string &in, const std::string &suffix)
construct output filename, if ouput is known return this value otherwise use the input filename and a...
Definition: utils.cpp:420
void EnforceExtension(std::string &filename, const std::string &defaultExtension)
check if filename contains extension, if not add default extension
Definition: utils.cpp:411
std::string doubleToString(double d, int digits)
convert a double to a string, suitable for display within a GUI.
Definition: utils.cpp:228
bool stringToUInt(const std::string &s, unsigned int &val)
convert string to unsigned integer value, returns true, if sucessful
Definition: utils.cpp:280
std::string GetDataDir()
returns the full path to the data directory
Definition: utils.cpp:441
std::string getPathPrefix(const std::string &filename)
Get the path to a filename.
Definition: utils.cpp:184
std::string GetAbsoluteFilename(const std::string &filename)
returns the full absolute filename
Definition: utils.cpp:368
bool IsFileTypeSupported(const std::string &filename)
return true, if file type by extension is supported by vigra
Definition: utils.cpp:405
std::string getExtension(const std::string &basename2)
Get extension of a filename.
Definition: utils.cpp:99
platform/compiler specific stuff.
std::string stripExtension(const std::string &basename2)
remove extension of a filename
Definition: utils.cpp:130
bool stringToInt(const std::string &s, int &val)
convert string to integer value, returns true, if sucessful
Definition: utils.cpp:264
bool stringToDouble(const STR &str_, double &dest)
convert a string to a double, ignore localisation.
Definition: utils.h:114
bool initGPU(int *argcp, char **argv)
Try to initalise GLUT and GLEW, and create an OpenGL context for GPU stitching.
Definition: utils.cpp:858
#define IMPEX
Definition: hugin_shared.h:39
bool StringContainsCaseInsensitive(const std::string &s1, const std::string &s2)
check if s1 contains s2 using case insensitive comparison
Definition: utils.cpp:335
void ReplaceAll(std::string &s, const std::string &oldChar, char newChar)
replace all characters oldChar in s with newChar
Definition: utils.cpp:325
std::vector< std::string > GetRawExtensions()
return vector of known extensions of raw files, all lower case
Definition: utils.cpp:953
#define DEBUG_DEBUG(msg)
Definition: utils.h:68
std::string GetUserAppDataDir()
returns the directory for user specific Hugin settings, e.g.
Definition: utils.cpp:497
void ControlPointErrorColour(const double cperr, double &r, double &g, double &b)
Definition: utils.cpp:341
std::string GetHuginVersion()
return a string with version numbers
Definition: utils.cpp:920
bool IsRawExtension(const std::string testExt)
return true if extension belongs to a raw file
Definition: utils.cpp:959
std::vector< std::string > SplitString(const std::string &s, const std::string &sep)
split string s at given sep, returns vector of strings
Definition: utils.cpp:294
std::string stripPath(const std::string &filename)
remove the path of a filename (mainly useful for gui display of filenames)
Definition: utils.cpp:160