summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2021-06-10 17:13:45 +0800
committerꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2021-06-10 17:13:45 +0800
commit3f96d8c84f7362b1800fcc8fdf3d0395ec265ca5 (patch)
treeeaafdcbba77295106477c4c706f5e36118123b83
parent06bea8ae54dd3cd714b231b3d8a8deef77136613 (diff)
Anticipate asynchronous data fetch
-rw-r--r--Makefile.am6
-rw-r--r--catalog.c15
-rw-r--r--client.c12
-rw-r--r--configure.ac2
-rw-r--r--icclient/catalog.h5
-rw-r--r--main.c36
6 files changed, 52 insertions, 24 deletions
diff --git a/Makefile.am b/Makefile.am
index 680bc0e..5ba54b0 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -17,7 +17,7 @@ libicclient_la_CPPFLAGS = -I${prefix}/include $(DEPS_CFLAGS)
if WASM
libicclient_la_LDFLAGS = -static
else
-libicclient_la_LDFLAGS = $(DEPS_LIBS)
+libicclient_la_LDFLAGS = $(DEPS_LIBS) -lcurl
endif
if IOS
libicclient_la_LDFLAGS += -static
@@ -34,7 +34,9 @@ pkginclude_HEADERS += \
icclient/admin.h
endif
+if !WASM
bin_PROGRAMS = testicclient
testicclient_SOURCES = main.c client.c catalog.c product.c request.c
testicclient_CPPFLAGS = -I${prefix}/include $(DEPS_CFLAGS)
-testicclient_LDFLAGS = $(DEPS_LIBS)
+testicclient_LDFLAGS = $(DEPS_LIBS) -lcurl
+endif
diff --git a/catalog.c b/catalog.c
index b7f7665..77d5030 100644
--- a/catalog.c
+++ b/catalog.c
@@ -1,6 +1,10 @@
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <json.h>
+#ifdef __EMSCRIPTEN__
+#include <emscripten/fetch.h>
+#endif
#include "icclient/product.h"
#include "icclient/catalog.h"
@@ -11,7 +15,11 @@ void icclient_catalog_init()
tokener = json_tokener_new();
}
+#ifdef __EMSCRIPTEN__
+void icclient_catalog_results(emscripten_fetch_t *fetch)
+#else
size_t icclient_catalog_results(void *contents, size_t size, size_t nmemb, void *userData)
+#endif
{
size_t realsize =
#ifdef __EMSCRIPTEN__
@@ -20,9 +28,11 @@ size_t icclient_catalog_results(void *contents, size_t size, size_t nmemb, void
size * nmemb
#endif
;
+#ifndef __EMSCRIPTEN__
char data[realsize];
memcpy(data, contents, realsize - 1);
data[realsize - 1] = '\0';
+#endif
json_object *products = json_tokener_parse_ex(tokener,
#ifdef __EMSCRIPTEN__
fetch->
@@ -51,11 +61,12 @@ size_t icclient_catalog_results(void *contents, size_t size, size_t nmemb, void
#endif
;
size_t length = json_object_array_length(products);
- struct icclient_catalog **catalogptr = (struct icclient_catalog **)
+ struct icclient_catalog_callback *catalog_callback = (struct icclient_catalog_callback *)
#ifdef __EMSCRIPTEN__
fetch->
#endif
userData;
+ struct icclient_catalog **catalogptr = catalog_callback->catalog;
*catalogptr = malloc(sizeof(struct icclient_catalog) + sizeof(struct icclient_product *[length]));
struct icclient_catalog *catalog = *catalogptr;
catalog->length = length;
@@ -91,6 +102,8 @@ size_t icclient_catalog_results(void *contents, size_t size, size_t nmemb, void
json_object_iter_next(&iterator);
}
}
+ catalog_callback->callback(catalog);
+ free(catalog_callback);
#ifdef __EMSCRIPTEN__
emscripten_fetch_close(fetch);
#else
diff --git a/client.c b/client.c
index 8813db9..5feac65 100644
--- a/client.c
+++ b/client.c
@@ -8,13 +8,16 @@
#ifdef __EMSCRIPTEN__
emscripten_fetch_attr_t attr;
+extern void icclient_catalog_results(emscripten_fetch_t *);
#else
CURL *curl = NULL;
char *server_url = NULL;
+extern size_t icclient_catalog_results(void *, size_t, size_t, void *);
#endif
bool icclient_init(const char *url, const char *certificate)
{
+ icclient_catalog_init();
#ifdef __EMSCRIPTEN__
emscripten_fetch_attr_init(&attr);
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
@@ -37,13 +40,10 @@ bool icclient_init(const char *url, const char *certificate)
if (append)
strcat(server_url, "/");
}
- icclient_catalog_init();
return (bool)curl;
#endif
}
-extern size_t icclient_catalog_results(void *, size_t, size_t, void *);
-
void icclient_results(const char *prod_group,
void (*callback)(struct icclient_catalog *), struct icclient_catalog **catalog, icclient_handler handler)
{
@@ -52,8 +52,10 @@ void icclient_results(const char *prod_group,
char *space = NULL;
while ((space = strchr(nonspaced, ' ')))
*space = '-';
- request(handler ? handler : icclient_catalog_results, (void *)catalog, 0, "%s", nonspaced);
- callback(*catalog);
+ struct icclient_catalog_callback *catalog_callback = malloc(sizeof(struct icclient_catalog_callback));
+ catalog_callback->catalog = catalog;
+ catalog_callback->callback = callback;
+ request(handler ? handler : icclient_catalog_results, (void *)catalog_callback, 0, "%s", nonspaced);
}
void icclient_flypage(const char *sku, icclient_handler handler, struct icclient_product **productptr)
diff --git a/configure.ac b/configure.ac
index 31ff210..1758af6 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([DEPS], [libcurl json-c])
+PKG_CHECK_MODULES([DEPS], [json-c])
AC_CHECK_HEADER_STDBOOL
AC_C_INLINE
AC_CHECK_FUNCS([memset])
diff --git a/icclient/catalog.h b/icclient/catalog.h
index 677d691..9d15a1a 100644
--- a/icclient/catalog.h
+++ b/icclient/catalog.h
@@ -6,6 +6,11 @@ struct icclient_catalog {
struct icclient_product *products[];
};
+struct icclient_catalog_callback {
+ struct icclient_catalog **catalog;
+ void (*callback)(struct icclient_catalog *);
+};
+
#ifdef __cplusplus
extern "C" {
#endif
diff --git a/main.c b/main.c
index 3c0aa3f..03dd621 100644
--- a/main.c
+++ b/main.c
@@ -2,49 +2,55 @@
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
-#include <icclient/typedefs.h>
-#include <icclient/product.h>
-#include <icclient/catalog.h>
-#include <icclient/client.h>
+#include "icclient/typedefs.h"
+#include "icclient/product.h"
+#include "icclient/catalog.h"
+#include "icclient/client.h"
static void callback(struct icclient_catalog *catalog)
{
for (size_t i = 0; i < catalog->length; i++) {
struct icclient_product *product = catalog->products[i];
- printf("SKU: %s\n", product->sku);
+ printf("SKU: %s\n"
+ "Description: %s\n"
+ "Thumb: %s\n"
+ "Image: %s\n"
+ "Price: %f\n"
+ "Product Group: %s\n",
+ product->sku,
+ product->description,
+ product->thumb,
+ product->image,
+ product->price,
+ product->prod_group
+ );
}
}
int main(void)
{
+ /*
char *url_line = NULL, *name_line = NULL, *pass_line = NULL;
printf("URL: ");
ssize_t url_nread = getline(&url_line, &(size_t){0}, stdin);
- /*
printf("Name: ");
ssize_t name_nread = getline(&name_line, &(size_t){0}, stdin);
printf("Pass: ");
ssize_t pass_nread = getline(&pass_line, &(size_t){0}, stdin);
- */
- char *url = malloc(--url_nread + 1);
- /*
- , *name = malloc(--name_nread + 1)
- , *pass = malloc(--pass_nread + 1);
- */
+ char *url = malloc(--url_nread + 1), *name = malloc(--name_nread + 1), *pass = malloc(--pass_nread + 1);
strncpy(url, url_line, url_nread);
free(url_line);
- /*
strncpy(name, name_line, name_nread);
free(name_line);
strncpy(pass, pass_line, pass_nread);
free(pass_line);
*/
- icclient_init(url, NULL);
+ icclient_init("http://localhost/namatoko", NULL);
+ /*
free(url);
- /*
icclient_login(NULL, NULL, name, pass, NULL, NULL, NULL);
free(name);
free(pass);