summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2021-07-20 22:33:29 +0800
committerꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2021-07-20 22:33:29 +0800
commitd7a0867335f3aef16baad0fe138f1d09ef29d928 (patch)
treef07819fe70ee9d369cb93ee12b870caaeb461965
parent1264915af1cd1f47a34873360c4e88ae73a1bd33 (diff)
Function for listing services ready in HTML
-rw-r--r--pikul.c46
-rw-r--r--pikul.h3
-rw-r--r--pikul.i2
3 files changed, 51 insertions, 0 deletions
diff --git a/pikul.c b/pikul.c
index 2255081..4a8245e 100644
--- a/pikul.c
+++ b/pikul.c
@@ -5,6 +5,15 @@
#include <json.h>
#include "private.h"
+#define SELECT \
+"<select name=\"%s\" %s>\n\
+%s\
+\t\t\t\t\t\t\t\t\t\t\t</select>"
+#define SELECT_NUM_PARAMS 3
+#define OPTION \
+"\t\t\t\t\t\t\t\t\t\t\t\t<option value=\"%s\"%s>%s%s</option>\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 *,