mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-03-20 07:59:00 +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 "base64.h"
|
||||||
#include "c-ctype.h"
|
#include "c-ctype.h"
|
||||||
#include "virstring.h"
|
#include "virstring.h"
|
||||||
|
#include "virthread.h"
|
||||||
#include "viralloc.h"
|
#include "viralloc.h"
|
||||||
#include "virbuffer.h"
|
#include "virbuffer.h"
|
||||||
#include "virerror.h"
|
#include "virerror.h"
|
||||||
@ -516,6 +517,24 @@ virStrToLong_ullp(char const *s, char **end_ptr, int base,
|
|||||||
return 0;
|
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
|
int
|
||||||
virStrToDouble(char const *s,
|
virStrToDouble(char const *s,
|
||||||
char **end_ptr,
|
char **end_ptr,
|
||||||
@ -536,6 +555,52 @@ virStrToDouble(char const *s,
|
|||||||
return 0;
|
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
|
int
|
||||||
virVasprintfInternal(bool report,
|
virVasprintfInternal(bool report,
|
||||||
int domcode,
|
int domcode,
|
||||||
|
@ -109,6 +109,9 @@ int virStrToDouble(char const *s,
|
|||||||
double *result)
|
double *result)
|
||||||
ATTRIBUTE_RETURN_CHECK;
|
ATTRIBUTE_RETURN_CHECK;
|
||||||
|
|
||||||
|
int virDoubleToStr(char **strp, double number)
|
||||||
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
|
||||||
|
|
||||||
void virSkipSpaces(const char **str) ATTRIBUTE_NONNULL(1);
|
void virSkipSpaces(const char **str) ATTRIBUTE_NONNULL(1);
|
||||||
void virSkipSpacesAndBackslash(const char **str) ATTRIBUTE_NONNULL(1);
|
void virSkipSpacesAndBackslash(const char **str) ATTRIBUTE_NONNULL(1);
|
||||||
void virTrimSpaces(char *str, char **endp) ATTRIBUTE_NONNULL(1);
|
void virTrimSpaces(char *str, char **endp) ATTRIBUTE_NONNULL(1);
|
||||||
|
@ -75,7 +75,6 @@
|
|||||||
#include "virlog.h"
|
#include "virlog.h"
|
||||||
#include "virbuffer.h"
|
#include "virbuffer.h"
|
||||||
#include "viralloc.h"
|
#include "viralloc.h"
|
||||||
#include "virthread.h"
|
|
||||||
#include "verify.h"
|
#include "verify.h"
|
||||||
#include "virfile.h"
|
#include "virfile.h"
|
||||||
#include "vircommand.h"
|
#include "vircommand.h"
|
||||||
@ -437,68 +436,6 @@ int virEnumFromString(const char *const*types,
|
|||||||
return -1;
|
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
|
* 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,
|
int virParseVersionString(const char *str, unsigned long *version,
|
||||||
bool allowMissing);
|
bool allowMissing);
|
||||||
|
|
||||||
int virDoubleToStr(char **strp, double number)
|
|
||||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
|
|
||||||
|
|
||||||
char *virFormatIntDecimal(char *buf, size_t buflen, int val)
|
char *virFormatIntDecimal(char *buf, size_t buflen, int val)
|
||||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user