From 03f47c60e7963f24b8fa073abe2ad94cd688fc26 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: Sat, 14 Sep 2019 10:42:33 +0800 Subject: Follow a convenient way for inlining request function https://en.wikipedia.org/wiki/Inline_function#C99 --- Makefile.am | 4 ++-- client.c | 5 ++++- common.c | 46 ---------------------------------------------- request.c | 5 +++++ rtclient/common.h | 13 ------------- rtclient/request.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ ticket.c | 2 +- user.c | 2 +- 8 files changed, 63 insertions(+), 64 deletions(-) delete mode 100644 common.c create mode 100644 request.c delete mode 100644 rtclient/common.h create mode 100644 rtclient/request.h diff --git a/Makefile.am b/Makefile.am index 731f72f..657de43 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,10 +1,10 @@ noinst_LIBRARIES = librtclient.a librtclient_a_SOURCES = \ - rtclient/common.h \ + rtclient/request.h \ rtclient/user.h \ rtclient/ticket.h \ rtclient/rtclient.h \ - common.c \ + request.c \ user.c \ ticket.c \ client.c diff --git a/client.c b/client.c index 6bb3d26..f0bb4c2 100644 --- a/client.c +++ b/client.c @@ -1,8 +1,11 @@ #include #include -#include "rtclient/common.h" +#include "rtclient/request.h" #include "rtclient/rtclient.h" +CURL *curl = NULL; +char *server_url = NULL; + bool rtclient_init(const char *url) { curl_global_init(CURL_GLOBAL_SSL); diff --git a/common.c b/common.c deleted file mode 100644 index 01b8a24..0000000 --- a/common.c +++ /dev/null @@ -1,46 +0,0 @@ -#ifdef DEBUG -#ifdef ANDROID -#include -#else -#include -#endif // ANDROID -#endif // DEBUG -#include -#include "rtclient/common.h" - -CURL *curl = NULL; -char *server_url = NULL; - -inline void request(const char *path, const char *suffix - , size_t (*writefunction)(void *, size_t, size_t, void *) - , void *writedata, struct curl_httppost *post) -{ - char url[strlen(server_url) + strlen(path) + strlen(suffix) + 1]; - sprintf(url, "%s%s%s", server_url, path, suffix); - curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunction); - if (writedata) - curl_easy_setopt(curl, CURLOPT_WRITEDATA, writedata); - else - curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout); - if (post) - curl_easy_setopt(curl, CURLOPT_HTTPPOST, post); - else - curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); - -#ifdef DEBUG - CURLcode res = -#endif // DEBUG - curl_easy_perform(curl); -#ifdef DEBUG - if (res != CURLE_OK) { - const char *error = curl_easy_strerror(res); -#ifdef ANDROID - __android_log_print(ANDROID_LOG_ERROR, "librtclient", "%s: %s" - , __func__, error); -#else - fprintf(stderr, "%s: %s\n", __func__, error); -#endif // ANDROID - } -#endif // DEBUG -} diff --git a/request.c b/request.c new file mode 100644 index 0000000..fe1b8bd --- /dev/null +++ b/request.c @@ -0,0 +1,5 @@ +#include "rtclient/request.h" + +extern inline void request(const char *, const char * + , size_t (*)(void *, size_t, size_t, void *) + , void *, struct curl_httppost *); diff --git a/rtclient/common.h b/rtclient/common.h deleted file mode 100644 index a63a10c..0000000 --- a/rtclient/common.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef RTCLIENT_COMMON_H -#define RTCLIENT_COMMON_H - -#include - -extern CURL *curl; -extern char *server_url; - -void request(const char *path, const char *suffix - , size_t (*writefunction)(void *, size_t, size_t, void *) - , void *writedata, struct curl_httppost *post); - -#endif // RTCLIENT_COMMON_H diff --git a/rtclient/request.h b/rtclient/request.h new file mode 100644 index 0000000..a523610 --- /dev/null +++ b/rtclient/request.h @@ -0,0 +1,50 @@ +#ifndef RTCLIENT_REQUEST_H +#define RTCLIENT_REQUEST_H + +#ifdef DEBUG +#ifdef ANDROID +#include +#else +#include +#endif // ANDROID +#endif // DEBUG +#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) +{ + char url[strlen(server_url) + strlen(path) + strlen(suffix) + 1]; + sprintf(url, "%s%s%s", server_url, path, suffix); + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunction); + if (writedata) + curl_easy_setopt(curl, CURLOPT_WRITEDATA, writedata); + else + curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout); + if (post) + curl_easy_setopt(curl, CURLOPT_HTTPPOST, post); + else + curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); + +#ifdef DEBUG + CURLcode res = +#endif // DEBUG + curl_easy_perform(curl); +#ifdef DEBUG + if (res != CURLE_OK) { + const char *error = curl_easy_strerror(res); +#ifdef ANDROID + __android_log_print(ANDROID_LOG_ERROR, "librtclient", "%s: %s" + , __func__, error); +#else + fprintf(stderr, "%s: %s\n", __func__, error); +#endif // ANDROID + } +#endif // DEBUG +} +#endif // RTCLIENT_REQUEST_H diff --git a/ticket.c b/ticket.c index 27bf538..80f122e 100644 --- a/ticket.c +++ b/ticket.c @@ -7,7 +7,7 @@ #endif // DEBUG #include #include -#include "rtclient/common.h" +#include "rtclient/request.h" #include "rtclient/ticket.h" typedef struct rt_ticketlist rt_ticketlist; diff --git a/user.c b/user.c index 15aafde..dcd8bb8 100644 --- a/user.c +++ b/user.c @@ -7,7 +7,7 @@ #endif // DEBUG #include #include -#include "rtclient/common.h" +#include "rtclient/request.h" #include "rtclient/user.h" typedef struct rt_user rt_user; -- cgit v1.2.3