GeoTessCPP  2.0.0
Software to facilitate storage and retrieval of 3D information about the Earth.
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
CPPUtils.h
Go to the documentation of this file.
1 //- ****************************************************************************
2 //-
3 //- Copyright 2009 Sandia Corporation. Under the terms of Contract
4 //- DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
5 //- retains certain rights in this software.
6 //-
7 //- BSD Open Source License.
8 //- All rights reserved.
9 //-
10 //- Redistribution and use in source and binary forms, with or without
11 //- modification, are permitted provided that the following conditions are met:
12 //-
13 //- * Redistributions of source code must retain the above copyright notice,
14 //- this list of conditions and the following disclaimer.
15 //- * Redistributions in binary form must reproduce the above copyright
16 //- notice, this list of conditions and the following disclaimer in the
17 //- documentation and/or other materials provided with the distribution.
18 //- * Neither the name of Sandia National Laboratories nor the names of its
19 //- contributors may be used to endorse or promote products derived from
20 //- this software without specific prior written permission.
21 //-
22 //- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 //- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 //- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 //- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 //- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 //- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 //- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 //- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 //- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 //- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 //- POSSIBILITY OF SUCH DAMAGE.
33 //-
34 //- ****************************************************************************
35 
36 #ifndef CPPUTILS_OBJECT_H
37 #define CPPUTILS_OBJECT_H
38 
39 // **** _SYSTEM INCLUDES_ ******************************************************
40 
41 #include "CPPGlobals.h"
42 
43 // **** # Defines **************************************************************
44 
45 // use standard library objects
46 using namespace std;
47 
48 //--------------------------
49 
50 // **** _LOCAL INCLUDES_ *******************************************************
51 
52 // **** _BEGIN GEOTESS NAMESPACE_ **********************************************
53 
54 namespace geotess {
55 
56 // **** _GLOBAL_REFERENCES_ ****************************************************
57 
58 // **** _FORWARD REFERENCES_ ***************************************************
59 
60 // **** _CLASS DEFINITION_ *****************************************************
61 
69 {
70 private:
71 
75  CPPUtils(const CPPUtils& bs) {};
76 
80  CPPUtils& operator=(const CPPUtils& bs) { return *this; };
81 
82 public:
83 
87  CPPUtils() {};
88 
92  virtual ~CPPUtils() {};
93 
98  static string class_name() { return "CPPUtils"; };
99 
103  virtual int class_size() const
104  { return (int) sizeof(CPPUtils); };
105 
109  static const string getOpSys();
110 
115  static string stringReplaceAll(const string& sf, const string& sr,
116  const string& s);
117 
121  static void removeEOL(string& s);
122 
127  static void addPathSeparator(string& path);
128 
133  static string insertPathSeparator(const string& dir, const string& name);
134 
139  static void removePathSeparator(string& path);
140 
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);
150 
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);
160 
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");
172 
188  static void getProperties(const string& str,
189  map<string, string>& props);
190 
203  static bool getProperty(const map<string, string>& props,
204  const string& tag, string& value);
205 
213  static void tokenizeString(const string& str, const string& delim,
214  vector<string>& tokens);
215 
220  static string lowercase_string(const string& str);
221  static string uppercase_string(const string& str);
222 
226  template <typename T>
227  static void minmax(const vector<T>& v, T& mn, T& mx);
228 
232  static double toDegrees(double a);
233 
237  static double toRadians(double a);
238 
242  template <typename T>
243  static T** new2DArrayOfArrays(int ni, int nj)
244  {
245  T** a = new T* [ni];
246  for (int i = 0; i < ni; ++i)
247  a[i] = new T [nj];
248  return a;
249  }
250 
258  template <typename T>
259  static T** new2DArray(int ni, int nj)
260  {
261  T** a = new T* [ni];
262  a[0] = new T [ni*nj];
263  for (int i=1; i<ni; ++i) a[i] = &a[0][i*nj];
264  return a;
265  }
266 
275  template <typename T>
276  static T*** new3DArray(int ni, int nj, int nk)
277  {
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)
282  {
283  a[i] = &a[0][i*nj];
284  for (int j = 0; j < nj; ++j)
285  a[i][j] = &a[0][0][(i*nj + j)*nk];
286  }
287 
288  return a;
289  }
290 
294  template <typename T>
295  static void delete2DArray(T**& a);
296 
300  template <typename T>
301  static void delete2DArrayOfArrays(T**& a, int ni)
302  {
303  if (a)
304  {
305  for (int i = 0; i < ni; ++i) delete [] a[i];
306  delete [] a;
307  a = NULL;
308  }
309  }
310 
314  template <typename T>
315  static void delete3DArray(T***& a);
316 
320  template <typename T>
321  static void resetArray(int n, T* array, T val)
322  {
323  for (int i = 0; i < n; ++i) array[i] = val;
324  }
325 
329  template <typename T>
330  static T* copyArray(T* a, int n)
331  {
332  T* copy = new T[n];
333  for (int i=0; i<n; ++i)
334  copy[i] = a[i];
335  return copy;
336  }
337 
341  static bool isBigEndian();
342 
346  static bool isint(const string& i);
347 
351  static const int SBOL;
352  static const int SBYT;
353  static const int SSHT;
354  static const int SINT;
355  static const int SLNG;
356  static const int SFLT;
357  static const int SDBL;
358 
362  static char const FILE_SEP;
363 
367  static string const NEWLINE;
368 
369 }; // End class CPPUtils
370 
371 // **** _INLINE FUNCTION IMPLEMENTATIONS_ **************************************
372 
378 inline double CPPUtils::toDegrees(double a)
379 {
380  return RAD_TO_DEG * a;
381 }
382 
388 inline double CPPUtils::toRadians(double a)
389 {
390  return DEG_TO_RAD * a;
391 }
392 
396 template <typename T>
397 inline void CPPUtils::delete2DArray(T**& a)
398 {
399  if (a)
400  {
401  delete [] a[0];
402  delete [] a;
403  a = NULL;
404  }
405 }
406 
410 template <typename T>
411 inline void CPPUtils::delete3DArray(T***& a)
412 {
413  if (a)
414  {
415  delete [] a[0][0];
416  delete [] a[0];
417  delete [] a;
418  a = NULL;
419  }
420 }
421 
425 inline void CPPUtils::removeEOL(string& s)
426 {
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);
429 }
430 
431 inline void CPPUtils::removePathSeparator(string& s)
432 {
433  if (s.size() && (s[s.length() - 1] == CPPUtils::FILE_SEP)) s.erase(s.length() - 1);
434 }
435 
436 inline void CPPUtils::addPathSeparator(string& s)
437 {
438  if ( s.find_last_of(CPPUtils::FILE_SEP) != s.length() - 1 )
439  s += CPPUtils::FILE_SEP;
440 }
441 
442 inline string CPPUtils::insertPathSeparator(const string& dir, const string& name)
443 {
444 
445  string path = dir;
446 
447  while (path.size() && (path[path.length() - 1] == CPPUtils::FILE_SEP))
448  path.erase(path.length() - 1);
449 
450  if (path.length() > 0)
451  path = path+CPPUtils::FILE_SEP;
452 
453  string nm = name;
454  while (nm.size() && (nm[0] == CPPUtils::FILE_SEP))
455  nm.erase((unsigned)0);
456 
457  return path+nm;
458 }
459 
463 template <typename T>
464 void CPPUtils::minmax(const vector<T>& v, T& mn, T& mx)
465 {
466  // exit without modification if v is empty
467 
468  if (v.size())
469  {
470  // assign mx and mn to first entry
471 
472  mx = mn = v[0];
473 
474  // adjust mx and mn based on remaining entries
475 
476  for (int i = 1; i < v.size(); i++)
477  {
478  if (mn > v[i]) mn = v[i];
479  if (mx < v[i]) mx = v[i];
480  }
481  }
482 }
483 
484 } // end namespace geotess
485 
486 #endif // CPPUTILS_OBJECT_H