From 143f5b64f187497af728a6d921c599dcc99ed807 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: Sun, 13 Jun 2021 15:41:54 +0800 Subject: Fix the way libcurl has been used --- main.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 9 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index fb2e13b..45ec093 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include "icclient.h" #include "icclient/member.h" @@ -22,17 +24,55 @@ static void print_catalog(struct icclient_catalog *catalog) product->prod_group ); } - icclient_catalog_free(catalog); + icclient_free_catalog(catalog); } -static size_t print_user(void *contents, size_t size, size_t nmemb, void *userData) +static void handle_results(icclient_fetch_t *fetch) { - size_t realsize = size * nmemb; - char data[realsize + 1]; - memcpy(data, contents, realsize); - data[realsize] = '\0'; - printf("%s\n", data); - return realsize; + json_tokener *tokener = json_tokener_new(); + json_object *products = json_tokener_parse_ex(tokener, fetch->data, fetch->numBytes); + 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 *))fetch->userData)(catalog); +} + +static void print_user(icclient_fetch_t *fetch) +{ + printf("%s\n", fetch->data); } int main(int argc, char *argv[]) @@ -48,7 +88,7 @@ int main(int argc, char *argv[]) icclient_init(url, NULL); free(url); - icclient_allproducts(print_catalog, NULL); + icclient_allproducts(print_catalog, handle_results); char *name_line = NULL; printf("\nName: "); -- cgit v1.2.3