All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SwissTools.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 
33 
34 /**
35  @file
36 
37  @author JC.Fabre <fabrejc@supagro.inra.fr>
38 */
39 
40 
41 #ifndef __SWISSTOOLS_HPP__
42 #define __SWISSTOOLS_HPP__
43 
44 #include <vector>
45 #include <string>
46 #include <sstream>
47 #include <cmath>
48 
49 #include <openfluid/dllexport.hpp>
51 
52 namespace openfluid { namespace core {
53  class DateTime;
54 } }
55 
56 
57 #define STRINGIFY(x) XSTRINGIFY(x)
58 #define XSTRINGIFY(x) #x
59 
60 
61 namespace openfluid { namespace tools {
62 
63 
64 /**
65  Converts a string to another type
66  @param[in] StrToConvert the string to convert
67  @param[out] Converted the result of the conversion
68  @return true if the conversion is correct
69 */
70 template<typename T>
71 inline bool ConvertString(const std::string& StrToConvert, T* Converted)
72 {
73  std::istringstream iss(StrToConvert);
74  char c;
75  return ((iss >> (*Converted)) && !iss.get(c));
76 }
77 
78 
79 /**
80  Converts a value to a string
81  @param[in] ValueToConvert the value to convert
82  @param[out] StrConverted the result of the conversion
83  @return true if the conversion is correct
84 */
85 template<typename T>
86 inline bool ConvertValue(const T ValueToConvert, std::string * StrConverted)
87 {
88  std::ostringstream oss;
89  bool IsOK = !(oss << ValueToConvert).fail();
90 
91  if (IsOK) *StrConverted = oss.str();
92 
93  return IsOK;
94 }
95 
96 
97 /**
98  Function for tokenizing string into a vector of tokens
99  @param[in] StrToTokenize the string to tokenize
100  @param[out] Tokens the resulting tokens
101  @param[in] Delimiters the string delimiter
102 */
103 void DLLEXPORT TokenizeString(const std::string& StrToTokenize,
104  std::vector<std::string>& Tokens,
105  const std::string& Delimiters);
106 
107 
108 /**
109  Function for testing equality between two double precision floats,
110  using the "close enough" method.
111  @param[in] A the firts term of the equality
112  @param[in] B the firts term of the equality
113  @param[in] Epsilon the comparison tolerance factor
114 
115  @see http://www.parashift.com/c++-faq-lite/floating-point-arith.html
116  @see http://www.boost.org/doc/libs/1_38_0/libs/test/doc/html/utf/testing-tools/floating_point_comparison.html
117 */
118 
119 
120 inline bool IsCloseEnough(double A, double B, double Epsilon = 0.00001)
121 {
122  // see Knuth section 4.2.2 pages 217-218
123  return ((std::fabs(A - B)) <= (Epsilon * std::fabs(A)));
124 }
125 
126 
127 /**
128  Function for testing equality between two double precision floats,
129  using the "very close" method.
130  @param[in] A the firts term of the equality
131  @param[in] B the firts term of the equality
132  @param[in] Epsilon the comparison tolerance factor
133 
134  @see http://www.parashift.com/c++-faq-lite/floating-point-arith.html
135  @see http://www.boost.org/doc/libs/1_38_0/libs/test/doc/html/utf/testing-tools/floating_point_comparison.html
136 */
137 inline bool IsVeryClose(double A, double B, double Epsilon = 0.00001)
138 {
139  // see Knuth section 4.2.2 pages 217-218
140  return (((std::fabs(A - B)) <= (Epsilon * std::fabs(A))) && ((std::fabs(A - B)) <= (Epsilon * std::fabs(B))));
141 }
142 
143 
144 /**
145  Checks if the given string matches the given pattern, including * and ? wildcards
146  @param[in] Pattern The pattern to match
147  @param[in] Str The string to test
148  @return true if the given string matches the given pattern
149 */
150 bool DLLEXPORT WildcardMatching(const std::string& Pattern, const std::string& Str);
151 
152 
153 /**
154  Gets the list of files with specified extension contained in the specified directory
155  @param[in] DirToExplore the directory to explore
156  @param[in] Ext the file extension
157  @param[in] WithPath return full path with file name if true, file name only otherwise
158  @param[in] ExtIncludeDot if true, the given extension through Ext parameter is suffixed by a dot
159 */
160 std::vector<std::string> DLLEXPORT GetFilesByExt(const std::string DirToExplore,
161  const std::string Ext,
162  bool WithPath = false,
163  bool ExtIncludeDot = false);
164 
165 
166 /**
167  Get list of files with specified extension contained in the specified dir
168  @param[in] DirToExplore the directory to explore
169  @param[in] Ext the file extension
170  @param[in] Suffix the file suffix
171  @param[in] WithPath return full path with file name if true, file name only otherwise
172  @param[in] ExtIncludeDot if true, the given extension through Ext parameter is suffixed by a dot
173 */
174 std::vector<std::string> DLLEXPORT GetFilesBySuffixAndExt(const std::string& DirToExplore,
175  const std::string& Suffix,
176  const std::string& Ext,
177  bool WithPath = false,
178  bool ExtIncludeDot = false);
179 
180 
181 /**
182  Splits the passed string into a std::string array, split using the given SepString
183  @param[in] StrToSplit the string to split
184  @param[in] Separators the string of separators used to split the string
185  @param[in] ReturnsEmpty if true, the empty strings are returned
186  @return a vector of strings
187 */
188 std::vector<std::string> DLLEXPORT SplitString(const std::string& StrToSplit,
189  const std::string& Separators,
190  bool ReturnsEmpty = false);
191 
192 
193 /**
194  Recursively removes all files and directories contained in the given directory.
195  It deletes the directory and recreates it.
196  @param[in] DirPath the directory to empty
197  @return true if successful
198 */
199 bool DLLEXPORT EmptyDirectoryRecursively(const std::string& DirPath);
200 
201 
202 /**
203  Recursively finds all the paths of the files that exist exist
204  in the given directory and subdirectories
205  @param[in] DirPath the directory to explore
206  @return a vector containing all files paths
207 */
208 std::vector<std::string> DLLEXPORT GetFilesRecursively(const std::string& DirPath);
209 
210 
211 /**
212  Replaces a string by another string if it is empty
213  @param[in] SourceStr the source string to process
214  @param[in] ReplaceStr the replacement string to use
215  @return the processed string
216 */
217 std::string DLLEXPORT ReplaceEmptyString(std::string SourceStr,
218  const std::string& ReplaceStr);
219 
220 
221 /**
222  Removes trailing slashes if any, useful for cleaning paths
223  @param[in] Str the string to process
224  @return the processed string
225 */
226 std::string DLLEXPORT RemoveTrailingSlashes(std::string Str);
227 
228 
229 // TODO check if it has to be removed
230 void DLLEXPORT printSTDOUT(std::vector<std::string> Strings, std::string Sep);
231 
232 
233 /**
234  Copies a source directory to destination path, including the root of the source directory.
235  If the destination path does not exists, it is created.
236  @param[in] SourcePath the source directory to copy
237  @param[in] IntoPath the destination directory
238  @param[in] DontCopyDotDirs flag for copying dot ('.*') directories. Default is false.
239 */
240 void DLLEXPORT CopyDirectoryRecursively(const std::string& SourcePath,
241  const std::string& IntoPath,
242  const bool DontCopyDotDirs = false);
243 
244 
245 /**
246  Copies a source directory to destination path, not including the root of the source directory.
247  If the destination path does not exists, it is created.
248  @param[in] SourcePath the source directory to copy
249  @param[in] IntoPath the destination directory
250  @param[in] DontCopyDotDirs flag for copying dot ('.*') directories. Default is false.
251 */
252 void DLLEXPORT CopyDirectoryContentsRecursively(const std::string& SourcePath,
253  const std::string& IntoPath,
254  const bool DontCopyDotDirs = false);
255 
256 
257 /**
258  Compares two OpenFLUID software versions. Version number must be formed as major.minor.patch[~status]
259  @param[in] VersionA the first version number
260  @param[in] VersionB the second version number
261  @param[in] Strict If true, the comparison include the status part of the version (it ignores it otherwise)
262  @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
263 */
264 int DLLEXPORT CompareVersions(const std::string& VersionA, const std::string& VersionB, bool Strict = true);
265 
266 
267 /*
268  Suspend execution for microseconds
269  @param[in] MSec the microseconds interval
270 */
271 void DLLEXPORT Sleep(const unsigned int MSec);
272 
273 
274 } } //namespaces
275 
276 
277 #endif // __SWISSTOOLS_H__
278 
std::vector< std::string > DLLEXPORT GetFilesRecursively(const std::string &DirPath)
int DLLEXPORT CompareVersions(const std::string &VersionA, const std::string &VersionB, bool Strict=true)
void DLLEXPORT CopyDirectoryRecursively(const std::string &SourcePath, const std::string &IntoPath, const bool DontCopyDotDirs=false)
std::vector< std::string > DLLEXPORT SplitString(const std::string &StrToSplit, const std::string &Separators, bool ReturnsEmpty=false)
bool IsVeryClose(double A, double B, double Epsilon=0.00001)
Definition: SwissTools.hpp:137
void DLLEXPORT CopyDirectoryContentsRecursively(const std::string &SourcePath, const std::string &IntoPath, const bool DontCopyDotDirs=false)
void DLLEXPORT TokenizeString(const std::string &StrToTokenize, std::vector< std::string > &Tokens, const std::string &Delimiters)
std::string DLLEXPORT RemoveTrailingSlashes(std::string Str)
void DLLEXPORT printSTDOUT(std::vector< std::string > Strings, std::string Sep)
bool ConvertValue(const T ValueToConvert, std::string *StrConverted)
Definition: SwissTools.hpp:86
std::vector< std::string > DLLEXPORT GetFilesByExt(const std::string DirToExplore, const std::string Ext, bool WithPath=false, bool ExtIncludeDot=false)
std::vector< std::string > DLLEXPORT GetFilesBySuffixAndExt(const std::string &DirToExplore, const std::string &Suffix, const std::string &Ext, bool WithPath=false, bool ExtIncludeDot=false)
bool DLLEXPORT WildcardMatching(const std::string &Pattern, const std::string &Str)
std::string DLLEXPORT ReplaceEmptyString(std::string SourceStr, const std::string &ReplaceStr)
bool DLLEXPORT EmptyDirectoryRecursively(const std::string &DirPath)
bool IsCloseEnough(double A, double B, double Epsilon=0.00001)
Definition: SwissTools.hpp:120
void DLLEXPORT Sleep(const unsigned int MSec)
bool ConvertString(const std::string &StrToConvert, T *Converted)
Definition: SwissTools.hpp:71
#define DLLEXPORT
Definition: dllexport.hpp:51