Mercurial > code
comparison C++/Parser.h @ 180:2bcdee0fe8d4
Update parser
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 26 Sep 2013 19:39:24 +0200 |
parents | 3648e9e6935b |
children | 600754c27c88 |
comparison
equal
deleted
inserted
replaced
179:3648e9e6935b | 180:2bcdee0fe8d4 |
---|---|
19 #ifndef _PARSER_H_ | 19 #ifndef _PARSER_H_ |
20 #define _PARSER_H_ | 20 #define _PARSER_H_ |
21 | 21 |
22 #include <cstdlib> | 22 #include <cstdlib> |
23 #include <exception> | 23 #include <exception> |
24 #include <functional> | |
24 #include <iostream> | 25 #include <iostream> |
25 #include <string> | 26 #include <string> |
26 #include <vector> | 27 #include <vector> |
27 | 28 |
28 /** | 29 /** |
33 private: | 34 private: |
34 std::string m_key; | 35 std::string m_key; |
35 | 36 |
36 public: | 37 public: |
37 NotFoundException(const std::string &key) | 38 NotFoundException(const std::string &key) |
38 :m_key(key) | 39 : m_key(key) |
39 { | 40 { |
40 } | 41 } |
41 | 42 |
42 const std::string & which() const | 43 const std::string & which() const |
43 { | 44 { |
64 /** | 65 /** |
65 * A list of section found in the file. If root | 66 * A list of section found in the file. If root |
66 * options are allowed (default behavior), the root | 67 * options are allowed (default behavior), the root |
67 * section is "". | 68 * section is "". |
68 */ | 69 */ |
69 struct Section | 70 class Section |
70 { | 71 { |
72 private: | |
73 const std::string findOption(const std::string &name) const; | |
74 | |
75 public: | |
71 std::string m_name; /*! name of section */ | 76 std::string m_name; /*! name of section */ |
72 std::vector<Option> m_options; /*! list of options inside */ | 77 std::vector<Option> m_options; /*! list of options inside */ |
73 bool m_allowed; /*! is authorized to push */ | 78 bool m_allowed; /*! is authorized to push */ |
74 | 79 |
80 /** | |
81 * Default constructor. | |
82 */ | |
75 Section(); | 83 Section(); |
76 ~Section(); | |
77 | |
78 /** | |
79 * Copy constructor | |
80 */ | |
81 Section(const Section &s); | |
82 | 84 |
83 /** | 85 /** |
84 * Get the section name | 86 * Get the section name |
85 * | 87 * |
86 * @return the section name | 88 * @return the section name |
87 */ | 89 */ |
88 const std::string & getName() const; | 90 const std::string &getName() const; |
89 | |
90 /** | |
91 * Search an option value. | |
92 * | |
93 * @param name the option name | |
94 * @return the value or "" if not found | |
95 */ | |
96 const std::string findOption(const std::string &name) const; | |
97 | 91 |
98 /** | 92 /** |
99 * Get all options from that section. | 93 * Get all options from that section. |
100 * | 94 * |
101 * @return the list of options | 95 * @return the list of options |
102 */ | 96 */ |
103 const std::vector<Option> & getOptions() const; | 97 const std::vector<Option> &getOptions() const; |
104 | 98 |
105 /** | 99 /** |
106 * Tells if that section has the specified option name. | 100 * Tells if that section has the specified option name. |
107 * | 101 * |
108 * @param name the option name | 102 * @param name the option name |
115 * | 109 * |
116 * @param name the option name | 110 * @param name the option name |
117 * @return the value if found | 111 * @return the value if found |
118 */ | 112 */ |
119 template <typename T> | 113 template <typename T> |
120 T getOption(const std::string &name) const; | 114 T getValue(const std::string &name) const; |
121 | 115 |
122 /** | 116 /** |
123 * Requires an option, this works like getOption except | 117 * Requires an option, this works like getOption except |
124 * that if an option is not found, an exception is | 118 * that if an option is not found, an exception is |
125 * thrown. | 119 * thrown. |
126 * | 120 * |
127 * @param name the name | 121 * @param name the name |
122 * @return the value | |
128 * @throw NotFoundException if not found | 123 * @throw NotFoundException if not found |
129 * @return the value | |
130 */ | 124 */ |
131 template <typename T> | 125 template <typename T> |
132 T requireOption(const std::string &name) const | 126 T requireValue(const std::string &name) const |
133 { | 127 { |
134 if (!hasOption(name)) | 128 if (!hasOption(name)) |
135 throw NotFoundException(name); | 129 throw NotFoundException(name); |
136 | 130 |
137 return getOption<T>(name); | 131 return getValue<T>(name); |
138 } | 132 } |
139 | 133 |
140 friend std::ostream & operator<<(std::ostream & stream, const Section §ion) | 134 friend std::ostream & operator<<(std::ostream & stream, const Section §ion) |
141 { | 135 { |
142 stream << "[" << section.getName() << "]" << std::endl; | 136 stream << "[" << section.getName() << "]" << std::endl; |
160 { | 154 { |
161 DisableRootSection = 1, /*! disable options on root */ | 155 DisableRootSection = 1, /*! disable options on root */ |
162 DisableRedefinition = 2, /*! disable multiple redefinition */ | 156 DisableRedefinition = 2, /*! disable multiple redefinition */ |
163 DisableVerbosity = 4 /*! be verbose by method */ | 157 DisableVerbosity = 4 /*! be verbose by method */ |
164 }; | 158 }; |
159 | |
160 typedef std::function<void (const Section &)> FindFunc; | |
165 | 161 |
166 private: | 162 private: |
167 std::vector<Section> m_sections; /*! list of sections found */ | 163 std::vector<Section> m_sections; /*! list of sections found */ |
168 std::string m_error; /*! if an error occured */ | 164 std::string m_error; /*! if an error occured */ |
169 std::string m_path; /*! path file */ | 165 std::string m_path; /*! path file */ |
212 /** | 208 /** |
213 * Get the error message if any | 209 * Get the error message if any |
214 * | 210 * |
215 * @return the error message | 211 * @return the error message |
216 */ | 212 */ |
217 const std::string & getError() const; | 213 const std::string &getError() const; |
218 | 214 |
219 /** | 215 /** |
220 * Get all sections found | 216 * Get all sections found |
221 * | 217 * |
222 * @return all sections | 218 * @return all sections |
223 */ | 219 */ |
224 const std::vector<Section> & getSections() const; | 220 const std::vector<Section> &getSections() const; |
225 | 221 |
226 /** | 222 /** |
227 * Get a list of sections for config which multiple | 223 * Find all sections matching the name. |
228 * definitions are allowed. This does a full copy of sections | |
229 * and options. | |
230 * | 224 * |
231 * @param name the sections name | 225 * @param name the sections name |
226 * @param func the function | |
232 * @return a list of section with the options | 227 * @return a list of section with the options |
233 */ | 228 */ |
234 std::vector<Section> findSections(const std::string &name) const; | 229 void findSections(const std::string &name, FindFunc func) const; |
235 | 230 |
236 /** | 231 /** |
237 * Tell if a section is existing. | 232 * Tell if a section is existing. |
238 * | 233 * |
239 * @return true if exists | 234 * @return true if exists |
243 /** | 238 /** |
244 * Get a specified section. | 239 * Get a specified section. |
245 * | 240 * |
246 * @param name the section name | 241 * @param name the section name |
247 * @return a section | 242 * @return a section |
248 */ | 243 * @throw NotFoundException if not found |
249 Section getSection(const std::string &name) const; | 244 */ |
245 const Section &getSection(const std::string &name) const; | |
250 | 246 |
251 /** | 247 /** |
252 * Same as getSection except that throws an exception if | 248 * Same as getSection except that throws an exception if |
253 * the section is not found. | 249 * the section is not found. |
254 * | 250 * |
255 * @param name the section name | 251 * @param name the section name |
252 * @return the section | |
256 * @throw NotFoundException if not found | 253 * @throw NotFoundException if not found |
257 * @return the section | 254 */ |
258 */ | 255 const Section &requireSection(const std::string &name) const; |
259 Section requireSection(const std::string &name) const; | |
260 | 256 |
261 /** | 257 /** |
262 * Logging function, used only if DisableVerbosity is not set. The | 258 * Logging function, used only if DisableVerbosity is not set. The |
263 * default behavior is to print to stdout something like: | 259 * default behavior is to print to stdout something like: |
264 * line 10: syntax error | 260 * line 10: syntax error |
292 * @param option the current option | 288 * @param option the current option |
293 * @see dump | 289 * @see dump |
294 */ | 290 */ |
295 virtual void dumpOption(const Option &option); | 291 virtual void dumpOption(const Option &option); |
296 | 292 |
297 friend std::ostream & operator<<(std::ostream & stream, const Parser &parser) | 293 friend std::ostream &operator<<(std::ostream & stream, const Parser &parser) |
298 { | 294 { |
299 for (auto s : parser.m_sections) | 295 for (auto s : parser.m_sections) |
300 stream << s;; | 296 stream << s;; |
301 | 297 |
302 return stream; | 298 return stream; |