edelib 1.0

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List

Config.h
1 /*
2 * $Id: Config.h 3441 2012-11-01 20:40:30Z karijes $
3 *
4 * Config file reader and writer
5 * Copyright (c) 2005-2007 edelib authors
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #ifndef __EDELIB_CONFIG_H__
22 #define __EDELIB_CONFIG_H__
23
24 #include <stdio.h>
25 #include "List.h"
26
27 EDELIB_NS_BEGIN
28
33 enum ConfigErrors {
34 CONF_SUCCESS = 0,
35 CONF_ERR_FILE,
36 CONF_ERR_BAD,
37 CONF_ERR_SECTION,
38 CONF_ERR_KEY,
39 CONF_ERR_MEMORY,
40 CONF_ERR_NOVALUE
41 };
42
43 class Config;
44 class ConfigSection;
45 struct ConfigEntry;
46
47 #ifndef SKIP_DOCS
48 typedef list<ConfigEntry*> EntryList;
49 typedef list<ConfigEntry*>::iterator EntryListIter;
50
51 typedef list<ConfigSection*> SectionList;
52 typedef list<ConfigSection*>::iterator SectionListIter;
53 #endif
54
112 class EDELIB_API Config {
113 private:
114 unsigned int errcode;
115 unsigned int linenum;
116 unsigned int sectnum;
117 ConfigSection* cached;
118
119 SectionList section_list;
120
121 ConfigSection* add_section(const char* section);
122 ConfigSection* find_section(const char* section);
123
124 E_DISABLE_CLASS_COPY(Config)
125 public:
127 Config();
128
130 ~Config() { clear(); }
131
138 bool load(const char* fname);
139
148 bool save(const char* fname);
149
163 operator bool(void) const { return ((errcode == CONF_SUCCESS) ? 1 : 0); }
164
168 void clear(void);
169
179 bool get(const char* section, const char* key, char* ret, unsigned int size);
180
203 bool get_localized(const char* section, const char* key, char* ret, unsigned int size);
204
216 bool get_allocated(const char* section, const char* key, char** ret, unsigned int& retsize);
217
227 bool get(const char* section, const char* key, bool& ret, bool dfl = false);
228
238 bool get(const char* section, const char* key, int& ret, int dfl = 0);
239
249 bool get(const char* section, const char* key, float& ret, float dfl = 0);
250
260 bool get(const char* section, const char* key, long& ret, long dfl = 0);
261
271 bool get(const char* section, const char* key, double& ret, double dfl = 0);
272
282 bool get(const char* section, const char* key, char& ret, char dfl = 0);
283
292 void set(const char* section, const char* key, char* val);
293
302 void set(const char* section, const char* key, const char* val);
303
312 void set_localized(const char* section, const char* key, char* val);
313
322 void set_localized(const char* section, const char* key, const char* val);
323
332 void set(const char* section, const char* key, bool val);
333
342 void set(const char* section, const char* key, int val);
343
352 void set(const char* section, const char* key, long val);
353
362 void set(const char* section, const char* key, float val);
363
372 void set(const char* section, const char* key, double val);
373
379 bool exist(const char* section);
380
386 bool key_exist(const char* section, const char* key);
387
393 unsigned int num_sections(void);
394
401 unsigned int line(void);
402
409 int error(void);
410
415 const char* strerror(void);
416
422 const char* strerror(int code);
423 };
424
425 #ifndef SKIP_DOCS
426 /* This function is for unit test only and should not be used in application code */
427 EDELIB_API int config_getline(char** buff, int* len, FILE* f);
428 #endif
429
430 EDELIB_NS_END
431 #endif