diff options
| -rw-r--r-- | catalog.c | 21 | ||||
| -rw-r--r-- | client.c | 9 | ||||
| -rw-r--r-- | icclient/catalog.h | 5 | ||||
| -rw-r--r-- | icclient/client.h | 56 | ||||
| -rw-r--r-- | main.c | 5 | 
5 files changed, 35 insertions, 61 deletions
@@ -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);  } @@ -13,22 +13,17 @@ extern size_t icclient_catalog_results(void *, size_t, size_t, void *);  bool icclient_init(const char *url, const char *certificate)  { -	icclient_catalog_init();  	return icclient_request_init(url, certificate);  } -void icclient_results(const char *prod_group, -		void (*callback)(struct icclient_catalog *), struct icclient_catalog **catalog, icclient_handler handler) +void icclient_results(const char *prod_group, void (*callback)(struct icclient_catalog *), icclient_handler handler)  {  	char nonspaced[strlen(prod_group) + 1];  	strcpy(nonspaced, prod_group);  	char *space = NULL;  	while ((space = strchr(nonspaced, ' ')))  		*space = '-'; -	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); +	request(handler ? handler : icclient_catalog_results, (void *)callback, 0, "%s", nonspaced);  }  void icclient_flypage(const char *sku, icclient_handler handler, struct icclient_product **productptr) diff --git a/icclient/catalog.h b/icclient/catalog.h index 9d15a1a..677d691 100644 --- a/icclient/catalog.h +++ b/icclient/catalog.h @@ -6,11 +6,6 @@ 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/icclient/client.h b/icclient/client.h index 5e0f01b..ae2a766 100644 --- a/icclient/client.h +++ b/icclient/client.h @@ -1,7 +1,7 @@  #ifndef ICCLIENT_CLIENT_H  #define ICCLIENT_CLIENT_H -#define	icclient_allproducts(callback, catalog, handler) icclient_results("All-Products", callback, catalog, handler) +#define	icclient_allproducts(callback, handler) icclient_results("All-Products", callback, handler)  struct icclient_product;  struct icclient_catalog; @@ -10,34 +10,32 @@ struct icclient_catalog;  extern "C" {  #endif -	/*! -	 * \brief A function that needs to be run first. -	 * \param url Server root URL. -	 * \param certificate Path to the CA certificate file. -	 * \return True if the initialisation works, false otherwise. -	 */ -	bool icclient_init(const char *url, const char *certificate); - -	/*! -	 * \brief For fetching data about products that belong a specific group. -	 * \param prod_group The name of the product group. -	 * \param callback A pointer to the function that needs to be called after the catalog is ready. -	 * \param catalog A pointer to pointer to the catalog to store the data. -	 * \param handler A pointer to the function when a custom handler is needed to arrange the data into the catalog. -	 */ -	void icclient_results(const char *prod_group, void (*callback)(struct icclient_catalog *), -			struct icclient_catalog **catalog, icclient_handler handler); - -	/*! -	 * \brief For fetching data about a specific product. -	 * \param sku The SKU of the product. -	 * \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, icclient_handler handler, struct icclient_product **productptr); - -	void icclient_page(const char *path, icclient_handler handler, void **dataptr); -	void icclient_cleanup(); +/*! + * \brief A function that needs to be run first. + * \param url Server root URL. + * \param certificate Path to the CA certificate file. + * \return True if the initialisation works, false otherwise. + */ +bool icclient_init(const char *url, const char *certificate); + +/*! + * \brief For fetching data about products that belong a specific group. + * \param prod_group The name of the product group. + * \param callback A pointer to the function that needs to be called after the catalog is ready. + * \param handler A pointer to the function when a custom handler is needed to arrange the data into the catalog. + */ +void icclient_results(const char *prod_group, void (*callback)(struct icclient_catalog *), icclient_handler handler); + +/*! + * \brief For fetching data about a specific product. + * \param sku The SKU of the product. + * \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, icclient_handler handler, struct icclient_product **productptr); + +void icclient_page(const char *path, icclient_handler handler, void **dataptr); +void icclient_cleanup();  #ifdef __cplusplus  } @@ -26,6 +26,7 @@ static void print_catalog(struct icclient_catalog *catalog)  				product->prod_group  		      );  	} +	icclient_catalog_free(catalog);  }  static size_t print_user(void *contents, size_t size, size_t nmemb, void *userData) @@ -51,9 +52,7 @@ int main(int argc, char *argv[])  	icclient_init(url, NULL);  	free(url); -	struct icclient_catalog *catalog; -	icclient_allproducts(print_catalog, &catalog, NULL); -	icclient_catalog_free(catalog); +	icclient_allproducts(print_catalog, NULL);  	char *name_line = NULL;  	printf("\nName: ");  |