LineParser.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2018 New Eagle
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of New Eagle nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
36 
37 namespace NewEagle
38 {
39  LineParser::LineParser(const std::string &line)
40  {
41  _line = line;
42  _position = 0;
43  }
44 
46  {
47  }
48 
50  {
51  return _position;
52  }
53 
55  {
57 
58  if (AtEOL())
59  {
61  }
62 
63  if (!isalpha(_line[_position]) && _line[_position] != '_')
64  {
65  throw std::runtime_error("ReadCIdentifier: Unexpected character");
66  }
67 
68  int32_t startIdx = _position;
69 
70  for(_position++; !AtEOL(); _position++)
71  {
72  if (!(isalpha(_line[_position]) || isdigit(_line[_position])) && _line[_position] != '_')
73  {
74  int32_t len = _position - startIdx;
75  return _line.substr(startIdx, len);
76  }
77  }
78 
79  return _line.substr(startIdx, std::string::npos);
80  }
81 
82  std::string LineParser::ReadCIdentifier(std::string fieldName)
83  {
84  std::string val = ReadCIdentifier();
85 
86  if (val == std::string())
87  {
88  throw std::runtime_error("Synxax Error: Expected : " + fieldName);
89  }
90 
91  return val;
92 
93  }
94 
96  {
97  while(!AtEOL() && isspace(_line[_position]))
98  {
99  _position++;
100  }
101  }
102 
104  {
105  return _position >= _line.length();
106  }
107 
109  {
110  SkipWhitespace();
111 
112  if (AtEOL())
113  {
114  throw LineParserAtEOLException();
115  }
116 
117  int32_t idx = _position;
118  _position++;
119 
120  return _line[idx];
121 
122  }
123 
124  char LineParser::ReadNextChar(std::string fieldName)
125  {
126  try
127  {
128  char val = ReadNextChar();
129  return val;
130  }
131  catch(LineParserExceptionBase& exlp)
132  {
133  throw;
134  }
135 
136  }
137 
139  {
140  SkipWhitespace();
141 
142  if (AtEOL())
143  {
144  throw LineParserAtEOLException();
145  }
146 
147  int32_t position = _position;
148 
149  int32_t startIdx = position;
150  int32_t len = -1;
151 
152  for(; !AtEOL(); position++)
153  {
154  if (!isdigit(_line[position]))
155  {
156  len = position - startIdx;
157  break;
158  }
159  }
160 
161  if (-1 == len)
162  {
163  len = position - startIdx;
164  }
165 
166  if (0 == len)
167  {
169  }
170 
171  std::istringstream reader(_line.substr(startIdx, len));
172  uint32_t val;
173  reader >> val;
174 
175  return val;
176  }
177 
179  {
180  SkipWhitespace();
181 
182  if (AtEOL())
183  {
184  throw LineParserAtEOLException();
185  }
186 
187  int32_t startIdx = _position;
188  int32_t len = -1;
189 
190  for(; !AtEOL(); _position++)
191  {
192  if (!isdigit(_line[_position]))
193  {
194  len = _position - startIdx;
195  break;
196  }
197  }
198 
199  if (-1 == len)
200  {
201  len = _position - startIdx;
202  }
203 
204  if (0 == len)
205  {
207  }
208 
209  std::istringstream reader(_line.substr(startIdx, len));
210  uint32_t val;
211  reader >> val;
212 
213  return val;
214  }
215 
216  uint32_t LineParser::ReadUInt(std::string fieldName)
217  {
218  try
219  {
220  uint32_t val = ReadUInt();
221  return val;
222  }
223  catch(LineParserExceptionBase& exlp)
224  {
225  throw;
226  }
227  }
228 
230  {
231  SkipWhitespace();
232 
233  if (AtEOL())
234  {
235  throw LineParserAtEOLException();
236  }
237 
238  if (!isdigit(_line[_position]) && _line[_position] != '-' && _line[_position] != '+')
239  {
241  }
242 
243  int32_t startIdx = _position;
244  int32_t len = -1;
245 
246  for(_position++; !AtEOL(); _position++)
247  {
248  if (!isdigit(_line[_position]))
249  {
250  len = _position - startIdx;
251  break;
252  }
253  }
254 
255  if (-1 == len)
256  {
257  len = _position - startIdx;
258  }
259 
260  if (0 == len)
261  {
263  }
264 
265  std::istringstream reader(_line.substr(startIdx, len));
266  int32_t val;
267  reader >> val;
268 
269  return val;
270 
271  }
272 
274  {
275  SkipWhitespace();
276 
277  if (AtEOL())
278  {
279  throw LineParserAtEOLException();
280  }
281 
282  if (!isdigit(_line[_position]) && _line[_position] != '-' && _line[_position] != '+')
283  {
285  }
286 
287  int32_t startIdx = _position;
288  int32_t len = -1;
289 
291 
292  for(_position++; !AtEOL(); _position++)
293  {
294  char c = _line[_position];
295 
296  switch (state) {
298  if ('E' == c || 'e' == c)
299  {
300  state = NewEagle::READ_E;
301  }
302  else if('.' == c)
303  {
305  }
306  else if (!isdigit(c))
307  {
308  goto DoneReading;
309  }
310  break;
312  if ('E' == c || 'e' == c)
313  {
314  state = NewEagle::READ_E;
315  }
316  else if(!isdigit(c))
317  {
318  goto DoneReading;
319  }
320  break;
321  case NewEagle::READ_E:
322  if ('+' == c || '-' == c)
323  {
324  state = NewEagle::READ_SIGN;
325  }
326  else if (isdigit(c))
327  {
328  state = NewEagle::READING_EXP;
329  }
330  break;
331  case NewEagle::READ_SIGN:
332  if (!isdigit(c))
333  {
335  }
336  else
337  {
338  state = NewEagle::READING_EXP;
339  }
340 
341  break;
343  if (!isdigit(c))
344  {
345  goto DoneReading;
346  }
347  break;
348  default:
349  break;
350 
351  }
352 
353  }
354 
355 DoneReading:
356  len = _position - startIdx;
357 
358  if (0 == len)
359  {
361  }
362 
363  std::istringstream reader(_line.substr(startIdx, len));
364  double val;
365  reader >> val;
366 
367  return val;
368 
369  }
370 
371  double LineParser::ReadDouble(std::string fieldName)
372  {
373  try
374  {
375  double val = ReadDouble();
376  return val;
377  }
378  catch(LineParserExceptionBase& exlp)
379  {
380  throw;
381  }
382 
383  }
384 
385  void LineParser::SeekSeparator(char separator)
386  {
387  char nextChar = ReadNextChar();
388 
389  if (nextChar == 0x00 || nextChar != separator)
390  {
391  throw std::runtime_error("Synxax Error: Expected : " + separator);
392  }
393  }
394 
396  {
397  SkipWhitespace();
398 
399  if (AtEOL())
400  {
401  throw LineParserAtEOLException();
402  }
403 
404  if (_line[_position] != '"')
405  {
406  throw std::runtime_error("ReadQuotedString: Missing Quote");
407  }
408 
409  int32_t startIdx = ++_position;
410  int32_t len = -1;
411 
412  for (; _position < _line.size(); _position++)
413  {
414  if (_line[_position] == '"')
415  {
416  len = _position - startIdx;
417  _position++;
418  break;
419  }
420  }
421 
422  if (-1 == len)
423  {
425  }
426 
427  if (0 == len)
428  {
430  }
431 
432  return _line.substr(startIdx, len);
433  }
434 }
Definition: Dbc.h:45
ReadDoubleState
Definition: LineParser.h:74
void SeekSeparator(char separator)
Definition: LineParser.cpp:385
LineParser(const std::string &line)
Definition: LineParser.cpp:39
std::string ReadCIdentifier()
Definition: LineParser.cpp:54
std::string _line
Definition: LineParser.h:104
std::string ReadQuotedString()
Definition: LineParser.cpp:395


can_dbc_parser
Author(s): Ryan Borchert
autogenerated on Sat Jan 9 2021 03:56:18