diff options
author | pommicket <pommicket@gmail.com> | 2025-09-14 12:53:39 -0400 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2025-09-14 12:53:39 -0400 |
commit | ee7e2dbf3d6bc4ef8d4019c666a8960976c1af75 (patch) | |
tree | c5b607087786e028f98e34de001fa9b1e0e08773 /pom.c | |
parent | c8ef3a67210c980d5d1ff2c1bd7d8fffd39846b5 (diff) |
Parse uint
Diffstat (limited to 'pom.c')
-rw-r--r-- | pom.c | 50 |
1 files changed, 50 insertions, 0 deletions
@@ -1535,3 +1535,53 @@ pom_conf_copy(const pom_conf *conf) { } return new_root; } + +static bool +parse_uint(const char *s, uint64_t *val) { + if (*s == '+') s++; + if (*s == 0) { + // empty number + return false; + } + uint64_t value = 0; + if (*s == '0' && (s[1] == 'x' || s[1] == 'X')) { + // hexadecimal + for (size_t i = 2; s[i]; i++) { + if (parse_hex_digit(s[i]) == -1) + return false; + } + value = (uint64_t) strtoull(s, NULL, 16); + } else if (*s == '0') { + if (s[1] != 0) { + // leading zero + return false; + } + } else { + for (size_t i = 0; s[i]; i++) { + if (s[i] < '0' || s[i] > '9') + return false; + } + value = (uint64_t) strtoull(s, NULL, 10); + } + if (value >> 53) { + // too big! + return false; + } + *val = value; + return true; +} + +const char * +pom_conf_get_uint(const pom_conf *conf, const char *key, uint64_t *value_uint) { + 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 = 0; + const char *value_str = pom_conf_get(conf, key); + if (!value_str) { + return ""; + } + if (parse_uint(value_str, value_uint)) + return NULL; + return value_str; +} |