Add handling of ranges in the config file.

This commit is contained in:
Ghislain MARY 2012-10-11 15:35:08 +02:00
parent 6f2d48c04e
commit d5f8a6f933
2 changed files with 39 additions and 0 deletions

View file

@ -271,6 +271,25 @@ 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) {
const char *str = lp_config_get_string(lpconfig, section, key, NULL);
if (str != NULL) {
char *minusptr = strchr(str, '-');
if ((minusptr == NULL) || (minusptr == str)) {
*min = default_min;
*max = default_max;
return FALSE;
}
*min = atoi(str);
*max = atoi(minusptr + 1);
return TRUE;
} else {
*min = default_min;
*max = default_max;
return TRUE;
}
}
int lp_config_get_int(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) {
@ -324,6 +343,12 @@ void lp_config_set_string(LpConfig *lpconfig,const char *section, const char *ke
lpconfig->modified++;
}
void lp_config_set_range(LpConfig *lpconfig, const char *section, const char *key, int min_value, int max_value) {
char tmp[30];
snprintf(tmp, sizeof(tmp), "%i-%i", min_value, max_value);
lp_config_set_string(lpconfig, section, key, tmp);
}
void lp_config_set_int(LpConfig *lpconfig,const char *section, const char *key, int value){
char tmp[30];
snprintf(tmp,sizeof(tmp),"%i",value);

View file

@ -59,6 +59,14 @@ int lp_config_read_file(LpConfig *lpconfig, const char *filename);
**/
const char *lp_config_get_string(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.
*
* @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.
*/
bool_t lp_config_get_range(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.
*
@ -90,6 +98,12 @@ float lp_config_get_float(LpConfig *lpconfig,const char *section, const char *ke
* @ingroup misc
**/
void lp_config_set_string(LpConfig *lpconfig,const char *section, const char *key, const char *value);
/**
* 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);
/**
* Sets an integer config item
*