diff options
-rw-r--r-- | examples/conf.pom | 2 | ||||
-rw-r--r-- | examples/read_conf.c | 6 | ||||
-rw-r--r-- | pom.c | 124 |
3 files changed, 127 insertions, 5 deletions
diff --git a/examples/conf.pom b/examples/conf.pom index 67890e5..4f4aa8c 100644 --- a/examples/conf.pom +++ b/examples/conf.pom @@ -1,2 +1,2 @@ [number] -best j = "-1234567891234567" +best = "2" diff --git a/examples/read_conf.c b/examples/read_conf.c index 1bff840..24fade0 100644 --- a/examples/read_conf.c +++ b/examples/read_conf.c @@ -13,9 +13,9 @@ int main(int argc, char **argv) { free(error); return EXIT_FAILURE; } - int64_t value; - const char *s = pom_conf_get_int(conf, "number.best", &value); - printf("%" PRId64 "\n", value); + double value; + const char *s = pom_conf_get_float_or_default(conf, "number.best", &value, 3.6); + printf("%f\n", value); if (s) printf(" -> %s\n",s); pom_conf_print(conf); pom_conf_free(conf); @@ -1,6 +1,6 @@ /* TODO: -- typed get functions +- get_list */ #include "pom.h" @@ -1639,6 +1639,52 @@ parse_int(const char *s, int64_t *val) { return true; } +static bool +parse_double(const char *s, double *val) { + bool can_have_dot = true; + for (const char *p = s; *p; p++) { + char c = *p; + if (c == '-' || c == '+') { + if (p > s && p[-1] != 'e' && p[-1] != 'E') + return false; + } else if (c == '.') { + // ensure digits before and after . + if (!can_have_dot) + return false; + if (p == s + || p[-1] < '0' || p[-1] > '9' + || p[1] < '0' || p[1] > '9') + return false; + can_have_dot = false; + } else if (c == 'e' || c == 'E') { + if (p == s || p[-1] < '0' || p[-1] > '9') + return false; + can_have_dot = false; + } else if (c < '0' || c > '9') { + return false; + } + } + *val = atof(s); + return true; +} + +static bool +parse_bool(const char *s, bool *val) { + if (strcmp(s, "off") == 0 || + strcmp(s, "false") == 0 || + strcmp(s, "no") == 0) { + *val = false; + return true; + } + if (strcmp(s, "on") == 0 || + strcmp(s, "true") == 0 || + strcmp(s, "yes") == 0) { + *val = true; + return true; + } + return false; +} + const char * pom_conf_get_uint(const pom_conf *conf, const char *key, uint64_t *value_uint) { check_conf(conf); @@ -1654,6 +1700,18 @@ pom_conf_get_uint(const pom_conf *conf, const char *key, uint64_t *value_uint) { } const char * +pom_conf_get_uint_or_default(const pom_conf *conf, const char *key, uint64_t *value_uint, uint64_t dflt) { + check_conf(conf); + if (!key) fatal_error("NULL key passed to %s", __func__); + if (!value_uint) fatal_error("NULL value passed to %s", __func__); + *value_uint = dflt; + const char *value_str = pom_conf_get(conf, key); + if (!value_str || parse_uint(value_str, value_uint)) + return NULL; + return value_str; +} + +const char * pom_conf_get_int(const pom_conf *conf, const char *key, int64_t *value_int) { check_conf(conf); if (!key) fatal_error("NULL key passed to %s", __func__); @@ -1666,3 +1724,67 @@ pom_conf_get_int(const pom_conf *conf, const char *key, int64_t *value_int) { return NULL; return value_str; } + +const char * +pom_conf_get_int_or_default(const pom_conf *conf, const char *key, int64_t *value_int, int64_t dflt) { + check_conf(conf); + if (!key) fatal_error("NULL key passed to %s", __func__); + if (!value_int) fatal_error("NULL value passed to %s", __func__); + *value_int = dflt; + const char *value_str = pom_conf_get(conf, key); + if (!value_str || parse_int(value_str, value_int)) + return NULL; + return value_str; +} + +const char * +pom_conf_get_float(const pom_conf *conf, const char *key, double *value_double) { + check_conf(conf); + if (!key) fatal_error("NULL key passed to %s", __func__); + if (!value_double) fatal_error("NULL value passed to %s", __func__); + *value_double = 0; + const char *value_str = pom_conf_get(conf, key); + if (!value_str) + return ""; + if (parse_double(value_str, value_double)) + return NULL; + return value_str; +} + +const char * +pom_conf_get_float_or_default(const pom_conf *conf, const char *key, double *value_double, double dflt) { + check_conf(conf); + if (!key) fatal_error("NULL key passed to %s", __func__); + if (!value_double) fatal_error("NULL value passed to %s", __func__); + *value_double = dflt; + const char *value_str = pom_conf_get(conf, key); + if (!value_str || parse_double(value_str, value_double)) + return NULL; + return value_str; +} + +const char * +pom_conf_get_bool(const pom_conf *conf, const char *key, bool *value_bool) { + check_conf(conf); + if (!key) fatal_error("NULL key passed to %s", __func__); + if (!value_bool) fatal_error("NULL value passed to %s", __func__); + *value_bool = false; + const char *value_str = pom_conf_get(conf, key); + if (!value_str) + return ""; + if (parse_bool(value_str, value_bool)) + return NULL; + return value_str; +} + +const char * +pom_conf_get_bool_or_default(const pom_conf *conf, const char *key, bool *value_bool, bool dflt) { + check_conf(conf); + if (!key) fatal_error("NULL key passed to %s", __func__); + if (!value_bool) fatal_error("NULL value passed to %s", __func__); + *value_bool = dflt; + const char *value_str = pom_conf_get(conf, key); + if (!value_str || parse_bool(value_str, value_bool)) + return NULL; + return value_str; +} |