00001 /************************************************************************************ 00002 TerraLib - a library for developing GIS applications. 00003 Copyright 2001-2004 INPE and Tecgraf/PUC-Rio. 00004 00005 This code is part of the TerraLib library. 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Lesser General Public 00008 License as published by the Free Software Foundation; either 00009 version 2.1 of the License, or (at your option) any later version. 00010 00011 You should have received a copy of the GNU Lesser General Public 00012 License along with this library. 00013 00014 The authors reassure the license terms regarding the warranties. 00015 They specifically disclaim any warranties, including, but not limited to, 00016 the implied warranties of merchantability and fitness for a particular purpose. 00017 The library provided hereunder is on an "as is" basis, and the authors have no 00018 obligation to provide maintenance, support, updates, enhancements, or modifications. 00019 In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct, 00020 indirect, special, incidental, or consequential damages arising out of the use 00021 of this library and its documentation. 00022 *************************************************************************************/ 00023 /*! \file TeUtils.h 00024 \brief This file contains some general purpose utilitary functions 00025 */ 00026 #ifndef __TERRALIB_INTERNAL_UTILS_H 00027 #define __TERRALIB_INTERNAL_UTILS_H 00028 00029 #if defined(_MSC_VER) /* MSVC Compiler */ 00030 #pragma warning(disable: 4786) 00031 #endif 00032 00033 #include <string> 00034 #include <vector> 00035 #include <cstdio> 00036 #include <time.h> 00037 00038 00039 #include "TeCoord2D.h" 00040 #include "TeProgress.h" 00041 #include "TeAttribute.h" 00042 #include "TeBox.h" 00043 00044 class TeProjection; 00045 00046 using namespace std; 00047 00048 /** @defgroup Utils General purpose utilitary functions 00049 * General purpose utilitary functions 00050 * @{ 00051 */ 00052 00053 /** @defgroup FileNameFunctions Functions to manipulate file names 00054 @ingroup Utils 00055 * @{ 00056 */ 00057 //! Get the extension part (right to the dot) of a string 00058 TL_DLL string TeGetExtension ( const char* value ); 00059 00060 //! Get the name part (left to the dot) of a string, path included 00061 TL_DLL string TeGetName ( const char* value ); 00062 00063 //! Get the base name part of a string, strip path information 00064 TL_DLL string TeGetBaseName ( const char* value ); 00065 00066 //! Get the path part of a file name 00067 TL_DLL string TeGetPath(const char* value); 00068 00069 //! Writes a string to a file. 00070 /*! 00071 \param fileName the file name to write to, including the directory. 00072 \param text the string with the text to write to. 00073 \param mode the mode used to write to the file (a | w | r). 00074 */ 00075 TL_DLL void TeWriteToFile(const string& fileName, const string& text, const string& mode); 00076 /** @} */ 00077 00078 /** @defgroup C2StringFunctions Functions convert numbers to strings 00079 @ingroup Utils 00080 * @{ 00081 */ 00082 //! Transforms an integer to string 00083 TL_DLL string Te2String ( const int value ); 00084 00085 //! Transforms an unsigned integer to string 00086 TL_DLL string Te2String ( const unsigned int value ); 00087 00088 //! Transforms a long to string 00089 TL_DLL string Te2String ( const long value ); 00090 00091 //! Transforms a long long int to string 00092 TL_DLL string Te2String ( const long long int value ); 00093 00094 //! Transforms an unsigned long to string 00095 TL_DLL string Te2String ( const unsigned long value ); 00096 00097 //! Transforms a double to string floating point notation with precision decimal places 00098 TL_DLL string Te2String ( const double value, int precision ); 00099 00100 //! Transforms a double to string in exponential notation 00101 TL_DLL string Te2String ( const double value ); 00102 00103 /** @} */ 00104 00105 /** @defgroup StringFunctions Functions to manipulate strings 00106 @ingroup Utils 00107 * @{ 00108 */ 00109 //! Converts a string to upper case 00110 TL_DLL void TeConvertToUpperCase ( const string& , char* ); 00111 00112 //! Converts a string to upper case 00113 TL_DLL string TeConvertToUpperCase (const string &name); 00114 00115 //! Converts a string to lower case 00116 TL_DLL string TeConvertToLowerCase (const string &name); 00117 00118 //! Removes special characteres from a string 00119 TL_DLL std::string TeRemoveSpecialChars ( const std::string& str); 00120 00121 //! Replace special characteres from a string 00122 TL_DLL std::string TeReplaceSpecialChars ( const std::string& str); 00123 00124 //! Replace special characteres from a string 00125 TL_DLL std::string TeReplaceSpecialChars ( const std::string& str, bool& changed); 00126 00127 //! Rename special field names 00128 TL_DLL bool TeRenameInvalidFieldName(TeAttributeList &attr); 00129 00130 //! Remove all occurrences right decimal zeros from the real number represented by strIn. 00131 /* 00132 \param strIn Input String. 00133 \param separatorSymbol The symbol between the decimal string part 00134 and the remaining characteres. 00135 \param strOut Output string. 00136 */ 00137 TL_DLL void TeRemoveRightDecimalZeroes( const std::string& strIn, 00138 char separatorSymbol, std::string& strOut ); 00139 00140 00141 //! Removes left and right blank, tab and carriage return characters of a string 00142 TL_DLL void TeTrim(string &str); 00143 00144 //! Splits a string, given a separator, in a vector of parts 00145 TL_DLL int TeSplitString(const string& input, const string& delimiter, vector<string>& results); 00146 00147 //! Compares two strings 00148 /* 00149 \params caseS flag indicating if it is a case sensitive comparation 00150 */ 00151 TL_DLL bool TeStringCompare(const string& str1, const string& str2, bool caseS=false); 00152 00153 00154 //! Validate a string to check if it can be used as a column name 00155 /* 00156 \param name string to be checked 00157 \param changed output flag to identify that string has changed 00158 \param invalidChar output or sequence of chars that are invalid in the name 00159 \return the modified valid name 00160 */ 00161 TL_DLL string TeCheckName(const string& name, bool& changed, string& invalidChar); 00162 /** @} */ 00163 00164 /** @defgroup MathFunctions Mathematical functions 00165 @ingroup Utils 00166 * @{ 00167 */ 00168 //! Rounds a double to int 00169 inline int TeRound(double val) 00170 { 00171 if (val>=0) 00172 return (int)(val+.5); 00173 else 00174 return (int)(val-.5); 00175 } 00176 00177 //! Rounds a double value to a given number of decimal digits 00178 TL_DLL double TeRoundD(double val, int precision=8); 00179 00180 //! Compares two doubles 00181 TL_DLL bool TeCompareDouble(double a, double b, int precision); 00182 00183 //! Adjust a number to a given precision 00184 /* 00185 \param val the number to be adjusted 00186 \param precision the number of decimals places to be used 00187 \param reduce flag to indicate that the number should be reduced instead to increased 00188 \return the adjusted number 00189 */ 00190 TL_DLL double TeAdjustToPrecision(double val, int precision, bool reduce=false); 00191 00192 //! Rounds a double raster element index to an integer 00193 /* 00194 Raster elements have area, so raster element in upper-left position has 00195 index from [-0.5,+0.5) in i and j dimension. 00196 */ 00197 inline int TeRoundRasterIndex(double val) 00198 { 00199 int ind = (int) val; 00200 if (val < (ind-0.5)) 00201 ind -= 1; 00202 else if (val >= (ind+0.5)) 00203 ind += 1; 00204 return ind; 00205 } 00206 /** 00207 * Cubic root from x. 00208 * 00209 * @param x X. 00210 * @return The cubic root from x. 00211 */ 00212 inline double TeCubicRoot( double x ) 00213 { 00214 if( x < 0 ) { 00215 return ( -1. ) * pow( ( -1. ) * x, ( 1. / 3. ) ); 00216 } else { 00217 return pow( x, ( 1. / 3. ) ); 00218 } 00219 }; 00220 00221 /*! Comparassion of two floating points, considering a given precision */ 00222 inline bool TeFPEquals(double d1, double d2, double precision) 00223 { 00224 double eps1 = fabs(d1), 00225 eps2 = fabs(d2), 00226 eps; 00227 eps = (eps1 > eps2) ? eps1 : eps2; 00228 if (eps == 0.0) 00229 return true; //both numbers are 0.0 00230 eps *= precision; 00231 return (fabs(d1 - d2) < eps); 00232 } 00233 00234 //! Swap the bytes of a short value 00235 static inline short swaps(short value) 00236 { 00237 short svalue = ((value & 0x00ff) << 8) | ((value >> 8) & 0x00ff); 00238 return svalue; 00239 } 00240 00241 /** 00242 * Returns the amount of free physical memory (bytes). 00243 * 00244 * @return The amount of free physical memory (bytes). 00245 */ 00246 TL_DLL unsigned long int TeGetFreePhysicalMemory(); 00247 00248 /** 00249 * Returns the amount of total physical memory (bytes). 00250 * 00251 * @return The amount of total physical memory (bytes). 00252 */ 00253 TL_DLL unsigned long int TeGetTotalPhysicalMemory(); 00254 00255 /** 00256 * Returns the amount of used virtual memory (bytes) 00257 * for the current process (physical + swapped). 00258 * 00259 * @return The amount of free virtual memory (bytes). 00260 */ 00261 TL_DLL unsigned long int TeGetUsedVirtualMemory(); 00262 00263 /** 00264 * Returns the amount of total virtual memory (bytes) 00265 * that can be claimed by the current process 00266 * (physical + swapped). 00267 * 00268 * @return The amount of total virtual memory (bytes). 00269 */ 00270 TL_DLL unsigned long int TeGetTotalVirtualMemory(); 00271 00272 /** 00273 * Returns the number of physical processors. 00274 * 00275 * @return The number of physical processors. 00276 */ 00277 TL_DLL unsigned int TeGetPhysProcNumber(); 00278 00279 /** 00280 * Generates a temporary unique file name. 00281 * 00282 * @param filename The generated file name. 00283 * @return true if ok, false errors. 00284 */ 00285 TL_DLL bool TeGetTempFileName( std::string& filename ); 00286 00287 /** 00288 * @brief The file size (bytes). 00289 * 00290 * @note Throws an exception if file not found. 00291 * @param filename The file name. 00292 * @return The file size (bytes). 00293 */ 00294 TL_DLL unsigned long int TeGetFileSize( const std::string& filename ); 00295 00296 /** 00297 * @brief Check the file existence. 00298 * 00299 * @param filename The file name. 00300 * @return true if the file exists, false if not. 00301 */ 00302 TL_DLL bool TeCheckFileExistence( const std::string& filename ); 00303 00304 //! Get the full names of all files inside the given directory. 00305 /* 00306 \param path The directory path. 00307 \param recursive If true, a recursive search will be performed. 00308 \param filesnames The files names. 00309 \return true if OK, false on errors. 00310 */ 00311 TL_DLL bool 00312 TeGetDirFullFilesNames( const std::string& path, 00313 const bool& recursive, 00314 std::vector< std::string >& filesnames ); 00315 00316 /** 00317 * @brief Creates a copy from the given input file name. 00318 * 00319 * @param inputFileName The input full file name. 00320 * @param outputFileName The output full file name. 00321 * @return true if OK, false if not. 00322 */ 00323 TL_DLL bool TeCopyFile( const std::string& inputFileName, 00324 const std::string& outputFileName ); 00325 00326 /** 00327 * @brief Compare two files. 00328 * 00329 * @param inputFileName1 The input full file 1 name. 00330 * @param inputFileName2 The input full file 2 name. 00331 * @return true if the two files are identical, false if not. 00332 */ 00333 TL_DLL bool TeCompareFiles( const std::string& inputFileName1, 00334 const std::string& inputFileName2 ); 00335 00336 /** 00337 * @brief Creates a copy from the given input text file name 00338 * replacing all ocurrences of 00339 * 00340 * @param inputFileName The input full file name. 00341 * @param inputFileName The output full file name. 00342 * @param oldSubString The sub-string to be replaced from the input file. 00343 * @param newSubString The new sub-string. 00344 * @return true if OK, false if not. 00345 */ 00346 TL_DLL bool TeReplaceTextFileSubString( const std::string& inputFileName, 00347 const std::string& outputFileName, const std::string& oldSubString, 00348 const std::string& newSubString ); 00349 00350 /** 00351 * @brief Creates a hash number from an input string. 00352 * @details This algorithm was created for sdbm (a public-domain 00353 * reimplementation of ndbm) database library. It was found to do well in 00354 * scrambling bits, causing better distribution of the keys and fewer 00355 * splits. It also happens to be a good general hashing function with 00356 * good distribution. the actual function is 00357 * hash(i) = hash(i - 1) * 65599 + str[i] 00358 * @param inputStringFileName The input full file name. 00359 * @param inputStringSize Input string size. 00360 * @return The generated hash value. 00361 */ 00362 TL_DLL unsigned long int TeCreateHashFromString( 00363 unsigned char const* inputString, 00364 const unsigned int& inputStringSize ); 00365 00366 00367 /** @} */ 00368 /** @} */ 00369 00370 /** 00371 * @brief Popule vectors with especial chars with accentuation and without 00372 * 00373 * @param vectorIn Vector with accentuation. 00374 * @param vectorOut Vector without accentuation. 00375 * @return void 00376 */ 00377 TL_DLL void TeGetEspecialCharsFixVector(std::vector<std::string> & especialIn, std::vector<std::string> & especialOut); 00378 00379 /** 00380 * @brief Popule vectors with upper chars with accentuation and without 00381 * 00382 * @param vectorIn Vector with accentuation. 00383 * @param vectorOut Vector without accentuation. 00384 * @return void 00385 */ 00386 TL_DLL void TeGetAccentuatedUpperVector(std::vector<std::string> & upperIn, std::vector<std::string> & upperOut); 00387 00388 /** 00389 * @brief Popule vectors with lower chars with accentuation and without 00390 * 00391 * @param vectorIn Vector with accentuation. 00392 * @param vectorOut Vector without accentuation. 00393 * @return void 00394 */ 00395 TL_DLL void TeGetAccentuatedLowerVector(std::vector<std::string> & lowerIn, std::vector<std::string> & lowerOut); 00396 00397 /** 00398 * @brief Checks if the given box at the given projection is inside the LatLong's representation limits 00399 * 00400 * @param box The box to be verified 00401 * @param boxProjection The projection of the box. 00402 * @return TRUE if the box is consisnt, FALSE otherwise. 00403 */ 00404 TL_DLL bool TeCheckBoxConsistency(const TeBox& box, TeProjection* boxProjection); 00405 00406 /** 00407 * @brief From a given unit enum value, returns the std::string associated to it 00408 * 00409 * @param units The enum value 00410 * @return The std::string associated to it 00411 */ 00412 TL_DLL std::string TeGetUnitName(const TeUnits& units); 00413 00414 /** 00415 * @brief From a given std::string, returns the unit enum value associated to it 00416 * 00417 * @param units The std::string associated to it 00418 * @return The enum value 00419 */ 00420 TL_DLL TeUnits TeGetUnit(const std::string& unitName); 00421 00422 /** 00423 * @brief Converts the units of the given 'value' from the 'unitFrom' to the 'unitTo' 00424 * 00425 * @param value The value to be converted 00426 * @param unitFrom The current unit of 'value' 00427 * @param unitTo The output converted unit 00428 * @return The converted value. 00429 */ 00430 TL_DLL double TeConvertUnits(const double& value, const TeUnits& unitFrom, const TeUnits& unitTo); 00431 00432 /** 00433 * @brief Checks if the given file name exists of the disk 00434 * 00435 * @param fileName The name of the file 00436 * @return TRUE if the file exists. FALSE otherwise 00437 */ 00438 TL_DLL bool TeFileExists(const std::string& fileName); 00439 00440 /** 00441 * @brief Copies the given fileNameIn to the given fileNameOut. If fileNameOut already exists, it will be overwritten. 00442 * 00443 * @param fileNameIn The name of the input file 00444 * @param fileNameIn The name of the output file 00445 * @return TRUE if the file was successfully copied. FALSE otherwise 00446 */ 00447 TL_DLL bool TeFileCopy(const std::string& fileNameIn, const std::string& fileNameOut); 00448 00449 /** 00450 * @brief Deletes a file from the the disk 00451 * 00452 * @param fileName The name of the file to be deleted 00453 * @return TRUE if the file was deleted. FALSE otherwise 00454 */ 00455 TL_DLL bool TeFileDelete(const std::string& fileName); 00456 00457 /** 00458 * @brief Renames a file 00459 * 00460 * @param oldFileName The actual name of the file 00461 * @param newFileName The new name of the file 00462 * @return TRUE if the file was successfully renamed. FALSE otherwise 00463 */ 00464 TL_DLL bool TeFileRename(const std::string& oldFileName, const std::string& newFileName); 00465 00466 #endif 00467 00468
1.5.3