diff options
-rw-r--r-- | rtclient.c | 33 | ||||
-rw-r--r-- | rtclient.h | 3 | ||||
-rw-r--r-- | rtticket.h | 9 | ||||
-rw-r--r-- | test.c | 16 |
4 files changed, 47 insertions, 14 deletions
@@ -11,6 +11,7 @@ #include "rtclient.h" typedef struct rt_user rt_user; +typedef struct rt_ticketlist rt_ticketlist; static CURL *curl = NULL; static char *server_url = NULL; @@ -39,8 +40,10 @@ user_callback(void *contents, size_t size, size_t nmemb, void *writedata) char response[realsize + 1]; memcpy(response, contents, realsize); response[realsize] = '\0'; + rt_user **userptr = (rt_user **)writedata; rt_user *user = *userptr; + char *linesaveptr = NULL; char *line = strtok_r(response, "\n", &linesaveptr); if (strstr(line, "200 Ok")) { @@ -160,28 +163,33 @@ search_callback(void *contents, size_t size, size_t nmemb, void *writedata) response[realsize] = '\0'; char lines[strlen(response) + 1]; strcpy(lines, response); + rt_ticketlist **listptr = (rt_ticketlist **)writedata; + char *line = strtok(response, "\n"); if (strstr(line, "200 Ok")) { - unsigned short nlines = 0; + (*listptr)->length = 0; line = strtok(NULL, "\n"); do { - nlines++; + (*listptr)->length++; } while ((line = strtok(NULL, "\n"))); - char **tickets = malloc(nlines * sizeof(char *)); + rt_ticketlist *ptr = realloc(*listptr, sizeof(*listptr) + + (*listptr)->length * sizeof(char *)); + *listptr = ptr; + rt_ticketlist *list = *listptr; 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++) { + for (unsigned int i = 0; i < list->length; 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]); + list->tickets[i] = malloc(strlen(token)); + strcpy(list->tickets[i], ++token); line = strtok_r(NULL, "\n", &linesaveptr); } - free(tickets); + } else { + free(*listptr); + *listptr = NULL; } return realsize; @@ -244,10 +252,11 @@ void rtclient_userget(rt_user **userptr, const char *name) request("/REST/1.0/user/", name, user_callback, (void *)userptr, NULL); } -void rtclient_search(const char *query) +void rtclient_search(rt_ticketlist **listptr, const char *query) { - request("/REST/1.0/search/ticket?query=", query, search_callback, NULL - , NULL); + *listptr = malloc(sizeof(rt_ticketlist)); + request("/REST/1.0/search/ticket?query=", query, search_callback + , (void *)listptr, NULL); } void rtclient_userfree(rt_user *user) @@ -2,6 +2,7 @@ #define RTCLIENT_H #include "rtuser.h" +#include "rtticket.h" #ifdef __cplusplus extern "C" { @@ -10,7 +11,7 @@ extern "C" { bool rtclient_init(const char *server_url); void rtclient_login(const char *name, const char *password); void rtclient_userget(struct rt_user **userptr, const char *name); - void rtclient_search(const char *query); + void rtclient_search(struct rt_ticketlist **listptr, const char *query); void rtclient_userfree(struct rt_user *user); void rtclient_cleanup(); diff --git a/rtticket.h b/rtticket.h new file mode 100644 index 0000000..3d78ac1 --- /dev/null +++ b/rtticket.h @@ -0,0 +1,9 @@ +#ifndef RTTICKET_H +#define RTTICKET_H + +struct rt_ticketlist { + unsigned int length; + char *tickets[]; +}; + +#endif // RTTICKET_H @@ -30,7 +30,6 @@ int main(void) struct rt_user *user = NULL; rtclient_userget(&user, name); - free(name); if (user) { printf("id: %s\npassword: %s\nname: %s\nemailaddress: %s\nrealname: %s\nnickname: %s\ngecos: %s\norganization: %s\naddress1: %s\naddress2: %s\ncity: %s\nstate: %s\nzip: %s\ncountry: %s\nhomephone: %s\nworkphone: %s\nmobilephone: %s\npagerphone: %s\ncontactinfo: %s\ncomments: %s\nsignature: %s\nlang: %s\nprivileged: %d\ndisabled: %d\n" @@ -43,7 +42,22 @@ int main(void) , user->signature, user->lang, user->privileged , user->disabled); rtclient_userfree(user); + + struct rt_ticketlist *list = NULL; + static const char *prefix = "Owner='"; + char query[strlen(prefix) + strlen(name) + 2]; + sprintf(query, "%s%s'", prefix, name); + rtclient_search(&list, query); + if (list) { + printf("List length = %d\n", list->length); + for (unsigned short i = 0; i < list->length; i++) { + char *ticket = list->tickets[i]; + printf("Ticket %d: %s\n", i, ticket); + } + free(list); + } } + free(name); rtclient_cleanup(); |