interplane.Get Omega
Read in your language

klib

[Ported from github.com/attractivechaos/klib] added block arena

Brought by drakestapleton

branch master Branches Tags History

klib / kson.h

64 lines · 1358 bytes Raw Blame History
1#ifndef KSON_H
2#define KSON_H
3
4#include <string.h>
5
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
11
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
16
17typedef 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;
25
26typedef struct {
27 long n_nodes;
28 kson_node_t *root;
29} kson_t;
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
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);
39
40#ifdef __cplusplus
41}
42#endif
43
44#define kson_is_internal(p) ((p)->type == KSON_TYPE_BRACKET || (p)->type == KSON_TYPE_BRACE)
45
46static inline const kson_node_t *kson_by_key(const kson_node_t *p, const char *key)
47{
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;
54 }
55 return 0;
56}
57
58static inline const kson_node_t *kson_by_index(const kson_node_t *p, long i)
59{
60 if (!kson_is_internal(p)) return 0;
61 return 0 <= i && i < (long)p->n? p->v.child[i] : 0;
62}
63
64#endif