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