edelib 2.0.0

edelib/Config.h

00001 /*
00002  * $Id: Config.h 3264 2012-04-25 15:39:56Z karijes $
00003  *
00004  * Config file reader and writer
00005  * Copyright (c) 2005-2007 edelib authors
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library. If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00021 #ifndef __EDELIB_CONFIG_H__
00022 #define __EDELIB_CONFIG_H__
00023 
00024 #include <stdio.h>
00025 #include "List.h"
00026 
00027 EDELIB_NS_BEGIN
00028 
00033 enum ConfigErrors {
00034         CONF_SUCCESS = 0,   
00035         CONF_ERR_FILE,      
00036         CONF_ERR_BAD,       
00037         CONF_ERR_SECTION,   
00038         CONF_ERR_KEY,       
00039         CONF_ERR_MEMORY,    
00040         CONF_ERR_NOVALUE    
00041 };
00042 
00043 class Config;
00044 class ConfigSection;
00045 class ConfigEntry;
00046 
00047 #ifndef SKIP_DOCS
00048 typedef list<ConfigEntry*> EntryList;
00049 typedef list<ConfigEntry*>::iterator EntryListIter;
00050 
00051 typedef list<ConfigSection*> SectionList;
00052 typedef list<ConfigSection*>::iterator SectionListIter;
00053 #endif
00054 
00112 class EDELIB_API Config {
00113 private:
00114         unsigned int errcode;
00115         unsigned int linenum;
00116         unsigned int sectnum;
00117         ConfigSection* cached;
00118 
00119         SectionList section_list;
00120 
00121         ConfigSection* add_section(const char* section);
00122         ConfigSection* find_section(const char* section);
00123 
00124         E_DISABLE_CLASS_COPY(Config)
00125 public:
00127         Config();
00128         
00130         ~Config() { clear(); }
00131 
00138         bool load(const char* fname);
00139 
00148         bool save(const char* fname);
00149 
00163         operator bool(void) const   { return ((errcode == CONF_SUCCESS) ? 1 : 0); }
00164 
00168         void clear(void);
00169 
00179         bool get(const char* section, const char* key, char* ret, unsigned int size);
00180 
00203         bool get_localized(const char* section, const char* key, char* ret, unsigned int size);
00204 
00216         bool get_allocated(const char* section, const char* key, char** ret, unsigned int& retsize);
00217 
00227         bool get(const char* section, const char* key, bool& ret, bool dfl = false);
00228 
00238         bool get(const char* section, const char* key, int& ret, int dfl = 0);
00239 
00249         bool get(const char* section, const char* key, float& ret, float dfl = 0);
00250 
00260         bool get(const char* section, const char* key, long& ret, long dfl = 0);
00261 
00271         bool get(const char* section, const char* key, double& ret, double dfl = 0);
00272 
00282         bool get(const char* section, const char* key, char& ret, char dfl = 0);
00283 
00292         void set(const char* section, const char* key, char* val);
00293 
00302         void set(const char* section, const char* key, const char* val);
00303 
00312         void set_localized(const char* section, const char* key, char* val);
00313 
00322         void set_localized(const char* section, const char* key, const char* val);
00323 
00332         void set(const char* section, const char* key, bool val);
00333 
00342         void set(const char* section, const char* key, int val);
00343 
00352         void set(const char* section, const char* key, long val);
00353 
00362         void set(const char* section, const char* key, float val);
00363 
00372         void set(const char* section, const char* key, double val);
00373 
00379         bool exist(const char* section);
00380 
00386         bool key_exist(const char* section, const char* key);
00387 
00393         unsigned int num_sections(void);
00394 
00401         unsigned int line(void);
00402 
00409         int  error(void);
00410 
00415         const char* strerror(void);
00416 
00422         const char* strerror(int code);
00423 };
00424 
00425 #ifndef SKIP_DOCS
00426 /* This function is for unit test only and should not be used in application code */
00427 EDELIB_API int config_getline(char** buff, int* len, FILE* f);
00428 #endif
00429 
00430 EDELIB_NS_END
00431 #endif