From 6fa0df3c46986b15dec3be672f41eb7ea62c20ef 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: Mon, 7 Oct 2019 10:12:47 +0800 Subject: Simplify order function memory allocations --- client.c | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/client.c b/client.c index d01a1bc..4ead161 100644 --- a/client.c +++ b/client.c @@ -56,34 +56,39 @@ static int itemcmp(const void *item1, const void *item2) void icclient_order(icclient_ord_order **orderptr, const char *sku , icclient_catalog *catalog) { - if (!*orderptr) { - (*orderptr) = malloc(sizeof(icclient_ord_order)); - (*orderptr)->subtotal = .0; - (*orderptr)->shipping = .0; - (*orderptr)->subtotal = .0; - (*orderptr)->nitems = 0; - } - icclient_ord_order *order = *orderptr; + icclient_product **products = catalog->products; + qsort(products, catalog->length, sizeof(icclient_product), prodcmp); + icclient_product *product = bsearch(sku, products, catalog->length + , sizeof(icclient_product), prodcmp); - qsort(catalog->products, catalog->length, sizeof(icclient_product) - , prodcmp); - icclient_product *product = bsearch(sku, catalog->products - , catalog->length, sizeof(icclient_product), prodcmp); - - qsort(order->items, order->nitems, sizeof(icclient_ord_item), itemcmp); - icclient_ord_item *item = bsearch(sku, order->items, order->nitems - , sizeof(icclient_ord_item), itemcmp); + icclient_ord_order *order = *orderptr; + icclient_ord_item *item = NULL; + + if (order && order->nitems) { + icclient_ord_item **items = order->items; + qsort(items, order->nitems, sizeof(icclient_ord_item), itemcmp); + item = bsearch(sku, items, order->nitems, sizeof(icclient_ord_item) + , itemcmp); + } else { + *orderptr = malloc(sizeof(icclient_ord_order)); + order = *orderptr; + order->subtotal = .0; + order->shipping = .0; + order->subtotal = .0; + order->nitems = 0; + } if (item) item->quantity++; else { - item = malloc(sizeof(icclient_ord_item)); + 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)); + item = order->items[i]; item->product = product; item->quantity = 1; - *orderptr = realloc(*orderptr, sizeof(*orderptr) - + sizeof(icclient_ord_item)); - (*orderptr)->items[(*orderptr)->nitems++] = item; - order = *orderptr; } order->subtotal += item->product->price; -- cgit v1.2.3