36 #ifndef CPPUTILS_OBJECT_H
37 #define CPPUTILS_OBJECT_H
102 virtual int class_size()
const
108 static const string getOpSys();
114 static string stringReplaceAll(
const string& sf,
const string& sr,
120 static void removeEOL(
string& s);
126 static void addPathSeparator(
string& path);
132 static string insertPathSeparator(
const string& dir,
const string& name);
138 static void removePathSeparator(
string& path);
144 static string itos(
int i,
const string& frmt =
"%d");
145 static string ltos(
LONG_INT l,
const string& frmt =
"%llu");
146 static string ftos(
float f,
const string& frmt =
"%.6f");
147 static string dtos(
double d,
const string& frmt =
"%.14f");
148 static string btos(
bool b);
154 static int stoi(
const string& i,
const string& frmt =
"%d");
155 static LONG_INT stol(
const string& i64,
const string& frmt =
"%llu");
156 static float stof(
const string& f,
const string& frmt =
"%f");
157 static double stod(
const string& d,
const string& frmt =
"%lf");
158 static bool stob(
const string& b);
165 static string trim(
const string& str,
166 const string& delim =
" \t");
167 static string trimLeft(
const string& str,
168 const string& delim =
" \t");
169 static string trimRight(
const string& str,
170 const string& delim =
" \t");
187 static void getProperties(
const string& str,
188 map<string, string>& props);
202 static bool getProperty(
const map<string, string>& props,
203 const string& tag,
string& value);
212 static void tokenizeString(
const string& str,
const string& delim,
213 vector<string>& tokens);
219 static string lowercase_string(
const string& str);
220 static string uppercase_string(
const string& str);
225 template <
typename T>
226 static void minmax(
const vector<T>& v, T& mn, T& mx);
231 static double toDegrees(
double a);
236 static double toRadians(
double a);
241 template <
typename T>
242 static T** new2DArrayOfArrays(
int ni,
int nj)
245 for (
int i = 0; i < ni; ++i)
257 template <
typename T>
258 static T** new2DArray(
int ni,
int nj)
261 a[0] =
new T [ni*nj];
262 for (
int i=1; i<ni; ++i) a[i] = &a[0][i*nj];
274 template <
typename T>
275 static T*** new3DArray(
int ni,
int nj,
int nk)
277 T*** a =
new T** [ni];
278 a[0] =
new T* [ni*nj];
279 a[0][0] =
new T [ni*nj*nk];
280 for (
int i = 0; i < ni; ++i)
283 for (
int j = 0; j < nj; ++j)
284 a[i][j] = &a[0][0][(i*nj + j)*nk];
293 template <
typename T>
294 static void delete2DArray(T**& a);
299 template <
typename T>
300 static void delete2DArrayOfArrays(T**& a,
int ni)
304 for (
int i = 0; i < ni; ++i)
delete [] a[i];
313 template <
typename T>
314 static void delete3DArray(T***& a);
319 template <
typename T>
320 static void resetArray(
int n, T* array, T val)
322 for (
int i = 0; i < n; ++i) array[i] = val;
328 template <
typename T>
329 static T* copyArray(T* a,
int n)
332 for (
int i=0; i<n; ++i)
340 static bool isBigEndian();
345 static bool isint(
const string& i);
377 inline double CPPUtils::toDegrees(
double a)
379 return RAD_TO_DEG * a;
387 inline double CPPUtils::toRadians(
double a)
389 return DEG_TO_RAD * a;
395 template <
typename T>
396 inline void CPPUtils::delete2DArray(T**& a)
409 template <
typename T>
410 inline void CPPUtils::delete3DArray(T***& a)
424 inline void CPPUtils::removeEOL(
string& s)
426 if (s.size() && (s[s.length() - 1] ==
'\n')) s.erase(s.length() - 1);
427 if (s.size() && (s[s.length() - 1] ==
'\r')) s.erase(s.length() - 1);
430 inline void CPPUtils::removePathSeparator(
string& s)
432 if (s.size() && (s[s.length() - 1] == CPPUtils::FILE_SEP)) s.erase(s.length() - 1);
435 inline void CPPUtils::addPathSeparator(
string& s)
437 if ( s.find_last_of(CPPUtils::FILE_SEP) != s.length() - 1 )
438 s += CPPUtils::FILE_SEP;
441 inline string CPPUtils::insertPathSeparator(
const string& dir,
const string& name)
446 while (path.size() && (path[path.length() - 1] == CPPUtils::FILE_SEP))
447 path.erase(path.length() - 1);
449 if (path.length() > 0)
450 path = path+CPPUtils::FILE_SEP;
453 while (nm.size() && (nm[0] == CPPUtils::FILE_SEP))
462 template <
typename T>
463 void CPPUtils::minmax(
const vector<T>& v, T& mn, T& mx)
475 for (
int i = 1; i < v.size(); i++)
477 if (mn > v[i]) mn = v[i];
478 if (mx < v[i]) mx = v[i];
485 #endif // CPPUTILS_OBJECT_H