diff options
author | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-07-16 10:51:24 +0800 |
---|---|---|
committer | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-07-16 10:51:24 +0800 |
commit | 920878096a1721fd6e0c1b7008abac51803c5aa4 (patch) | |
tree | b31469427abeaf6c252bdad4ea5f61c8ddbe1fce | |
parent | 4298408d8e810ecfd22927987dc8482b0129466d (diff) |
Generalise the response handler
so the common part gets to be reused.
-rw-r--r-- | anteraja.c | 2 | ||||
-rw-r--r-- | handler.h | 75 | ||||
-rw-r--r-- | pikul.c | 4 |
3 files changed, 46 insertions, 35 deletions
@@ -37,7 +37,7 @@ size_t anteraja_services_handle(const char *contents, size_t size, size_t nmemb, struct pikul_services **services) { size_t realsize = size * nmemb; - handle_services(contents, realsize, status_trail, (const char *[]){ + handle(SERVICES, contents, realsize, status_trail, (const char *[]){ "content", "services", NULL @@ -8,8 +8,10 @@ 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 *trail[], const char *attributes[], struct pikul_services **services) +enum type { SERVICES, ORDER }; + +inline void handle(enum type type, const char *contents, size_t num_bytes, const char *status_trail[], + const char *trail[], const char *attributes[], void *data) { #ifdef DEBUG ((char *)contents)[num_bytes] = '\0'; @@ -30,37 +32,46 @@ inline void handle_services(const char *contents, size_t num_bytes, const char * recurse(response, status_trail, &status); if (json_object_get_int(status) != 200) return; - struct json_object *array = NULL; - recurse(response, trail, &array); - size_t length = json_object_array_length(array); - *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(array, 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 *name = json_object_iter_peek_name(&iterator); - json_object *value = json_object_iter_peek_value(&iterator); - if (!strcmp(name, attributes[COST])) - service->cost = json_object_get_double(value); - else { - int len = json_object_get_string_len(value); - if (len) { - char *string = malloc(len + 1); - strcpy(string, json_object_get_string(value)); - if (!strcmp(name, attributes[CODE])) - service->code = string; - else if (!strcmp(name, attributes[NAME])) - service->name = string; - else if (!strcmp(name, attributes[ETD])) - service->etd = string; + switch (type) { + case SERVICES: + ; + struct json_object *array = NULL; + recurse(response, trail, &array); + size_t length = json_object_array_length(array); + struct pikul_services **services = (struct pikul_services **)data; + *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(array, 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 *name = json_object_iter_peek_name(&iterator); + json_object *value = json_object_iter_peek_value(&iterator); + if (!strcmp(name, attributes[COST])) + service->cost = json_object_get_double(value); + else { + int len = json_object_get_string_len(value); + if (len) { + char *string = malloc(len + 1); + strcpy(string, json_object_get_string(value)); + if (!strcmp(name, attributes[CODE])) + service->code = string; + else if (!strcmp(name, attributes[NAME])) + service->name = string; + else if (!strcmp(name, attributes[ETD])) + service->etd = string; + } + } + json_object_iter_next(&iterator); } } - json_object_iter_next(&iterator); - } + break; + default: + break; } } @@ -6,8 +6,8 @@ json_tokener *tokener; struct shipping shipping; extern inline void headers(const char *[], char *[]); -extern inline void handle_services(const char *, size_t, const char *[], const char *[], const char *[], - struct pikul_services **); +extern inline void handle(enum type, const char *, size_t, const char *[], const char *[], const char *[], + void *); extern void anteraja_init(char *[]); extern void anteraja_services(const char *, const char *, double, char **, char **); |