tools/SwissTools.hpp
Go to the documentation of this file.
00001 /*
00002 
00003   This file is part of OpenFLUID software
00004   Copyright(c) 2007, INRA - Montpellier SupAgro
00005 
00006 
00007  == GNU General Public License Usage ==
00008 
00009   OpenFLUID is free software: you can redistribute it and/or modify
00010   it under the terms of the GNU General Public License as published by
00011   the Free Software Foundation, either version 3 of the License, or
00012   (at your option) any later version.
00013 
00014   OpenFLUID is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017   GNU General Public License for more details.
00018 
00019   You should have received a copy of the GNU General Public License
00020   along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
00021 
00022 
00023  == Other Usage ==
00024 
00025   Other Usage means a use of OpenFLUID that is inconsistent with the GPL
00026   license, and requires a written agreement between You and INRA.
00027   Licensees for Other Usage of OpenFLUID may use this file in accordance
00028   with the terms contained in the written agreement between You and INRA.
00029   
00030 */
00031 
00032 
00033 
00034 /**
00035   @file
00036 
00037   @author JC.Fabre <fabrejc@supagro.inra.fr>
00038 */
00039 
00040 
00041 #ifndef __SWISSTOOLS_HPP__
00042 #define __SWISSTOOLS_HPP__
00043 
00044 #include <vector>
00045 #include <string>
00046 #include <sstream>
00047 #include <cmath>
00048 
00049 #include <openfluid/dllexport.hpp>
00050 #include <openfluid/core/TypeDefs.hpp>
00051 
00052 namespace openfluid { namespace core {
00053   class DateTime;
00054 } }
00055 
00056 
00057 #define STRINGIFY(x) XSTRINGIFY(x)
00058 #define XSTRINGIFY(x) #x
00059 
00060 
00061 namespace openfluid { namespace tools {
00062 
00063 /**
00064   Template function for string to other type conversion
00065   @param[in] StrToConvert the string to convert
00066   @param[out] Converted the result of the conversion
00067   @return true if the conversion is correct
00068 */
00069 template<typename T>
00070 inline bool ConvertString(const std::string& StrToConvert, T* Converted)
00071 {
00072   std::istringstream iss(StrToConvert);
00073   char c;
00074   return ((iss >> (*Converted)) && !iss.get(c));
00075 }
00076 
00077 // =====================================================================
00078 // =====================================================================
00079 
00080 /**
00081   Template function for value to string conversion
00082   @param[in] ValueToConvert the value to convert
00083   @param[out] StrConverted the result of the conversion
00084   @return true if the conversion is correct
00085 */
00086 template<typename T>
00087 inline bool ConvertValue(const T ValueToConvert, std::string * StrConverted)
00088 {
00089   std::ostringstream oss;
00090   bool IsOK = !(oss << ValueToConvert).fail();
00091 
00092   if (IsOK) *StrConverted = oss.str();
00093 
00094   return IsOK;
00095 }
00096 
00097 
00098 // =====================================================================
00099 // =====================================================================
00100 
00101 /**
00102   Function for tokenizing string into a vector of tokens
00103   @param[in] StrToTokenize the string to tokenize
00104   @param[out] Tokens the resulting tokens
00105   @param[in] Delimiters the string delimiter
00106 */
00107 void DLLEXPORT TokenizeString(const std::string& StrToTokenize,
00108                               std::vector<std::string>& Tokens,
00109                               const std::string& Delimiters);
00110 
00111 // =====================================================================
00112 // =====================================================================
00113 
00114 
00115 /**
00116   Function for testing equality between two double precision floats,
00117   using the "close enough" method.
00118   @param[in] A the firts term of the equality
00119   @param[in] B the firts term of the equality
00120   @param[in] Epsilon the comparison tolerance factor
00121 
00122   @see http://www.parashift.com/c++-faq-lite/floating-point-arith.html
00123   @see http://www.boost.org/doc/libs/1_38_0/libs/test/doc/html/utf/testing-tools/floating_point_comparison.html
00124 */
00125 inline bool IsCloseEnough(double A, double B, double Epsilon = 0.00001)
00126 {
00127   // see Knuth section 4.2.2 pages 217-218
00128   return ((std::fabs(A - B)) <= (Epsilon * std::fabs(A)));
00129 }
00130 
00131 
00132 // =====================================================================
00133 // =====================================================================
00134 
00135 
00136 /**
00137   Function for testing equality between two double precision floats,
00138   using the "very close" method.
00139   @param[in] A the firts term of the equality
00140   @param[in] B the firts term of the equality
00141   @param[in] Epsilon the comparison tolerance factor
00142 
00143   @see http://www.parashift.com/c++-faq-lite/floating-point-arith.html
00144   @see http://www.boost.org/doc/libs/1_38_0/libs/test/doc/html/utf/testing-tools/floating_point_comparison.html
00145 */
00146 inline bool IsVeryClose(double A, double B, double Epsilon = 0.00001)
00147 {
00148   // see Knuth section 4.2.2 pages 217-218
00149   return (((std::fabs(A - B)) <= (Epsilon * std::fabs(A))) && ((std::fabs(A - B)) <= (Epsilon * std::fabs(B))));
00150 }
00151 
00152 
00153 // =====================================================================
00154 // =====================================================================
00155 
00156 
00157 bool DLLEXPORT WildcardMatching(const std::string Pattern, const std::string Str);
00158 
00159 
00160 
00161 // =====================================================================
00162 // =====================================================================
00163 
00164 
00165 
00166 /**
00167   Get list of files with specified extension contained in the specified dir
00168   @param[in] DirToExplore the directory to explore
00169   @param[in] Ext the file extension
00170   @param[in] WithPath return full path with file name if true, file name only otherwise
00171   @param[in] ExtIncludeDot if true, the given extension through Ext parameter is suffixed by a dot
00172 */
00173 std::vector<std::string> DLLEXPORT GetFilesByExt(const std::string DirToExplore,
00174                                                  const std::string Ext,
00175                                                  bool WithPath = false,
00176                                                  bool ExtIncludeDot = false);
00177 
00178 /**
00179   Get list of files with specified extension contained in the specified dir
00180   @param[in] DirToExplore the directory to explore
00181   @param[in] Ext the file extension
00182   @param[in] WithPath return full path with file name if true, file name only otherwise
00183   @param[in] ExtIncludeDot if true, the given extension through Ext parameter is suffixed by a dot
00184 */
00185 std::vector<std::string> DLLEXPORT GetFilesBySuffixAndExt(const std::string& DirToExplore,
00186                                                           const std::string& Suffix,
00187                                                           const std::string& Ext,
00188                                                           bool WithPath = false,
00189                                                           bool ExtIncludeDot = false);
00190 
00191 /**
00192   Splits the passed string into a std::string array, split using the given SepString
00193   @param[in] StrToSplit the string to split
00194   @param[in] Separators the string of separators used to split the string
00195   @param[in] ReturnsEmpty if true, the empty strings are returned
00196   @return a vector of strings
00197 */
00198 std::vector<std::string> DLLEXPORT SplitString(const std::string& StrToSplit,
00199                                                const std::string& Separators,
00200                                                bool ReturnsEmpty = false);
00201 
00202 bool DLLEXPORT EmptyDirectoryRecursively(const std::string DirPath);
00203 
00204 /**
00205   @action Store all the paths of the files existing in dir passed as parameter
00206 */
00207 std::vector<std::string> DLLEXPORT getFilesRecursively(const std::string& DirPath);
00208 
00209 std::string DLLEXPORT ReplaceEmptyString(std::string SourceStr, const std::string& ReplaceStr);
00210 
00211 std::string DLLEXPORT RemoveTrailingSlashes(std::string Str);
00212 
00213 void DLLEXPORT printSTDOUT(std::vector<std::string> Strings, std::string Sep);
00214 
00215 void DLLEXPORT CopyDirectoryRecursively(const std::string SourcePath, const std::string IntoPath, const bool DontCopyDotDirs = false);
00216 
00217 void DLLEXPORT CopyDirectoryContentsRecursively(const std::string SourcePath, const std::string IntoPath, const bool DontCopyDotDirs = false);
00218 
00219 
00220 /**
00221   Compare two OpenFLUID software versions. Version number must be formed as major.minor.patch[~status]
00222   @param[in] VersionA the first version number
00223   @param[in] VersionB the second version number
00224   @param[in] Strict If true, the comparison include the status part of the version (it ignores it otherwise)
00225   @return 1 if VersionA is greater than VersionB, -1 if VersionB is greater than VersionA, 0 if versions are equals, -2 if a version format is not well-formed
00226 */
00227 int DLLEXPORT CompareVersions(const std::string& VersionA, const std::string& VersionB, bool Strict = true);
00228 
00229 /*
00230   Suspend execution for microseconds
00231   @param[in] MSec the microseconds interval
00232 */
00233 void DLLEXPORT Sleep(const unsigned int MSec);
00234 
00235 
00236 } } //namespaces
00237 
00238 
00239 #endif // __SWISSTOOLS_H__
00240 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines