From: Art Cancro Date: Fri, 22 Dec 2023 16:10:14 +0000 (-0500) Subject: room_functions.c: get_msglist() now returns Array X-Git-Tag: v997~59 X-Git-Url: https://code.citadel.org/?p=citadel.git;a=commitdiff_plain;h=372fb8eddb5c00a42314dd32f65f57394b90a7d2 room_functions.c: get_msglist() now returns Array Previously it returned a long* which contained a list of longs. Assembling this required the same allocation logic used in our array class, so now we're just using the array class. --- diff --git a/webcit-ng/server/room_functions.c b/webcit-ng/server/room_functions.c index 8d8ca3bcc..9153319c6 100644 --- a/webcit-ng/server/room_functions.c +++ b/webcit-ng/server/room_functions.c @@ -8,32 +8,26 @@ #include "webcit.h" -// Return a "zero-terminated" array of message numbers in the current room. +// Return an array of message numbers in the current room. // Caller owns the memory and must free it. Returns NULL if any problems. -long *get_msglist(struct ctdlsession *c, char *which_msgs) { +Array *get_msglist(struct ctdlsession *c, char *which_msgs) { char buf[1024]; - long *msglist = NULL; - int num_msgs = 0; - int num_alloc = 0; + Array *msglist = NULL; + + msglist = array_new(sizeof(long)); + if (msglist == NULL) { + return(NULL); + } ctdl_printf(c, "MSGS %s", which_msgs); ctdl_readline(c, buf, sizeof(buf)); if (buf[0] == '1') { - do { - if (num_msgs >= num_alloc) { - if (num_alloc == 0) { - num_alloc = 1024; - msglist = malloc(num_alloc * sizeof(long)); - } - else { - num_alloc *= 2; - msglist = realloc(msglist, num_alloc * sizeof(long)); - } - } - ctdl_readline(c, buf, sizeof(buf)); - msglist[num_msgs++] = atol(buf); - } while (strcmp(buf, "000")); // this makes the last element a "0" terminator + while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) { + long m = atol(buf); + array_append(msglist, &m); + } } + return msglist; } @@ -146,14 +140,16 @@ void json_mailbox(struct http_transaction *h, struct ctdlsession *c) { // Client is requesting a message list void json_msglist(struct http_transaction *h, struct ctdlsession *c, char *which) { int i = 0; - long *msglist = get_msglist(c, which); + Array *msglist = get_msglist(c, which); JsonValue *j = NewJsonArray(HKEY("msgs")); if (msglist != NULL) { - for (i = 0; msglist[i] > 0; ++i) { - JsonArrayAppend(j, NewJsonNumber(HKEY("m"), msglist[i])); + for (i = 0; i < array_len(msglist); ++i) { + long m; + memcpy(&m, array_get_element_at(msglist, i), sizeof(long)); + JsonArrayAppend(j, NewJsonNumber(HKEY("m"), m)); } - free(msglist); + array_free(msglist); } StrBuf *sj = NewStrBuf(); @@ -418,18 +414,21 @@ void propfind_the_room_itself(struct http_transaction *h, struct ctdlsession *c) // If a depth greater than zero was specified, transmit the collection listing // BEGIN COLLECTION if (dav_depth > 0) { - long *msglist = get_msglist(c, "ALL"); + Array *msglist = get_msglist(c, "ALL"); if (msglist) { int i; - for (i = 0; (msglist[i] > 0); ++i) { + for (i = 0; i < array_len(msglist); ++i) { if ((i % 10) == 0) { syslog(LOG_DEBUG, "PROPFIND enumerated %d messages", i); } e = NULL; // EUID gets stored here timestamp = 0; + long m; + memcpy(&m, array_get_element_at(msglist, i), sizeof(long)); + char cbuf[1024]; - ctdl_printf(c, "MSG0 %ld|3", msglist[i]); + ctdl_printf(c, "MSG0 %ld|3", m); ctdl_readline(c, cbuf, sizeof(cbuf)); if (cbuf[0] == '1') while (ctdl_readline(c, cbuf, sizeof(cbuf)), strcmp(cbuf, "000")) { @@ -445,7 +444,7 @@ void propfind_the_room_itself(struct http_transaction *h, struct ctdlsession *c) } if (e == NULL) { e = malloc(20); - sprintf(e, "%ld", msglist[i]); + sprintf(e, "%ld", m); } StrBufAppendPrintf(Buf, ""); @@ -484,13 +483,13 @@ void propfind_the_room_itself(struct http_transaction *h, struct ctdlsession *c) free(datestring); } if (enumerate_by_euid) { // FIXME ajc 2017oct30 should this be inside the timestamp conditional? - StrBufAppendPrintf(Buf, "\"%ld\"", msglist[i]); + StrBufAppendPrintf(Buf, "\"%ld\"", m); } } StrBufAppendPrintf(Buf, "\n"); free(e); } - free(msglist); + array_free(msglist); }; } // END COLLECTION diff --git a/webcit-ng/server/webcit.h b/webcit-ng/server/webcit.h index 8af98ec26..956e24863 100644 --- a/webcit-ng/server/webcit.h +++ b/webcit-ng/server/webcit.h @@ -156,7 +156,6 @@ char *get_url_param(struct http_transaction *h, char *requested_param); int unescape_input(char *); void http_redirect(struct http_transaction *h, char *to_where); char *http_datestring(time_t xtime); -long *get_msglist(struct ctdlsession *c, char *which_msgs); void caldav_report(struct http_transaction *h, struct ctdlsession *c); long locate_message_by_uid(struct ctdlsession *c, char *uid); void ctdl_delete_msgs(struct ctdlsession *c, long *msgnums, int num_msgs);