Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
3.6 KiB

  1. /* main.c */
  2. /*
  3. This program demonstrates a simple application of JSON_parser. It reads
  4. a JSON text from STDIN, producing an error message if the text is rejected.
  5. % JSON_parser <test/pass1.json
  6. */
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <assert.h>
  11. #include <locale.h>
  12. #include "JSON_parser.h"
  13. static int print(void* ctx, int type, const JSON_value* value);
  14. int main(int argc, char* argv[]) {
  15. int count = 0, result = 0;
  16. FILE* input;
  17. JSON_config config;
  18. struct JSON_parser_struct* jc = NULL;
  19. init_JSON_config(&config);
  20. config.depth = 19;
  21. config.callback = &print;
  22. config.allow_comments = 1;
  23. config.handle_floats_manually = 0;
  24. /* Important! Set locale before parser is created.*/
  25. if (argc >= 2) {
  26. if (!setlocale(LC_ALL, argv[1])) {
  27. fprintf(stderr, "Failed to set locale to '%s'\n", argv[1]);
  28. }
  29. } else {
  30. fprintf(stderr, "No locale provided, C locale is used\n");
  31. }
  32. jc = new_JSON_parser(&config);
  33. input = stdin;
  34. for (; input ; ++count) {
  35. int next_char = fgetc(input);
  36. if (next_char <= 0) {
  37. break;
  38. }
  39. if (!JSON_parser_char(jc, next_char)) {
  40. fprintf(stderr, "JSON_parser_char: syntax error, byte %d\n", count);
  41. result = 1;
  42. goto done;
  43. }
  44. }
  45. if (!JSON_parser_done(jc)) {
  46. fprintf(stderr, "JSON_parser_end: syntax error\n");
  47. result = 1;
  48. goto done;
  49. }
  50. done:
  51. delete_JSON_parser(jc);
  52. return result;
  53. }
  54. static size_t s_Level = 0;
  55. static const char* s_pIndention = " ";
  56. static int s_IsKey = 0;
  57. static void print_indention()
  58. {
  59. size_t i;
  60. for (i = 0; i < s_Level; ++i) {
  61. printf("%s", s_pIndention);
  62. }
  63. }
  64. static int print(void* ctx, int type, const JSON_value* value)
  65. {
  66. switch(type) {
  67. case JSON_T_ARRAY_BEGIN:
  68. if (!s_IsKey) print_indention();
  69. s_IsKey = 0;
  70. printf("[\n");
  71. ++s_Level;
  72. break;
  73. case JSON_T_ARRAY_END:
  74. assert(!s_IsKey);
  75. if (s_Level > 0) --s_Level;
  76. print_indention();
  77. printf("]\n");
  78. break;
  79. case JSON_T_OBJECT_BEGIN:
  80. if (!s_IsKey) print_indention();
  81. s_IsKey = 0;
  82. printf("{\n");
  83. ++s_Level;
  84. break;
  85. case JSON_T_OBJECT_END:
  86. assert(!s_IsKey);
  87. if (s_Level > 0) --s_Level;
  88. print_indention();
  89. printf("}\n");
  90. break;
  91. case JSON_T_INTEGER:
  92. if (!s_IsKey) print_indention();
  93. s_IsKey = 0;
  94. printf("integer: "JSON_PARSER_INTEGER_SPRINTF_TOKEN"\n", value->vu.integer_value);
  95. break;
  96. case JSON_T_FLOAT:
  97. if (!s_IsKey) print_indention();
  98. s_IsKey = 0;
  99. printf("float: %f\n", value->vu.float_value); /* We wanted stringified floats */
  100. break;
  101. case JSON_T_NULL:
  102. if (!s_IsKey) print_indention();
  103. s_IsKey = 0;
  104. printf("null\n");
  105. break;
  106. case JSON_T_TRUE:
  107. if (!s_IsKey) print_indention();
  108. s_IsKey = 0;
  109. printf("true\n");
  110. break;
  111. case JSON_T_FALSE:
  112. if (!s_IsKey) print_indention();
  113. s_IsKey = 0;
  114. printf("false\n");
  115. break;
  116. case JSON_T_KEY:
  117. s_IsKey = 1;
  118. print_indention();
  119. printf("key = '%s', value = ", value->vu.str.value);
  120. break;
  121. case JSON_T_STRING:
  122. if (!s_IsKey) print_indention();
  123. s_IsKey = 0;
  124. printf("string: '%s'\n", value->vu.str.value);
  125. break;
  126. default:
  127. assert(0);
  128. break;
  129. }
  130. return 1;
  131. }