From 58bbe66096bc40cede5ea78442f9f0702c73c6ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=20=EA=A6=AB=EA=A6=B6=20=EA=A6=8F=EA=A7=80?= =?UTF-8?q?=EA=A6=A6=EA=A6=BF=20=EA=A6=A7=20=EA=A6=AE=20=EA=A6=91=20?= =?UTF-8?q?=EA=A6=A9=20=EA=A6=AD=EA=A7=80?= Date: Fri, 20 Sep 2019 08:17:57 +0800 Subject: Make the request function a variadic one --- client.c | 2 +- post.h | 2 +- request.c | 5 ++--- request.h | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ ticket.c | 4 ++-- user.c | 3 ++- 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 #endif #include +#include #include #include 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) -- cgit v1.2.3