From e39fa22f6896b7b7200229ffba95c56074a17535 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: Wed, 25 Sep 2019 16:24:39 +0800 Subject: Ticket structure now containing ID member --- main.c | 4 ++-- rtclient/ticket.h | 7 ++++++- ticket.c | 15 +++++++++++---- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index e0129e4..e18939f 100644 --- a/main.c +++ b/main.c @@ -70,8 +70,8 @@ int main(void) rtclient_ticket_search(&list, query); if (list) { for (size_t i = 0; i < list->length; i++) { - char *ticket = list->tickets[i]; - printf("Ticket %zu: %s\n", i, ticket); + struct rtclient_ticket *ticket = list->tickets[i]; + printf("%d: %s\n", ticket->id, ticket->subject); } rtclient_ticket_freelist(list); } diff --git a/rtclient/ticket.h b/rtclient/ticket.h index 87e7496..5b6bdd6 100644 --- a/rtclient/ticket.h +++ b/rtclient/ticket.h @@ -1,9 +1,14 @@ #ifndef RTCLIENT_TICKET_H #define RTCLIENT_TICKET_H +struct rtclient_ticket { + unsigned int id; + char *subject; +}; + struct rtclient_ticketlist { size_t length; - char *tickets[]; + struct rtclient_ticket *tickets[]; }; #ifdef __cplusplus diff --git a/ticket.c b/ticket.c index 77da6b2..c97badd 100644 --- a/ticket.c +++ b/ticket.c @@ -2,6 +2,7 @@ #include "post.h" #include "rtclient/ticket.h" +typedef struct rtclient_ticket rtclient_ticket; typedef struct rtclient_ticketlist rtclient_ticketlist; void rtclient_ticket_new(const char *queue @@ -69,10 +70,13 @@ static size_t search_callback(void *contents, size_t size, size_t nmemb line = strtok_r(NULL, "\n", &linesaveptr); char *tokensaveptr = NULL, *token = NULL; for (unsigned int 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); - list->tickets[i] = malloc(strlen(token)); - strcpy(list->tickets[i], ++token); + ticket->subject = malloc(strlen(token)); + strcpy(ticket->subject, ++token); line = strtok_r(NULL, "\n", &linesaveptr); } } else { @@ -101,8 +105,11 @@ void rtclient_ticket_search(rtclient_ticketlist **listptr, const char *query) void rtclient_ticket_freelist(rtclient_ticketlist *list) { - for (unsigned short i = 0; i < list->length; i++) - free(list->tickets[i]); + for (unsigned short i = 0; i < list->length; i++) { + rtclient_ticket *ticket = list->tickets[i]; + free(ticket->subject); + free(ticket); + } free(list); list = NULL; } -- cgit v1.2.3