GeoTessCPP: rstt/GeoTessCPP/include/CPPUtils.h Source File

GeoTessCPP 2.2.3 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 National Technology & Engineering Solutions of Sandia, LLC
4 //- (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
5 //- Government 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 //- 1. Redistributions of source code must retain the above copyright notice,
14 //- this list of conditions and the following disclaimer.
15 //-
16 //- 2. Redistributions in binary form must reproduce the above copyright
17 //- notice, this list of conditions and the following disclaimer in the
18 //- documentation and/or other materials provided with the distribution.
19 //-
20 //- 3. Neither the name of the copyright holder nor the names of its
21 //- contributors may be used to endorse or promote products derived from
22 //- this software without specific prior written permission.
23 //-
24 //- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 //- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 //- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 //- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
28 //- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 //- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 //- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 //- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 //- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 //- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 //- POSSIBILITY OF SUCH DAMAGE.
35 //-
36 //- ****************************************************************************
37
38 #ifndef CPPUTILS_OBJECT_H
39 #define CPPUTILS_OBJECT_H
40
41 // **** _SYSTEM INCLUDES_ ******************************************************
42
43 #include "CPPGlobals.h"
44 #include <fstream>
45
46 // **** # Defines **************************************************************
47
48 // use standard library objects
49 using namespace std;
50
51 //--------------------------
52
53 // **** _LOCAL INCLUDES_ *******************************************************
54
55 // **** _BEGIN GEOTESS NAMESPACE_ **********************************************
56
57 namespace geotess {
58
59 // **** _GLOBAL_REFERENCES_ ****************************************************
60
61 // **** _FORWARD REFERENCES_ ***************************************************
62
63 // **** _CLASS DEFINITION_ *****************************************************
64
71 class GEOTESS_EXP_IMP CPPUtils
72 {
73 private:
74
78 CPPUtils(const CPPUtils& bs) {};
79
83 CPPUtils& operator=(const CPPUtils& bs) { return *this; };
84
85 public:
86
90 CPPUtils() {};
91
95 virtual ~CPPUtils() {};
96
101 static string class_name() { return "CPPUtils"; };
102
106 virtual int class_size() const
107 { return (int) sizeof(CPPUtils); };
108
112 static const string getOpSys();
113
118 static string stringReplaceAll(const string& sf, const string& sr,
119 const string& s);
120
124 static void removeEOL(string& s);
125
130 static void addPathSeparator(string& path);
131
136 static string insertPathSeparator(const string& dir, const string& name);
137
142 static void removePathSeparator(string& path);
143
148 static string itos(int i, const string& frmt = "%d");
149 static string ltos(LONG_INT l, const string& frmt = "%llu");
150 static string ftos(float f, const string& frmt = "%.6f");
151 static string dtos(double d, const string& frmt = "%.14f");
152 static string btos(bool b);
153
158 static int stoi(const string& i, const string& frmt = "%d");
159 static LONG_INT stol(const string& i64, const string& frmt = "%llu");
160 static float stof(const string& f, const string& frmt = "%f");
161 static double stod(const string& d, const string& frmt = "%lf");
162 static bool stob(const string& b);
163
169 static string trim(const string& str,
170 const string& delim = " \t");
171 static string trimLeft(const string& str,
172 const string& delim = " \t");
173 static string trimRight(const string& str,
174 const string& delim = " \t");
175
191 static void getProperties(const string& str,
192 map<string, string>& props);
193
206 static bool getProperty(const map<string, string>& props,
207 const string& tag, string& value);
208
216 static void tokenizeString(const string& str, const string& delim,
217 vector<string>& tokens);
218
223 static string lowercase_string(const string& str);
224 static string uppercase_string(const string& str);
225
229 template <typename T>
230 static void minmax(const vector<T>& v, T& mn, T& mx);
231
235 static double toDegrees(double a);
236
240 static double toRadians(double a);
241
245 template <typename T>
246 static T** new2DArrayOfArrays(int ni, int nj)
247 {
248 T** a = new T* [ni];
249 for (int i = 0; i < ni; ++i)
250 a[i] = new T [nj];
251 return a;
252 }
253
261 template <typename T>
262 static T** new2DArray(int ni, int nj)
263 {
264 T** a = new T* [ni];
265 a[0] = new T [ni*nj];
266 for (int i=1; i<ni; ++i) a[i] = &a[0][i*nj];
267 return a;
268 }
269
278 template <typename T>
279 static T*** new3DArray(int ni, int nj, int nk)
280 {
281 T*** a = new T** [ni];
282 a[0] = new T* [ni*nj];
283 a[0][0] = new T [ni*nj*nk];
284 for (int i = 0; i < ni; ++i)
285 {
286 a[i] = &a[0][i*nj];
287 for (int j = 0; j < nj; ++j)
288 a[i][j] = &a[0][0][(i*nj + j)*nk];
289 }
290
291 return a;
292 }
293
297 template <typename T>
298 static void delete2DArray(T**& a);
299
303 template <typename T>
304 static void delete2DArrayOfArrays(T**& a, int ni)
305 {
306 if (a)
307 {
308 for (int i = 0; i < ni; ++i) delete [] a[i];
309 delete [] a;
310 a = NULL;
311 }
312 }
313
317 template <typename T>
318 static void delete3DArray(T***& a);
319
323 template <typename T>
324 static void resetArray(int n, T* array, T val)
325 {
326 for (int i = 0; i < n; ++i) array[i] = val;
327 }
328
332 template <typename T>
333 static T* copyArray(T* a, int n)
334 {
335 T* copy = new T[n];
336 for (int i=0; i<n; ++i)
337 copy[i] = a[i];
338 return copy;
339 }
340
344 static bool isBigEndian();
345
349 static bool isint(const string& i);
350
354 static const int SBOL;
355 static const int SBYT;
356 static const int SSHT;
357 static const int SINT;
358 static const int SLNG;
359 static const int SFLT;
360 static const int SDBL;
361
365 static char const FILE_SEP;
366
370 static string const NEWLINE;
371
372 static bool fileExists(const string& fileName)
373 {
374 fstream f;
375
376 f.open(fileName.c_str(), ios::in);
377 if (f.is_open())
378 {
379 f.close();
380 return true;
381 }
382 return false;
383 }
384
385 }; // End class CPPUtils
386
387 // **** _INLINE FUNCTION IMPLEMENTATIONS_ **************************************
388
394 inline double CPPUtils::toDegrees(double a)
395 {
396 return RAD_TO_DEG * a;
397 }
398
404 inline double CPPUtils::toRadians(double a)
405 {
406 return DEG_TO_RAD * a;
407 }
408
412 template <typename T>
413 inline void CPPUtils::delete2DArray(T**& a)
414 {
415 if (a)
416 {
417 delete [] a[0];
418 delete [] a;
419 a = NULL;
420 }
421 }
422
426 template <typename T>
427 inline void CPPUtils::delete3DArray(T***& a)
428 {
429 if (a)
430 {
431 delete [] a[0][0];
432 delete [] a[0];
433 delete [] a;
434 a = NULL;
435 }
436 }
437
441 inline void CPPUtils::removeEOL(string& s)
442 {
443 if (s.size() && (s[s.length() - 1] == '\n')) s.erase(s.length() - 1);
444 if (s.size() && (s[s.length() - 1] == '\r')) s.erase(s.length() - 1);
445 }
446
447 inline void CPPUtils::removePathSeparator(string& s)
448 {
449 if (s.size() && (s[s.length() - 1] == CPPUtils::FILE_SEP)) s.erase(s.length() - 1);
450 }
451
452 inline void CPPUtils::addPathSeparator(string& s)
453 {
454 if ( s.find_last_of(CPPUtils::FILE_SEP) != s.length() - 1 )
455 s += CPPUtils::FILE_SEP;
456 }
457
458 inline string CPPUtils::insertPathSeparator(const string& dir, const string& name)
459 {
460
461 string path = dir;
462
463 while (path.size() && (path[path.length() - 1] == CPPUtils::FILE_SEP))
464 path.erase(path.length() - 1, 1);
465
466 if (path.length() > 0)
467 path = path+CPPUtils::FILE_SEP;
468
469 string nm = name;
470 while (nm.size() && (nm[0] == CPPUtils::FILE_SEP))
471 nm.erase((unsigned)0, 1);
472
473 return path+nm;
474 }
475
479 template <typename T>
480 void CPPUtils::minmax(const vector<T>& v, T& mn, T& mx)
481 {
482 // exit without modification if v is empty
483
484 if (v.size())
485 {
486 // assign mx and mn to first entry
487
488 mx = mn = v[0];
489
490 // adjust mx and mn based on remaining entries
491
492 for (int i = 1; i < v.size(); i++)
493 {
494 if (mn > v[i]) mn = v[i];
495 if (mx < v[i]) mx = v[i];
496 }
497 }
498 }
499
500 } // end namespace geotess
501
502 #endif // CPPUTILS_OBJECT_H
geotess::CPPUtils::SBYT
static const int SBYT
Definition: CPPUtils.h:355
geotess::CPPUtils::ltos
static string ltos(LONG_INT l, const string &frmt="%llu")
geotess::CPPUtils::dtos
static string dtos(double d, const string &frmt="%.14f")
geotess
Definition: ArrayReuse.h:57
geotess::CPPUtils::stob
static bool stob(const string &b)
geotess::CPPUtils::btos
static string btos(bool b)
geotess::CPPUtils::new2DArray
static T ** new2DArray(int ni, int nj)
Definition: CPPUtils.h:262
geotess::CPPUtils::stod
static double stod(const string &d, const string &frmt="%lf")
geotess::CPPUtils::tokenizeString
static void tokenizeString(const string &str, const string &delim, vector< string > &tokens)
geotess::CPPUtils::~CPPUtils
virtual ~CPPUtils()
Definition: CPPUtils.h:95
geotess::CPPUtils::class_name
static string class_name()
Definition: CPPUtils.h:101
geotess::CPPUtils::lowercase_string
static string lowercase_string(const string &str)
geotess::CPPUtils::new2DArrayOfArrays
static T ** new2DArrayOfArrays(int ni, int nj)
Definition: CPPUtils.h:246
geotess::CPPUtils::resetArray
static void resetArray(int n, T *array, T val)
Definition: CPPUtils.h:324
geotess::CPPUtils::class_size
virtual int class_size() const
Definition: CPPUtils.h:106
geotess::CPPUtils::CPPUtils
CPPUtils()
Definition: CPPUtils.h:90
geotess::CPPUtils::ftos
static string ftos(float f, const string &frmt="%.6f")
geotess::CPPUtils::FILE_SEP
static char const FILE_SEP
Definition: CPPUtils.h:365
geotess::CPPUtils::trimRight
static string trimRight(const string &str, const string &delim=" \t")
geotess::CPPUtils::getOpSys
static const string getOpSys()
geotess::CPPUtils::isBigEndian
static bool isBigEndian()
geotess::CPPUtils::getProperties
static void getProperties(const string &str, map< string, string > &props)
geotess::CPPUtils::stringReplaceAll
static string stringReplaceAll(const string &sf, const string &sr, const string &s)
geotess::CPPUtils::SSHT
static const int SSHT
Definition: CPPUtils.h:356
geotess::CPPUtils::trim
static string trim(const string &str, const string &delim=" \t")
LONG_INT
#define LONG_INT
Definition: CPPGlobals.h:113
geotess::CPPUtils::new3DArray
static T *** new3DArray(int ni, int nj, int nk)
Definition: CPPUtils.h:279
geotess::CPPUtils::getProperty
static bool getProperty(const map< string, string > &props, const string &tag, string &value)
geotess::CPPUtils::SLNG
static const int SLNG
Definition: CPPUtils.h:358
geotess::CPPUtils::copyArray
static T * copyArray(T *a, int n)
Definition: CPPUtils.h:333
geotess::CPPUtils::stol
static LONG_INT stol(const string &i64, const string &frmt="%llu")
GEOTESS_EXP_IMP
#define GEOTESS_EXP_IMP
Definition: CPPGlobals.h:73
geotess::CPPUtils::SDBL
static const int SDBL
Definition: CPPUtils.h:360
geotess::CPPUtils::delete2DArrayOfArrays
static void delete2DArrayOfArrays(T **&a, int ni)
Definition: CPPUtils.h:304
geotess::CPPUtils
Basic static utility functions and variables.
Definition: CPPUtils.h:72
geotess::CPPUtils::isint
static bool isint(const string &i)
geotess::CPPUtils::SBOL
static const int SBOL
Definition: CPPUtils.h:354
geotess::CPPUtils::uppercase_string
static string uppercase_string(const string &str)
geotess::CPPUtils::SINT
static const int SINT
Definition: CPPUtils.h:357
geotess::CPPUtils::NEWLINE
static string const NEWLINE
Definition: CPPUtils.h:370
geotess::CPPUtils::itos
static string itos(int i, const string &frmt="%d")
CPPGlobals.h
geotess::CPPUtils::SFLT
static const int SFLT
Definition: CPPUtils.h:359
geotess::CPPUtils::stoi
static int stoi(const string &i, const string &frmt="%d")
geotess::CPPUtils::trimLeft
static string trimLeft(const string &str, const string &delim=" \t")
geotess::CPPUtils::fileExists
static bool fileExists(const string &fileName)
Definition: CPPUtils.h:372
geotess::CPPUtils::stof
static float stof(const string &f, const string &frmt="%f")