-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.c
42 lines (36 loc) · 829 Bytes
/
json.c
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
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <regex.h>
char *jsonExtract(const char *tag, const char *data, bool subtable)
{
regex_t regex;
regmatch_t captures[2];
char reg[128];
int match;
int i;
char *result;
int capture_len;
if (subtable)
{
//TODO: this regex does not work with sub table
snprintf(reg, sizeof(reg), "\"%s\" *: *{\\([^}]*\\)}", tag);
}
else
{
snprintf(reg, sizeof(reg), "\"%s\" *: *\"\\([^\"]*\\)\"", tag);
}
regcomp(®ex, reg, 0);
result = NULL;
match = regexec(®ex, data, 2, captures, 0);
if (!match)
{
capture_len = captures[1].rm_eo - captures[1].rm_so;
result = (char *)malloc(capture_len + 1);
strncpy(result, data + captures[1].rm_so, capture_len);
result[capture_len] = '\0';
}
regfree(®ex);
return result;
}