Manual for OpenFLUID 2.1.10

DataHelpers.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 DataHelpers.hpp
35 
36  @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
37  */
38 
39 
40 #ifndef __OPENFLUID_TOOLS_DATAHELPERS_HPP__
41 #define __OPENFLUID_TOOLS_DATAHELPERS_HPP__
42 
43 
44 #include <string>
45 #include <list>
46 #include <sstream>
47 #include <vector>
48 #include <set>
49 
50 #include <openfluid/dllexport.hpp>
53 
54 
55 namespace openfluid { namespace tools {
56 
57 
58 /**
59  Converts a string to another type
60  @param[in] StrToConvert the string to convert
61  @param[out] Converted the result of the conversion
62  @return true if the conversion is correct
63 */
64 template<typename T>
65 inline bool convertString(const std::string& StrToConvert, T* Converted)
66 {
67  std::istringstream iss(StrToConvert);
68  char c;
69  return ((iss >> (*Converted)) && !iss.get(c));
70 }
71 
72 
73 // =====================================================================
74 // =====================================================================
75 
76 
77 /**
78  Converts a string to another type
79  @param[in] StrToConvert the string to convert
80  @return true the string converted
81  @throws openfluid::base::FrameworkException Error in string conversion
82 */
83 template<typename T>
84 inline T convertString(const std::string& StrToConvert)
85 {
86  T Converted;
87 
88  if (convertString(StrToConvert,&Converted))
89  {
90  return Converted;
91  }
92 
93  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Error in string conversion");
94 }
95 
96 
97 // =====================================================================
98 // =====================================================================
99 
100 
101 /**
102  @deprecated Since version 2.1.0. Use openfluid::tools::convertString instead
103 */
104 template<typename T>
105 [[deprecated]] inline bool ConvertString(const std::string& StrToConvert, T* Converted);
106 template<typename T>
107 inline bool ConvertString(const std::string& StrToConvert, T* Converted)
108 {
109  return convertString(StrToConvert,Converted);
110 }
111 
112 
113 // =====================================================================
114 // =====================================================================
115 
116 
117 /**
118  Converts a value to a string
119  @param[in] ValueToConvert the value to convert
120  @param[out] StrConverted the result of the conversion
121  @return true if the conversion is correct
122 */
123 template<typename T>
124 inline bool convertValue(const T& ValueToConvert, std::string* StrConverted)
125 {
126  std::ostringstream oss;
127  bool IsOK = !(oss << ValueToConvert).fail();
128 
129  if (IsOK)
130  {
131  (*StrConverted) = oss.str();
132  }
133 
134  return IsOK;
135 }
136 
137 
138 // =====================================================================
139 // =====================================================================
140 
141 
142 /**
143  Converts a value to a string
144  @param[in] ValueToConvert the value to convert
145  @return the converted value
146  @throws openfluid::base::FrameworkException Error in value conversion
147 */
148 template<typename T>
149 inline std::string convertValue(const T& ValueToConvert)
150 {
151  std::string Converted;
152 
153  if (convertValue(ValueToConvert,&Converted))
154  {
155  return Converted;
156  }
157 
158  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Error in value conversion");
159 }
160 
161 
162 // =====================================================================
163 // =====================================================================
164 
165 
166 /**
167  @deprecated Since version 2.1.0. Use openfluid::tools::convertValue instead
168 */
169 template<typename T>
170 [[deprecated]] inline bool ConvertValue(const T& ValueToConvert, std::string* StrConverted);
171 template<typename T>
172 inline bool ConvertValue(const T& ValueToConvert, std::string* StrConverted)
173 {
174  return convertValue(ValueToConvert,StrConverted);
175 }
176 
177 
178 // =====================================================================
179 // =====================================================================
180 
181 
182 /**
183  Function for tokenizing string into a vector of tokens
184  @param[in] StrToTokenize the string to tokenize
185  @param[out] Tokens the resulting tokens
186  @param[in] Delimiters the string delimiter
187 */
188 void OPENFLUID_API tokenizeString(const std::string& StrToTokenize,
189  std::vector<std::string>& Tokens,
190  const std::string& Delimiters);
191 
192 
193 /**
194  @deprecated Since version 2.1.0. Use openfluid::tools::tokenizeString instead
195 */
196 [[deprecated]] inline void TokenizeString(const std::string& StrToTokenize,
197  std::vector<std::string>& Tokens,
198  const std::string& Delimiters);
199 inline void TokenizeString(const std::string& StrToTokenize,
200  std::vector<std::string>& Tokens,
201  const std::string& Delimiters)
202 {
203  tokenizeString(StrToTokenize,Tokens,Delimiters);
204 }
205 
206 
207 /**
208  Splits the given string into a std::string array, split using the given SepString
209  @param[in] StrToSplit the string to split
210  @param[in] Separators the string of separators used to split the string
211  @param[in] ReturnsEmpty if true, the empty strings are returned
212  @return a vector of strings
213 */
214 std::vector<std::string> OPENFLUID_API splitString(const std::string& StrToSplit,
215  const std::string& Separators,
216  bool ReturnsEmpty = false);
217 
218 /**
219  @deprecated Since version 2.1.0. Use openfluid::tools::splitString instead
220 */
221 [[deprecated]] inline std::vector<std::string> SplitString(const std::string& StrToSplit,
222  const std::string& Separators,
223  bool ReturnsEmpty = false) ;
224 inline std::vector<std::string> SplitString(const std::string& StrToSplit,
225  const std::string& Separators,
226  bool ReturnsEmpty)
227 {
228  return splitString(StrToSplit,Separators,ReturnsEmpty);
229 }
230 
231 
232 /**
233  Splits the given string into a std::string array, split using the given SepString
234  @param[in] StrToSplit the string to split
235  @param[in] Separator the separator character used to split the string
236  @param[in] ReturnsEmpty if true, the empty strings are returned
237  @return a vector of strings
238 */
239 std::vector<std::string> OPENFLUID_API splitString(const std::string& StrToSplit,
240  char Separator,
241  bool ReturnsEmpty = false);
242 
243 
244 /**
245  Replace every occurrence of a given substring (target) in a string by wanted replacement substring
246  @param[in] Data the string to work on
247  @param[in] ToSearch the target substring
248  @param[in] ReplaceStr the replacement substring
249 */
250 void OPENFLUID_API stringReplace(std::string& Data, std::string ToSearch, std::string ReplaceStr);
251 
252 
253 } } // namespaces
254 
255 
256 #endif /* __OPENFLUID_TOOLS_DATAHELPERS_HPP__ */
std::vector< std::string > SplitString(const std::string &StrToSplit, const std::string &Separators, bool ReturnsEmpty=false)
Definition: DataHelpers.hpp:224
void TokenizeString(const std::string &StrToTokenize, std::vector< std::string > &Tokens, const std::string &Delimiters)
Definition: DataHelpers.hpp:199
Definition: ApplicationException.hpp:47
bool convertString(const std::string &StrToConvert, T *Converted)
Definition: DataHelpers.hpp:65
void OPENFLUID_API stringReplace(std::string &Data, std::string ToSearch, std::string ReplaceStr)
Definition: FrameworkException.hpp:50
bool convertValue(const T &ValueToConvert, std::string *StrConverted)
Definition: DataHelpers.hpp:124
bool ConvertString(const std::string &StrToConvert, T *Converted)
Definition: DataHelpers.hpp:107
#define OPENFLUID_API
Definition: dllexport.hpp:86
bool ConvertValue(const T &ValueToConvert, std::string *StrConverted)
Definition: DataHelpers.hpp:172
void OPENFLUID_API tokenizeString(const std::string &StrToTokenize, std::vector< std::string > &Tokens, const std::string &Delimiters)
std::vector< std::string > OPENFLUID_API splitString(const std::string &StrToSplit, const std::string &Separators, bool ReturnsEmpty=false)