• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/json-c/tests/test_locale.c

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt