1
// *************************************************************************
4
// * File: gsm_parser.cc
6
// * Purpose: Parser to parse MA/TA result strings
8
// * Author: Peter Hofmann (software@pxh.de)
10
// * Created: 13.5.1999
11
// *************************************************************************
14
#include <gsm_config.h>
16
#include <gsmlib/gsm_parser.h>
17
#include <gsmlib/gsm_nls.h>
23
using namespace gsmlib;
27
int Parser::nextChar(bool skipWhiteSpace)
30
while (_i < _s.length() && isspace(_s[_i])) ++_i;
32
if (_i == _s.length())
41
bool Parser::checkEmptyParameter(bool allowNoParameter) throw(GsmException)
44
if (c == ',' || c == -1)
51
throwParseException(_("expected parameter"));
57
string Parser::parseString2(bool stringWithQuotationMarks)
62
if (parseChar('"', true)) // OK, string starts and ends with quotation mark
63
if (stringWithQuotationMarks)
65
// read till end of line
66
while ((c = nextChar(false)) != -1)
69
// check for """ at end of line
70
if (result.length() == 0 || result[result.length() - 1] != '"')
71
throwParseException(_("expected '\"'"));
73
// remove """ at the end
74
result.resize(result.length() - 1);
79
while ((c = nextChar(false)) != '"')
81
throwParseException();
85
else // string ends with "," or EOL
88
while (c != ',' && c != -1)
93
if (c == ',') putBackChar();
99
int Parser::parseInt2() throw(GsmException)
105
while (isdigit(c = nextChar())) s += c;
109
throwParseException(_("expected number"));
111
istrstream is(s.c_str());
116
void Parser::throwParseException(string message) throw(GsmException)
119
if (message.length() == 0)
120
throw GsmException(stringPrintf(_("unexpected end of string '%s'"),
121
_s.c_str()), ParserError);
123
throw GsmException(message +
124
stringPrintf(_(" (at position %d of string '%s')"), _i,
125
_s.c_str()), ParserError);
128
Parser::Parser(string s) : _i(0), _s(s), _eos(false)
132
bool Parser::parseChar(char c, bool allowNoChar) throw(GsmException)
141
throwParseException(stringPrintf(_("expected '%c'"), c));
145
vector<string> Parser::parseStringList(bool allowNoList)
148
// handle case of empty parameter
149
vector<string> result;
150
if (checkEmptyParameter(allowNoList)) return result;
153
if (nextChar() != ')')
158
result.push_back(parseString());
163
throwParseException();
165
throwParseException(_("expected ')' or ','"));
172
vector<bool> Parser::parseIntList(bool allowNoList)
175
// handle case of empty parameter
176
bool isRange = false;
178
int resultCapacity = 0;
179
unsigned int saveI = _i;
181
if (checkEmptyParameter(allowNoList)) return result;
183
// check for the case of a integer list consisting of only one parameter
184
// some TAs omit the parentheses in this case
185
if (isdigit(nextChar()))
188
int num = parseInt();
189
result.resize(num + 1, false);
196
// pass 0: find capacity needed for result
197
// pass 1: resize result and fill it in
198
for (int pass = 0; pass < 2; ++pass)
203
result.resize(resultCapacity + 1, false);
207
if (nextChar() != ')')
213
int thisInt = parseInt();
217
assert(lastInt != -1);
218
if (lastInt <= thisInt)
219
for (int i = lastInt; i < thisInt; ++i)
221
if (i > resultCapacity)
227
for (int i = thisInt; i < lastInt; ++i)
229
if (i > resultCapacity)
237
if (thisInt > resultCapacity)
238
resultCapacity = thisInt;
240
result[thisInt] = true;
248
throwParseException();
250
if (c != ',' && c != '-')
251
throwParseException(_("expected ')', ',' or '-'"));
257
throwParseException(_("range of the form a-b-c not allowed"));
264
throwParseException(_("range of the form a- no allowed"));
268
IntRange Parser::parseRange(bool allowNoRange)
271
// handle case of empty parameter
273
if (checkEmptyParameter(allowNoRange)) return result;
276
result._low = parseInt();
278
result._high = parseInt();
284
int Parser::parseInt(bool allowNoInt) throw(GsmException)
286
// handle case of empty parameter
287
int result = NOT_SET;
288
if (checkEmptyParameter(allowNoInt)) return result;
290
result = parseInt2();
295
string Parser::parseString(bool allowNoString,
296
bool stringWithQuotationMarks)
299
// handle case of empty parameter
301
if (checkEmptyParameter(allowNoString)) return result;
303
result = parseString2(stringWithQuotationMarks);
308
bool Parser::parseComma(bool allowNoComma) throw(GsmException)
310
if (nextChar() != ',')
317
throwParseException(_("expected comma"));
321
string Parser::parseEol() throw(GsmException)
325
while ((c = nextChar()) != -1) result += c;
329
void Parser::checkEol() throw(GsmException)
331
if (nextChar() != -1)
334
throwParseException(_("expected end of line"));
338
string Parser::getEol()
342
unsigned int saveI = _i;
344
while ((c = nextChar()) != -1) result += c;