diff options
author | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-06-09 19:51:18 +0800 |
---|---|---|
committer | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-06-09 19:51:18 +0800 |
commit | c6e5f421e2738b6005f6c3da6700b1343214f7e5 (patch) | |
tree | f4c45010fac52e4c30610ddd4640fb5b36a602a8 | |
parent | a0e54cb948c9862936e16eedd2a2015be05408c8 (diff) |
Add support for Emscripten
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | Makefile.am | 20 | ||||
-rw-r--r-- | admin.c | 2 | ||||
-rw-r--r-- | client.c | 36 | ||||
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | icclient/admin.h | 2 | ||||
-rw-r--r-- | icclient/client.h | 15 | ||||
-rw-r--r-- | icclient/member.h | 6 | ||||
-rw-r--r-- | icclient/typedefs.h | 11 | ||||
-rw-r--r-- | login.c | 3 | ||||
-rw-r--r-- | login.h | 6 | ||||
-rw-r--r-- | member.c | 5 | ||||
-rw-r--r-- | request.c | 9 | ||||
-rw-r--r-- | request.h | 46 |
14 files changed, 111 insertions, 55 deletions
@@ -1,7 +1,9 @@ *~ +*.js *.la *.lo *.o +*.out *.swp *.wasm .deps @@ -19,7 +21,6 @@ configure configure.scan depcomp html -icclienttest install-sh libtool ltmain.sh diff --git a/Makefile.am b/Makefile.am index 66867b2..01f7b92 100644 --- a/Makefile.am +++ b/Makefile.am @@ -4,24 +4,32 @@ libicclient_la_SOURCES = \ request.c \ product.c \ catalog.c \ + client.c +if !WASM +libicclient_la_SOURCES += \ ord.c \ login.h \ login.c \ member.c \ - admin.c \ - client.c + admin.c +endif libicclient_la_CFLAGS = -I${prefix}/include -libicclient_la_LDFLAGS = $(CURL_LIBS) if WASM -libicclient_la_LDFLAGS += -static +libicclient_la_LDFLAGS = -static +else +libicclient_la_LDFLAGS = $(CURL_LIBS) endif if IOS libicclient_la_LDFLAGS += -static endif pkginclude_HEADERS = \ + icclient/typedefs.h \ icclient/product.h \ icclient/catalog.h \ + icclient/client.h +if !WASM +pkginclude_HEADERS += \ icclient/ord.h \ icclient/member.h \ - icclient/admin.h \ - icclient/client.h + icclient/admin.h +endif @@ -7,7 +7,7 @@ typedef struct icclient_admin icclient_admin; icclient_admin *icclient_admin_login(const char *username, const char *password, const char *successpage, const char *nextpage, const char *failpage, - size_t (*handler)(void *, size_t, size_t, void *)) + icclient_handler handler) { icclient_admin *admin = malloc(sizeof(icclient_admin)); admin->name = NULL; @@ -6,11 +6,20 @@ #include "icclient/catalog.h" #include "icclient/client.h" +#ifdef __EMSCRIPTEN__ +emscripten_fetch_attr_t attr; +#else CURL *curl = NULL; char *server_url = NULL; +#endif bool icclient_init(const char *url, const char *certificate) { +#ifdef __EMSCRIPTEN__ + emscripten_fetch_attr_init(&attr); + attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; + return true; +#else curl_global_init(CURL_GLOBAL_SSL); curl = curl_easy_init(); if (curl) { @@ -28,47 +37,42 @@ bool icclient_init(const char *url, const char *certificate) if (append) strcat(server_url, "/"); } - return (bool)curl; +#endif } -void icclient_results(const char *prod_group, - size_t (*handler)(void *, size_t, size_t, void *), - struct icclient_catalog **catalogptr) +void icclient_results(const char *prod_group, icclient_handler handler, struct icclient_catalog **catalogptr) { char nonspaced[strlen(prod_group) + 1]; strcpy(nonspaced, prod_group); char *space = NULL; while ((space = strchr(nonspaced, ' '))) *space = '-'; - request(handler, (void *)catalogptr, NULL, "%s", nonspaced); + request(handler, (void *)catalogptr, 0, "%s", nonspaced); } -void icclient_allproducts(size_t (*handler)(void *, size_t, size_t, void *), - struct icclient_catalog **catalogptr) +void icclient_allproducts(icclient_handler handler, struct icclient_catalog **catalogptr) { - request(handler, (void *)catalogptr, NULL, "%s", "All-Products"); + request(handler, (void *)catalogptr, 0, "%s", "All-Products"); } -void icclient_flypage(const char *sku, - size_t (*handler)(void *, size_t, size_t, void *), - struct icclient_product **productptr) +void icclient_flypage(const char *sku, icclient_handler handler, struct icclient_product **productptr) { - request(handler, (void *)productptr, NULL, "%s", sku); + request(handler, (void *)productptr, 0, "%s", sku); } -void icclient_page(const char *path, - size_t (*handler)(void *, size_t, size_t, void *), - void **dataptr) +void icclient_page(const char *path, icclient_handler handler, void **dataptr) { - request(handler, (void *)dataptr, NULL, "%s", path); + request(handler, (void *)dataptr, 0, "%s", path); } void icclient_cleanup() { +#ifndef __EMSCRIPTEN__ if (curl) { free(server_url); curl_easy_cleanup(curl); } curl_global_cleanup(); +#endif } diff --git a/configure.ac b/configure.ac index 66826d2..7ab0bc0 100644 --- a/configure.ac +++ b/configure.ac @@ -3,7 +3,7 @@ AM_INIT_AUTOMAKE([-Wall -Werror foreign]) AC_PROG_CC AC_CONFIG_HEADERS([config.h]) AC_CONFIG_FILES([Makefile]) -PKG_CHECK_MODULES([CURL], [libcurl]) +PKG_CHECK_EXISTS([CURL], [libcurl]) AC_CHECK_HEADER_STDBOOL AC_C_INLINE AC_FUNC_MALLOC diff --git a/icclient/admin.h b/icclient/admin.h index 1ba73c7..36ec0d6 100644 --- a/icclient/admin.h +++ b/icclient/admin.h @@ -15,7 +15,7 @@ extern "C" { struct icclient_admin *icclient_admin_login(const char *username, const char *password, const char *successpage, const char *nextpage, const char *failpage, - size_t (*handler)(void *, size_t, size_t, void *)); + icclient_handler handler); void icclient_admin_newitem(const char *description, const char *comment, const char *price, const char *image_path); void icclient_admin_logout(struct icclient_admin *admin); diff --git a/icclient/client.h b/icclient/client.h index 02e0bfa..00f9445 100644 --- a/icclient/client.h +++ b/icclient/client.h @@ -22,17 +22,14 @@ extern "C" { * \param handler A pointer to a cURL write function callback. * \param catalogptr A pointer to pointer to the catalog to store the data. */ - void icclient_results(const char *prod_group, - size_t (*handler)(void *, size_t, size_t, void *), - struct icclient_catalog **catalogptr); + void icclient_results(const char *prod_group, icclient_handler handler, struct icclient_catalog **catalogptr); /*! * \brief For fetching data about all active products. * \param handler A pointer to a cURL write function callback. * \param catalogptr A pointer to pointer to the catalog to store the data. */ - void icclient_allproducts(size_t (*handler)(void *, size_t, size_t, void *), - struct icclient_catalog **catalogptr); + void icclient_allproducts(icclient_handler handler, struct icclient_catalog **catalogptr); /*! * \brief For fetching data about a specific product. @@ -40,13 +37,9 @@ extern "C" { * \param handler A pointer to a cURL write function callback. * \param productptr A pointer to pointer to the product to store the data. */ - void icclient_flypage(const char *sku, - size_t (*handler)(void *, size_t, size_t, void *), - struct icclient_product **productptr); + void icclient_flypage(const char *sku, icclient_handler handler, struct icclient_product **productptr); - void icclient_page(const char *path, - size_t (*handler)(void *, size_t, size_t, void *), - void **dataptr); + void icclient_page(const char *path, icclient_handler handler, void **dataptr); void icclient_cleanup(); #ifdef __cplusplus diff --git a/icclient/member.h b/icclient/member.h index 0f4ac22..1c0943a 100644 --- a/icclient/member.h +++ b/icclient/member.h @@ -62,12 +62,10 @@ extern "C" { struct icclient_member *icclient_member_newaccount(const char *username, const char *password, const char *verify, const char *successpage, - const char *nextpage, const char *failpage, - size_t (*handler)(void *, size_t, size_t, void *)); + const char *nextpage, const char *failpage, icclient_handler handler); struct icclient_member *icclient_member_login(const char *username, const char *password, const char *successpage, - const char *nextpage, const char *failpage, - size_t (*handler)(void *, size_t, size_t, void *)); + const char *nextpage, const char *failpage, icclient_handler handler); 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, diff --git a/icclient/typedefs.h b/icclient/typedefs.h new file mode 100644 index 0000000..1b2f176 --- /dev/null +++ b/icclient/typedefs.h @@ -0,0 +1,11 @@ +#ifndef ICCLIENT_TYPEDEFS_H +#define ICCLIENT_TYPEDEFS_H + +#ifdef __EMSCRIPTEN__ +#include <emscripten/fetch.h> +typedef void (*icclient_handler)(emscripten_fetch_t *); +#else +typedef size_t (*icclient_handler)(void *, size_t, size_t, void *); +#endif + +#endif @@ -1,5 +1,4 @@ #include "login.h" extern inline void login(const char *, const char *, const char *, const char *, - const char *, const char *, const char *, - size_t (*)(void *, size_t, size_t, void *), void *); + const char *, const char *, const char *, icclient_handler, void *); @@ -3,10 +3,8 @@ #include "request.h" -inline void login(const char *username, const char *password, const char *verify, - const char *click, const char *successpage, const char *nextpage, - const char *failpage, size_t (*handler)(void *, size_t, size_t, void *), - void *user) +inline void login(const char *username, const char *password, const char *verify, const char *click, + const char *successpage, const char *nextpage, const char *failpage, icclient_handler handler, void *user) { struct curl_httppost *post, *last = NULL; curl_formadd(&post, &last, @@ -60,7 +60,7 @@ icclient_member *initialise(const char *username, const char *password) icclient_member *icclient_member_newaccount(const char *username, const char *password, const char *verify, const char *successpage, const char *nextpage, - const char *failpage, size_t (*handler)(void *, size_t, size_t, void *)) + const char *failpage, icclient_handler handler) { icclient_member *member = initialise(username, password); login(username, password, verify, "NewAccount", successpage, nextpage, failpage, @@ -69,8 +69,7 @@ icclient_member *icclient_member_newaccount(const char *username, const char *pa } icclient_member *icclient_member_login(const char *username, const char *password, - const char *successpage, const char *nextpage, const char *failpage, - size_t (*handler)(void *, size_t, size_t, void *)) + const char *successpage, const char *nextpage, const char *failpage, icclient_handler handler) { icclient_member *member = initialise(username, password); login(username, password, NULL, "Login", successpage, nextpage, failpage, handler, @@ -1,4 +1,9 @@ #include "request.h" -extern inline void request(size_t (*)(void *, size_t, size_t, void *), void * - , struct curl_httppost *, char *, ...); +extern inline void request(icclient_handler, void *, +#ifdef __EMSCRIPTEN__ + int +#else + struct curl_httppost * +#endif + , char *, ...); @@ -7,18 +7,34 @@ #include <string.h> #include <stdarg.h> #include <stdio.h> +#ifndef __EMSCRIPTEN__ #include <curl/curl.h> +#endif +#include "icclient/typedefs.h" +#ifdef __EMSCRIPTEN__ +extern emscripten_fetch_attr_t attr; +#else extern CURL *curl; extern char *server_url; +#endif -inline void request(size_t (*writefunction)(void *, size_t, size_t, void *), - void *writedata, struct curl_httppost *post, char *fmt, ...) +inline void request(icclient_handler writefunction, void *writedata, +#ifdef __EMSCRIPTEN__ + int +#else + struct curl_httppost * +#endif + post, char *fmt, ...) { va_list ap; char *p, *sval; unsigned int ival; - size_t length = strlen(server_url) + strlen(fmt); + size_t length = +#ifndef __EMSCRIPTEN__ + strlen(server_url) + +#endif + strlen(fmt); va_start(ap, fmt); for (p = fmt; *p; p++) { @@ -43,7 +59,11 @@ inline void request(size_t (*writefunction)(void *, size_t, size_t, void *), va_end(ap); char url[length + 1]; +#ifdef __EMSCRIPTEN__ + memset(url, 0, length + 1); +#else strcpy(url, server_url); +#endif va_start(ap, fmt); for (p = fmt; *p; p++) { @@ -64,17 +84,36 @@ inline void request(size_t (*writefunction)(void *, size_t, size_t, void *), } va_end(ap); +#ifdef __EMSCRIPTEN__ + attr.onsuccess = writefunction; +#else curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunction); +#endif if (writedata) +#ifdef __EMSCRIPTEN__ + attr.userData = writedata; +#else curl_easy_setopt(curl, CURLOPT_WRITEDATA, writedata); else curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout); +#endif if (post) +#ifdef __EMSCRIPTEN__ + strcpy(attr.requestMethod, "POST"); +#else curl_easy_setopt(curl, CURLOPT_HTTPPOST, post); +#endif else +#ifdef __EMSCRIPTEN__ + strcpy(attr.requestMethod, "GET"); +#else curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); +#endif +#ifdef __EMSCRIPTEN__ + emscripten_fetch(&attr, url); +#else #ifdef DEBUG CURLcode res = #endif @@ -87,6 +126,7 @@ inline void request(size_t (*writefunction)(void *, size_t, size_t, void *), #else fprintf(stderr, "%s: %s\n", __func__, error); #endif +#endif } #endif } |