This repository has been archived by the owner on Feb 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.c
82 lines (76 loc) · 2.06 KB
/
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
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
/*
* json.c
*
* Created on: Mar 11, 2020
* Author: Troi-Ryan Stoeffler
*/
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "json.h"
typedef struct JsonVar {
char name[20];
char value[20];
} JsonVar;
static JsonVar reportedVals[MAX_VAL];
static int currIndex = 0;
// simple json parser going through each thing
void parseJSON(char *json) {
bool quoteMode = false, colonMode = false, reportMode = false;
static char str[20] = "", strIndex = 0;
char i, *j;
j = json;
currIndex = 0;
for (i = 0; i < MAX_VAL; i++) {
reportedVals[i] = (JsonVar) { .name = "", .value = "" };
}
while (*j != '\0') {
if (quoteMode) {
if (strIndex < 20) {
str[strIndex++] = *j;
}
}
if (*j == '\"') {
if (!quoteMode) {
quoteMode = true;
strIndex = 0;
} else {
quoteMode = false;
str[strIndex - 1] = '\0';
if (strstr(str, "reported") != NULL) {
reportMode = true;
} else {
if (reportMode) {
if (currIndex < MAX_VAL) {
if (colonMode) {
strcpy(reportedVals[currIndex++].value, str);
colonMode = false;
} else {
strcpy(reportedVals[currIndex].name, str);
}
}
}
}
}
} else if (*j == ':') {
colonMode = true;
} else if (*j == '{') {
colonMode = false;
} else if (*j == '}') {
colonMode = false;
if (reportMode) {
return;
}
}
j++;
}
}
char* getValue(char *name) {
int i;
for (i = 0; i < currIndex; i++) {
if(strcmp(reportedVals[i].name, name) == 0) {
return reportedVals[i].value;
}
}
return NULL;
}