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 /request.h | |
parent | 2ecd7fbbc8456bd5a3d4f6b188c626bc44c60eb2 (diff) |
Make the request function a variadic one
Diffstat (limited to 'request.h')
-rw-r--r-- | request.h | 66 |
1 files changed, 60 insertions, 6 deletions
@@ -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 |