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 /** 00065 Converts a string to another type 00066 @param[in] StrToConvert the string to convert 00067 @param[out] Converted the result of the conversion 00068 @return true if the conversion is correct 00069 */ 00070 template<typename T> 00071 inline bool ConvertString(const std::string& StrToConvert, T* Converted) 00072 { 00073 std::istringstream iss(StrToConvert); 00074 char c; 00075 return ((iss >> (*Converted)) && !iss.get(c)); 00076 } 00077 00078 00079 /** 00080 Converts a value to a string 00081 @param[in] ValueToConvert the value to convert 00082 @param[out] StrConverted the result of the conversion 00083 @return true if the conversion is correct 00084 */ 00085 template<typename T> 00086 inline bool ConvertValue(const T ValueToConvert, std::string * StrConverted) 00087 { 00088 std::ostringstream oss; 00089 bool IsOK = !(oss << ValueToConvert).fail(); 00090 00091 if (IsOK) *StrConverted = oss.str(); 00092 00093 return IsOK; 00094 } 00095 00096 00097 /** 00098 Function for tokenizing string into a vector of tokens 00099 @param[in] StrToTokenize the string to tokenize 00100 @param[out] Tokens the resulting tokens 00101 @param[in] Delimiters the string delimiter 00102 */ 00103 void DLLEXPORT TokenizeString(const std::string& StrToTokenize, 00104 std::vector<std::string>& Tokens, 00105 const std::string& Delimiters); 00106 00107 00108 /** 00109 Function for testing equality between two double precision floats, 00110 using the "close enough" method. 00111 @param[in] A the firts term of the equality 00112 @param[in] B the firts term of the equality 00113 @param[in] Epsilon the comparison tolerance factor 00114 00115 @see http://www.parashift.com/c++-faq-lite/floating-point-arith.html 00116 @see http://www.boost.org/doc/libs/1_38_0/libs/test/doc/html/utf/testing-tools/floating_point_comparison.html 00117 */ 00118 00119 00120 inline bool IsCloseEnough(double A, double B, double Epsilon = 0.00001) 00121 { 00122 // see Knuth section 4.2.2 pages 217-218 00123 return ((std::fabs(A - B)) <= (Epsilon * std::fabs(A))); 00124 } 00125 00126 00127 /** 00128 Function for testing equality between two double precision floats, 00129 using the "very close" method. 00130 @param[in] A the firts term of the equality 00131 @param[in] B the firts term of the equality 00132 @param[in] Epsilon the comparison tolerance factor 00133 00134 @see http://www.parashift.com/c++-faq-lite/floating-point-arith.html 00135 @see http://www.boost.org/doc/libs/1_38_0/libs/test/doc/html/utf/testing-tools/floating_point_comparison.html 00136 */ 00137 inline bool IsVeryClose(double A, double B, double Epsilon = 0.00001) 00138 { 00139 // see Knuth section 4.2.2 pages 217-218 00140 return (((std::fabs(A - B)) <= (Epsilon * std::fabs(A))) && ((std::fabs(A - B)) <= (Epsilon * std::fabs(B)))); 00141 } 00142 00143 00144 /** 00145 Checks if the given string matches the given pattern, including * and ? wildcards 00146 @param[in] Pattern The pattern to match 00147 @param[in] Str The string to test 00148 @return true if the given string matches the given pattern 00149 */ 00150 bool DLLEXPORT WildcardMatching(const std::string& Pattern, const std::string& Str); 00151 00152 00153 /** 00154 Gets the list of files with specified extension contained in the specified directory 00155 @param[in] DirToExplore the directory to explore 00156 @param[in] Ext the file extension 00157 @param[in] WithPath return full path with file name if true, file name only otherwise 00158 @param[in] ExtIncludeDot if true, the given extension through Ext parameter is suffixed by a dot 00159 */ 00160 std::vector<std::string> DLLEXPORT GetFilesByExt(const std::string DirToExplore, 00161 const std::string Ext, 00162 bool WithPath = false, 00163 bool ExtIncludeDot = false); 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] Suffix the file suffix 00171 @param[in] WithPath return full path with file name if true, file name only otherwise 00172 @param[in] ExtIncludeDot if true, the given extension through Ext parameter is suffixed by a dot 00173 */ 00174 std::vector<std::string> DLLEXPORT GetFilesBySuffixAndExt(const std::string& DirToExplore, 00175 const std::string& Suffix, 00176 const std::string& Ext, 00177 bool WithPath = false, 00178 bool ExtIncludeDot = false); 00179 00180 00181 /** 00182 Splits the passed string into a std::string array, split using the given SepString 00183 @param[in] StrToSplit the string to split 00184 @param[in] Separators the string of separators used to split the string 00185 @param[in] ReturnsEmpty if true, the empty strings are returned 00186 @return a vector of strings 00187 */ 00188 std::vector<std::string> DLLEXPORT SplitString(const std::string& StrToSplit, 00189 const std::string& Separators, 00190 bool ReturnsEmpty = false); 00191 00192 00193 /** 00194 Recursively removes all files and directories contained in the given directory. 00195 It deletes the directory and recreates it. 00196 @param[in] DirPath the directory to empty 00197 @return true if successful 00198 */ 00199 bool DLLEXPORT EmptyDirectoryRecursively(const std::string& DirPath); 00200 00201 00202 /** 00203 Recursively finds all the paths of the files that exist exist 00204 in the given directory and subdirectories 00205 @param[in] DirPath the directory to explore 00206 @return a vector containing all files paths 00207 */ 00208 std::vector<std::string> DLLEXPORT GetFilesRecursively(const std::string& DirPath); 00209 00210 00211 /** 00212 Replaces a string by another string if it is empty 00213 @param[in] SourceStr the source string to process 00214 @param[in] ReplaceStr the replacement string to use 00215 @return the processed string 00216 */ 00217 std::string DLLEXPORT ReplaceEmptyString(std::string SourceStr, 00218 const std::string& ReplaceStr); 00219 00220 00221 /** 00222 Removes trailing slashes if any, useful for cleaning paths 00223 @param[in] Str the string to process 00224 @return the processed string 00225 */ 00226 std::string DLLEXPORT RemoveTrailingSlashes(std::string Str); 00227 00228 00229 // TODO check if it has to be removed 00230 void DLLEXPORT printSTDOUT(std::vector<std::string> Strings, std::string Sep); 00231 00232 00233 /** 00234 Copies a source directory to destination path, including the root of the source directory. 00235 If the destination path does not exists, it is created. 00236 @param[in] SourcePath the source directory to copy 00237 @param[in] IntoPath the destination directory 00238 @param[in] DontCopyDotDirs flag for copying dot ('.*') directories. Default is false. 00239 */ 00240 void DLLEXPORT CopyDirectoryRecursively(const std::string& SourcePath, 00241 const std::string& IntoPath, 00242 const bool DontCopyDotDirs = false); 00243 00244 00245 /** 00246 Copies a source directory to destination path, not including the root of the source directory. 00247 If the destination path does not exists, it is created. 00248 @param[in] SourcePath the source directory to copy 00249 @param[in] IntoPath the destination directory 00250 @param[in] DontCopyDotDirs flag for copying dot ('.*') directories. Default is false. 00251 */ 00252 void DLLEXPORT CopyDirectoryContentsRecursively(const std::string& SourcePath, 00253 const std::string& IntoPath, 00254 const bool DontCopyDotDirs = false); 00255 00256 00257 /** 00258 Compares two OpenFLUID software versions. Version number must be formed as major.minor.patch[~status] 00259 @param[in] VersionA the first version number 00260 @param[in] VersionB the second version number 00261 @param[in] Strict If true, the comparison include the status part of the version (it ignores it otherwise) 00262 @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 00263 */ 00264 int DLLEXPORT CompareVersions(const std::string& VersionA, const std::string& VersionB, bool Strict = true); 00265 00266 00267 /* 00268 Suspend execution for microseconds 00269 @param[in] MSec the microseconds interval 00270 */ 00271 void DLLEXPORT Sleep(const unsigned int MSec); 00272 00273 00274 } } //namespaces 00275 00276 00277 #endif // __SWISSTOOLS_H__ 00278