Mercurial > code
changeset 82:c8bbdbedcf07
Added a little convenient API for ini
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 16 Nov 2011 21:28:35 +0100 |
parents | f42bcb9e7b4a |
children | f028a84bc3f1 |
files | ini.c ini.h |
diffstat | 2 files changed, 130 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/ini.c Wed Nov 16 20:41:36 2011 +0100 +++ b/ini.c Wed Nov 16 21:28:35 2011 +0100 @@ -277,6 +277,79 @@ } /* + * These section provide very small API to convert value + * and store them to the dst pointer. Because you can also + * use your own handlers, the user data here can't be used. + */ + +void +ini_value_dispatch(struct ini_config *config, struct ini_handler *hdrs, int length) +{ + const char *sectionName; + int i; + char *value; + struct ini_section *section; + + sectionName = NULL; + for (i = 0; i < length; ++i) { + /* Do not select the same section for performance. */ + if (sectionName == NULL || !strcmp(hdrs[i].section, sectionName)) { + sectionName = hdrs[i].section; + section = ini_select_section(config, sectionName); + } + + /* Skip the section if does not exists in the config */ + if (section == NULL) + continue; + + if ((value = ini_get_option(section, hdrs[i].option)) != NULL) + hdrs[i].handler(hdrs[i].dst, value, hdrs[i].userdata); + } +} + +void +ini_convert_bool(void *dst, const char *value, void *dummy) +{ + int *p = dst; + + if (strcmp(value, "yes") == 0 || + strcmp(value, "true") == 0 || + strcmp(value, "1") == 0) + *p = 1; + else + *p = 0; + (void)dummy; +} + + +void +ini_convert_int(void *dst, const char *value, void *dummy) +{ + int *p = dst; + + *p = (int) strtol(value, NULL, 10); + (void)dummy; +} + +void +ini_convert_short(void *dst, const char *value, void *dummy) +{ + short *p = dst; + + *p = (short) strtol(value, NULL, 10); + (void)dummy; +} + +void +ini_convert_string(void *dst, const char *value, void *dummy) +{ + char **p = dst; + + *p = strdup(value); + (void)dummy; +} + +/* * Return the last error or "No error" if there is not error at all. */ @@ -316,9 +389,9 @@ free(conf); } -/* -------------------------------------------------------- */ -/* private functions */ -/* -------------------------------------------------------- */ +/* -------------------------------------------------------- + * private functions + * -------------------------------------------------------- */ /* * Read file line per line and try to parse ini like file. Lines
--- a/ini.h Wed Nov 16 20:41:36 2011 +0100 +++ b/ini.h Wed Nov 16 21:28:35 2011 +0100 @@ -26,6 +26,16 @@ struct ini_config; struct ini_section; +struct ini_handler { + const char *section; /* section to query */ + const char *option; /* option to check */ + void *dst; /* where to store */ + + /* Conversion function */ + void (*handler)(void *, const char *, void *); + void *userdata; /* optional user data */ +}; + /* -------------------------------------------------------- * struct ini_config functions. * -------------------------------------------------------- */ @@ -61,4 +71,48 @@ char * ini_get_option(const struct ini_section *, const char *key); +/* -------------------------------------------------------- + * convenient api to query and convert data + * -------------------------------------------------------- */ + +/* + * For the config, read all available value and store them in + * the array ini_handler. + */ + +void +ini_value_dispatch(struct ini_config *, struct ini_handler *, int); + +/* + * Convert to bool. dst must be (char *). + * It converts "yes" "true" "1" or opposites. Only lower + * case is supported right now. + */ + +void +ini_convert_bool(void *, const char *, void *); + +/* + * Convert to a int. dst must be (int *). + */ + +void +ini_convert_int(void *, const char *, void *); + +/* + * Convert to a short. dst must be (short *). + */ + +void +ini_convert_short(void *, const char *, void *); + +/* + * Convert to a char *. dst must be (char **). This + * function use strdup() you need to free the dst + * pointer. + */ + +void +ini_convert_string(void *, const char *, void *); + #endif /* _INI_H_ */