MiscHelpers.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  @file MiscHelpers.hpp
35 
36  @author Jean-Christophe Fabre <jean-christophe.fabre@inra.fr>
37 */
38 
39 
40 #ifndef __OPENFLUID_TOOLS_MISCHELPERS_HPP__
41 #define __OPENFLUID_TOOLS_MISCHELPERS_HPP__
42 
43 
44 #include <vector>
45 #include <string>
46 #include <cmath>
47 
48 #include <openfluid/dllexport.hpp>
49 #include <openfluid/core/TypeDefs.hpp>
50 
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  Checks if the given string matches the given pattern, including * and ? wildcards
66  @param[in] Pattern The pattern to match
67  @param[in] Str The string to test
68  @return true if the given string matches the given pattern
69 */
70 bool OPENFLUID_API matchWithWildcard(const std::string& Pattern, const std::string& Str);
71 
72 
73 /**
74  Replaces a string by another string if it is empty
75  @param[in] SourceStr the source string to process
76  @param[in] ReplaceStr the replacement string to use
77  @return the processed string
78 */
79 std::string OPENFLUID_API replaceEmptyString(std::string SourceStr,
80  const std::string& ReplaceStr);
81 
82 
83 /**
84  Removes trailing slashes if any, useful for cleaning paths
85  @param[in] Str the string to process
86  @return the processed string
87 */
88 std::string OPENFLUID_API removeTrailingSlashes(const std::string& Str);
89 
90 
91 /**
92  Compares two OpenFLUID software versions. Version number must be formed as major.minor.patch[~status]
93  @param[in] VersionA the first version number
94  @param[in] VersionB the second version number
95  @param[in] Strict If true, the comparison include the status part of the version (it ignores it otherwise)
96  @return 1 if VersionA is greater than VersionB,
97  -1 if VersionB is greater than VersionA,
98  0 if versions are equals,
99  -2 if a version format is not well-formed
100 */
101 int OPENFLUID_API compareVersions(const std::string& VersionA, const std::string& VersionB, bool Strict = true);
102 
103 
104 /*
105  Suspends execution of current thread for milliseconds
106  @param[in] MSec the milliseconds interval
107 */
108 void OPENFLUID_API millisleep(const unsigned long MSec);
109 
110 
111 /*
112  Suspend execution of current thread for microseconds
113  @param[in] USec the microseconds interval
114 */
115 void OPENFLUID_API microsleep(const unsigned long USec);
116 
117 
118 /**
119  Generates a pseudo-unique identifier using alphanumeric characters and Mersenne Twister random engine.
120  @param[in] Length the length of the identifier to generate
121  @return the generated identifier
122 */
123 std::string OPENFLUID_API generatePseudoUniqueIdentifier(const unsigned int Length);
124 
125 
126 /**
127  splits the given duration in milliseconds into days, hours, minutes, seconds and milliseconds.
128  @param[in] MSecsDuration the duration in milliseconds
129  @param[out] Days the number of days
130  @param[out] Hours the number of hours
131  @param[out] Minutes the number of minutes
132  @param[out] Seconds the number of seconds
133  @param[out] MSecs the number of milliseconds
134 */
135 void OPENFLUID_API splitDuration(long int MSecsDuration, int& Days, int& Hours, int& Minutes, int& Seconds, int& MSecs);
136 
137 
138 /**
139  returns the given duration as a pretty string representing days, hours, minutes and decimal seconds.
140  @code
141  openfluid::tools::getDurationAsPrettyString(123456789); // returns "1d 10h 17m 36.789s"
142  openfluid::tools::getDurationAsPrettyString(12345678); // returns "3h 25m 45.678s"
143  openfluid::tools::getDurationAsPrettyString(123456); // returns "2m 3.456s"
144  openfluid::tools::getDurationAsPrettyString(1234); // returns "1.234s"
145  openfluid::tools::getDurationAsPrettyString(12); // returns "0.012s"
146  @endcode
147  @param[in] MSecsDuration the duration in milliseconds
148  @return the converted duration as a string
149 */
150 std::string OPENFLUID_API getDurationAsPrettyString(long int MSecsDuration);
151 
152 
153 } } //namespaces
154 
155 
156 #endif // __OPENFLUID_TOOLS_MISCHELPERS_HPP__
157 
std::string OPENFLUID_API removeTrailingSlashes(const std::string &Str)
int OPENFLUID_API compareVersions(const std::string &VersionA, const std::string &VersionB, bool Strict=true)
std::string OPENFLUID_API replaceEmptyString(std::string SourceStr, const std::string &ReplaceStr)
std::string OPENFLUID_API getDurationAsPrettyString(long int MSecsDuration)
std::string OPENFLUID_API generatePseudoUniqueIdentifier(const unsigned int Length)
void OPENFLUID_API splitDuration(long int MSecsDuration, int &Days, int &Hours, int &Minutes, int &Seconds, int &MSecs)
Definition: ApplicationException.hpp:47
void OPENFLUID_API microsleep(const unsigned long USec)
void OPENFLUID_API millisleep(const unsigned long MSec)
#define OPENFLUID_API
Definition: dllexport.hpp:86
bool OPENFLUID_API matchWithWildcard(const std::string &Pattern, const std::string &Str)