summaryrefslogtreecommitdiff
path: root/pom.c
diff options
context:
space:
mode:
Diffstat (limited to 'pom.c')
-rw-r--r--pom.c124
1 files changed, 123 insertions, 1 deletions
diff --git a/pom.c b/pom.c
index ab8e144..fbbe6e0 100644
--- a/pom.c
+++ b/pom.c
@@ -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;
+}