Newer
Older
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
/*****************************************************************************
*
* $Id$
*
* 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)
{
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
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) {
// 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] == ',') {
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()
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
{
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
)
{
if (i <= j) {
for (; i <= j; i++) {
ret.push_back(i);
}
} else {
for (; j <= i; j++) {
ret.push_front(j);
}
}
return ret;
}
/****************************************************************************/