On Wed, Jun 14, 2017 at 01:24:42PM -0300, Julio Faracco wrote:
Following the GNU Documentation, functions to convert double/float to
string
and vice versa, use the locale to set the mantissa separator. Some languages
use comma and other use dot as a separator.
For example: 1,212.67 (en_US) = 1.112,67 (pt_BR).
This can be used to parse values in float/double from XML and other definition
files.
Signed-off-by: Julio Faracco <jcfaracco(a)gmail.com>
---
src/libvirt_private.syms | 1 +
src/util/virstring.c | 31 +++++++++++++++++++++++++++++++
src/util/virstring.h | 4 ++++
3 files changed, 36 insertions(+)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 044510f..9d791e6 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2659,6 +2659,7 @@ virStringTrimOptionalNewline;
virStrncpy;
virStrndup;
virStrToDouble;
+virStrToDoubleSafe;
virStrToLong_i;
virStrToLong_l;
virStrToLong_ll;
diff --git a/src/util/virstring.c b/src/util/virstring.c
index 089b539..6dad00f 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -537,6 +537,37 @@ virStrToDouble(char const *s,
}
int
+virStrToDoubleSafe(char const *s,
+ char **end_ptr,
+ double *result)
+{
+ char *cur_locale = NULL;
+ char *saved_locale = NULL;
+ int ret = -1;
+
+ cur_locale = setlocale (LC_ALL, NULL);
+ if (!cur_locale)
+ return ret;
+
+ if (VIR_STRDUP(saved_locale, cur_locale) < 0)
+ goto cleanup;
+
+ if (!setlocale (LC_ALL, "C"))
+ goto cleanup;
+
This is MT-Unsafe. We cannot set this for the whole process, so NACK to
this approach. You need to use uselocale() as in commit 43bfa23e6f96
(or just see virDoubleToStr()).
Martin