summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorꦌ ꦫꦶ ꦏ꧀ꦦꦿ ꦧ ꦮ ꦑ ꦩ ꦭ꧀ <erik@darapsa.co.id>2019-09-20 08:17:57 +0800
committerꦌ ꦫꦶ ꦏ꧀ꦦꦿ ꦧ ꦮ ꦑ ꦩ ꦭ꧀ <erik@darapsa.co.id>2019-09-20 08:17:57 +0800
commit58bbe66096bc40cede5ea78442f9f0702c73c6ed (patch)
tree6be77be7957de2ec7d965362e5d9aab1328155f0
parent2ecd7fbbc8456bd5a3d4f6b188c626bc44c60eb2 (diff)
Make the request function a variadic one
-rw-r--r--client.c2
-rw-r--r--post.h2
-rw-r--r--request.c5
-rw-r--r--request.h66
-rw-r--r--ticket.c4
-rw-r--r--user.c3
6 files changed, 68 insertions, 14 deletions
diff --git a/client.c b/client.c
index ebcc188..2972167 100644
--- a/client.c
+++ b/client.c
@@ -35,7 +35,7 @@ void rtclient_login(const char *name, const char *password)
, CURLFORM_PTRCONTENTS, password
, CURLFORM_END);
last = NULL;
- request("", "", NULL, NULL, post);
+ request(NULL, NULL, post, "");
curl_formfree(post);
post = NULL;
}
diff --git a/post.h b/post.h
index 05af269..8420d73 100644
--- a/post.h
+++ b/post.h
@@ -35,7 +35,7 @@ inline void post(const char *path, const char *pairs[], size_t n)
, CURLFORM_PTRCONTENTS, content
, CURLFORM_END);
last = NULL;
- request(path, "", NULL, NULL, post);
+ request(NULL, NULL, post, "%s", path);
curl_formfree(post);
post = NULL;
}
diff --git a/request.c b/request.c
index e491349..652ec71 100644
--- a/request.c
+++ b/request.c
@@ -1,5 +1,4 @@
#include "request.h"
-extern inline void request(const char *, const char *
- , size_t (*)(void *, size_t, size_t, void *)
- , void *, struct curl_httppost *);
+extern inline void request(size_t (*writefunction)(void *, size_t, size_t, void *)
+ , void *writedata, struct curl_httppost *post, char *fmt, ...);
diff --git a/request.h b/request.h
index e67686c..f6269a1 100644
--- a/request.h
+++ b/request.h
@@ -1,22 +1,75 @@
#ifndef RTCLIENT_REQUEST_H
#define RTCLIENT_REQUEST_H
-#if defined(DEBUG) && defined(ANDROID)
+#if defined(ANDROID) && defined(DEBUG)
#include <android/log.h>
#endif
#include <string.h>
+#include <stdarg.h>
#include <stdio.h>
#include <curl/curl.h>
extern CURL *curl;
extern char *server_url;
-inline void request(const char *path, const char *suffix
- , size_t (*writefunction)(void *, size_t, size_t, void *)
- , void *writedata, struct curl_httppost *post)
+inline void request(size_t (*writefunction)(void *, size_t, size_t, void *)
+ , void *writedata, struct curl_httppost *post, char *fmt, ...)
{
- char url[strlen(server_url) + strlen(path) + strlen(suffix) + 1];
- sprintf(url, "%s%s%s", server_url, path, suffix);
+ va_list ap;
+ char *p, *sval;
+ unsigned int ival;
+ size_t length = strlen(server_url) + strlen(fmt);
+
+ va_start(ap, fmt);
+ for (p = fmt; *p; p++) {
+ if (*p != '%')
+ continue;
+ switch(*++p) {
+ case 's':
+ sval = va_arg(ap, char *);
+ length += strlen(sval) - 2;
+ break;
+ case 'd':
+ ival = va_arg(ap, unsigned int);
+ do {
+ length++;
+ } while (ival / 10);
+ length -= 2;
+ break;
+ default:
+ break;
+ }
+ }
+ va_end(ap);
+
+ char url[length + 1];
+ strcpy(url, server_url);
+
+ va_start(ap, fmt);
+ for (p = fmt; *p; p++) {
+ if (*p != '%')
+ continue;
+ switch(*++p) {
+ case 's':
+ sval = va_arg(ap, char *);
+#ifdef DEBUG
+ printf("String: %s\n", sval);
+#endif
+ strcat(url, sval);
+ break;
+ case 'd':
+ ival = va_arg(ap, unsigned int);
+#ifdef DEBUG
+ printf("Integer: %d\n", ival);
+#endif
+ sprintf(url, "%s%d", url, ival);
+ break;
+ default:
+ break;
+ }
+ }
+ va_end(ap);
+
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunction);
if (writedata)
@@ -44,4 +97,5 @@ inline void request(const char *path, const char *suffix
}
#endif // DEBUG
}
+
#endif // RTCLIENT_REQUEST_H
diff --git a/ticket.c b/ticket.c
index 0493656..75a00e5 100644
--- a/ticket.c
+++ b/ticket.c
@@ -90,8 +90,8 @@ static size_t search_callback(void *contents, size_t size, size_t nmemb
void rtclient_ticket_search(rtclient_ticketlist **listptr, const char *query)
{
*listptr = malloc(sizeof(rtclient_ticketlist));
- request("/REST/1.0/search/ticket?query=", query, search_callback
- , (void *)listptr, NULL);
+ request(search_callback, (void *)listptr, NULL, "%s%s"
+ , "/REST/1.0/search/ticket?query=", query);
}
void rtclient_ticket_freelist(rtclient_ticketlist *list)
diff --git a/user.c b/user.c
index 7cad6ee..4614f6a 100644
--- a/user.c
+++ b/user.c
@@ -207,7 +207,8 @@ void rtclient_user_showname(rtclient_user **userptr, const char *name)
user->timezone = RTCLIENT_TIMEZONE_NONE;
user->privileged = false;
user->disabled = true;
- request("/REST/1.0/user/", name, show_callback, (void *)userptr, NULL);
+ request(show_callback, (void *)userptr, NULL, "%s%s", "/REST/1.0/user/"
+ , name);
}
void rtclient_user_free(rtclient_user *user)