-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonedit.c
More file actions
294 lines (229 loc) · 6.2 KB
/
jsonedit.c
File metadata and controls
294 lines (229 loc) · 6.2 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "litejson.h"
#define READ_STDIN_MIN 12
#define IS_HELP(option) (tolower(option[1]) == 'h' || option[1] == '?')
#define IS_AN_OPTION(str) (strlen(str) >= 2 && str[0] == '-')
#define AUTOEXTEND_BUFFER(str, count, size) \
{ \
if ((count + 3) >= size) { \
size += READ_STDIN_MIN; \
str = realloc(str, sizeof(char) * size); \
} \
}
char* read_stdin(void) {
// prepare our autoextendable buffer
json_index_t size = READ_STDIN_MIN;
json_index_t count = 0;
char* result = calloc(size, sizeof(char));
while (feof(stdin) == 0) {
char current = fgetc(stdin);
// save current character
result[count++] = current;
result[count] = '\0';
// need to autoextend to prepare for more data
AUTOEXTEND_BUFFER(result, count, size)
}
return result;
}
/// reads the specified file path into a C string
char* read_file(const char* fn) {
if (!fn || (strlen(fn) < 2 && fn[0] == '-'))
return read_stdin(); // reading from stdin in this case
FILE* stream = fopen(fn, "r");
if (!stream) {
// no luck, sorry (or the file doesn't exist)
perror("Failed to open file for reading!!");
return NULL;
}
// first detect file size
json_index_t size = 0;
fseek(stream, 0L, SEEK_END);
size = ftell(stream);
rewind(stream);
// now read in everything into a buffer
char* result = calloc(size + 1, sizeof(char));
fread(result, sizeof(char), size, stream);
// clean up
fclose(stream);
return result;
}
char** split_get_query(const char* query, json_index_t* countP) {
json_index_t count = 0;
json_index_t size = READ_STDIN_MIN;
char** parts = calloc(size, sizeof(char*));
// again, autoextendable token components
json_index_t tCount, tSize = 0;
char* token = NULL;
// inside '\\'
bool insideEscape = false;
for (json_index_t i = 0; i < strlen(query); i++) {
if (!token) {
tCount = 0;
tSize = READ_STDIN_MIN;
token = calloc(tSize, sizeof(char));
}
char current = query[i];
if (current == '.' && !insideEscape) {
// end of token
if (tCount >= 1)
parts[count++] = token;
else
free(token); // empty token skipped
token = NULL;
} else {
if (current == '\\' && !insideEscape) {
// handle '\\'
insideEscape = true;
continue;
}
insideEscape = false;
token[tCount++] = current;
token[tCount] = '\0';
}
// autoextensions
if ((count + 1) >= size) {
size += READ_STDIN_MIN;
parts = realloc(parts, sizeof(char*));
}
AUTOEXTEND_BUFFER(token, tCount, tSize)
}
// add last stray token to parts
if (token && tCount >= 1)
parts[count++] = token;
if (countP)
(*countP) = count;
return parts;
}
void free_string_array(char** input, const json_index_t count) {
for (json_index_t i = 0; i < count; i++)
free(input[i]);
free(input);
}
bool is_a_number(const char* str) {
if (!str || strlen(str) < 1)
return false;
else {
for (json_index_t i = 0; i < strlen(str); i++) {
const char current = str[i];
if (current != '.' && current != '-' && current != '+' &&
isdigit(current) == 0)
return false;
}
return true;
}
}
json_value_ref find_json_value(json_value_ref root, const char* query) {
// split query into parts first
json_index_t partsCount = 0;
char** parts = split_get_query(query, &partsCount);
if (partsCount < 1)
return NULL; // invalid query
json_value_ref current = root;
for (json_index_t level = 0; level < partsCount; level++) {
char* label = parts[level];
if (is_a_number(label)) {
// this is an index, need to get the relevant item
int index = atoi(label);
if (json_value_get_type(current) == JSON_TYPE_ARRAY &&
index >= 0) {
json_value_ref first = json_value_get_at(current, (json_index_t)index);
if (!first) {
// out of bounds
fprintf(stderr, "Index out of bounds inside '%s' - asked for %u",
json_value_get_key(current), (json_index_t)index);
// clean up and fail
free_string_array(parts, partsCount);
return NULL;
} else {
// found it
current = first;
continue;
}
}
} else if (strlen(label) >= 2 && label[0] == '@') {
// TODO: implement special keys
fprintf(stderr, "TODO for '%s'\n", label);
free_string_array(parts, partsCount);
return NULL;
}
// otherwise look for the specified key
json_value_ref found = json_value_get(current, label);
if (!found) {
if (json_value_get_type(current) != JSON_TYPE_OBJECT)
fprintf(stderr, "\"%s\" is not an object, can't look for \"%s\"",
json_value_get_key(current),
label);
else
fprintf(stderr, "No item labeled \"%s\" inside \"%s\".",
label,
json_value_get_key(current));
// clean up and exit
free_string_array(parts, partsCount);
return NULL;
}
current = found;
}
free_string_array(parts, partsCount);
return current;
}
int show_help(const char* identity) {
fprintf(stderr, "Usage: %s -get KEY1.KEY2 FILENAME\n", identity);
fprintf(stderr, " %s -help\n", identity);
return 1;
}
int main(const int argc, const char** argv) {
const char* option = argv[1];
if (argc < 4 || !IS_AN_OPTION(option) || IS_HELP(option))
return show_help(argv[0]);
const char* query = argv[2];
const char* filename = argv[3];
// read in file contents and parse it first
char* raw = read_file(filename);
if (!raw)
return 1; // fail
json_error error;
json_value_ref root = json_parse(raw, &error);
if (error.fail) {
fprintf(stderr, "Parsing error - line %u, character %u, %s\n",
error.line, error.character, error.message);
// clean up and exit
free(raw);
return 2;
}
free(raw);
switch (option[1]) {
case 'g': {
// -get
json_value_ref result = find_json_value(root, query);
if (!result) {
// not found
json_value_release_tree(root);
return 3;
}
json_type_t type = json_value_get_type(result);
switch (type) {
case JSON_TYPE_ARRAY:
case JSON_TYPE_OBJECT: {
// containers need stringification first
char* strV = json_value_stringify(result, false);
printf("%s\n", strV);
free(strV);
break;
}
default: {
printf("%s\n", json_value_get_string(result));
break;
}
}
break;
}
default:
break;
}
// clean up
json_value_release_tree(root);
return 0;
}