summaryrefslogtreecommitdiff
path: root/catalog.c
diff options
context:
space:
mode:
authorꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2021-06-12 20:07:47 +0800
committerꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2021-06-12 20:07:47 +0800
commitee87c46227c838961250bb8d50edca57ac154462 (patch)
tree99a522cb793b29e2e9e2253b2a7fae843ff61a2e /catalog.c
parentb94e7d2e3e33490d5b94da3c5406d4fe2e143edd (diff)
Further simplify icclient_results function
Diffstat (limited to 'catalog.c')
-rw-r--r--catalog.c21
1 files changed, 4 insertions, 17 deletions
diff --git a/catalog.c b/catalog.c
index 75e40dc..5938706 100644
--- a/catalog.c
+++ b/catalog.c
@@ -15,14 +15,6 @@
#define ICCLIENT_HANDLER_RETURN realsize
#endif
-
-static json_tokener *tokener = NULL;
-
-void icclient_catalog_init()
-{
- tokener = json_tokener_new();
-}
-
#ifdef __EMSCRIPTEN__
void icclient_catalog_results(emscripten_fetch_t *fetch)
#else
@@ -30,6 +22,7 @@ size_t icclient_catalog_results(void *data, size_t size, size_t nmemb, void *use
#endif
{
size_t realsize = ICCLIENT_HANDLER_REALSIZE;
+ json_tokener *tokener = json_tokener_new();
json_object *products = json_tokener_parse_ex(tokener, ICCLIENT_HANDLER_STRUCT data, realsize);
enum json_tokener_error error = json_tokener_get_error(tokener);
if (!products) {
@@ -41,12 +34,9 @@ size_t icclient_catalog_results(void *data, size_t size, size_t nmemb, void *use
}
} else if (!json_object_is_type(products, json_type_array) || error != json_tokener_success)
return ICCLIENT_HANDLER_RETURN;
+ json_tokener_free(tokener);
size_t length = json_object_array_length(products);
- struct icclient_catalog_callback *catalog_callback
- = (struct icclient_catalog_callback *)ICCLIENT_HANDLER_STRUCT userData;
- struct icclient_catalog **catalogptr = catalog_callback->catalog;
- *catalogptr = malloc(sizeof(struct icclient_catalog) + sizeof(struct icclient_product *[length]));
- struct icclient_catalog *catalog = *catalogptr;
+ struct icclient_catalog *catalog = malloc(sizeof(struct icclient_catalog) + sizeof(struct icclient_product *[length]));
catalog->length = length;
for (size_t i = 0; i < length; i++) {
catalog->products[i] = malloc(sizeof(struct icclient_product));
@@ -80,8 +70,7 @@ size_t icclient_catalog_results(void *data, size_t size, size_t nmemb, void *use
json_object_iter_next(&iterator);
}
}
- catalog_callback->callback(catalog);
- free(catalog_callback);
+ ((void (*)(struct icclient_catalog *))userData)(catalog);
#ifdef __EMSCRIPTEN__
emscripten_fetch_close(fetch);
#endif
@@ -93,6 +82,4 @@ void icclient_catalog_free(struct icclient_catalog *catalog)
for (size_t i = 0; i < catalog->length; i++)
icclient_product_free(catalog->products[i]);
free(catalog);
- catalog = NULL;
- json_tokener_free(tokener);
}