summaryrefslogtreecommitdiff
path: root/catalog.c
blob: 92dce735a3c1fe0a1d289d34cb7a6e48622bd803 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
#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_RETURN 
#define ICCLIENT_HANDLER_STRUCT fetch->
#else
#define ICCLIENT_HANDLER_REALSIZE size * nmemb
#define ICCLIENT_HANDLER_RETURN realsize
#define ICCLIENT_HANDLER_STRUCT
#endif

#ifdef __EMSCRIPTEN__
void icclient_catalog_results(emscripten_fetch_t *fetch)
#else
size_t icclient_catalog_results(void *data, size_t size, size_t nmemb, void *userData)
#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) {
		if (error == json_tokener_continue)
			return ICCLIENT_HANDLER_RETURN;
		else {
			json_tokener_reset(tokener);
			return ICCLIENT_HANDLER_RETURN;
		}
	} 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 *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));
		struct icclient_product *product = catalog->products[i];
		memset(product, '\0', sizeof(struct icclient_product));
		json_object *object = json_object_array_get_idx(products, i);
		struct json_object_iterator iterator = json_object_iter_begin(object);
		struct json_object_iterator iterator_end = json_object_iter_end(object);
		while (!json_object_iter_equal(&iterator, &iterator_end)) {
			const char *key = json_object_iter_peek_name(&iterator);
			json_object *val = json_object_iter_peek_value(&iterator);
			if (!strcmp(key, "price"))
				product->price = json_object_get_double(val);
			else {
				int len = json_object_get_string_len(val);
				if (len) {
					char *value = malloc(len + 1);
					strcpy(value, json_object_get_string(val));
					if (!strcmp(key, "sku"))
						product->sku = value;
					else if (!strcmp(key, "thumb"))
						product->thumb = value;
					else if (!strcmp(key, "image"))
						product->image = value;
					else if (!strcmp(key, "description"))
						product->description = value;
					else if (!strcmp(key, "prod_group"))
						product->prod_group = value;
				}
			}
			json_object_iter_next(&iterator);
		}
	}
	((void (*)(struct icclient_catalog *))ICCLIENT_HANDLER_STRUCT userData)(catalog);
#ifdef __EMSCRIPTEN__
	emscripten_fetch_close(fetch);
#endif
	return ICCLIENT_HANDLER_RETURN;
}

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);
}