-
Notifications
You must be signed in to change notification settings - Fork 3
/
SignalParser.cxx
141 lines (119 loc) · 2.7 KB
/
SignalParser.cxx
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
/*
*
*
*/
#include <iostream>
#include <vector>
#include <string>
#if 0
struct signal_prop {
uint32_t id;
uint32_t channel;
uint32_t offset;
};
class SignalParser
{
public:
SignalParser(){};
virtual ~SignalParser(){};
std::vector<struct signal_prop> & Parsing(std::string &);
protected:
private:
std::vector<struct signal_prop> signals;
};
std::vector<struct signal_prop> & SignalParser::Parsing(std::string &sigstr)
{
std::cout << sigstr << std::endl;
return signals;
}
#endif
namespace SignalParser {
const std::string bra("(");
const std::string ket(")");
const std::string delim(" ");
std::vector<std::string> bksplit(const std::string str)
{
char cbra = bra.c_str()[0];
char cket = ket.c_str()[0];
char cdelim = delim.c_str()[0];
std::vector<std::string> words;
std::string word;
for (char ch : str) {
if ((ch == cbra) || (ch == cket)) {
if (! word.empty()) {
words.emplace_back(word);
word.clear();
}
char temp_ch[2] = {0, 0};
temp_ch[0] = ch;
std::string braket(temp_ch);
words.emplace_back(braket);
} else
if (ch == cdelim) {
if (! word.empty()) {
words.emplace_back(word);
word.clear();
}
} else {
word += ch;
}
}
if (! word.empty()) words.emplace_back(word);
return words;
}
std::vector<std::vector<uint32_t> > Parsing(const std::string &sigstr)
{
std::vector<std::vector<uint32_t> > signals;
#if 0
std::cout << sigstr << std::endl;
#endif
std::vector<std::string> words = bksplit(sigstr);
#if 0
std::cout << "# " << words.size() ;
for (auto &s : words) std::cout << "_" << s;
std::cout << std::endl;
#endif
int level = 0;
std::vector<uint32_t> low;
for (auto &v : words) {
if (v == bra) {
level++;
low.clear();
low.resize(0);
} else
if (v == ket) {
level--;
if (low.size() > 0) signals.emplace_back(low);
} else {
try {
unsigned long ulval = std::stoul(v, nullptr, 0);
uint32_t val = 0xffffffff & ulval;
low.emplace_back(val);
} catch (const std::invalid_argument &e) {
std::cout << "#E Bad number word: " << v << " " << e.what() << std::endl;
//throw e;
} catch (const std::out_of_range &e) {
std::cout << "#E Bad number word (out of range) : "
<< v << " " << e.what() << std::endl;
//throw e;
}
}
}
return signals;
}
} // namespace SignalParser
#ifdef SIGNALPARSER_TEST_MAIN
int main(int argc, char* argv[])
{
std::string param("(0x1234 0 0) ( 0xc0a802a9 1 0 ) (0xa235 xxx1 1)");
std::vector<std::vector<uint32_t> > signals
= SignalParser::Parsing(param);
std::cout << "String: " << param << std::endl;
std::cout << std::hex;
for (auto &vec : signals) {
for (auto &val : vec) std::cout << " 0x" << val;
std::cout << std::endl;
}
return 0;
}
#endif