From a7526494614abd16a8871b665704e2bda05440b4 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, 13 Sep 2019 11:49:39 +0800 Subject: Search callback draft --- rtclient.c | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/rtclient.c b/rtclient.c index 9dd788f..fb7aa30 100644 --- a/rtclient.c +++ b/rtclient.c @@ -151,6 +151,42 @@ user_callback(void *contents, size_t size, size_t nmemb, void *writedata) return realsize; } +static size_t +search_callback(void *contents, size_t size, size_t nmemb, void *writedata) +{ + size_t realsize = size * nmemb; + char response[realsize + 1]; + memcpy(response, contents, realsize); + response[realsize] = '\0'; + char lines[strlen(response) + 1]; + strcpy(lines, response); + char *line = strtok(response, "\n"); + if (strstr(line, "200 Ok")) { + unsigned short nlines = 0; + line = strtok(NULL, "\n"); + do { + nlines++; + } while ((line = strtok(NULL, "\n"))); + char **tickets = malloc(nlines * sizeof(char *)); + char *linesaveptr = NULL; + line = strtok_r(lines, "\n", &linesaveptr); + line = strtok_r(NULL, "\n", &linesaveptr); + char *tokensaveptr = NULL, *token = NULL; + for (unsigned short i = 0; i < nlines; i++) { + token = strtok_r(line, ":", &tokensaveptr); + token = strtok_r(NULL, ":", &tokensaveptr); + tickets[i] = malloc(strlen(token)); + strcpy(tickets[i], ++token); + printf("Ticket %d: %s\n", i, tickets[i]); + free(tickets[i]); + line = strtok_r(NULL, "\n", &linesaveptr); + } + free(tickets); + } + + return realsize; +} + inline static void request(const char *path, const char *suffix , size_t (*writefunction)(void *, size_t, size_t, void *) , void *writedata, struct curl_httppost *post) @@ -210,7 +246,8 @@ void rtclient_userget(rt_user **userptr, const char *name) void rtclient_search(const char *query) { - request("/REST/1.0/search/ticket?query=", query, NULL, NULL, NULL); + request("/REST/1.0/search/ticket?query=", query, search_callback, NULL + , NULL); } void rtclient_userfree(rt_user *user) -- cgit v1.2.3