mirror of
https://github.com/nillerusr/source-engine.git
synced 2026-08-07 17:29:36 +00:00
1
This commit is contained in:
Vendored
+1168
File diff suppressed because it is too large
Load Diff
Vendored
+226
@@ -0,0 +1,226 @@
|
||||
/* See JSON_parser.c for copyright information and licensing. */
|
||||
|
||||
#ifndef JSON_PARSER_H
|
||||
#define JSON_PARSER_H
|
||||
|
||||
/* JSON_parser.h */
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/* Windows DLL stuff */
|
||||
#ifdef JSON_PARSER_DLL
|
||||
# ifdef _MSC_VER
|
||||
# ifdef JSON_PARSER_DLL_EXPORTS
|
||||
# define JSON_PARSER_DLL_API __declspec(dllexport)
|
||||
# else
|
||||
# define JSON_PARSER_DLL_API __declspec(dllimport)
|
||||
# endif
|
||||
# else
|
||||
# define JSON_PARSER_DLL_API
|
||||
# endif
|
||||
#else
|
||||
# define JSON_PARSER_DLL_API
|
||||
#endif
|
||||
|
||||
/* Determine the integer type use to parse non-floating point numbers */
|
||||
#if __STDC_VERSION__ >= 199901L || HAVE_LONG_LONG == 1
|
||||
typedef long long JSON_int_t;
|
||||
#define JSON_PARSER_INTEGER_SSCANF_TOKEN "%lld"
|
||||
#define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%lld"
|
||||
#else
|
||||
typedef long JSON_int_t;
|
||||
#define JSON_PARSER_INTEGER_SSCANF_TOKEN "%ld"
|
||||
#define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%ld"
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum
|
||||
{
|
||||
JSON_E_NONE = 0,
|
||||
JSON_E_INVALID_CHAR,
|
||||
JSON_E_INVALID_KEYWORD,
|
||||
JSON_E_INVALID_ESCAPE_SEQUENCE,
|
||||
JSON_E_INVALID_UNICODE_SEQUENCE,
|
||||
JSON_E_INVALID_NUMBER,
|
||||
JSON_E_NESTING_DEPTH_REACHED,
|
||||
JSON_E_UNBALANCED_COLLECTION,
|
||||
JSON_E_EXPECTED_KEY,
|
||||
JSON_E_EXPECTED_COLON,
|
||||
JSON_E_OUT_OF_MEMORY
|
||||
} JSON_error;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
JSON_T_NONE = 0,
|
||||
JSON_T_ARRAY_BEGIN,
|
||||
JSON_T_ARRAY_END,
|
||||
JSON_T_OBJECT_BEGIN,
|
||||
JSON_T_OBJECT_END,
|
||||
JSON_T_INTEGER,
|
||||
JSON_T_FLOAT,
|
||||
JSON_T_NULL,
|
||||
JSON_T_TRUE,
|
||||
JSON_T_FALSE,
|
||||
JSON_T_STRING,
|
||||
JSON_T_KEY,
|
||||
JSON_T_MAX
|
||||
} JSON_type;
|
||||
|
||||
typedef struct JSON_value_struct {
|
||||
union {
|
||||
JSON_int_t integer_value;
|
||||
|
||||
double float_value;
|
||||
|
||||
struct {
|
||||
const char* value;
|
||||
size_t length;
|
||||
} str;
|
||||
} vu;
|
||||
} JSON_value;
|
||||
|
||||
typedef struct JSON_parser_struct* JSON_parser;
|
||||
|
||||
/*! \brief JSON parser callback
|
||||
|
||||
\param ctx The pointer passed to new_JSON_parser.
|
||||
\param type An element of JSON_type but not JSON_T_NONE.
|
||||
\param value A representation of the parsed value. This parameter is NULL for
|
||||
JSON_T_ARRAY_BEGIN, JSON_T_ARRAY_END, JSON_T_OBJECT_BEGIN, JSON_T_OBJECT_END,
|
||||
JSON_T_NULL, JSON_T_TRUE, and JSON_T_FALSE. String values are always returned
|
||||
as zero-terminated C strings.
|
||||
|
||||
\return Non-zero if parsing should continue, else zero.
|
||||
*/
|
||||
typedef int (*JSON_parser_callback)(void* ctx, int type, const JSON_value* value);
|
||||
|
||||
|
||||
/**
|
||||
A typedef for allocator functions semantically compatible with malloc().
|
||||
*/
|
||||
typedef void* (*JSON_malloc_t)(size_t n);
|
||||
/**
|
||||
A typedef for deallocator functions semantically compatible with free().
|
||||
*/
|
||||
typedef void (*JSON_free_t)(void* mem);
|
||||
|
||||
/*! \brief The structure used to configure a JSON parser object
|
||||
*/
|
||||
typedef struct {
|
||||
/** Pointer to a callback, called when the parser has something to tell
|
||||
the user. This parameter may be NULL. In this case the input is
|
||||
merely checked for validity.
|
||||
*/
|
||||
JSON_parser_callback callback;
|
||||
/**
|
||||
Callback context - client-specified data to pass to the
|
||||
callback function. This parameter may be NULL.
|
||||
*/
|
||||
void* callback_ctx;
|
||||
/** Specifies the levels of nested JSON to allow. Negative numbers yield unlimited nesting.
|
||||
If negative, the parser can parse arbitrary levels of JSON, otherwise
|
||||
the depth is the limit.
|
||||
*/
|
||||
int depth;
|
||||
/**
|
||||
To allow C style comments in JSON, set to non-zero.
|
||||
*/
|
||||
int allow_comments;
|
||||
/**
|
||||
To decode floating point numbers manually set this parameter to
|
||||
non-zero.
|
||||
*/
|
||||
int handle_floats_manually;
|
||||
/**
|
||||
The memory allocation routine, which must be semantically
|
||||
compatible with malloc(3). If set to NULL, malloc(3) is used.
|
||||
|
||||
If this is set to a non-NULL value then the 'free' member MUST be
|
||||
set to the proper deallocation counterpart for this function.
|
||||
Failure to do so results in undefined behaviour at deallocation
|
||||
time.
|
||||
*/
|
||||
JSON_malloc_t malloc;
|
||||
/**
|
||||
The memory deallocation routine, which must be semantically
|
||||
compatible with free(3). If set to NULL, free(3) is used.
|
||||
|
||||
If this is set to a non-NULL value then the 'alloc' member MUST be
|
||||
set to the proper allocation counterpart for this function.
|
||||
Failure to do so results in undefined behaviour at deallocation
|
||||
time.
|
||||
*/
|
||||
JSON_free_t free;
|
||||
} JSON_config;
|
||||
|
||||
/*! \brief Initializes the JSON parser configuration structure to default values.
|
||||
|
||||
The default configuration is
|
||||
- 127 levels of nested JSON (depends on JSON_PARSER_STACK_SIZE, see json_parser.c)
|
||||
- no parsing, just checking for JSON syntax
|
||||
- no comments
|
||||
- Uses realloc() for memory de/allocation.
|
||||
|
||||
\param config. Used to configure the parser.
|
||||
*/
|
||||
JSON_PARSER_DLL_API void init_JSON_config(JSON_config * config);
|
||||
|
||||
/*! \brief Create a JSON parser object
|
||||
|
||||
\param config. Used to configure the parser. Set to NULL to use
|
||||
the default configuration. See init_JSON_config. Its contents are
|
||||
copied by this function, so it need not outlive the returned
|
||||
object.
|
||||
|
||||
\return The parser object, which is owned by the caller and must eventually
|
||||
be freed by calling delete_JSON_parser().
|
||||
*/
|
||||
JSON_PARSER_DLL_API JSON_parser new_JSON_parser(JSON_config const* config);
|
||||
|
||||
/*! \brief Destroy a previously created JSON parser object. */
|
||||
JSON_PARSER_DLL_API void delete_JSON_parser(JSON_parser jc);
|
||||
|
||||
/*! \brief Parse a character.
|
||||
|
||||
\return Non-zero, if all characters passed to this function are part of are valid JSON.
|
||||
*/
|
||||
JSON_PARSER_DLL_API int JSON_parser_char(JSON_parser jc, int next_char);
|
||||
|
||||
/*! \brief Finalize parsing.
|
||||
|
||||
Call this method once after all input characters have been consumed.
|
||||
|
||||
\return Non-zero, if all parsed characters are valid JSON, zero otherwise.
|
||||
*/
|
||||
JSON_PARSER_DLL_API int JSON_parser_done(JSON_parser jc);
|
||||
|
||||
/*! \brief Determine if a given string is valid JSON white space
|
||||
|
||||
\return Non-zero if the string is valid, zero otherwise.
|
||||
*/
|
||||
JSON_PARSER_DLL_API int JSON_parser_is_legal_white_space_string(const char* s);
|
||||
|
||||
/*! \brief Gets the last error that occurred during the use of JSON_parser.
|
||||
|
||||
\return A value from the JSON_error enum.
|
||||
*/
|
||||
JSON_PARSER_DLL_API int JSON_parser_get_last_error(JSON_parser jc);
|
||||
|
||||
/*! \brief Re-sets the parser to prepare it for another parse run.
|
||||
|
||||
\return True (non-zero) on success, 0 on error (e.g. !jc).
|
||||
*/
|
||||
JSON_PARSER_DLL_API int JSON_parser_reset(JSON_parser jc);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* JSON_PARSER_H */
|
||||
Vendored
+8
@@ -0,0 +1,8 @@
|
||||
LIST=CPU
|
||||
ifndef QRECURSE
|
||||
QRECURSE=recurse.mk
|
||||
ifdef QCONFIG
|
||||
QRDIR=$(dir $(QCONFIG))
|
||||
endif
|
||||
endif
|
||||
include $(QRDIR)$(QRECURSE)
|
||||
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
A JSON parser based on http://www.json.org's JSON_checker code.
|
||||
|
||||
Copyright (c) 2007-2010 Jean Gressmann (jean@0x42.de).
|
||||
|
||||
For license information, see JSON_parser.c.
|
||||
|
||||
JSON parser features:
|
||||
- arbitrary levels of JSON object/array nesting,
|
||||
- C-style comments,
|
||||
- UTF-16 & escape sequence (\uXXXX) decoding to UTF-8.
|
||||
- Manual processing of floating point values.
|
||||
|
||||
The parser processes UTF-8 encoded JSON only.
|
||||
JSON object keys and JSON strings are returned as UTF-8 encoded C strings.
|
||||
|
||||
|
||||
Vendored
+8
@@ -0,0 +1,8 @@
|
||||
LIST=VARIANT
|
||||
ifndef QRECURSE
|
||||
QRECURSE=recurse.mk
|
||||
ifdef QCONFIG
|
||||
QRDIR=$(dir $(QCONFIG))
|
||||
endif
|
||||
endif
|
||||
include $(QRDIR)$(QRECURSE)
|
||||
@@ -0,0 +1 @@
|
||||
include ../../common.mk
|
||||
Vendored
+116
@@ -0,0 +1,116 @@
|
||||
/***
|
||||
{},
|
||||
[],
|
||||
{ "foo" : "bar",
|
||||
"bar" : 1},
|
||||
[42],
|
||||
[
|
||||
"\u0001\u0002\u0003\uD834\uDD1E",
|
||||
"\uCDEF\uabcd\uef4A" ,
|
||||
{
|
||||
"real": -9876.543210,
|
||||
"e": 0.123456789e-12,
|
||||
"E": 1.234567890E+34,
|
||||
"": 23456789012E66,
|
||||
"zero": 0,
|
||||
"one": 1,
|
||||
"space": " ",
|
||||
"quote": "\"",
|
||||
"backslash": "\\",
|
||||
"controls": "\b\f\n\r\t",
|
||||
"slash": "/ & \/",
|
||||
"alpha": "abcdefghijklmnopqrstuvwyz",
|
||||
"ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
|
||||
"digit": "0123456789",
|
||||
"0123456789": "digit",
|
||||
"special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?",
|
||||
"hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",
|
||||
"true": true,
|
||||
"false": false,
|
||||
"null": null,
|
||||
"array":[ ],
|
||||
"object":{ },
|
||||
"address": "50 St. James Street",
|
||||
"url": "http://www.JSON.org/",
|
||||
"comment": "// /* <!-- --",
|
||||
|
||||
ŸŠš§
|
||||
}
|
||||
]]]]]]]]]]]
|
||||
|
||||
***/[
|
||||
"\u0001\u0002\u0003\uD834\uDD1E",
|
||||
"\uCDEF\uabcd\uef4A" ,
|
||||
|
||||
/**/-42/**/,/**/
|
||||
/**/ true/**/,
|
||||
/**/ false/**/,
|
||||
/**/ null/*\*/,
|
||||
|
||||
/**/ "JSON Test Pattern pass1"/**/, /**/
|
||||
/**/{ /**/ /**/ "object with 1 member"/**/:/**/ [/**/"array with 1 element"/**/]/**/ /**/ } /**/, /**/
|
||||
{},
|
||||
[],
|
||||
|
||||
{ "foo" : "bar",
|
||||
"bar" : 1},
|
||||
[42],
|
||||
{
|
||||
"integer"/**/: /**/ /**/1234567890/*
|
||||
|
||||
{
|
||||
"e": 0.123456789e-12,
|
||||
"E": 1.234567890E+34,
|
||||
"": 23456789012E66,
|
||||
"zero": 0,
|
||||
}
|
||||
|
||||
|
||||
*/,
|
||||
"real": -9876.543210/**/,
|
||||
"e": 0.123456789e-12,
|
||||
"E": 1.234567890E+34,
|
||||
"": 23456789012E66,
|
||||
"zero": 0,
|
||||
"one": 1,
|
||||
"space": " ",
|
||||
"quote": "\"",
|
||||
"backslash": "\\",
|
||||
"controls": "\b\f\n\r\t",
|
||||
"slash": "/ & \/",
|
||||
"alpha": "abcdefghijklmnopqrstuvwyz",
|
||||
"ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
|
||||
"digit": "0123456789",
|
||||
"0123456789": "digit",
|
||||
"special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?",
|
||||
"hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",
|
||||
"true": true,
|
||||
"false": false,
|
||||
"null": null,
|
||||
"array":[ ],
|
||||
"object":{ },
|
||||
"address": "50 St. James Street",
|
||||
"url": "http://www.JSON.org/",
|
||||
"comment": "// /* <!-- --",
|
||||
"# -- --> */": " ",
|
||||
" s p a c e d " :[1,2 , 3
|
||||
|
||||
,
|
||||
|
||||
4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7],
|
||||
"jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}",
|
||||
"quotes": "" \u0022 %22 0x22 034 "",
|
||||
"\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"
|
||||
: "A key can be any string"
|
||||
},
|
||||
0.5 ,98.6
|
||||
,
|
||||
99.44
|
||||
,
|
||||
|
||||
1066,
|
||||
1e1,
|
||||
0.1e1,
|
||||
1e-1,
|
||||
1e00,2e+00,2e-00
|
||||
,"rosebud", "\u005C"]/** ******/
|
||||
Vendored
+28
@@ -0,0 +1,28 @@
|
||||
ifndef QCONFIG
|
||||
QCONFIG=qconfig.mk
|
||||
endif
|
||||
include $(QCONFIG)
|
||||
|
||||
#####################################
|
||||
# Preset make macros go here
|
||||
#####################################
|
||||
|
||||
EXTRA_SRCVPATH+=$(PROJECT_ROOT)/
|
||||
|
||||
EXTRA_INCVPATH+=$(PRODUCT_ROOT)/
|
||||
|
||||
# Use same flags from Jamfile to make a shared library
|
||||
CCFLAGS +=
|
||||
|
||||
define PINFO
|
||||
PINFO DESCRIPTION=JSON_Parser
|
||||
endef
|
||||
INSTALLDIR=usr/bin
|
||||
|
||||
NAME=json_parser
|
||||
|
||||
include $(MKFILES_ROOT)/qtargets.mk
|
||||
|
||||
#####################################
|
||||
# Post-set make macros go here
|
||||
#####################################
|
||||
Vendored
+155
@@ -0,0 +1,155 @@
|
||||
/* main.c */
|
||||
|
||||
/*
|
||||
This program demonstrates a simple application of JSON_parser. It reads
|
||||
a JSON text from STDIN, producing an error message if the text is rejected.
|
||||
|
||||
% JSON_parser <test/pass1.json
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <locale.h>
|
||||
|
||||
#include "JSON_parser.h"
|
||||
|
||||
static int print(void* ctx, int type, const JSON_value* value);
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
int count = 0, result = 0;
|
||||
FILE* input;
|
||||
|
||||
JSON_config config;
|
||||
|
||||
struct JSON_parser_struct* jc = NULL;
|
||||
|
||||
init_JSON_config(&config);
|
||||
|
||||
config.depth = 19;
|
||||
config.callback = &print;
|
||||
config.allow_comments = 1;
|
||||
config.handle_floats_manually = 0;
|
||||
|
||||
/* Important! Set locale before parser is created.*/
|
||||
if (argc >= 2) {
|
||||
if (!setlocale(LC_ALL, argv[1])) {
|
||||
fprintf(stderr, "Failed to set locale to '%s'\n", argv[1]);
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "No locale provided, C locale is used\n");
|
||||
}
|
||||
|
||||
jc = new_JSON_parser(&config);
|
||||
|
||||
input = stdin;
|
||||
for (; input ; ++count) {
|
||||
int next_char = fgetc(input);
|
||||
if (next_char <= 0) {
|
||||
break;
|
||||
}
|
||||
if (!JSON_parser_char(jc, next_char)) {
|
||||
fprintf(stderr, "JSON_parser_char: syntax error, byte %d\n", count);
|
||||
result = 1;
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
if (!JSON_parser_done(jc)) {
|
||||
fprintf(stderr, "JSON_parser_end: syntax error\n");
|
||||
result = 1;
|
||||
goto done;
|
||||
}
|
||||
|
||||
done:
|
||||
delete_JSON_parser(jc);
|
||||
return result;
|
||||
}
|
||||
|
||||
static size_t s_Level = 0;
|
||||
|
||||
static const char* s_pIndention = " ";
|
||||
|
||||
static int s_IsKey = 0;
|
||||
|
||||
static void print_indention()
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < s_Level; ++i) {
|
||||
printf("%s", s_pIndention);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int print(void* ctx, int type, const JSON_value* value)
|
||||
{
|
||||
switch(type) {
|
||||
case JSON_T_ARRAY_BEGIN:
|
||||
if (!s_IsKey) print_indention();
|
||||
s_IsKey = 0;
|
||||
printf("[\n");
|
||||
++s_Level;
|
||||
break;
|
||||
case JSON_T_ARRAY_END:
|
||||
assert(!s_IsKey);
|
||||
if (s_Level > 0) --s_Level;
|
||||
print_indention();
|
||||
printf("]\n");
|
||||
break;
|
||||
case JSON_T_OBJECT_BEGIN:
|
||||
if (!s_IsKey) print_indention();
|
||||
s_IsKey = 0;
|
||||
printf("{\n");
|
||||
++s_Level;
|
||||
break;
|
||||
case JSON_T_OBJECT_END:
|
||||
assert(!s_IsKey);
|
||||
if (s_Level > 0) --s_Level;
|
||||
print_indention();
|
||||
printf("}\n");
|
||||
break;
|
||||
case JSON_T_INTEGER:
|
||||
if (!s_IsKey) print_indention();
|
||||
s_IsKey = 0;
|
||||
printf("integer: "JSON_PARSER_INTEGER_SPRINTF_TOKEN"\n", value->vu.integer_value);
|
||||
break;
|
||||
case JSON_T_FLOAT:
|
||||
if (!s_IsKey) print_indention();
|
||||
s_IsKey = 0;
|
||||
printf("float: %f\n", value->vu.float_value); /* We wanted stringified floats */
|
||||
break;
|
||||
case JSON_T_NULL:
|
||||
if (!s_IsKey) print_indention();
|
||||
s_IsKey = 0;
|
||||
printf("null\n");
|
||||
break;
|
||||
case JSON_T_TRUE:
|
||||
if (!s_IsKey) print_indention();
|
||||
s_IsKey = 0;
|
||||
printf("true\n");
|
||||
break;
|
||||
case JSON_T_FALSE:
|
||||
if (!s_IsKey) print_indention();
|
||||
s_IsKey = 0;
|
||||
printf("false\n");
|
||||
break;
|
||||
case JSON_T_KEY:
|
||||
s_IsKey = 1;
|
||||
print_indention();
|
||||
printf("key = '%s', value = ", value->vu.str.value);
|
||||
break;
|
||||
case JSON_T_STRING:
|
||||
if (!s_IsKey) print_indention();
|
||||
s_IsKey = 0;
|
||||
printf("string: '%s'\n", value->vu.str.value);
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
break;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user