C++ main module for stochmagnet Package  1.0
CORE_OptionsReader.h
Go to the documentation of this file.
1 #ifndef CORE_OptionsReader_H
2 #define CORE_OptionsReader_H
3 
4 //base class header
5 #include "CORE_Object.h"
6 
7 //file system header
8 #include "CORE_IO.h"
9 //output stream
10 #include "CORE_Out.h"
11 //map header
12 #include <map>
13 
14 //string conversion header
15 #include<charconv>
16 #include <string>
17 
18 //file header
19 #include <fstream>
20 
21 //data header
22 #include "CORE_Chrono.h"
23 
49 class CORE_OptionsReader : public virtual CORE_Object {
50 
51  //attributes
52 private :
53 
54 
55 
56 public:
57  // CONSTRUCTORS
61  }
62 
63  // DESTRUCTORS
66  virtual ~CORE_OptionsReader(void) {
67  }
68 
69 
70 
71 public :
72  // CREATE class
73 
74 
75  //options reader
76  //==============
84  virtual tMemSize getMemorySize() const override {
85  return sizeof(*this)+getContentsMemorySize();
86  }
87 
88 
94  static tBoolean ReadOptions(const tString& fileName, std::map<tString,tString>& options);
95 
101  static void ReadOptions(int argc,char *argv[],std::map<tString,tString>& options);
102 
110  static tBoolean ReadOption(int nArgs,char *argv[],const tString& optionName,tString& optionValue);
111 
116  template<typename T,std::enable_if_t<std::is_floating_point_v<T>> * = nullptr>
117  inline static T ReadNumericValue(const tString& strValue) {
118  T var;
119  std::istringstream iss;
120  iss.str(strValue);
121  iss >> var;
122  // deal with any error bits that may have been set on the stream
123  return var;
124 
125  }
126 
131  template<typename T,std::enable_if_t<std::is_integral_v<T>> * = nullptr>
132  inline static T ReadNumericValue(const tString& strValue) {
133  T var=0;
134  std::from_chars(strValue.data(), strValue.data() + strValue.size(), var);
135  return var;
136 
137  }
138 
143  inline static tReal ReadReal(const tString& strValue) {
144  return ReadNumericValue<tReal>(strValue);
145 
146  }
151  inline static tInteger ReadInteger(const tString& strValue) {
152  return ReadNumericValue<tInteger>(strValue);
153 
154  }
159  inline static tInteger ReadRelativeInteger(const tString& strValue) {
160  return ReadNumericValue<tRelativeInteger>(strValue);
161 
162  }
167  inline static tInt ReadInt(const tString& strValue) {
168  return ReadNumericValue<tInt>(strValue);
169 
170  }
171 
172 
178  inline static tBoolean ReadBoolean(const tString& strValue) {
179  if (strValue.length()==0) return true;
180  tString boolValue=strValue;
181  core_functions::trim(boolValue);
182  core_functions::toLower(boolValue);
183  if (strValue.compare("")==0) return true;
184  if (strValue.compare("true")==0) return true;
185  if (strValue.compare("1")==0) return true;
186  return false;
187  }
188 
189 
190  template<class T>
191  inline static tBoolean ReadVector(tString& current,std::vector<T>& values) {
192  // length of the string
193  tIndex lmax=current.length();
194 
195 
196  // find a [, current is set to the chars after [
197  tIndex indexOB=current.find("[");
198  if (indexOB!=tString::npos) {
199  current=current.substr(indexOB+1);
200  lmax=current.length();
201  values.clear();
202  }
203 
204 
205 
206  //max index to read
207  tIndex indexMax=current.find("]");
208  if (indexMax==tString::npos) indexMax=lmax;
209  // find a coma
210  tIndex indexC=current.find(",");
211  tIndex i=0;
212  T value;
213  while (indexC<indexMax) {//the first exists
214  // read the value at index i
215  core_functions::parse<T>(current.substr(0,indexC),value);
216  // add it
217  values.push_back(value);
218  // get the next value
219  current=current.substr(indexC+1,lmax-indexC-1);
220  lmax=current.length();
221  indexC=current.find(",");
222  indexMax=current.find("]");
223  if (indexMax==tString::npos) {
224  indexMax=lmax;
225  }
226 
227  i++;
228  }
229 
230 
231  //set the current to chars after ] it it exists
232 
233  tString v=current;
234  if (indexMax<lmax) {
235  // take the string v before ]
236  v=current.substr(0,indexMax);
237  // erase from current all the string before ]
238  current=current.substr(indexMax+1,lmax-indexMax-1);
239  // erase the ,
240  if (current[0]==',') current=current.substr(1);
241  }
242 
243 
244 
245  // parse the last value
246  core_functions::parse(v,value);
247  values.push_back(value);
248 
249 
250  return true;
251  }
252 
253  template<class T,tIndex N>
254  inline static tBoolean ReadArray(tString& current,std::array<T,N>& values) {
255 
256 
257  //initialize
258 
259  for(auto& v:values) v=0;
260  T* iValue=values.data();
261  const T* eValue=values.data()+N;
262 
263  // length of the string
264  tIndex lmax=current.length();
265 
266 
267  // find a [, current is set to the chars after [
268  tIndex indexOB=current.find("[");
269  if (indexOB!=tString::npos) {
270  current=current.substr(indexOB+1);
271  lmax=current.length();
272  }
273 
274 
275 
276  //max index to read
277  tIndex indexMax=current.find("]");
278  if (indexMax==tString::npos) indexMax=lmax;
279  // find a coma
280  tIndex indexC=current.find(",");
281  while ((indexC<indexMax)&&(iValue!=eValue)) {//the first exists
282 
283  // read the value at index i
284  core_functions::parse<T>(current.substr(0,indexC),(*iValue));
285  iValue++;
286 
287  // get the next value
288  current=current.substr(indexC+1,lmax-indexC-1);
289  lmax=current.length();
290  indexC=current.find(",");
291  indexMax=current.find("]");
292  if (indexMax==tString::npos) {
293  indexMax=lmax;
294  }
295 
296  }
297 
298 
299  //set the current to chars after ] it it exists
300  tString v=current;
301  if (indexMax<lmax) {
302  // take the string v before ]
303  v=current.substr(0,indexMax);
304  // erase from current all the string before ]
305  current=current.substr(indexMax+1,lmax-indexMax-1);
306  // erase the ,
307  if (current[0]==',') current=current.substr(1);
308  }
309 
310 
311 
312  // parse the last value
313  if (iValue!=eValue) core_functions::parse(v,(*iValue));
314 
315 
316  return true;
317  }
318 
322  inline static tString ReadPath(const tString& value) {
323  return CORE_IO::GetAbsolutePath(value);
324  }
325 
326 
327  //options saver
328  //=============
333  inline static tBoolean SaveOptionsToFile(const std::map<tString,std::map<tString,tString> >& manOptions,
334  const std::map<tString,tString>& options) {
335 
336  //get the output path
337  std::stringstream fileName;
338  std::map<tString,tString>::const_iterator iOptions=options.find("output-path");
339  if (iOptions!=options.end()) fileName<<ReadPath(iOptions->second);
340  else fileName<<".";
341  fileName<<"/options.txt";
342  return SaveOptionsToFile(fileName.str(),manOptions,options);
343  }
344 
350  static tBoolean SaveOptionsToFile(const tString& fileName,
351  const std::map<tString,std::map<tString,tString> >& manOptions,
352  const std::map<tString,tString>& options);
353 
359  static inline CORE_Out& PrintOptions(CORE_Out& out,
360  const std::map<tString,tString>& options) {
361 
362  for_each(options.begin(),options.end(),
363  [&](auto& opt) {
364  out.println(opt.first+"="+opt.second);
365  });
366  return out;
367  }
368 
369 
370 private:
378  static void RegisterOption(tString& arg,std::map<tString,tString>& options);
379 
387  static void ReadOptionValue(tString& optionValue);
388 
389 
390 };
391 
392 
393 #endif
static tString GetAbsolutePath(const tString &path)
get the absolute path of the path
Definition: CORE_IO.h:341
abstract base class for most classes.
Definition: CORE_Object.h:48
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Object.h:259
This class is a Run class for core package.
Definition: CORE_OptionsReader.h:49
static tBoolean ReadArray(tString &current, std::array< T, N > &values)
Definition: CORE_OptionsReader.h:254
static tString ReadPath(const tString &value)
read a path from a string
Definition: CORE_OptionsReader.h:322
static void RegisterOption(tString &arg, std::map< tString, tString > &options)
register the option
Definition: CORE_OptionsReader.cpp:131
static tInteger ReadRelativeInteger(const tString &strValue)
read a relative integer
Definition: CORE_OptionsReader.h:159
virtual ~CORE_OptionsReader(void)
destroy
Definition: CORE_OptionsReader.h:66
CORE_OptionsReader(void)
create
Definition: CORE_OptionsReader.h:60
static tBoolean SaveOptionsToFile(const std::map< tString, std::map< tString, tString > > &manOptions, const std::map< tString, tString > &options)
save the options to the file
Definition: CORE_OptionsReader.h:333
virtual tMemSize getMemorySize() const override
return the memory size of the class
Definition: CORE_OptionsReader.h:84
static tBoolean ReadOption(int nArgs, char *argv[], const tString &optionName, tString &optionValue)
read the only the option with optionName in command line
Definition: CORE_OptionsReader.cpp:169
static tInteger ReadInteger(const tString &strValue)
read a natural integer
Definition: CORE_OptionsReader.h:151
static CORE_Out & PrintOptions(CORE_Out &out, const std::map< tString, tString > &options)
print the options
Definition: CORE_OptionsReader.h:359
static tReal ReadReal(const tString &strValue)
read areal
Definition: CORE_OptionsReader.h:143
static tBoolean ReadOptions(const tString &fileName, std::map< tString, tString > &options)
read the options from a file
Definition: CORE_OptionsReader.cpp:5
static void ReadOptionValue(tString &optionValue)
read the option value
Definition: CORE_OptionsReader.cpp:215
static tBoolean ReadVector(tString &current, std::vector< T > &values)
Definition: CORE_OptionsReader.h:191
static T ReadNumericValue(const tString &strValue)
read a numeric value of type T
Definition: CORE_OptionsReader.h:117
static tInt ReadInt(const tString &strValue)
read an int
Definition: CORE_OptionsReader.h:167
static tBoolean ReadBoolean(const tString &strValue)
read the boolean from option
Definition: CORE_OptionsReader.h:178
this class describes the output stream by default write on standart output
Definition: CORE_Out.h:28
tString & trim(tString &s)
remove all the empty characters at the begining and end of the string
Definition: functions.h:349
void parse(const tString &s, T &v)
parse the string
Definition: functions.h:518
void toLower(tString &s)
turn a string to lower value
Definition: functions.h:375
#define tIndex
Definition: types.h:157
#define tString
Definition: types.h:147
#define tInt
Definition: types.h:47
#define tMemSize
Definition: types.h:166
#define tInteger
Definition: types.h:114
#define tBoolean
Definition: types.h:151
#define tReal
Definition: types.h:137