r20192 - gnucash/trunk/src - Adjust gnc_push_locale and gnc_pop_locale to take a locale category and

John Ralls jralls at code.gnucash.org
Sat Jan 29 15:40:02 EST 2011


Author: jralls
Date: 2011-01-29 15:40:02 -0500 (Sat, 29 Jan 2011)
New Revision: 20192
Trac: http://svn.gnucash.org/trac/changeset/20192

Modified:
   gnucash/trunk/src/app-utils/gnc-ui-util.c
   gnucash/trunk/src/app-utils/gnc-ui-util.h
   gnucash/trunk/src/backend/dbi/Makefile.am
   gnucash/trunk/src/backend/dbi/gnc-backend-dbi.c
   gnucash/trunk/src/html/gnc-html-graph-gog-gtkhtml.c
   gnucash/trunk/src/html/gnc-html-graph-gog-webkit.c
Log:
Adjust gnc_push_locale and gnc_pop_locale to take a locale category and 
to handle MSWin32 (following gnome/druid-hierarchy.c) correctly.
Use them in gnc-backend-dbi.c instead of the incorrect code from r20189 
(Thanks, Christian).



Modified: gnucash/trunk/src/app-utils/gnc-ui-util.c
===================================================================
--- gnucash/trunk/src/app-utils/gnc-ui-util.c	2011-01-29 00:40:46 UTC (rev 20191)
+++ gnucash/trunk/src/app-utils/gnc-ui-util.c	2011-01-29 20:40:02 UTC (rev 20192)
@@ -1226,19 +1226,26 @@
 static GList *locale_stack = NULL;
 
 void
-gnc_push_locale (const char *locale)
+gnc_push_locale (int category, const char *locale)
 {
     char *saved_locale;
 
     g_return_if_fail (locale != NULL);
 
-    saved_locale = g_strdup (setlocale (LC_ALL, NULL));
+# ifdef G_OS_WIN32
+    /* On win32, setlocale() doesn't say anything useful. Use
+       glib's function instead. */
+    saved_locale = g_win32_getlocale();
+# else
+    saved_locale = g_strdup(setlocale(category, NULL) ?
+			    setlocale(category, NULL) : "C");
+#endif
     locale_stack = g_list_prepend (locale_stack, saved_locale);
-    setlocale (LC_ALL, locale);
+    setlocale (category, locale);
 }
 
 void
