From c0fba9db9290fb30884b9c4f8d847212164e3b50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=20=EA=A6=AB=EA=A6=B6=20=EA=A6=8F=EA=A7=80?= =?UTF-8?q?=EA=A6=A6=EA=A6=BF=20=EA=A6=A7=20=EA=A6=AE=20=EA=A6=91=20?= =?UTF-8?q?=EA=A6=A9=20=EA=A6=AD=EA=A7=80?= Date: Sat, 11 Jul 2020 11:48:19 +0800 Subject: Move order function to ord namespace --- client.c | 76 ++-------------------------------------------------------------- 1 file changed, 2 insertions(+), 74 deletions(-) (limited to 'client.c') diff --git a/client.c b/client.c index ac9dc15..94dacb3 100644 --- a/client.c +++ b/client.c @@ -4,14 +4,8 @@ #include "request.h" #include "icclient/product.h" #include "icclient/catalog.h" -#include "icclient/ord.h" #include "icclient/client.h" -typedef struct icclient_product icclient_product; -typedef struct icclient_catalog icclient_catalog; -typedef struct icclient_ord_item icclient_ord_item; -typedef struct icclient_ord_order icclient_ord_order; - CURL *curl = NULL; char *server_url = NULL; @@ -51,84 +45,18 @@ void icclient_results(const char *prodgroup, } void icclient_allproducts(size_t (*handler)(void *, size_t, size_t, void *), - icclient_catalog **catalogptr) + struct icclient_catalog **catalogptr) { request(handler, (void *)catalogptr, NULL, "%s", "All-Products"); } void icclient_flypage(const char *sku, size_t (*handler)(void *, size_t, size_t, void *), - icclient_product **productptr) + struct icclient_product **productptr) { request(handler, (void *)productptr, NULL, "%s", sku); } -static int prodcmp(const void *product1, const void *product2) -{ - return strcmp((*(icclient_product * const *)product1)->sku - , (*(icclient_product * const *)product2)->sku); -} - -static int itemcmp(const void *item1, const void *item2) -{ - return strcmp((*(icclient_ord_item * const *)item1)->product->sku - , (*(icclient_ord_item * const *)item2)->product->sku); -} - -void icclient_order(const char *sku, const icclient_catalog *catalog, - icclient_ord_order **orderptr) -{ - icclient_product **products = ((icclient_catalog *)catalog)->products; - qsort(products, catalog->length, sizeof(icclient_product *), prodcmp); - icclient_product *key_product = malloc(sizeof(icclient_product)); - icclient_product_init(key_product); - key_product->sku = malloc(strlen(sku) + 1); - strcpy(key_product->sku, sku); - icclient_product *product = *(icclient_product **)bsearch(&key_product - , products, catalog->length, sizeof(icclient_product *) - , prodcmp); - icclient_product_free(key_product); - - icclient_ord_order *order = *orderptr; - icclient_ord_item *item = NULL; - - if (order) { - icclient_ord_item **items = order->items; - qsort(items, order->nitems, sizeof(icclient_ord_item *), itemcmp); - icclient_ord_item *key_item = malloc(sizeof(icclient_ord_item)); - key_item->product = product; - icclient_ord_item **itemptr = bsearch(&key_item, items - , order->nitems, sizeof(icclient_ord_item *) - , itemcmp); - if (itemptr) - item = *itemptr; - free(key_item); - } else { - *orderptr = malloc(sizeof(icclient_ord_order)); - order = *orderptr; - icclient_ord_init(order); - } - - if (item) - item->quantity++; - else { - size_t i = order->nitems; - *orderptr = realloc(order, sizeof(icclient_ord_order) - + (i + 1) * sizeof(icclient_ord_item)); - order = *orderptr; - order->items[i] = malloc(sizeof(icclient_ord_item)); - order->nitems++; - item = order->items[i]; - item->product = product; - item->quantity = 1; - } - - order->subtotal += item->product->price; - order->total_cost += item->product->price; - - request(NULL, NULL, NULL, "%s%s", "order?mv_arg=", sku); -} - void icclient_page(const char *path, size_t (*handler)(void *, size_t, size_t, void *), void **dataptr) -- cgit v1.2.3