]> granicus.if.org Git - json-c/commitdiff
move locale change to be global for perf
authorRemi Collet <fedora@famillecollet.com>
Thu, 13 Dec 2012 08:47:33 +0000 (09:47 +0100)
committerRemi Collet <fedora@famillecollet.com>
Thu, 13 Dec 2012 08:47:33 +0000 (09:47 +0100)
json_tokener.c
json_util.c
tests/Makefile.am
tests/test_locale.c [new file with mode: 0644]

index 85c530b447ec54760ec5a57a21c5bc648dd14aee..63bb41b397fe0bc7058219dbcd8d667c1f8aaf07 100644 (file)
 #include "json_tokener.h"
 #include "json_util.h"
 
+#ifdef HAVE_LOCALE_H
+#include <locale.h>
+#endif /* HAVE_LOCALE_H */
+
 #if !HAVE_STRDUP && defined(_MSC_VER)
   /* MSC has the version as _strdup */
 # define strdup _strdup
@@ -227,6 +231,13 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
 {
   struct json_object *obj = NULL;
   char c = '\1';
+#ifdef HAVE_SETLOCALE
+  char *oldlocale=NULL, *tmplocale;
+
+  tmplocale = setlocale(LC_NUMERIC, NULL);
+  if (tmplocale) oldlocale = strdup(tmplocale);
+  setlocale(LC_NUMERIC, "C");
+#endif
 
   tok->char_offset = 0;
   tok->err = json_tokener_success;
@@ -724,6 +735,11 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
       tok->err = json_tokener_error_parse_eof;
   }
 
+#ifdef HAVE_SETLOCALE
+  setlocale(LC_NUMERIC, oldlocale);
+  if (oldlocale) free(oldlocale);
+#endif
+
   if (tok->err == json_tokener_success) 
   {
     json_object *ret = json_object_get(current);
index c144059fded82488f3be60c055e5d6fa3ff15602..0a598115e2a14ea059bb1eeeaa19b7fc4c2278c1 100644 (file)
 # include <unistd.h>
 #endif /* HAVE_UNISTD_H */
 
-#ifdef HAVE_LOCALE_H
-#include <locale.h>
-#endif /* HAVE_LOCALE_H */
-
 #ifdef WIN32
 # define WIN32_LEAN_AND_MEAN
 # include <windows.h>
@@ -148,23 +144,7 @@ int json_object_to_file(char *filename, struct json_object *obj)
 
 int json_parse_double(const char *buf, double *retval)
 {
-       int ret;
-#ifdef HAVE_SETLOCALE
-       char *old=NULL, *tmp;
-
-       tmp = setlocale(LC_NUMERIC, NULL);
-       if (tmp) old = strdup(tmp);
-       setlocale(LC_NUMERIC, "C");
-#endif
-
-       ret = sscanf(buf, "%lf", retval);
-
-#ifdef HAVE_SETLOCALE
-       setlocale(LC_NUMERIC, old);
-       if (old) free(old);
-#endif
-
-       return (ret==1 ? 0 : 1);
+  return (sscanf(buf, "%lf", retval)==1 ? 0 : 1);
 }
 
 int json_parse_int64(const char *buf, int64_t *retval)
index 8057acdf51c8e8587d9ac021c70a55f4c0625c93..90222ba0cd8697897289c4b3ecf840916e03f351 100644 (file)
@@ -11,6 +11,7 @@ check_PROGRAMS += test_parse_int64
 check_PROGRAMS += test_null
 check_PROGRAMS += test_cast
 check_PROGRAMS += test_parse
+check_PROGRAMS += test_locale
 
 test1_LDADD = $(LIBJSON_LA)
 
@@ -36,6 +37,8 @@ test_cast_LDADD = $(LIBJSON_LA)
 
 test_parse_LDADD = $(LIBJSON_LA)
 
+test_locale_LDADD = $(LIBJSON_LA)
+
 TESTS = test1.test test2.test test4.test testReplaceExisting.test parse_int64.test test_null.test test_cast.test test_parse.test
 
 TESTS+= test_printbuf.test
diff --git a/tests/test_locale.c b/tests/test_locale.c
new file mode 100644 (file)
index 0000000..6926a5d
--- /dev/null
@@ -0,0 +1,30 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <string.h>
+#include <assert.h>
+
+#include "config.h"
+#include "json.h"
+#include "json_tokener.h"
+
+#ifdef HAVE_LOCALE_H
+#include <locale.h>
+#endif /* HAVE_LOCALE_H */
+
+int main(int argc, char **argv)
+{
+       json_object *new_obj;
+#ifdef HAVE_SETLOCALE
+       setlocale(LC_NUMERIC, "de_DE");
+#else
+       printf("No locale\n");
+#endif
+
+       MC_SET_DEBUG(1);
+
+       new_obj = json_tokener_parse("[1.2,3.4,123456.78,5.0]");
+       printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+       json_object_put(new_obj);
+}
+