From: Yann Diorcet Date: Wed, 16 Jan 2013 13:35:37 +0000 (+0100) Subject: Improve the lpconfig functions X-Git-Url: http://sjero.net/git/?p=linphone;a=commitdiff_plain;h=4a1e99432616ba380cff895929df41c68fcd3b08 Improve the lpconfig functions --- diff --git a/coreapi/lpconfig.c b/coreapi/lpconfig.c index bd2adfbb..8457cf65 100644 --- a/coreapi/lpconfig.c +++ b/coreapi/lpconfig.c @@ -109,7 +109,7 @@ static bool_t is_first_char(const char *start, const char *pos){ return TRUE; } -LpSection *lp_config_find_section(LpConfig *lpconfig, const char *name){ +LpSection *lp_config_find_section(const LpConfig *lpconfig, const char *name){ LpSection *sec; MSList *elem; /*printf("Looking for section %s\n",name);*/ @@ -123,7 +123,7 @@ LpSection *lp_config_find_section(LpConfig *lpconfig, const char *name){ return NULL; } -LpItem *lp_section_find_item(LpSection *sec, const char *name){ +LpItem *lp_section_find_item(const LpSection *sec, const char *name){ MSList *elem; LpItem *item; /*printf("Looking for item %s\n",name);*/ @@ -260,7 +260,7 @@ void lp_section_remove_item(LpSection *sec, LpItem *item){ lp_item_destroy(item); } -const char *lp_config_get_string(LpConfig *lpconfig, const char *section, const char *key, const char *default_string){ +const char *lp_config_get_string(const LpConfig *lpconfig, const char *section, const char *key, const char *default_string){ LpSection *sec; LpItem *item; sec=lp_config_find_section(lpconfig,section); @@ -271,7 +271,7 @@ const char *lp_config_get_string(LpConfig *lpconfig, const char *section, const return default_string; } -bool_t lp_config_get_range(LpConfig *lpconfig, const char *section, const char *key, int *min, int *max, int default_min, int default_max) { +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) { const char *str = lp_config_get_string(lpconfig, section, key, NULL); if (str != NULL) { char *minusptr = strchr(str, '-'); @@ -290,7 +290,7 @@ bool_t lp_config_get_range(LpConfig *lpconfig, const char *section, const char * } } -int lp_config_get_int(LpConfig *lpconfig,const char *section, const char *key, int default_value){ +int lp_config_get_int(const LpConfig *lpconfig,const char *section, const char *key, int default_value){ const char *str=lp_config_get_string(lpconfig,section,key,NULL); if (str!=NULL) { int ret=0; @@ -302,7 +302,7 @@ int lp_config_get_int(LpConfig *lpconfig,const char *section, const char *key, i else return default_value; } -int64_t lp_config_get_int64(LpConfig *lpconfig,const char *section, const char *key, int64_t default_value){ +int64_t lp_config_get_int64(const LpConfig *lpconfig,const char *section, const char *key, int64_t default_value){ const char *str=lp_config_get_string(lpconfig,section,key,NULL); if (str!=NULL) { #ifdef WIN32 @@ -314,7 +314,7 @@ int64_t lp_config_get_int64(LpConfig *lpconfig,const char *section, const char * else return default_value; } -float lp_config_get_float(LpConfig *lpconfig,const char *section, const char *key, float default_value){ +float lp_config_get_float(const LpConfig *lpconfig,const char *section, const char *key, float default_value){ const char *str=lp_config_get_string(lpconfig,section,key,NULL); float ret=default_value; if (str==NULL) return default_value; @@ -404,11 +404,32 @@ int lp_config_sync(LpConfig *lpconfig){ return 0; } -int lp_config_has_section(LpConfig *lpconfig, const char *section){ +int lp_config_has_section(const LpConfig *lpconfig, const char *section){ if (lp_config_find_section(lpconfig,section)!=NULL) return 1; return 0; } +void lp_config_for_each_section(const LpConfig *lpconfig, void (*callback)(const char *section, void *ctx), void *ctx) { + LpSection *sec; + MSList *elem; + for (elem=lpconfig->sections;elem!=NULL;elem=ms_list_next(elem)){ + sec=(LpSection*)elem->data; + callback(sec->name, ctx); + } +} + +void lp_config_for_each_entry(const LpConfig *lpconfig, const char *section, void (*callback)(const char *entry, void *ctx), void *ctx) { + LpItem *item; + MSList *elem; + LpSection *sec=lp_config_find_section(lpconfig,section); + if (sec!=NULL){ + for (elem=sec->items;elem!=NULL;elem=ms_list_next(elem)){ + item=(LpItem*)elem->data; + callback(item->key, ctx); + } + } +} + void lp_config_clean_section(LpConfig *lpconfig, const char *section){ LpSection *sec=lp_config_find_section(lpconfig,section); if (sec!=NULL){ diff --git a/coreapi/lpconfig.h b/coreapi/lpconfig.h index 8e6f9212..310baaff 100644 --- a/coreapi/lpconfig.h +++ b/coreapi/lpconfig.h @@ -73,7 +73,7 @@ int lp_config_read_file(LpConfig *lpconfig, const char *filename); * @ingroup misc * The default value string is returned if the config item isn't found. **/ -const char *lp_config_get_string(LpConfig *lpconfig, const char *section, const char *key, const char *default_string); +const char *lp_config_get_string(const LpConfig *lpconfig, const char *section, const char *key, const char *default_string); int lp_config_read_file(LpConfig *lpconfig, const char *filename); /** * Retrieves a configuration item as a range, given its section, key, and default min and max values. @@ -82,14 +82,14 @@ int lp_config_read_file(LpConfig *lpconfig, const char *filename); * @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. */ -bool_t lp_config_get_range(LpConfig *lpconfig, const char *section, const char *key, int *min, int *max, int default_min, int default_max); +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); /** * 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. **/ -int lp_config_get_int(LpConfig *lpconfig,const char *section, const char *key, int default_value); +int lp_config_get_int(const LpConfig *lpconfig,const char *section, const char *key, int default_value); /** * Retrieves a configuration item as a 64 bit integer, given its section, key, and default value. @@ -97,7 +97,7 @@ int lp_config_get_int(LpConfig *lpconfig,const char *section, const char *key, i * @ingroup misc * The default integer value is returned if the config item isn't found. **/ -int64_t lp_config_get_int64(LpConfig *lpconfig,const char *section, const char *key, int64_t default_value); +int64_t lp_config_get_int64(const LpConfig *lpconfig,const char *section, const char *key, int64_t default_value); int lp_config_read_file(LpConfig *lpconfig, const char *filename); @@ -107,7 +107,7 @@ int lp_config_read_file(LpConfig *lpconfig, const char *filename); * @ingroup misc * The default float value is returned if the config item isn't found. **/ -float lp_config_get_float(LpConfig *lpconfig,const char *section, const char *key, float default_value); +float lp_config_get_float(const LpConfig *lpconfig,const char *section, const char *key, float default_value); /** * Sets a string config item * @@ -158,13 +158,26 @@ int lp_config_sync(LpConfig *lpconfig); * * @ingroup misc **/ -int lp_config_has_section(LpConfig *lpconfig, const char *section); +int lp_config_has_section(const LpConfig *lpconfig, const char *section); /** * Removes every pair of key,value in a section and remove the section. * * @ingroup misc **/ void lp_config_clean_section(LpConfig *lpconfig, const char *section); +/** + * 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); + /*tells whether uncommited (with lp_config_sync()) modifications exist*/ int lp_config_needs_commit(const LpConfig *lpconfig); void lp_config_destroy(LpConfig *cfg);