-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNumberListParser.cpp
executable file
·235 lines (206 loc) · 7.06 KB
/
NumberListParser.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
/*****************************************************************************
*
* $Id: NumberListParser.cpp,v ee4782738e30 2011/01/04 07:36:41 fp $
*
* Copyright (C) 2006-2009 Florian Pose, Ingenieurgemeinschaft IgH
*
* This file is part of the IgH EtherCAT Master.
*
* The IgH EtherCAT Master is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* The IgH EtherCAT Master is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with the IgH EtherCAT Master; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* ---
*
* The license mentioned above concerns the source code only. Using the
* EtherCAT technology and brand is only permitted in compliance with the
* industrial property and similar rights of Beckhoff Automation GmbH.
*
****************************************************************************/
#include <cstring>
#include <sstream>
#include <stdexcept>
using namespace std;
#include "NumberListParser.h"
/*****************************************************************************/
NumberListParser::NumberListParser():
max(0U),
hasMax(false)
{
}
/*****************************************************************************/
NumberListParser::~NumberListParser()
{
}
/*****************************************************************************/
NumberListParser::List NumberListParser::parse(const char *data)
{
List ret;
unsigned int i = 0, size = strlen(data), firstNum = 0U, secondNum = 0U;
typedef enum {
SectionStart,
FirstNumber,
Range,
SecondNumber,
Finished
} State;
State state = SectionStart;
while (state != Finished) {
switch (state) {
case SectionStart:
if (i >= size) {
state = Finished;
} else if (isNumeric(data[i])) {
firstNum = parseNumber(data, &i, size);
state = FirstNumber;
} else if (data[i] == '-') {
firstNum = 0U;
i++;
state = Range;
} else if (data[i] == ',') {
i++;
} else {
stringstream err;
err << "Invalid character " << data[i]
<< " at position " << i << "in state "
<< state << "." << endl;
throw runtime_error(err.str());
}
break;
case FirstNumber:
if (i >= size) {
ret.push_back(firstNum);
state = Finished;
} else if (data[i] == '-') {
i++;
state = Range;
} else if (data[i] == ',') {
i++;
ret.push_back(firstNum);
state = SectionStart;
} else {
stringstream err;
err << "Invalid character " << data[i]
<< " at position " << i << "in state "
<< state << "." << endl;
throw runtime_error(err.str());
}
break;
case Range:
if (i >= size) {
int max = maximum();
// only increasing ranges if second number omitted
if (max >= 0 && firstNum <= (unsigned int) max) {
List r = range(firstNum, max);
ret.splice(ret.end(), r);
}
state = Finished;
} else if (isNumeric(data[i])) {
secondNum = parseNumber(data, &i, size);
state = SecondNumber;
} else if (data[i] == ',') {
int max = maximum();
i++;
if (max >= 0) {
List r = range(firstNum, max);
ret.splice(ret.end(), r);
}
state = SectionStart;
} else {
stringstream err;
err << "Invalid character " << data[i]
<< " at position " << i << "in state "
<< state << "." << endl;
throw runtime_error(err.str());
}
break;
case SecondNumber:
if (i >= size) {
List r = range(firstNum, secondNum);
ret.splice(ret.end(), r);
state = Finished;
} else if (data[i] == ',') {
i++;
List r = range(firstNum, secondNum);
ret.splice(ret.end(), r);
state = SectionStart;
} else {
stringstream err;
err << "Invalid character " << data[i]
<< " at position " << i << "in state "
<< state << "." << endl;
throw runtime_error(err.str());
}
break;
default:
{
stringstream err;
err << "Invalid state " << state << ".";
throw runtime_error(err.str());
}
}
}
return ret;
}
/*****************************************************************************/
int NumberListParser::maximum()
{
if (!hasMax) {
max = getMax();
}
return max;
}
/*****************************************************************************/
bool NumberListParser::isNumeric(char c)
{
return c >= '0' && c <= '9';
}
/*****************************************************************************/
unsigned int NumberListParser::parseNumber(
const char *data,
unsigned int *i,
unsigned int size
)
{
unsigned int numSize = 0U, ret;
while (*i + numSize < size && isNumeric(data[*i + numSize])) {
numSize++;
}
if (numSize) {
stringstream str;
str << string(data + *i, numSize);
str >> ret;
} else {
throw runtime_error("EOF");
}
*i = *i + numSize;
return ret;
}
/****************************************************************************/
NumberListParser::List NumberListParser::range(
unsigned int i,
unsigned int j
)
{
List ret;
if (i <= j) {
for (; i <= j; i++) {
ret.push_back(i);
}
} else {
for (; j <= i; j++) {
ret.push_front(j);
}
}
return ret;
}
/****************************************************************************/