From ee87c46227c838961250bb8d50edca57ac154462 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: Sat, 12 Jun 2021 20:07:47 +0800 Subject: Further simplify icclient_results function --- catalog.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) (limited to 'catalog.c') 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); } -- cgit v1.2.3