From d4e80de3971a28850becba876e9e46483a76d1ff Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Mon, 22 Oct 2018 10:47:20 +1000 Subject: [PATCH] Convert www_files to tree style --- src/www_files.c | 206 +++++++++++++++++++++++++++++++++++------------- src/www_tree.c | 16 +++- src/www_tree.h | 1 + 3 files changed, 168 insertions(+), 55 deletions(-) diff --git a/src/www_files.c b/src/www_files.c index 5274d90..8886da2 100644 --- a/src/www_files.c +++ b/src/www_files.c @@ -296,8 +296,14 @@ char *www_create_link(int dir, int sub, int fid) { } char *www_files_display_listing(int dir, int sub) { static const char *sql = "select id, filename, description, size, dlcount, uploaddate from files where approved=1 ORDER BY filename"; - - stralloc page = EMPTY_STRALLOC; + struct www_tag *page = www_tag_new(NULL, ""); + struct www_tag *cur_tag; + struct www_tag *child_tag; + struct www_tag *child_child_tag; + struct www_tag *child_child_child_tag; + struct www_tag *child_child_child_child_tag; + struct www_tag *child_child_child_child_child_tag; + //stralloc page = EMPTY_STRALLOC; char pathbuf[PATH_MAX]; sqlite3 *db = NULL; sqlite3_stmt *res = NULL; @@ -312,43 +318,107 @@ char *www_files_display_listing(int dir, int sub) { struct file_sub *fsub = ptr_vector_get(&fdir->file_subs, sub); assert(fsub != NULL); - stralloc_copys(&page, "

Files: "); - stralloc_cats(&page, fdir->name); - stralloc_cats(&page, " - "); - stralloc_cats(&page, fsub->name); - stralloc_cats(&page, "

\n"); + + cur_tag = www_tag_new("div", NULL); + www_tag_add_attrib(cur_tag, "class", "content-header"); + www_tag_add_child(page, cur_tag); + + child_tag = www_tag_new("h2", NULL); + www_tag_add_child(cur_tag, child_tag); + + child_child_tag = www_tag_new(NULL, "Files: "); + www_tag_add_child(child_tag, child_child_tag); + + child_child_tag = www_tag_new(NULL, fdir->name); + www_tag_add_child(child_tag, child_child_tag); + + child_child_tag = www_tag_new(NULL, " - "); + www_tag_add_child(child_tag, child_child_tag); + + child_child_tag = www_tag_new(NULL, fsub->name); + www_tag_add_child(child_tag, child_child_tag); snprintf(pathbuf, sizeof pathbuf, "%s/%s.sq3", conf.bbs_path, fsub->database); rc = sqlite3_open(pathbuf, &db); if (rc != SQLITE_OK) { - free(page.s); + www_tag_destroy(page); return NULL; } sqlite3_busy_timeout(db, 5000); rc = sqlite3_prepare_v2(db, sql, -1, &res, 0); if (rc != SQLITE_OK) { sqlite3_close(db); - free(page.s); + www_tag_destroy(page); return NULL; } - stralloc_cats(&page, "\n"); + + cur_tag = www_tag_new("table", NULL); + www_tag_add_attrib(cur_tag, "class", "fileentry"); + www_tag_add_child(page, cur_tag); + + child_tag = www_tag_new("thead", NULL); + www_tag_add_child(cur_tag, child_tag); + + child_child_tag = www_tag_new("tr", NULL); + www_tag_add_child(child_tag, child_child_tag); + + child_child_child_tag = www_tag_new("td", NULL); + www_tag_add_child(child_child_tag, child_child_child_tag); + + child_child_child_child_tag = www_tag_new(NULL, "Filename"); + www_tag_add_child(child_child_child_tag, child_child_child_child_tag); + + child_child_child_tag = www_tag_new("td", NULL); + www_tag_add_child(child_child_tag, child_child_child_tag); + + child_child_child_child_tag = www_tag_new(NULL, "Size"); + www_tag_add_child(child_child_child_tag, child_child_child_child_tag); + + child_child_child_tag = www_tag_new("td", NULL); + www_tag_add_child(child_child_tag, child_child_child_tag); + + child_child_child_child_tag = www_tag_new(NULL, "Description"); + www_tag_add_child(child_child_child_tag, child_child_child_child_tag); + + child_tag = www_tag_new("tbody", NULL); + www_tag_add_child(cur_tag, child_tag); + while (sqlite3_step(res) == SQLITE_ROW) { char *filename = strdup((char *)sqlite3_column_text(res, 1)); char *base_filename = basename(filename); - stralloc_cats(&page, ""); + child_child_tag = www_tag_new("tr", NULL); + www_tag_add_child(child_tag, child_child_tag); + + child_child_child_tag = www_tag_new("td", NULL); + www_tag_add_attrib(child_child_child_tag, "class", "filename"); + www_tag_add_child(child_child_tag, child_child_child_tag); + + child_child_child_child_tag = www_tag_new("a", NULL); + www_tag_add_child(child_child_child_tag, child_child_child_child_tag); + + stralloc url = EMPTY_STRALLOC; + + stralloc_cats(&url, conf.www_url); + stralloc_cats(&url, "files/areas/"); + stralloc_cat_long(&url, dir); + stralloc_append1(&url, '/'); + stralloc_cat_long(&url, sub); + stralloc_append1(&url, '/'); + www_encode(&url, base_filename); + + stralloc_0(&url); + + www_tag_add_attrib(child_child_child_child_tag, "href", url.s); + free(url.s); + + child_child_child_child_child_tag = www_tag_new(NULL, base_filename); + www_tag_add_child(child_child_child_child_tag, child_child_child_child_child_tag); int size = sqlite3_column_int(res, 3); - stralloc_cats(&page, ""); + + stralloc size_str = EMPTY_STRALLOC; + + stralloc_cat_long(&size_str, size); + stralloc_append1(&size_str, c); - stralloc_cats(&page, "\n"); } - stralloc_cats(&page, "
FilenameSizeDescription
"); - stralloc_cats(&page, base_filename); - stralloc_cats(&page, ""); + child_child_child_tag = www_tag_new("td", NULL); + www_tag_add_attrib(child_child_child_tag, "class", "filesize"); + www_tag_add_child(child_child_tag, child_child_child_tag); + int c = 'b'; if (size > 1024) { size /= 1024; @@ -362,64 +432,92 @@ char *www_files_display_listing(int dir, int sub) { size /= 1024; c = 'G'; } - stralloc_cat_long(&page, size); - stralloc_append1(&page, c); - stralloc_cats(&page, ""); + child_child_child_child_tag = www_tag_new(NULL, size_str.s); + www_tag_add_child(child_child_child_tag, child_child_child_child_tag); + free(size_str.s); + + child_child_child_tag = www_tag_new("td", NULL); + www_tag_add_attrib(child_child_child_tag, "class", "filedesc"); + www_tag_add_child(child_child_tag, child_child_child_tag); + char *description = strdup((char *)sqlite3_column_text(res, 2)); for (char *p = description; *p != '\0'; ++p) { if (*p == '\n') *p = '\r'; } - struct www_tag *aha_out = www_tag_new(NULL, ""); - aha(description, aha_out); - - char *aha_data = www_tag_unwravel(aha_out); - - stralloc_cats(&page, aha_data); - free(aha_data); + aha(description, child_child_child_tag); + free(description); free(filename); - - stralloc_cats(&page, "
\n"); - stralloc_0(&page); sqlite3_finalize(res); sqlite3_close(db); - return page.s; + return www_tag_unwravel(page); } char *www_files_areas() { - stralloc page = EMPTY_STRALLOC; + struct www_tag *page = www_tag_new(NULL, ""); + struct www_tag *cur_tag; + struct www_tag *child_tag; + struct www_tag *child_child_tag; + + cur_tag = www_tag_new("div", NULL); + www_tag_add_attrib(cur_tag, "class", "content-header"); + www_tag_add_child(page, cur_tag); + + child_tag = www_tag_new("h2", NULL); + www_tag_add_child(cur_tag, child_tag); + + child_child_tag = www_tag_new(NULL, "File Directories"); + www_tag_add_child(child_tag, child_child_tag); - stralloc_copys(&page, "

File Directories

\n"); for (size_t i = 0; i < ptr_vector_len(&conf.file_directories); ++i) { struct file_directory *dir = ptr_vector_get(&conf.file_directories, i); if (!dir->display_on_web) continue; - stralloc_cats(&page, "
"); - stralloc_cats(&page, dir->name); - stralloc_cats(&page, "
\n"); + + cur_tag = www_tag_new("div", NULL); + www_tag_add_attrib(cur_tag, "class", "conference-list-item"); + www_tag_add_child(page, cur_tag); + + child_tag = www_tag_new(NULL, dir->name); + www_tag_add_child(cur_tag, child_tag); + for (size_t j = 0; j < ptr_vector_len(&dir->file_subs); ++j) { struct file_sub *sub = ptr_vector_get(&dir->file_subs, j); - stralloc_cats(&page, "
"); - stralloc_cats(&page, sub->name); - stralloc_cats(&page, "
\n"); + cur_tag = www_tag_new("div", NULL); + www_tag_add_attrib(cur_tag, "class", "area-list-item"); + www_tag_add_child(page, cur_tag); + + child_tag = www_tag_new("a", NULL); + + stralloc url = EMPTY_STRALLOC; + + stralloc_cats(&url, conf.www_url); + stralloc_cats(&url, "files/areas/"); + stralloc_cat_long(&url, i); + stralloc_append1(&url, '/'); + stralloc_cat_long(&url, j); + stralloc_0(&url); + + www_tag_add_attrib(child_tag, "href", url.s); + free(url.s); + www_tag_add_child(cur_tag, child_tag); + + child_child_tag = www_tag_new(NULL, sub->name); + www_tag_add_child(child_tag, child_child_tag); } } - stralloc_0(&page); - return page.s; + return www_tag_unwravel(page); } char *www_files_get_from_area(int dir, int sub, char *clean_file) { diff --git a/src/www_tree.c b/src/www_tree.c index 430db11..93a89c5 100644 --- a/src/www_tree.c +++ b/src/www_tree.c @@ -581,9 +581,23 @@ void www_tag_add_child(struct www_tag *tag, struct www_tag *child) { ptr_vector_append(&tag->children, child); } +char *www_tag_destroy(struct www_tag *tag) { + while (tag->children.len > 0) { + struct www_tag *child = ptr_vector_del(&tag->children, 0); + www_tag_destroy(child); + } + + if (tag->tag != NULL) { + ptr_vector_apply(&tag->attribs, free); + destroy_ptr_vector(&tag->attribs); + ptr_vector_apply(&tag->values, free); + destroy_ptr_vector(&tag->values); + } + destroy_ptr_vector(&tag->children); +} + char *www_tag_unwravel(struct www_tag *tag) { stralloc thedata = EMPTY_STRALLOC; - int children = tag->children.len; while (tag->children.len > 0) { struct www_tag *child = ptr_vector_del(&tag->children, 0); if (child->children.len > 0) { diff --git a/src/www_tree.h b/src/www_tree.h index faae72c..fbc7f41 100644 --- a/src/www_tree.h +++ b/src/www_tree.h @@ -17,6 +17,7 @@ extern struct www_tag *www_tag_new(char *tag, char *data); extern void www_tag_add_attrib(struct www_tag *tag, char *attrib, char *value); extern struct www_tag *www_tag_duplicate(struct www_tag *oldtag); extern void www_tag_add_child(struct www_tag *tag, struct www_tag *child); +extern char *www_tag_destroy(struct www_tag *tag); extern char *www_tag_unwravel(struct www_tag *tag); #endif #endif