diff options
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | main.c | 7 | ||||
-rw-r--r-- | rtclient/search.h | 21 | ||||
-rw-r--r-- | rtclient/ticket.h | 8 | ||||
-rw-r--r-- | search.c | 87 | ||||
-rw-r--r-- | ticket.c | 78 |
6 files changed, 114 insertions, 89 deletions
diff --git a/Makefile.am b/Makefile.am index e12c540..7b42214 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,6 +2,7 @@ noinst_LIBRARIES = librtclient.a librtclient_a_SOURCES = \ rtclient/user.h \ rtclient/ticket.h \ + rtclient/search.h \ rtclient/client.h \ request.h \ request.c \ @@ -9,6 +10,7 @@ librtclient_a_SOURCES = \ post.c \ user.c \ ticket.c \ + search.c \ client.c librtclient_a_CFLAGS = -fPIC @@ -5,6 +5,7 @@ #include <rtclient/client.h> #include <rtclient/user.h> #include <rtclient/ticket.h> +#include <rtclient/search.h> int main(void) { @@ -63,17 +64,17 @@ int main(void) , user->disabled); rtclient_user_free(user); - struct rtclient_ticket_list *list = NULL; + struct rtclient_search_ticket_list *list = NULL; static const char *prefix = "Owner='"; char query[strlen(prefix) + strlen(name) + 2]; sprintf(query, "%s%s'", prefix, name); - rtclient_ticket_search(&list, query); + rtclient_search_ticket(&list, query); if (list) { for (size_t i = 0; i < list->length; i++) { struct rtclient_ticket *ticket = list->tickets[i]; printf("%u: %s\n", ticket->id, ticket->subject); } - rtclient_ticket_freelist(list); + rtclient_search_ticket_free(list); } } free(name); diff --git a/rtclient/search.h b/rtclient/search.h new file mode 100644 index 0000000..e0e386a --- /dev/null +++ b/rtclient/search.h @@ -0,0 +1,21 @@ +#ifndef RTCLIENT_SEARCH_H +#define RTCLIENT_SEARCH_H + +struct rtclient_search_ticket_list { + size_t length; + struct rtclient_ticket *tickets[]; +}; + +#ifdef __cplusplus +extern "C" { +#endif + + void rtclient_search_ticket(struct rtclient_search_ticket_list **listptr + , const char *query); + void rtclient_search_ticket_free(struct rtclient_search_ticket_list *list); + +#ifdef __cplusplus +} +#endif + +#endif // RTCLIENT_SEARCH_H diff --git a/rtclient/ticket.h b/rtclient/ticket.h index db2d031..5ff5c2f 100644 --- a/rtclient/ticket.h +++ b/rtclient/ticket.h @@ -6,11 +6,6 @@ struct rtclient_ticket { char *subject; }; -struct rtclient_ticket_list { - size_t length; - struct rtclient_ticket *tickets[]; -}; - enum rtclient_ticket_history_type { RTCLIENT_TICKET_HISTORY_TYPE_CREATE , RTCLIENT_TICKET_HISTORY_TYPE_EMAILRECORD @@ -60,10 +55,7 @@ extern "C" { , const char *starts , const char *due , const char *text); - void rtclient_ticket_search(struct rtclient_ticket_list **listptr - , const char *query); void rtclient_ticket_history(unsigned int id); - void rtclient_ticket_freelist(struct rtclient_ticket_list *list); #ifdef __cplusplus } diff --git a/search.c b/search.c new file mode 100644 index 0000000..5a73d51 --- /dev/null +++ b/search.c @@ -0,0 +1,87 @@ +#include <stdlib.h> +#include "request.h" +#include "rtclient/ticket.h" +#include "rtclient/search.h" + +typedef struct rtclient_ticket rtclient_ticket; +typedef struct rtclient_search_ticket_list rtclient_search_ticket_list; + +static size_t ticket_handler(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); + rtclient_search_ticket_list **listptr + = (rtclient_search_ticket_list **)writedata; + + char *line = strtok(response, "\n"); + if (strstr(line, "200 Ok")) { + line = strtok(NULL, "\n"); + do { + (*listptr)->length++; + if (!strcmp(line, "No matching results.")) { + free(*listptr); + *listptr = NULL; + return realsize; + } + } while ((line = strtok(NULL, "\n"))); + + rtclient_search_ticket_list *ptr = realloc(*listptr + , sizeof(*listptr) + + (*listptr)->length * sizeof(rtclient_ticket)); + *listptr = ptr; + rtclient_search_ticket_list *list = *listptr; + + char *linesaveptr = NULL; + line = strtok_r(lines, "\n", &linesaveptr); + line = strtok_r(NULL, "\n", &linesaveptr); + char *tokensaveptr = NULL, *token = NULL; + for (size_t i = 0; i < list->length; i++) { + list->tickets[i] = malloc(sizeof(rtclient_ticket)); + rtclient_ticket *ticket = list->tickets[i]; + token = strtok_r(line, ":", &tokensaveptr); + ticket->id = atoi(token); + token = strtok_r(NULL, ":", &tokensaveptr); + ticket->subject = malloc(strlen(token)); + strcpy(ticket->subject, ++token); + line = strtok_r(NULL, "\n", &linesaveptr); + } + } else { + free(*listptr); + *listptr = NULL; +#ifdef DEBUG +#ifdef ANDROID + __android_log_print(ANDROID_LOG_INFO, "librtclient" + , "%s response status:\n%s", __func__, line); +#else + printf("%s response status:\n%s\n", __func__, line); +#endif // ANDROID +#endif // DEBUG + } + + return realsize; +} + +void rtclient_search_ticket(rtclient_search_ticket_list **listptr + , const char *query) +{ + *listptr = malloc(sizeof(rtclient_search_ticket_list)); + (*listptr)->length = 0; + request(ticket_handler, (void *)listptr, NULL, "%s%s" + , "REST/1.0/search/ticket?query=", query); +} + +void rtclient_search_ticket_free(rtclient_search_ticket_list *list) +{ + for (size_t i = 0; i < list->length; i++) { + rtclient_ticket *ticket = list->tickets[i]; + free(ticket->subject); + free(ticket); + } + free(list); + list = NULL; +} @@ -3,7 +3,6 @@ #include "rtclient/ticket.h" typedef struct rtclient_ticket rtclient_ticket; -typedef struct rtclient_ticket_list rtclient_ticket_list; void rtclient_ticket_new(const char *queue , const char *requestor @@ -39,85 +38,8 @@ void rtclient_ticket_new(const char *queue }, 28); } -static size_t search_handler(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); - rtclient_ticket_list **listptr = (rtclient_ticket_list **)writedata; - - char *line = strtok(response, "\n"); - if (strstr(line, "200 Ok")) { - line = strtok(NULL, "\n"); - do { - (*listptr)->length++; - if (!strcmp(line, "No matching results.")) { - free(*listptr); - *listptr = NULL; - return realsize; - } - } while ((line = strtok(NULL, "\n"))); - - rtclient_ticket_list *ptr = realloc(*listptr, sizeof(*listptr) - + (*listptr)->length * sizeof(rtclient_ticket)); - *listptr = ptr; - rtclient_ticket_list *list = *listptr; - - char *linesaveptr = NULL; - line = strtok_r(lines, "\n", &linesaveptr); - line = strtok_r(NULL, "\n", &linesaveptr); - char *tokensaveptr = NULL, *token = NULL; - for (size_t i = 0; i < list->length; i++) { - list->tickets[i] = malloc(sizeof(rtclient_ticket)); - rtclient_ticket *ticket = list->tickets[i]; - token = strtok_r(line, ":", &tokensaveptr); - ticket->id = atoi(token); - token = strtok_r(NULL, ":", &tokensaveptr); - ticket->subject = malloc(strlen(token)); - strcpy(ticket->subject, ++token); - line = strtok_r(NULL, "\n", &linesaveptr); - } - } else { - free(*listptr); - *listptr = NULL; -#ifdef DEBUG -#ifdef ANDROID - __android_log_print(ANDROID_LOG_INFO, "librtclient" - , "%s response status:\n%s", __func__, line); -#else - printf("%s response status:\n%s\n", __func__, line); -#endif // ANDROID -#endif // DEBUG - } - - return realsize; -} - -void rtclient_ticket_search(rtclient_ticket_list **listptr, const char *query) -{ - *listptr = malloc(sizeof(rtclient_ticket_list)); - (*listptr)->length = 0; - request(search_handler, (void *)listptr, NULL, "%s%s" - , "REST/1.0/search/ticket?query=", query); -} - void rtclient_ticket_history(unsigned int id) { request(NULL, NULL, NULL, "%s%u%s", "REST/1.0/ticket/", id , "/history?format=l"); } - -void rtclient_ticket_freelist(rtclient_ticket_list *list) -{ - for (size_t i = 0; i < list->length; i++) { - rtclient_ticket *ticket = list->tickets[i]; - free(ticket->subject); - free(ticket); - } - free(list); - list = NULL; -} |