36 #ifndef CPPUTILS_OBJECT_H
37 #define CPPUTILS_OBJECT_H
103 virtual int class_size()
const
109 static const string getOpSys();
115 static string stringReplaceAll(
const string& sf,
const string& sr,
121 static void removeEOL(
string& s);
127 static void addPathSeparator(
string& path);
133 static string insertPathSeparator(
const string& dir,
const string& name);
139 static void removePathSeparator(
string& path);
145 static string itos(
int i,
const string& frmt =
"%d");
146 static string ltos(
LONG_INT l,
const string& frmt =
"%llu");
147 static string ftos(
float f,
const string& frmt =
"%.6f");
148 static string dtos(
double d,
const string& frmt =
"%.14f");
149 static string btos(
bool b);
155 static int stoi(
const string& i,
const string& frmt =
"%d");
156 static LONG_INT stol(
const string& i64,
const string& frmt =
"%llu");
157 static float stof(
const string& f,
const string& frmt =
"%f");
158 static double stod(
const string& d,
const string& frmt =
"%lf");
159 static bool stob(
const string& b);
166 static string trim(
const string& str,
167 const string& delim =
" \t");
168 static string trimLeft(
const string& str,
169 const string& delim =
" \t");
170 static string trimRight(
const string& str,
171 const string& delim =
" \t");
188 static void getProperties(
const string& str,
189 map<string, string>& props);
203 static bool getProperty(
const map<string, string>& props,
204 const string& tag,
string& value);
213 static void tokenizeString(
const string& str,
const string& delim,
214 vector<string>& tokens);
220 static string lowercase_string(
const string& str);
221 static string uppercase_string(
const string& str);
226 template <
typename T>
227 static void minmax(
const vector<T>& v, T& mn, T& mx);
232 static double toDegrees(
double a);
237 static double toRadians(
double a);
242 template <
typename T>
243 static T** new2DArrayOfArrays(
int ni,
int nj)
246 for (
int i = 0; i < ni; ++i)
258 template <
typename T>
259 static T** new2DArray(
int ni,
int nj)
262 a[0] =
new T [ni*nj];
263 for (
int i=1; i<ni; ++i) a[i] = &a[0][i*nj];
275 template <
typename T>
276 static T*** new3DArray(
int ni,
int nj,
int nk)
278 T*** a =
new T** [ni];
279 a[0] =
new T* [ni*nj];
280 a[0][0] =
new T [ni*nj*nk];
281 for (
int i = 0; i < ni; ++i)
284 for (
int j = 0; j < nj; ++j)
285 a[i][j] = &a[0][0][(i*nj + j)*nk];
294 template <
typename T>
295 static void delete2DArray(T**& a);
300 template <
typename T>
301 static void delete2DArrayOfArrays(T**& a,
int ni)
305 for (
int i = 0; i < ni; ++i)
delete [] a[i];
314 template <
typename T>
315 static void delete3DArray(T***& a);
320 template <
typename T>
321 static void resetArray(
int n, T* array, T val)
323 for (
int i = 0; i < n; ++i) array[i] = val;
329 template <
typename T>
330 static T* copyArray(T* a,
int n)
333 for (
int i=0; i<n; ++i)
341 static bool isBigEndian();
346 static bool isint(
const string& i);
378 inline double CPPUtils::toDegrees(
double a)
380 return RAD_TO_DEG * a;
388 inline double CPPUtils::toRadians(
double a)
390 return DEG_TO_RAD * a;
396 template <
typename T>
397 inline void CPPUtils::delete2DArray(T**& a)
410 template <
typename T>
411 inline void CPPUtils::delete3DArray(T***& a)
425 inline void CPPUtils::removeEOL(
string& s)
427 if (s.size() && (s[s.length() - 1] ==
'\n')) s.erase(s.length() - 1);
428 if (s.size() && (s[s.length() - 1] ==
'\r')) s.erase(s.length() - 1);
431 inline void CPPUtils::removePathSeparator(
string& s)
433 if (s.size() && (s[s.length() - 1] == CPPUtils::FILE_SEP)) s.erase(s.length() - 1);
436 inline void CPPUtils::addPathSeparator(
string& s)
438 if ( s.find_last_of(CPPUtils::FILE_SEP) != s.length() - 1 )
439 s += CPPUtils::FILE_SEP;
442 inline string CPPUtils::insertPathSeparator(
const string& dir,
const string& name)
447 while (path.size() && (path[path.length() - 1] == CPPUtils::FILE_SEP))
448 path.erase(path.length() - 1);
450 if (path.length() > 0)
451 path = path+CPPUtils::FILE_SEP;
454 while (nm.size() && (nm[0] == CPPUtils::FILE_SEP))
455 nm.erase((
unsigned)0);
463 template <
typename T>
464 void CPPUtils::minmax(
const vector<T>& v, T& mn, T& mx)
476 for (
int i = 1; i < v.size(); i++)
478 if (mn > v[i]) mn = v[i];
479 if (mx < v[i]) mx = v[i];
486 #endif // CPPUTILS_OBJECT_H