summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorꦌ ꦫꦶ ꦏ꧀ꦦꦿ ꦧ ꦮ ꦑ ꦩ ꦭ꧀ <erik@darapsa.co.id>2020-07-11 11:48:19 +0800
committerꦌ ꦫꦶ ꦏ꧀ꦦꦿ ꦧ ꦮ ꦑ ꦩ ꦭ꧀ <erik@darapsa.co.id>2020-07-11 11:48:19 +0800
commitc0fba9db9290fb30884b9c4f8d847212164e3b50 (patch)
treea3a1124b200840ea28ec1b475c8cd305bd47af82
parent1a1f3c047d36dd59958d3dba6e4fa690d2178fec (diff)
Move order function to ord namespace
-rw-r--r--client.c76
-rw-r--r--icclient/client.h11
-rw-r--r--icclient/ord.h8
-rw-r--r--ord.c72
4 files changed, 83 insertions, 84 deletions
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)
diff --git a/icclient/client.h b/icclient/client.h
index ee11a2a..60c60d4 100644
--- a/icclient/client.h
+++ b/icclient/client.h
@@ -3,7 +3,6 @@
struct icclient_product;
struct icclient_catalog;
-struct icclient_ord_order;
#ifdef __cplusplus
extern "C" {
@@ -37,7 +36,7 @@ extern "C" {
/*!
* \brief For fetching data about a specific product.
- * \param sku The SKU of the item to order.
+ * \param sku The SKU of the product.
* \param handler A pointer to a cURL write function callback.
* \param productptr A pointer to pointer to the product to store the data.
*/
@@ -45,14 +44,6 @@ extern "C" {
size_t (*handler)(void *, size_t, size_t, void *),
struct icclient_product **productptr);
- /*!
- * \brief For putting an item to a cart.
- * \param sku The SKU of the item to order.
- * \param catalog A pointer to the catalog from which the item is.
- * \param orderptr A pointer to pointer to the order.
- */
- void icclient_order(const char *sku, const struct icclient_catalog *catalog,
- struct icclient_ord_order **orderptr);
void icclient_page(const char *path,
size_t (*handler)(void *, size_t, size_t, void *),
void **dataptr);
diff --git a/icclient/ord.h b/icclient/ord.h
index 638880a..5195e81 100644
--- a/icclient/ord.h
+++ b/icclient/ord.h
@@ -19,6 +19,14 @@ extern "C" {
#endif
void icclient_ord_init(struct icclient_ord_order *order);
+ /*!
+ * \brief For putting an item to a cart.
+ * \param sku The SKU of the item to order.
+ * \param catalog A pointer to the catalog from which the item is.
+ * \param orderptr A pointer to pointer to the order.
+ */
+ void icclient_ord_order(const char *sku, const struct icclient_catalog *catalog,
+ struct icclient_ord_order **orderptr);
void icclient_ord_free(struct icclient_ord_order *order);
#ifdef __cplusplus
diff --git a/ord.c b/ord.c
index f59a4b9..cf7bd12 100644
--- a/ord.c
+++ b/ord.c
@@ -1,8 +1,14 @@
#include <stdlib.h>
#include <stdbool.h>
+#include "request.h"
+#include "icclient/catalog.h"
#include "icclient/product.h"
#include "icclient/ord.h"
+typedef struct icclient_catalog icclient_catalog;
+typedef struct icclient_product icclient_product;
+typedef struct icclient_ord_item icclient_ord_item;
+
void icclient_ord_init(struct icclient_ord_order *order)
{
order->subtotal = .0;
@@ -11,6 +17,72 @@ void icclient_ord_init(struct icclient_ord_order *order)
order->nitems = 0;
}
+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_ord_order(const char *sku, const icclient_catalog *catalog,
+ struct 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);
+
+ struct 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(struct icclient_ord_order));
+ order = *orderptr;
+ icclient_ord_init(order);
+ }
+
+ if (item)
+ item->quantity++;
+ else {
+ size_t i = order->nitems;
+ *orderptr = realloc(order, sizeof(struct 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_ord_free(struct icclient_ord_order *order)
{
for (size_t i = 0; i < order->nitems; i++)