diff options
author | ꦌ ꦫꦶ ꦏ꧀ꦦꦿ ꦧ ꦮ ꦑ ꦩ ꦭ꧀ <erik@darapsa.co.id> | 2019-10-07 10:12:47 +0800 |
---|---|---|
committer | ꦌ ꦫꦶ ꦏ꧀ꦦꦿ ꦧ ꦮ ꦑ ꦩ ꦭ꧀ <erik@darapsa.co.id> | 2019-10-07 10:12:47 +0800 |
commit | 6fa0df3c46986b15dec3be672f41eb7ea62c20ef (patch) | |
tree | 311b606902a16dabde282983d409af68c8d7b64b /client.c | |
parent | b4791f689f28b05462645db8e6d83639515c358e (diff) |
Simplify order function memory allocations
Diffstat (limited to 'client.c')
-rw-r--r-- | client.c | 47 |
1 files changed, 26 insertions, 21 deletions
@@ -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; |