-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_buffer.cpp
More file actions
632 lines (581 loc) · 17 KB
/
code_buffer.cpp
File metadata and controls
632 lines (581 loc) · 17 KB
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
#include "code_buffer.h"
#include <iostream>
#include <fstream>
void IntParser::print_data() {
std::cout << _data << std::endl;
}
bool IntParser::data_parser(const std::string& str) {
_data = std::stoi(str);
return true;
}
void FloatParser::print_data() {
std::cout << _data << std::endl;
}
bool FloatParser::data_parser(const std::string& str) {
_data = std::stof(str);
return true;
}
bool StrParser::data_parser(const std::string& str) {
_data = str;
return true;
}
void IntArrayParser::print_data() {
std::cout << "(int_array)" << std::endl;
for (const auto& item : _data) {
std::cout << item << std::endl;
}
}
bool IntArrayParser::data_parser(const std::string& str) {
std::vector<std::string> array_split_vec = Utils::split_string(str, ":");
if (array_split_vec.size() != 2) {
//LOG : FATAL("int[] data format illegal");
return false;
}
const size_t array_size = std::stoi(array_split_vec[0]);
std::vector<std::string> array_content = Utils::split_string(array_split_vec[1], ",");
if (array_content.size() != array_size) {
//LOG : FATAL("int[] data size incorrect");
return false;
}
std::vector<int> res;
for (const auto& item : array_content) {
res.emplace_back(std::stoi(item)); //TODO how to templatize it ?
}
_data.swap(res);
return true;
}
void FloatArrayParser::print_data() {
printf("(float_array)");
for (const auto& item : _data) {
std::cout << item << std::endl;
}
}
//TODO <T>ArrayParser
bool FloatArrayParser::data_parser(const std::string& str) {
std::vector<std::string> array_split_vec = Utils::split_string(str, ":");
if (array_split_vec.size() != 2) {
//LOG : FATAL("int[] data format illegal");
return false;
}
const size_t array_size = std::stoi(array_split_vec[0]);
std::vector<std::string> array_content = Utils::split_string(array_split_vec[1], ",");
if (array_content.size() != array_size) {
//LOG : FATAL("int[] data size incorrect");
return false;
}
std::vector<float> res;
for (const auto& item : array_content) {
res.emplace_back(std::stof(item)); //TODO how to templatize it ?
}
_data.swap(res);
return true;
}
void StrArrayParser::print_data() {
printf("(str_array)");
for (const auto& item : _data) {
std::cout << item << std::endl;
}
}
bool StrArrayParser::data_parser(const std::string& str) {
std::vector<std::string> array_split_vec = Utils::split_string(str, ":");
if (array_split_vec.size() != 2) {
//LOG : FATAL("int[] data format illegal");
return false;
}
const size_t array_size = std::stoi(array_split_vec[0]);
std::vector<std::string> array_content = Utils::split_string(array_split_vec[1], ",");
if (array_content.size() != array_size) {
//LOG : FATAL("int[] data size incorrect");
return false;
}
std::vector<std::string> res;
for (const auto& item : array_content) {
res.emplace_back(item); //TODO how to templatize it ?
}
_data.swap(res);
return true;
}
void UserData::print_data() {
std::cout << "(name) " << _name << "(age) " << _age << "(K/D/A)" << _kill << "/" << _dead << "/" << _assist << std::endl;
}
bool UserData::data_parser(const std::string& str) {
std::vector<std::string> split_array =
Utils::split_string(str, _str_separator);
int array_size = split_array.size();
if (array_size != get_member_cnt()) {
//LOG FATAL("user_data size incorrect");
return false;
}
// name
_name = split_array[0];
// age
_age = std::stoi(split_array[1]);
// kill
_kill = std::stof(split_array[2]);
// dead
_dead = std::stof(split_array[3]);
// assist
_assist = std::stof(split_array[4]);
return true;
}
DictParser::DictParser() :
_total_col_cnt(0) {
//初始化建立映射关系
_type_parser_map["int"] = new(std::nothrow) IntParser();
_type_parser_map["float"] = new(std::nothrow) FloatParser();
_type_parser_map["string"] = new(std::nothrow) StrParser();
_type_parser_map["int_array"] = new(std::nothrow) IntArrayParser();
_type_parser_map["float_array"] = new(std::nothrow) FloatArrayParser();
_type_parser_map["str_array"] = new(std::nothrow) StrArrayParser();
_type_parser_map["user_data"] = new(std::nothrow) UserData();
}
DictParser::~DictParser() {
for (auto& item : _type_parser_map) {
delete item.second;
}
}
bool DictParser::read_file_by_line(const std::string& file_name) {
std::ifstream data_file;
data_file.open(file_name.c_str(), std::ifstream::in);
//load failed
if (data_file.fail()) {
//LOG FATAL("Open file error");
return -1;
}
std::string cur_line_str;
int line = 0;
//逐行读取数据
while (getline(data_file, cur_line_str)) {
if (line == 0) {
++line;
continue;
//第一行获取每一列的数据类型
++line;
int ret = get_each_col_data_type(cur_line_str);
if (ret == -1) {
return -1;
}
} else if (line == 1) {
//第二行获取每一列的数据类型
++line;
int ret = get_each_col_data_type(cur_line_str);
if (ret == -1) {
return -1;
}
} else {
//第二行开始进行解析
printf("\n");
++line;
printf("line:%d,\n", line - 1);
if (cur_line_str.empty()) {
//LOG FATAL("line %d of data.conf is empty", line - 1);
continue;
}
std::vector<std::string> each_col_data_value =
Utils::split_string(cur_line_str, "\t");
int cur_line_cnt = each_col_data_value.size();
//该行数据数量与第一行的类型数量不匹配
if (cur_line_cnt != _total_col_cnt) {
//LOG FATAL("line %d's columns_number incorrect", line - 1);
continue;
}
for (int i = 0; i < cur_line_cnt; ++i) {
//对每一列数据进行解析
std::cout << "col: " << i + 1 << std::endl;
std::string cur_col_data_str = each_col_data_value[i];
if (cur_col_data_str.empty()) {
//LOG FATAL("line %d,column %d is empty", line - 1, i + 1);
continue;
}
int res = col_str_data_parser(cur_col_data_str, i, line);
printf("\n");
if (res == -1) {
continue;
}
}
}
}
if (line == 0) {
//LOG WARNING("data.conf is empty");
return -1;
}
data_file.close();
return 0;
}
bool DictParser::get_each_col_data_type(const std::string& first_line_str) {
//根据词表文件的第一行,获取每一列的数据类型
if (first_line_str.empty()) {
//LOG WARNING("data.conf is empty");
return false;
}
_column_type = Utils::split_string(first_line_str, "\t");
for (const auto& cur_str : _column_type) {
auto it = _type_parser_map.find(cur_str);
//在映射表中没找到该类型,表明输入的数据类型非法
if (it == _type_parser_map.end()) {
//LOG FATAL("line 1,column %d is illegal date type", (int)(i + 1));
return false;
}
}
_total_col_cnt = _column_type.size();
return true;
}
bool DictParser::col_str_data_parser(const std::string& col_str, int index, int line) {
//对输入的字符串进行解析
const std::string& cur_col_data_type = _column_type[index];
auto it = _type_parser_map.find(cur_col_data_type);
//找不到对应的映射关系
if (it == _type_parser_map.end()) {
//LOG FATAL("line %d,column %d is illegal data type", line - 1, index + 1);
return false;
}
bool ret = false;
try {
ret = it->second->data_parser(col_str);
} catch (std::exception e) {
std::cerr << e.what() << std::endl;
ret = false;
}
//解析失败
if (!ret) {
//LOG FATAL("line %d,column %d parse error", line - 1, index + 1);
return false;
}
it->second->print_data();
return true;
}
std::vector<std::string> Utils::split_string(const std::string& str, const std::string& separator) {
std::vector<std::string> res;
std::size_t start_index = 0;
std::size_t end_index = str.find_first_of(separator);
while (end_index != std::string::npos) {
res.emplace_back(str.substr(start_index, end_index - start_index));
start_index = end_index + separator.size();
end_index = str.find_first_of(separator, start_index);
}
if (start_index < str.size()) {
res.emplace_back(str.substr(start_index));
}
return res;
}
bool LoaderFStream::load(const std::string& file_name) {
if (file_name.empty()) {
return false;
}
clear();
std::ifstream in_file;
in_file.open(file_name, std::ifstream::in);
if (!in_file.good()) {
//log
return false;
}
{//parse label_ori
std::string label_ori;
std::getline(in_file, label_ori, '\n');
label_ori = format_string(label_ori);
//label_ori += '\t';
std::vector<std::string> label_list = Utils::split_string(label_ori, "\t");
for (const auto& str : label_list) {
LabelInfo info;
info.name = str;
_label.emplace_back(info);
}
}
{//parse type
std::string type_ori;
std::getline(in_file, type_ori, '\n');
type_ori = format_string(type_ori);
//label_ori += '\t';
std::vector<std::string> type_list = Utils::split_string(type_ori, "\t");
if (_label.size() != type_list.size()) {
//log
return false;
}
for (size_t i = 0; i < type_list.size(); ++i) {
swap(_label[i].info, type_list[i]);
}
}
{//parse real content
std::string tmp_str;
while (std::getline(in_file, tmp_str, '\n')) {
tmp_str = format_string(tmp_str);
//tmp_str += '\t';
StringVector value_list = Utils::split_string(tmp_str, "\t");
if (value_list.size() != _label.size()) {
//TODO something wrong, log it and process
//do not continue,
}
_content.emplace_back(value_list);
}
}
return true;
}
std::string LoaderFStream::format_string(const std::string& str) {
const size_t pos = str.find('\r', 0);
if (-1 == pos) {
return str;
}
return str.substr(0, pos);
}
//bool FileInterface::ExecuteSql(const char* sSql, Table* pTable)
//{
// if (!sSql || !pTable)
// {
// return false;
// }
//
// Clear();
// if (!LoaderFStream::load(sSql))
// {
// return false;
// }
//
// LabelVector labels;
// if (!LoaderFStream::get_meta_list(labels))
// {
// return false;
// }
// int DBType = DB_COLUMN_Unkonw;
// for (size_t i = 0; i < (size_t)labels.size(); ++i)
// {
// if (labels[i].info == "int8" ||
// labels[i].info == "uint8" ||
// labels[i].info == "int16" ||
// labels[i].info == "uint16" ||
// labels[i].info == "int32" ||
// labels[i].info == "uint32"
// )
// {
// DBType = DB_COLUMN_INT;
// }
// else if (labels[i].info == "int64" ||
// labels[i].info == "uint64")
// {
// DBType = DB_COLUMN_INT64;
// }
// else if (labels[i].info == "f32")
// {
// DBType = DB_COLUMN_FLOAT;
// }
// else if (labels[i].info == "f64")
// {
// DBType = DB_COLUMN_DOUBLE;
// }
// else if (labels[i].info == "char")
// {
// DBType = DB_COLUMN_TEXT;
// }
// pTable->add_column((char*)labels[i].name.c_str(), i, DBType);
// }
//
//
// StringVector values;
// int32_t idx = 0;
// while (get_value_list(idx, values))
// {
// FillValues(pTable, values);
// idx++;
// }
//
//
// return true;
//}
//
//void Table::add_column(const std::string& col_name, int32_t col_index, int32_t valueType) {
// _colu
//}
//#include <iostream>
//#include <fstream>
//#include <string>
//#include <sstream>
//
//std::string LoaderFStream::FormatString(std::string& str)
//{
// size_t pos2 = str.find('\r', 0);
// if (pos2 == -1)
// {
// return str;
// }
// return str.substr(0, pos2);
//}
//
//bool LoaderFStream::Load(const char* sFile)
//{
// if (!sFile)
// {
// return false;
// }
// Clear();
// std::ifstream infile;
// infile.open(sFile, std::ifstream::in);
// if (!infile.good())
// {
// printf("ifstream error! file name is %s\n", sFile);
// return false;
// }
// //get name
// char buffer[MAX_CFG_LINE_SIZE];
// std::string slab;
// infile.getline(buffer, MAX_CFG_LINE_SIZE, '\n');
// slab = buffer;
// slab = FormatString(slab);
// slab += '\t';
//
// //get info
// char infobuffer[MAX_CFG_LINE_SIZE];
// std::string slabInfo;
// infile.getline(infobuffer, MAX_CFG_LINE_SIZE, '\n');
// slabInfo = infobuffer;
// slabInfo = FormatString(slabInfo);
// slabInfo += '\t';
//
// StringVector tmpVal;
// while (infile.getline(buffer, MAX_CFG_LINE_SIZE, '\n'))
// {
// std::string tmpV = buffer;
// tmpV = FormatString(tmpV);
// tmpV += '\t';
// tmpVal.push_back(tmpV);
// }
//
// {//paser label
// size_t pos1 = -1;
// std::string tmpString = slab;
// while (true)
// {
// size_t pos2 = tmpString.find('\t', ++pos1);
// if (pos2 == -1)
// {
// break;
// }
// std::string val = tmpString.substr(pos1, pos2 - pos1);
// pos1 = pos2;
// LabelInfo info;
// info.m_Name = val;
// m_Label.push_back(info);
// }
// }
//
// {//paser info
// size_t pos1 = -1;
// std::string tmpString = slabInfo;
// int32_t idx = 0;
// while (true)
// {
// size_t pos2 = tmpString.find('\t', ++pos1);
// if (pos2 == -1)
// {
// break;
// }
// std::string info = tmpString.substr(pos1, pos2 - pos1);
// pos1 = pos2;
// if (idx >= (int32_t)m_Label.size())
// {
// return false;
// }
// m_Label[idx].m_Info = info;
// idx++;
// }
// }
//
// {//paser val
// for (int32_t i = 0; i < (int32_t)tmpVal.size(); ++i)
// {
// size_t pos1 = -1;
// std::string tmpString = tmpVal[i];
// StringVector valList;
// while (true)
// {
// size_t pos2 = tmpString.find('\t', ++pos1);
// if (pos2 == -1)
// {
// break;
// }
// std::string val = tmpString.substr(pos1, pos2 - pos1);
// pos1 = pos2;
// valList.push_back(val);
// }
// m_Val.push_back(valList);
// }
// }
//
// return true;
//}
//bool LoaderFStream::Save(const char* sFile)
//{
// if (!sFile)
// {
// return false;
// }
// if (m_Label.empty())
// {
// return false;
// }
// std::ofstream ofile;
// ofile.open(sFile, std::ifstream::out);
// if (!ofile.good())
// {
// printf("ifstream error! file name is %s\n", sFile);
// return false;
// }
// //write label name
// //write label type
// std::stringstream sstr;
// std::stringstream ssType;
// sstr << m_Label[0].m_Name.c_str();
// ssType << m_Label[0].m_Info.c_str();
// for (int32_t i = 1; i < (int32_t)m_Label.size(); ++i)
// {
// sstr << "\t" << m_Label[i].m_Name.c_str();
// ssType << "\t" << m_Label[i].m_Info.c_str();
// }
// sstr << "\n";
// ssType << "\n";
// ofile << sstr.str().c_str();
// ofile << ssType.str().c_str();
//
// //write value
// for (int32_t i = 0; i < (int32_t)m_Val.size(); ++i)
// {
// StringVector& vec = m_Val[i];
// if (vec.empty())
// {
// continue;
// }
// std::stringstream ssVal;
// ssVal << vec[0].c_str();
// for (int32_t x = 1; x < (int32_t)vec.size(); ++x)
// {
// ssVal << "\t" << vec[x].c_str();
// }
// ssVal << "\n";
// ofile << ssVal.str().c_str();
// }
//
// //close
// ofile.close();
// return true;
//}
//bool LoaderFStream::GetLabelList(LabelVector& list)
//{
// list = m_Label;
// return true;
//}
//bool LoaderFStream::GetValueList(int32_t index, StringVector& list)
//{
// if (index < 0 || index >= (int32_t)m_Val.size())
// {
// return false;
// }
// list = m_Val[index];
// return true;
//}
//void LoaderFStream::Clear()
//{
// m_Label.clear();
// for (int32_t i = 0; i < (int32_t)m_Val.size(); ++i)
// {
// m_Val[i].clear();
// }
// m_Val.clear();
//}