From 7e15b32baaf737d28e0086b516b9ed307ab3f0c5 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:44:12 +0800 Subject: No need for shipping to be passed as pointer when it needs to be global anyway. --- anteraja.c | 20 +++++++++----------- pikul.c | 12 ++++++------ shipping.h | 10 +++++----- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/anteraja.c b/anteraja.c index 2fc265c..cd2932f 100644 --- a/anteraja.c +++ b/anteraja.c @@ -13,21 +13,19 @@ extern CURL *curl; static const char *status_trail[] = { "status", NULL }; -void anteraja_init(char *provisions[], struct shipping *shipping) +void anteraja_init(char *provisions[]) { - enum { BASE_PATH, ACCESS_KEY }; - shipping->base = malloc(strlen(provisions[BASE_PATH]) + 1); - strcpy(shipping->base, provisions[BASE_PATH]); - headers((const char *[]){ "access-key-id", "secret-access-key", NULL }, - &provisions[ACCESS_KEY], shipping); - shipping->headers = curl_slist_append(shipping->headers, "Content-Type:application/json"); + enum { BASE_PATH, ACCESS_KEY_ID, SECRET_ACCESS_KEY, PREFIX }; + shipping.base = malloc(strlen(provisions[BASE_PATH]) + 1); + strcpy(shipping.base, provisions[BASE_PATH]); + headers((const char *[]){ "access-key-id", "secret-access-key", NULL }, &provisions[ACCESS_KEY_ID]); + shipping.headers = curl_slist_append(shipping.headers, "Content-Type:application/json"); } -void anteraja_services(const char *origin, const char *destination, double weight, - struct shipping *shipping, char **url, char **post) +void anteraja_services(const char *origin, const char *destination, double weight, char **url, char **post) { - *url = malloc(strlen(shipping->base) + strlen(SERVICES_PATH) + 1); - sprintf(*url, "%s%s", shipping->base, SERVICES_PATH); + *url = malloc(strlen(shipping.base) + strlen(SERVICES_PATH) + 1); + sprintf(*url, "%s%s", shipping.base, SERVICES_PATH); *post = malloc(strlen(SERVICES_POST) + strlen(origin) + strlen(destination) + strlen("50000") - 2 * strlen("%s") - strlen("%d") + 1); sprintf(*post, SERVICES_POST, origin, destination, weight < 1.0 ? 1000 : (int)weight * 1000); diff --git a/pikul.c b/pikul.c index 1ccf717..f410006 100644 --- a/pikul.c +++ b/pikul.c @@ -3,14 +3,14 @@ CURL *curl; json_tokener *tokener; -static struct shipping shipping; +struct shipping shipping; -extern inline void headers(const char *[], char *[], struct 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 void anteraja_init(char *[], struct shipping *); -extern void anteraja_services(const char *, const char *, double, struct shipping *, char **, char **); +extern void anteraja_init(char *[]); +extern void anteraja_services(const char *, const char *, double, char **, char **); extern size_t anteraja_services_handle(const char *, size_t, size_t, struct pikul_services **); void pikul_init(enum pikul_company company, char *provisions[]) @@ -23,7 +23,7 @@ void pikul_init(enum pikul_company company, char *provisions[]) shipping.company = company; switch (company) { case PIKUL_ANTERAJA: - anteraja_init(provisions, &shipping); + anteraja_init(provisions); break; default: break; @@ -51,7 +51,7 @@ struct pikul_services *pikul_services(const char *origin, const char *destinatio size_t (*handler)(const char *, size_t, size_t, struct pikul_services **); switch (shipping.company) { case PIKUL_ANTERAJA: - anteraja_services(origin, destination, weight, &shipping, &url, &post); + anteraja_services(origin, destination, weight, &url, &post); handler = anteraja_services_handle; break; default: diff --git a/shipping.h b/shipping.h index e1c5ba5..37ac20f 100644 --- a/shipping.h +++ b/shipping.h @@ -2,18 +2,18 @@ #include #include "pikul.h" -struct shipping { +extern struct shipping { enum pikul_company company; char *base; struct curl_slist *headers; -}; +} shipping; -inline void headers(const char *fields[], char *provisions[], struct shipping *shipping) +inline void headers(const char *fields[], char *provisions[]) { - shipping->headers = NULL; + 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); + shipping.headers = curl_slist_append(shipping.headers, header); } } -- cgit v1.2.3 From 4298408d8e810ecfd22927987dc8482b0129466d 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:46:39 +0800 Subject: Services weight max length macro --- anteraja.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/anteraja.c b/anteraja.c index cd2932f..49d2802 100644 --- a/anteraja.c +++ b/anteraja.c @@ -8,6 +8,7 @@ \"destination\":\"%s\",\ \"weight\":%d\ }" +#define SERVICES_WEIGHT 5 extern CURL *curl; @@ -26,7 +27,7 @@ void anteraja_services(const char *origin, const char *destination, double weigh { *url = malloc(strlen(shipping.base) + strlen(SERVICES_PATH) + 1); sprintf(*url, "%s%s", shipping.base, SERVICES_PATH); - *post = malloc(strlen(SERVICES_POST) + strlen(origin) + strlen(destination) + strlen("50000") + *post = malloc(strlen(SERVICES_POST) + strlen(origin) + strlen(destination) + SERVICES_WEIGHT - 2 * strlen("%s") - strlen("%d") + 1); sprintf(*post, SERVICES_POST, origin, destination, weight < 1.0 ? 1000 : (int)weight * 1000); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post); -- cgit v1.2.3 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. --- anteraja.c | 2 +- handler.h | 75 +++++++++++++++++++++++++++++++++++--------------------------- pikul.c | 4 ++-- 3 files changed, 46 insertions(+), 35 deletions(-) diff --git a/anteraja.c b/anteraja.c index 49d2802..06a9b24 100644 --- a/anteraja.c +++ b/anteraja.c @@ -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 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; } } diff --git a/pikul.c b/pikul.c index f410006..5dd0750 100644 --- a/pikul.c +++ b/pikul.c @@ -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 **); -- cgit v1.2.3 From d1b9c27a403fb2ac9e69e82ebea32722c6795daf 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:55:51 +0800 Subject: SWIG files, therefore deprecating Pikul-XS --- .gitignore | 3 +++ Makefile.PL | 7 +++++++ pikul.i | 28 ++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 Makefile.PL create mode 100644 pikul.i diff --git a/.gitignore b/.gitignore index d367ba6..3c5fb6d 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,6 @@ install-sh ltmain.sh Makefile.in missing +*.old +*.p* +*_wrap.c diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..2344c9c --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,7 @@ +use ExtUtils::MakeMaker; +WriteMakefile( + NAME => 'Pikul', + INC => '`pkg-config --cflags json-c`', + LIBS => ['-lpikul -lcurl -ljson-c'], + OBJECT => '$(O_FILES)' +); diff --git a/pikul.i b/pikul.i new file mode 100644 index 0000000..b00709e --- /dev/null +++ b/pikul.i @@ -0,0 +1,28 @@ +%module Pikul +%{ +#include "pikul.h" +%} + +%typemap(in) char *[] { + AV *tempav = (AV *)SvRV($input); + I32 len = av_len(tempav); + $1 = (char **)malloc((len + 2) * sizeof(char *)); + int i; + for (i = 0; i <= len; i++) { + SV **tv = av_fetch(tempav, i, 0); + $1[i] = (char *)SvPV(*tv, PL_na); + } + $1[i] = NULL; +}; +%typemap(freearg) char *[] { + free($1); +} + +%rename("%(strip:[pikul_])s") ""; +void pikul_init(enum pikul_company company, char *provisions[]); +double pikul_cost(const char *origin, const char *destination, double weight, const char *service); +char *pikul_order(const char *trx_id, const char *service, const char *sender_name, + const char *sender_phone, const char *origin, const char *sender_address, + const char *receiver_name, const char *receiver_phone, const char *destination, + const char *receiver_address, int nitems, char **items[], double value); +void pikul_cleanup(); -- 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. --- anteraja.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ configure.ac | 1 + handler.h | 8 ++++++ pikul.c | 43 +++++++++++++++++++++++++++ pikul.h | 4 +++ 5 files changed, 150 insertions(+) diff --git a/anteraja.c b/anteraja.c index 06a9b24..aad6d04 100644 --- a/anteraja.c +++ b/anteraja.c @@ -10,9 +10,45 @@ }" #define SERVICES_WEIGHT 5 +#define ORDER_PATH "order" +#define ORDER_POST \ +"{\ +\"booking_id\":\"%s-%s\",\ +\"service_code\":\"%s\",\ +\"shipper\":{\ +\"name\":\"%s\",\ +\"phone\":\"%s\",\ +\"district\":\"%s\",\ +\"address\":\"%s\"\ +},\ +\"receiver\":{\ +\"name\":\"%s\",\ +\"phone\":\"%s\",\ +\"district\":\"%s\",\ +\"address\":\"%s\"\ +},\ +\"items\":[\ +%s\ +],\ +\"declared_value\":%d\ +}" +#define ORDER_SUBTOTAL 9 + +#define ORDER_ITEM \ +"{\ +\"item_name\":\"%s\",\ +\"item_quantity\":%d,\ +\"declared_value\":%d,\ +\"weight\":%d\ +}" +#define ORDER_ITEM_QUANTITY 2 +#define ORDER_ITEM_PRICE 9 +#define ORDER_ITEM_WEIGHT 5 + extern CURL *curl; static const char *status_trail[] = { "status", NULL }; +static char *prefix = NULL; void anteraja_init(char *provisions[]) { @@ -21,6 +57,10 @@ void anteraja_init(char *provisions[]) strcpy(shipping.base, provisions[BASE_PATH]); headers((const char *[]){ "access-key-id", "secret-access-key", NULL }, &provisions[ACCESS_KEY_ID]); shipping.headers = curl_slist_append(shipping.headers, "Content-Type:application/json"); + if (provisions[PREFIX]) { + prefix = malloc(strlen(provisions[PREFIX]) + 1); + strcpy(prefix, provisions[PREFIX]); + } } void anteraja_services(const char *origin, const char *destination, double weight, char **url, char **post) @@ -49,3 +89,57 @@ size_t anteraja_services_handle(const char *contents, size_t size, size_t nmemb, }, services); return realsize; } + +void anteraja_order(const char *trx_id, const char *service, const char *sender_name, + const char *sender_phone, const char *origin, const char *sender_address, + const char *receiver_name, const char *receiver_phone, const char *destination, + const char *receiver_address, int nitems, char **items[], double subtotal, + char **url, char **post) +{ + *url = malloc(strlen(shipping.base) + strlen(ORDER_PATH) + 1); + sprintf(*url, "%s%s", shipping.base, ORDER_PATH); + enum { SKU, QUANTITY, DESCRIPTION, PRICE, WEIGHT }; + char *json = NULL; + for (int i = 0; i < nitems; i++) { + size_t length = strlen(ORDER_ITEM) + strlen(items[i][DESCRIPTION]) + ORDER_ITEM_QUANTITY + + ORDER_ITEM_PRICE + ORDER_ITEM_WEIGHT - strlen("%s") - 3 * strlen("%d") + 1; + char item[length]; + sprintf(item, ORDER_ITEM, items[i][DESCRIPTION], atoi(items[i][QUANTITY]), + atoi(items[i][PRICE]), atoi(items[i][WEIGHT]) * 1000); + if (json) + json = realloc(json, strlen(json) + length); + else + json = malloc(length); + strcat(json, item); + if (i + 1 < nitems) + json[length] = ','; + else + json[length] = '\0'; + } + *post = malloc(strlen(ORDER_POST) + strlen(prefix) + strlen(trx_id) + strlen(service) + + strlen(sender_name) + strlen(sender_phone) + strlen(origin) + + strlen(sender_address) + strlen(receiver_name) + strlen(receiver_phone) + + strlen(destination) + strlen(receiver_address) + strlen(json) + ORDER_SUBTOTAL + - 12 * strlen("%s") - strlen("%d") + 1); + sprintf(*post, ORDER_POST, prefix, trx_id, service, sender_name, sender_phone, origin, + sender_address, receiver_name, receiver_phone, destination, receiver_address, json, + (int)subtotal); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post); +} + +size_t anteraja_order_handle(const char *contents, size_t size, size_t nmemb, char **waybill) +{ + size_t realsize = size * nmemb; + handle(ORDER, contents, realsize, status_trail, (const char *[]){ + "content", + "waybill_no", + NULL + }, NULL, waybill); + return realsize; +} + +void anteraja_cleanup() +{ + if (prefix) + free(prefix); +} diff --git a/configure.ac b/configure.ac index c6d63c1..5dbace7 100644 --- a/configure.ac +++ b/configure.ac @@ -7,6 +7,7 @@ PKG_CHECK_MODULES([DEPS], [libcurl json-c]) AC_TYPE_SIZE_T AC_C_INLINE AC_FUNC_MALLOC +AC_FUNC_REALLOC AC_CHECK_FUNCS([memset]) AC_CONFIG_HEADERS([config.h]) AC_CONFIG_FILES([Makefile]) 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; } diff --git a/pikul.c b/pikul.c index 5dd0750..d299cbe 100644 --- a/pikul.c +++ b/pikul.c @@ -4,6 +4,7 @@ CURL *curl; json_tokener *tokener; struct shipping shipping; +static char *waybill = NULL; extern inline void headers(const char *[], char *[]); extern inline void handle(enum type, const char *, size_t, const char *[], const char *[], const char *[], @@ -12,6 +13,11 @@ extern inline void handle(enum type, const char *, size_t, const char *[], const extern void anteraja_init(char *[]); extern void anteraja_services(const char *, const char *, double, char **, char **); extern size_t anteraja_services_handle(const char *, size_t, size_t, struct pikul_services **); +extern void anteraja_order(const char *, const char *, const char *, const char *, const char *, + const char *, const char *, const char *, const char *, const char *, int, + char **[], double, char **, char **); +extern size_t anteraja_order_handle(const char *, size_t size, size_t nmemb, char **); +extern void anteraja_cleanup(); void pikul_init(enum pikul_company company, char *provisions[]) { @@ -106,8 +112,45 @@ double pikul_cost(const char *origin, const char *destination, double weight, co return cost; } +char *pikul_order(const char *trx_id, const char *service, const char *sender_name, + const char *sender_phone, const char *origin, const char *sender_address, + const char *receiver_name, const char *receiver_phone, const char *destination, + const char *receiver_address, int nitems, char **items[], double subtotal) +{ + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &waybill); + char *url; + char *post = NULL; + size_t (*handler)(const char *, size_t, size_t, char **); + switch (shipping.company) { + case PIKUL_ANTERAJA: + anteraja_order(trx_id, service, sender_name, sender_phone, origin, sender_address, + receiver_name, receiver_phone, destination, receiver_address, + nitems, items, subtotal, &url, &post); + handler = anteraja_order_handle; + break; + default: + break; + } + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handler); + curl_easy_perform(curl); + if (post) + free(post); + free(url); + return waybill; +} + void pikul_cleanup() { + switch (shipping.company) { + case PIKUL_ANTERAJA: + anteraja_cleanup(); + break; + default: + break; + } + if (waybill) + free(waybill); free(shipping.base); json_tokener_free(tokener); curl_slist_free_all(shipping.headers); diff --git a/pikul.h b/pikul.h index 4cd11f7..d88d218 100644 --- a/pikul.h +++ b/pikul.h @@ -25,6 +25,10 @@ void pikul_init(enum pikul_company company, char *provisions[]); struct pikul_services *pikul_services(const char *origin, const char *destination, double weight); void pikul_free_services(struct pikul_services *services); double pikul_cost(const char *origin, const char *destination, double weight, const char *service); +char *pikul_order(const char *trx_id, const char *service, const char *sender_name, + const char *sender_phone, const char *origin, const char *sender_address, + const char *receiver_name, const char *receiver_phone, const char *destination, + const char *receiver_address, int nitems, char **items[], double value); void pikul_cleanup(); #ifdef __cplusplus -- cgit v1.2.3 From abe673f9ae7d9d23e92a188ade187ec020b1f67a 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 12:01:56 +0800 Subject: Update value parameter to subtotal --- pikul.h | 2 +- pikul.i | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pikul.h b/pikul.h index d88d218..0bb4e23 100644 --- a/pikul.h +++ b/pikul.h @@ -28,7 +28,7 @@ double pikul_cost(const char *origin, const char *destination, double weight, co char *pikul_order(const char *trx_id, const char *service, const char *sender_name, const char *sender_phone, const char *origin, const char *sender_address, const char *receiver_name, const char *receiver_phone, const char *destination, - const char *receiver_address, int nitems, char **items[], double value); + const char *receiver_address, int nitems, char **items[], double subtotal); void pikul_cleanup(); #ifdef __cplusplus diff --git a/pikul.i b/pikul.i index b00709e..6a0882b 100644 --- a/pikul.i +++ b/pikul.i @@ -24,5 +24,5 @@ double pikul_cost(const char *origin, const char *destination, double weight, co char *pikul_order(const char *trx_id, const char *service, const char *sender_name, const char *sender_phone, const char *origin, const char *sender_address, const char *receiver_name, const char *receiver_phone, const char *destination, - const char *receiver_address, int nitems, char **items[], double value); + const char *receiver_address, int nitems, char **items[], double subtotal); void pikul_cleanup(); -- cgit v1.2.3 From b018ae643dbba21c0060ccb0748a7b239b4ecb5a 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 21:11:36 +0800 Subject: Initialise items JSON before concatenating --- anteraja.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/anteraja.c b/anteraja.c index aad6d04..ad194f5 100644 --- a/anteraja.c +++ b/anteraja.c @@ -108,8 +108,10 @@ void anteraja_order(const char *trx_id, const char *service, const char *sender_ atoi(items[i][PRICE]), atoi(items[i][WEIGHT]) * 1000); if (json) json = realloc(json, strlen(json) + length); - else + else { json = malloc(length); + memset(json, '\0', strlen(json)); + } strcat(json, item); if (i + 1 < nitems) json[length] = ','; -- cgit v1.2.3 From 3500f0a5cd000ff0d650ab716f92a35dcf1c1581 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 22:29:59 +0800 Subject: Fix the truncated items JSON By making sure it's always concatenated with a null terminated string, AND fix the index that gets null terminated in the end. --- anteraja.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/anteraja.c b/anteraja.c index ad194f5..c64ac68 100644 --- a/anteraja.c +++ b/anteraja.c @@ -102,21 +102,22 @@ void anteraja_order(const char *trx_id, const char *service, const char *sender_ char *json = NULL; for (int i = 0; i < nitems; i++) { size_t length = strlen(ORDER_ITEM) + strlen(items[i][DESCRIPTION]) + ORDER_ITEM_QUANTITY - + ORDER_ITEM_PRICE + ORDER_ITEM_WEIGHT - strlen("%s") - 3 * strlen("%d") + 1; - char item[length]; + + ORDER_ITEM_PRICE + ORDER_ITEM_WEIGHT - strlen("%s") - 3 * strlen("%d") + + strlen(","); + char item[length + 1]; sprintf(item, ORDER_ITEM, items[i][DESCRIPTION], atoi(items[i][QUANTITY]), atoi(items[i][PRICE]), atoi(items[i][WEIGHT]) * 1000); if (json) - json = realloc(json, strlen(json) + length); + json = realloc(json, strlen(json) + length + 1); else { - json = malloc(length); + json = malloc(length + 1); memset(json, '\0', strlen(json)); } strcat(json, item); if (i + 1 < nitems) - json[length] = ','; + strcat(json, ","); else - json[length] = '\0'; + json[strlen(json)] = '\0'; } *post = malloc(strlen(ORDER_POST) + strlen(prefix) + strlen(trx_id) + strlen(service) + strlen(sender_name) + strlen(sender_phone) + strlen(origin) -- cgit v1.2.3 From 6960f565208af11828920f262d201e9db7a9aba4 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 22:32:55 +0800 Subject: parcel_total_weight turns out to be required We use Interchange as a reference, and the unit weight is not saved to database for the transaction, only shipping cost. So to make it convenient for the library users, the total weight calculation is done by the library, so no need to pass any total weight argument. --- anteraja.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/anteraja.c b/anteraja.c index c64ac68..8b78815 100644 --- a/anteraja.c +++ b/anteraja.c @@ -15,6 +15,7 @@ "{\ \"booking_id\":\"%s-%s\",\ \"service_code\":\"%s\",\ +\"parcel_total_weight\":%d,\ \"shipper\":{\ \"name\":\"%s\",\ \"phone\":\"%s\",\ @@ -32,6 +33,7 @@ ],\ \"declared_value\":%d\ }" +#define ORDER_WEIGHT 5 #define ORDER_SUBTOTAL 9 #define ORDER_ITEM \ @@ -100,13 +102,18 @@ void anteraja_order(const char *trx_id, const char *service, const char *sender_ sprintf(*url, "%s%s", shipping.base, ORDER_PATH); enum { SKU, QUANTITY, DESCRIPTION, PRICE, WEIGHT }; char *json = NULL; + double total_weight = 0; for (int i = 0; i < nitems; i++) { size_t length = strlen(ORDER_ITEM) + strlen(items[i][DESCRIPTION]) + ORDER_ITEM_QUANTITY + ORDER_ITEM_PRICE + ORDER_ITEM_WEIGHT - strlen("%s") - 3 * strlen("%d") + strlen(","); char item[length + 1]; + double weight = atof(items[i][WEIGHT]) * 1000.0; + if (weight < 100.0) + weight = 100.0; sprintf(item, ORDER_ITEM, items[i][DESCRIPTION], atoi(items[i][QUANTITY]), - atoi(items[i][PRICE]), atoi(items[i][WEIGHT]) * 1000); + atoi(items[i][PRICE]), (int)weight); + total_weight += weight; if (json) json = realloc(json, strlen(json) + length + 1); else { @@ -120,13 +127,13 @@ void anteraja_order(const char *trx_id, const char *service, const char *sender_ json[strlen(json)] = '\0'; } *post = malloc(strlen(ORDER_POST) + strlen(prefix) + strlen(trx_id) + strlen(service) - + strlen(sender_name) + strlen(sender_phone) + strlen(origin) + + ORDER_WEIGHT + strlen(sender_name) + strlen(sender_phone) + strlen(origin) + strlen(sender_address) + strlen(receiver_name) + strlen(receiver_phone) + strlen(destination) + strlen(receiver_address) + strlen(json) + ORDER_SUBTOTAL - - 12 * strlen("%s") - strlen("%d") + 1); - sprintf(*post, ORDER_POST, prefix, trx_id, service, sender_name, sender_phone, origin, - sender_address, receiver_name, receiver_phone, destination, receiver_address, json, - (int)subtotal); + - 12 * strlen("%s") - 2 * strlen("%d") + 1); + sprintf(*post, ORDER_POST, prefix, trx_id, service, (int)total_weight, sender_name, sender_phone, + origin, sender_address, receiver_name, receiver_phone, destination, + receiver_address, json, (int)subtotal); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post); } -- cgit v1.2.3 From 4482f46ee073110c7c52d73bc8a8006437eba6f2 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 23:02:24 +0800 Subject: use_insurance turns out to be required This one follows the example given for the boolean JSON value, which is written as true or false. The response is error 500, internal server error. Despite the example, it says max length is 1 in the middle part of the doc, so maybe it should be written as 1 or 0. Will be tried next. --- anteraja.c | 10 ++++++---- pikul.c | 6 +++--- pikul.h | 2 +- pikul.i | 2 +- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/anteraja.c b/anteraja.c index 8b78815..65f7206 100644 --- a/anteraja.c +++ b/anteraja.c @@ -31,9 +31,11 @@ \"items\":[\ %s\ ],\ +\"use_insurance\":%s,\ \"declared_value\":%d\ }" #define ORDER_WEIGHT 5 +#define ORDER_INSURANCE strlen("false") #define ORDER_SUBTOTAL 9 #define ORDER_ITEM \ @@ -95,7 +97,7 @@ size_t anteraja_services_handle(const char *contents, size_t size, size_t nmemb, void anteraja_order(const char *trx_id, const char *service, const char *sender_name, const char *sender_phone, const char *origin, const char *sender_address, const char *receiver_name, const char *receiver_phone, const char *destination, - const char *receiver_address, int nitems, char **items[], double subtotal, + const char *receiver_address, int nitems, char **items[], _Bool insurance, double subtotal, char **url, char **post) { *url = malloc(strlen(shipping.base) + strlen(ORDER_PATH) + 1); @@ -129,11 +131,11 @@ void anteraja_order(const char *trx_id, const char *service, const char *sender_ *post = malloc(strlen(ORDER_POST) + strlen(prefix) + strlen(trx_id) + strlen(service) + ORDER_WEIGHT + strlen(sender_name) + strlen(sender_phone) + strlen(origin) + strlen(sender_address) + strlen(receiver_name) + strlen(receiver_phone) - + strlen(destination) + strlen(receiver_address) + strlen(json) + ORDER_SUBTOTAL - - 12 * strlen("%s") - 2 * strlen("%d") + 1); + + strlen(destination) + strlen(receiver_address) + strlen(json) + ORDER_INSURANCE + + ORDER_SUBTOTAL - 13 * strlen("%s") - 2 * strlen("%d") + 1); sprintf(*post, ORDER_POST, prefix, trx_id, service, (int)total_weight, sender_name, sender_phone, origin, sender_address, receiver_name, receiver_phone, destination, - receiver_address, json, (int)subtotal); + receiver_address, json, insurance ? "true" : "false", (int)subtotal); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post); } diff --git a/pikul.c b/pikul.c index d299cbe..a08b101 100644 --- a/pikul.c +++ b/pikul.c @@ -15,7 +15,7 @@ extern void anteraja_services(const char *, const char *, double, char **, char extern size_t anteraja_services_handle(const char *, size_t, size_t, struct pikul_services **); extern void anteraja_order(const char *, const char *, const char *, const char *, const char *, const char *, const char *, const char *, const char *, const char *, int, - char **[], double, char **, char **); + char **[], _Bool, double, char **, char **); extern size_t anteraja_order_handle(const char *, size_t size, size_t nmemb, char **); extern void anteraja_cleanup(); @@ -115,7 +115,7 @@ double pikul_cost(const char *origin, const char *destination, double weight, co char *pikul_order(const char *trx_id, const char *service, const char *sender_name, const char *sender_phone, const char *origin, const char *sender_address, const char *receiver_name, const char *receiver_phone, const char *destination, - const char *receiver_address, int nitems, char **items[], double subtotal) + const char *receiver_address, int nitems, char **items[], _Bool insurance, double subtotal) { curl_easy_setopt(curl, CURLOPT_WRITEDATA, &waybill); char *url; @@ -125,7 +125,7 @@ char *pikul_order(const char *trx_id, const char *service, const char *sender_na case PIKUL_ANTERAJA: anteraja_order(trx_id, service, sender_name, sender_phone, origin, sender_address, receiver_name, receiver_phone, destination, receiver_address, - nitems, items, subtotal, &url, &post); + nitems, items, insurance, subtotal, &url, &post); handler = anteraja_order_handle; break; default: diff --git a/pikul.h b/pikul.h index 0bb4e23..e2c6331 100644 --- a/pikul.h +++ b/pikul.h @@ -28,7 +28,7 @@ double pikul_cost(const char *origin, const char *destination, double weight, co char *pikul_order(const char *trx_id, const char *service, const char *sender_name, const char *sender_phone, const char *origin, const char *sender_address, const char *receiver_name, const char *receiver_phone, const char *destination, - const char *receiver_address, int nitems, char **items[], double subtotal); + const char *receiver_address, int nitems, char **items[], _Bool insurance, double subtotal); void pikul_cleanup(); #ifdef __cplusplus diff --git a/pikul.i b/pikul.i index 6a0882b..05b45c9 100644 --- a/pikul.i +++ b/pikul.i @@ -24,5 +24,5 @@ double pikul_cost(const char *origin, const char *destination, double weight, co char *pikul_order(const char *trx_id, const char *service, const char *sender_name, const char *sender_phone, const char *origin, const char *sender_address, const char *receiver_name, const char *receiver_phone, const char *destination, - const char *receiver_address, int nitems, char **items[], double subtotal); + const char *receiver_address, int nitems, char **items[], _Bool insurance, double subtotal); void pikul_cleanup(); -- cgit v1.2.3 From b6abbff926e9ff9908935224c1655d94dcc09441 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 23:28:28 +0800 Subject: Typemap for char **[] Not tested yet. Here so this compiles up to Perl module. --- pikul.i | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pikul.i b/pikul.i index 05b45c9..69f582a 100644 --- a/pikul.i +++ b/pikul.i @@ -18,6 +18,27 @@ free($1); } +%typemap(in) char **[] { + AV *tempav = (AV *)SvRV($input); + I32 len = av_len(tempav); + $1 = (char ***)malloc((len + 2) * sizeof(char **)); + int i; + for (i = 0; i <= len; i++) { + AV *av = (AV *)av_fetch(tempav, i, 0); + I32 avlen = av_len(av); + $1[i] = (char **)malloc((avlen + 2) * sizeof(char *)); + int j; + for (j = 0; j <= avlen; j++) { + SV **tv = av_fetch(av, j, 0); + $1[i][j] = (char *)SvPV(*tv, PL_na); + } + $1[i][j] = NULL; + } +}; +%typemap(freearg) char **[] { + free($1); +} + %rename("%(strip:[pikul_])s") ""; void pikul_init(enum pikul_company company, char *provisions[]); double pikul_cost(const char *origin, const char *destination, double weight, const char *service); -- cgit v1.2.3 From 0930b738f5261d4e2630242ca5fa2a6bc63026ff 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 09:41:41 +0800 Subject: Finally able to typemap char **[] right For now the item object is still flattened. --- pikul.i | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/pikul.i b/pikul.i index 69f582a..a4215e9 100644 --- a/pikul.i +++ b/pikul.i @@ -19,21 +19,23 @@ } %typemap(in) char **[] { - AV *tempav = (AV *)SvRV($input); - I32 len = av_len(tempav); - $1 = (char ***)malloc((len + 2) * sizeof(char **)); + AV *items = (AV *)SvRV($input); + I32 nitems_min1 = av_len(items); + $1 = (char ***)malloc((nitems_min1 + 2) * sizeof(char **)); int i; - for (i = 0; i <= len; i++) { - AV *av = (AV *)av_fetch(tempav, i, 0); - I32 avlen = av_len(av); - $1[i] = (char **)malloc((avlen + 2) * sizeof(char *)); + for (i = 0; i <= nitems_min1; i++) { + SV **refptr = av_fetch(items, i, 0); + AV *flat = (AV *)SvRV(*refptr); + I32 nattrs_min1 = av_len(flat); + $1[i] = (char **)malloc((nattrs_min1 + 2) * sizeof(char *)); int j; - for (j = 0; j <= avlen; j++) { - SV **tv = av_fetch(av, j, 0); + for (j = 0; j <= nattrs_min1; j++) { + SV **tv = av_fetch(flat, j, 0); $1[i][j] = (char *)SvPV(*tv, PL_na); } $1[i][j] = NULL; } + $1[i] = NULL; }; %typemap(freearg) char **[] { free($1); -- cgit v1.2.3 From 3eb347f32335a58c681d1777dfeef8c6dc1c76d5 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 09:52:30 +0800 Subject: _Bool typemap --- pikul.i | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pikul.i b/pikul.i index a4215e9..e2d363f 100644 --- a/pikul.i +++ b/pikul.i @@ -41,6 +41,10 @@ free($1); } +%typemap(in) _Bool { + $1 = $input; +} + %rename("%(strip:[pikul_])s") ""; void pikul_init(enum pikul_company company, char *provisions[]); double pikul_cost(const char *origin, const char *destination, double weight, const char *service); -- cgit v1.2.3 From dfa57092e26c242c2ab64dc1d029e5dff44eee67 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 11:05:19 +0800 Subject: Rename trx_id to order_number --- anteraja.c | 8 ++++---- pikul.c | 8 ++++---- pikul.h | 2 +- pikul.i | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/anteraja.c b/anteraja.c index 65f7206..b49c07c 100644 --- a/anteraja.c +++ b/anteraja.c @@ -94,7 +94,7 @@ size_t anteraja_services_handle(const char *contents, size_t size, size_t nmemb, return realsize; } -void anteraja_order(const char *trx_id, const char *service, const char *sender_name, +void anteraja_order(const char *order_number, const char *service, const char *sender_name, const char *sender_phone, const char *origin, const char *sender_address, const char *receiver_name, const char *receiver_phone, const char *destination, const char *receiver_address, int nitems, char **items[], _Bool insurance, double subtotal, @@ -128,13 +128,13 @@ void anteraja_order(const char *trx_id, const char *service, const char *sender_ else json[strlen(json)] = '\0'; } - *post = malloc(strlen(ORDER_POST) + strlen(prefix) + strlen(trx_id) + strlen(service) + *post = malloc(strlen(ORDER_POST) + strlen(prefix) + strlen(order_number) + strlen(service) + ORDER_WEIGHT + strlen(sender_name) + strlen(sender_phone) + strlen(origin) + strlen(sender_address) + strlen(receiver_name) + strlen(receiver_phone) + strlen(destination) + strlen(receiver_address) + strlen(json) + ORDER_INSURANCE + ORDER_SUBTOTAL - 13 * strlen("%s") - 2 * strlen("%d") + 1); - sprintf(*post, ORDER_POST, prefix, trx_id, service, (int)total_weight, sender_name, sender_phone, - origin, sender_address, receiver_name, receiver_phone, destination, + sprintf(*post, ORDER_POST, prefix, order_number, service, (int)total_weight, sender_name, + sender_phone, origin, sender_address, receiver_name, receiver_phone, destination, receiver_address, json, insurance ? "true" : "false", (int)subtotal); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post); } diff --git a/pikul.c b/pikul.c index a08b101..5f04136 100644 --- a/pikul.c +++ b/pikul.c @@ -112,7 +112,7 @@ double pikul_cost(const char *origin, const char *destination, double weight, co return cost; } -char *pikul_order(const char *trx_id, const char *service, const char *sender_name, +char *pikul_order(const char *order_number, const char *service, const char *sender_name, const char *sender_phone, const char *origin, const char *sender_address, const char *receiver_name, const char *receiver_phone, const char *destination, const char *receiver_address, int nitems, char **items[], _Bool insurance, double subtotal) @@ -123,9 +123,9 @@ char *pikul_order(const char *trx_id, const char *service, const char *sender_na size_t (*handler)(const char *, size_t, size_t, char **); switch (shipping.company) { case PIKUL_ANTERAJA: - anteraja_order(trx_id, service, sender_name, sender_phone, origin, sender_address, - receiver_name, receiver_phone, destination, receiver_address, - nitems, items, insurance, subtotal, &url, &post); + anteraja_order(order_number, service, sender_name, sender_phone, origin, + sender_address, receiver_name, receiver_phone, destination, + receiver_address, nitems, items, insurance, subtotal, &url, &post); handler = anteraja_order_handle; break; default: diff --git a/pikul.h b/pikul.h index e2c6331..623c87a 100644 --- a/pikul.h +++ b/pikul.h @@ -25,7 +25,7 @@ void pikul_init(enum pikul_company company, char *provisions[]); struct pikul_services *pikul_services(const char *origin, const char *destination, double weight); void pikul_free_services(struct pikul_services *services); double pikul_cost(const char *origin, const char *destination, double weight, const char *service); -char *pikul_order(const char *trx_id, const char *service, const char *sender_name, +char *pikul_order(const char *order_number, const char *service, const char *sender_name, const char *sender_phone, const char *origin, const char *sender_address, const char *receiver_name, const char *receiver_phone, const char *destination, const char *receiver_address, int nitems, char **items[], _Bool insurance, double subtotal); diff --git a/pikul.i b/pikul.i index e2d363f..9defe86 100644 --- a/pikul.i +++ b/pikul.i @@ -48,7 +48,7 @@ %rename("%(strip:[pikul_])s") ""; void pikul_init(enum pikul_company company, char *provisions[]); double pikul_cost(const char *origin, const char *destination, double weight, const char *service); -char *pikul_order(const char *trx_id, const char *service, const char *sender_name, +char *pikul_order(const char *order_number, const char *service, const char *sender_name, const char *sender_phone, const char *origin, const char *sender_address, const char *receiver_name, const char *receiver_phone, const char *destination, const char *receiver_address, int nitems, char **items[], _Bool insurance, double subtotal); -- cgit v1.2.3 From b999dd2f128bcf209c62d3419a559e70222e7b04 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 15:29:20 +0800 Subject: Postal codes for both sender & receiver --- anteraja.c | 24 ++++++++++++++---------- pikul.c | 14 ++++++++------ pikul.h | 5 +++-- pikul.i | 5 +++-- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/anteraja.c b/anteraja.c index b49c07c..89239ab 100644 --- a/anteraja.c +++ b/anteraja.c @@ -20,13 +20,15 @@ \"name\":\"%s\",\ \"phone\":\"%s\",\ \"district\":\"%s\",\ -\"address\":\"%s\"\ +\"address\":\"%s\",\ +\"postcode\":\"%s\"\ },\ \"receiver\":{\ \"name\":\"%s\",\ \"phone\":\"%s\",\ \"district\":\"%s\",\ -\"address\":\"%s\"\ +\"address\":\"%s\",\ +\"postcode\":\"%s\"\ },\ \"items\":[\ %s\ @@ -96,9 +98,9 @@ size_t anteraja_services_handle(const char *contents, size_t size, size_t nmemb, void anteraja_order(const char *order_number, const char *service, const char *sender_name, const char *sender_phone, const char *origin, const char *sender_address, - const char *receiver_name, const char *receiver_phone, const char *destination, - const char *receiver_address, int nitems, char **items[], _Bool insurance, double subtotal, - char **url, char **post) + const char *sender_postal, const char *receiver_name, const char *receiver_phone, + const char *destination, const char *receiver_address, const char *receiver_postal, + int nitems, char **items[], _Bool insurance, double subtotal, char **url, char **post) { *url = malloc(strlen(shipping.base) + strlen(ORDER_PATH) + 1); sprintf(*url, "%s%s", shipping.base, ORDER_PATH); @@ -130,12 +132,14 @@ void anteraja_order(const char *order_number, const char *service, const char *s } *post = malloc(strlen(ORDER_POST) + strlen(prefix) + strlen(order_number) + strlen(service) + ORDER_WEIGHT + strlen(sender_name) + strlen(sender_phone) + strlen(origin) - + strlen(sender_address) + strlen(receiver_name) + strlen(receiver_phone) - + strlen(destination) + strlen(receiver_address) + strlen(json) + ORDER_INSURANCE - + ORDER_SUBTOTAL - 13 * strlen("%s") - 2 * strlen("%d") + 1); + + strlen(sender_address) + strlen(sender_postal) + strlen(receiver_name) + + strlen(receiver_phone) + strlen(destination) + strlen(receiver_address) + + strlen(receiver_postal) + strlen(json) + ORDER_INSURANCE + ORDER_SUBTOTAL + - 15 * strlen("%s") - 2 * strlen("%d") + 1); sprintf(*post, ORDER_POST, prefix, order_number, service, (int)total_weight, sender_name, - sender_phone, origin, sender_address, receiver_name, receiver_phone, destination, - receiver_address, json, insurance ? "true" : "false", (int)subtotal); + sender_phone, origin, sender_address, sender_postal, receiver_name, receiver_phone, + destination, receiver_address, receiver_postal, json, insurance ? "true" : "false", + (int)subtotal); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post); } diff --git a/pikul.c b/pikul.c index 5f04136..cb74feb 100644 --- a/pikul.c +++ b/pikul.c @@ -14,8 +14,8 @@ extern void anteraja_init(char *[]); extern void anteraja_services(const char *, const char *, double, char **, char **); extern size_t anteraja_services_handle(const char *, size_t, size_t, struct pikul_services **); extern void anteraja_order(const char *, const char *, const char *, const char *, const char *, - const char *, const char *, const char *, const char *, const char *, int, - char **[], _Bool, double, char **, char **); + const char *, const char *, const char *, const char *, const char *, const char *, + const char *, int, char **[], _Bool, double, char **, char **); extern size_t anteraja_order_handle(const char *, size_t size, size_t nmemb, char **); extern void anteraja_cleanup(); @@ -114,8 +114,9 @@ double pikul_cost(const char *origin, const char *destination, double weight, co char *pikul_order(const char *order_number, const char *service, const char *sender_name, const char *sender_phone, const char *origin, const char *sender_address, - const char *receiver_name, const char *receiver_phone, const char *destination, - const char *receiver_address, int nitems, char **items[], _Bool insurance, double subtotal) + const char *sender_postal, const char *receiver_name, const char *receiver_phone, + const char *destination, const char *receiver_address, const char *receiver_postal, + int nitems, char **items[], _Bool insurance, double subtotal) { curl_easy_setopt(curl, CURLOPT_WRITEDATA, &waybill); char *url; @@ -124,8 +125,9 @@ char *pikul_order(const char *order_number, const char *service, const char *sen switch (shipping.company) { case PIKUL_ANTERAJA: anteraja_order(order_number, service, sender_name, sender_phone, origin, - sender_address, receiver_name, receiver_phone, destination, - receiver_address, nitems, items, insurance, subtotal, &url, &post); + sender_address, sender_postal, receiver_name, receiver_phone, + destination, receiver_address, receiver_postal, nitems, items, + insurance, subtotal, &url, &post); handler = anteraja_order_handle; break; default: diff --git a/pikul.h b/pikul.h index 623c87a..07db738 100644 --- a/pikul.h +++ b/pikul.h @@ -27,8 +27,9 @@ void pikul_free_services(struct pikul_services *services); double pikul_cost(const char *origin, const char *destination, double weight, const char *service); char *pikul_order(const char *order_number, const char *service, const char *sender_name, const char *sender_phone, const char *origin, const char *sender_address, - const char *receiver_name, const char *receiver_phone, const char *destination, - const char *receiver_address, int nitems, char **items[], _Bool insurance, double subtotal); + const char *sender_postal, const char *receiver_name, const char *receiver_phone, + const char *destination, const char *receiver_address, const char *receiver_postal, + int nitems, char **items[], _Bool insurance, double subtotal); void pikul_cleanup(); #ifdef __cplusplus diff --git a/pikul.i b/pikul.i index 9defe86..aa4c707 100644 --- a/pikul.i +++ b/pikul.i @@ -50,6 +50,7 @@ void pikul_init(enum pikul_company company, char *provisions[]); double pikul_cost(const char *origin, const char *destination, double weight, const char *service); char *pikul_order(const char *order_number, const char *service, const char *sender_name, const char *sender_phone, const char *origin, const char *sender_address, - const char *receiver_name, const char *receiver_phone, const char *destination, - const char *receiver_address, int nitems, char **items[], _Bool insurance, double subtotal); + const char *sender_postal, const char *receiver_name, const char *receiver_phone, + const char *destination, const char *receiver_address, const char *receiver_postal, + int nitems, char **items[], _Bool insurance, double subtotal); void pikul_cleanup(); -- cgit v1.2.3 From 3f8539bd52be8fdfaca4a056373d751799241659 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 15:33:11 +0800 Subject: Remove parameter names to save from retyping --- pikul.i | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/pikul.i b/pikul.i index aa4c707..e2938ac 100644 --- a/pikul.i +++ b/pikul.i @@ -46,11 +46,9 @@ } %rename("%(strip:[pikul_])s") ""; -void pikul_init(enum pikul_company company, char *provisions[]); -double pikul_cost(const char *origin, const char *destination, double weight, const char *service); -char *pikul_order(const char *order_number, const char *service, const char *sender_name, - const char *sender_phone, const char *origin, const char *sender_address, - const char *sender_postal, const char *receiver_name, const char *receiver_phone, - const char *destination, const char *receiver_address, const char *receiver_postal, - int nitems, char **items[], _Bool insurance, double subtotal); +void pikul_init(enum pikul_company, char *[]); +double pikul_cost(const char *, const char *, double, const char *); +char *pikul_order(const char *, const char *, const char *, const char *, const char *, const char *, + const char *, const char *, const char *, const char *, const char *, const char *, int, + char **[], _Bool, double); void pikul_cleanup(); -- cgit v1.2.3 From 738aa65629d7b552762cc825829ab5871cd99c4d 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 16:01:30 +0800 Subject: Tidy up a bit --- anteraja.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/anteraja.c b/anteraja.c index 89239ab..ffd739a 100644 --- a/anteraja.c +++ b/anteraja.c @@ -106,7 +106,7 @@ void anteraja_order(const char *order_number, const char *service, const char *s sprintf(*url, "%s%s", shipping.base, ORDER_PATH); enum { SKU, QUANTITY, DESCRIPTION, PRICE, WEIGHT }; char *json = NULL; - double total_weight = 0; + double total_weight = .0; for (int i = 0; i < nitems; i++) { size_t length = strlen(ORDER_ITEM) + strlen(items[i][DESCRIPTION]) + ORDER_ITEM_QUANTITY + ORDER_ITEM_PRICE + ORDER_ITEM_WEIGHT - strlen("%s") - 3 * strlen("%d") -- cgit v1.2.3 From f09a04b548164a6571234b9800e5e29e185595db 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 16:02:04 +0800 Subject: Minimum subtotal is 1000 for Anteraja --- anteraja.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/anteraja.c b/anteraja.c index ffd739a..d743a54 100644 --- a/anteraja.c +++ b/anteraja.c @@ -139,7 +139,7 @@ void anteraja_order(const char *order_number, const char *service, const char *s sprintf(*post, ORDER_POST, prefix, order_number, service, (int)total_weight, sender_name, sender_phone, origin, sender_address, sender_postal, receiver_name, receiver_phone, destination, receiver_address, receiver_postal, json, insurance ? "true" : "false", - (int)subtotal); + subtotal < 1000.0 ? 1000 :(int)subtotal); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post); } -- cgit v1.2.3 From 7b90ae5879f80ddb9f0a11aaa0fb63d16844fa59 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 16:06:00 +0800 Subject: Minimum unit price must be 1000 too for Anteraja --- anteraja.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/anteraja.c b/anteraja.c index d743a54..14e32a0 100644 --- a/anteraja.c +++ b/anteraja.c @@ -115,8 +115,11 @@ void anteraja_order(const char *order_number, const char *service, const char *s double weight = atof(items[i][WEIGHT]) * 1000.0; if (weight < 100.0) weight = 100.0; - sprintf(item, ORDER_ITEM, items[i][DESCRIPTION], atoi(items[i][QUANTITY]), - atoi(items[i][PRICE]), (int)weight); + double price = atof(items[i][PRICE]); + if (price < 1000.0) + price = 1000.0; + sprintf(item, ORDER_ITEM, items[i][DESCRIPTION], atoi(items[i][QUANTITY]), (int)price, + (int)weight); total_weight += weight; if (json) json = realloc(json, strlen(json) + length + 1); -- 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 --- anteraja.c | 4 ++-- handler.h | 6 +++--- pikul.c | 10 +++++----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/anteraja.c b/anteraja.c index 14e32a0..0e5f2e7 100644 --- a/anteraja.c +++ b/anteraja.c @@ -146,14 +146,14 @@ void anteraja_order(const char *order_number, const char *service, const char *s curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post); } -size_t anteraja_order_handle(const char *contents, size_t size, size_t nmemb, char **waybill) +size_t anteraja_order_handle(const char *contents, size_t size, size_t nmemb, char **tracking_number) { size_t realsize = size * nmemb; handle(ORDER, contents, realsize, status_trail, (const char *[]){ "content", "waybill_no", NULL - }, NULL, waybill); + }, NULL, tracking_number); return realsize; } 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; diff --git a/pikul.c b/pikul.c index cb74feb..b3e6352 100644 --- a/pikul.c +++ b/pikul.c @@ -4,7 +4,7 @@ CURL *curl; json_tokener *tokener; struct shipping shipping; -static char *waybill = NULL; +static char *tracking_number = NULL; extern inline void headers(const char *[], char *[]); extern inline void handle(enum type, const char *, size_t, const char *[], const char *[], const char *[], @@ -118,7 +118,7 @@ char *pikul_order(const char *order_number, const char *service, const char *sen const char *destination, const char *receiver_address, const char *receiver_postal, int nitems, char **items[], _Bool insurance, double subtotal) { - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &waybill); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &tracking_number); char *url; char *post = NULL; size_t (*handler)(const char *, size_t, size_t, char **); @@ -139,7 +139,7 @@ char *pikul_order(const char *order_number, const char *service, const char *sen if (post) free(post); free(url); - return waybill; + return tracking_number; } void pikul_cleanup() @@ -151,8 +151,8 @@ void pikul_cleanup() default: break; } - if (waybill) - free(waybill); + if (tracking_number) + free(tracking_number); free(shipping.base); json_tokener_free(tokener); curl_slist_free_all(shipping.headers); -- cgit v1.2.3 From 12150c2dff8d226987beb78c8d37f595d90502eb 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 21:52:06 +0800 Subject: Should be the user's responsibility to free it --- pikul.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pikul.c b/pikul.c index b3e6352..ed11d9c 100644 --- a/pikul.c +++ b/pikul.c @@ -4,7 +4,6 @@ CURL *curl; json_tokener *tokener; struct shipping shipping; -static char *tracking_number = NULL; extern inline void headers(const char *[], char *[]); extern inline void handle(enum type, const char *, size_t, const char *[], const char *[], const char *[], @@ -118,6 +117,7 @@ char *pikul_order(const char *order_number, const char *service, const char *sen const char *destination, const char *receiver_address, const char *receiver_postal, int nitems, char **items[], _Bool insurance, double subtotal) { + char *tracking_number = NULL; curl_easy_setopt(curl, CURLOPT_WRITEDATA, &tracking_number); char *url; char *post = NULL; @@ -151,8 +151,6 @@ void pikul_cleanup() default: break; } - if (tracking_number) - free(tracking_number); free(shipping.base); json_tokener_free(tokener); curl_slist_free_all(shipping.headers); -- cgit v1.2.3 From e9e2ea78fa012e909aff1b0f0838c321e496fb67 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 22:42:03 +0800 Subject: nitems include item quantities that are more than 1 --- anteraja.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/anteraja.c b/anteraja.c index 0e5f2e7..a3bc1c3 100644 --- a/anteraja.c +++ b/anteraja.c @@ -112,15 +112,15 @@ void anteraja_order(const char *order_number, const char *service, const char *s + ORDER_ITEM_PRICE + ORDER_ITEM_WEIGHT - strlen("%s") - 3 * strlen("%d") + strlen(","); char item[length + 1]; - double weight = atof(items[i][WEIGHT]) * 1000.0; - if (weight < 100.0) - weight = 100.0; + int quantity = atoi(items[i][QUANTITY]); double price = atof(items[i][PRICE]); if (price < 1000.0) price = 1000.0; - sprintf(item, ORDER_ITEM, items[i][DESCRIPTION], atoi(items[i][QUANTITY]), (int)price, - (int)weight); + double weight = atof(items[i][WEIGHT]) * 1000.0; + if (weight < 100.0) + weight = 100.0; total_weight += weight; + sprintf(item, ORDER_ITEM, items[i][DESCRIPTION], quantity, (int)price, (int)weight); if (json) json = realloc(json, strlen(json) + length + 1); else { @@ -132,6 +132,7 @@ void anteraja_order(const char *order_number, const char *service, const char *s strcat(json, ","); else json[strlen(json)] = '\0'; + i += quantity - 1; } *post = malloc(strlen(ORDER_POST) + strlen(prefix) + strlen(order_number) + strlen(service) + ORDER_WEIGHT + strlen(sender_name) + strlen(sender_phone) + strlen(origin) -- cgit v1.2.3 From ba54930b22390f34ca9a445753c5ed8acde17549 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 22:47:56 +0800 Subject: Correct previous commit --- anteraja.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/anteraja.c b/anteraja.c index a3bc1c3..422b2d0 100644 --- a/anteraja.c +++ b/anteraja.c @@ -113,6 +113,8 @@ void anteraja_order(const char *order_number, const char *service, const char *s + strlen(","); char item[length + 1]; int quantity = atoi(items[i][QUANTITY]); + if (quantity > 1) + nitems -= quantity - 1; double price = atof(items[i][PRICE]); if (price < 1000.0) price = 1000.0; @@ -132,7 +134,6 @@ void anteraja_order(const char *order_number, const char *service, const char *s strcat(json, ","); else json[strlen(json)] = '\0'; - i += quantity - 1; } *post = malloc(strlen(ORDER_POST) + strlen(prefix) + strlen(order_number) + strlen(service) + ORDER_WEIGHT + strlen(sender_name) + strlen(sender_phone) + strlen(origin) @@ -145,6 +146,7 @@ void anteraja_order(const char *order_number, const char *service, const char *s destination, receiver_address, receiver_postal, json, insurance ? "true" : "false", subtotal < 1000.0 ? 1000 :(int)subtotal); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post); + printf("POST: %s\n", *post); } size_t anteraja_order_handle(const char *contents, size_t size, size_t nmemb, char **tracking_number) -- cgit v1.2.3 From b82ced0aaf6019f1893190d5fadfc1537f996dd9 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 22:54:16 +0800 Subject: Shouldn't have been added --- anteraja.c | 1 - 1 file changed, 1 deletion(-) diff --git a/anteraja.c b/anteraja.c index 422b2d0..9874d34 100644 --- a/anteraja.c +++ b/anteraja.c @@ -146,7 +146,6 @@ void anteraja_order(const char *order_number, const char *service, const char *s destination, receiver_address, receiver_postal, json, insurance ? "true" : "false", subtotal < 1000.0 ? 1000 :(int)subtotal); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post); - printf("POST: %s\n", *post); } size_t anteraja_order_handle(const char *contents, size_t size, size_t nmemb, char **tracking_number) -- cgit v1.2.3 From b167efeabf9b6292c6c7a15f7a83b48c2eb86a1b 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: Sun, 18 Jul 2021 10:53:32 +0800 Subject: Weight times quantity to make up total --- anteraja.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/anteraja.c b/anteraja.c index 9874d34..8c151ff 100644 --- a/anteraja.c +++ b/anteraja.c @@ -121,7 +121,7 @@ void anteraja_order(const char *order_number, const char *service, const char *s double weight = atof(items[i][WEIGHT]) * 1000.0; if (weight < 100.0) weight = 100.0; - total_weight += weight; + total_weight += weight * quantity; sprintf(item, ORDER_ITEM, items[i][DESCRIPTION], quantity, (int)price, (int)weight); if (json) json = realloc(json, strlen(json) + length + 1); -- cgit v1.2.3 From b53957a8fd25bcce8eb98f71b6e9e711b48ce605 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: Sun, 18 Jul 2021 11:59:55 +0800 Subject: Insurance is not determined by user's decision It depends on some weight threshhold. --- anteraja.c | 8 ++++---- pikul.c | 6 +++--- pikul.h | 2 +- pikul.i | 6 +----- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/anteraja.c b/anteraja.c index 8c151ff..81b709a 100644 --- a/anteraja.c +++ b/anteraja.c @@ -100,7 +100,7 @@ void anteraja_order(const char *order_number, const char *service, const char *s const char *sender_phone, const char *origin, const char *sender_address, const char *sender_postal, const char *receiver_name, const char *receiver_phone, const char *destination, const char *receiver_address, const char *receiver_postal, - int nitems, char **items[], _Bool insurance, double subtotal, char **url, char **post) + int nitems, char **items[], double subtotal, char **url, char **post) { *url = malloc(strlen(shipping.base) + strlen(ORDER_PATH) + 1); sprintf(*url, "%s%s", shipping.base, ORDER_PATH); @@ -142,9 +142,9 @@ void anteraja_order(const char *order_number, const char *service, const char *s + strlen(receiver_postal) + strlen(json) + ORDER_INSURANCE + ORDER_SUBTOTAL - 15 * strlen("%s") - 2 * strlen("%d") + 1); sprintf(*post, ORDER_POST, prefix, order_number, service, (int)total_weight, sender_name, - sender_phone, origin, sender_address, sender_postal, receiver_name, receiver_phone, - destination, receiver_address, receiver_postal, json, insurance ? "true" : "false", - subtotal < 1000.0 ? 1000 :(int)subtotal); + sender_phone, origin, sender_address, sender_postal, receiver_name, receiver_phone, + destination, receiver_address, receiver_postal, json, + total_weight < 1000.0 ? "true" : "false", subtotal < 1000.0 ? 1000 : (int)subtotal); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post); } diff --git a/pikul.c b/pikul.c index ed11d9c..8d776a4 100644 --- a/pikul.c +++ b/pikul.c @@ -14,7 +14,7 @@ extern void anteraja_services(const char *, const char *, double, char **, char extern size_t anteraja_services_handle(const char *, size_t, size_t, struct pikul_services **); extern void anteraja_order(const char *, const char *, const char *, const char *, const char *, const char *, const char *, const char *, const char *, const char *, const char *, - const char *, int, char **[], _Bool, double, char **, char **); + const char *, int, char **[], double, char **, char **); extern size_t anteraja_order_handle(const char *, size_t size, size_t nmemb, char **); extern void anteraja_cleanup(); @@ -115,7 +115,7 @@ char *pikul_order(const char *order_number, const char *service, const char *sen const char *sender_phone, const char *origin, const char *sender_address, const char *sender_postal, const char *receiver_name, const char *receiver_phone, const char *destination, const char *receiver_address, const char *receiver_postal, - int nitems, char **items[], _Bool insurance, double subtotal) + int nitems, char **items[], double subtotal) { char *tracking_number = NULL; curl_easy_setopt(curl, CURLOPT_WRITEDATA, &tracking_number); @@ -127,7 +127,7 @@ char *pikul_order(const char *order_number, const char *service, const char *sen anteraja_order(order_number, service, sender_name, sender_phone, origin, sender_address, sender_postal, receiver_name, receiver_phone, destination, receiver_address, receiver_postal, nitems, items, - insurance, subtotal, &url, &post); + subtotal, &url, &post); handler = anteraja_order_handle; break; default: diff --git a/pikul.h b/pikul.h index 07db738..fde0483 100644 --- a/pikul.h +++ b/pikul.h @@ -29,7 +29,7 @@ char *pikul_order(const char *order_number, const char *service, const char *sen const char *sender_phone, const char *origin, const char *sender_address, const char *sender_postal, const char *receiver_name, const char *receiver_phone, const char *destination, const char *receiver_address, const char *receiver_postal, - int nitems, char **items[], _Bool insurance, double subtotal); + int nitems, char **items[], double subtotal); void pikul_cleanup(); #ifdef __cplusplus diff --git a/pikul.i b/pikul.i index e2938ac..331cfcb 100644 --- a/pikul.i +++ b/pikul.i @@ -41,14 +41,10 @@ free($1); } -%typemap(in) _Bool { - $1 = $input; -} - %rename("%(strip:[pikul_])s") ""; void pikul_init(enum pikul_company, char *[]); double pikul_cost(const char *, const char *, double, const char *); char *pikul_order(const char *, const char *, const char *, const char *, const char *, const char *, const char *, const char *, const char *, const char *, const char *, const char *, int, - char **[], _Bool, double); + char **[], double); void pikul_cleanup(); -- cgit v1.2.3 From 39190eb032cc6a8c2fe9d07ca85f712a229c0b69 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: Sun, 18 Jul 2021 13:18:44 +0800 Subject: The qualifiers have been unnecessary --- pikul.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/pikul.c b/pikul.c index 8d776a4..636aeb3 100644 --- a/pikul.c +++ b/pikul.c @@ -5,18 +5,17 @@ CURL *curl; json_tokener *tokener; struct shipping shipping; -extern inline void headers(const char *[], char *[]); -extern inline void handle(enum type, const char *, size_t, const char *[], const char *[], const char *[], - void *); +void headers(const char *[], char *[]); +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 **); -extern size_t anteraja_services_handle(const char *, size_t, size_t, struct pikul_services **); -extern void anteraja_order(const char *, const char *, const char *, const char *, const char *, - const char *, const char *, const char *, const char *, const char *, const char *, - const char *, int, char **[], double, char **, char **); -extern size_t anteraja_order_handle(const char *, size_t size, size_t nmemb, char **); -extern void anteraja_cleanup(); +void anteraja_init(char *[]); +void anteraja_services(const char *, const char *, double, char **, char **); +size_t anteraja_services_handle(const char *, size_t, size_t, struct pikul_services **); +void anteraja_order(const char *, const char *, const char *, const char *, const char *, const char *, + const char *, const char *, const char *, const char *, const char *, const char *, int, + char **[], double, char **, char **); +size_t anteraja_order_handle(const char *, size_t size, size_t nmemb, char **); +void anteraja_cleanup(); void pikul_init(enum pikul_company company, char *provisions[]) { -- cgit v1.2.3