lpconfig.h 6.91 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/***************************************************************************
 *            lpconfig.h
 *
 *  Thu Mar 10 15:02:49 2005
 *  Copyright  2005  Simon Morlat
 *  Email simon.morlat@linphone.org
 ****************************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 
#ifndef LPCONFIG_H
#define LPCONFIG_H

Yann Diorcet's avatar
Yann Diorcet committed
28 29
#include <ortp/port.h>

smorlat's avatar
smorlat committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/**
 * The LpConfig object is used to manipulate a configuration file.
 * 
 * @ingroup misc
 * The format of the configuration file is a .ini like format:
 * - sections are defined in []
 * - each section contains a sequence of key=value pairs.
 *
 * Example:
 * @code
 * [sound]
 * echocanceler=1
 * playback_dev=ALSA: Default device
 *
 * [video]
 * enabled=1
 * @endcode
**/
aymeric's avatar
aymeric committed
48 49 50 51 52 53
typedef struct _LpConfig LpConfig;

#ifdef __cplusplus
extern "C" {
#endif

54 55 56 57 58 59 60 61 62 63 64 65 66 67

#define LP_CONFIG_DEFAULT_STRING(config, name, default) \
	(config) ? (lp_config_get_string(config, "default_values", name, default)) : (default)

#define LP_CONFIG_DEFAULT_INT(config, name, default) \
	(config) ? (lp_config_get_int(config, "default_values", name, default)) : (default)

#define LP_CONFIG_DEFAULT_INT64(config, name, default) \
	(config) ? (lp_config_get_int64(config, "default_values", name, default)) : (default)

#define LP_CONFIG_DEFAULT_FLOAT(config, name, default) \
	(config) ? (lp_config_get_float(config, "default_values", name, default)) : (default)


68 69 70 71 72 73 74
/**
 * Instantiates a LpConfig object from a user config file.
 *
 * @ingroup misc
 * @param filename the filename of the config file to read to fill the instantiated LpConfig
 * @see lp_config_new_with_factory
 */
aymeric's avatar
aymeric committed
75
LpConfig * lp_config_new(const char *filename);
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

/**
 * Instantiates a LpConfig object from a user config file and a factory config file.
 *
 * @ingroup misc
 * @param config_filename the filename of the user config file to read to fill the instantiated LpConfig
 * @param factory_config_filename the filename of the factory config file to read to fill the instantiated LpConfig
 * @see lp_config_new
 *
 * The user config file is read first to fill the LpConfig and then the factory config file is read.
 * Therefore the configuration parameters defined in the user config file will be overwritten by the parameters
 * defined in the factory config file.
 */
LpConfig * lp_config_new_with_factory(const char *config_filename, const char *factory_config_filename);

91
int lp_config_read_file(LpConfig *lpconfig, const char *filename);
smorlat's avatar
smorlat committed
92 93 94 95 96 97
/**
 * Retrieves a configuration item as a string, given its section, key, and default value.
 * 
 * @ingroup misc
 * The default value string is returned if the config item isn't found.
**/
98
const char *lp_config_get_string(const LpConfig *lpconfig, const char *section, const char *key, const char *default_string);
smorlat's avatar
smorlat committed
99
int lp_config_read_file(LpConfig *lpconfig, const char *filename);
100 101 102 103 104 105 106
/**
 * Retrieves a configuration item as a range, given its section, key, and default min and max values.
 *
 * @ingroup misc
 * @return TRUE if the value is successfully parsed as a range, FALSE otherwise.
 * If FALSE is returned, min and max are filled respectively with default_min and default_max values.
 */
107
bool_t lp_config_get_range(const LpConfig *lpconfig, const char *section, const char *key, int *min, int *max, int default_min, int default_max);
smorlat's avatar
smorlat committed
108 109 110 111 112 113
/**
 * Retrieves a configuration item as an integer, given its section, key, and default value.
 * 
 * @ingroup misc
 * The default integer value is returned if the config item isn't found.
**/
114
int lp_config_get_int(const LpConfig *lpconfig,const char *section, const char *key, int default_value);
115 116 117 118 119 120 121

/**
 * Retrieves a configuration item as a 64 bit integer, given its section, key, and default value.
 * 
 * @ingroup misc
 * The default integer value is returned if the config item isn't found.
**/
122
int64_t lp_config_get_int64(const LpConfig *lpconfig,const char *section, const char *key, int64_t default_value);
123 124


smorlat's avatar
smorlat committed
125 126 127 128 129 130 131
int lp_config_read_file(LpConfig *lpconfig, const char *filename);
/**
 * Retrieves a configuration item as a float, given its section, key, and default value.
 * 
 * @ingroup misc
 * The default float value is returned if the config item isn't found.
**/
132
float lp_config_get_float(const LpConfig *lpconfig,const char *section, const char *key, float default_value);
smorlat's avatar
smorlat committed
133 134 135 136 137
/**
 * Sets a string config item 
 *
 * @ingroup misc
**/
aymeric's avatar
aymeric committed
138
void lp_config_set_string(LpConfig *lpconfig,const char *section, const char *key, const char *value);
139 140 141 142 143 144
/**
 * Sets a range config item
 *
 * @ingroup misc
 */
void lp_config_set_range(LpConfig *lpconfig, const char *section, const char *key, int min_value, int max_value);
smorlat's avatar
smorlat committed
145 146 147 148 149
/**
 * Sets an integer config item
 *
 * @ingroup misc
**/
aymeric's avatar
aymeric committed
150
void lp_config_set_int(LpConfig *lpconfig,const char *section, const char *key, int value);
151 152 153 154 155 156 157 158

/**
 * Sets an integer config item, but store it as hexadecimal
 *
 * @ingroup misc
**/
void lp_config_set_int_hex(LpConfig *lpconfig,const char *section, const char *key, int value);

159 160 161 162 163 164 165
/**
 * Sets a 64 bits integer config item
 *
 * @ingroup misc
**/
void lp_config_set_int64(LpConfig *lpconfig,const char *section, const char *key, int64_t value);

166 167 168 169 170 171
/**
 * Sets a float config item
 *
 * @ingroup misc
**/
void lp_config_set_float(LpConfig *lpconfig,const char *section, const char *key, float value);	
smorlat's avatar
smorlat committed
172 173 174 175 176
/**
 * Writes the config file to disk.
 * 
 * @ingroup misc
**/
aymeric's avatar
aymeric committed
177
int lp_config_sync(LpConfig *lpconfig);
smorlat's avatar
smorlat committed
178 179 180 181 182
/**
 * Returns 1 if a given section is present in the configuration.
 *
 * @ingroup misc
**/
183
int lp_config_has_section(const LpConfig *lpconfig, const char *section);
smorlat's avatar
smorlat committed
184 185 186 187 188
/**
 * Removes every pair of key,value in a section and remove the section.
 *
 * @ingroup misc
**/
aymeric's avatar
aymeric committed
189
void lp_config_clean_section(LpConfig *lpconfig, const char *section);
190 191 192 193 194 195 196 197 198 199 200 201 202
/**
 * Call a function for each section present in the configuration.
 *
 * @ingroup misc
**/
void lp_config_for_each_section(const LpConfig *lpconfig, void (*callback)(const char *section, void *ctx), void *ctx);
/**
 * Call a function for each entry present in a section configuration.
 *
 * @ingroup misc
**/
void lp_config_for_each_entry(const LpConfig *lpconfig, const char *section, void (*callback)(const char *entry, void *ctx), void *ctx);

203 204
/*tells whether uncommited (with lp_config_sync()) modifications exist*/
int lp_config_needs_commit(const LpConfig *lpconfig);
aymeric's avatar
aymeric committed
205 206 207 208 209 210 211
void lp_config_destroy(LpConfig *cfg);
	
#ifdef __cplusplus
}
#endif

#endif