Parse the JSON input (global) into an array of tokens. Returns the allocated token array. The number of tokens is passed back via num_tokens. */
| 215 | The number of tokens is passed back via num_tokens. |
| 216 | */ |
| 217 | static jsmntok_t *parse(int *num_tokens) |
| 218 | { |
| 219 | jsmn_parser parser; |
| 220 | size_t nalloc = 1024; /* tokens allocated */ |
| 221 | size_t nread = read_input(); |
| 222 | int ntokens; |
| 223 | jsmntok_t *tokens; |
| 224 | |
| 225 | if (debug) |
| 226 | fprintf(stderr, "(D): Parsing the input...\n"); |
| 227 | jsmn_init(&parser); |
| 228 | tokens = malloc(nalloc * sizeof(tokens[0])); |
| 229 | |
| 230 | restart: { |
| 231 | ntokens = jsmn_parse(&parser, input, nread, tokens, nalloc); |
| 232 | switch (ntokens) { |
| 233 | case JSMN_ERROR_NOMEM: |
| 234 | /* Reallocate and keep going. */ |
| 235 | if (debug) |
| 236 | fprintf(stderr, "(D): Reallocating tokens array.\n"); |
| 237 | nalloc <<= 1; |
| 238 | tokens = realloc((void *)tokens, nalloc * sizeof(tokens[0])); |
| 239 | goto restart; |
| 240 | |
| 241 | case JSMN_ERROR_INVAL: |
| 242 | fprintf(stderr, "(E): Invalid character in input.\n"); |
| 243 | exit(2); |
| 244 | |
| 245 | case JSMN_ERROR_PART: |
| 246 | /* Should never happen! */ |
| 247 | fprintf(stderr, "(E): Incomplete input, more bytes expected.\n"); |
| 248 | exit(3); |
| 249 | |
| 250 | default: |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 | *num_tokens = ntokens; |
| 255 | return tokens; |
| 256 | } |
| 257 | |
| 258 | /** Returns string representation for JSON type. */ |
| 259 | static const char *type(jsmntype_t t) |
no test coverage detected