Counter Strike : Global Offensive Source Code
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.

230 lines
6.7 KiB

  1. /* See JSON_parser.c for copyright information and licensing. */
  2. #ifndef JSON_PARSER_H
  3. #define JSON_PARSER_H
  4. /* JSON_parser.h */
  5. #include <stddef.h>
  6. /* Windows DLL stuff */
  7. #ifdef JSON_PARSER_DLL
  8. # ifdef _MSC_VER
  9. # ifdef JSON_PARSER_DLL_EXPORTS
  10. # define JSON_PARSER_DLL_API __declspec(dllexport)
  11. # else
  12. # define JSON_PARSER_DLL_API __declspec(dllimport)
  13. # endif
  14. # else
  15. # define JSON_PARSER_DLL_API
  16. # endif
  17. #else
  18. # define JSON_PARSER_DLL_API
  19. #endif
  20. /* Determine the integer type use to parse non-floating point numbers */
  21. #if defined(_MSC_VER)
  22. typedef __int64 JSON_int_t;
  23. #define JSON_PARSER_INTEGER_SSCANF_TOKEN "%lld"
  24. #define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%lld"
  25. #elif __STDC_VERSION__ >= 199901L || HAVE_LONG_LONG == 1
  26. typedef long long JSON_int_t;
  27. #define JSON_PARSER_INTEGER_SSCANF_TOKEN "%lld"
  28. #define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%lld"
  29. #else
  30. typedef long JSON_int_t;
  31. #define JSON_PARSER_INTEGER_SSCANF_TOKEN "%ld"
  32. #define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%ld"
  33. #endif
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. typedef enum
  38. {
  39. JSON_E_NONE = 0,
  40. JSON_E_INVALID_CHAR,
  41. JSON_E_INVALID_KEYWORD,
  42. JSON_E_INVALID_ESCAPE_SEQUENCE,
  43. JSON_E_INVALID_UNICODE_SEQUENCE,
  44. JSON_E_INVALID_NUMBER,
  45. JSON_E_NESTING_DEPTH_REACHED,
  46. JSON_E_UNBALANCED_COLLECTION,
  47. JSON_E_EXPECTED_KEY,
  48. JSON_E_EXPECTED_COLON,
  49. JSON_E_OUT_OF_MEMORY
  50. } JSON_error;
  51. typedef enum
  52. {
  53. JSON_T_NONE = 0,
  54. JSON_T_ARRAY_BEGIN,
  55. JSON_T_ARRAY_END,
  56. JSON_T_OBJECT_BEGIN,
  57. JSON_T_OBJECT_END,
  58. JSON_T_INTEGER,
  59. JSON_T_FLOAT,
  60. JSON_T_NULL,
  61. JSON_T_TRUE,
  62. JSON_T_FALSE,
  63. JSON_T_STRING,
  64. JSON_T_KEY,
  65. JSON_T_MAX
  66. } JSON_type;
  67. typedef struct JSON_value_struct {
  68. union {
  69. JSON_int_t integer_value;
  70. double float_value;
  71. struct {
  72. const char* value;
  73. size_t length;
  74. } str;
  75. } vu;
  76. } JSON_value;
  77. typedef struct JSON_parser_struct* JSON_parser;
  78. /*! \brief JSON parser callback
  79. \param ctx The pointer passed to new_JSON_parser.
  80. \param type An element of JSON_type but not JSON_T_NONE.
  81. \param value A representation of the parsed value. This parameter is NULL for
  82. JSON_T_ARRAY_BEGIN, JSON_T_ARRAY_END, JSON_T_OBJECT_BEGIN, JSON_T_OBJECT_END,
  83. JSON_T_NULL, JSON_T_TRUE, and JSON_T_FALSE. String values are always returned
  84. as zero-terminated C strings.
  85. \return Non-zero if parsing should continue, else zero.
  86. */
  87. typedef int (*JSON_parser_callback)(void* ctx, int type, const struct JSON_value_struct* value);
  88. /**
  89. A typedef for allocator functions semantically compatible with malloc().
  90. */
  91. typedef void* (*JSON_malloc_t)(size_t n);
  92. /**
  93. A typedef for deallocator functions semantically compatible with free().
  94. */
  95. typedef void (*JSON_free_t)(void* mem);
  96. /*! \brief The structure used to configure a JSON parser object
  97. */
  98. typedef struct {
  99. /** Pointer to a callback, called when the parser has something to tell
  100. the user. This parameter may be NULL. In this case the input is
  101. merely checked for validity.
  102. */
  103. JSON_parser_callback callback;
  104. /**
  105. Callback context - client-specified data to pass to the
  106. callback function. This parameter may be NULL.
  107. */
  108. void* callback_ctx;
  109. /** Specifies the levels of nested JSON to allow. Negative numbers yield unlimited nesting.
  110. If negative, the parser can parse arbitrary levels of JSON, otherwise
  111. the depth is the limit.
  112. */
  113. int depth;
  114. /**
  115. To allow C style comments in JSON, set to non-zero.
  116. */
  117. int allow_comments;
  118. /**
  119. To decode floating point numbers manually set this parameter to
  120. non-zero.
  121. */
  122. int handle_floats_manually;
  123. /**
  124. The memory allocation routine, which must be semantically
  125. compatible with malloc(3). If set to NULL, malloc(3) is used.
  126. If this is set to a non-NULL value then the 'free' member MUST be
  127. set to the proper deallocation counterpart for this function.
  128. Failure to do so results in undefined behaviour at deallocation
  129. time.
  130. */
  131. JSON_malloc_t malloc;
  132. /**
  133. The memory deallocation routine, which must be semantically
  134. compatible with free(3). If set to NULL, free(3) is used.
  135. If this is set to a non-NULL value then the 'alloc' member MUST be
  136. set to the proper allocation counterpart for this function.
  137. Failure to do so results in undefined behaviour at deallocation
  138. time.
  139. */
  140. JSON_free_t free;
  141. } JSON_config;
  142. /*! \brief Initializes the JSON parser configuration structure to default values.
  143. The default configuration is
  144. - 127 levels of nested JSON (depends on JSON_PARSER_STACK_SIZE, see json_parser.c)
  145. - no parsing, just checking for JSON syntax
  146. - no comments
  147. - Uses realloc() for memory de/allocation.
  148. \param config. Used to configure the parser.
  149. */
  150. JSON_PARSER_DLL_API void init_JSON_config(JSON_config * config);
  151. /*! \brief Create a JSON parser object
  152. \param config. Used to configure the parser. Set to NULL to use
  153. the default configuration. See init_JSON_config. Its contents are
  154. copied by this function, so it need not outlive the returned
  155. object.
  156. \return The parser object, which is owned by the caller and must eventually
  157. be freed by calling delete_JSON_parser().
  158. */
  159. JSON_PARSER_DLL_API JSON_parser new_JSON_parser(JSON_config const* config);
  160. /*! \brief Destroy a previously created JSON parser object. */
  161. JSON_PARSER_DLL_API void delete_JSON_parser(JSON_parser jc);
  162. /*! \brief Parse a character.
  163. \return Non-zero, if all characters passed to this function are part of are valid JSON.
  164. */
  165. JSON_PARSER_DLL_API int JSON_parser_char(JSON_parser jc, int next_char);
  166. /*! \brief Finalize parsing.
  167. Call this method once after all input characters have been consumed.
  168. \return Non-zero, if all parsed characters are valid JSON, zero otherwise.
  169. */
  170. JSON_PARSER_DLL_API int JSON_parser_done(JSON_parser jc);
  171. /*! \brief Determine if a given string is valid JSON white space
  172. \return Non-zero if the string is valid, zero otherwise.
  173. */
  174. JSON_PARSER_DLL_API int JSON_parser_is_legal_white_space_string(const char* s);
  175. /*! \brief Gets the last error that occurred during the use of JSON_parser.
  176. \return A value from the JSON_error enum.
  177. */
  178. JSON_PARSER_DLL_API int JSON_parser_get_last_error(JSON_parser jc);
  179. /*! \brief Re-sets the parser to prepare it for another parse run.
  180. \return True (non-zero) on success, 0 on error (e.g. !jc).
  181. */
  182. JSON_PARSER_DLL_API int JSON_parser_reset(JSON_parser jc);
  183. #ifdef __cplusplus
  184. }
  185. #endif
  186. #endif /* JSON_PARSER_H */