GeoTessCPP  2.2
Software to facilitate storage and retrieval of 3D information about the Earth.
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 #include <fstream>
43 
44 // **** # Defines **************************************************************
45 
46 // use standard library objects
47 using namespace std;
48 
49 //--------------------------
50 
51 // **** _LOCAL INCLUDES_ *******************************************************
52 
53 // **** _BEGIN GEOTESS NAMESPACE_ **********************************************
54 
55 namespace geotess {
56 
57 // **** _GLOBAL_REFERENCES_ ****************************************************
58 
59 // **** _FORWARD REFERENCES_ ***************************************************
60 
61 // **** _CLASS DEFINITION_ *****************************************************
62 
70 {
71 private:
72 
76  CPPUtils(const CPPUtils& bs) {};
77 
81  CPPUtils& operator=(const CPPUtils& bs) { return *this; };
82 
83 public:
84 
88  CPPUtils() {};
89 
93  virtual ~CPPUtils() {};
94 
99  static string class_name() { return "CPPUtils"; };
100 
104  virtual int class_size() const
105  { return (int) sizeof(CPPUtils); };
106 
110  static const string getOpSys();
111 
116  static string stringReplaceAll(const string& sf, const string& sr,
117  const string& s);
118 
122  static void removeEOL(string& s);
123 
128  static void addPathSeparator(string& path);
129 
134  static string insertPathSeparator(const string& dir, const string& name);
135 
140  static void removePathSeparator(string& path);
141 
146  static string itos(int i, const string& frmt = "%d");
147  static string ltos(LONG_INT l, const string& frmt = "%llu");
148  static string ftos(float f, const string& frmt = "%.6f");
149  static string dtos(double d, const string& frmt = "%.14f");
150  static string btos(bool b);
151 
156  static int stoi(const string& i, const string& frmt = "%d");
157  static LONG_INT stol(const string& i64, const string& frmt = "%llu");
158  static float stof(const string& f, const string& frmt = "%f");
159  static double stod(const string& d, const string& frmt = "%lf");
160  static bool stob(const string& b);
161 
167  static string trim(const string& str,
168  const string& delim = " \t");
169  static string trimLeft(const string& str,
170  const string& delim = " \t");
171  static string trimRight(const string& str,
172  const string& delim = " \t");
173 
189  static void getProperties(const string& str,
190  map<string, string>& props);
191 
204  static bool getProperty(const map<string, string>& props,
205  const string& tag, string& value);
206 
214  static void tokenizeString(const string& str, const string& delim,
215  vector<string>& tokens);
216 
221  static string lowercase_string(const string& str);
222  static string uppercase_string(const string& str);
223 
227  template <typename T>
228  static void minmax(const vector<T>& v, T& mn, T& mx);
229 
233  static double toDegrees(double a);
234 
238  static double toRadians(double a);
239 
243  template <typename T>
244  static T** new2DArrayOfArrays(int ni, int nj)
245  {
246  T** a = new T* [ni];
247  for (int i = 0; i < ni; ++i)
248  a[i] = new T [nj];
249  return a;
250  }
251 
259  template <typename T>
260  static T** new2DArray(int ni, int nj)
261  {
262  T** a = new T* [ni];
263  a[0] = new T [ni*nj];
264  for (int i=1; i<ni; ++i) a[i] = &a[0][i*nj];
265  return a;
266  }
267 
276  template <typename T>
277  static T*** new3DArray(int ni, int nj, int nk)
278  {
279  T*** a = new T** [ni];
280  a[0] = new T* [ni*nj];
281  a[0][0] = new T [ni*nj*nk];
282  for (int i = 0; i < ni; ++i)
283  {
284  a[i] = &a[0][i*nj];
285  for (int j = 0; j < nj; ++j)
286  a[i][j] = &a[0][0][(i*nj + j)*nk];
287  }
288 
289  return a;
290  }
291 
295  template <typename T>
296  static void delete2DArray(T**& a);
297 
301  template <typename T>
302  static void delete2DArrayOfArrays(T**& a, int ni)
303  {
304  if (a)
305  {
306  for (int i = 0; i < ni; ++i) delete [] a[i];
307  delete [] a;
308  a = NULL;
309  }
310  }
311 
315  template <typename T>
316  static void delete3DArray(T***& a);
317 
321  template <typename T>
322  static void resetArray(int n, T* array, T val)
323  {
324  for (int i = 0; i < n; ++i) array[i] = val;
325  }
326 
330  template <typename T>
331  static T* copyArray(T* a, int n)
332  {
333  T* copy = new T[n];
334  for (int i=0; i<n; ++i)
335  copy[i] = a[i];
336  return copy;
337  }
338 
342  static bool isBigEndian();
343 
347  static bool isint(const string& i);
348 
352  static const int SBOL;
353  static const int SBYT;
354  static const int SSHT;
355  static const int SINT;
356  static const int SLNG;
357  static const int SFLT;
358  static const int SDBL;
359 
363  static char const FILE_SEP;
364 
368  static string const NEWLINE;
369 
370  static bool fileExists(const string& fileName)
371  {
372  fstream f;
373 
374  f.open(fileName.c_str(), ios::in);
375  if (f.is_open())
376  {
377  f.close();
378  return true;
379  }
380  return false;
381  }
382 
383 }; // End class CPPUtils
384 
385 // **** _INLINE FUNCTION IMPLEMENTATIONS_ **************************************
386 
392 inline double CPPUtils::toDegrees(double a)
393 {
394  return RAD_TO_DEG * a;
395 }
396 
402 inline double CPPUtils::toRadians(double a)
403 {
404  return DEG_TO_RAD * a;
405 }
406 
410 template <typename T>
411 inline void CPPUtils::delete2DArray(T**& a)
412 {
413  if (a)
414  {
415  delete [] a[0];
416  delete [] a;
417  a = NULL;
418  }
419 }
420 
424 template <typename T>
425 inline void CPPUtils::delete3DArray(T***& a)
426 {
427  if (a)
428  {
429  delete [] a[0][0];
430  delete [] a[0];
431  delete [] a;
432  a = NULL;
433  }
434 }
435 
439 inline void CPPUtils::removeEOL(string& s)
440 {
441  if (s.size() && (s[s.length() - 1] == '\n')) s.erase(s.length() - 1);
442  if (s.size() && (s[s.length() - 1] == '\r')) s.erase(s.length() - 1);
443 }
444 
445 inline void CPPUtils::removePathSeparator(string& s)
446 {
447  if (s.size() && (s[s.length() - 1] == CPPUtils::FILE_SEP)) s.erase(s.length() - 1);
448 }
449 
450 inline void CPPUtils::addPathSeparator(string& s)
451 {
452  if ( s.find_last_of(CPPUtils::FILE_SEP) != s.length() - 1 )
453  s += CPPUtils::FILE_SEP;
454 }
455 
456 inline string CPPUtils::insertPathSeparator(const string& dir, const string& name)
457 {
458 
459  string path = dir;
460 
461  while (path.size() && (path[path.length() - 1] == CPPUtils::FILE_SEP))
462  path.erase(path.length() - 1, 1);
463 
464  if (path.length() > 0)
465  path = path+CPPUtils::FILE_SEP;
466 
467  string nm = name;
468  while (nm.size() && (nm[0] == CPPUtils::FILE_SEP))
469  nm.erase((unsigned)0, 1);
470 
471  return path+nm;
472 }
473 
477 template <typename T>
478 void CPPUtils::minmax(const vector<T>& v, T& mn, T& mx)
479 {
480  // exit without modification if v is empty
481 
482  if (v.size())
483  {
484  // assign mx and mn to first entry
485 
486  mx = mn = v[0];
487 
488  // adjust mx and mn based on remaining entries
489 
490  for (int i = 1; i < v.size(); i++)
491  {
492  if (mn > v[i]) mn = v[i];
493  if (mx < v[i]) mx = v[i];
494  }
495  }
496 }
497 
498 } // end namespace geotess
499 
500 #endif // CPPUTILS_OBJECT_H
static const int SBYT
Definition: CPPUtils.h:353
static T ** new2DArray(int ni, int nj)
Returns a new intrinsic 2D array of size [ni][nj].
Definition: CPPUtils.h:260
static bool fileExists(const string &fileName)
Definition: CPPUtils.h:370
static void resetArray(int n, T *array, T val)
Resets all n entries in array to val.
Definition: CPPUtils.h:322
Definition: ArrayReuse.h:55
static T ** new2DArrayOfArrays(int ni, int nj)
Create a 2D array of arrays (Java style).
Definition: CPPUtils.h:244
virtual ~CPPUtils()
Protected destructor ...
Definition: CPPUtils.h:93
static string class_name()
Returns the class name.
Definition: CPPUtils.h:99
static const int SSHT
Definition: CPPUtils.h:354
Basic static utility functions and variables.
Definition: CPPUtils.h:69
static char const FILE_SEP
Path separator.
Definition: CPPUtils.h:363
static T *** new3DArray(int ni, int nj, int nk)
Returns a new intrinsic 3D array of size [ni][nj].
Definition: CPPUtils.h:277
CPPUtils()
Default constructor.
Definition: CPPUtils.h:88
static void delete2DArrayOfArrays(T **&a, int ni)
Deletes the input 2D array of arrays reference and sets it to null.
Definition: CPPUtils.h:302
static const int SBOL
Standard sizes of basic intrinsics.
Definition: CPPUtils.h:352
static T * copyArray(T *a, int n)
Return a deep copy of the specified array.
Definition: CPPUtils.h:331
static const int SDBL
Definition: CPPUtils.h:358
#define LONG_INT
Definition: CPPGlobals.h:111
static string const NEWLINE
End-of-line string.
Definition: CPPUtils.h:368
static const int SLNG
Definition: CPPUtils.h:356
static const int SINT
Definition: CPPUtils.h:355
#define GEOTESS_EXP_IMP
Definition: CPPGlobals.h:71
virtual int class_size() const
Returns the class size.
Definition: CPPUtils.h:104
static const int SFLT
Definition: CPPUtils.h:357