parser.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef CXXTOOLS_BIN_PARSER_H
30 #define CXXTOOLS_BIN_PARSER_H
31 
33 #include <vector>
34 
35 namespace cxxtools
36 {
37 
38 namespace bin
39 {
40 
41 class Deserializer;
42 
43 class Parser
44 {
45  Parser(const Parser&) { }
46  Parser& operator= (const Parser&) { return *this; }
47 
48  explicit Parser(std::vector<std::string>* dictionary)
49  : _next(0),
50  _dictionary(dictionary)
51  { }
52 
53  public:
55  : _next(0),
56  _dictionary(&_mydictionary)
57  { }
58 
60  {
61  delete _next;
62  }
63 
64  void begin(Deserializer& handler);
65 
66  void finish();
67 
68  void skip();
69 
70  bool advance(char ch); // returns true, if number is read completely
71 
72  private:
73 
74  bool processFloatBase(char ch, unsigned shift, unsigned expOffset);
75  void dict(const std::string& s);
76 
77  enum State
78  {
79  state_type,
80  state_name,
81  state_name_idx0,
82  state_name_idx1,
83  state_value_type_other,
84  state_value_type_other_idx0,
85  state_value_type_other_idx1,
86  state_value_intsign,
87  state_value_int,
88  state_value_uint,
89  state_value_bool,
90  state_value_bcd0,
91  state_value_bcd,
92  state_value_binary_length,
93  state_value_binary,
94  state_value_value,
95  state_sfloat_exp,
96  state_sfloat_base,
97  state_mfloat_exp,
98  state_mfloat_base,
99  state_lfloat_exp,
100  state_lfloat_base,
101  state_object_type,
102  state_object_type_other,
103  state_object_type_other_idx0,
104  state_object_type_other_idx1,
105  state_object_member,
106  state_object_member_value,
107  state_array_type,
108  state_array_type_other,
109  state_array_type_other_idx0,
110  state_array_type_other_idx1,
111  state_array_member,
112  state_array_member_value,
113  state_array_member_value_next,
114  state_end
115  } _state, _nextstate;
116 
117  std::string _token;
118  unsigned _count;
119  uint64_t _int;
120  int _exp;
121  bool _isNeg;
122  unsigned _dictidx;
123  Deserializer* _deserializer;
124  Parser* _next;
125  std::vector<std::string> _mydictionary;
126  std::vector<std::string>* _dictionary;
127 };
128 }
129 }
130 
131 #endif // CXXTOOLS_BIN_PARSER_H
132