Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Parser.h
Go to the documentation of this file.
1 /* Copyright (C) 2009 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19 Customizeable Text Parser
20 
21 --Overview--
22 
23 CParserValue Data! (an int, real, string etc), basically an argument
24 CParserTaskType Syntax description for a line (ex. "variable=value")
25 CParserLine Parse _one_ line
26 CParser Include all syntax (CParserTaskTypes)
27 
28 The whole CParser* class group is used to read in config files and
29 give instruction on how that should be made. The CParserTaskType
30 declares what in a line is arguments, of course different CParserTaskTypes
31 will exist, and it's up to the system to figure out which one acquired.
32 
33 
34 --More Info--
35 
36  http://www.wildfiregames.com/forum/index.php?showtopic=134
37 
38 */
39 
40 #ifndef INCLUDED_PARSER
41 #define INCLUDED_PARSER
42 
43 #include "Pyrogenesis.h"
44 
45 #if MSC_VERSION
46 #pragma warning(disable:4786)
47 #endif
48 
49 //--------------------------------------------------------
50 // Includes / Compiler directives
51 //--------------------------------------------------------
52 
53 #include <vector>
54 #include <string>
55 #include <map>
56 #include <deque>
57 #include <cmath>
58 
59 #include "CStr.h"
60 
61 //-------------------------------------------------
62 // Types
63 //-------------------------------------------------
64 
66 {
72 };
73 
74 //-------------------------------------------------
75 // Declarations
76 //-------------------------------------------------
77 
78 class CParserValue;
79 class CParserTaskType;
80 class CParserLine;
81 class CParser;
82 
83 
84 // CParserValue
85 // ---------------------------------------------------------------------
86 // A parser value represents an argument
87 // color=r, g, b
88 // r, g and b will be CParserValues, or the arguments as they are called.
89 // This class can store only a string, but can try parsing it to different
90 // types
92 {
93 public:
94  CParserValue();
95  ~CParserValue();
96 
97  // return is error status
98  bool GetString(std::string &ret);
99  bool GetString( CStr& ret );
100  bool GetBool(bool &ret);
101  bool GetChar(char &ret); // As number! otherwise use GetString make sure size=1
102  bool GetShort(short &ret);
103  bool GetInt(int &ret);
104  bool GetLong(long &ret);
105  bool GetUnsignedShort(unsigned short &ret);
106  bool GetUnsignedInt(unsigned int &ret);
107  bool GetUnsignedLong(unsigned long &ret);
108  bool GetFloat(float &ret);
109  bool GetDouble(double &ret);
110 
111  // Memory regardless if it's an int, real, string or whatever
112  std::string m_String;
113 };
114 
115 // CParserTaskTypeNode
116 // ---------------------------------------------------------------------| Class
117 // A task type is basically a tree, this is because dynamic arguments
118 // requires alternative routes, so basically it's a binary tree with an
119 // obligatory next node (if it's not the end of the tree) and an alternative
120 // dynamic arguments node
121 //
122 // If we are at the beginning of this string, this will be the layout of the node
123 // "<$value_>:_"
124 //
125 // m_Element ":"
126 // m_AltNode => "$value"
127 // m_NextNode => "_"
128 //
130 {
131 public:
134 
135  // Free node pointers that are below this
136  void DeleteChildren();
137 
138  // Either the node is a letter or a type, if m_Letter is '\0'
139  // then check m_Type what it is
140  char m_Letter;
142  std::string m_String; // Used for diverse storage
143  // mainly for the typeAddArg
144 
145  // Parent node
147 
148  // Next node
150 
151  // true means AltNode can be looped <...>
152  // false means AltNode is just an optional part [...]
154 
155  // Whenever a dynamic argument is used, its first node is stored in this
156  // as an alternative node. The parser first checks if there is an
157  // alternative route, and if it applies to the next node. If not, proceed
158  // as usual with m_String and the next node
160 
161  // There are different kinds of alternative routes
162  //int m_AltRouteType;
163 };
164 
165 // CParserTaskType
166 // ---------------------------------------------------------------------| Class
167 // A task type is basically different kinds of lines for the parser
168 // variable=value is one type...
170 {
171 public:
172  CParserTaskType();
174 
175  // Delete the whole tree
176  void DeleteTree();
177 
179 
180  // Something to identify it with
181  std::string m_Name;
182 };
183 
184 // CParserLine
185 // ---------------------------------------------------------------------| Class
186 // Representing one line, i.e. one task, in a config file
188 {
189 public:
190  CParserLine();
191  ~CParserLine();
192 
193  std::deque<CParserValue> m_Arguments;
194  bool m_ParseOK; // same as ParseString will return
195  std::string m_TaskTypeName; // Name of the task type found
196 
197 protected:
198  bool ClearArguments();
199 
200 public:
201  // Interface
202  bool ParseString(const CParser& parser, const std::string &line);
203 
204  // Methods for getting arguments
205  // it returns success
206  bool GetArgString (size_t arg, std::string &ret);
207  bool GetArgBool (size_t arg, bool &ret);
208  bool GetArgChar (size_t arg, char &ret);
209  bool GetArgShort (size_t arg, short &ret);
210  bool GetArgInt (size_t arg, int &ret);
211  bool GetArgLong (size_t arg, long &ret);
212  bool GetArgUnsignedShort (size_t arg, unsigned short &ret);
213  bool GetArgUnsignedInt (size_t arg, unsigned int &ret);
214  bool GetArgUnsignedLong (size_t arg, unsigned long &ret);
215  bool GetArgFloat (size_t arg, float &ret);
216  bool GetArgDouble (size_t arg, double &ret);
217 
218  // Get Argument count
219  size_t GetArgCount() const { return m_Arguments.size(); }
220 };
221 
222 // CParser
223 // ---------------------------------------------------------------------| Class
224 // Includes parsing instruction, i.e. task-types
225 class CParser
226 {
227 public:
228  CParser();
229  ~CParser();
230 
231  std::vector<CParserTaskType> m_TaskTypes;
232 
233  // Interface
234  bool InputTaskType(const std::string& strName, const std::string& strSyntax);
235 };
236 
237 
238 
239 // CParserCache
240 // ---------------------------------------------------------------------| Class
241 // Provides access to CParser objects, caching them to avoid
242 // reconstructing the object every time a string needs to be parsed.
244 {
245 public:
246  // Returns a simple parser based on a single nameless task-type
247  static CParser& Get(const char* str);
248 
249 private:
250  // Self-destructing std::map
251  template <typename T, typename P> class SDMap : public std::map<T,P>
252  {
253  public:
254  typedef typename std::map<T,P>::iterator iterator;
256  {
257  for (iterator it = this->begin(); it != this->end(); ++it) delete it->second;
258  }
259  };
260 
262 
264 };
265 
266 #endif
bool GetArgBool(size_t arg, bool &ret)
bool GetInt(int &ret)
SDMap< std::string, CParser * > CacheType
Definition: Parser.h:261
std::string m_Name
Definition: Parser.h:181
std::deque< CParserValue > m_Arguments
Definition: Parser.h:193
bool GetArgShort(size_t arg, short &ret)
std::string m_String
Definition: Parser.h:112
void DeleteTree()
Definition: Parser.cpp:270
_ParserValueType
Definition: Parser.h:65
bool GetArgLong(size_t arg, long &ret)
bool GetArgInt(size_t arg, int &ret)
bool GetString(std::string &ret)
Definition: Parser.cpp:204
bool GetArgFloat(size_t arg, float &ret)
std::string m_TaskTypeName
Definition: Parser.h:195
bool GetBool(bool &ret)
Definition: Parser.cpp:102
CParserValue()
Definition: Parser.cpp:91
~CParser()
Definition: Parser.cpp:799
static CacheType m_Cached
Definition: Parser.h:263
bool GetUnsignedShort(unsigned short &ret)
bool GetArgUnsignedInt(size_t arg, unsigned int &ret)
bool GetArgDouble(size_t arg, double &ret)
bool GetDouble(double &ret)
Definition: Parser.cpp:133
bool GetFloat(float &ret)
bool GetShort(short &ret)
bool ClearArguments()
Definition: Parser.cpp:294
bool GetArgChar(size_t arg, char &ret)
std::map< T, P >::iterator iterator
Definition: Parser.h:254
CParserTaskTypeNode * m_AltNode
Definition: Parser.h:159
~CParserLine()
Definition: Parser.cpp:287
std::string m_String
Definition: Parser.h:142
CParserTaskTypeNode * m_NextNode
Definition: Parser.h:149
void DeleteChildren()
Definition: Parser.cpp:240
bool GetChar(char &ret)
std::vector< CParserTaskType > m_TaskTypes
Definition: Parser.h:231
bool ParseString(const CParser &parser, const std::string &line)
Definition: Parser.cpp:325
bool GetUnsignedLong(unsigned long &ret)
_ParserValueType m_Type
Definition: Parser.h:141
bool GetArgString(size_t arg, std::string &ret)
static CParser & Get(const char *str)
Definition: Parser.cpp:1069
size_t GetArgCount() const
Definition: Parser.h:219
bool m_ParseOK
Definition: Parser.h:194
CParser()
Definition: Parser.cpp:794
bool GetArgUnsignedLong(size_t arg, unsigned long &ret)
bool GetUnsignedInt(unsigned int &ret)
bool InputTaskType(const std::string &strName, const std::string &strSyntax)
Definition: Parser.cpp:816
CParserTaskTypeNode * m_BaseNode
Definition: Parser.h:178
CParserTaskTypeNode * m_ParentNode
Definition: Parser.h:146
bool m_AltNodeRepeatable
Definition: Parser.h:153
bool GetLong(long &ret)
~CParserValue()
Definition: Parser.cpp:95
bool GetArgUnsignedShort(size_t arg, unsigned short &ret)