You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_locale.c 1.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stddef.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include "config.h"
  7. #include "json.h"
  8. #include "json_tokener.h"
  9. #ifdef HAVE_LOCALE_H
  10. #include <locale.h>
  11. #endif /* HAVE_LOCALE_H */
  12. #ifdef HAVE_XLOCALE_H
  13. #include <xlocale.h>
  14. #endif
  15. int main(int argc, char **argv)
  16. {
  17. json_object *new_obj;
  18. #ifdef HAVE_SETLOCALE
  19. setlocale(LC_NUMERIC, "de_DE");
  20. #else
  21. printf("No locale\n");
  22. #endif
  23. char buf1[10], buf2[10];
  24. // Should result in "0,1", if the locale is installed.
  25. // Regardless of what it generates, we check that it's
  26. // consistent below.
  27. (void)snprintf(buf1, sizeof(buf1), "%f", 0.1);
  28. MC_SET_DEBUG(1);
  29. new_obj = json_tokener_parse("[1.2,3.4,123456.78,5.0,2.3e10]");
  30. (void)snprintf(buf2, sizeof(buf2), "%f", 0.1);
  31. if (strcmp(buf1, buf2) != 0)
  32. printf("ERROR: Original locale not restored \"%s\" != \"%s\"",
  33. buf1, buf2);
  34. #ifdef HAVE_SETLOCALE
  35. setlocale(LC_NUMERIC, "C");
  36. #endif
  37. // Explicitly print each value, to avoid having the "special"
  38. // serialization done for parsed doubles simply re-emit the original
  39. // string that was parsed. (see json_object_new_double_s())
  40. printf("new_obj.to_string()=[");
  41. unsigned int ii;
  42. for (ii = 0 ; ii < json_object_array_length(new_obj); ii++)
  43. {
  44. json_object *val = json_object_array_get_idx(new_obj, ii);
  45. printf("%s%.2lf", (ii > 0) ? "," : "", json_object_get_double(val));
  46. }
  47. printf("]\n");
  48. printf("new_obj.to_string()=%s\n", json_object_to_json_string_ext(new_obj,JSON_C_TO_STRING_NOZERO));
  49. json_object_put(new_obj);
  50. }