-
Notifications
You must be signed in to change notification settings - Fork 1
/
CSVop.cpp
392 lines (349 loc) · 11.6 KB
/
CSVop.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
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
#include "CSVop.h"
#include <iostream>
CSVop::CSVop()
{
}
CSVop::~CSVop()
{
if(iCSVfile.is_open()) iCSVfile.close();
}
int CSVop::readCSVfile(ifstream &file)
{
int err;
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
if(!file.is_open()) return -1;
err = tab.loadTable(file, CSVparams.delim, CSVparams.ignorebefore, CSVparams.ignoreafter);
if(0!=err) return err;
tab.rmquotmarks();
return 0;
}
int CSVop::readCSVfile(string filename)
{
iCSVfile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try{
iCSVfile.open(filename.c_str());
}
catch(std::ifstream::failure e){
return -1;
}
return readCSVfile(iCSVfile);
}
int CSVop::readCSVfile(void)
{
iCSVfile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try{
iCSVfile.open(CSVparams.filename.c_str());
}
catch(std::ifstream::failure e){
return -1;
}
return readCSVfile(iCSVfile);
}
int CSVop::findrow(string fieldname, string fieldentry, int startrow, bool namecontains, bool entrycontains, bool strcontainsname, bool strcontainsentry, bool valuesearch, bool withtolerance, unsigned subpart, double precision)
{
int col, row;
col = tab.findcol(fieldname, 0, 0, namecontains, strcontainsname);
if(withtolerance){
if(0==col){
cout << "error: Not a value with Tolerance entry, because it is on most left column!" << endl;
}else{
col--;
}
}
row = tab.findrow(fieldentry, col, startrow, entrycontains, strcontainsentry, valuesearch, precision);
return row;
}
// first match
// Nbr2find (1 - or; number of search entries - and)
int CSVop::findrow(vector<datapair_t> entrypairs, int startrow, int Nbr2find)
{
int col, row, cnt, valcol;
vector<int> cols;
col = -1;
valcol = -1;
unsigned i;
row = startrow;
if(0==entrypairs.size()) return -1;
cols.assign(entrypairs.size(),-1);
for(i=0;i<entrypairs.size();i++){
cols[i] = tab.findcol(entrypairs[i].fieldname, 0, 0, entrypairs[i].namecontains, entrypairs[i].strcontainsname);
}
for(row=startrow;row<tab.getNorows();row++){
cnt = 0;
for(i=0;i<entrypairs.size();i++){
if(entrypairs[i].withtolerance){
if(entrypairs.size() < i+2){
cout << "Error: value with tolerance specified but no search entry in configuration file for tolerance." << endl;
break;
}
valcol = cols[i];
}else if((i>0)&&(entrypairs[i-1].withtolerance)){
valcol = cols[i-1];
}else{
col = cols[i];
}
// this is a bit "special" - value and tolerance are two parameters, upper and lower bound of value are also two parameters
if(entrypairs[i].withtolerance){
// our "cursor" is at first on the value, that means the first parameter
// but we read also the second parameter (i+1), the tolerance
// and we are searching for the lower bound
if(0==valcol){
cout << "Error: value column is first one in table - there is no lower bound value on the left, tolerance operation failed." << endl;
break;
}
if(entrymatchtol(tab.Tableread(row, valcol-1),entrypairs[i].fieldentry,entrypairs[i+1].fieldentry,false)) cnt++;
else break;
}else{// a normal parameter does not have the tolerance flag set, same is the tolerance of the value
// our "cursor" is on the second parameter, that means the second parameter
// but we read also the first parameter again (i-1), the value
// and we are searching for the upper bound
if((i>0) && (entrypairs[i-1].withtolerance)){
if(tab.getNocols() < valcol+2){
cout << "Error: value column is last one in table - there is no upper bound value on the right, tolerance operation failed." << endl;
break;
}
if(entrymatchtol(tab.Tableread(row, valcol+1),entrypairs[i-1].fieldentry,entrypairs[i].fieldentry,true)) cnt++;
else break;
}else{
if(entrymatch(tab.Tableread(row, col),entrypairs[i].fieldentry,entrypairs[i].strcontainsentry,entrypairs[i].entrycontains,entrypairs[i].valuesearch,entrypairs[i].precision)) cnt++;
else break;
}
}
}
if(Nbr2find==cnt) return row;
}
return -1;
}
// search and
int CSVop::findrow(vector<datapair_t> entrypairs, int startrow)
{
return findrow(entrypairs, startrow, int(entrypairs.size()));
}
// all matches
vector<int> CSVop::findrows(vector<datapair_t> entrypairs, int startrow, int Nbr2find)
{
vector<int> rows;
int row = 0;
rows.clear();
row = startrow-1;
while(-1!=row){
row = findrow(entrypairs,row+1,Nbr2find);
rows.push_back(row);
}
rows.pop_back();
return rows;
}
vector<int> CSVop::findrows(vector<datapair_t> entrypairs, int startrow)
{
return findrows(entrypairs, startrow, int(entrypairs.size()));
}
string CSVop::getEntry(int row, string fieldname, bool namecontains, bool strcontainsname)
{
int col;
col = tab.findcol(fieldname, 0, 0, namecontains, strcontainsname);
return tab.Tableread(row, col);
}
string CSVop::getfullName(string fieldname, bool namecontains, bool strcontainsname)
{
int col;
col = tab.findcol(fieldname, 0, 0, namecontains, strcontainsname);
if(-1==col) return fieldname;
return tab.Tableread(0, col);
}
void CSVop::getEntrys(int row, vector<datapair_t> &data2update)
{
unsigned i;
for(i=0;i<data2update.size();i++){
data2update[i].fieldentry = getEntry(row, data2update[i].fieldname, data2update[i].namecontains, data2update[i].strcontainsname);
if(data2update[i].takeDatabasefieldname) data2update[i].fieldname = getfullName(data2update[i].fieldname, data2update[i].namecontains, data2update[i].strcontainsname);
}
}
double CSVop::getLowest(int col)
{
double minval;
int i;
if(0==tab.getNorows()) return NAN;
minval = norm_value(tab.Tableread(0, col));
for(i=1;i<getNorows();i++){
minval = min(minval,norm_value(tab.Tableread(i, col)));
}
return minval;
}
double CSVop::getLowest(int col, vector<int> rows)
{
double minval;
unsigned i;
if(0==rows.size()) return NAN;
minval = norm_value(tab.Tableread(rows[0], col));
for(i=1;i<rows.size();i++){
minval = min(minval,norm_value(tab.Tableread(rows[i], col)));
}
return minval;
}
double CSVop::getHighest(int col)
{
double maxval;
int i;
if(0==tab.getNorows()) return NAN;
maxval = norm_value(tab.Tableread(0, col));
for(i=1;i<getNorows();i++){
maxval = max(maxval,norm_value(tab.Tableread(i, col)));
}
return maxval;
}
double CSVop::getHighest(int col, vector<int> rows)
{
double maxval;
unsigned i;
if(0==rows.size()) return NAN;
maxval = norm_value(tab.Tableread(rows[0], col));
for(i=1;i<rows.size();i++){
maxval = max(maxval,norm_value(tab.Tableread(rows[i], col)));
}
return maxval;
}
double CSVop::getNearest(int col, double val)
{
double minval, retval, tabval;
int i;
if(0==tab.getNorows()) return NAN;
retval = norm_value(tab.Tableread(0, col));
minval = abs(retval-val);
for(i=1;i<getNorows();i++){
tabval = norm_value(tab.Tableread(i, col));
if(abs(tabval-val)<minval){
minval = abs(tabval-val);
retval = tabval;
}
}
return retval;
}
double CSVop::getNearest(int col, double val, vector<int> rows)
{
double minval, retval, tabval;
unsigned i;
if(0==rows.size()) return NAN;
retval = norm_value(tab.Tableread(rows[0], col));
minval = abs(retval-val);
for(i=1;i<rows.size();i++){
tabval = norm_value(tab.Tableread(rows[i], col));
if(abs(tabval-val)<minval){
minval = abs(tabval-val);
retval = tabval;
}
}
return retval;
}
vector<int> CSVop::getrowswtol(int col, double val, double prec, bool absolute)
{
int i;
double tabval;
vector<int> rows;
rows.clear();
if(absolute){
for(i=0;i<tab.getNorows();i++){
tabval = norm_value(tab.Tableread(i,col));
if((val-prec < tabval) && (tabval < val+prec)){
rows.push_back(i);
}
}
}else{
for(i=0;i<tab.getNorows();i++){
tabval = norm_value(tab.Tableread(i,col));
if((val*(1-prec) < tabval) && (tabval < val*(1+prec))){
rows.push_back(i);
}
}
}
return rows;
}
vector<int> CSVop::getrowswtol(vector<int> irows, int col, double val, double prec, bool absolute)
{
unsigned i;
double tabval;
vector<int> rows;
rows.clear();
if(absolute){
for(i=0;i<irows.size();i++){
tabval = norm_value(tab.Tableread(irows[i],col));
if((val-prec < tabval) && (tabval < val+prec)){
rows.push_back(irows[i]);
}
}
}else{
for(i=0;i<irows.size();i++){
tabval = norm_value(tab.Tableread(irows[i],col));
if((val*(1-prec) < tabval) && (tabval < val*(1+prec))){
rows.push_back(irows[i]);
}
}
}
return rows;
}
vector<int> CSVop::getLowestrows(int col, double precision, bool absolute)
{
double maxval;
maxval = getLowest(col);
return getrowswtol(col, maxval, precision, absolute);
}
vector<int> CSVop::getLowestrows(vector<int> rows, int col, double precision, bool absolute)
{
double maxval;
maxval = getLowest(col, rows);
return getrowswtol(rows, col, maxval, precision, absolute);
}
vector<int> CSVop::getHighestrows(int col, double precision, bool absolute)
{
double maxval;
maxval = getHighest(col);
return getrowswtol(col, maxval, precision, absolute);
}
vector<int> CSVop::getHighestrows(vector<int> rows, int col, double precision, bool absolute)
{
double maxval;
maxval = getHighest(col, rows);
return getrowswtol(rows, col, maxval, precision, absolute);
}
vector<int> CSVop::getNearestrows(int col, double val, double precision, bool absolute)
{
double maxval;
maxval = getNearest(col, val);
return getrowswtol(col, maxval, precision, absolute);
}
vector<int> CSVop::getNearestrows(vector<int> rows, int col, double val, double precision, bool absolute)
{
double maxval;
maxval = getNearest(col, val, rows);
return getrowswtol(rows, col, maxval, precision, absolute);
}
vector<int> CSVop::findbestrows(vector<int> rows, datapair_t sortdata)
{
int col;
vector<int> rrows;
if(2>rows.size()) return rows;
col = tab.findcol(sortdata.fieldname, 0, 0, sortdata.namecontains,sortdata.strcontainsname);
if(sortdata.lowest){
rrows = getLowestrows(rows, col, sortdata.precision, false);
}else if(sortdata.highest){
rrows = getHighestrows(rows, col, sortdata.precision, false);
}else if(sortdata.nearest){
rrows = getNearestrows(rows, col, norm_value(sortdata.fieldentry), sortdata.precision, false);
}else{
rrows = rows;
}
return rrows;
}
// multiple results possible, if entries differ in tolerance area
vector<int> CSVop::findbestrows(vector<int> rows, vector<datapair_t> sortdatav)
{
unsigned i;
if(2>rows.size()) return rows;
for(i=0;i<sortdatav.size();i++){
rows = findbestrows(rows, sortdatav[i]);
}
return rows;
}
int CSVop::getNorows(void)
{
return tab.getNorows();
}