From 8b97416417c2994b787d81960ae185c7aa7cd39b Mon Sep 17 00:00:00 2001 From: Arnaud Ferraris Date: Tue, 2 Mar 2021 18:06:55 +0100 Subject: [PATCH] json: add integer parsing functions While most numeric values used in pipewire are floating-point values, it can still be useful to be able to directly parse integer values. --- spa/include/spa/utils/json.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/spa/include/spa/utils/json.h b/spa/include/spa/utils/json.h index de2cce6fa..5a5601e05 100644 --- a/spa/include/spa/utils/json.h +++ b/spa/include/spa/utils/json.h @@ -244,6 +244,27 @@ static inline int spa_json_get_float(struct spa_json *iter, float *res) return spa_json_parse_float(value, len, res); } +/* int */ +static inline int spa_json_parse_int(const char *val, int len, int *result) +{ + char *end; + *result = strtol(val, &end, 0); + return end == val + len; +} +static inline bool spa_json_is_int(const char *val, int len) +{ + int dummy; + return spa_json_parse_int(val, len, &dummy); +} +static inline int spa_json_get_int(struct spa_json *iter, int *res) +{ + const char *value; + int len; + if ((len = spa_json_next(iter, &value)) <= 0) + return -1; + return spa_json_parse_int(value, len, res); +} + /* bool */ static inline bool spa_json_is_true(const char *val, int len) {