From 920878096a1721fd6e0c1b7008abac51803c5aa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=EA=A6=AB=EA=A6=B6=EA=A6=8F=EA=A7=80=EA=A6=A6?= =?UTF-8?q?=EA=A6=BF=EA=A6=A7=EA=A6=AE=EA=A6=91=EA=A6=A9=EA=A6=AD=EA=A7=80?= Date: Fri, 16 Jul 2021 10:51:24 +0800 Subject: Generalise the response handler so the common part gets to be reused. --- handler.h | 75 ++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 32 deletions(-) (limited to 'handler.h') diff --git a/handler.h b/handler.h index 9c46975..065820c 100644 --- a/handler.h +++ b/handler.h @@ -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; } } -- cgit v1.2.3 From 6f78942176d5909349305db37b5424a1c6ceccef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=EA=A6=AB=EA=A6=B6=EA=A6=8F=EA=A7=80=EA=A6=A6?= =?UTF-8?q?=EA=A6=BF=EA=A6=A7=EA=A6=AE=EA=A6=91=EA=A6=A9=EA=A6=AD=EA=A7=80?= Date: Fri, 16 Jul 2021 11:02:02 +0800 Subject: Order functionality draft Not tested yet. Now the item object is still flattened as an array, initially to hurry the interfacing with the Perl module. But we were stuck with having to typemap char *** anyway, so we switch to SWIG because of the potential of ease. Still, we need to typemap char ***, but now that we're at SWIG, we might as well typemap a custom item struct. --- handler.h | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'handler.h') diff --git a/handler.h b/handler.h index 065820c..e940d91 100644 --- a/handler.h +++ b/handler.h @@ -71,6 +71,14 @@ inline void handle(enum type type, const char *contents, size_t num_bytes, const } } break; + case ORDER: + ; + struct json_object *string = NULL; + recurse(response, trail, &string); + char **waybill = (char **)data; + *waybill = malloc(json_object_get_string_len(string) + 1); + strcpy(*waybill, json_object_get_string(string)); + break; default: break; } -- cgit v1.2.3 From 2419d61c5171479586ba606834dcbfaa138c9bb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=EA=A6=AB=EA=A6=B6=EA=A6=8F=EA=A7=80=EA=A6=A6?= =?UTF-8?q?=EA=A6=BF=EA=A6=A7=EA=A6=AE=EA=A6=91=EA=A6=A9=EA=A6=AD=EA=A7=80?= Date: Sat, 17 Jul 2021 20:47:59 +0800 Subject: Rename waybill to tracking_number --- handler.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'handler.h') diff --git a/handler.h b/handler.h index e940d91..a2c031b 100644 --- a/handler.h +++ b/handler.h @@ -75,9 +75,9 @@ inline void handle(enum type type, const char *contents, size_t num_bytes, const ; struct json_object *string = NULL; recurse(response, trail, &string); - char **waybill = (char **)data; - *waybill = malloc(json_object_get_string_len(string) + 1); - strcpy(*waybill, json_object_get_string(string)); + char **tracking_number = (char **)data; + *tracking_number = malloc(json_object_get_string_len(string) + 1); + strcpy(*tracking_number, json_object_get_string(string)); break; default: break; -- cgit v1.2.3