AC_FUNC_VPRINTF
AC_FUNC_MEMCMP
AC_CHECK_FUNCS([realloc])
-AC_CHECK_FUNCS(strcasecmp strdup strerror snprintf vsnprintf vasprintf open vsyslog strncasecmp setlocale)
+AC_CHECK_FUNCS(strcasecmp strdup strerror snprintf vsnprintf vasprintf open vsyslog strncasecmp setlocale uselocale)
AC_CHECK_DECLS([INFINITY], [], [], [[#include <math.h>]])
AC_CHECK_DECLS([nan], [], [], [[#include <math.h>]])
AC_CHECK_DECLS([isnan], [], [], [[#include <math.h>]])
{
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");
+#ifdef HAVE_USELOCALE
+ locale_t oldlocale = uselocale(NULL);
+ locale_t newloc;
+#elif defined(HAVE_SETLOCALE)
+ char *oldlocale = NULL;
#endif
tok->char_offset = 0;
the string length is less than INT32_MAX (2GB) */
if ((len < -1) || (len == -1 && strlen(str) > INT32_MAX)) {
tok->err = json_tokener_error_size;
-#ifdef HAVE_SETLOCALE
- free(oldlocale);
-#endif
return NULL;
}
+#ifdef HAVE_USELOCALE
+ {
+ locale_t duploc = duplocale(oldlocale);
+ newloc = newlocale(LC_NUMERIC, "C", duploc);
+ // XXX at least Debian 8.4 has a bug in newlocale where it doesn't
+ // change the decimal separator unless you set LC_TIME!
+ if (newloc)
+ newloc = newlocale(LC_TIME, "C", newloc);
+ if (newloc == NULL)
+ {
+ freelocale(duploc);
+ return NULL;
+ }
+ }
+#elif defined(HAVE_SETLOCALE)
+ {
+ char *tmplocale;
+ tmplocale = setlocale(LC_NUMERIC, NULL);
+ if (tmplocale) oldlocale = strdup(tmplocale);
+ setlocale(LC_NUMERIC, "C");
+ }
+#endif
+
while (PEEK_CHAR(c, tok)) {
redo_char:
goto redo_char;
case json_tokener_state_object_sep:
+ /* { */
if(c == '}') {
saved_state = json_tokener_state_finish;
state = json_tokener_state_eatws;
tok->err = json_tokener_error_parse_eof;
}
-#ifdef HAVE_SETLOCALE
+#ifdef HAVE_USELOCALE
+ uselocale(oldlocale);
+ freelocale(newloc);
+#elif defined(HAVE_SETLOCALE)
setlocale(LC_NUMERIC, oldlocale);
free(oldlocale);
#endif
printf("No locale\n");
#endif
+ char buf1[10], buf2[10];
+ // Should result in "0,1", if the locale is installed.
+ // Regardless of what it generates, we check that it's
+ // consistent below.
+ (void)snprintf(buf1, sizeof(buf1), "%f", 0.1);
+
MC_SET_DEBUG(1);
new_obj = json_tokener_parse("[1.2,3.4,123456.78,5.0,2.3e10]");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
printf("new_obj.to_string()=%s\n", json_object_to_json_string_ext(new_obj,JSON_C_TO_STRING_NOZERO));
json_object_put(new_obj);
+
+ (void)snprintf(buf2, sizeof(buf2), "%f", 0.1);
+ if (strcmp(buf1, buf2) != 0)
+ printf("ERROR: Original locale not restored \"%s\" != \"%s\"",
+ buf1, buf2);
}