summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--client.c47
1 files 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;