Start using stralloc.

Clean up a few web page generation functions.

Signed-off-by: Dan Cross <patchdev@fat-dragon.org>
This commit is contained in:
Dan Cross 2018-10-11 01:46:12 +00:00 committed by Andrew Pamment
parent e196292503
commit 77bf763939
4 changed files with 78 additions and 125 deletions

View File

@ -268,6 +268,8 @@ struct ptr_vector {
void **ptrs; void **ptrs;
}; };
static const struct ptr_vector EMPTY_PTR_VECTOR = { 0, 0, NULL };
extern void init_ptr_vector(struct ptr_vector *vec); extern void init_ptr_vector(struct ptr_vector *vec);
extern void ptr_vector_clear(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); extern void *ptr_vector_get(struct ptr_vector *vec, size_t i);

View File

@ -63,3 +63,33 @@ int stralloc_append1(stralloc *sa, char b) {
sa->s[sa->len++] = b; sa->s[sa->len++] = b;
return 1; 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]);
}

View File

@ -12,11 +12,17 @@ struct stralloc {
size_t a; size_t a;
}; };
static const stralloc EMPTY_STRALLOC = { NULL, 0, 0 };
extern int stralloc_ready(stralloc *sa, size_t n); extern int stralloc_ready(stralloc *sa, size_t n);
extern int stralloc_starts(stralloc *sa, const char *s); extern int stralloc_starts(stralloc *sa, const char *s);
extern int stralloc_copyb(stralloc *sa, const char *s, size_t n); 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_catb(stralloc *sa, const char *s, size_t n);
extern int stralloc_append1(stralloc *sa, char b); 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) { static inline int stralloc_readyplus(stralloc *sa, size_t n) {
assert(sa != NULL); assert(sa != NULL);

View File

@ -58,53 +58,35 @@ char *www_sanitize(char *inp) {
} }
char *www_msgs_arealist(struct user_record *user) { char *www_msgs_arealist(struct user_record *user) {
char *page; stralloc page = EMPTY_STRALLOC;
int max_len;
int len;
char buffer[4096];
int i, j; int i, j;
page = (char *)malloz(4096); stralloc_copys(&page, "<div class=\"content-header\"><h2>Message Conferences</h2></div>\n");
max_len = 4096;
len = 0;
sprintf(buffer, "<div class=\"content-header\"><h2>Message Conferences</h2></div>\n");
if (len + strlen(buffer) > max_len - 1) {
max_len += 4096;
page = (char *)realloc(page, max_len);
}
strcat(page, buffer);
len += strlen(buffer);
for (i = 0; i < conf.mail_conference_count; i++) { for (i = 0; i < conf.mail_conference_count; i++) {
if (conf.mail_conferences[i]->sec_level <= user->sec_level) { if (conf.mail_conferences[i]->sec_level <= user->sec_level) {
sprintf(buffer, "<div class=\"conference-list-item\">%s</div>\n", conf.mail_conferences[i]->name); stralloc_cats(&page, "<div class=\"conference-list-item\">");
if (len + strlen(buffer) > max_len - 1) { stralloc_cats(&page, conf.mail_conferences[i]->name);
max_len += 4096; stralloc_cats(&page, "</div>\n");
page = (char *)realloc(page, max_len);
}
strcat(page, buffer);
len += strlen(buffer);
for (j = 0; j < conf.mail_conferences[i]->mail_area_count; j++) { 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 (conf.mail_conferences[i]->mail_areas[j]->read_sec_level > user->sec_level) {
continue;
if (new_messages(user, i, j) > 0) {
sprintf(buffer, "<div class=\"area-list-new\"><a href=\"%smsgs/%d/%d/\">%s</a></div>\n", conf.www_url, i, j, conf.mail_conferences[i]->mail_areas[j]->name);
} else {
sprintf(buffer, "<div class=\"area-list-item\"><a href=\"%smsgs/%d/%d/\">%s</a></div>\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);
} }
stralloc_cats(&page, "<div class=\"area-list-");
stralloc_cats(&page, (new_messages(user, i, j) > 0) ? "new" : "item");
stralloc_cats(&page, "\"><a href=\"");
stralloc_cats(&page, conf.www_url);
stralloc_cats(&page, "msgs/");
stralloc_cat_long(&page, i);
stralloc_append1(&page, '/');
stralloc_cat_long(&page, j);
stralloc_cats(&page, "/\">");
stralloc_cats(&page, conf.mail_conferences[i]->mail_areas[j]->name);
stralloc_cats(&page, "</a></div>\n");
} }
} }
} }
return page; stralloc_0(&page);
return page.s;
} }
char *www_msgs_messagelist(struct user_record *user, int conference, int area, int skip) { 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 *www_new_msg(struct user_record *user, int conference, int area) {
char *page; stralloc page = EMPTY_STRALLOC;
int max_len;
int len;
char buffer[4096];
page = (char *)malloz(4096); stralloc_copys(&page, "<div class=\"content-header\"><h2>New Message</h2></div>\n");
max_len = 4096; stralloc_cats(&page, "<form action=\"");
len = 0; stralloc_cats(&page, conf.www_url);
stralloc_cats(&page, "msgs/\" method=\"POST\" onsubmit=\"return validate()\" "
"enctype=\"application/x-www-form-urlencoded;charset=UTF-8\">\n");
stralloc_cats(&page, "<input type=\"hidden\" name=\"conference\" value=\"");
stralloc_cat_long(&page, conference);
stralloc_cats(&page, "\" />\n");
stralloc_cats(&page, "<input type=\"hidden\" name=\"area\" value=\"");
stralloc_cat_long(&page, area);
stralloc_cats(&page, "\" />\n");
stralloc_cats(&page, "<input type=\"hidden\" name=\"replyid\" value=\"NULL\" />\n");
stralloc_cats(&page, "To : <input type=\"text\" name=\"recipient\" value=\"All\" id=\"recipient\"/><br />\n");
stralloc_cats(&page, "Subject : <input type=\"text\" name=\"subject\" id=\"subject\" /><br />\n");
stralloc_cats(&page, "<textarea name=\"body\" id=\"body\" rows=\"25\" cols=\"79\" wrap=\"soft\"></textarea>\n<br />");
stralloc_cats(&page, "<input type=\"submit\" name=\"submit\" value=\"Send\" />\n<br />");
stralloc_cats(&page, "</form>\n");
stralloc_0(&page);
sprintf(buffer, "<div class=\"content-header\"><h2>New Message</h2></div>\n"); return page.s;
if (len + strlen(buffer) > max_len - 1) {
max_len += 4096;
page = (char *)realloc(page, max_len);
}
strcat(page, buffer);
len += strlen(buffer);
sprintf(buffer, "<form action=\"%smsgs/\" method=\"POST\" onsubmit=\"return validate()\" enctype=\"application/x-www-form-urlencoded;charset=UTF-8\">\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, "<input type=\"hidden\" name=\"conference\" value=\"%d\" />\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, "<input type=\"hidden\" name=\"area\" value=\"%d\" />\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, "<input type=\"hidden\" name=\"replyid\" value=\"NULL\" />\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 : <input type=\"text\" name=\"recipient\" value=\"All\" id=\"recipient\"/><br />\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 : <input type=\"text\" name=\"subject\" id=\"subject\" /><br />\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, "<textarea name=\"body\" id=\"body\" rows=\"25\" cols=\"79\" wrap=\"soft\"></textarea>\n<br />");
if (len + strlen(buffer) > max_len - 1) {
max_len += 4096;
page = (char *)realloc(page, max_len);
}
strcat(page, buffer);
len += strlen(buffer);
sprintf(buffer, "<input type=\"submit\" name=\"submit\" value=\"Send\" />\n<br />");
if (len + strlen(buffer) > max_len - 1) {
max_len += 4096;
page = (char *)realloc(page, max_len);
}
strcat(page, buffer);
len += strlen(buffer);
sprintf(buffer, "</form>\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;
} }
#endif #endif