aboutsummaryrefslogtreecommitdiff
path: root/lib/dtree_utils.c
blob: 3d1c39441af8edb326623549772c48be5cb77f5a (plain)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <dtree/dtree.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

#include "jsmn.h"


#define DTREE_TOK_BOOLEAN   (1 << 1)
#define DTREE_TOK_NUMERICAL (1 << 2)
#define DTREE_TOK_LITERAL   (1 << 3)


char *tok_to_str(jsmntype_t *tok)
{
    switch(*tok) {
        case JSMN_UNDEFINED: return "UNDEFINED";
        case JSMN_OBJECT: return "OBJECT";
        case JSMN_ARRAY: return "ARRAY";
        case JSMN_STRING: return "STRING";
        case JSMN_PRIMITIVE: return "PRIMITIVE";
        default: return "UNKNOWN";
    }
}


int digest_payload(const char *token)
{
    char* end;
    size_t len = strlen(token);

    if(len == strlen("true") || len == strlen("false"))
        if(strcmp(token, "true") == 0 || strcmp(token, "false") == 0)
            return DTREE_TOK_BOOLEAN;

    /* It could still be a number! */
    strtol(token, &end, 10);
    if (!*end) return DTREE_TOK_NUMERICAL;

    return DTREE_TOK_LITERAL;
}


dt_err dtree_decode_json(dtree *(*data), const char *json_data, size_t len)
{
    jsmn_parser parse;
    jsmn_init(&parse);

    // FIXME: Variable amount of tokens?
    jsmntok_t *tokens = malloc(sizeof(jsmntok_t) * len);
    memset(tokens, 0, sizeof(jsmntok_t) * len);

    int ret = jsmn_parse(&parse, json_data, strlen(json_data), tokens, sizeof(tokens) / sizeof(tokens[0]));

    jsmntok_t tok;
    unsigned int idx = 0;

    /** Prepare dtree nodes */
    dtree *root, *curr;
    dtree_malloc(&root);
    curr = root;

    struct bounds {
        int low, high;
    };

    struct pair {
        short   state;
#define TOK_PAIR_KEYED  1
#define TOK_PAIR_VALUED 2
        char    key[1024];
        union   value {
            char            string[1024];
            unsigned long   num;
        } value;
    };

    /* Save some space to store token bounds */
    struct bounds *bounds = malloc(sizeof(struct bounds) * len);
    memset(bounds, 0, sizeof(struct bounds) * len);
    int focused = -1;

    struct pair c_pair;
    memset(&c_pair, 0, sizeof(struct pair));

    while(tok = tokens[idx++], tok.type != NULL) {

        size_t tok_len = (size_t) tok.end - tok.start;
        char token[tok_len];
        memset(token, 0, tok_len);
        memcpy(token, json_data + tok.start, tok_len);

        /** Check if we need to move the boundry scope (again) */
        if(focused > 0 && tok.end >= bounds[focused].high) {
            focused--;

            /* Because of how our root node is a VALUE node, we need the parents parent */
            dtree *parent, *pair_parent;
            dtree_parent(root, curr, &parent);
            dtree_parent(root, parent, &pair_parent);

            /* Assign the new root node - old scope restored */
            curr = pair_parent;
        }

        switch(tok.type) {

             /**
             * When we encounter a new json object, shift our "focus" over by one so we can
             * record in what range this object is going to accumilate tokens.
             *
             * We then create a new child node under the current root node and switch the
             * curr root pointer over to that child.
             *
             * When we reach the end of the token scope, we need to re-reference the parent as
             * current root and switch over our boundry scope as well. This is done before the
             * parsing switch statement.
             */
            case JSMN_OBJECT:
            {
                focused++;
                bounds[focused].low = tok.start;
                bounds[focused].high = tok.end;

                /**
                 * Most of the time, we will create a new object under the key of
                 * a pair. This is the case, when the c_pair state buffer has been
                 * set to KEYED. In this case we allocate a new pair node for key
                 * and value and set that value to the new root.
                 */
                if(c_pair.state == TOK_PAIR_KEYED) {

                    /* Create pair nodes & new_root which becomes curr */
                    dtree *pair, *key, *val;
                    dtree_addlist(curr, &pair);
                    dtree_addpair(pair, &key, &val);

                    /* Assign key and new_root as a value of the pair */
                    dtree_addliteral(key, c_pair.key);

                    /* Move curr root pointer */
                    curr = val;

                    /* Blank c_pair data for next tokens */
                    memset(&c_pair, 0, sizeof(struct pair));

                }

                /* Skip to next token */
                continue;
            }

            case JSMN_STRING:
            {
                /**
                 * Here we need to check if we are adding a string as a key
                 * or as a value. This is simply done by checking for the existance
                 * of a key in the c_pair (current pair) variable.
                 *
                 * We know the token positions so we can manualy copy from the json stream
                 */
                 if(c_pair.state == 0) {
                     memcpy(c_pair.key, json_data + tok.start, (size_t) tok.end - tok.start);
                     c_pair.state = TOK_PAIR_KEYED;

                 } else if(c_pair.state == TOK_PAIR_KEYED){

                     /** Create a PAIR node under current root */
                     dtree *pair, *key, *val;
                     dtree_addlist(curr, &pair);
                     dtree_addpair(pair, &key, &val);

                     /* Key is always literal */
                     dtree_addliteral(key, c_pair.key);

                     /* Parse payload and asign to value node */
                     switch(digest_payload(token)) {
                         case DTREE_TOK_LITERAL:
                             dtree_addliteral(val, token);
                             break;

                         case DTREE_TOK_NUMERICAL:
                             dtree_addnumeral(val, atol(token));
                             break;

                         default: continue;
                     }

                     /* Blank c_pair data for next tokens */
                     memset(&c_pair, 0, sizeof(struct pair));
                 }

                /* Skip to next token */
                continue;
            }



            default:
                continue;
        }
    }

    /* Switch over data pointer and return */
    (*data) = root;
    return SUCCESS;
}