mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-11-05 04:41:20 +00:00
util: moving virDoubleToStr() from virutil to virstring.
The function virDoubleToStr() is defined in virutil.* and virStrToDouble() is defined in virstring.*. Joining both functions into the same file makes more sense. Signed-off-by: Julio Faracco <jcfaracco@gmail.com>
This commit is contained in:
parent
8a129b75e2
commit
5c54d29aae
@ -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,
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user