summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2021-06-13 15:41:54 +0800
committerꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2021-06-13 15:41:54 +0800
commit143f5b64f187497af728a6d921c599dcc99ed807 (patch)
treeba7264b8748c53c57697f9a1c9bf34db279ed468 /main.c
parent4cd6fdb93eca409a76b0af860d9a0599b2f95f53 (diff)
Fix the way libcurl has been used
Diffstat (limited to 'main.c')
-rw-r--r--main.c58
1 files changed, 49 insertions, 9 deletions
diff --git a/main.c b/main.c
index fb2e13b..45ec093 100644
--- a/main.c
+++ b/main.c
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <stdbool.h>
+#include <json.h>
#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: ");