GeoTessCPP  2.0.0
Software to facilitate storage and retrieval of 3D information about the Earth.
 All Classes Namespaces Files Functions Variables Typedefs Friends Defines
include/CPPUtils.h
Go to the documentation of this file.
00001 //- ****************************************************************************
00002 //- 
00003 //- Copyright 2009 Sandia Corporation. Under the terms of Contract
00004 //- DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
00005 //- retains certain rights in this software.
00006 //- 
00007 //- BSD Open Source License.
00008 //- All rights reserved.
00009 //- 
00010 //- Redistribution and use in source and binary forms, with or without
00011 //- modification, are permitted provided that the following conditions are met:
00012 //- 
00013 //-    * Redistributions of source code must retain the above copyright notice,
00014 //-      this list of conditions and the following disclaimer.
00015 //-    * Redistributions in binary form must reproduce the above copyright
00016 //-      notice, this list of conditions and the following disclaimer in the
00017 //-      documentation and/or other materials provided with the distribution.
00018 //-    * Neither the name of Sandia National Laboratories nor the names of its
00019 //-      contributors may be used to endorse or promote products derived from
00020 //-      this software without specific prior written permission.
00021 //- 
00022 //- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00023 //- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024 //- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025 //- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00026 //- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00027 //- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00028 //- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00029 //- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00030 //- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00031 //- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 //- POSSIBILITY OF SUCH DAMAGE.
00033 //-
00034 //- ****************************************************************************
00035 
00036 #ifndef CPPUTILS_OBJECT_H
00037 #define CPPUTILS_OBJECT_H
00038 
00039 // **** _SYSTEM INCLUDES_ ******************************************************
00040 
00041 #include "CPPGlobals.h"
00042 
00043 // **** # Defines **************************************************************
00044 
00045 // use standard library objects
00046 using namespace std;
00047 
00048 //--------------------------
00049 
00050 // **** _LOCAL INCLUDES_ *******************************************************
00051 
00052 // **** _BEGIN GEOTESS NAMESPACE_ **********************************************
00053 
00054 namespace geotess {
00055 
00056 // **** _GLOBAL_REFERENCES_ ****************************************************
00057 
00058 // **** _FORWARD REFERENCES_ ***************************************************
00059 
00060 // **** _CLASS DEFINITION_ *****************************************************
00061 
00068 class GEOTESS_EXP_IMP CPPUtils
00069 {
00070 private:
00071 
00075         CPPUtils(const CPPUtils& bs) {};
00076 
00080         CPPUtils&                                       operator=(const CPPUtils& bs) { return *this; };
00081 
00082 public:
00083 
00087         CPPUtils() {};
00088 
00092         virtual                                         ~CPPUtils() {};
00093 
00098         static  string          class_name() { return "CPPUtils"; };
00099 
00103         virtual int                             class_size() const
00104         { return (int) sizeof(CPPUtils); };
00105 
00109         static const string getOpSys();
00110 
00115         static string                   stringReplaceAll(const string& sf, const string& sr,
00116                         const string& s);
00117 
00121         static void                     removeEOL(string& s);
00122 
00127         static void addPathSeparator(string& path);
00128 
00133         static string insertPathSeparator(const string& dir, const string& name);
00134 
00139         static void removePathSeparator(string& path);
00140 
00145         static string                   itos(int i, const string& frmt = "%d");
00146         static string                   ltos(LONG_INT l, const string& frmt = "%llu");
00147         static string                   ftos(float f, const string& frmt = "%.6f");
00148         static string                   dtos(double d, const string& frmt = "%.14f");
00149         static string                   btos(bool b);
00150 
00155         static int                              stoi(const string& i, const string& frmt = "%d");
00156         static LONG_INT                         stol(const string& i64, const string& frmt = "%llu");
00157         static float                    stof(const string& f, const string& frmt = "%f");
00158         static double                   stod(const string& d, const string& frmt = "%lf");
00159         static bool                             stob(const string& b);
00160 
00166         static string                   trim(const string& str,
00167                         const string& delim = " \t");
00168         static string                   trimLeft(const string& str,
00169                         const string& delim = " \t");
00170         static string                   trimRight(const string& str,
00171                         const string& delim = " \t");
00172 
00188         static void                             getProperties(const string& str,
00189                         map<string, string>& props);
00190 
00203         static bool                             getProperty(const map<string, string>& props,
00204                         const string& tag, string& value);
00205 
00213         static void                             tokenizeString(const string& str, const string& delim,
00214                         vector<string>& tokens);
00215 
00220         static string                   lowercase_string(const string& str);
00221         static string                   uppercase_string(const string& str);
00222 
00226         template <typename T>
00227         static void                             minmax(const vector<T>& v, T& mn, T& mx);
00228 
00232         static double                   toDegrees(double a);
00233 
00237         static double                   toRadians(double a);
00238 
00242         template <typename T>
00243         static T**                              new2DArrayOfArrays(int ni, int nj)
00244         {
00245                 T** a = new T* [ni];
00246                 for (int i = 0; i < ni; ++i)
00247                         a[i] = new T [nj];
00248                 return a;
00249         }
00250 
00258         template <typename T>
00259         static T**                              new2DArray(int ni, int nj)
00260         {
00261                 T** a = new T* [ni];
00262                 a[0] = new T [ni*nj];
00263                 for (int i=1; i<ni; ++i) a[i] = &a[0][i*nj];
00264                 return a;
00265         }
00266 
00275         template <typename T>
00276         static T***                             new3DArray(int ni, int nj, int nk)
00277         {
00278                 T*** a  = new T** [ni];
00279                 a[0]    = new T*  [ni*nj];
00280                 a[0][0] = new T   [ni*nj*nk];
00281                 for (int i = 0; i < ni; ++i)
00282                 {
00283                         a[i] = &a[0][i*nj];
00284                         for (int j = 0; j < nj; ++j)
00285                                 a[i][j] = &a[0][0][(i*nj + j)*nk];
00286                 }
00287 
00288                 return a;
00289         }
00290 
00294         template <typename T>
00295         static void                             delete2DArray(T**& a);
00296 
00300         template <typename T>
00301         static void                             delete2DArrayOfArrays(T**& a, int ni)
00302         {
00303                 if (a)
00304                 {
00305                         for (int i = 0; i < ni; ++i) delete [] a[i];
00306                         delete [] a;
00307                         a = NULL;
00308                 }
00309         }
00310 
00314         template <typename T>
00315         static void                             delete3DArray(T***& a);
00316 
00320         template <typename T>
00321         static void                             resetArray(int n, T* array, T val)
00322         {
00323                 for (int i = 0; i < n; ++i) array[i] = val;
00324         }
00325 
00329         template <typename T>
00330         static T* copyArray(T* a, int n)
00331         {
00332                 T* copy = new T[n];
00333                 for (int i=0; i<n; ++i)
00334                         copy[i] = a[i];
00335                 return copy;
00336         }
00337 
00341         static bool                             isBigEndian();
00342 
00346         static  bool                    isint(const string& i);
00347 
00351         static const int        SBOL;
00352         static const int        SBYT;
00353         static const int        SSHT;
00354         static const int        SINT;
00355         static const int        SLNG;
00356         static const int        SFLT;
00357         static const int        SDBL;
00358 
00362         static  char const FILE_SEP;
00363 
00367         static  string const NEWLINE;
00368 
00369 }; // End class CPPUtils
00370 
00371 // **** _INLINE FUNCTION IMPLEMENTATIONS_ **************************************
00372 
00378 inline double CPPUtils::toDegrees(double a)
00379 {
00380         return RAD_TO_DEG * a;
00381 }
00382 
00388 inline double CPPUtils::toRadians(double a)
00389 {
00390         return DEG_TO_RAD * a;
00391 }
00392 
00396 template <typename T>
00397 inline void CPPUtils::delete2DArray(T**& a)
00398 {
00399         if (a)
00400         {
00401                 delete [] a[0];
00402                 delete [] a;
00403                 a = NULL;
00404         }
00405 }
00406 
00410 template <typename T>
00411 inline void CPPUtils::delete3DArray(T***& a)
00412 {
00413         if (a)
00414         {
00415                 delete [] a[0][0];
00416                 delete [] a[0];
00417                 delete [] a;
00418                 a = NULL;
00419         }
00420 }
00421 
00425 inline void CPPUtils::removeEOL(string& s)
00426 {
00427         if (s.size() && (s[s.length() - 1] == '\n'))    s.erase(s.length() - 1);
00428         if (s.size() && (s[s.length() - 1] == '\r'))    s.erase(s.length() - 1);
00429 }
00430 
00431 inline void CPPUtils::removePathSeparator(string& s)
00432 {
00433         if (s.size() && (s[s.length() - 1] == CPPUtils::FILE_SEP))      s.erase(s.length() - 1);
00434 }
00435 
00436 inline void CPPUtils::addPathSeparator(string& s)
00437 {
00438         if ( s.find_last_of(CPPUtils::FILE_SEP) != s.length() - 1 )
00439                 s += CPPUtils::FILE_SEP;
00440 }
00441 
00442 inline string CPPUtils::insertPathSeparator(const string& dir, const string& name)
00443 {
00444 
00445         string path = dir;
00446 
00447         while (path.size() && (path[path.length() - 1] == CPPUtils::FILE_SEP))
00448                 path.erase(path.length() - 1);
00449         
00450         if (path.length() > 0)
00451                 path = path+CPPUtils::FILE_SEP;
00452 
00453         string nm = name;
00454         while (nm.size() && (nm[0] == CPPUtils::FILE_SEP))
00455                 nm.erase(0);
00456 
00457         return path+nm;
00458 }
00459 
00463 template <typename T>
00464 void CPPUtils::minmax(const vector<T>& v, T& mn, T& mx)
00465 {
00466         // exit without modification if v is empty
00467 
00468         if (v.size())
00469         {
00470                 // assign mx and mn to first entry
00471 
00472                 mx = mn = v[0];
00473 
00474                 // adjust mx and mn based on remaining entries
00475 
00476                 for (int i = 1; i < v.size(); i++)
00477                 {
00478                         if (mn > v[i]) mn = v[i];
00479                         if (mx < v[i]) mx = v[i];
00480                 }
00481         }
00482 }
00483 
00484 } // end namespace geotess
00485 
00486 #endif  // CPPUTILS_OBJECT_H