-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.h
129 lines (106 loc) · 2.52 KB
/
common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef LIB_COMMON
#define LIB_COMMON
#define HASH_CAPACITY 50000 // Size of the HashTable.
typedef enum JsonType
{
JSON_TRUE,
JSON_FALSE,
JSON_NULL,
JSON_NUMBER,
JSON_STRING,
JSON_ARRAY,
JSON_OBJECT,
JSON_NOT_AVAILABLE
} JsonType;
typedef enum InsertResult
{
INSERT_ERROR,
INSERT_OK,
INSERT_OVERFLOW,
INSERT_EXISTS
} InsertResult;
typedef enum ParseResult
{
PARSE_ERROR,
PARSE_ERROR_OVERFLOW_UNDERFLOW,
PARSE_ERROR_INVALID_FORMAT,
PARSE_OK
} ParseResult;
typedef struct JsonValue JsonValue;
typedef struct JsonLinkedNode JsonLinkedNode;
typedef struct JsonObject JsonObject;
typedef struct JsonObjectItem JsonObjectItem;
typedef struct JsonArray JsonArray;
typedef struct JsonError JsonError;
struct JsonObjectItem
{
unsigned long hash;
JsonLinkedNode *head;
};
struct JsonObject
{
JsonObjectItem **items;
int size;
int count;
};
struct JsonLinkedNode
{
JsonValue *value;
JsonLinkedNode *next;
char *key;
int index;
};
struct JsonArray
{
JsonLinkedNode *head;
int size;
};
struct JsonValue
{
JsonError *error;
/* starting index */
int start;
/* ending index (inclusive) */
int end;
/* indicate if we partially matched the type or not,
* value should be ignored when there is no error
*/
int partial;
/** value type */
enum JsonType type;
/** number value */
double number;
/** string value */
char *string;
/** array value */
struct JsonArray *array;
/** object value */
struct JsonObject *object;
};
struct JsonError
{
int index;
char *code;
};
void skip_empty(char *json, int *current, int size);
unsigned long hash_key(char *key, int capacity);
JsonValue *create_json_value();
void free_json_value(JsonValue *value);
JsonLinkedNode *create_json_list();
int append_value_to_list(JsonLinkedNode *list, JsonValue *value);
JsonArray *create_json_array();
void free_json_array(JsonArray *array);
int append_to_json_array(JsonArray *array, JsonValue *value);
void free_json_list(JsonLinkedNode *node);
JsonObjectItem *create_object_item(unsigned long hash);
void free_object_item(JsonObjectItem *item);
JsonObject *create_object(int size);
void free_object(JsonObject *object);
int insert_in_object(JsonObject *object, char *key, JsonValue *value);
JsonValue *get_object_value(JsonObject *object, char *key);
JsonValue *match_any(char *json, int *current, int size);
char *get_json_file_content(char *path);
JsonError *create_json_error(int index, char *code);
void free_json_error(JsonError *error);
char *slice_string(const char *str, int start, int end);
#endif