From fe162b60af464b8809336942bc46f22ec0dfba1c Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 26 Dec 2020 17:50:47 +0100 Subject: [PATCH] ListPage now loads all files from a directory, sorts them, parses bbcode in them and displays them as messages. --- modules/list_page/list_page.cpp | 78 +++++++++++++++++++++++++++++---- modules/list_page/list_page.h | 5 ++- 2 files changed, 73 insertions(+), 10 deletions(-) diff --git a/modules/list_page/list_page.cpp b/modules/list_page/list_page.cpp index 3890e6b..2f607ba 100644 --- a/modules/list_page/list_page.cpp +++ b/modules/list_page/list_page.cpp @@ -1,28 +1,88 @@ #include "list_page.h" #include "core/query_builder.h" -#include "core/table_builder.h" #include "core/query_result.h" +#include "core/table_builder.h" +#include "core/utils.h" + +#include +#include void ListPage::index(Request *request) { std::string r = ""; - for (uint32_t i = 0; i < messages.size(); ++i) { - r += "

" + messages[i] + "


"; + for (uint32_t i = 0; i < list_entries.size(); ++i) { + r += "
" + list_entries[i] + "
"; } - r += ""; + request->body += r; - request->body += r; + request->compile_and_send_body(); +} - request->compile_and_send_body(); +void ListPage::load() { + if (folder == "") { + printf("Error: ListPage::load called, but a folder is not set!"); + return; + } + + std::vector files; + + tinydir_dir dir; + if (tinydir_open(&dir, folder.c_str()) == -1) { + printf("Error opening ListPage::folder!\n"); + return; + } + + while (dir.has_next) { + tinydir_file file; + if (tinydir_readfile(&dir, &file) == -1) { + tinydir_next(&dir); + continue; + } + + if (!file.is_dir) { + std::string np = file.path; + + files.push_back(np); + } + + tinydir_next(&dir); + } + + tinydir_close(&dir); + + std::sort(files.begin(), files.end()); + + for (uint32_t i = 0; i < files.size(); ++i) { + FILE *f = fopen(files[i].c_str(), "r"); + + if (!f) { + printf("Settings::parse_file: Error opening file!\n"); + return; + } + + fseek(f, 0, SEEK_END); + long fsize = ftell(f); + fseek(f, 0, SEEK_SET); /* same as rewind(f); */ + + std::string fd; + fd.resize(fsize); + + fread(&fd[0], 1, fsize, f); + fclose(f); + + Utils::newline_to_br(&fd); + Utils::bbcode_evaluate_simple(&fd); + + list_entries.push_back(fd); + + //printf("ListPage: Loaded %s!\n", files[i].c_str()); + } } ListPage::ListPage() : Object() { - - messages.push_back("T message 1"); - messages.push_back("T message 2"); } ListPage::~ListPage() { diff --git a/modules/list_page/list_page.h b/modules/list_page/list_page.h index b97b964..c274926 100644 --- a/modules/list_page/list_page.h +++ b/modules/list_page/list_page.h @@ -15,10 +15,13 @@ class ListPage : public Object { public: void index(Request *request); + void load(); + ListPage(); ~ListPage(); - std::vector messages; + std::vector list_entries; + std::string folder; }; #endif \ No newline at end of file