From 7270c84fe616357da17baca20437ef1113bb87eb 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: Tue, 16 May 2023 15:13:17 +0800 Subject: Replace struct body with const char *[][2] --- admin.c | 118 +++++++++++++++++++++++++++++++++----------------------------- login.h | 20 +++++------ member.c | 51 ++++++++++++++------------- ord.c | 62 ++++++++++++++++++--------------- request.c | 37 ++++++++++---------- request.h | 11 ++---- 6 files changed, 153 insertions(+), 146 deletions(-) diff --git a/admin.c b/admin.c index 7ce6ad2..fec0fcd 100644 --- a/admin.c +++ b/admin.c @@ -16,75 +16,81 @@ void interchange_admin_login(const char *username, const char *password, failpage, handler, (void (*)(void *))callback); } -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_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" }, - { "mv_data_table", "access" }, - { "mv_data_key", "username" }, - { "mv_update_empty", "1" }, - { "mv_data_fields", "username password name super groups" }, - { "mv_click", "process_filter" }, - { "user_id", "NEW" }, - { "name", name }, - { "mv_data_function", "insert" }, - { "username", username }, - { "password", password }, - { "super", super ? "1" : "0" }, - { "groups", group == INTERCHANGE_ADMIN_GROUP_CONTENT ? ":CONTENT" - : group == INTERCHANGE_ADMIN_GROUP_MERCH ? ":MERCH" : ":ORDERS" } - }}, "%s", "ui"); + request(handler, NULL, (const char *[][2]){ + "mv_todo", "set", + "mv_data_table", "access", + "mv_data_key", "username", + "mv_update_empty", "1", + "mv_data_fields", "username password name super groups", + "mv_click", "process_filter", + "user_id", "NEW", + "name", name, + "mv_data_function", "insert", + "username", username, + "password", password, + "super", super ? "1" : "0", + "groups", group == INTERCHANGE_ADMIN_GROUP_CONTENT ? ":CONTENT" + : group == INTERCHANGE_ADMIN_GROUP_MERCH ? ":MERCH" : ":ORDERS" + }, "%s", "ui"); } -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_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" }, - { "mv_data_fields", "sku description prod_group category comment inactive price wholesale" - "image thumb image_large weight nontaxable gift_cert" }, - { "mv_ui", "1" }, - { "ui_new_item", "1" }, - { "mv_todo", "set" }, - { "mv_update_empty", "1" }, - { "mv_action", "set" }, - { "mv_data_table", "products" }, - { "mv_data_function", "insert" }, - { "mv_data_key", "sku" }, - { "mv_return_table", "products" }, - { "sku", image_path }, - { "description", description }, - { "comment", comment }, - { "price", price } + request(handler, NULL, (const char *[][2]){ + "mv_click", "process_filter", + "mv_data_fields", + "sku description prod_group category comment inactive price " + "wholesale image thumb image_large weight nontaxable gift_cert", + "mv_ui", "1", + "ui_new_item", "1", + "mv_todo", "set", + "mv_update_empty", "1", + "mv_action", "set", + "mv_data_table", "products", + "mv_data_function", "insert", + "mv_data_key", "sku", + "mv_return_table", "products", + "sku", image_path, + "description", description, + "comment", comment, + "price", price, /* - { "image", + "image", CURLFORM_FILE, image_path, - CURLFORM_CONTENTTYPE, "image/jpeg" }, - { "mv_data_file_field", "image" }, - { "mv_data_file_path", "images/items" } + CURLFORM_CONTENTTYPE, "image/jpeg", + "mv_data_file_field", "image", + "mv_data_file_path", "images/items" */ - }}, "%s", "admin/item_edit"); + NULL + }, "%s", "admin/item_edit"); } 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" }, - { "order_desk_entry", "1" }, - { "customer_id", member->username }, - { "new_customer_id", new_customer_id ? "1" : "0" }, - { "fname", member->fname }, - { "lname", member->lname }, - { "address1", member->address1 }, - { "city", member->city }, - { "zip", member->zip }, - { "email", member->email }, - { "country", member->country }, - { "phone_day", member->phone_day } - }}, "%s", "process"); + request(handler, NULL, (const char *[][2]){ + "mv_values_space", "order_entry", + "order_desk_entry", "1", + "customer_id", member->username, + "new_customer_id", new_customer_id ? "1" : "0", + "fname", member->fname, + "lname", member->lname, + "address1", member->address1, + "city", member->city, + "zip", member->zip, + "email", member->email, + "country", member->country, + "phone_day", member->phone_day, + NULL + }, "%s", "process"); } void interchange_admin_logout() diff --git a/login.h b/login.h index 6df9b0c..19f83ea 100644 --- a/login.h +++ b/login.h @@ -9,16 +9,16 @@ static inline void login(const char *username, const char *password, void (*handler)(interchange_response *), void (*callback)(void *)) { - request(handler, callback, &(struct body){ 5 + (successpage ? 1 : 0) - + (failpage ? 1 : 0), { - { "mv_username", username }, - { "mv_password", password }, - { "mv_verify", verify }, - { "mv_click", click }, - { "mv_nextpage", nextpage }, - { "mv_successpage", successpage }, - { "mv_failpage", failpage }, - }}, "%s", "process"); + request(handler, callback, (const char *[][2]){ + "mv_username", username, + "mv_password", password, + "mv_verify", verify, + "mv_click", click, + "mv_nextpage", nextpage, + "mv_successpage", successpage, + "mv_failpage", failpage, + NULL + }, "%s", "process"); } #endif diff --git a/member.c b/member.c index 6034c10..8f97622 100644 --- a/member.c +++ b/member.c @@ -31,33 +31,36 @@ void interchange_member_account(void (*handler)(interchange_response *)) void interchange_member_setaccount(const struct interchange_member *member, void (*handler)(interchange_response *)) { - request(handler, NULL, &(struct body){ 13, { - { "mv_form_profile", "account_change" }, - { "mv_todo", "return" }, - { "mv_nextpage", "member/account" }, - { "mv_check", "Save_database" }, - { "fname", member->fname }, - { "lname", member->lname }, - { "address1", member->address1 }, - { "address2", member->address2 }, - { "city", member->city }, - { "state", member->state }, - { "zip", member->zip }, - { "email", member->email }, - { "phone_day", member->phone_day } - }}, "%s", "process"); + request(handler, NULL, (const char *[][2]){ + "mv_form_profile", "account_change", + "mv_todo", "return", + "mv_nextpage", "member/account", + "mv_check", "Save_database", + "fname", member->fname, + "lname", member->lname, + "address1", member->address1, + "address2", member->address2, + "city", member->city, + "state", member->state, + "zip", member->zip, + "email", member->email, + "phone_day", member->phone_day, + NULL + }, "%s", "process"); } -void interchange_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" }, - { "mv_check", "Change_password" }, - { "mv_successpage", "member/service" }, - { "mv_password_old", password_old }, - { "mv_password", password }, - { "mv_verify", verify } - }}, "%s", "member/change_password"); + request(NULL, NULL, (const char *[][2]){ + "mv_action", "return", + "mv_check", "Change_password", + "mv_successpage", "member/service", + "mv_password_old", password_old, + "mv_password", password, + "mv_verify", verify, + NULL + }, "%s", "member/change_password"); } void interchange_member_logout() diff --git a/ord.c b/ord.c index 4964931..c9b8f22 100644 --- a/ord.c +++ b/ord.c @@ -17,12 +17,13 @@ void interchange_ord_order(const char *sku, const char *item, } while ((qty /= 10)); char *qty_str = malloc(length + 1); sprintf(qty_str, "%d", quantity); - request(parser, NULL, &(struct body){ 4, { - { "mv_action", "refresh" }, - { "mv_sku", sku }, - { "mv_order_item", item }, - { "mv_order_quantity", qty_str } - }}, "%s", "ord/basket"); + request(parser, NULL, (const char *[][2]){ + "mv_action", "refresh", + "mv_sku", sku, + "mv_order_item", item, + "mv_order_quantity", qty_str, + NULL + }, "%s", "ord/basket"); } void interchange_ord_update(const char *name, const unsigned int quantity, @@ -36,35 +37,38 @@ void interchange_ord_update(const char *name, const unsigned int quantity, } while ((qty /= 10)); char *qty_str = malloc(length + 1); sprintf(qty_str, "%d", quantity); - request(parser, NULL, &(struct body){ 4 + (nextpage ? 1 : 0), { - { "mv_quantity_update", "1" }, - { "mv_doit", "refresh" }, - { name, qty_str }, - { "mv_orderpage", orderpage ? orderpage : "ord/basket" }, - { "mv_nextpage", nextpage } - }}, "%s", "process"); + request(parser, NULL, (const char *[][2]){ + "mv_quantity_update", "1", + "mv_doit", "refresh", + name, qty_str, + "mv_orderpage", orderpage ? orderpage : "ord/basket", + "mv_nextpage", nextpage, + NULL + }, "%s", "process"); } void interchange_ord_checkout(const char *order_profile, struct interchange_member member, void (*handler)(interchange_response *)) { - request(handler, NULL, &(struct body){ 14, { - { "mv_todo", "submit" }, - { "mv_action", "refresh" }, - { "mv_order_profile", order_profile }, - { "fname", member.fname }, - { "lname", member.lname }, - { "address1", member.address1 }, - { "address2", member.address2 }, - { "city", member.city }, - { "state", member.state }, - { "zip", member.zip }, - { "email", member.email }, - { "phone_day", member.phone_day }, - { "mv_same_billing", member.preferences.mv_same_billing? "1" : "0" }, - { "email_copy", member.preferences.email_copy? "1" : "0" } - }}, "%s", "ord/checkout"); + request(handler, NULL, (const char *[][2]){ + "mv_todo", "submit", + "mv_action", "refresh", + "mv_order_profile", order_profile, + "fname", member.fname, + "lname", member.lname, + "address1", member.address1, + "address2", member.address2, + "city", member.city, + "state", member.state, + "zip", member.zip, + "email", member.email, + "phone_day", member.phone_day, + "mv_same_billing", + member.preferences.mv_same_billing ? "1" : "0", + "email_copy", member.preferences.email_copy? "1" : "0", + NULL + }, "%s", "ord/checkout"); } void interchange_ord_free_order(struct interchange_ord_order *order) diff --git a/request.c b/request.c index 2ab8fc4..c20587d 100644 --- a/request.c +++ b/request.c @@ -59,7 +59,8 @@ static void *async(void *arg) } #endif -void request(void (*handler)(interchange_response *), void (*callback)(void *), struct body *body, char *fmt, ...) +void request(void (*handler)(interchange_response *), void (*callback)(void *), + const char *body[][2], char *fmt, ...) { va_list ap; char *p, *sval; @@ -125,17 +126,18 @@ void request(void (*handler)(interchange_response *), void (*callback)(void *), size_t length = 0; char *post = malloc(1); memset(post, '\0', 1); - for (size_t i = 0; i < body->num_pairs; i++) { - struct pair pair = body->pairs[i]; - if (!pair.value) - continue; - length += strlen(pair.key) + strlen(pair.value) + (i ? 1 : 0) + 1; + size_t i = 0; + const char **pair = *body; + while (pair[0] && pair[1]) { + length += strlen(pair[0]) + strlen(pair[1]) + + (i ? 1 : 0) + 1; post = realloc(post, length + 1); - if (i) + if (i++) strcat(post, "&"); - sprintf(post, "%s%s=%s", post, pair.key, pair.value); - if (!strncmp(pair.key, "quantity", 8)) - free(pair.value); + sprintf(post, "%s%s=%s", post, pair[0], pair[1]); + if (!strncmp(pair[0], "quantity", 8)) + free((void *)pair[1]); + pair = *++body; } strcpy(attr.requestMethod, "POST"); static const char *headers[] = { "Content-Type", @@ -171,15 +173,14 @@ void request(void (*handler)(interchange_response *), void (*callback)(void *), struct curl_httppost *post = NULL; struct curl_httppost *last = NULL; if (body) { - for (size_t i = 0; i < body->num_pairs; i++) { - struct pair pair = body->pairs[i]; - if (!pair.value) - continue; - curl_formadd(&post, &last, CURLFORM_COPYNAME, pair.key, - CURLFORM_COPYCONTENTS, pair.value, + const char **pair = *body; + while (pair[0] && pair[1]) { + curl_formadd(&post, &last, CURLFORM_COPYNAME, pair[0], + CURLFORM_COPYCONTENTS, pair[1], CURLFORM_END); - if (!strncmp(pair.key, "quantity", 8)) - free(pair.value); + if (!strncmp(pair[0], "quantity", 8)) + free((void *)pair[1]); + pair = *++body; } last = NULL; curl_easy_setopt(curl, CURLOPT_HTTPPOST, post); diff --git a/request.h b/request.h index 0b57cf7..e27d284 100644 --- a/request.h +++ b/request.h @@ -3,14 +3,7 @@ #include "interchange/typedefs.h" -struct body { - size_t num_pairs; - struct pair { - const char *key; - const char *value; - } pairs[16]; -}; - -void request(void (*)(interchange_response *), void (*)(void *), struct body *, char *, ...); +void request(void (*)(interchange_response *), void (*)(void *), + const char *[][2], char *, ...); #endif -- cgit v1.2.3