[da792d]: src / hugin_base / hugin_utils / utils.h  Maximize  Restore  History

Download this file

285 lines (236 with data), 9.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// -*- c-basic-offset: 4 -*-
/** @file utils.h
*
* @author Pablo d'Angelo <pablo.dangelo@web.de>
*
* $Id: utils.h 1952 2007-04-15 20:57:55Z dangelo $
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef _HUGIN_UTILS_UTILS_H
#define _HUGIN_UTILS_UTILS_H
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <cassert>
#include <hugin_utils/platform.h>
#if 0
// ??????????????????????????????????????????
#ifdef __WXMSW__
// has to be included before!
#include <wx/log.h>
#endif
#ifdef WIN32
#define snprintf _snprintf
#endif
#ifdef __WXMSW__
// has to be included before!
#include <wx/log.h>
#define snprintf _snprintf
#endif
// ??????????????????????????????????????????
#endif
// misc utility functions / macros
#ifdef __GNUC__
// the full function name is too long..
// #define DEBUG_HEADER hugin_utils::CurrentTime() <<" (" << __FILE__ << ":" << __LINE__ << ") " << __PRETTY_FUNCTION__ << "()" << std::endl << " "
#define DEBUG_HEADER hugin_utils::CurrentTime() <<" (" << __FILE__ << ":" << __LINE__ << ") " << __func__ << "(): "
#elif _MSC_VER > 1300
#define DEBUG_HEADER hugin_utils::CurrentTime() <<" (" << __FILE__ << ":" << __LINE__ << ") " << __FUNCTION__ << "(): "
#else
#define DEBUG_HEADER hugin_utils::CurrentTime() <<" (" << __FILE__ << ":" << __LINE__ << ") " << __func__ << "(): "
#endif
#ifdef DEBUG
// debug trace
#define DEBUG_TRACE(msg) { std::cerr << "TRACE " << DEBUG_HEADER << msg << std::endl; }
// low level debug info
#define DEBUG_DEBUG(msg) { std::cerr << "DEBUG " << DEBUG_HEADER << msg << std::endl; }
// informational debug message,
#define DEBUG_INFO(msg) { std::cerr << "INFO " << DEBUG_HEADER << msg << std::endl; }
// major change/operation should use this
#define DEBUG_NOTICE(msg) { std::cerr << "NOTICE " << DEBUG_HEADER << msg << std::endl; }
#else
#define DEBUG_TRACE(msg)
#define DEBUG_DEBUG(msg)
#define DEBUG_INFO(msg)
#define DEBUG_NOTICE(msg)
#endif
// when an error occured, but can be handled by the same function
#define DEBUG_WARN(msg) { std::cerr << "WARN: " << DEBUG_HEADER << msg << std::endl; }
// an error occured, might be handled by a calling function
#define DEBUG_ERROR(msg) { std::cerr << "ERROR: " << DEBUG_HEADER << msg << std::endl; }
// a fatal error occured. further program execution is unlikely
#define DEBUG_FATAL(msg) { std::cerr << "FATAL: " << DEBUG_HEADER << "(): " << msg << std::endl; }
// C-style assertion
#define DEBUG_ASSERT(cond) assert(cond)
// use trace function under windows, because usually there is
// no stdout under windows
#ifdef __WXMSW__
#ifdef DEBUG
#undef DEBUG_TRACE
#undef DEBUG_DEBUG
#undef DEBUG_INFO
#undef DEBUG_NOTICE
// debug trace
// #define DEBUG_TRACE(msg) { std::stringstream o; o << "TRACE " << DEBUG_HEADER << msg; wxLogDebug(o.str().c_str());}
#define DEBUG_TRACE(msg) { std::cerr << "TRACE " << DEBUG_HEADER << msg << std::endl; }
// low level debug info
// #define DEBUG_DEBUG(msg) { std::stringstream o; o << "DEBUG " << DEBUG_HEADER << msg; wxLogDebug(o.str().c_str()); }
#define DEBUG_DEBUG(msg) { std::cerr << "DEBUG " << DEBUG_HEADER << msg << std::endl; }
// informational debug message,
// #define DEBUG_INFO(msg) { std::stringstream o; o << "INFO " << DEBUG_HEADER << msg; wxLogDebug(o.str().c_str()); }
#define DEBUG_INFO(msg) { std::cerr << "INFO " << DEBUG_HEADER << msg << std::endl; }
// major change/operation should use this
// #define DEBUG_NOTICE(msg) { std::stringstream o; o << "NOTICE " << DEBUG_HEADER << msg; wxLogMessage(o.str().c_str()); }
#define DEBUG_NOTICE(msg) { std::cerr << "NOTICE " << DEBUG_HEADER << msg << std::endl; }
#endif
#undef DEBUG_WARN
#undef DEBUG_ERROR
#undef DEBUG_FATAL
#undef DEBUG_ASSERT
// when an error occured, but can be handled by the same function
#define DEBUG_WARN(msg) { std::stringstream o; o << "WARN: " << DEBUG_HEADER << msg; wxLogWarning(wxString(o.str().c_str(), wxConvISO8859_1));}
// an error occured, might be handled by a calling function
#define DEBUG_ERROR(msg) { std::stringstream o; o << "ERROR: " << DEBUG_HEADER << msg; wxLogError(wxString(o.str().c_str(),wxConvISO8859_1));}
// a fatal error occured. further program execution is unlikely
#define DEBUG_FATAL(msg) { std::stringstream o; o << "FATAL: " << DEBUG_HEADER << "(): " << msg; wxLogError(wxString(o.str().c_str(),wxConvISO8859_1)); }
// assertion
#define DEBUG_ASSERT(cond) \
do { \
if (!(cond)) { \
std::stringstream o; o << "ASSERTATION: " << DEBUG_HEADER << "(): " << #cond; \
wxLogFatalError(wxString(o.str().c_str(),wxConvISO8859_1)); \
} \
} while(0)
#endif
//
#define UTILS_THROW(class, msg) { std::stringstream o; o << msg; throw(class(o.str().c_str())); };
namespace hugin_utils
{
/** current time as a string */
std::string CurrentTime();
/** convert a double to a string, suitable for display
* within a GUI.
*
* @p d value to convert t a string
* @p fractionaldigits number of fractional digits.
* -1: not specified, use default.
*/
std::string doubleToString(double d, int fractionaldigits=-1);
/** convert a string to a double, ignore localisation.
* always accept both.
*
* sets \p dest to the new value, and returns true
* if it could be read.
*
* it the conversion fails, returns false and does not
* modify \p dest.
*
* @return success
*/
template <typename STR>
bool stringToDouble(const STR & str_, double & dest)
{
double res=0;
// set numeric locale to C, for correct number output
char * old_locale = setlocale(LC_NUMERIC,NULL);
old_locale = strdup(old_locale);
setlocale(LC_NUMERIC,"C");
STR str(str_);
// replace all kommas with points, independant of the locale..
for (typename STR::iterator it = str.begin(); it != str.end(); ++it) {
if (*it == ',') {
*it = '.';
}
}
const char * p = str.c_str();
char * pe=0;
res = strtod(p,&pe);
// reset locale
setlocale(LC_NUMERIC,old_locale);
free(old_locale);
if (pe == p) {
// conversion failed.
DEBUG_DEBUG("conversion failed: " << str << " to:" << dest);
return false;
} else {
// conversion ok.
dest = res;
// DEBUG_DEBUG("converted: " << str << " to:" << dest);
return true;
}
}
/** Get the path to a filename */
std::string getPathPrefix(const std::string & filename);
/** Get extension of a filename */
std::string getExtension(const std::string & basename);
/** remove the path of a filename (mainly useful for gui
* display of filenames)
*/
std::string stripPath(const std::string & filename);
/** remove extension of a filename */
std::string stripExtension(const std::string & basename);
template <typename Target, typename Source>
Target lexical_cast(Source arg) {
std::stringstream interpreter;
Target result;
if (!(interpreter << arg) ||
!(interpreter >> result) ||
!(interpreter >> std::ws).eof()) {
DEBUG_ERROR("lexical cast error");
// cast error. handle it somehow
// boost guys throw an exception here
};
return result;
}; // lexical cast
template <class str>
str QuoteStringInternal(const str & arg, const str & quotechar,
const str & replacements)
{
// loop over all chars..
str ret(arg);
size_t len = replacements.size();
for (size_t i = 0; i < len; i++) {
str source(replacements.substr(i,1));
str dest(quotechar + source);
size_t idx = 0;
do {
idx = ret.find(source,idx);
if (idx != str::npos) {
ret.replace(idx, 1, dest);
// skip to next unknown char.
idx += 2;
}
} while (idx != str::npos);
}
return ret;
}
///
template <class str>
str replaceAll(const str& arg, const str& target, const str& replacement)
{
str ret(arg);
typename str::size_type pos = ret.find(target, 0);
for ( typename str::size_type n = 0 ; pos != str::npos ; pos = ret.find(target, n) )
{
ret.replace(pos, target.size(), replacement);
n = pos + replacement.size();
}
return ret;
}
} // namespace
#endif // _HUGIN_UTILS_UTILS_H