diff options
author | ꦌ ꦫꦶ ꦏ꧀ꦦꦿ ꦧ ꦮ ꦑ ꦩ ꦭ꧀ <erik@darapsa.co.id> | 2019-09-20 08:17:57 +0800 |
---|---|---|
committer | ꦌ ꦫꦶ ꦏ꧀ꦦꦿ ꦧ ꦮ ꦑ ꦩ ꦭ꧀ <erik@darapsa.co.id> | 2019-09-20 08:17:57 +0800 |
commit | 58bbe66096bc40cede5ea78442f9f0702c73c6ed (patch) | |
tree | 6be77be7957de2ec7d965362e5d9aab1328155f0 | |
parent | 2ecd7fbbc8456bd5a3d4f6b188c626bc44c60eb2 (diff) |
Make the request function a variadic one
-rw-r--r-- | client.c | 2 | ||||
-rw-r--r-- | post.h | 2 | ||||
-rw-r--r-- | request.c | 5 | ||||
-rw-r--r-- | request.h | 66 | ||||
-rw-r--r-- | ticket.c | 4 | ||||
-rw-r--r-- | user.c | 3 |
6 files changed, 68 insertions, 14 deletions
@@ -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; } @@ -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; } @@ -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, ...); @@ -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 @@ -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) @@ -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) |