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 
97  static string class_name() { return "CPPUtils"; };
98 
102  virtual int class_size() const
103  { return (int) sizeof(CPPUtils); };
104 
108  static const string getOpSys();
109 
114  static string stringReplaceAll(const string& sf, const string& sr,
115  const string& s);
116 
120  static void removeEOL(string& s);
121 
126  static void addPathSeparator(string& path);
127 
132  static string insertPathSeparator(const string& dir, const string& name);
133 
138  static void removePathSeparator(string& path);
139 
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);
149 
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);
159 
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");
171 
187  static void getProperties(const string& str,
188  map<string, string>& props);
189 
202  static bool getProperty(const map<string, string>& props,
203  const string& tag, string& value);
204 
212  static void tokenizeString(const string& str, const string& delim,
213  vector<string>& tokens);
214 
219  static string lowercase_string(const string& str);
220  static string uppercase_string(const string& str);
221 
225  template <typename T>
226  static void minmax(const vector<T>& v, T& mn, T& mx);
227 
231  static double toDegrees(double a);
232 
236  static double toRadians(double a);
237 
241  template <typename T>
242  static T** new2DArrayOfArrays(int ni, int nj)
243  {
244  T** a = new T* [ni];
245  for (int i = 0; i < ni; ++i)
246  a[i] = new T [nj];
247  return a;
248  }
249 
257  template <typename T>
258  static T** new2DArray(int ni, int nj)
259  {
260  T** a = new T* [ni];
261  a[0] = new T [ni*nj];
262  for (int i=1; i<ni; ++i) a[i] = &a[0][i*nj];
263  return a;
264  }
265 
274  template <typename T>
275  static T*** new3DArray(int ni, int nj, int nk)
276  {
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)
281  {
282  a[i] = &a[0][i*nj];
283  for (int j = 0; j < nj; ++j)
284  a[i][j] = &a[0][0][(i*nj + j)*nk];
285  }
286 
287  return a;
288  }
289 
293  template <typename T>
294  static void delete2DArray(T**& a);
295 
299  template <typename T>
300  static void delete2DArrayOfArrays(T**& a, int ni)
301  {
302  if (a)
303  {
304  for (int i = 0; i < ni; ++i) delete [] a[i];
305  delete [] a;
306  a = NULL;
307  }
308  }
309 
313  template <typename T>
314  static void delete3DArray(T***& a);
315 
319  template <typename T>
320  static void resetArray(int n, T* array, T val)
321  {
322  for (int i = 0; i < n; ++i) array[i] = val;
323  }
324 
328  template <typename T>
329  static T* copyArray(T* a, int n)
330  {
331  T* copy = new T[n];
332  for (int i=0; i<n; ++i)
333  copy[i] = a[i];
334  return copy;
335  }
336 
340  static bool isBigEndian();
341 
345  static bool isint(const string& i);
346 
350  static const int SBOL;
351  static const int SBYT;
352  static const int SSHT;
353  static const int SINT;
354  static const int SLNG;
355  static const int SFLT;
356  static const int SDBL;
357 
361  static char const FILE_SEP;
362 
366  static string const NEWLINE;
367 
368 }; // End class CPPUtils
369 
370 // **** _INLINE FUNCTION IMPLEMENTATIONS_ **************************************
371 
377 inline double CPPUtils::toDegrees(double a)
378 {
379  return RAD_TO_DEG * a;
380 }
381 
387 inline double CPPUtils::toRadians(double a)
388 {
389  return DEG_TO_RAD * a;
390 }
391 
395 template <typename T>
396 inline void CPPUtils::delete2DArray(T**& a)
397 {
398  if (a)
399  {
400  delete [] a[0];
401  delete [] a;
402  a = NULL;
403  }
404 }
405 
409 template <typename T>
410 inline void CPPUtils::delete3DArray(T***& a)
411 {
412  if (a)
413  {
414  delete [] a[0][0];
415  delete [] a[0];
416  delete [] a;
417  a = NULL;
418  }
419 }
420 
424 inline void CPPUtils::removeEOL(string& s)
425 {
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);
428 }
429 
430 inline void CPPUtils::removePathSeparator(string& s)
431 {
432  if (s.size() && (s[s.length() - 1] == CPPUtils::FILE_SEP)) s.erase(s.length() - 1);
433 }
434 
435 inline void CPPUtils::addPathSeparator(string& s)
436 {
437  if ( s.find_last_of(CPPUtils::FILE_SEP) != s.length() - 1 )
438  s += CPPUtils::FILE_SEP;
439 }
440 
441 inline string CPPUtils::insertPathSeparator(const string& dir, const string& name)
442 {
443 
444  string path = dir;
445 
446  while (path.size() && (path[path.length() - 1] == CPPUtils::FILE_SEP))
447  path.erase(path.length() - 1);
448 
449  if (path.length() > 0)
450  path = path+CPPUtils::FILE_SEP;
451 
452  string nm = name;
453  while (nm.size() && (nm[0] == CPPUtils::FILE_SEP))
454  nm.erase(0);
455 
456  return path+nm;
457 }
458 
462 template <typename T>
463 void CPPUtils::minmax(const vector<T>& v, T& mn, T& mx)
464 {
465  // exit without modification if v is empty
466 
467  if (v.size())
468  {
469  // assign mx and mn to first entry
470 
471  mx = mn = v[0];
472 
473  // adjust mx and mn based on remaining entries
474 
475  for (int i = 1; i < v.size(); i++)
476  {
477  if (mn > v[i]) mn = v[i];
478  if (mx < v[i]) mx = v[i];
479  }
480  }
481 }
482 
483 } // end namespace geotess
484 
485 #endif // CPPUTILS_OBJECT_H