From c8ef3a67210c980d5d1ff2c1bd7d8fffd39846b5 Mon Sep 17 00:00:00 2001 From: pommicket Date: Sun, 14 Sep 2025 11:49:47 -0400 Subject: New typed-get API, again --- pom.c | 3 ++- pom.h | 73 +++++++++++++++++++++++++++++++++++++------------------------------ 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/pom.c b/pom.c index 906e970..ad3919c 100644 --- a/pom.c +++ b/pom.c @@ -234,9 +234,10 @@ make_error(const char *file, uint64_t line, const char *fmt, ...) { err->line = line; err->message = message; char *string = strchr(message, '\0') + 1; - // no, clang, string will not overlap with message. + // no, GCC, string will not overlap with message. #if __GNUC__ >= 4 #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wunknown-warning-option" #pragma GCC diagnostic ignored "-Wrestrict" #endif sprintf(string, "Error:\n%s:%" PRIu64 ": %s\n", file, line, message); diff --git a/pom.h b/pom.h index 9307508..1139ee8 100644 --- a/pom.h +++ b/pom.h @@ -239,82 +239,89 @@ pom_conf_get_or_default(const pom_conf *conf, const char *key, const char *dflt) /// Get signed integer value of `key`. /// -/// Returns true on success, putting the integer value in `*value`. +/// Returns `NULL` on success, putting the integer value in `*value`. /// -/// If `key` is not set or `key` is set but not a valid integer, -/// returns false. -/// `*value` is set to 0 in this case. -bool +/// If `key` is not set, returns `""`. +/// If `key` is set but not a valid integer +/// (decimal or `0x`/`0X`-prefixed hexadecimal, absolute value less than 253), +/// returns the value of `key`. +/// `*value` is set to 0 in these cases. +const char * pom_conf_get_int(const pom_conf *conf, const char *key, int64_t *value); /// Get signed integer value of `key`, or `dflt` if not present. /// -/// Returns true on success, putting the integer value in `*value`. +/// Returns `NULL` on success, putting the integer value in `*value`. /// -/// If `key` is set but not a valid integer, -/// returns false. +/// If `key` is set but not a valid integer +/// (decimal or `0x`/`0X`-prefixed hexadecimal, absolute value less than 253), +/// returns the value of `key`. /// `*value` is still set to `dflt` in this case. -bool +const char * pom_conf_get_int_or_default(const pom_conf *conf, const char *key, int64_t *value, int64_t dflt); /// Get unsigned integer value of `key`. /// -/// Returns true on success, putting the unsigned integer value in `*value`. +/// Returns `NULL` on success, putting the unsigned integer value in `*value`. /// -/// If `key` is not set or `key` is set but not a valid unsigned integer, -/// returns false. -/// `*value` is set to 0 in this case. -bool +/// If `key` is not set, returns `""`. If `key` is set but not a valid unsigned integer +/// (decimal or `0x`/`0X`-prefixed hexadecimal, less than 253), +/// returns the value of `key`. +/// `*value` is set to 0 in these case cases. +const char * pom_conf_get_uint(const pom_conf *conf, const char *key, uint64_t *value); /// Get unsigned integer value of `key`, or `dflt` if not present. /// -/// Returns true on success, putting the unsigned integer value in `*value`. +/// Returns `NULL` on success, putting the unsigned integer value in `*value`. /// -/// If `key` is set but not a valid unsigned integer, -/// returns false. +/// If `key` is set but not a valid unsigned integer +/// (decimal or `0x`/`0X`-prefixed hexadecimal, less than 253), +/// returns the value of `key`. /// `*value` is set to 0 in this case. -bool +const char * pom_conf_get_uint_or_default(const pom_conf *conf, const char *key, uint64_t *value, uint64_t dflt); /// Get floating-point value of `key`. /// -/// Returns true on success, putting the floating-point value in `*value`. +/// Returns `NULL` on success, putting the floating-point value in `*value`. /// -/// If `key` is not set or `key` is set but not a valid floating-point number, -/// returns false. +/// If `key` is not set, returns `""`. If `key` is set but not a valid floating-point number, +/// returns the value of `key`. /// `*value` is set to 0.0 in this case. -bool +const char * pom_conf_get_float(const pom_conf *conf, const char *key, double *value); /// Get floating-point value of `key`, or `dflt` if not present. /// -/// Returns true on success, putting the floating-point value in `*value`. +/// Returns `NULL` on success, putting the floating-point value in `*value`. /// /// If `key` is set but not a valid floating-point number, -/// returns false. +/// returns the value of `key`. /// `*value` is still set to `dflt` in this case. -bool +const char * pom_conf_get_float_or_default(const pom_conf *conf, const char *key, double *value, double dflt); /// Get boolean value of `key`. /// -/// Returns true on success], putting the boolean value in `*value`. +/// Returns `NULL` on success, putting the boolean value in `*value`. /// -/// If `key` is not set or `key` is set but not a valid boolean, -/// returns false. +/// If `key` is not set, returns `""`. If `key` is set but not a valid boolean +/// (`off`/`false`/`no`/`on`/`true`/`yes`), +/// returns the value of `key`. /// `*value` is set to `false` in this case. -bool +const char * pom_conf_get_bool(const pom_conf *conf, const char *key, bool *value); /// Get boolean value of `key`, or `dflt` if not present. /// -/// Returns true on success, putting the boolean value in `*value`. +/// Returns `NULL` on success, putting the boolean value in `*value`. /// -/// If `key` is set but not a valid boolean, -/// returns false. +/// If `key` is set but not a valid boolean +/// (`off`/`false`/`no`/`on`/`true`/`yes`), +/// returns the value of `key`. /// `*value` is still set to `dflt` in this case. -bool +const char * pom_conf_get_bool_or_default(const pom_conf *conf, const char *key, bool *value, bool dflt); /// Get comma-separated list value of `key`, or `NULL` if not present. The return value must be freed with `free`. -- cgit v1.2.3