Work like qemu_strtod() and qemu_strtold_finite, except store long double. Signed-off-by: Tao Xu <tao3...@intel.com> ---
No changes in v17. --- include/qemu/cutils.h | 3 +++ util/cutils.c | 48 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h index b54c847e0f..48cf9bf776 100644 --- a/include/qemu/cutils.h +++ b/include/qemu/cutils.h @@ -146,6 +146,9 @@ int qemu_strtou64(const char *nptr, const char **endptr, int base, uint64_t *result); int qemu_strtod(const char *nptr, const char **endptr, double *result); int qemu_strtod_finite(const char *nptr, const char **endptr, double *result); +int qemu_strtold(const char *nptr, const char **endptr, long double *result); +int qemu_strtold_finite(const char *nptr, const char **endptr, + long double *result); int parse_uint(const char *s, unsigned long long *value, char **endptr, int base); diff --git a/util/cutils.c b/util/cutils.c index fd591cadf0..5db3b2add5 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -553,7 +553,7 @@ int qemu_strtou64(const char *nptr, const char **endptr, int base, /** * Convert string @nptr to a double. - * + * * This is a wrapper around strtod() that is harder to misuse. * Semantics of @nptr and @endptr match strtod() with differences * noted below. @@ -616,6 +616,52 @@ int qemu_strtod_finite(const char *nptr, const char **endptr, double *result) return ret; } +/* + * Convert string @nptr to a long double. + * + * Works like qemu_strtod(), except it stores long double. + */ +int qemu_strtold(const char *nptr, const char **endptr, long double *result) +{ + char *ep; + + if (!nptr) { + if (endptr) { + *endptr = nptr; + } + return -EINVAL; + } + + errno = 0; + *result = strtold(nptr, &ep); + return check_strtox_error(nptr, ep, endptr, errno); +} + +/* + * Convert string @nptr to a finite long double. + * + * Works like qemu_strtod_finite(), except it stores long double. + */ +int qemu_strtold_finite(const char *nptr, const char **endptr, + long double *result) +{ + long double tmp; + int ret; + + ret = qemu_strtold(nptr, endptr, &tmp); + if (!ret && !isfinite(tmp)) { + if (endptr) { + *endptr = nptr; + } + ret = -EINVAL; + } + + if (ret != -EINVAL) { + *result = tmp; + } + return ret; +} + /** * Searches for the first occurrence of 'c' in 's', and returns a pointer * to the trailing null byte if none was found. -- 2.20.1