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