-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLATfield2_SettingsFile.hpp
executable file
·455 lines (374 loc) · 11.3 KB
/
LATfield2_SettingsFile.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#ifndef LATFIELD2_SETTINGSFILE_HPP
#define LATFIELD2_SETTINGSFILE_HPP
/*! \file LATfield2_SettingsFile.hpp
\brief LATfield2_SettingsFile.hpp contain the class SettingsFile definition.
\author N. Bevis
*/
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
//CLASS PROTOTYPE======================
/*! \class SettingsFile
\brief A utility class designed to make reading in runtime parameter values easier.
If the command-line arguments are input via optional inputs on
either the constructor or open member function, then these
take preceident: they are effectively last in the file.
Note, when used with std::string objects, only one word
is allowed per setting, ie. spaces are not allowed. This
is because of the way that the >> operator works for
this class. This fits nicely with the command-line override, however.
Note that the string specified followed by = is searched
for in the file and then the input read. If one setting
name is also the end of another that precedes it in the file
then the wrong one will be read.
Only the primary MPI process is able to create or add to the setting
file. Further processes will be sent the file contents via
MPI. To use this class in serial code the preprocessor defintion SERIAL must be set. This flag have not been remove to allow users to use it outside LATfield2. LATfield2 have no serial version, therefor setting prepocessor flag -DSERIAL should never be used with LATfield2.
*/
class SettingsFile
{
private:
std::string filename_;
std::fstream file_;
std::stringstream stream_;
int mode_;
bool isRoot_; //is process the root one (false if non-parallel)
//Search=================================
bool search(const std::string search_string);
public:
static int noCreate;
static int autoCreate;
//Constructors=======================
//! Constructor
SettingsFile();
/*!
Constructor + open a file
\param filename : path to the file.
\param mode : noCreate (the read method will exit if the parameter does not exist) or autoCreate (read will add the missing parameter).
\param argc : additionnal argument number.
\param argv : pointer to the additionnal arguments.
*/
SettingsFile(const std::string filename, const int mode, const int argc = 0, char** argv = NULL);
//Destructor=========================
//! desctructor
~SettingsFile();
//File open / close / create ========
/*!
Open an existinge settings file
\param filename : path to the file
\param mode : noCreate (the read method will exit if the parameter does not exist) or autoCreate (read will add the missing parameter).
\param argc : additional argument number.
\param argv : pointer to the additional arguments.
*/
void open(const std::string filename, const int mode, const int argc = 0, char** argv = NULL);
/*!
Close the current settings file
*/
void close();
/*!
Create a new settings file and open it.
\param filename: path to the file.
*/
void create(const std::string filename);
//Settings read / write==================
/*!
Method to read a parameter.
\param parameter_name : string containing the name of the parameter. If the parameter does not exite and the mode autocreate is set, this method will add the parameter to the settings file with the current value of "parameter". In the case the mode is set to nocreate, then read will exit for security, for this reason in it is always advise to set the mode to nocreate for production runs.
\param parameter : pointer to the variable where the parameter will be assigned.
*/
template<class TemplateClass>
void read(const std::string parameter_name, TemplateClass& parameter);
/*!
Method to add a parameter to the settings file. The new parameter will be just added to the end of the file, even if it already exists.
\param parameter_name: string containing the name of the parameter.
\param parameter: pointer to the value of the parameter.
*/
template<class TemplateClass>
void add(const std::string parameter_name, const TemplateClass& parameter);
/*!
Method to write a parameter in the settings file. If the parameter_name exist, it will overwrite the parameter. And if it does not exist in the file, it will be added at the end of the file.
\param parameter_name: string containing the name of the parameter
\param parameter: pointer to the value of the parameter.
*/
template<class TemplateClass>
void write(const std::string parameter_name, const TemplateClass& parameter);
};
//CONSTANTS===========================
int SettingsFile::noCreate = 1;
int SettingsFile::autoCreate = 0;
//CONSTRUCTORS========================
SettingsFile::SettingsFile()
{
#ifndef SERIAL
isRoot_=parallel.isRoot();
#else
isRoot_=true;
#endif
}
SettingsFile::SettingsFile(const std::string filename, const int mode, const int argc, char** argv)
{
#ifndef SERIAL
isRoot_=parallel.isRoot();
#else
isRoot_=true;
#endif
this->open(filename, mode, argc, argv);
}
//DESTRUCTOR==========================
SettingsFile::~SettingsFile() {this->close();}
//OPEN================================
void SettingsFile::open(const std::string filename, const int mode, const int argc, char** argv)
{
char c;
filename_=filename;
mode_=mode;
if(isRoot_)
{
//Open file
file_.open(filename_.c_str(), std::fstream::in);
if(!file_.is_open())
{
if((mode_ & SettingsFile::noCreate) == 0)
{
std::cout<<"SettingsFile: "<<filename_<<" not found."<<std::endl;
std::cout<<"SettingsFile: Creating..."<<std::endl;
this->create(filename_);
std::cout<<"creating ok"<<std::endl;
}
else
{
std::cout<<"SettingsFile: "<<filename_<<" not found and auto create off."<<std::endl;
std::cout<<"SettingsFile: Exiting..." << std::endl;
#ifndef SERIAL
parallel.abortRequest();
#else
exit(555);
#endif
}
}
//Read command line into stringstream
for(int i=0; i<argc; i++)
{
for(int j=0; argv[i][j]!='\0'; j++)
{
stream_<<argv[i][j];
}
stream_<<endl;
}
//Read file into stringstream
while(!file_.eof())
{
c=file_.get();
if(c=='#')
{
while(!file_.eof() && c!='\n') { c=file_.get(); }
if(file_.eof()) { break; }
}
stream_.put(c);
}
file_.close();
}
#ifndef SERIAL
//Broadcast results to all processes
parallel.barrier();
if(parallel.size()>1)
{
if(parallel.isRoot())
{
int len = stream_.str().length();
char* streamString = new char[len+1];
for(int i=0;i<=len;i++) { streamString[i]=stream_.str()[i]; }
parallel.broadcast(len, parallel.root());
parallel.broadcast(streamString, len+1, parallel.root());
}
else
{
int len;
char* streamString;
parallel.broadcast(len, parallel.root());
streamString=new char[len+1];
parallel.broadcast(streamString, len+1, parallel.root());
stream_<<streamString;
}
}
#endif
}
//FILE CLOSE============================
void SettingsFile::close()
{
if(isRoot_) { filename_="."; }
}
//FILE CREATE===========================
void SettingsFile::create(const std::string filename)
{
if(isRoot_)
{
filename_=filename;
mode_=autoCreate;
file_.open(filename_.c_str(), std::fstream::out);
if(!file_.is_open())
{
std::cout<<"SettingsFile: Cannot create: "<<filename<<std::endl;
std::cout<<"SettingsFile: Exiting..."<<std::endl;
#ifndef SERIAL
parallel.abortRequest();
#else
exit(555);
#endif
}
else
{
file_.close();
file_.clear();
file_.open(filename.c_str(), std::fstream::in);
}
}
//parallel.barrier();
}
//PARAMETER READ===========================
template<class TemplateClass>
void SettingsFile::read(const std::string parameterName, TemplateClass ¶meter)
{
if(this->search(parameterName+'='))
{
stream_>>parameter;
}
else
{
if(isRoot_)
{
//verifiy that the parameter name is no autocreate
if(parameterName!="autocreate")
{
std::cout<<"SettingsFile: "<<filename_<<" has no parameter: "<<parameterName<<std::endl;
std::cout<<"SettingsFile: No command-line override given either"<<std::endl;
if((mode_ & SettingsFile::noCreate) == 0 )
{
std::cout << "SettingsFile: Adding with current value: " << parameter << std::endl;
this->add(parameterName, parameter);
}
else
{
std::cout << "SettingsFile: Auto create off. Exiting..." << std::endl;
#ifndef SERIAL
parallel.abortRequest();
#else
exit(555);
#endif
}
}
}
#ifndef SERIAL
parallel.barrier();
#endif
}
}
//PARAMETER WRITE===========================
template<class TemplateClass>
void SettingsFile::add(const std::string parameter_name, const TemplateClass ¶meter)
{
if(isRoot_)
{
file_.clear();
file_.open(filename_.c_str(), std::ios::out | std::ios::app);
file_ << parameter_name << '=' << parameter << std::endl;
if(!file_.good())
{
std::cout << "SettingsFile: Could not write to file: " << filename_ << std::endl;
std::cout << "SettingsFile: Exiting... " << std::endl;
#ifndef SERIAL
parallel.abortRequest();
#else
exit(555);
#endif
}
file_.close();
}
}
template<class TemplateClass>
void SettingsFile::write(const std::string parameter_name, const TemplateClass ¶meter)
{
if(isRoot_)
{
unsigned int i=0;
unsigned int l=0;
int line_number=0;
char c;
string line_temp;
if(file_.good())file_.close();
file_.clear();
file_.open(filename_.c_str(), std::ios::in);
//get number of non void line
file_.seekg(0);
while (!file_.eof())
{
getline(file_,line_temp);
line_number++;
}
//creat line array and this_param array
string * lines;
lines = new string[line_number];
bool this_param[line_number];
file_.seekg(0);
file_.close();
file_.open(filename_.c_str(), std::ios::in);
//implement non void lines
l=0;
while (!file_.eof())
{
getline(file_,line_temp);
lines[l]=line_temp;
l++;
}
for(l=0;l<line_number;l++)
{
for(i=0;i<parameter_name.length();i++)
{
c = lines[l][i];
if(c==parameter_name[i])this_param[l]=true;
else
{
this_param[l]=false;
i=parameter_name.length();
}
}
}
file_.close();
file_.open(filename_.c_str(), std::ios::out | std::ios::trunc);
bool writen;
for(l=0;l<line_number;l++)
{
if(!this_param[l])file_<<lines[l]<<endl;
else
{
file_<< parameter_name <<'='<<parameter<<endl;
writen = true;
}
}
if(!writen)file_<< parameter_name <<'='<<parameter<<endl;
file_.close();
file_.clear();
}
#ifndef SERIAL
parallel.barrier();
#endif
}
//SEARCH=====================================
bool SettingsFile::search(const std::string searchString)
{
unsigned int i=0;
char c;
//Set to beginning of file
stream_.clear(); //clear any errors from having failed a previous search
stream_.seekg(0);
//Search
while(stream_.good() && i<searchString.length())
{
c=stream_.get();
if(c==searchString[i]){i++;}
else{i=0;}
}
return stream_.good();
}
#endif