From 77bf763939508b712d0fbb2e38f0827f79efc532 Mon Sep 17 00:00:00 2001 From: Dan Cross Date: Thu, 11 Oct 2018 01:46:12 +0000 Subject: [PATCH] Start using stralloc. Clean up a few web page generation functions. Signed-off-by: Dan Cross --- src/bbs.h | 2 + src/stralloc/stralloc.c | 30 ++++++++ src/stralloc/stralloc.h | 6 ++ src/www_msgs.c | 165 ++++++++++------------------------------ 4 files changed, 78 insertions(+), 125 deletions(-) diff --git a/src/bbs.h b/src/bbs.h index e441211..d0cc70a 100644 --- a/src/bbs.h +++ b/src/bbs.h @@ -268,6 +268,8 @@ struct ptr_vector { void **ptrs; }; +static const struct ptr_vector EMPTY_PTR_VECTOR = { 0, 0, NULL }; + extern void init_ptr_vector(struct ptr_vector *vec); extern void ptr_vector_clear(struct ptr_vector *vec); extern void *ptr_vector_get(struct ptr_vector *vec, size_t i); diff --git a/src/stralloc/stralloc.c b/src/stralloc/stralloc.c index e2e8244..180b2fe 100644 --- a/src/stralloc/stralloc.c +++ b/src/stralloc/stralloc.c @@ -63,3 +63,33 @@ int stralloc_append1(stralloc *sa, char b) { sa->s[sa->len++] = b; return 1; } + +static int stralloc_cat_ulong_rec(stralloc *sa, unsigned long uv) { + if (uv == 0) return 1; + if (!stralloc_cat_ulong_rec(sa, uv / 10)) return 0; + return stralloc_append1(sa, (uv % 10) + '0'); +} + +int stralloc_cat_ulong(stralloc *sa, unsigned long uv) { + if (uv == 0) + return stralloc_append1(sa, '0'); + return stralloc_cat_ulong_rec(sa, uv); +} + +int stralloc_cat_long(stralloc *sa, long v) { + if (v < 0) + if (!stralloc_append1(sa, '-')) return 0; + return stralloc_cat_ulong(sa, labs(v)); +} + +int stralloc_cat_byte(stralloc *sa, int b) { + static const char *hex = "0123456789abcdef"; + if (!stralloc_append1(sa, hex[(b >> 4) & 0xF])) return 0; + return stralloc_append1(sa, hex[(b >> 0) & 0xF]); +} + +int stralloc_cat_Byte(stralloc *sa, int b) { + static const char *hex = "0123456789ABCDEF"; + if (!stralloc_append1(sa, hex[(b >> 4) & 0xF])) return 0; + return stralloc_append1(sa, hex[(b >> 0) & 0xF]); +} diff --git a/src/stralloc/stralloc.h b/src/stralloc/stralloc.h index 8c21e9f..7545276 100644 --- a/src/stralloc/stralloc.h +++ b/src/stralloc/stralloc.h @@ -12,11 +12,17 @@ struct stralloc { size_t a; }; +static const stralloc EMPTY_STRALLOC = { NULL, 0, 0 }; + extern int stralloc_ready(stralloc *sa, size_t n); extern int stralloc_starts(stralloc *sa, const char *s); extern int stralloc_copyb(stralloc *sa, const char *s, size_t n); extern int stralloc_catb(stralloc *sa, const char *s, size_t n); extern int stralloc_append1(stralloc *sa, char b); +extern int stralloc_cat_ulong(stralloc *sa, unsigned long uv); +extern int stralloc_cat_long(stralloc *sa, long v); +extern int stralloc_cat_byte(stralloc *sa, int b); +extern int stralloc_cat_Byte(stralloc *sa, int b); static inline int stralloc_readyplus(stralloc *sa, size_t n) { assert(sa != NULL); diff --git a/src/www_msgs.c b/src/www_msgs.c index 2193e8b..87dfc2a 100644 --- a/src/www_msgs.c +++ b/src/www_msgs.c @@ -58,53 +58,35 @@ char *www_sanitize(char *inp) { } char *www_msgs_arealist(struct user_record *user) { - char *page; - int max_len; - int len; - char buffer[4096]; + stralloc page = EMPTY_STRALLOC; int i, j; - page = (char *)malloz(4096); - max_len = 4096; - len = 0; - - sprintf(buffer, "

Message Conferences

\n"); - if (len + strlen(buffer) > max_len - 1) { - max_len += 4096; - page = (char *)realloc(page, max_len); - } - strcat(page, buffer); - len += strlen(buffer); - + stralloc_copys(&page, "

Message Conferences

\n"); for (i = 0; i < conf.mail_conference_count; i++) { if (conf.mail_conferences[i]->sec_level <= user->sec_level) { - sprintf(buffer, "
%s
\n", conf.mail_conferences[i]->name); - if (len + strlen(buffer) > max_len - 1) { - max_len += 4096; - page = (char *)realloc(page, max_len); - } - strcat(page, buffer); - len += strlen(buffer); - + stralloc_cats(&page, "
"); + stralloc_cats(&page, conf.mail_conferences[i]->name); + stralloc_cats(&page, "
\n"); for (j = 0; j < conf.mail_conferences[i]->mail_area_count; j++) { - if (conf.mail_conferences[i]->mail_areas[j]->read_sec_level <= user->sec_level) { - - if (new_messages(user, i, j) > 0) { - sprintf(buffer, "\n", conf.www_url, i, j, conf.mail_conferences[i]->mail_areas[j]->name); - } else { - sprintf(buffer, "\n", conf.www_url, i, j, conf.mail_conferences[i]->mail_areas[j]->name); - } - if (len + strlen(buffer) > max_len - 1) { - max_len += 4096; - page = (char *)realloc(page, max_len); - } - strcat(page, buffer); - len += strlen(buffer); + if (conf.mail_conferences[i]->mail_areas[j]->read_sec_level > user->sec_level) { + continue; } + stralloc_cats(&page, "\n"); } } } - return page; + stralloc_0(&page); + return page.s; } char *www_msgs_messagelist(struct user_record *user, int conference, int area, int skip) { @@ -1085,95 +1067,28 @@ int www_send_msg(struct user_record *user, char *to, char *subj, int conference, } char *www_new_msg(struct user_record *user, int conference, int area) { - char *page; - int max_len; - int len; - char buffer[4096]; + stralloc page = EMPTY_STRALLOC; - page = (char *)malloz(4096); - max_len = 4096; - len = 0; + stralloc_copys(&page, "

New Message

\n"); + stralloc_cats(&page, "
\n"); + stralloc_cats(&page, "\n"); + stralloc_cats(&page, "\n"); + stralloc_cats(&page, "\n"); + stralloc_cats(&page, "To :
\n"); + stralloc_cats(&page, "Subject :
\n"); + stralloc_cats(&page, "\n
"); + stralloc_cats(&page, "\n
"); + stralloc_cats(&page, "
\n"); + stralloc_0(&page); - sprintf(buffer, "

New Message

\n"); - if (len + strlen(buffer) > max_len - 1) { - max_len += 4096; - page = (char *)realloc(page, max_len); - } - strcat(page, buffer); - len += strlen(buffer); - - sprintf(buffer, "
\n", conf.www_url); - if (len + strlen(buffer) > max_len - 1) { - max_len += 4096; - page = (char *)realloc(page, max_len); - } - strcat(page, buffer); - len += strlen(buffer); - - sprintf(buffer, "\n", conference); - if (len + strlen(buffer) > max_len - 1) { - max_len += 4096; - page = (char *)realloc(page, max_len); - } - strcat(page, buffer); - len += strlen(buffer); - sprintf(buffer, "\n", area); - if (len + strlen(buffer) > max_len - 1) { - max_len += 4096; - page = (char *)realloc(page, max_len); - } - strcat(page, buffer); - len += strlen(buffer); - - sprintf(buffer, "\n"); - if (len + strlen(buffer) > max_len - 1) { - max_len += 4096; - page = (char *)realloc(page, max_len); - } - strcat(page, buffer); - len += strlen(buffer); - - sprintf(buffer, "To :
\n"); - if (len + strlen(buffer) > max_len - 1) { - max_len += 4096; - page = (char *)realloc(page, max_len); - } - strcat(page, buffer); - len += strlen(buffer); - - sprintf(buffer, "Subject :
\n"); - if (len + strlen(buffer) > max_len - 1) { - max_len += 4096; - page = (char *)realloc(page, max_len); - } - strcat(page, buffer); - len += strlen(buffer); - - sprintf(buffer, "\n
"); - if (len + strlen(buffer) > max_len - 1) { - max_len += 4096; - page = (char *)realloc(page, max_len); - } - strcat(page, buffer); - len += strlen(buffer); - - sprintf(buffer, "\n
"); - if (len + strlen(buffer) > max_len - 1) { - max_len += 4096; - page = (char *)realloc(page, max_len); - } - strcat(page, buffer); - len += strlen(buffer); - - sprintf(buffer, "
\n"); - if (len + strlen(buffer) > max_len - 1) { - max_len += 4096; - page = (char *)realloc(page, max_len); - } - strcat(page, buffer); - len += strlen(buffer); - - return page; + return page.s; } #endif