-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.c
More file actions
42 lines (32 loc) · 881 Bytes
/
json.c
File metadata and controls
42 lines (32 loc) · 881 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <curl/curl.h>
#include "json.h"
static size_t write_json_struct(void *, size_t, size_t, parsed_json *);
parsed_json
get_parsed_json(const char *url)
{
parsed_json s;
s.len = 0;
s.ptr = (char *)malloc(s.len + 1);
s.ptr[0] = '\0';
CURL *handle = curl_easy_init();
curl_easy_setopt(handle, CURLOPT_URL, url);
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_json_struct);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &s);
curl_easy_perform(handle);
curl_easy_cleanup(handle);
return s;
}
size_t
write_json_struct(void *ptr, size_t size, size_t nmemb, parsed_json *s)
{
size_t new_len = s->len + size * nmemb;
s->ptr = (char *)realloc(s->ptr, new_len + 1);
memcpy(s->ptr + s->len, ptr, size * nmemb);
s->ptr[new_len] = '\0';
s->len = new_len;
return size * nmemb;
}