[glade/glade-3-38] GladePropertyDef: do not use setlocale in glade_dtostr()



commit 7b10c43cbea1a61c700e98c7d6b6602057a021e0
Author: Juan Pablo Ugarte <juanpablougarte gmail com>
Date:   Wed Oct 7 10:24:40 2020 -0300

    GladePropertyDef: do not use setlocale in glade_dtostr()
    
    Windows does not have setlocale() function

 gladeui/glade-property-def.c | 41 +++++++++++++++++++----------------------
 1 file changed, 19 insertions(+), 22 deletions(-)
---
diff --git a/gladeui/glade-property-def.c b/gladeui/glade-property-def.c
index 661c1735..c3ca5952 100644
--- a/gladeui/glade-property-def.c
+++ b/gladeui/glade-property-def.c
@@ -38,7 +38,6 @@
 #include <string.h>
 #include <stdlib.h>
 #include <glib/gi18n-lib.h>
-#include <locale.h>
 
 #include "glade.h"
 #include "glade-widget.h"
@@ -458,34 +457,32 @@ glade_property_def_make_string_from_objects (GladePropertyDef *
   return string;
 }
 
-static locale_t
-get_C_locale (void)
-{
-  static gsize initialized = FALSE;
-  static locale_t C_locale = NULL;
-
-  if (g_once_init_enter (&initialized))
-    {
-      C_locale = newlocale (LC_ALL_MASK, "C", NULL);
-      g_once_init_leave (&initialized, TRUE);
-    }
-
-  return C_locale;
-}
-
 static gchar *
 glade_dtostr (double number, gdouble epsilon)
 {
-  char *str = g_malloc (G_ASCII_DTOSTR_BUF_SIZE);
+  char *str = g_malloc (G_ASCII_DTOSTR_BUF_SIZE + 1);
+  char real_number[G_ASCII_DTOSTR_BUF_SIZE + 1];
+  const gchar *decimal = NULL;
   int i;
 
-  for (i = 0; i <= 20; i++)
+  g_ascii_dtostr (str, G_ASCII_DTOSTR_BUF_SIZE, number);
+  g_ascii_dtostr (real_number, G_ASCII_DTOSTR_BUF_SIZE, number);
+  decimal = g_strstr_len (real_number, G_ASCII_DTOSTR_BUF_SIZE, ".");
+
+  if (!decimal)
+    return str;
+
+  decimal++;
+
+  for (i = 1; i <= 20; i++)
     {
+      gint len = (decimal - real_number) + i;
       double rounded;
-      locale_t old_locale;
-      old_locale = uselocale (get_C_locale ());
-      snprintf (str, G_ASCII_DTOSTR_BUF_SIZE, "%.*f", i, number);
-      uselocale (old_locale);
+
+      /* add up to i decimal points */
+      str[len] = real_number[len];
+      str[len+1] = '\0';
+
       rounded = g_ascii_strtod (str, NULL);
 
       if (ABS (rounded - number) <= epsilon)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]