diff options
author | Iru Cai <mytbk920423@gmail.com> | 2018-06-27 23:34:21 +0800 |
---|---|---|
committer | Iru Cai <mytbk920423@gmail.com> | 2018-06-28 00:16:51 +0800 |
commit | b0e376b6ba1891fcf749d22095c4f79a843a5d0f (patch) | |
tree | 5008f76cf09bbda7b979180997a8cb3e142f0899 /util.c | |
parent | e6d9edc048efb5b19d79bbe7871b2ff779d72311 (diff) | |
download | matrix-curl-b0e376b6ba1891fcf749d22095c4f79a843a5d0f.tar.xz |
util.c: string builder
and rename util.{c,h} to curl_util
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 55 |
1 files changed, 23 insertions, 32 deletions
@@ -1,36 +1,27 @@ -#include "util.h" +#include <string.h> +#include <stdlib.h> -static size_t get_json_callback(void *contents, size_t size, size_t nmemb, void *userp) -{ - size_t realsize = size * nmemb; - - json_tokener *tok = json_tokener_new(); - if (userp != NULL) - *((json_object **)userp) = json_tokener_parse_ex(tok, contents, realsize); - - json_tokener_free(tok); - - return realsize; -} +#define SLMAX 100 -CURLcode -_curl_get(CURL *curl, const char *url, json_object **resp) +char * alloc_build_string(const char *ss[]) { - curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, get_json_callback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)resp); - - return curl_easy_perform(curl); -} - -CURLcode -_curl_post(CURL *curl, const char *url, const char *postdata, json_object **resp) -{ - curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, get_json_callback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)resp); - - return curl_easy_perform(curl); + size_t total_len = 1; + size_t slens[SLMAX]; /* should be enough */ + int ns = 0; + for (int i = 0; ss[i] != NULL; i++) { + if (ns >= SLMAX) + return NULL; + size_t l = strlen(ss[i]); + slens[ns] = l; + ns++; + total_len += l; + } + char *res = malloc(total_len); + size_t cur = 0; + for (int i = 0; i < ns; i++) { + memcpy(res+cur, ss[i], slens[i]); + cur += slens[i]; + } + res[cur] = 0; + return res; } |