-gnc_pop_locale (void)
+gnc_pop_locale (int category)
 {
     char *saved_locale;
     GList *node;
@@ -1248,7 +1255,7 @@
     node = locale_stack;
     saved_locale = node->data;
 
-    setlocale (LC_ALL, saved_locale);
+    setlocale (category, saved_locale);
 
     locale_stack = g_list_remove_link (locale_stack, node);
     g_list_free_1 (node);

Modified: gnucash/trunk/src/app-utils/gnc-ui-util.h
===================================================================
--- gnucash/trunk/src/app-utils/gnc-ui-util.h	2011-01-29 00:40:46 UTC (rev 20191)
+++ gnucash/trunk/src/app-utils/gnc-ui-util.h	2011-01-29 20:40:02 UTC (rev 20192)
@@ -241,12 +241,27 @@
 /* Returns the number of decimal place to print in the current locale */
 int gnc_locale_decimal_places (void);
 
-/* Push and pop locales. Currently, this has no effect on gnc_localeconv.
- * i.e., after the first call to gnc_localeconv, subsequent calls will
- * return the same information. */
-void gnc_push_locale (const char *locale);
-void gnc_pop_locale (void);
+/** Temporarily change locale, pushing the old one onto a stack
+ * Currently, this has no effect on gnc_localeconv.  i.e., after the
+ * first call to gnc_localeconv, subsequent calls will return the same
+ * information.
+ *
+ * WARNING: Be careful to maintain the correct nesting order of pushes
+ * or pops; otherwise, the localization results might be
+ * interesting. Note that the stack does not keep track of which
+ * category a locale was pushed from, so careless use will alse
+ * produce interesting results.
+ *
+ * @param category: The locale category (e.g. LC_ALL, LC_NUMERIC) to push onto
+ * @param locale: The new locale to set
+ */
+void gnc_push_locale (int category, const char *locale);
 
+/** Restore the last-pushed locale.
+ * @param category: The locale category to restore the locale to.
+ */
+void gnc_pop_locale (int category);
+
 /* Amount printing and parsing **************************************/
 
 /*

Modified: gnucash/trunk/src/backend/dbi/Makefile.am
===================================================================
--- gnucash/trunk/src/backend/dbi/Makefile.am	2011-01-29 00:40:46 UTC (rev 20191)
+++ gnucash/trunk/src/backend/dbi/Makefile.am	2011-01-29 20:40:02 UTC (rev 20192)
@@ -12,6 +12,7 @@
   -I${top_srcdir}/src/backend/sql \
   -I${top_srcdir}/src/debug \
   -I${top_srcdir}/src/engine \
+  -I${top_srcdir}/src/app-utils \
   -I${top_srcdir}/src/core-utils \
   -I${top_srcdir}/lib/libc \
   -I${top_srcdir}/src/libqof/qof \

Modified: gnucash/trunk/src/backend/dbi/gnc-backend-dbi.c
===================================================================
--- gnucash/trunk/src/backend/dbi/gnc-backend-dbi.c	2011-01-29 00:40:46 UTC (rev 20191)
+++ gnucash/trunk/src/backend/dbi/gnc-backend-dbi.c	2011-01-29 20:40:02 UTC (rev 20192)
@@ -29,13 +29,10 @@
 #include "config.h"
 
 #include <errno.h>
-#include <glib.h>
-#include <glib/gi18n.h>
 #include <glib/gstdio.h>
 #if !HAVE_GMTIME_R
 #include "gmtime_r.h"
 #endif
-#include <locale.h>
 
 #include "gnc-backend-dbi-priv.h"
 
@@ -50,6 +47,7 @@
 
 #include "gnc-gconf-utils.h"
 #include "gnc-uri-utils.h"
+#include "gnc-ui-util.h"
 
 #include "gnc-backend-dbi.h"
 
@@ -1842,13 +1840,11 @@
     guint attrs;
     GValue* value;
     time_t time;
-    char *locale = setlocale( LC_NUMERIC, "" );
     struct tm tm_struct;
 
     type = dbi_result_get_field_type( dbi_row->result, col_name );
     attrs = dbi_result_get_field_attribs( dbi_row->result, col_name );
     value = g_new0( GValue, 1 );
-    setlocale( LC_NUMERIC, "C" );
     g_assert( value != NULL );
 
     switch ( type )
@@ -1858,6 +1854,7 @@
         g_value_set_int64( value, dbi_result_get_longlong( dbi_row->result, col_name ) );
         break;
     case DBI_TYPE_DECIMAL:
+	gnc_push_locale( LC_NUMERIC, "C" );
         if ( (attrs & DBI_DECIMAL_SIZEMASK) == DBI_DECIMAL_SIZE4 )
         {
             (void)g_value_init( value, G_TYPE_FLOAT );
@@ -1872,6 +1869,7 @@
         {
             PERR( "Field %s: strange decimal length attrs=%d\n", col_name, attrs );
         }
+	gnc_pop_locale( LC_NUMERIC );
         break;
     case DBI_TYPE_STRING:
         (void)g_value_init( value, G_TYPE_STRING );
@@ -1900,7 +1898,6 @@
     }
 
     dbi_row->gvalue_list = g_list_prepend( dbi_row->gvalue_list, value );
-    setlocale( LC_NUMERIC, locale );
     return value;
 }
 
@@ -2902,12 +2899,11 @@
     }
     while ( dbi_result_next_row( result ))
     {
-	const char *locale = setlocale( LC_NUMERIC, "" );
-	setlocale( LC_NUMERIC, "C");
+	gnc_push_locale( LC_NUMERIC, "C");
         resultlonglong = dbi_result_get_longlong( result, "test_int" );
         resultulonglong = dbi_result_get_ulonglong( result, "test_unsigned" );
         resultdouble = dbi_result_get_double( result, "test_double" );
-	setlocale (LC_NUMERIC, locale );
+	gnc_pop_locale( LC_NUMERIC );
     }
     if ( testlonglong != resultlonglong )
     {

Modified: gnucash/trunk/src/html/gnc-html-graph-gog-gtkhtml.c
===================================================================
--- gnucash/trunk/src/html/gnc-html-graph-gog-gtkhtml.c	2011-01-29 00:40:46 UTC (rev 20191)
+++ gnucash/trunk/src/html/gnc-html-graph-gog-gtkhtml.c	2011-01-29 20:40:02 UTC (rev 20192)
@@ -97,7 +97,7 @@
     // guile is going to (puts ...) the elements of the double array
     // together. In non-POSIX locales, that will be in a format that
     // the locale-specific sscanf will not be able to parse.
-    gnc_push_locale("C");
+    gnc_push_locale(LC_NUMERIC, "C");
     {
         for (n = 0; n < nvalues; n++)
         {
@@ -105,7 +105,7 @@
             string = next;
         }
     }
-    gnc_pop_locale();
+    gnc_pop_locale(LC_NUMERIC);
 
     return retval;
 }

Modified: gnucash/trunk/src/html/gnc-html-graph-gog-webkit.c
===================================================================
--- gnucash/trunk/src/html/gnc-html-graph-gog-webkit.c	2011-01-29 00:40:46 UTC (rev 20191)
+++ gnucash/trunk/src/html/gnc-html-graph-gog-webkit.c	2011-01-29 20:40:02 UTC (rev 20192)
@@ -78,7 +78,7 @@
     // guile is going to (puts ...) the elements of the double array
     // together. In non-POSIX locales, that will be in a format that
     // the locale-specific sscanf will not be able to parse.
-    gnc_push_locale("C");
+    gnc_push_locale(LC_NUMERIC, "C");
     {
         for (n = 0; n < nvalues; n++)
         {
@@ -86,7 +86,7 @@
             string = next;
         }
     }
-    gnc_pop_locale();
+    gnc_pop_locale(LC_NUMERIC);
 
     return retval;
 }



More information about the gnucash-changes mailing list