summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIru Cai <mytbk920423@gmail.com>2018-06-27 23:34:21 +0800
committerIru Cai <mytbk920423@gmail.com>2018-06-28 00:16:51 +0800
commitb0e376b6ba1891fcf749d22095c4f79a843a5d0f (patch)
tree5008f76cf09bbda7b979180997a8cb3e142f0899
parente6d9edc048efb5b19d79bbe7871b2ff779d72311 (diff)
downloadmatrix-curl-b0e376b6ba1891fcf749d22095c4f79a843a5d0f.tar.xz
util.c: string builder
and rename util.{c,h} to curl_util
-rw-r--r--Makefile2
-rw-r--r--api.c14
-rw-r--r--curl_util.c36
-rw-r--r--curl_util.h39
-rw-r--r--session.c2
-rw-r--r--t/t_build_string.c19
-rw-r--r--util.c55
-rw-r--r--util.h45
8 files changed, 141 insertions, 71 deletions
diff --git a/Makefile b/Makefile
index d4b6daa..3cb03b9 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
LDLIBS=-lcurl -ljson-c
CFLAGS=-g -Wall
-api: api.o session.o util.o
+api: api.o session.o curl_util.o util.o
diff --git a/api.c b/api.c
index e5b802f..0ae34e5 100644
--- a/api.c
+++ b/api.c
@@ -4,14 +4,20 @@
#include <curl/curl.h>
#include <json-c/json.h>
#include "session.h"
+#include "curl_util.h"
#include "util.h"
/* logout: return 1 if success, 0 if failed */
int matrix_logout(matrix_session *sess)
{
- char requrl[1000]; /* FIXME */
- sprintf(requrl, "%s/_matrix/client/r0/logout?access_token=%s", sess->url, sess->token);
+ char * requrl = alloc_build_string((const char *[]){
+ sess->url,
+ "/_matrix/client/r0/logout?access_token=",
+ sess->token,
+ NULL
+ });
CURLcode res = _curl_post(sess->curl, requrl, "", NULL);
+ free(requrl);
if (res == CURLE_OK)
return 1;
else
@@ -110,7 +116,7 @@ int main()
if (roomid == NULL)
puts("fail to resolv #hello:my.domain.name");
- free(roomid);
+ if (roomid) free(roomid);
/* create a room */
roomid = matrix_create_room(sess);
@@ -119,7 +125,7 @@ int main()
else
printf("create room %s\n", roomid);
- free(roomid);
+ if (roomid) free(roomid);
if (matrix_logout(sess))
puts("logout!");
diff --git a/curl_util.c b/curl_util.c
new file mode 100644
index 0000000..5d1b21a
--- /dev/null
+++ b/curl_util.c
@@ -0,0 +1,36 @@
+#include "curl_util.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;
+}
+
+CURLcode
+_curl_get(CURL *curl, const char *url, json_object **resp)
+{
+ 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);
+}
diff --git a/curl_util.h b/curl_util.h
new file mode 100644
index 0000000..509bcb9
--- /dev/null
+++ b/curl_util.h
@@ -0,0 +1,39 @@
+#ifndef MATRIX_CURL_UTIL_H
+#define MATRIX_CURL_UTIL_H
+
+#include <curl/curl.h>
+#include <json-c/json.h>
+#include <string.h>
+
+CURLcode _curl_get(CURL *curl, const char *url, json_object **resp);
+CURLcode _curl_post(CURL *curl, const char *url, const char *postdata,
+ json_object **resp);
+
+static inline int
+json_add_string(json_object *j, const char *k, const char *v)
+{
+ return json_object_object_add(j, k, json_object_new_string(v));
+}
+
+static inline
+const char *json_gets(json_object *j, const char *key)
+{
+ json_object *val;
+
+ if (json_object_object_get_ex(j, key, &val)) {
+ if (json_object_is_type(val, json_type_string))
+ return json_object_get_string(val);
+ }
+ return NULL;
+}
+
+static inline
+char* copy_str(const char *s)
+{
+ size_t l = strlen(s)+1;
+ char *ss = (char*)malloc(l);
+ memcpy(ss, s, l);
+ return ss;
+}
+
+#endif
diff --git a/session.c b/session.c
index bd9deb0..1d40b88 100644
--- a/session.c
+++ b/session.c
@@ -1,5 +1,5 @@
#include "session.h"
-#include "util.h"
+#include "curl_util.h"
matrix_session*
matrix_session_new(const char *url)
diff --git a/t/t_build_string.c b/t/t_build_string.c
new file mode 100644
index 0000000..f49cc2b
--- /dev/null
+++ b/t/t_build_string.c
@@ -0,0 +1,19 @@
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+char * alloc_build_string(const char *ss[]);
+
+int main()
+{
+ const char *greets[] = {"hello", " world", " all", NULL};
+ char *greet = alloc_build_string(greets);
+ char *greet2 = alloc_build_string((const char *[]){
+ "hello world", " all", NULL
+ });
+ printf("%d\n", strcmp(greet, "hello world all"));
+ printf("%d\n", strcmp(greet2, "hello world all"));
+ free(greet);
+ free(greet2);
+}
+
diff --git a/util.c b/util.c
index 3e67b9d..6cd56ac 100644
--- a/util.c
+++ b/util.c
@@ -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;
}
diff --git a/util.h b/util.h
index 509bcb9..ab0b158 100644
--- a/util.h
+++ b/util.h
@@ -1,39 +1,18 @@
-#ifndef MATRIX_CURL_UTIL_H
-#define MATRIX_CURL_UTIL_H
+#ifndef LIBMC_UTIL_H
+#define LIBMC_UTIL_H
-#include <curl/curl.h>
-#include <json-c/json.h>
-#include <string.h>
+#ifdef __cplusplus
+extern "C" {
+#endif /* } */
-CURLcode _curl_get(CURL *curl, const char *url, json_object **resp);
-CURLcode _curl_post(CURL *curl, const char *url, const char *postdata,
- json_object **resp);
+/* build a new C string by concatenate strings in ss
+ * @ss: C strings, the last element is NULL
+ * @note callers should call free() to free the returned C string
+ */
+char * alloc_build_string(const char *ss[]);
-static inline int
-json_add_string(json_object *j, const char *k, const char *v)
-{
- return json_object_object_add(j, k, json_object_new_string(v));
-}
-
-static inline
-const char *json_gets(json_object *j, const char *key)
-{
- json_object *val;
-
- if (json_object_object_get_ex(j, key, &val)) {
- if (json_object_is_type(val, json_type_string))
- return json_object_get_string(val);
- }
- return NULL;
-}
-
-static inline
-char* copy_str(const char *s)
-{
- size_t l = strlen(s)+1;
- char *ss = (char*)malloc(l);
- memcpy(ss, s, l);
- return ss;
+#ifdef __cplusplus
}
+#endif
#endif