/
checksettingfilecontent.cpp
331 lines (218 loc) · 8.15 KB
/
checksettingfilecontent.cpp
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
// Copyright (C) 2012-2017, Ágoston Sasvári
// All rights reserved.
// This code is published under the GNU Lesser General Public License.
#include <cstdlib> // TODO Instead of atof, a string_to_int function should be used in common.cpp
#include <fstream>
#include <iomanip>
#include <iostream>
#include "assertions.hpp"
#include "checksettingfilecontent.h"
#include "exceptions.hpp"
#include "generate_default_settings.hpp"
#include "run_mode.h"
#include "settings.hpp"
vector < vector <string> > READ_SETTINGS_FILE (const string settingfilename) {
vector < vector <string> > SET;
ifstream inputfile (settingfilename.c_str());
while (!inputfile.eof()) {
string key, value;
vector <string> buf;
getline(inputfile, key, '\t');
getline(inputfile, value);
const size_t len = value.size();
if (len > 0 && value.at(len-1)=='\r') { // on Linux/Mac, reading files from Windows
value.erase(len-1);
}
if (!is_WHITE_CHARACTER (key) && !is_WHITE_CHARACTER (value)) {
buf.push_back (to_uppercase (key));
buf.push_back (to_uppercase (value));
SET.push_back(buf);
}
}
return SET;
}
bool is_WHITE_CHARACTER (const string in) {
size_t length = in.size();
if (length == 0) return true;
else {};
bool is_character_white = false;
for (size_t i = 0; i < length; i++) if (in.at(i) == '\t' || in.at(i) == '\n' ) is_character_white = true;
return is_character_white;
}
bool is_RECORD_CORRECT (const vector<string>& SETrecord, const vector<vector<vector<string> > >& DEF, size_t j, size_t k) {
string actual_key = SETrecord.at(0);
string list_key = DEF.at(j).at(0).at(0);
if (actual_key != list_key) return false;
string actual_value = capslock (SETrecord.at(1));
string list_value = capslock (DEF.at(j).at(k).at(0));
if (actual_key == "LINEWIDTH:" || actual_key == "STRESSANGLE:" || actual_key == "WELLINTERVAL_LENGTH:") {
bool conv_has_failed = true;
double min_value = string_to_double (DEF.at(j).at(k).at(1), conv_has_failed);
double max_value = string_to_double (DEF.at(j).at(k).at(2), conv_has_failed);
string_to_int (actual_value, conv_has_failed);
if (actual_key == "LINEWIDTH:" && conv_has_failed) return false;
if (actual_key == "STRESSANGLE:" && conv_has_failed) return false;
if (actual_key == "WELLINTERVAL_LENGTH:" && conv_has_failed) return false;
double value = string_to_double (actual_value, conv_has_failed);
if (is_in_range(min_value, max_value, value) && !conv_has_failed) return true;
else return false;
}
else if (actual_key == "CLUSTERNUMBER:") {
if (actual_value == "A" || actual_value == "N") {
if (actual_value == list_value) return true;
else return false;
}
else {
bool conv_has_failed = true;
double min_value = string_to_double (DEF.at(j).at(k).at(1), conv_has_failed);
double max_value = string_to_double (DEF.at(j).at(k).at(2), conv_has_failed);
double value = string_to_double (actual_value, conv_has_failed);
if (is_in_range(min_value, max_value, value) && !conv_has_failed) return true;
else return false;
}
}
else {
if (actual_value == list_value) return true;
else return false;
}
}
bool is_SETTINGS_RECORD_CORRECT (const vector <string>& SETrecord) {
const vector <vector <vector <string> > > DEF = RETURN_ALL_SETTINGS ();
for (size_t j = 0; j < DEF.size(); j++) {
for (size_t k = 1; k < DEF.at(j).size(); k++) {
if (is_RECORD_CORRECT (SETrecord, DEF, j, k)) return true;
}
}
return false;
}
bool is_SETTINGS_FILE_CORRECT (const string settingfilename) {
ifstream inputfile (settingfilename.c_str());
if (! inputfile.is_open()) return false;
const vector <vector <string> > SET = READ_SETTINGS_FILE (settingfilename);
if (SET.size() == 0) return false;
for (size_t i = 0; i < SET.size(); i++) {
if (!is_SETTINGS_RECORD_CORRECT (SET.at(i))){
writeln (" - Incorrect key/value pair for '" + SET.at(i).at(0)+ "' user input key.");
return false;
}
else {
writeln (" - Correct key/value pair for '" + SET.at(i).at(0)+ "' user input key.");
}
}
return true;
}
vector <vector <string> > RETURN_HARDCODED_SETTINGS () {
vector < vector < vector <string> > > DEF = RETURN_ALL_SETTINGS ();
vector < vector <string> > OUT;
for (size_t i = 0; i < DEF.size(); i++) {
vector <string> buf;
buf.push_back(DEF.at(i).at(0).at(0));
if (DEF.at(i).at(0).at(0) == "LINEWIDTH:") buf.push_back("6");
else if (DEF.at(i).at(0).at(0) == "WELLINTERVAL_LENGTH:") buf.push_back("100");
else if (DEF.at(i).at(0).at(0) == "STRESSANGLE:") buf.push_back("30");
else buf.push_back(DEF.at(i).at(1).at(0));
OUT.push_back(buf);
}
return OUT;
}
vector <vector <string> > COMPLETE_SET_WITH_DEFAULT (const vector <vector <string> >& SET) {
vector <vector <vector <string> > > DEF = RETURN_ALL_SETTINGS ();
vector <vector <string> > OUT;
for (size_t i = 0; i < DEF.size(); i++) {
string out_key = DEF.at(i).at(0).at(0);
string out_value = DEF.at(i).at(1).at(0);
for (size_t j = 0; j < SET.size(); j++) {
if (SET.at(j).at(0) == out_key) out_value = SET.at(j).at(1);
}
vector <string> buf;
buf.push_back (out_key);
buf.push_back (out_value);
OUT.push_back (buf);
}
return OUT;
}
void LOAD_SETTINGS (const string projectname) {
vector <vector <string> > SET = READ_SETTINGS_FILE (projectname + ".set");
SET = COMPLETE_SET_WITH_DEFAULT (SET);
dump_settings (SET);
INIT_SETTINGS (SET);
return;
}
void WRITE_SET_FILE (const vector <vector <string> >& SET, const string projectname) {
ofstream o;
o.open ((projectname + ".set").c_str());
for (size_t i = 0; i < SET.size(); i++) o << SET.at(i).at(0) << '\t' << SET.at(i).at(1) << endl;
}
void CHECK_SETTINGS (const string projectname) {
ASSERT_EXACTLY_ONE_TRUE (is_mode_STD(), is_mode_DEBUG());
const string SFN = capslock (projectname + ".set");
const string sfn = projectname + ".set";
writeln ("");
writeln ("============================");
writeln ("1) CHECKING OF SETTINGS FILE");
writeln ("============================");
writeln ("");
writeln (" - CHECKING " + SFN + " SETTINGS FILE");
const bool CORR_SET = is_SETTINGS_FILE_CORRECT (sfn);
if (CORR_SET) {
writeln (" - " + SFN + " SETTINGS FILE IS CORRECT.");
vector <vector <string> > SET = READ_SETTINGS_FILE (sfn);
SET = COMPLETE_SET_WITH_DEFAULT (SET);
INIT_SETTINGS (SET);
}
else {
writeln ("");
writeln (" WARNING, SET_ERROR: the input " + SFN + " file structure is incorrect, the file will not be processed.");
throw set_error();
}
}
void dump_settings (const vector <vector <string> >& SET) {
writeln ("");
writeln ("===============================");
writeln ("3) SETTINGS USED FOR EVALUATION");
writeln ("===============================");
writeln ("");
vector <vector <vector <string> > > DEF = RETURN_ALL_SETTINGS ();
for (size_t i = 0; i < SET.size(); i++) {
for (size_t j = 0; j < DEF.size(); j++) {
for (size_t k = 1; k < DEF.at(j).size(); k++) {
if (is_RECORD_CORRECT (SET.at(i), DEF, j, k)) {
string actual_key = SET.at(i).at(0);
string actual_value = SET.at(i).at(1);
if (actual_key == "LINEWIDTH:") {
cout << DEF.at(j).at(k).at(3) << flush;
double LW = string_to_double(SET.at(i).at(1));
cout << fixed << setprecision (1) << LW / 10.0 << flush;
cout << DEF.at(j).at(k).at(4) << endl;
}
else if (actual_key == "STRESSANGLE:") {
cout << DEF.at(j).at(k).at(3) << flush;
cout << SET.at(i).at(1) << flush;
cout << DEF.at(j).at(k).at(4) << endl;
}
else if (actual_key == "WELLINTERVAL_LENGTH:") {
cout << DEF.at(j).at(k).at(3) << flush;
cout << SET.at(i).at(1) << flush;
cout << DEF.at(j).at(k).at(4) << endl;
}
else if (actual_key == "CLUSTERNUMBER:") {
if (actual_value == "A" || actual_value == "N") cout << DEF.at(j).at(k).at(1) << endl;
else {
cout << DEF.at(j).at(k).at(3) << flush;
cout << SET.at(i).at(1) << flush;
cout << DEF.at(j).at(k).at(4) << endl;
}
}
else cout << DEF.at(j).at(k).at(1) << endl;
}
}
}
}
writeln ("");
return;
}
void dbg_cout_settings_vector (const vector < vector <string> >& IN) {
for (size_t i = 0; i < IN.size(); i++) {
cout << IN.at(i).at(0) << '\t' << IN.at(i).at(1) << endl;
}
}