Config.h
/*
* $Id: Config.h 3441 2012-11-01 20:40:30Z karijes $
*
* Config file reader and writer
* Copyright (c) 2005-2007 edelib authors
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __EDELIB_CONFIG_H__
#define __EDELIB_CONFIG_H__
#include <stdio.h>
#include "List.h"
EDELIB_NS_BEGIN
enum ConfigErrors {
CONF_SUCCESS = 0,
CONF_ERR_FILE,
CONF_ERR_BAD,
CONF_ERR_SECTION,
CONF_ERR_KEY,
CONF_ERR_MEMORY,
CONF_ERR_NOVALUE
};
class Config;
class ConfigSection;
struct ConfigEntry;
#ifndef SKIP_DOCS
typedef list<ConfigEntry*> EntryList;
typedef list<ConfigEntry*>::iterator EntryListIter;
typedef list<ConfigSection*> SectionList;
typedef list<ConfigSection*>::iterator SectionListIter;
#endif
class EDELIB_API Config {
private:
unsigned int errcode;
unsigned int linenum;
unsigned int sectnum;
ConfigSection* cached;
SectionList section_list;
ConfigSection* add_section(const char* section);
ConfigSection* find_section(const char* section);
E_DISABLE_CLASS_COPY(Config)
public:
Config();
~Config() { clear(); }
bool load(const char* fname);
bool save(const char* fname);
operator bool(void) const { return ((errcode == CONF_SUCCESS) ? 1 : 0); }
void clear(void);
bool get(const char* section, const char* key, char* ret, unsigned int size);
bool get_localized(const char* section, const char* key, char* ret, unsigned int size);
bool get_allocated(const char* section, const char* key, char** ret, unsigned int& retsize);
bool get(const char* section, const char* key, bool& ret, bool dfl = false);
bool get(const char* section, const char* key, int& ret, int dfl = 0);
bool get(const char* section, const char* key, float& ret, float dfl = 0);
bool get(const char* section, const char* key, long& ret, long dfl = 0);
bool get(const char* section, const char* key, double& ret, double dfl = 0);
bool get(const char* section, const char* key, char& ret, char dfl = 0);
void set(const char* section, const char* key, char* val);
void set(const char* section, const char* key, const char* val);
void set_localized(const char* section, const char* key, char* val);
void set_localized(const char* section, const char* key, const char* val);
void set(const char* section, const char* key, bool val);
void set(const char* section, const char* key, int val);
void set(const char* section, const char* key, long val);
void set(const char* section, const char* key, float val);
void set(const char* section, const char* key, double val);
bool exist(const char* section);
bool key_exist(const char* section, const char* key);
unsigned int num_sections(void);
unsigned int line(void);
int error(void);
const char* strerror(void);
const char* strerror(int code);
};
#ifndef SKIP_DOCS
/* This function is for unit test only and should not be used in application code */
EDELIB_API int config_getline(char** buff, int* len, FILE* f);
#endif
EDELIB_NS_END
#endif
edelib::CONF_ERR_SECTION
requested section was not found
Definition: Config.h:37
edelib::Config
A config file reader.
Definition: Config.h:112
edelib::list
Linked list class.
Definition: List.h:160
edelib::CONF_ERR_BAD
malformed file
Definition: Config.h:36
edelib::CONF_ERR_FILE
trouble accessing config file or directory
Definition: Config.h:35
edelib::CONF_ERR_MEMORY
memory allocation error
Definition: Config.h:39
edelib::CONF_ERR_NOVALUE
key found, but invalid value associated with it
Definition: Config.h:40
E_DISABLE_CLASS_COPY
#define E_DISABLE_CLASS_COPY(klass)
Definition: edelib-global.h:161
edelib::ConfigErrors
ConfigErrors
Error codes from Config class.
Definition: Config.h:33
edelib::Config::~Config
~Config()
Definition: Config.h:130
edelib::CONF_SUCCESS
successful operation
Definition: Config.h:34
edelib::CONF_ERR_KEY
requested key was not found
Definition: Config.h:38