diff --git a/spa/include/spa/utils/json.h b/spa/include/spa/utils/json.h index cfa7fc15f..8303d4a3e 100644 --- a/spa/include/spa/utils/json.h +++ b/spa/include/spa/utils/json.h @@ -34,9 +34,11 @@ extern "C" { #include #include #include -#include +#include +#include #include +#include /** \defgroup spa_json JSON * Relaxed JSON variant parsing @@ -238,13 +240,7 @@ static inline bool spa_json_is_null(const char *val, int len) static inline int spa_json_parse_float(const char *val, int len, float *result) { char *end; - static locale_t locale = NULL; - if (SPA_UNLIKELY(locale == NULL)) - locale = newlocale(LC_ALL_MASK, "C", NULL); - if (locale != NULL) - *result = strtof_l(val, &end, locale); - else - *result = strtof(val, &end); + *result = spa_strtof(val, &end); return len > 0 && end == val + len; } diff --git a/spa/include/spa/utils/string.h b/spa/include/spa/utils/string.h index 2260bb1cb..d8d68657a 100644 --- a/spa/include/spa/utils/string.h +++ b/spa/include/spa/utils/string.h @@ -32,6 +32,8 @@ extern "C" { #include #include #include +#include +#include #include @@ -263,6 +265,27 @@ static inline int spa_scnprintf(char *buffer, size_t size, const char *format, . return r; } +/** + * Convert \a str to a float in the C locale. + * + * If \a endptr is not NULL, a pointer to the character after the last character + * used in the conversion is stored in the location referenced by endptr. + * + * \return the result float. + */ +static inline float spa_strtof(const char *str, char **endptr) +{ + static locale_t locale = NULL; + float v; + if (SPA_UNLIKELY(locale == NULL)) + locale = newlocale(LC_ALL_MASK, "C", NULL); + if (locale != NULL) + v = strtof_l(str, endptr, locale); + else + v = strtof(str, endptr); + return v; +} + /** * Convert \a str to a float and store the result in \a val. * @@ -277,9 +300,8 @@ static inline bool spa_atof(const char *str, float *val) if (!str || *str =='\0') return false; - errno = 0; - v = strtof(str, &endptr); + v = spa_strtof(str, &endptr); if (errno != 0 || *endptr != '\0') return false; @@ -287,6 +309,27 @@ static inline bool spa_atof(const char *str, float *val) return true; } +/** + * Convert \a str to a double in the C locale. + * + * If \a endptr is not NULL, a pointer to the character after the last character + * used in the conversion is stored in the location referenced by endptr. + * + * \return the result float. + */ +static inline double spa_strtod(const char *str, char **endptr) +{ + static locale_t locale = NULL; + double v; + if (SPA_UNLIKELY(locale == NULL)) + locale = newlocale(LC_ALL_MASK, "C", NULL); + if (locale != NULL) + v = strtod_l(str, endptr, locale); + else + v = strtod(str, endptr); + return v; +} + /** * Convert \a str to a double and store the result in \a val. * @@ -303,7 +346,7 @@ static inline bool spa_atod(const char *str, double *val) return false; errno = 0; - v = strtod(str, &endptr); + v = spa_strtod(str, &endptr); if (errno != 0 || *endptr != '\0') return false;