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.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <assert.h>
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "config.h"
  7. #include "json.h"
  8. #include "json_tokener.h"
  9. #include "snprintf_compat.h"
  10. #ifdef HAVE_LOCALE_H
  11. #include <locale.h>
  12. #endif /* HAVE_LOCALE_H */
  13. #ifdef HAVE_XLOCALE_H
  14. #include <xlocale.h>
  15. #endif
  16. int main(int argc, char **argv)
  17. {
  18. json_object *new_obj;
  19. #ifdef HAVE_SETLOCALE
  20. setlocale(LC_NUMERIC, "de_DE");
  21. #else
  22. printf("No locale\n");
  23. #endif
  24. char buf1[10], buf2[10];
  25. // Should result in "0,1", if the locale is installed.
  26. // Regardless of what it generates, we check that it's
  27. // consistent below.
  28. (void)snprintf(buf1, sizeof(buf1), "%f", 0.1);
  29. MC_SET_DEBUG(1);
  30. new_obj = json_tokener_parse("[1.2,3.4,123456.78,5.0,2.3e10]");
  31. (void)snprintf(buf2, sizeof(buf2), "%f", 0.1);
  32. if (strcmp(buf1, buf2) != 0)
  33. printf("ERROR: Original locale not restored \"%s\" != \"%s\"", 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",
  49. json_object_to_json_string_ext(new_obj, JSON_C_TO_STRING_NOZERO));
  50. json_object_put(new_obj);
  51. return 0;
  52. }