room_functions.c: get_msglist() now returns Array
authorArt Cancro <ajc@citadel.org>
Fri, 22 Dec 2023 16:10:14 +0000 (11:10 -0500)
committerArt Cancro <ajc@citadel.org>
Fri, 22 Dec 2023 16:10:14 +0000 (11:10 -0500)
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.

webcit-ng/server/room_functions.c
webcit-ng/server/webcit.h

index 8d8ca3bcc5113c670c5a48161a5b630a39d1a858..9153319c64d51beac9542ec3f535368de8763f4c 100644 (file)
@@ -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, "<D:response>");
 
@@ -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, "<D:getetag>\"%ld\"</D:getetag>", msglist[i]);
+                                               StrBufAppendPrintf(Buf, "<D:getetag>\"%ld\"</D:getetag>", m);
                                        }
                                }
                                StrBufAppendPrintf(Buf, "</D:prop></D:propstat></D:response>\n");
                                free(e);
                        }
-                       free(msglist);
+                       array_free(msglist);
                };
        }
        // END COLLECTION
index 8af98ec26cf4ebd1cf21f5d991845e589ff9ab12..956e248639a60d2dc03e7aade05e536e590e19aa 100644 (file)
@@ -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);