-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfuncecsv.cpp
357 lines (311 loc) · 9.08 KB
/
funcecsv.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
#include "funcecsv.h"
//Новый массив bool
bool* newbool(int n, bool val)
{
bool* mas = new bool[n];
boolinit(mas, n, val);
return mas;
}
//Инициализация массива bool
void boolinit(bool* mas, int n, bool val)
{
for(int i = 0; i < n; i++) {mas[i] = val;}
}
//Сравнение элементов массива, операция AND
bool booland(bool* mas, int n)
{
bool bl = true;
int ind = 0;
while(bl && ind < n) {bl = mas[ind]; ind++;}
return bl;
}
//Сравнение элементов массива, операция OR
bool boolor(bool* mas, int n)
{
bool bl = (n > 0 ? false : true);
int ind = 0;
while(!bl && ind < n) {bl = mas[ind]; ind++;}
return bl;
}
//Возвращает цельный путь
string getpath(string parent, string& field, const char sym)
{
if(parent[0] == sym) {parent = parent.substr(1);}
return parent + sym + field;
}
string getpath(string parent, const string& field, const char sym)
{
if(parent[0] == sym) {parent = parent.substr(1);}
return parent + sym + field;
}
//Разрешённое поле
bool resolvedfield(vector<string>* fieldsNotSave, string& parent, string& field)
{
if(fieldsNotSave == nullptr) {return true;}
bool bl = true;
for(size_t ind = 0; bl && ind < fieldsNotSave->size(); ind++)
{
fieldsNotSave->at(ind);
bl = !cmppath(getpath(parent, field), fieldsNotSave->at(ind), false);
}
//return fieldsNotSave == nullptr || find(fieldsNotSave->begin(), fieldsNotSave->end(), getpath(parent, field)) != fieldsNotSave->end();
return bl;
}
bool resolvedfield(vector<string>* fieldsNotSave, string& parent, const string& field)
{
if(fieldsNotSave == nullptr) {return true;}
bool bl = true;
for(size_t ind = 0; bl && ind < fieldsNotSave->size(); ind++)
{
fieldsNotSave->at(ind);
bl = !cmppath(getpath(parent, field), fieldsNotSave->at(ind), false);
}
//return fieldsNotSave == nullptr || find(fieldsNotSave->begin(), fieldsNotSave->end(), getpath(parent, field)) != fieldsNotSave->end();
return bl;
}
//Подстрока входит в строку
bool cmppath(const string& str, const string& strsub, bool acc, const char* sym)
{
vector<string> path, subpath;
split(path, str, sym);
split(subpath, strsub, sym);
if(path.size() > 0 && path[0] == "") {path.erase(path.begin());}
if(subpath.size() > 0 && subpath[0] == "") {subpath.erase(subpath.begin());}
return cmppath(path, subpath, acc);
}
bool cmppath(vector<string>& path, vector<string>& subpath, bool acc)
{
bool bl = (!acc || path.size() == subpath.size());
for(size_t i = 0; bl == true && i < subpath.size(); i++)
{
if(subpath[i] != path[i]) {bl = false;}
}
return bl;
}
//Сравнение с символом экранирования
bool cmpstrslash(const string& str, size_t ind1, size_t ind2)
{
if(ind2 > 0 && str[ind2-1] == '\\')
{
size_t ind = ind1;
while(ind < ind2) {if(str[ind] == '\\') {ind++;} ind++;}
if(ind > ind2) {return true;}
}
return false;
}
//Разделение строки на подстроки по разделителю
void split(vector<string>& dest, const string& str, const char* delim)
{
size_t cout_delim = strlen(delim);
size_t ind1 = 0, ind2 = 0;
string substr;
while(ind1 < str.length() && ind2 != std::string::npos)
{
ind2 = str.find(delim, ind1, cout_delim);
if(ind2 == std::string::npos) {ind2 = str.length();}
substr = (ind1 == ind2 ? "" : str.substr(ind1, ind2 - ind1));
dest.push_back(substr);
ind1 = ind2 + cout_delim;
}
if(ind2 < str.length()) {dest.push_back("");}
}
//Разделение строки на подстроки по разделителю с учётом символа экранирования
void split_no_slash(vector<string>& dest, const string& str, const char* delim)
{
size_t cout_delim = strlen(delim);
size_t ind1 = 0, ind2 = 0, poisk = 0;
string substr;
while(ind1 < str.length() && ind2 != std::string::npos)
{
ind2 = str.find(delim, poisk, cout_delim);
if(ind2 != std::string::npos && cmpstrslash(str, ind1, ind2))
{
poisk = ind2 + 1;
}
else
{
if(ind2 == std::string::npos) {ind2 = str.length();}
substr = (ind1 == ind2 ? "" : str.substr(ind1, ind2 - ind1));
dest.push_back(substr);
poisk = ind2 + cout_delim;
ind1 = poisk;
}
}
if(ind2 < str.length()) {dest.push_back("");}
}
//Сбор строки из подстрок
void concat(string& str, vector<string>& dest, const char* delim)
{
str = "";
for(vector<string>::iterator iter = dest.begin(); iter != dest.end(); iter++)
{
str.append(*iter);
str.append(delim);
}
size_t len_delim = strlen(delim);
if(str.length() > len_delim) {str.erase(str.length()-len_delim, len_delim);}
}
//Сбор строки из подстрок
void concat(string& str, string* dest, int len, const char* delim)
{
str = "";
for(int i = 0; i < len; i++)
{
str.append(dest[i]);
str.append(delim);
}
size_t len_delim = strlen(delim);
if(str.length() > len_delim) {str.erase(str.length()-len_delim, len_delim);}
}
//Удаление концевых пробелов
string& trimstr(string& s)
{
const string SPACES(" \r\t\n");
size_t head = s.find_first_not_of(SPACES);
if(head == string::npos) {s = "";}
else if(head > 0) {s.erase(0, head);}
size_t tail = s.find_last_not_of(SPACES);
if(tail != s.size() - 1) {s.erase(tail + 1);}
return s;
}
//Преобразование строки в элемент
TypeDataECSV str_to_element(string& dest, const string& str)
{
string tempStr(str);
trimstr(tempStr);
size_t n_back = tempStr.length() - 1;
if(tempStr[0] == '\"' && tempStr[n_back] == '\"')
{
tempStr.erase(n_back);
tempStr.erase(0, 1);
dest = tempStr;
return TypeDataECSV::String;
}
else
{
dest = tempStr;
return TypeDataECSV::Element;
}
}
//Преобразование строки в элемент
vector<TypeDataECSV> str_to_element(vector<string>& vecstr)
{
vector<TypeDataECSV> td;
for(vector<string>::iterator iter = vecstr.begin(); iter != vecstr.end(); iter++)
{
td.push_back(str_to_element(*iter, *iter));
}
return td;
}
//Преобразование символов в нижний регистр
string& str_to_lower(string& str)
{
for(string::iterator iter = str.begin(); iter != str.end(); iter++)
{
*iter = tolower(*iter);
}
return str;
}
//Удаление комментариев в строке
size_t delcomment(string& dest)
{
size_t ind = dest.find("//", 0, 2);
if(ind == 0)
{
dest = "";
}
else if(ind != std::string::npos)
{
dest = dest.substr(0, ind);
}
else
{
ind = dest.length();
}
return ind;
}
//Экранирование
void shield(string& dest, const string& str)
{
const string SPEC("\";\\$/");
dest = "";
for(size_t ind = 0; ind < str.length(); ind++)
{
if(str[ind] == '\n')
{
dest.push_back('\\');
dest.push_back('n');
}
else if(SPEC.find(str[ind]) != string::npos)
{
dest.push_back('\\');
}
dest.push_back(str[ind]);
}
}
//Экранирование
void shield(vector<string>& vecstr)
{
for(vector<string>::iterator iter = vecstr.begin(); iter != vecstr.end(); iter++)
{
shield(*iter, *iter);
}
}
//Преобразование экранированных символов
void freeshield(string& dest)
{
string str = dest;
dest = "";
size_t ind = 0;
size_t n_str = str.length();
while(ind < n_str)
{
if(str[ind] == '\\')
{
ind++;
if(str[ind] == 'n') {dest.push_back('\n');}
else {dest.push_back(str[ind]);}
}
else {dest.push_back(str[ind]);}
ind++;
}
}
//Преобразование экранированных символов
void freeshield(vector<string>& vecstr)
{
for(vector<string>::iterator iter = vecstr.begin(); iter != vecstr.end(); iter++)
{
freeshield(*iter);
}
}
//Преобразование строки в значение
void to_value(double& value, string& str_val)
{
value = atof(str_val.c_str());
}
void to_value(float& value, string& str_val)
{
value = atof(str_val.c_str());
}
void to_value(signed int& value, string& str_val)
{
value = atoi(str_val.c_str());
}
void to_value(unsigned int& value, string& str_val)
{
value = atol(str_val.c_str());
}
void to_value(long& value, string& str_val)
{
value = atol(str_val.c_str());
}
void to_value(bool& value, string str_val)
{
str_to_lower(str_val);
if(str_val == "false" || str_val == "0") {value = false;}
else {value = true;}
}
string to_vstring(bool& value)
{
return (value ? "true" : "false");
}