modified: src1/input.c
[GalaxyCodeBases.git] / c_cpp / lib / klib / kson.h
bloba03eb52f58a3ad4663784973203b5f5b8db55fef
1 #ifndef KSON_H
2 #define KSON_H
4 #include <string.h>
6 #define KSON_TYPE_NO_QUOTE 1
7 #define KSON_TYPE_SGL_QUOTE 2
8 #define KSON_TYPE_DBL_QUOTE 3
9 #define KSON_TYPE_BRACKET 4
10 #define KSON_TYPE_BRACE 5
12 #define KSON_OK 0
13 #define KSON_ERR_EXTRA_LEFT 1
14 #define KSON_ERR_EXTRA_RIGHT 2
15 #define KSON_ERR_NO_KEY 3
17 typedef struct kson_node_s {
18 unsigned long long type:3, n:61;
19 char *key;
20 union {
21 struct kson_node_s **child;
22 char *str;
23 } v;
24 } kson_node_t;
26 typedef struct {
27 long n_nodes;
28 kson_node_t *root;
29 } kson_t;
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
35 kson_t *kson_parse(const char *json);
36 void kson_destroy(kson_t *kson);
37 const kson_node_t *kson_by_path(const kson_node_t *root, int path_len, ...);
38 void kson_format(const kson_node_t *root);
40 #ifdef __cplusplus
42 #endif
44 #define kson_is_internal(p) ((p)->type == KSON_TYPE_BRACKET || (p)->type == KSON_TYPE_BRACE)
46 static inline const kson_node_t *kson_by_key(const kson_node_t *p, const char *key)
48 long i;
49 if (!kson_is_internal(p)) return 0;
50 for (i = 0; i < (long)p->n; ++i) {
51 const kson_node_t *q = p->v.child[i];
52 if (q->key && strcmp(q->key, key) == 0)
53 return q;
55 return 0;
58 static inline const kson_node_t *kson_by_index(const kson_node_t *p, long i)
60 if (!kson_is_internal(p)) return 0;
61 return 0 <= i && i < (long)p->n? p->v.child[i] : 0;
64 #endif