From 1e88f321279b60816a0fe3c78b9ac53facdd4203 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: Wed, 17 May 2023 14:06:32 +0800 Subject: order takes custom options params --- interchange/ord.h | 3 ++- ord.c | 36 ++++++++++++++++++++++++++++-------- request.c | 6 ++++++ 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/interchange/ord.h b/interchange/ord.h index e31006e..56bd648 100644 --- a/interchange/ord.h +++ b/interchange/ord.h @@ -33,10 +33,11 @@ extern "C" { * \param sku The product or variant SKU of the item to order. * \param item The product SKU of the item to order. * \param quantity The quantity of the item to order. + * \param options Any additional pairs of custom options. * \param parser Function for parsing the formatted response. */ void interchange_ord_order(const char *sku, const char *item, - const unsigned int quantity, + const unsigned int quantity, const char *options[][2], void (*parser)(interchange_response *)); /*! diff --git a/ord.c b/ord.c index c9b8f22..df5017f 100644 --- a/ord.c +++ b/ord.c @@ -7,7 +7,7 @@ #include "interchange/ord.h" void interchange_ord_order(const char *sku, const char *item, - const unsigned int quantity, + const unsigned int quantity, const char *options[][2], void (*parser)(interchange_response *)) { size_t length = 0; @@ -17,13 +17,33 @@ void interchange_ord_order(const char *sku, const char *item, } while ((qty /= 10)); char *qty_str = malloc(length + 1); sprintf(qty_str, "%d", quantity); - request(parser, NULL, (const char *[][2]){ - "mv_action", "refresh", - "mv_sku", sku, - "mv_order_item", item, - "mv_order_quantity", qty_str, - NULL - }, "%s", "ord/basket"); + const char **pair = *options; + size_t nopts = 0; + while (pair[0] && pair[1]) { + nopts++; + pair = *++options; + } + for (size_t i = 0; i < nopts; i++) + --options; + size_t total_nopts = 4 + nopts; + const char *order[total_nopts + 1][2]; + order[0][0] = "mv_action"; + order[0][1] = "refresh"; + order[1][0] = "mv_sku"; + order[1][1] = sku; + order[2][0] = "mv_order_item"; + order[2][1] = item; + order[3][0] = "mv_order_quantity"; + order[3][1] = qty_str; + for (size_t i = 0; i < nopts; i++) { + const char **pair = options[i]; + order[4 + i][0] = malloc(strlen(pair[0]) + 1); + strcpy((char *)order[4 + i][0], pair[0]); + order[4 + i][1] = malloc(strlen(pair[1]) + 1); + strcpy((char *)order[4 + i][1], pair[1]); + } + order[total_nopts][0] = NULL; + request(parser, NULL, order, "%s", "ord/basket"); } void interchange_ord_update(const char *name, const unsigned int quantity, diff --git a/request.c b/request.c index c20587d..0993e25 100644 --- a/request.c +++ b/request.c @@ -180,6 +180,12 @@ void request(void (*handler)(interchange_response *), void (*callback)(void *), CURLFORM_END); if (!strncmp(pair[0], "quantity", 8)) free((void *)pair[1]); + else if (!strncmp(pair[0], "mv_order_", 9) + && strcmp(pair[0], "mv_order_quantity") + && strcmp(pair[0], "mv_order_item")) { + free((void *)pair[0]); + free((void *)pair[1]); + } pair = *++body; } last = NULL; -- cgit v1.2.3