From c954ef683bce2217fc81723ba79d0b53b01de2e2 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: Mon, 19 Jul 2021 12:03:06 +0800 Subject: Merge headers into common.h --- common.h | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 common.h (limited to 'common.h') diff --git a/common.h b/common.h new file mode 100644 index 0000000..bef5e73 --- /dev/null +++ b/common.h @@ -0,0 +1,102 @@ +#ifdef DEBUG +#include +#endif +#include +#include +#include +#include "pikul.h" + +extern struct shipping { + enum pikul_company company; + char *base; + struct curl_slist *headers; +} shipping; +extern json_tokener *tokener; + +void recurse(struct json_object *, const char *[], struct json_object **); + +enum type { SERVICES, ORDER }; + +inline void headers(const char *fields[], char *provisions[]) +{ + shipping.headers = NULL; + while (*fields) { + char header[strlen(*fields) + strlen(*provisions) + 2]; + sprintf(header, "%s:%s", *fields++, *provisions++); + shipping.headers = curl_slist_append(shipping.headers, header); + } +} + +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'; + fprintf(stderr, "%s\n", contents); +#endif + 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; + 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); + } + } + break; + case ORDER: + ; + struct json_object *string = NULL; + recurse(response, trail, &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