From 7f934ce683df5252fa6350dcc15e187b1c7623cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=EA=A6=AB=EA=A6=B6=EA=A6=8F=EA=A7=80=EA=A6=A6?= =?UTF-8?q?=EA=A6=BF=EA=A6=A7=EA=A6=AE=EA=A6=91=EA=A6=A9=EA=A6=AD=EA=A7=80?= Date: Sun, 25 Sep 2022 10:20:12 +0800 Subject: Rename remaining icclient to interchange --- Makefile.am | 12 +++--- admin.c | 32 +++++++-------- client.c | 107 ------------------------------------------------- icclient.h | 77 ----------------------------------- icclient/admin.h | 39 ------------------ icclient/member.h | 79 ------------------------------------ icclient/ord.h | 37 ----------------- icclient/typedefs.h | 18 --------- interchange.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++++ interchange.h | 77 +++++++++++++++++++++++++++++++++++ interchange/admin.h | 39 ++++++++++++++++++ interchange/member.h | 79 ++++++++++++++++++++++++++++++++++++ interchange/ord.h | 37 +++++++++++++++++ interchange/typedefs.h | 18 +++++++++ login.c | 2 +- login.h | 6 +-- main.c | 24 +++++------ member.c | 16 ++++---- ord.c | 60 +++++++++++++-------------- request.c | 12 +++--- request.h | 4 +- 21 files changed, 441 insertions(+), 441 deletions(-) delete mode 100644 client.c delete mode 100644 icclient.h delete mode 100644 icclient/admin.h delete mode 100644 icclient/member.h delete mode 100644 icclient/ord.h delete mode 100644 icclient/typedefs.h create mode 100644 interchange.c create mode 100644 interchange.h create mode 100644 interchange/admin.h create mode 100644 interchange/member.h create mode 100644 interchange/ord.h create mode 100644 interchange/typedefs.h diff --git a/Makefile.am b/Makefile.am index 625a358..19549fd 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,6 +1,6 @@ lib_LTLIBRARIES = libinterchange.la libinterchange_la_SOURCES = \ - client.c \ + interchange.c \ request.c \ ord.c \ login.c \ @@ -18,9 +18,9 @@ if STRAP libinterchange_la_SOURCES += strap.c libinterchange_la_CPPFLAGS = $(libinterchange_la_CPPFLAGS) $(TIDY_CFLAGS) endif -include_HEADERS = icclient.h +include_HEADERS = interchange.h pkginclude_HEADERS = \ - icclient/typedefs.h \ - icclient/ord.h \ - icclient/member.h \ - icclient/admin.h + interchange/typedefs.h \ + interchange/ord.h \ + interchange/member.h \ + interchange/admin.h diff --git a/admin.c b/admin.c index f284595..a4ea6c1 100644 --- a/admin.c +++ b/admin.c @@ -1,19 +1,19 @@ #include #include #include "login.h" -#include "icclient.h" -#include "icclient/member.h" -#include "icclient/ord.h" -#include "icclient/admin.h" +#include "interchange.h" +#include "interchange/member.h" +#include "interchange/ord.h" +#include "interchange/admin.h" -void icclient_admin_login(const char *username, const char *password, void (*handler)(icclient_response *), - void (*callback)(struct icclient_admin *)) +void interchange_admin_login(const char *username, const char *password, void (*handler)(interchange_response *), + void (*callback)(struct interchange_admin *)) { login(username, password, NULL, "MMLogin", handler, (void (*)(void *))callback); } -void icclient_admin_new_admin(const char *username, const char *password, const char *name, bool super, - enum icclient_admin_group group, void (*handler)(icclient_response *)) +void interchange_admin_new_admin(const char *username, const char *password, const char *name, bool super, + enum interchange_admin_group group, void (*handler)(interchange_response *)) { request(handler, NULL, &(struct body){ 13, { { "mv_todo", "set" }, @@ -28,13 +28,13 @@ void icclient_admin_new_admin(const char *username, const char *password, const { "username", username }, { "password", password }, { "super", super ? "1" : "0" }, - { "groups", group == ICCLIENT_ADMIN_GROUP_CONTENT ? ":CONTENT" - : group == ICCLIENT_ADMIN_GROUP_MERCH ? ":MERCH" : ":ORDERS" } + { "groups", group == INTERCHANGE_ADMIN_GROUP_CONTENT ? ":CONTENT" + : group == INTERCHANGE_ADMIN_GROUP_MERCH ? ":MERCH" : ":ORDERS" } }}, "%s", "ui"); } -void icclient_admin_new_item(const char *description, const char *comment, const char *price, - const char *image_path, void (*handler)(icclient_response *)) +void interchange_admin_new_item(const char *description, const char *comment, const char *price, + const char *image_path, void (*handler)(interchange_response *)) { request(handler, NULL, &(struct body){ 15, { { "mv_click", "process_filter" }, @@ -63,9 +63,9 @@ void icclient_admin_new_item(const char *description, const char *comment, const }}, "%s", "admin/item_edit"); } -void icclient_admin_new_transaction(const struct icclient_ord_order *order, - const struct icclient_member *member, bool new_customer_id, - void (*handler)(icclient_response *)) +void interchange_admin_new_transaction(const struct interchange_ord_order *order, + const struct interchange_member *member, bool new_customer_id, + void (*handler)(interchange_response *)) { request(handler, NULL, &(struct body){ 12, { { "mv_values_space", "order_entry" }, @@ -83,7 +83,7 @@ void icclient_admin_new_transaction(const struct icclient_ord_order *order, }}, "%s", "process"); } -void icclient_admin_logout(struct icclient_admin *admin, void (*handler)(icclient_response *)) +void interchange_admin_logout(struct interchange_admin *admin, void (*handler)(interchange_response *)) { request(handler, NULL, NULL, "%s", "admin/login"); if (admin->name) diff --git a/client.c b/client.c deleted file mode 100644 index d7c8f70..0000000 --- a/client.c +++ /dev/null @@ -1,107 +0,0 @@ -#include -#include -#include "request.h" -#include "icclient.h" - -char *image_dir; -#ifdef __EMSCRIPTEN__ -emscripten_fetch_attr_t attr; -#else -char *sampleurl; -char *cainfo = NULL; -#endif - -extern void handle_results(icclient_response *); - -void icclient_init(const char *url, const char *dir, const char *certificate) -{ - image_dir = malloc(strlen(dir) + 1); - strcpy(image_dir, dir); -#ifdef __EMSCRIPTEN__ - emscripten_fetch_attr_init(&attr); - attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; -#else - size_t length = strlen(url); - size_t append = url[length - 1] != '/'; - sampleurl = malloc(length + append + 1); - strcpy(sampleurl, url); - if (append) - strcat(sampleurl, "/"); - curl_global_init(CURL_GLOBAL_SSL); - if (certificate) { - cainfo = malloc(strlen(certificate) + 1); - strcpy(cainfo, certificate); - } -#endif -} - -void icclient_catalog(const char *prod_group, void (*handler)(icclient_response *), void (*callback)(struct icclient_catalog *)) -{ - char nonspaced[strlen(prod_group) + 1]; - strcpy(nonspaced, prod_group); - char *space = NULL; - while ((space = strchr(nonspaced, ' '))) - *space = '-'; - request(handler ? handler : handle_results, (void (*)(void *))callback, NULL, "%s", nonspaced); -} - -void icclient_product(const char *sku, void (*handler)(icclient_response *), void (*callback)(struct icclient_product *)) -{ - request(handler, (void (*)(void *))callback, NULL, "%s", sku); -} - -void icclient_page(const char *path, void (*handler)(icclient_response *)) -{ - request(handler, NULL, NULL, "%s", path); -} - -void icclient_free_product(struct icclient_product *product) -{ - if (product->crosssell) - for (size_t i = 0; i < product->crosssell->length; i++) - free(product->crosssell->skus[i]); - if (product->author) - free(product->author); - if (product->prod_group) - free(product->prod_group); - if (product->image) - free(product->image); - if (product->thumb) - free(product->thumb); - if (product->comment) - free(product->comment); - if (product->description) - free(product->description); - free(product->sku); - free(product); -} - -void icclient_free_catalog(struct icclient_catalog *catalog) -{ - for (size_t i = 0; i < catalog->length; i++) - icclient_free_product(catalog->products[i]); - free(catalog); -} - -void icclient_free_response(icclient_response *response) -{ - if (response->userData) - free(response->userData); -#ifdef __EMSCRIPTEN__ - emscripten_fetch_close(response); -#else - free(response->data); - curl_easy_cleanup(response->curl); - free(response); -#endif -} - -void icclient_cleanup() -{ - free(image_dir); -#ifndef __EMSCRIPTEN__ - free(cainfo); - free(sampleurl); - curl_global_cleanup(); -#endif -} diff --git a/icclient.h b/icclient.h deleted file mode 100644 index b962a24..0000000 --- a/icclient.h +++ /dev/null @@ -1,77 +0,0 @@ -#ifndef ICCLIENT_H -#define ICCLIENT_H - -#include "icclient/typedefs.h" - -struct icclient_product { - char *sku; - char *description; - char *comment; - char *thumb; - char *image; - double price; - char *prod_group; - char *category; - double weight; - char *author; - struct icclient_product_crosssell { - size_t length; - char *skus[]; - } *crosssell; -}; - -struct icclient_catalog { - size_t length; - struct icclient_product *products[]; -}; - -/*! - * \brief For fetching data about all active products. - * \param handler A pointer to the function when a custom handler is needed to arrange the data into the catalog. - * \param callback A pointer to the function that needs to be called after the catalog is ready. - */ -#define icclient_allproducts(handler, callback) icclient_catalog("All-Products", handler, callback) - -#ifdef __cplusplus -extern "C" { -#endif - -/*! - * \brief A function that needs to be run first. - * \param sampleurl The value of the SAMPLEURL setting in products/variable.txt. - * \param image_dir The value of the IMAGE_DIR setting in products/variable.txt. - * \param certificate Path to the CA certificate file. - */ -void icclient_init(const char *sampleurl, const char *image_dir, const char *certificate); - -/*! - * \brief For fetching data about products that belong a specific group. - * \param prod_group The name of the product group. - * \param handler A pointer to the function when a custom handler is needed to arrange the data into the catalog. - * \param callback A pointer to the function that needs to be called after the catalog is ready. - */ -void icclient_catalog(const char *prod_group, void (*handler)(icclient_response *), void (*callback)(struct icclient_catalog *)); - -/*! - * \brief For fetching data about a specific product. - * \param sku The SKU of the product. - * \param handler A pointer to the function when a custom handler is needed to arrange the data into the product. - * \param callback A pointer to the function that needs to be called after the product is ready. -*/ -void icclient_product(const char *sku, void (*handler)(icclient_response *), void (*callback)(struct icclient_product *)); - -void icclient_page(const char *path, void (*handler)(icclient_response *)); - -void icclient_free_product(struct icclient_product *product); - -void icclient_free_catalog(struct icclient_catalog *catalog); - -void icclient_free_response(icclient_response *response); - -void icclient_cleanup(); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/icclient/admin.h b/icclient/admin.h deleted file mode 100644 index 08e4751..0000000 --- a/icclient/admin.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef ICCLIENT_ADMIN_H -#define ICCLIENT_ADMIN_H - -#include "typedefs.h" - -enum icclient_admin_group { - ICCLIENT_ADMIN_GROUP_CONTENT, - ICCLIENT_ADMIN_GROUP_MERCH, - ICCLIENT_ADMIN_GROUP_ORDERS -}; - -struct icclient_admin { - char *username; - char *password; - char *name; - bool super; - enum icclient_admin_group group; -}; - -#ifdef __cplusplus -extern "C" { -#endif - -void icclient_admin_login(const char *username, const char *password, void (*handler)(icclient_response *), - void (*callback)(struct icclient_admin *)); -void icclient_admin_new_admin(const char *username, const char *password, const char *name, bool super, - enum icclient_admin_group group, void (*handler)(icclient_response *)); -void icclient_admin_new_item(const char *description, const char *comment, const char *price, - const char *image_path, void (*handler)(icclient_response *)); -void icclient_admin_new_transaction(const struct icclient_ord_order *order, - const struct icclient_member *member, bool new_customer_id, - void (*handler)(icclient_response *)); -void icclient_admin_logout(struct icclient_admin *admin, void (*handler)(icclient_response *)); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/icclient/member.h b/icclient/member.h deleted file mode 100644 index 6b9d2ee..0000000 --- a/icclient/member.h +++ /dev/null @@ -1,79 +0,0 @@ -#ifndef ICCLIENT_MEMBER_H -#define ICCLIENT_MEMBER_H - -#include -#include "typedefs.h" - -struct icclient_member_preferences { - bool mv_same_billing; - bool email_copy; -}; - -struct icclient_member { - char *username; - char *usernick; - char *password; - char *expiration; - char *acl; - char *mod_time; - char *s_nickname; - char *company; - char *fname; /*!< Shipping first name */ - char *lname; /*!< Shipping last name */ - char *address1; - char *address2; - char *address3; - char *city; - char *state; - char *zip; - char *country; - char *phone_day; - char *mv_shipmode; - char *b_nickname; - char *b_fname; /*!< Billing first name */ - char *b_lname; /*!< Billing last name */ - char *b_company; - char *b_address1; - char *b_address2; - char *b_address3; - char *b_city; - char *b_state; - char *b_zip; - char *b_country; - char *b_phone; - char *p_nickname; - char *email; - char *fax; - char *phone_night; - char *address_book; - char *accounts; - struct icclient_member_preferences *preferences; - char *carts; - char *owner; - char *file_acl; - char *db_acl; - char *mail_list; - char *credit_limit; - bool inactive; - bool dealer; - char *price_level; -}; - -#ifdef __cplusplus -extern "C" { -#endif - - void icclient_member_newaccount(const char *username, const char *password, const char *verify, - void (*handler)(icclient_response *), void (*callback)(struct icclient_member *)); - void icclient_member_login(const char *username, const char *password, void (*handler)(icclient_response *), - void (*callback)(struct icclient_member *)); - void icclient_member_account(const char *fname, const char *lname, const char *address1, const char *address2, - const char *city, const char *state, const char *zip, const char *email, const char *phone_day); - void icclient_member_changepassword(const char *password_old, const char *password, const char *verify); - void icclient_member_logout(struct icclient_member *member); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/icclient/ord.h b/icclient/ord.h deleted file mode 100644 index eccc822..0000000 --- a/icclient/ord.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef ICCLIENT_ORD_H -#define ICCLIENT_ORD_H - -struct icclient_ord_item { - struct icclient_product *product; - unsigned int quantity; -}; - -struct icclient_ord_order { - double subtotal; - double shipping; - double total_cost; - char *profile; - size_t nitems; - struct icclient_ord_item *items[]; -}; - -#ifdef __cplusplus -extern "C" { -#endif - -/*! - * \brief For putting an item to a cart. - * \param sku The SKU of the item to order. - * \param catalog The catalog from which the item is. - * \param order The address of an order instance. - */ -void icclient_ord_order(const char *sku, const struct icclient_catalog *catalog, - struct icclient_ord_order **order); -void icclient_ord_checkout(const struct icclient_ord_order *order, const struct icclient_member *member); -void icclient_ord_free(struct icclient_ord_order *order); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/icclient/typedefs.h b/icclient/typedefs.h deleted file mode 100644 index 4bb6dcc..0000000 --- a/icclient/typedefs.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef ICCLIENT_TYPEDEFS_H -#define ICCLIENT_TYPEDEFS_H - -#include -#ifdef __EMSCRIPTEN__ -#include -typedef emscripten_fetch_t icclient_response; -#else -#include -typedef struct { - void *userData; - char *data; - size_t numBytes; - CURL *curl; -} icclient_response; -#endif - -#endif diff --git a/interchange.c b/interchange.c new file mode 100644 index 0000000..66497b8 --- /dev/null +++ b/interchange.c @@ -0,0 +1,107 @@ +#include +#include +#include "request.h" +#include "interchange.h" + +char *image_dir; +#ifdef __EMSCRIPTEN__ +emscripten_fetch_attr_t attr; +#else +char *sampleurl; +char *cainfo = NULL; +#endif + +extern void handle_results(interchange_response *); + +void interchange_init(const char *url, const char *dir, const char *certificate) +{ + image_dir = malloc(strlen(dir) + 1); + strcpy(image_dir, dir); +#ifdef __EMSCRIPTEN__ + emscripten_fetch_attr_init(&attr); + attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; +#else + size_t length = strlen(url); + size_t append = url[length - 1] != '/'; + sampleurl = malloc(length + append + 1); + strcpy(sampleurl, url); + if (append) + strcat(sampleurl, "/"); + curl_global_init(CURL_GLOBAL_SSL); + if (certificate) { + cainfo = malloc(strlen(certificate) + 1); + strcpy(cainfo, certificate); + } +#endif +} + +void interchange_catalog(const char *prod_group, void (*handler)(interchange_response *), void (*callback)(struct interchange_catalog *)) +{ + char nonspaced[strlen(prod_group) + 1]; + strcpy(nonspaced, prod_group); + char *space = NULL; + while ((space = strchr(nonspaced, ' '))) + *space = '-'; + request(handler ? handler : handle_results, (void (*)(void *))callback, NULL, "%s", nonspaced); +} + +void interchange_product(const char *sku, void (*handler)(interchange_response *), void (*callback)(struct interchange_product *)) +{ + request(handler, (void (*)(void *))callback, NULL, "%s", sku); +} + +void interchange_page(const char *path, void (*handler)(interchange_response *)) +{ + request(handler, NULL, NULL, "%s", path); +} + +void interchange_free_product(struct interchange_product *product) +{ + if (product->crosssell) + for (size_t i = 0; i < product->crosssell->length; i++) + free(product->crosssell->skus[i]); + if (product->author) + free(product->author); + if (product->prod_group) + free(product->prod_group); + if (product->image) + free(product->image); + if (product->thumb) + free(product->thumb); + if (product->comment) + free(product->comment); + if (product->description) + free(product->description); + free(product->sku); + free(product); +} + +void interchange_free_catalog(struct interchange_catalog *catalog) +{ + for (size_t i = 0; i < catalog->length; i++) + interchange_free_product(catalog->products[i]); + free(catalog); +} + +void interchange_free_response(interchange_response *response) +{ + if (response->userData) + free(response->userData); +#ifdef __EMSCRIPTEN__ + emscripten_fetch_close(response); +#else + free(response->data); + curl_easy_cleanup(response->curl); + free(response); +#endif +} + +void interchange_cleanup() +{ + free(image_dir); +#ifndef __EMSCRIPTEN__ + free(cainfo); + free(sampleurl); + curl_global_cleanup(); +#endif +} diff --git a/interchange.h b/interchange.h new file mode 100644 index 0000000..91a2222 --- /dev/null +++ b/interchange.h @@ -0,0 +1,77 @@ +#ifndef INTERCHANGE_H +#define INTERCHANGE_H + +#include "interchange/typedefs.h" + +struct interchange_product { + char *sku; + char *description; + char *comment; + char *thumb; + char *image; + double price; + char *prod_group; + char *category; + double weight; + char *author; + struct interchange_product_crosssell { + size_t length; + char *skus[]; + } *crosssell; +}; + +struct interchange_catalog { + size_t length; + struct interchange_product *products[]; +}; + +/*! + * \brief For fetching data about all active products. + * \param handler A pointer to the function when a custom handler is needed to arrange the data into the catalog. + * \param callback A pointer to the function that needs to be called after the catalog is ready. + */ +#define interchange_allproducts(handler, callback) interchange_catalog("All-Products", handler, callback) + +#ifdef __cplusplus +extern "C" { +#endif + +/*! + * \brief A function that needs to be run first. + * \param sampleurl The value of the SAMPLEURL setting in products/variable.txt. + * \param image_dir The value of the IMAGE_DIR setting in products/variable.txt. + * \param certificate Path to the CA certificate file. + */ +void interchange_init(const char *sampleurl, const char *image_dir, const char *certificate); + +/*! + * \brief For fetching data about products that belong a specific group. + * \param prod_group The name of the product group. + * \param handler A pointer to the function when a custom handler is needed to arrange the data into the catalog. + * \param callback A pointer to the function that needs to be called after the catalog is ready. + */ +void interchange_catalog(const char *prod_group, void (*handler)(interchange_response *), void (*callback)(struct interchange_catalog *)); + +/*! + * \brief For fetching data about a specific product. + * \param sku The SKU of the product. + * \param handler A pointer to the function when a custom handler is needed to arrange the data into the product. + * \param callback A pointer to the function that needs to be called after the product is ready. +*/ +void interchange_product(const char *sku, void (*handler)(interchange_response *), void (*callback)(struct interchange_product *)); + +void interchange_page(const char *path, void (*handler)(interchange_response *)); + +void interchange_free_product(struct interchange_product *product); + +void interchange_free_catalog(struct interchange_catalog *catalog); + +void interchange_free_response(interchange_response *response); + +void interchange_cleanup(); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/interchange/admin.h b/interchange/admin.h new file mode 100644 index 0000000..d0e47d7 --- /dev/null +++ b/interchange/admin.h @@ -0,0 +1,39 @@ +#ifndef INTERCHANGE_ADMIN_H +#define INTERCHANGE_ADMIN_H + +#include "typedefs.h" + +enum interchange_admin_group { + INTERCHANGE_ADMIN_GROUP_CONTENT, + INTERCHANGE_ADMIN_GROUP_MERCH, + INTERCHANGE_ADMIN_GROUP_ORDERS +}; + +struct interchange_admin { + char *username; + char *password; + char *name; + bool super; + enum interchange_admin_group group; +}; + +#ifdef __cplusplus +extern "C" { +#endif + +void interchange_admin_login(const char *username, const char *password, void (*handler)(interchange_response *), + void (*callback)(struct interchange_admin *)); +void interchange_admin_new_admin(const char *username, const char *password, const char *name, bool super, + enum interchange_admin_group group, void (*handler)(interchange_response *)); +void interchange_admin_new_item(const char *description, const char *comment, const char *price, + const char *image_path, void (*handler)(interchange_response *)); +void interchange_admin_new_transaction(const struct interchange_ord_order *order, + const struct interchange_member *member, bool new_customer_id, + void (*handler)(interchange_response *)); +void interchange_admin_logout(struct interchange_admin *admin, void (*handler)(interchange_response *)); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/interchange/member.h b/interchange/member.h new file mode 100644 index 0000000..0e9ca91 --- /dev/null +++ b/interchange/member.h @@ -0,0 +1,79 @@ +#ifndef INTERCHANGE_MEMBER_H +#define INTERCHANGE_MEMBER_H + +#include +#include "typedefs.h" + +struct interchange_member_preferences { + bool mv_same_billing; + bool email_copy; +}; + +struct interchange_member { + char *username; + char *usernick; + char *password; + char *expiration; + char *acl; + char *mod_time; + char *s_nickname; + char *company; + char *fname; /*!< Shipping first name */ + char *lname; /*!< Shipping last name */ + char *address1; + char *address2; + char *address3; + char *city; + char *state; + char *zip; + char *country; + char *phone_day; + char *mv_shipmode; + char *b_nickname; + char *b_fname; /*!< Billing first name */ + char *b_lname; /*!< Billing last name */ + char *b_company; + char *b_address1; + char *b_address2; + char *b_address3; + char *b_city; + char *b_state; + char *b_zip; + char *b_country; + char *b_phone; + char *p_nickname; + char *email; + char *fax; + char *phone_night; + char *address_book; + char *accounts; + struct interchange_member_preferences *preferences; + char *carts; + char *owner; + char *file_acl; + char *db_acl; + char *mail_list; + char *credit_limit; + bool inactive; + bool dealer; + char *price_level; +}; + +#ifdef __cplusplus +extern "C" { +#endif + + void interchange_member_newaccount(const char *username, const char *password, const char *verify, + void (*handler)(interchange_response *), void (*callback)(struct interchange_member *)); + void interchange_member_login(const char *username, const char *password, void (*handler)(interchange_response *), + void (*callback)(struct interchange_member *)); + void interchange_member_account(const char *fname, const char *lname, const char *address1, const char *address2, + const char *city, const char *state, const char *zip, const char *email, const char *phone_day); + void interchange_member_changepassword(const char *password_old, const char *password, const char *verify); + void interchange_member_logout(struct interchange_member *member); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/interchange/ord.h b/interchange/ord.h new file mode 100644 index 0000000..c62bcf0 --- /dev/null +++ b/interchange/ord.h @@ -0,0 +1,37 @@ +#ifndef INTERCHANGE_ORD_H +#define INTERCHANGE_ORD_H + +struct interchange_ord_item { + struct interchange_product *product; + unsigned int quantity; +}; + +struct interchange_ord_order { + double subtotal; + double shipping; + double total_cost; + char *profile; + size_t nitems; + struct interchange_ord_item *items[]; +}; + +#ifdef __cplusplus +extern "C" { +#endif + +/*! + * \brief For putting an item to a cart. + * \param sku The SKU of the item to order. + * \param catalog The catalog from which the item is. + * \param order The address of an order instance. + */ +void interchange_ord_order(const char *sku, const struct interchange_catalog *catalog, + struct interchange_ord_order **order); +void interchange_ord_checkout(const struct interchange_ord_order *order, const struct interchange_member *member); +void interchange_ord_free(struct interchange_ord_order *order); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/interchange/typedefs.h b/interchange/typedefs.h new file mode 100644 index 0000000..02a412f --- /dev/null +++ b/interchange/typedefs.h @@ -0,0 +1,18 @@ +#ifndef INTERCHANGE_TYPEDEFS_H +#define INTERCHANGE_TYPEDEFS_H + +#include +#ifdef __EMSCRIPTEN__ +#include +typedef emscripten_fetch_t interchange_response; +#else +#include +typedef struct { + void *userData; + char *data; + size_t numBytes; + CURL *curl; +} interchange_response; +#endif + +#endif diff --git a/login.c b/login.c index 1099479..4b529f9 100644 --- a/login.c +++ b/login.c @@ -1,3 +1,3 @@ #include "login.h" -extern inline void login(const char *, const char *, const char *, const char *, void (*)(icclient_response *), void (*)(void *)); +extern inline void login(const char *, const char *, const char *, const char *, void (*)(interchange_response *), void (*)(void *)); diff --git a/login.h b/login.h index dc0bc63..37d9066 100644 --- a/login.h +++ b/login.h @@ -1,10 +1,10 @@ -#ifndef ICCLIENT_LOGIN_H -#define ICCLIENT_LOGIN_H +#ifndef INTERCHANGE_LOGIN_H +#define INTERCHANGE_LOGIN_H #include "request.h" static inline void login(const char *username, const char *password, const char *verify, const char *click, - void (*handler)(icclient_response *), void (*callback)(void *)) + void (*handler)(interchange_response *), void (*callback)(void *)) { request(handler, callback, &(struct body){ 4, { { "mv_username", username }, diff --git a/main.c b/main.c index befa49a..d284055 100644 --- a/main.c +++ b/main.c @@ -2,14 +2,14 @@ #include #include #include -#include "icclient.h" -#include "icclient/member.h" -#include "icclient/admin.h" +#include "interchange.h" +#include "interchange/member.h" +#include "interchange/admin.h" -static void print_catalog(struct icclient_catalog *catalog) +static void print_catalog(struct interchange_catalog *catalog) { for (size_t i = 0; i < catalog->length; i++) { - struct icclient_product *product = catalog->products[i]; + struct interchange_product *product = catalog->products[i]; printf("sku: %s | " "desc: %s | " "img: %s | " @@ -22,19 +22,19 @@ static void print_catalog(struct icclient_catalog *catalog) product->category ); } - icclient_free_catalog(catalog); + interchange_free_catalog(catalog); } -static void print(icclient_response *response) +static void print(interchange_response *response) { printf("%s\n", response->data); } int main(int argc, char *argv[]) { - icclient_init("https://demo.interchangecommerce.org/i/demo", "/demo/images", NULL); - icclient_allproducts(NULL, print_catalog); - icclient_admin_login("demo", "demo", print, NULL); - icclient_member_login("kirk@icdevgroup.net", "kirk", print, NULL); - icclient_cleanup(); + interchange_init("https://demo.interchangecommerce.org/i/demo", "/demo/images", NULL); + interchange_allproducts(NULL, print_catalog); + interchange_admin_login("demo", "demo", print, NULL); + interchange_member_login("kirk@icdevgroup.net", "kirk", print, NULL); + interchange_cleanup(); } diff --git a/member.c b/member.c index a9f334a..29232ce 100644 --- a/member.c +++ b/member.c @@ -1,20 +1,20 @@ #include #include "login.h" -#include "icclient/member.h" +#include "interchange/member.h" -void icclient_member_newaccount(const char *username, const char *password, const char *verify, - void (*handler)(icclient_response *), void (*callback)(struct icclient_member *)) +void interchange_member_newaccount(const char *username, const char *password, const char *verify, + void (*handler)(interchange_response *), void (*callback)(struct interchange_member *)) { login(username, password, verify, "NewAccount", handler, (void (*)(void *))callback); } -void icclient_member_login(const char *username, const char *password, void (*handler)(icclient_response *), - void (*callback)(struct icclient_member *)) +void interchange_member_login(const char *username, const char *password, void (*handler)(interchange_response *), + void (*callback)(struct interchange_member *)) { login(username, password, NULL, "Login", handler, (void (*)(void *))callback); } -void icclient_member_account(const char *fname, const char *lname, const char *address1, +void interchange_member_account(const char *fname, const char *lname, const char *address1, const char *address2, const char *city, const char *state, const char *zip, const char *email, const char *phone_day) { @@ -35,7 +35,7 @@ void icclient_member_account(const char *fname, const char *lname, const char *a }}, "%s", "process"); } -void icclient_member_changepassword(const char *password_old, const char *password, const char *verify) +void interchange_member_changepassword(const char *password_old, const char *password, const char *verify) { request(NULL, NULL, &(struct body){ 6, { { "mv_action", "return" }, @@ -47,7 +47,7 @@ void icclient_member_changepassword(const char *password_old, const char *passwo }}, "%s", "member/change_password"); } -void icclient_member_logout(struct icclient_member *member) +void interchange_member_logout(struct interchange_member *member) { request(NULL, NULL, NULL, "%s", "logout"); if (member->username) diff --git a/ord.c b/ord.c index 90bb17c..5af1a91 100644 --- a/ord.c +++ b/ord.c @@ -1,56 +1,56 @@ #include #include #include "request.h" -#include "icclient.h" -#include "icclient/member.h" -#include "icclient/ord.h" +#include "interchange.h" +#include "interchange/member.h" +#include "interchange/ord.h" static int prodcmp(const void *product1, const void *product2) { - return strcmp((*(struct icclient_product * const *)product1)->sku, - (*(struct icclient_product * const *)product2)->sku); + return strcmp((*(struct interchange_product * const *)product1)->sku, + (*(struct interchange_product * const *)product2)->sku); } static int itemcmp(const void *item1, const void *item2) { - return strcmp((*(struct icclient_ord_item * const *)item1)->product->sku, - (*(struct icclient_ord_item * const *)item2)->product->sku); + return strcmp((*(struct interchange_ord_item * const *)item1)->product->sku, + (*(struct interchange_ord_item * const *)item2)->product->sku); } -void icclient_ord_order(const char *sku, const struct icclient_catalog *catalog, - struct icclient_ord_order **order) +void interchange_ord_order(const char *sku, const struct interchange_catalog *catalog, + struct interchange_ord_order **order) { - struct icclient_product **products = ((struct icclient_catalog *)catalog)->products; - qsort(products, catalog->length, sizeof(struct icclient_product *), prodcmp); - struct icclient_product *key_product = malloc(sizeof(struct icclient_product)); - memset(key_product, '\0', sizeof(struct icclient_product)); + struct interchange_product **products = ((struct interchange_catalog *)catalog)->products; + qsort(products, catalog->length, sizeof(struct interchange_product *), prodcmp); + struct interchange_product *key_product = malloc(sizeof(struct interchange_product)); + memset(key_product, '\0', sizeof(struct interchange_product)); key_product->sku = malloc(strlen(sku) + 1); strcpy(key_product->sku, sku); - struct icclient_product *product = *(struct icclient_product **)bsearch(&key_product, products, - catalog->length, sizeof(struct icclient_product *), prodcmp); - icclient_free_product(key_product); - struct icclient_ord_item *item = NULL; + struct interchange_product *product = *(struct interchange_product **)bsearch(&key_product, products, + catalog->length, sizeof(struct interchange_product *), prodcmp); + interchange_free_product(key_product); + struct interchange_ord_item *item = NULL; if (*order) { - struct icclient_ord_item **items = (*order)->items; - qsort(items, (*order)->nitems, sizeof(struct icclient_ord_item *), itemcmp); - struct icclient_ord_item *key_item = malloc(sizeof(struct icclient_ord_item)); + struct interchange_ord_item **items = (*order)->items; + qsort(items, (*order)->nitems, sizeof(struct interchange_ord_item *), itemcmp); + struct interchange_ord_item *key_item = malloc(sizeof(struct interchange_ord_item)); key_item->product = product; - struct icclient_ord_item **itemptr = bsearch(&key_item, items, (*order)->nitems, - sizeof(struct icclient_ord_item *), itemcmp); + struct interchange_ord_item **itemptr = bsearch(&key_item, items, (*order)->nitems, + sizeof(struct interchange_ord_item *), itemcmp); if (itemptr) item = *itemptr; free(key_item); } else { - *order = malloc(sizeof(struct icclient_ord_order)); - memset(*order, '\0', sizeof(struct icclient_ord_order)); + *order = malloc(sizeof(struct interchange_ord_order)); + memset(*order, '\0', sizeof(struct interchange_ord_order)); } if (item) item->quantity++; else { size_t i = (*order)->nitems; - *order = realloc(*order, sizeof(struct icclient_ord_order) + (i + 1) - * sizeof(struct icclient_ord_item)); - (*order)->items[i] = malloc(sizeof(struct icclient_ord_item)); + *order = realloc(*order, sizeof(struct interchange_ord_order) + (i + 1) + * sizeof(struct interchange_ord_item)); + (*order)->items[i] = malloc(sizeof(struct interchange_ord_item)); (*order)->nitems++; item = (*order)->items[i]; item->product = product; @@ -61,7 +61,7 @@ void icclient_ord_order(const char *sku, const struct icclient_catalog *catalog, request(NULL, NULL, NULL, "%s%s", "order?mv_arg=", sku); } -void icclient_ord_checkout(const struct icclient_ord_order *order, const struct icclient_member *member) +void interchange_ord_checkout(const struct interchange_ord_order *order, const struct interchange_member *member) { request(NULL, NULL, &(struct body){ 14, { { "mv_todo", "submit" }, @@ -81,8 +81,8 @@ void icclient_ord_checkout(const struct icclient_ord_order *order, const struct }}, "%s", "ord/checkout"); } -void icclient_ord_free(struct icclient_ord_order *order) +void interchange_ord_free(struct interchange_ord_order *order) { for (size_t i = 0; i < order->nitems; i++) - icclient_free_product(order->items[i]->product); + interchange_free_product(order->items[i]->product); } diff --git a/request.c b/request.c index 3b1d715..2719d0a 100644 --- a/request.c +++ b/request.c @@ -26,11 +26,11 @@ extern char *cainfo; struct container { struct curl_httppost *post; - void (*handler)(icclient_response *); - icclient_response *response; + void (*handler)(interchange_response *); + interchange_response *response; }; -static size_t append(char *data, size_t size, size_t nmemb, icclient_response *response) +static size_t append(char *data, size_t size, size_t nmemb, interchange_response *response) { size_t realsize = size * nmemb; response->data = realloc(response->data, response->numBytes + realsize + 1); @@ -64,7 +64,7 @@ async(void *arg) #ifdef DEBUG const char *error = curl_easy_strerror(res); #ifdef __ANDROID__ - __android_log_print(ANDROID_LOG_ERROR, "libicclient.so", "%s", error); + __android_log_print(ANDROID_LOG_ERROR, "libinterchange.so", "%s", error); #else fprintf(stderr, "%s\n", error); #endif @@ -79,7 +79,7 @@ async(void *arg) } #endif -void request(void (*handler)(icclient_response *), void (*callback)(void *), struct body *body, char *fmt, ...) +void request(void (*handler)(interchange_response *), void (*callback)(void *), struct body *body, char *fmt, ...) { va_list ap; char *p, *sval; @@ -177,7 +177,7 @@ void request(void (*handler)(icclient_response *), void (*callback)(void *), str #endif curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, append); - icclient_response *response = malloc(sizeof(icclient_response)); + interchange_response *response = malloc(sizeof(interchange_response)); response->data = malloc(1); response->numBytes = 0; response->userData = callback; diff --git a/request.h b/request.h index 3f67148..0b57cf7 100644 --- a/request.h +++ b/request.h @@ -1,7 +1,7 @@ #ifndef REQUEST_H #define REQUEST_H -#include "icclient/typedefs.h" +#include "interchange/typedefs.h" struct body { size_t num_pairs; @@ -11,6 +11,6 @@ struct body { } pairs[16]; }; -void request(void (*)(icclient_response *), void (*)(void *), struct body *, char *, ...); +void request(void (*)(interchange_response *), void (*)(void *), struct body *, char *, ...); #endif -- cgit v1.2.3