summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--catalog.c61
1 files changed, 19 insertions, 42 deletions
diff --git a/catalog.c b/catalog.c
index 91fe952..75e40dc 100644
--- a/catalog.c
+++ b/catalog.c
@@ -2,11 +2,19 @@
#include <stdlib.h>
#include <string.h>
#include <json.h>
+#include "icclient/product.h"
+#include "icclient/catalog.h"
#ifdef __EMSCRIPTEN__
#include <emscripten/fetch.h>
+#define ICCLIENT_HANDLER_REALSIZE fetch->numBytes
+#define ICCLIENT_HANDLER_STRUCT fetch->
+#define ICCLIENT_HANDLER_RETURN
+#else
+#define ICCLIENT_HANDLER_REALSIZE size * nmemb
+#define ICCLIENT_HANDLER_STRUCT
+#define ICCLIENT_HANDLER_RETURN realsize
#endif
-#include "icclient/product.h"
-#include "icclient/catalog.h"
+
static json_tokener *tokener = NULL;
@@ -18,54 +26,24 @@ void icclient_catalog_init()
#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)
+size_t icclient_catalog_results(void *data, size_t size, size_t nmemb, void *userData)
#endif
{
- size_t realsize =
-#ifdef __EMSCRIPTEN__
- fetch->numBytes
-#else
- size * nmemb
-#endif
- ;
-#ifndef __EMSCRIPTEN__
- char data[realsize + 1];
- memcpy(data, contents, realsize);
- data[realsize] = '\0';
-#endif
- json_object *products = json_tokener_parse_ex(tokener,
-#ifdef __EMSCRIPTEN__
- fetch->
-#endif
- data, realsize);
+ size_t realsize = ICCLIENT_HANDLER_REALSIZE;
+ 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) {
if (error == json_tokener_continue)
- return
-#ifndef __EMSCRIPTEN__
- realsize
-#endif
- ;
+ return ICCLIENT_HANDLER_RETURN;
else {
json_tokener_reset(tokener);
- return
-#ifndef __EMSCRIPTEN__
- realsize
-#endif
- ;
+ return ICCLIENT_HANDLER_RETURN;
}
} else if (!json_object_is_type(products, json_type_array) || error != json_tokener_success)
- return
-#ifndef __EMSCRIPTEN__
- realsize
-#endif
- ;
+ return ICCLIENT_HANDLER_RETURN;
size_t length = json_object_array_length(products);
- struct icclient_catalog_callback *catalog_callback = (struct icclient_catalog_callback *)
-#ifdef __EMSCRIPTEN__
- fetch->
-#endif
- userData;
+ 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;
@@ -106,9 +84,8 @@ size_t icclient_catalog_results(void *contents, size_t size, size_t nmemb, void
free(catalog_callback);
#ifdef __EMSCRIPTEN__
emscripten_fetch_close(fetch);
-#else
- return realsize;
#endif
+ return ICCLIENT_HANDLER_RETURN;
}
void icclient_catalog_free(struct icclient_catalog *catalog)