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
|
#include <string.h>
#include <json.h>
#include "pikul.h"
extern json_tokener *tokener;
void recurse(struct json_object *, const char *[], struct json_object **);
inline void handle_services(const char *contents, size_t num_bytes, const char *status_trail[],
const char *services_trail[], const char *attributes[], struct pikul_services **services)
{
json_object *response = json_tokener_parse_ex(tokener, contents, num_bytes);
enum json_tokener_error error = json_tokener_get_error(tokener);
if (!response) {
if (error == json_tokener_continue)
return;
else {
json_tokener_reset(tokener);
return;
}
} else if (!json_object_is_type(response, json_type_object) || error != json_tokener_success)
return;
struct json_object *status = NULL;
recurse(response, status_trail, &status);
if (json_object_get_int(status) != 200)
return;
struct json_object *objects = NULL;
recurse(response, services_trail, &objects);
size_t length = json_object_array_length(objects);
*services = malloc(sizeof(struct pikul_services)
+ sizeof(struct pikul_service *[length]));
(*services)->length = length;
enum { CODE, NAME, ETD, COST };
for (size_t i = 0; i < length; i++) {
(*services)->list[i] = malloc(sizeof(struct pikul_service));
struct pikul_service *service = (*services)->list[i];
json_object *object = json_object_array_get_idx(objects, i);
struct json_object_iterator iterator = json_object_iter_begin(object);
struct json_object_iterator iterator_end = json_object_iter_end(object);
while (!json_object_iter_equal(&iterator, &iterator_end)) {
const char *key = json_object_iter_peek_name(&iterator);
json_object *val = json_object_iter_peek_value(&iterator);
if (!strcmp(key, attributes[COST]))
service->cost = json_object_get_double(val);
else {
int len = json_object_get_string_len(val);
if (len) {
char *value = malloc(len + 1);
strcpy(value, json_object_get_string(val));
if (!strcmp(key, attributes[CODE]))
service->code = value;
else if (!strcmp(key, attributes[NAME]))
service->name = value;
else if (!strcmp(key, attributes[ETD]))
service->etd = value;
}
}
json_object_iter_next(&iterator);
}
}
}
|