summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorꦌ ꦫꦶ ꦏ꧀ꦦꦿ ꦧ ꦮ ꦑ ꦩ ꦭ꧀ <erik@darapsa.co.id>2019-10-06 22:45:25 +0800
committerꦌ ꦫꦶ ꦏ꧀ꦦꦿ ꦧ ꦮ ꦑ ꦩ ꦭ꧀ <erik@darapsa.co.id>2019-10-06 22:45:25 +0800
commit758050268f93c45e0eda1f7b39bb080ab212c97d (patch)
tree579bc5c5ba55c401598e9f365253cb3f211dbdb2
parentabaaef65e251fe5c39f01349e172e7d85c8d6788 (diff)
Further simplify memory allocation
-rw-r--r--search.c33
1 files changed, 15 insertions, 18 deletions
diff --git a/search.c b/search.c
index ff7895a..9a98f12 100644
--- a/search.c
+++ b/search.c
@@ -15,43 +15,42 @@ static size_t ticket_handler(void *contents, size_t size, size_t nmemb
response[realsize] = '\0';
char lines[strlen(response) + 1];
strcpy(lines, response);
- rtclient_search_ticket_list **listptr
- = (rtclient_search_ticket_list **)writedata;
- rtclient_search_ticket_list *list = *listptr;
char *line = strtok(response, "\n");
if (strstr(line, "200 Ok")) {
line = strtok(NULL, "\n");
+ size_t length = 0;
do {
- list->length++;
- if (!strcmp(line, "No matching results.")) {
- free(*listptr);
- *listptr = NULL;
+ length++;
+ if (!strcmp(line, "No matching results."))
return realsize;
- }
} while ((line = strtok(NULL, "\n")));
- *listptr = realloc(list, sizeof(rtclient_search_ticket_list)
- + list->length * sizeof(rtclient_ticket));
- list = *listptr;
+ rtclient_search_ticket_list **listptr
+ = (rtclient_search_ticket_list **)writedata;
+ *listptr = malloc(sizeof(rtclient_search_ticket_list)
+ + length * sizeof(rtclient_ticket));
+ rtclient_search_ticket_list *list = *listptr;
+ list->length = length;
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++) {
+ for (size_t i = 0; i < length; i++) {
list->tickets[i] = malloc(sizeof(rtclient_ticket));
rtclient_ticket *ticket = list->tickets[i];
- token = strtok_r(line, ":", &tokensaveptr);
+
+ char *tokensaveptr = NULL;
+ char *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"
@@ -68,8 +67,6 @@ static size_t ticket_handler(void *contents, size_t size, size_t nmemb
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);
}