From b94e7d2e3e33490d5b94da3c5406d4fe2e143edd 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?=
 <erik@darapsa.co.id>
Date: Sat, 12 Jun 2021 15:02:38 +0800
Subject: Some defines to make code clearer

---
 catalog.c | 61 +++++++++++++++++++------------------------------------------
 1 file 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)
-- 
cgit v1.2.3