diff options
author | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2023-05-17 14:06:32 +0800 |
---|---|---|
committer | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2023-05-17 14:06:32 +0800 |
commit | 1e88f321279b60816a0fe3c78b9ac53facdd4203 (patch) | |
tree | 7d6e0034d20227b43fb8551b44a3240b94f6826f | |
parent | 7270c84fe616357da17baca20437ef1113bb87eb (diff) |
order takes custom options params
-rw-r--r-- | interchange/ord.h | 3 | ||||
-rw-r--r-- | ord.c | 36 | ||||
-rw-r--r-- | 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 *)); /*! @@ -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, @@ -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; |