1 | /* ---------------------------------------------------------------------------- |
---|
2 | |
---|
3 | THE FILE IS UNDER THE CONSTRUCTION! |
---|
4 | |
---|
5 | This file is a mex clone of libconfig.h++ originally found |
---|
6 | in the libconfig library which was desgigned by Mark A Lindner |
---|
7 | |
---|
8 | ---------------------------------------------------------------------------- |
---|
9 | */ |
---|
10 | |
---|
11 | #ifndef LIBCONFIG_MEX_H |
---|
12 | #define LIBCONFIG_MEX_H |
---|
13 | |
---|
14 | #include <stdio.h> |
---|
15 | #include <string> |
---|
16 | #include <map> |
---|
17 | |
---|
18 | |
---|
19 | struct config_t; // fwd decl |
---|
20 | struct config_setting_t; // fwd decl |
---|
21 | |
---|
22 | namespace libconfig { |
---|
23 | |
---|
24 | class ConfigException : public std::exception { }; |
---|
25 | |
---|
26 | class Setting; // fwd decl |
---|
27 | |
---|
28 | class SettingException : public ConfigException |
---|
29 | { |
---|
30 | friend class Config; |
---|
31 | |
---|
32 | public: |
---|
33 | |
---|
34 | SettingException(const SettingException &other); |
---|
35 | SettingException& operator=(const SettingException &other); |
---|
36 | |
---|
37 | virtual ~SettingException() throw(); |
---|
38 | |
---|
39 | const char *getPath() const; |
---|
40 | |
---|
41 | virtual const char *what() const throw(); |
---|
42 | |
---|
43 | protected: |
---|
44 | |
---|
45 | SettingException(const Setting &setting); |
---|
46 | SettingException(const Setting &setting, int idx); |
---|
47 | SettingException(const Setting &setting, const char *name); |
---|
48 | SettingException(const char *path); |
---|
49 | |
---|
50 | private: |
---|
51 | |
---|
52 | char *_path; |
---|
53 | }; |
---|
54 | |
---|
55 | class SettingTypeException : public SettingException |
---|
56 | { |
---|
57 | friend class Config; |
---|
58 | friend class Setting; |
---|
59 | |
---|
60 | public: |
---|
61 | |
---|
62 | const char *what() const throw(); |
---|
63 | |
---|
64 | private: |
---|
65 | |
---|
66 | SettingTypeException(const Setting &setting); |
---|
67 | SettingTypeException(const Setting &setting, int idx); |
---|
68 | SettingTypeException(const Setting &setting, const char *name); |
---|
69 | }; |
---|
70 | |
---|
71 | class SettingNotFoundException : public SettingException |
---|
72 | { |
---|
73 | friend class Config; |
---|
74 | friend class Setting; |
---|
75 | |
---|
76 | public: |
---|
77 | |
---|
78 | const char *what() const throw(); |
---|
79 | |
---|
80 | private: |
---|
81 | |
---|
82 | SettingNotFoundException(const Setting &setting, int idx); |
---|
83 | SettingNotFoundException(const Setting &setting, const char *name); |
---|
84 | SettingNotFoundException(const char *path); |
---|
85 | }; |
---|
86 | |
---|
87 | class SettingNameException : public SettingException |
---|
88 | { |
---|
89 | friend class Config; |
---|
90 | friend class Setting; |
---|
91 | |
---|
92 | public: |
---|
93 | |
---|
94 | const char *what() const throw(); |
---|
95 | |
---|
96 | private: |
---|
97 | |
---|
98 | SettingNameException(const Setting &setting, const char *name); |
---|
99 | }; |
---|
100 | |
---|
101 | class FileIOException : public ConfigException |
---|
102 | { |
---|
103 | public: |
---|
104 | |
---|
105 | const char *what() const throw(); |
---|
106 | }; |
---|
107 | |
---|
108 | class ParseException : public ConfigException |
---|
109 | { |
---|
110 | friend class Config; |
---|
111 | |
---|
112 | public: |
---|
113 | |
---|
114 | virtual ~ParseException() throw(); |
---|
115 | |
---|
116 | inline const char *getFile() const throw() |
---|
117 | { return(_file); } |
---|
118 | |
---|
119 | inline int getLine() const throw() |
---|
120 | { return(_line); } |
---|
121 | |
---|
122 | inline const char *getError() const throw() |
---|
123 | { return(_error); } |
---|
124 | |
---|
125 | const char *what() const throw(); |
---|
126 | |
---|
127 | private: |
---|
128 | |
---|
129 | ParseException(const char *file, int line, const char *error); |
---|
130 | |
---|
131 | const char *_file; |
---|
132 | int _line; |
---|
133 | const char *_error; |
---|
134 | }; |
---|
135 | |
---|
136 | class Setting |
---|
137 | { |
---|
138 | friend class Config; |
---|
139 | |
---|
140 | public: |
---|
141 | |
---|
142 | enum Type |
---|
143 | { |
---|
144 | TypeNone = 0, |
---|
145 | // scalar types |
---|
146 | TypeInt, |
---|
147 | TypeInt64, |
---|
148 | TypeFloat, |
---|
149 | TypeString, |
---|
150 | TypeBoolean, |
---|
151 | // aggregate types |
---|
152 | TypeGroup, |
---|
153 | TypeArray, |
---|
154 | TypeList |
---|
155 | }; |
---|
156 | |
---|
157 | enum Format |
---|
158 | { |
---|
159 | FormatDefault = 0, |
---|
160 | FormatHex = 1 |
---|
161 | }; |
---|
162 | |
---|
163 | private: |
---|
164 | |
---|
165 | config_setting_t *_setting; |
---|
166 | Type _type; |
---|
167 | Format _format; |
---|
168 | |
---|
169 | Setting(config_setting_t *setting); |
---|
170 | |
---|
171 | void assertType(Type type) const |
---|
172 | throw(SettingTypeException); |
---|
173 | static Setting & wrapSetting(config_setting_t *setting); |
---|
174 | |
---|
175 | Setting(const Setting& other); // not supported |
---|
176 | Setting& operator=(const Setting& other); // not supported |
---|
177 | |
---|
178 | public: |
---|
179 | |
---|
180 | virtual ~Setting() throw(); |
---|
181 | |
---|
182 | inline Type getType() const throw() { return(_type); } |
---|
183 | |
---|
184 | inline Format getFormat() const throw() { return(_format); } |
---|
185 | void setFormat(Format format) throw(); |
---|
186 | |
---|
187 | operator bool() const throw(SettingTypeException); |
---|
188 | operator int() const throw(SettingTypeException); |
---|
189 | operator unsigned int() const throw(SettingTypeException); |
---|
190 | operator long() const throw(SettingTypeException); |
---|
191 | operator unsigned long() const throw(SettingTypeException); |
---|
192 | operator long long() const throw(SettingTypeException); |
---|
193 | operator unsigned long long() const throw(SettingTypeException); |
---|
194 | operator double() const throw(SettingTypeException); |
---|
195 | operator float() const throw(SettingTypeException); |
---|
196 | operator const char *() const throw(SettingTypeException); |
---|
197 | operator std::string() const throw(SettingTypeException); |
---|
198 | |
---|
199 | Setting & operator=(bool value) throw(SettingTypeException); |
---|
200 | Setting & operator=(int value) throw(SettingTypeException); |
---|
201 | Setting & operator=(long value) throw(SettingTypeException); |
---|
202 | Setting & operator=(const long long &value) throw(SettingTypeException); |
---|
203 | Setting & operator=(const double &value) throw(SettingTypeException); |
---|
204 | Setting & operator=(float value) throw(SettingTypeException); |
---|
205 | Setting & operator=(const char *value) throw(SettingTypeException); |
---|
206 | Setting & operator=(const std::string &value) throw(SettingTypeException); |
---|
207 | |
---|
208 | Setting & operator[](const char * key) const |
---|
209 | throw(SettingTypeException, SettingNotFoundException); |
---|
210 | |
---|
211 | inline Setting & operator[](const std::string & key) const |
---|
212 | throw(SettingTypeException, SettingNotFoundException) |
---|
213 | { return(operator[](key.c_str())); } |
---|
214 | |
---|
215 | Setting & operator[](int index) const |
---|
216 | throw(SettingTypeException, SettingNotFoundException); |
---|
217 | |
---|
218 | bool lookupValue(const char *name, bool &value) const throw(); |
---|
219 | bool lookupValue(const char *name, int &value) const throw(); |
---|
220 | bool lookupValue(const char *name, unsigned int &value) const throw(); |
---|
221 | bool lookupValue(const char *name, long long &value) const throw(); |
---|
222 | bool lookupValue(const char *name, unsigned long long &value) |
---|
223 | const throw(); |
---|
224 | bool lookupValue(const char *name, double &value) const throw(); |
---|
225 | bool lookupValue(const char *name, float &value) const throw(); |
---|
226 | bool lookupValue(const char *name, const char *&value) const throw(); |
---|
227 | bool lookupValue(const char *name, std::string &value) const throw(); |
---|
228 | |
---|
229 | inline bool lookupValue(const std::string &name, bool &value) |
---|
230 | const throw() |
---|
231 | { return(lookupValue(name.c_str(), value)); } |
---|
232 | |
---|
233 | inline bool lookupValue(const std::string &name, int &value) |
---|
234 | const throw() |
---|
235 | { return(lookupValue(name.c_str(), value)); } |
---|
236 | |
---|
237 | inline bool lookupValue(const std::string &name, unsigned int &value) |
---|
238 | const throw() |
---|
239 | { return(lookupValue(name.c_str(), value)); } |
---|
240 | |
---|
241 | inline bool lookupValue(const std::string &name, long long &value) |
---|
242 | const throw() |
---|
243 | { return(lookupValue(name.c_str(), value)); } |
---|
244 | |
---|
245 | inline bool lookupValue(const std::string &name, |
---|
246 | unsigned long long &value) const throw() |
---|
247 | { return(lookupValue(name.c_str(), value)); } |
---|
248 | |
---|
249 | inline bool lookupValue(const std::string &name, double &value) const |
---|
250 | throw() |
---|
251 | { return(lookupValue(name.c_str(), value)); } |
---|
252 | |
---|
253 | inline bool lookupValue(const std::string &name, float &value) const |
---|
254 | throw() |
---|
255 | { return(lookupValue(name.c_str(), value)); } |
---|
256 | |
---|
257 | inline bool lookupValue(const std::string &name, const char *&value) const |
---|
258 | throw() |
---|
259 | { return(lookupValue(name.c_str(), value)); } |
---|
260 | |
---|
261 | inline bool lookupValue(const std::string &name, std::string &value) const |
---|
262 | throw() |
---|
263 | { return(lookupValue(name.c_str(), value)); } |
---|
264 | |
---|
265 | void remove(const char *name) |
---|
266 | throw(SettingTypeException, SettingNotFoundException); |
---|
267 | |
---|
268 | inline void remove(const std::string & name) |
---|
269 | throw(SettingTypeException, SettingNotFoundException) |
---|
270 | { remove(name.c_str()); } |
---|
271 | |
---|
272 | void remove(unsigned int idx) |
---|
273 | throw(SettingTypeException, SettingNotFoundException); |
---|
274 | |
---|
275 | inline Setting & add(const std::string & name, Type type) |
---|
276 | throw(SettingNameException, SettingTypeException) |
---|
277 | { return(add(name.c_str(), type)); } |
---|
278 | |
---|
279 | Setting & add(const char *name, Type type) |
---|
280 | throw(SettingNameException, SettingTypeException); |
---|
281 | |
---|
282 | Setting & add(Type type) throw(SettingTypeException); |
---|
283 | |
---|
284 | inline bool exists(const std::string & name) const throw() |
---|
285 | { return(exists(name.c_str())); } |
---|
286 | |
---|
287 | bool exists(const char *name) const throw(); |
---|
288 | |
---|
289 | int getLength() const throw(); |
---|
290 | const char *getName() const throw(); |
---|
291 | std::string getPath() const; |
---|
292 | int getIndex() const throw(); |
---|
293 | |
---|
294 | const Setting & getParent() const throw(SettingNotFoundException); |
---|
295 | Setting & getParent() throw(SettingNotFoundException); |
---|
296 | |
---|
297 | bool isRoot() const throw(); |
---|
298 | |
---|
299 | inline bool isGroup() const throw() |
---|
300 | { return(_type == TypeGroup); } |
---|
301 | |
---|
302 | inline bool isArray() const throw() |
---|
303 | { return(_type == TypeArray); } |
---|
304 | |
---|
305 | inline bool isList() const throw() |
---|
306 | { return(_type == TypeList); } |
---|
307 | |
---|
308 | inline bool isAggregate() const throw() |
---|
309 | { return(_type >= TypeGroup); } |
---|
310 | |
---|
311 | inline bool isScalar() const throw() |
---|
312 | { return((_type > TypeNone) && (_type < TypeGroup)); } |
---|
313 | |
---|
314 | inline bool isNumber() const throw() |
---|
315 | { return((_type == TypeInt) || (_type == TypeInt64) |
---|
316 | || (_type == TypeFloat)); } |
---|
317 | |
---|
318 | unsigned int getSourceLine() const throw(); |
---|
319 | const char *getSourceFile() const throw(); |
---|
320 | }; |
---|
321 | |
---|
322 | class Config |
---|
323 | { |
---|
324 | private: |
---|
325 | |
---|
326 | config_t *_config; |
---|
327 | |
---|
328 | static void ConfigDestructor(void *arg); |
---|
329 | Config(const Config& other); // not supported |
---|
330 | Config& operator=(const Config& other); // not supported |
---|
331 | |
---|
332 | public: |
---|
333 | |
---|
334 | Config(); |
---|
335 | virtual ~Config(); |
---|
336 | |
---|
337 | void setAutoConvert(bool flag); |
---|
338 | bool getAutoConvert() const; |
---|
339 | |
---|
340 | void setTabWidth(unsigned short width) throw(); |
---|
341 | unsigned short getTabWidth() const throw(); |
---|
342 | |
---|
343 | void read(FILE *stream) throw(ParseException); |
---|
344 | void write(FILE *stream) const; |
---|
345 | |
---|
346 | void readString(const char *str) throw(ParseException); |
---|
347 | inline void readString(const std::string &str) throw(ParseException) |
---|
348 | { return(readString(str.c_str())); } |
---|
349 | |
---|
350 | void readFile(const char *filename) throw(FileIOException, ParseException); |
---|
351 | void writeFile(const char *filename) throw(FileIOException); |
---|
352 | |
---|
353 | inline Setting & lookup(const std::string &path) const |
---|
354 | throw(SettingNotFoundException) |
---|
355 | { return(lookup(path.c_str())); } |
---|
356 | |
---|
357 | Setting & lookup(const char *path) const throw(SettingNotFoundException); |
---|
358 | |
---|
359 | inline bool exists(const std::string & path) const throw() |
---|
360 | { return(exists(path.c_str())); } |
---|
361 | |
---|
362 | bool exists(const char *path) const throw(); |
---|
363 | |
---|
364 | bool lookupValue(const char *path, bool &value) const throw(); |
---|
365 | bool lookupValue(const char *path, int &value) const throw(); |
---|
366 | bool lookupValue(const char *path, unsigned int &value) const throw(); |
---|
367 | bool lookupValue(const char *path, long long &value) const throw(); |
---|
368 | bool lookupValue(const char *path, unsigned long long &value) |
---|
369 | const throw(); |
---|
370 | bool lookupValue(const char *path, double &value) const throw(); |
---|
371 | bool lookupValue(const char *path, float &value) const throw(); |
---|
372 | bool lookupValue(const char *path, const char *&value) const throw(); |
---|
373 | bool lookupValue(const char *path, std::string &value) const throw(); |
---|
374 | |
---|
375 | inline bool lookupValue(const std::string &path, bool &value) const throw() |
---|
376 | { return(lookupValue(path.c_str(), value)); } |
---|
377 | |
---|
378 | inline bool lookupValue(const std::string &path, int &value) const throw() |
---|
379 | { return(lookupValue(path.c_str(), value)); } |
---|
380 | |
---|
381 | inline bool lookupValue(const std::string &path, unsigned int &value) |
---|
382 | const throw() |
---|
383 | { return(lookupValue(path.c_str(), value)); } |
---|
384 | |
---|
385 | inline bool lookupValue(const std::string &path, long long &value) |
---|
386 | const throw() |
---|
387 | { return(lookupValue(path.c_str(), value)); } |
---|
388 | |
---|
389 | inline bool lookupValue(const std::string &path, |
---|
390 | unsigned long long &value) const throw() |
---|
391 | { return(lookupValue(path.c_str(), value)); } |
---|
392 | |
---|
393 | inline bool lookupValue(const std::string &path, double &value) |
---|
394 | const throw() |
---|
395 | { return(lookupValue(path.c_str(), value)); } |
---|
396 | |
---|
397 | inline bool lookupValue(const std::string &path, float &value) |
---|
398 | const throw() |
---|
399 | { return(lookupValue(path.c_str(), value)); } |
---|
400 | |
---|
401 | inline bool lookupValue(const std::string &path, const char *&value) |
---|
402 | const throw() |
---|
403 | { return(lookupValue(path.c_str(), value)); } |
---|
404 | |
---|
405 | inline bool lookupValue(const std::string &path, std::string &value) |
---|
406 | const throw() |
---|
407 | { return(lookupValue(path.c_str(), value)); } |
---|
408 | |
---|
409 | Setting & getRoot() const; |
---|
410 | |
---|
411 | private: |
---|
412 | |
---|
413 | void handleError() const; |
---|
414 | }; |
---|
415 | |
---|
416 | }; // namespace libconfig |
---|
417 | |
---|
418 | |
---|
419 | #endif // LIBCONFIG_MEX_H |
---|