diff --git a/src/util/virstring.c b/src/util/virstring.c index 089b53946a212ece919689f6d6a550d157c715f2..9c12f7be84ce9489dc11f4e10973385aca4507d2 100644 --- a/src/util/virstring.c +++ b/src/util/virstring.c @@ -28,6 +28,7 @@ #include "base64.h" #include "c-ctype.h" #include "virstring.h" +#include "virthread.h" #include "viralloc.h" #include "virbuffer.h" #include "virerror.h" @@ -516,6 +517,24 @@ virStrToLong_ullp(char const *s, char **end_ptr, int base, return 0; } +/* In case thread-safe locales are available */ +#if HAVE_NEWLOCALE + +static locale_t virLocale; + +static int +virLocaleOnceInit(void) +{ + virLocale = newlocale(LC_ALL_MASK, "C", (locale_t)0); + if (!virLocale) + return -1; + return 0; +} + +VIR_ONCE_GLOBAL_INIT(virLocale); +#endif + + int virStrToDouble(char const *s, char **end_ptr, @@ -536,6 +555,52 @@ virStrToDouble(char const *s, return 0; } +/** + * virDoubleToStr + * + * converts double to string with C locale (thread-safe). + * + * Returns -1 on error, size of the string otherwise. + */ +int +virDoubleToStr(char **strp, double number) +{ + int ret = -1; + +#if HAVE_NEWLOCALE + + locale_t old_loc; + + if (virLocaleInitialize() < 0) + goto error; + + old_loc = uselocale(virLocale); + ret = virAsprintf(strp, "%lf", number); + uselocale(old_loc); + +#else + + char *radix, *tmp; + struct lconv *lc; + + if ((ret = virAsprintf(strp, "%lf", number) < 0)) + goto error; + + lc = localeconv(); + radix = lc->decimal_point; + tmp = strstr(*strp, radix); + if (tmp) { + *tmp = '.'; + if (strlen(radix) > 1) + memmove(tmp + 1, tmp + strlen(radix), strlen(*strp) - (tmp - *strp)); + } + +#endif /* HAVE_NEWLOCALE */ + error: + return ret; +} + + int virVasprintfInternal(bool report, int domcode, diff --git a/src/util/virstring.h b/src/util/virstring.h index 0038a405b121eb9d2bd3e09c6118aa613a85a6d4..5eaaaea0a2fb21db8aac6d8fe644e167c687f0e4 100644 --- a/src/util/virstring.h +++ b/src/util/virstring.h @@ -109,6 +109,9 @@ int virStrToDouble(char const *s, double *result) ATTRIBUTE_RETURN_CHECK; +int virDoubleToStr(char **strp, double number) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK; + void virSkipSpaces(const char **str) ATTRIBUTE_NONNULL(1); void virSkipSpacesAndBackslash(const char **str) ATTRIBUTE_NONNULL(1); void virTrimSpaces(char *str, char **endp) ATTRIBUTE_NONNULL(1); diff --git a/src/util/virutil.c b/src/util/virutil.c index aba7c6d4fe5d19115ac0423ec6a52bf6f510369a..d7e01d46426814d629ec098aeac7dcd9eef03a80 100644 --- a/src/util/virutil.c +++ b/src/util/virutil.c @@ -75,7 +75,6 @@ #include "virlog.h" #include "virbuffer.h" #include "viralloc.h" -#include "virthread.h" #include "verify.h" #include "virfile.h" #include "vircommand.h" @@ -437,68 +436,6 @@ int virEnumFromString(const char *const*types, return -1; } -/* In case thread-safe locales are available */ -#if HAVE_NEWLOCALE - -static locale_t virLocale; - -static int -virLocaleOnceInit(void) -{ - virLocale = newlocale(LC_ALL_MASK, "C", (locale_t)0); - if (!virLocale) - return -1; - return 0; -} - -VIR_ONCE_GLOBAL_INIT(virLocale) -#endif - -/** - * virDoubleToStr - * - * converts double to string with C locale (thread-safe). - * - * Returns -1 on error, size of the string otherwise. - */ -int -virDoubleToStr(char **strp, double number) -{ - int ret = -1; - -#if HAVE_NEWLOCALE - - locale_t old_loc; - - if (virLocaleInitialize() < 0) - goto error; - - old_loc = uselocale(virLocale); - ret = virAsprintf(strp, "%lf", number); - uselocale(old_loc); - -#else - - char *radix, *tmp; - struct lconv *lc; - - if ((ret = virAsprintf(strp, "%lf", number) < 0)) - goto error; - - lc = localeconv(); - radix = lc->decimal_point; - tmp = strstr(*strp, radix); - if (tmp) { - *tmp = '.'; - if (strlen(radix) > 1) - memmove(tmp + 1, tmp + strlen(radix), strlen(*strp) - (tmp - *strp)); - } - -#endif /* HAVE_NEWLOCALE */ - error: - return ret; -} - /** * Format @val as a base-10 decimal number, in the diff --git a/src/util/virutil.h b/src/util/virutil.h index 86e9051225ba7ef4a2df6ba47ea49d3361e12d7d..49382557f6d6292c8bda5087b8c096bd0f246dd3 100644 --- a/src/util/virutil.h +++ b/src/util/virutil.h @@ -63,9 +63,6 @@ int virParseNumber(const char **str); int virParseVersionString(const char *str, unsigned long *version, bool allowMissing); -int virDoubleToStr(char **strp, double number) - ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK; - char *virFormatIntDecimal(char *buf, size_t buflen, int val) ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;