From c6e5f421e2738b6005f6c3da6700b1343214f7e5 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: Wed, 9 Jun 2021 19:51:18 +0800 Subject: Add support for Emscripten --- .gitignore | 3 ++- Makefile.am | 20 ++++++++++++++------ admin.c | 2 +- client.c | 36 ++++++++++++++++++++---------------- configure.ac | 2 +- icclient/admin.h | 2 +- icclient/client.h | 15 ++++----------- icclient/member.h | 6 ++---- icclient/typedefs.h | 11 +++++++++++ login.c | 3 +-- login.h | 6 ++---- member.c | 5 ++--- request.c | 9 +++++++-- request.h | 46 +++++++++++++++++++++++++++++++++++++++++++--- 14 files changed, 111 insertions(+), 55 deletions(-) create mode 100644 icclient/typedefs.h diff --git a/.gitignore b/.gitignore index 7b3df9b..22044aa 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/admin.c b/admin.c index 28fdb08..d0490a5 100644 --- a/admin.c +++ b/admin.c @@ -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; diff --git a/client.c b/client.c index fa59587..0dad4c9 100644 --- a/client.c +++ b/client.c @@ -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 +typedef void (*icclient_handler)(emscripten_fetch_t *); +#else +typedef size_t (*icclient_handler)(void *, size_t, size_t, void *); +#endif + +#endif diff --git a/login.c b/login.c index 0a5bcb2..8beb856 100644 --- a/login.c +++ b/login.c @@ -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 *); diff --git a/login.h b/login.h index 903adf8..1501993 100644 --- a/login.h +++ b/login.h @@ -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, diff --git a/member.c b/member.c index 971bd26..16488c1 100644 --- a/member.c +++ b/member.c @@ -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, diff --git a/request.c b/request.c index 7581fe7..6b6a07d 100644 --- a/request.c +++ b/request.c @@ -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 *, ...); diff --git a/request.h b/request.h index a943687..73b26d8 100644 --- a/request.h +++ b/request.h @@ -7,18 +7,34 @@ #include #include #include +#ifndef __EMSCRIPTEN__ #include +#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 @@ -86,6 +125,7 @@ inline void request(size_t (*writefunction)(void *, size_t, size_t, void *), __android_log_print(ANDROID_LOG_ERROR, "libicclient.so", "%s: %s", __func__, error); #else fprintf(stderr, "%s: %s\n", __func__, error); +#endif #endif } #endif -- cgit v1.2.3