diff options
-rw-r--r-- | ticket.c | 183 |
1 files changed, 76 insertions, 107 deletions
@@ -3,9 +3,10 @@ #include "rtclient/ticket.h" typedef struct rtclient_ticket rtclient_ticket; -typedef struct rtclient_ticket_history_list rtclient_ticket_history_list; -typedef struct rtclient_ticket_history_attachment rtclient_ticket_history_attachment; -typedef struct rtclient_ticket_history_attachment_list rtclient_ticket_history_attachment_list; +typedef struct rtclient_ticket_history ticket_history; +typedef struct rtclient_ticket_history_list history_list; +typedef struct rtclient_ticket_history_attachment history_attachment; +typedef struct rtclient_ticket_history_attachment_list attachment_list; void rtclient_ticket_new(const char *queue , const char *requestor @@ -48,32 +49,27 @@ static size_t history_handler(void *contents, size_t size, size_t nmemb char response[realsize + 1]; memcpy(response, contents, realsize); response[realsize] = '\0'; - rtclient_ticket_history_list **listptr - = (rtclient_ticket_history_list **)writedata; char *linesaveptr = NULL; char *line = strtok_r(response, "\n", &linesaveptr); if (strstr(line, "200 Ok")) { line = strtok_r(NULL, "\n", &linesaveptr); - char *length = strtok(line, "/"); - (*listptr)->length = atoi(&length[2]); - rtclient_ticket_history_list *ptr = realloc(*listptr - , sizeof(*listptr) - + (*listptr)->length - * sizeof(struct rtclient_ticket_history)); - *listptr = ptr; - rtclient_ticket_history_list *list = *listptr; - - char *tokensaveptr = NULL, *token = NULL; - for (size_t i = 0; i < list->length; i++) { + char *lengthstr = strtok(line, "/"); + size_t length = atoi(&lengthstr[2]); + history_list **listptr = (history_list **)writedata; + *listptr = malloc(sizeof(history_list) + + length * sizeof(ticket_history)); + history_list *list = *listptr; + list->length = length; + + for (size_t i = 0; i < length; i++) { line = strtok_r(NULL, "\n", &linesaveptr); - list->histories[i] - = malloc(sizeof(struct rtclient_ticket_history)); - struct rtclient_ticket_history *ticket_history - = list->histories[i]; + list->histories[i] = malloc(sizeof(ticket_history)); + ticket_history *ticket_history = list->histories[i]; - token = strtok_r(line, ":", &tokensaveptr); + char *tokensaveptr = NULL; + char *token = strtok_r(line, ":", &tokensaveptr); ticket_history->id = atoi(token); ticket_history->ticket = 0; @@ -92,13 +88,10 @@ static size_t history_handler(void *contents, size_t size, size_t nmemb ticket_history->creator = NULL; ticket_history->created = NULL; ticket_history->attachments - = malloc - (sizeof(rtclient_ticket_history_attachment_list)); + = malloc(sizeof(attachment_list)); ticket_history->attachments->length = 0; } } else { - free(*listptr); - *listptr = NULL; #ifdef DEBUG #ifdef ANDROID __android_log_print(ANDROID_LOG_INFO, "librtclient" @@ -119,24 +112,22 @@ static size_t history_l_handler(void *contents, size_t size, size_t nmemb char response[realsize + 1]; memcpy(response, contents, realsize); response[realsize] = '\0'; - rtclient_ticket_history_list **listptr - = (rtclient_ticket_history_list **)writedata; - rtclient_ticket_history_list *list = *listptr; char *linesaveptr = NULL; char *line = strtok_r(response, "\n", &linesaveptr); if (strstr(line, "200 Ok")) { + history_list **listptr = (history_list **)writedata; + history_list *list = *listptr; - char *tokensaveptr = NULL, *token = NULL; for (size_t i = 0; i < list->length; i++) { - struct rtclient_ticket_history *ticket_history - = list->histories[i]; + ticket_history *ticket_history = list->histories[i]; strtok_r(NULL, "\n", &linesaveptr); line = strtok_r(NULL, "\n", &linesaveptr); line = strtok_r(NULL, "\n", &linesaveptr); + char *tokensaveptr = NULL; strtok_r(line, ":", &tokensaveptr); - token = strtok_r(NULL, ":", &tokensaveptr); + char *token = strtok_r(NULL, ":", &tokensaveptr); ticket_history->ticket = atoi(++token); line = strtok_r(NULL, "\n", &linesaveptr); @@ -147,57 +138,42 @@ static size_t history_l_handler(void *contents, size_t size, size_t nmemb strtok_r(line, ":", &tokensaveptr); token = strtok_r(NULL, ":", &tokensaveptr); + enum rtclient_ticket_history_type type + = ticket_history->type; if (!strcmp(++token, "AddLink")) - ticket_history->type - = RTCLIENT_TICKET_HISTORY_TYPE_ADD_LINK; + type = RTCLIENT_TICKET_HISTORY_TYPE_ADD_LINK; else if (!strcmp(token, "AddMember")) - ticket_history->type - = RTCLIENT_TICKET_HISTORY_TYPE_ADD_MEMBER; + type = RTCLIENT_TICKET_HISTORY_TYPE_ADD_MEMBER; else if (!strcmp(token, "AddMembership")) - ticket_history->type - = RTCLIENT_TICKET_HISTORY_TYPE_ADD_MEMBERSHIP; + type = RTCLIENT_TICKET_HISTORY_TYPE_ADD_MEMBERSHIP; else if (!strcmp(token, "AddReminder")) - ticket_history->type - = RTCLIENT_TICKET_HISTORY_TYPE_ADD_REMINDER; + type = RTCLIENT_TICKET_HISTORY_TYPE_ADD_REMINDER; else if (!strcmp(token, "AddWatcher")) - ticket_history->type - = RTCLIENT_TICKET_HISTORY_TYPE_ADD_WATCHER; + type = RTCLIENT_TICKET_HISTORY_TYPE_ADD_WATCHER; else if (!strcmp(token, "Comment")) - ticket_history->type - = RTCLIENT_TICKET_HISTORY_TYPE_COMMENT; + type = RTCLIENT_TICKET_HISTORY_TYPE_COMMENT; else if (!strcmp(token, "CommentEmailRecord")) - ticket_history->type - = RTCLIENT_TICKET_HISTORY_TYPE_COMMENT_EMAIL_RECORD; + type = RTCLIENT_TICKET_HISTORY_TYPE_COMMENT_EMAIL_RECORD; else if (!strcmp(token, "Correspond")) - ticket_history->type - = RTCLIENT_TICKET_HISTORY_TYPE_CORRESPOND; + type = RTCLIENT_TICKET_HISTORY_TYPE_CORRESPOND; else if (!strcmp(token, "Create")) - ticket_history->type - = RTCLIENT_TICKET_HISTORY_TYPE_CREATE; + type = RTCLIENT_TICKET_HISTORY_TYPE_CREATE; else if (!strcmp(token, "DelWatcher")) - ticket_history->type - = RTCLIENT_TICKET_HISTORY_TYPE_DEL_WATCHER; + type = RTCLIENT_TICKET_HISTORY_TYPE_DEL_WATCHER; else if (!strcmp(token, "Disabled")) - ticket_history->type - = RTCLIENT_TICKET_HISTORY_TYPE_DISABLED; + type = RTCLIENT_TICKET_HISTORY_TYPE_DISABLED; else if (!strcmp(token, "EmailRecord")) - ticket_history->type - = RTCLIENT_TICKET_HISTORY_TYPE_EMAIL_RECORD; + type = RTCLIENT_TICKET_HISTORY_TYPE_EMAIL_RECORD; else if (!strcmp(token, "ResolveReminder")) - ticket_history->type - = RTCLIENT_TICKET_HISTORY_TYPE_RESOLVE_REMINDER; + type = RTCLIENT_TICKET_HISTORY_TYPE_RESOLVE_REMINDER; else if (!strcmp(token, "Set")) - ticket_history->type - = RTCLIENT_TICKET_HISTORY_TYPE_SET; + type = RTCLIENT_TICKET_HISTORY_TYPE_SET; else if (!strcmp(token, "SetWatcher")) - ticket_history->type - = RTCLIENT_TICKET_HISTORY_TYPE_SET_WATCHER; + type = RTCLIENT_TICKET_HISTORY_TYPE_SET_WATCHER; else if (!strcmp(token, "Status")) - ticket_history->type - = RTCLIENT_TICKET_HISTORY_TYPE_STATUS; + type = RTCLIENT_TICKET_HISTORY_TYPE_STATUS; else if (!strcmp(token, "SystemError")) - ticket_history->type - = RTCLIENT_TICKET_HISTORY_TYPE_SYSTEM_ERROR; + type = RTCLIENT_TICKET_HISTORY_TYPE_SYSTEM_ERROR; line = strtok_r(NULL, "\n", &linesaveptr); strtok_r(line, ":", &tokensaveptr); @@ -237,15 +213,15 @@ static size_t history_l_handler(void *contents, size_t size, size_t nmemb strtok_r(line, ":", &tokensaveptr); token = strtok_r(NULL, ":", &tokensaveptr); ticket_history->content = malloc(strlen(token)); - strcpy(ticket_history->content, ++token); + char *content = ticket_history->content; + strcpy(content, ++token); line = strtok_r(NULL, "\n", &linesaveptr); while (strncmp(line, "Creator", 7)) { - char *ptr = realloc(ticket_history->content - , strlen(ticket_history->content) + ticket_history->content = realloc(content + , strlen(content) + strlen(line) + 2); - ticket_history->content = ptr; - sprintf(ticket_history->content, "%s\n%s" - , ticket_history->content, line); + content = ticket_history->content; + sprintf(content, "%s\n%s", content, line); line = strtok_r(NULL, "\n", &linesaveptr); } @@ -257,21 +233,22 @@ static size_t history_l_handler(void *contents, size_t size, size_t nmemb strtok_r(line, ": -", &tokensaveptr); ticket_history->created = malloc(sizeof(struct tm)); - ticket_history->created->tm_wday = 0; - ticket_history->created->tm_yday = 0; - ticket_history->created->tm_isdst = -1; + struct tm *created = ticket_history->created; + created->tm_wday = 0; + created->tm_yday = 0; + created->tm_isdst = -1; token = strtok_r(NULL, ": -", &tokensaveptr); - ticket_history->created->tm_year = atoi(token) - 1900; + created->tm_year = atoi(token) - 1900; token = strtok_r(NULL, ": -", &tokensaveptr); - ticket_history->created->tm_mon = atoi(token) - 1; + created->tm_mon = atoi(token) - 1; token = strtok_r(NULL, ": -", &tokensaveptr); - ticket_history->created->tm_mday = atoi(token); + created->tm_mday = atoi(token); token = strtok_r(NULL, ": -", &tokensaveptr); - ticket_history->created->tm_hour = atoi(token); + created->tm_hour = atoi(token); token = strtok_r(NULL, ": -", &tokensaveptr); - ticket_history->created->tm_min = atoi(token); + created->tm_min = atoi(token); token = strtok_r(NULL, ": -", &tokensaveptr); - ticket_history->created->tm_sec = atoi(token); + created->tm_sec = atoi(token); line = strtok_r(NULL, "\n", &linesaveptr); line = strtok_r(NULL, "\n", &linesaveptr); @@ -281,23 +258,19 @@ static size_t history_l_handler(void *contents, size_t size, size_t nmemb size_t i = 0; while (strcmp(line, "--")) { - i = ticket_history->attachments->length++; - rtclient_ticket_history_attachment_list *ptr + ticket_history->attachments = realloc(ticket_history->attachments - , sizeof(ticket_history - ->attachments) - + ticket_history - ->attachments->length + , sizeof + (attachment_list) + + i + 1 * sizeof - (rtclient_ticket_history_attachment)); - ticket_history->attachments = ptr; - ticket_history->attachments->attachments[i] - = malloc - (sizeof - (rtclient_ticket_history_attachment)); - rtclient_ticket_history_attachment *attachment - = ticket_history->attachments - ->attachments[i]; + (history_attachment)); + attachment_list *list = ticket_history->attachments; + list->length++; + list->attachments[i] + = malloc(sizeof(history_attachment)); + history_attachment *attachment + = list->attachments[i++]; attachment->id = atoi(token); token = strtok_r(NULL, ": ()", &tokensaveptr); @@ -321,8 +294,6 @@ static size_t history_l_handler(void *contents, size_t size, size_t nmemb } } } else { - free(*listptr); - *listptr = NULL; #ifdef DEBUG #ifdef ANDROID __android_log_print(ANDROID_LOG_INFO, "librtclient" @@ -336,11 +307,9 @@ static size_t history_l_handler(void *contents, size_t size, size_t nmemb return realsize; } -void rtclient_ticket_history(rtclient_ticket_history_list **listptr +void rtclient_ticket_history(history_list **listptr , unsigned int id, bool long_format) { - *listptr = malloc(sizeof(rtclient_ticket_history_list)); - (*listptr)->length = 0; request(history_handler, (void *)listptr, NULL, "%s%u%s" , "REST/1.0/ticket/", id, "/history"); if (long_format) @@ -348,16 +317,16 @@ void rtclient_ticket_history(rtclient_ticket_history_list **listptr , "REST/1.0/ticket/", id, "/history?format=l"); } -void rtclient_ticket_history_free(struct rtclient_ticket_history *history) +void rtclient_ticket_history_free(ticket_history *history) { - for (size_t i = 0; i < history->attachments->length; i++) { - rtclient_ticket_history_attachment *attachment - = history->attachments->attachments[i]; + attachment_list *list = history->attachments; + for (size_t i = 0; i < list->length; i++) { + history_attachment *attachment = list->attachments[i]; if (attachment->file_name) free(attachment->file_name); free(attachment); } - free(history->attachments); + free(list); free(history->created); free(history->creator); free(history->content); @@ -374,7 +343,7 @@ void rtclient_ticket_history_free(struct rtclient_ticket_history *history) history = NULL; } -void rtclient_ticket_history_list_free(struct rtclient_ticket_history_list *list) +void rtclient_ticket_history_list_free(history_list *list) { for (size_t i = 0; i < list->length; i++) rtclient_ticket_history_free(list->histories[i]); |