-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse.cpp
105 lines (89 loc) · 3.61 KB
/
parse.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
/* Copyright (C) 2014 Stefan Helmert <stefan.helmert@gmx.net>
smartRNS parse/interprete configuration and data strings in TXT records
*/
#include "parse.h"
// main parser function - onverts one TXT record to vector of key-value-pairs contain configuration and data
vector<keyval_t> txtrec2keyvalvec(string txtstr)
{
vector<keyval_t> smartrnsvec;
keyval_t elem;
uint32_t i, k;
string key, keysstr, val;
vector<string> keys;
size_t pos, oldpos, oldpos2;
key = "";
keys.clear();
i = 0;
pos = 0;
oldpos = std::string::npos;
oldpos2 = std::string::npos;
try{
while(std::string::npos!=pos){ // iterate over TXTrecord
pos = txtstr.find_first_not_of(" "); // delete all spaces...
txtstr = txtstr.substr(pos); // ...from beginning
if("}"==txtstr.substr(0,1)){ // exit {...} expression
if(0==keys.size()){
elem.key = "smartrns.error.parser";
elem.val = "syntax: '{'<'}'";
smartrnsvec.push_back(elem);
break;
}
keys.pop_back();
}
pos = txtstr.find_first_of("{=; "); // variable name ended, expression begins
key = txtstr.substr(0,pos); // get variable name
if("."==key.substr(0,1)){ // delete beginning "." of name (if structure{ .element1=...)
key = key.substr(1);
}
txtstr = txtstr.substr(pos);
pos = txtstr.find_first_not_of(" "); // delete trailing spaces
txtstr = txtstr.substr(pos);
if("="==txtstr.substr(0,1)){ // assignement
txtstr = txtstr.substr(1);
pos = txtstr.find_first_not_of(" "); // delete trailing spaces
txtstr = txtstr.substr(pos);
pos = txtstr.find_first_of(";"); // assigned value ends with ";"
val = txtstr.substr(0,pos);
keysstr = "";
for(k=0;k<keys.size();k++){ // get the complete name if structure{.element=...} is used -> structure.element
keysstr += keys[k] + ".";
}
elem.key = keysstr + key; // build complete variable name and save to key-value-pair
elem.val = val; // ... add the value
smartrnsvec.push_back(elem);
i++; // ... next key-val-pair
txtstr = txtstr.substr(pos+1);
}else if("{"==txtstr.substr(0,1)){ // shorted structure initialisation starts: structurename{.elem1=42; .elem2=23}
keys.push_back(key); // do the structurename on top of the stack - yes, nesting is possible
txtstr = txtstr.substr(1);
}
// break if in loop
if((pos == oldpos) && (oldpos == oldpos2)) break;
oldpos2 = oldpos;
oldpos = pos;
}
}
catch( std::exception const &exc){ // config ends
}
return smartrnsvec;
}
// show content of key-value vector
void print_key_val_vec(vector<keyval_t> arg)
{
uint32_t i;
cout << "ANSWER" << endl << endl;
for(i=0;i<arg.size();i++){
cout << " " << arg[i].key << " " << arg[i].val << endl;
}
}
// extends parser to process a vector of TXT records, writing the key-value-pairs to one large vector
vector<keyval_t> txtrec2keyvalvec(vector<string> TXT)
{
uint32_t i;
vector<keyval_t> keyvalvec, keyvalvectmp;
for(i=0;i<TXT.size();i++){
keyvalvectmp = txtrec2keyvalvec(TXT[i]);
keyvalvec.insert(keyvalvec.end(), keyvalvectmp.begin(), keyvalvectmp.end());
}
return keyvalvec;
}