From d7a0867335f3aef16baad0fe138f1d09ef29d928 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, 20 Jul 2021 22:33:29 +0800 Subject: Function for listing services ready in HTML --- pikul.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ pikul.h | 3 +++ pikul.i | 2 ++ 3 files changed, 51 insertions(+) diff --git a/pikul.c b/pikul.c index 2255081..4a8245e 100644 --- a/pikul.c +++ b/pikul.c @@ -5,6 +5,15 @@ #include #include "private.h" +#define SELECT \ +"" +#define SELECT_NUM_PARAMS 3 +#define OPTION \ +"\t\t\t\t\t\t\t\t\t\t\t\t\n" +#define OPTION_NUM_PARAMS 4 + CURL *curl; json_tokener *tokener; struct shipping shipping; @@ -174,6 +183,43 @@ static int servicecmp(const void *service1, const void *service2) (*(struct pikul_service * const *)service2)->code); } +char *pikul_html(const char *origin, const char *destination, double weight, + const char *widget, const char *extra, const char *name, const char *value, + char *code_prefixes[], char *name_prefixes[]) +{ + struct pikul_services *services = pikul_services(origin, destination, weight); + char *html; + if (!strcmp(widget, "select")) { + char *options = NULL; + for (size_t i = 0; i < services->length; i++) { + struct pikul_service *service = services->list[i]; + char *code_prefix = code_prefixes[shipping.company]; + char *name_prefix = name_prefixes[shipping.company]; + size_t code_length = strlen(code_prefix) + strlen(service->code); + char code[code_length + 1]; + sprintf(code, "%s%s", code_prefix, service->code); + _Bool selected = !strcmp(code, value); + size_t length = strlen(OPTION) + code_length + (selected ? strlen(" selected") : 0) + + strlen(name_prefix) + strlen(service->name) + - OPTION_NUM_PARAMS * strlen("%s"); + char option[length + 1]; + sprintf(option, OPTION, code, selected ? " selected" : "", + name_prefix, service->name); + if (options) + options = realloc(options, strlen(options) + length + 1); + else { + options = malloc(length + 1); + memset(options, '\0', strlen(options)); + } + strcat(options, option); + } + html = malloc(strlen(SELECT) + strlen(name) + (extra ? strlen(extra) : 0) + strlen(options) + - SELECT_NUM_PARAMS * strlen("%s") + 1); + sprintf(html, SELECT, name, extra ? extra : "", options); + } + return html; +} + char **pikul_codes(const char *origin, const char *destination, double weight) { char **codes = malloc(sizeof(char *)); diff --git a/pikul.h b/pikul.h index 07c6880..7d46197 100644 --- a/pikul.h +++ b/pikul.h @@ -24,6 +24,9 @@ extern "C" { void pikul_init(enum pikul_company company, char *provisions[]); struct pikul_services *pikul_services(const char *origin, const char *destination, double weight); void pikul_free_services(struct pikul_services *services); +char *pikul_html(const char *origin, const char *destination, double weight, + const char *widget, const char *extra, const char *name, const char *value, + char *code_prefixes[], char *name_prefixes[]); char **pikul_codes(const char *origin, const char *destination, double weight); double pikul_cost(const char *origin, const char *destination, double weight, const char *service); char *pikul_order(const char *order_number, const char *service, const char *sender_name, diff --git a/pikul.i b/pikul.i index fca870a..d217982 100644 --- a/pikul.i +++ b/pikul.i @@ -58,6 +58,8 @@ %rename("%(strip:[pikul_])s") ""; void pikul_init(enum pikul_company, char *[]); +char *pikul_html(const char *, const char *, double, const char *, const char *, const char *, const char *, + char *[], char *[]); char **pikul_codes(const char *, const char *, double); double pikul_cost(const char *, const char *, double, const char *); char *pikul_order(const char *, const char *, const char *, const char *, const char *, const char *, -- cgit v1.2.3