From f7b5952d2aa49e475c9f26f2740bea91ce7cdf04 Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 1 Nov 2021 19:53:35 +0100 Subject: [PATCH] Did most of the remaining std::string and vector to String and Vector conversions. --- core/file_cache.cpp | 14 ++--- core/file_cache.h | 21 +++---- core/resource.cpp | 4 +- core/resource.h | 6 +- core/settings.cpp | 6 +- core/settings.h | 4 +- core/string.cpp | 74 ++++++++++++++++++++++++- core/string.h | 6 +- core/utils.cpp | 28 +++------- core/utils.h | 14 ++--- modules/list_page/list_page.cpp | 11 ++-- modules/list_page/list_page.h | 8 +-- modules/message_page/message_page.cpp | 4 +- modules/message_page/message_page.h | 6 +- modules/paged_article/paged_article.cpp | 43 +++++++------- modules/paged_article/paged_article.h | 20 +++---- modules/paged_list/paged_list.cpp | 2 +- modules/paged_list/paged_list.h | 6 +- modules/users/user.cpp | 6 +- modules/users/user.h | 19 ++++--- 20 files changed, 183 insertions(+), 119 deletions(-) diff --git a/core/file_cache.cpp b/core/file_cache.cpp index ae44d66..91102eb 100644 --- a/core/file_cache.cpp +++ b/core/file_cache.cpp @@ -4,15 +4,15 @@ #include -void FileCache::wwwroot_register_file(const std::string &file_path) { +void FileCache::wwwroot_register_file(const String &file_path) { registered_files.insert(file_path); } -void FileCache::wwwroot_deregister_file(const std::string &file_path) { +void FileCache::wwwroot_deregister_file(const String &file_path) { registered_files.erase(file_path); } -bool FileCache::wwwroot_has_file(const std::string &file_path) { +bool FileCache::wwwroot_has_file(const String &file_path) { return registered_files.find(file_path) != registered_files.end(); } @@ -40,7 +40,7 @@ void FileCache::wwwroot_evaluate_dir(const char *path, const bool should_exist) } if (!file.is_dir) { - std::string np = file.path; + String np = file.path; np = np.substr(wwwroot.size(), np.size() - wwwroot.size()); registered_files.insert(np); @@ -59,7 +59,7 @@ void FileCache::wwwroot_evaluate_dir(const char *path, const bool should_exist) tinydir_close(&dir); } -bool FileCache::get_cached_body(const std::string &path, std::string *body) { +bool FileCache::get_cached_body(const String &path, String *body) { //TODO ERROR MACRO body == null //this shouldn't need mutexes @@ -78,12 +78,12 @@ bool FileCache::get_cached_body(const std::string &path, std::string *body) { return false; } - body->append(e->body); + body->append_str(e->body); return true; } -void FileCache::set_cached_body(const std::string &path, const std::string &body) { +void FileCache::set_cached_body(const String &path, const String &body) { cache_mutex.lock(); CacheEntry *e = cache_map[path]; diff --git a/core/file_cache.h b/core/file_cache.h index 4f57740..56b8738 100644 --- a/core/file_cache.h +++ b/core/file_cache.h @@ -1,7 +1,8 @@ #ifndef FILE_CACHE_H #define FILE_CACHE_H -#include +#include "core/string.h" + #include #include #include @@ -9,32 +10,32 @@ class FileCache { public: - std::string wwwroot; + String wwwroot; int cache_invalidation_time; //Note: file path should be the url you want to access the file with, inculding lead slash //e.g. http://127.0.0.1/a/b/d.jpg -> /a/b/d.jpg - void wwwroot_register_file(const std::string &file_path); - void wwwroot_deregister_file(const std::string &file_path); - bool wwwroot_has_file(const std::string &file_path); + void wwwroot_register_file(const String &file_path); + void wwwroot_deregister_file(const String &file_path); + bool wwwroot_has_file(const String &file_path); void wwwroot_refresh_cache(); void wwwroot_evaluate_dir(const char *path, const bool should_exist = true); - bool get_cached_body(const std::string &path, std::string *body); - void set_cached_body(const std::string &path, const std::string &body); + bool get_cached_body(const String &path, String *body); + void set_cached_body(const String &path, const String &body); FileCache(bool singleton = false); virtual ~FileCache(); static FileCache *get_singleton(); - std::set registered_files; + std::set registered_files; protected: struct CacheEntry { int64_t timestamp; - std::string body; + String body; CacheEntry() { timestamp = 0; @@ -43,7 +44,7 @@ protected: std::mutex cache_mutex; - std::map cache_map; + std::map cache_map; private: static FileCache *_instance; diff --git a/core/resource.cpp b/core/resource.cpp index 1359fde..9194a8e 100644 --- a/core/resource.cpp +++ b/core/resource.cpp @@ -1,9 +1,9 @@ #include "resource.h" -std::string Resource::to_json(rapidjson::Document *into) { +String Resource::to_json(rapidjson::Document *into) { return ""; } -void Resource::from_json(const std::string &data) { +void Resource::from_json(const String &data) { } Resource::Resource() : diff --git a/core/resource.h b/core/resource.h index 1a0981b..e8f6e8a 100644 --- a/core/resource.h +++ b/core/resource.h @@ -1,7 +1,7 @@ #ifndef RESOURCE_H #define RESOURCE_H -#include +#include "core/string.h" #include "reference.h" #include "rapidjson/document.h" @@ -16,8 +16,8 @@ class Resource : public Reference { public: int id; - virtual std::string to_json(rapidjson::Document *into = nullptr); - virtual void from_json(const std::string &data); + virtual String to_json(rapidjson::Document *into = nullptr); + virtual void from_json(const String &data); Resource(); ~Resource(); diff --git a/core/settings.cpp b/core/settings.cpp index 89c8132..422ce0d 100644 --- a/core/settings.cpp +++ b/core/settings.cpp @@ -3,7 +3,7 @@ #include #include -void Settings::parse_file(const std::string &path) { +void Settings::parse_file(const String &path) { FILE *f = fopen(path.c_str(), "r"); if (!f) { @@ -15,10 +15,10 @@ void Settings::parse_file(const std::string &path) { long fsize = ftell(f); fseek(f, 0, SEEK_SET); /* same as rewind(f); */ - std::string config_str; + String config_str; config_str.resize(fsize); - fread(&config_str[0], 1, fsize, f); + fread(config_str.dataw(), 1, fsize, f); fclose(f); settings.Parse(config_str.c_str()); diff --git a/core/settings.h b/core/settings.h index e941119..cd8da58 100644 --- a/core/settings.h +++ b/core/settings.h @@ -1,7 +1,7 @@ #ifndef SETTINGS_H #define SETTINGS_H -#include +#include "core/string.h" #include "rapidjson/document.h" @@ -9,7 +9,7 @@ class Settings { public: rapidjson::Document settings; - void parse_file(const std::string &path); + void parse_file(const String &path); static Settings *get_singleton(); diff --git a/core/string.cpp b/core/string.cpp index 83669d7..c527e93 100644 --- a/core/string.cpp +++ b/core/string.cpp @@ -115,8 +115,8 @@ void String::resize(const int s) { _data[_size] = '\0'; } -int String::find(const char val) const { - for (int i = 0; i < _size; ++i) { +int String::find(const char val, const int from) const { + for (int i = from; i < _size; ++i) { if (_data[i] == val) { return i; } @@ -125,6 +125,26 @@ int String::find(const char val) const { return -1; } +int String::find(const String &val, const int from) const { + int ve = _size - val.size(); + + for (int i = from; i < ve; ++i) { + bool found = true; + for (int j = 0; j < val.size(); ++j) { + if (_data[i + j] != val[j]) { + found = false; + break; + } + } + + if (found) { + return i; + } + } + + return -1; +} + void String::get_substr(char *into_buf, const int start_index, const int len) { ERR_FAIL_INDEX(start_index + len - 1, _size); @@ -150,7 +170,7 @@ String String::substr(const int start_index, const int len) { int sil = start_index + len; - ERR_FAIL_INDEX_V(sil, _size, String()); + ERR_FAIL_INDEX_V(sil, _size + 1, String()); String str; str.ensure_capacity(len + 1); @@ -163,6 +183,54 @@ String String::substr(const int start_index, const int len) { return str; } +void String::replace_from(const int start_index, const int length, const String &with) { + ERR_FAIL_INDEX(start_index, _size); + + int sil = start_index + length; + + ERR_FAIL_INDEX(sil, _size + 1); + + if (length < with.size()) { + int loffs = with.size() - length; + + ensure_capacity(_size + loffs + 1); + + _size += loffs; + _data[_size] = '\0'; + + for (int i = _size - 1; i > start_index + loffs; --i) { + _data[i] = _data[i - loffs]; + } + } else if (length > with.size()) { + int loffs = length - with.size(); + + for (int i = start_index + with.size(); i < _size; ++i) { + _data[i] = _data[i + loffs]; + } + + _size -= loffs; + } + + for (int i = 0; i < length; ++i) { + _data[i + start_index] = with._data[i]; + } +} + +void String::replace(const String &find_str, const String &with) { + if (empty()) { + return; + } + + if (find_str.empty()) + return; + + int start_pos = 0; + while ((start_pos = find(find_str, start_pos)) != -1) { + replace_from(start_pos, find_str.size(), with); + start_pos += with.size(); + } +} + int String::compare(const String &other) const { if (size() < other.size()) { return 1; diff --git a/core/string.h b/core/string.h index cda9cc1..08ff49d 100644 --- a/core/string.h +++ b/core/string.h @@ -22,11 +22,15 @@ public: int capacity() const; void ensure_capacity(const int capacity); void resize(const int s); - int find(const char val) const; + int find(const char val, const int from = 0) const; + int find(const String &val, const int from = 0) const; void get_substr(char *into_buf, const int start_index, const int len); void get_substr_nt(char *into_buf, const int start_index, const int len); String substr(const int start_index, const int len); + void replace_from(const int start_index, const int length, const String &with); + void replace(const String &find_str, const String &with); + int compare(const String &other) const; uint8_t read_uint8_bytes_at(int &index, bool advance_index = true); diff --git a/core/utils.cpp b/core/utils.cpp index 463dccb..03067be 100644 --- a/core/utils.cpp +++ b/core/utils.cpp @@ -6,12 +6,12 @@ #include #include -void Utils::newline_to_br(std::string *str) { - str_replace(str, "\r\n", "
"); - str_replace(str, "\n", "
"); +void Utils::newline_to_br(String *str) { + str->replace("\r\n", "
"); + str->replace("\n", "
"); } -void Utils::markdown_to_html(std::string *str) { +void Utils::markdown_to_html(String *str) { std::shared_ptr config = std::make_shared(); config->isEmphasizedParserEnabled = false; config->isHTMLWrappedInParagraph = true; @@ -20,13 +20,13 @@ void Utils::markdown_to_html(std::string *str) { std::stringstream ss((*str)); - std::string htmlOutput = parser->Parse(ss); + String htmlOutput = parser->Parse(ss); (*str) = htmlOutput; } -std::string Utils::get_pagination(const std::string base_url, const uint32_t max, const uint32_t current_index, const uint32_t max_visible_links) { - std::string s = base_url; +String Utils::get_pagination(const String base_url, const uint32_t max, const uint32_t current_index, const uint32_t max_visible_links) { + String s = base_url; if (s.size() > 0 && s[s.size() - 1] != '/') { s += '/'; } @@ -103,8 +103,8 @@ std::string Utils::get_pagination(const std::string base_url, const uint32_t max return b.result; } -std::string Utils::get_pagination_links(const std::string base_url, const std::vector &links, const uint32_t current_index, const uint32_t max_visible_links) { - std::string s = base_url; +String Utils::get_pagination_links(const String base_url, const Vector &links, const uint32_t current_index, const uint32_t max_visible_links) { + String s = base_url; if (s.size() > 0 && s[s.size() - 1] != '/') { s += '/'; } @@ -183,13 +183,3 @@ std::string Utils::get_pagination_links(const std::string base_url, const std::v return b.result; } -void Utils::str_replace(std::string *str, const std::string &from, const std::string &to) { - if (from.empty()) - return; - - size_t start_pos = 0; - while ((start_pos = str->find(from, start_pos)) != std::string::npos) { - str->replace(start_pos, from.length(), to); - start_pos += to.length(); - } -} diff --git a/core/utils.h b/core/utils.h index 2971ec2..73eeefa 100644 --- a/core/utils.h +++ b/core/utils.h @@ -1,20 +1,18 @@ #ifndef UTILS_H #define UTILS_H -#include -#include +#include "core/string.h" +#include "core/containers/vector.h" class Utils { public: - static void newline_to_br(std::string *str); + static void newline_to_br(String *str); //htmlspecialchars - static void markdown_to_html(std::string *str); + static void markdown_to_html(String *str); - static std::string get_pagination(const std::string base_url, const uint32_t max, const uint32_t current_index, const uint32_t max_visible_links = 10); - static std::string get_pagination_links(const std::string base_url, const std::vector &links, const uint32_t current_index, const uint32_t max_visible_links = 10); - - static void str_replace(std::string *str, const std::string &from, const std::string &to); + static String get_pagination(const String base_url, const uint32_t max, const uint32_t current_index, const uint32_t max_visible_links = 10); + static String get_pagination_links(const String base_url, const Vector &links, const uint32_t current_index, const uint32_t max_visible_links = 10); protected: }; diff --git a/modules/list_page/list_page.cpp b/modules/list_page/list_page.cpp index fec36ba..3be2927 100644 --- a/modules/list_page/list_page.cpp +++ b/modules/list_page/list_page.cpp @@ -9,7 +9,7 @@ #include void ListPage::index(Request *request) { - std::string r = ""; + String r = ""; for (uint32_t i = 0; i < list_entries.size(); ++i) { r += "
" + list_entries[i] + "
"; @@ -26,7 +26,7 @@ void ListPage::load() { return; } - std::vector files; + std::vector files; tinydir_dir dir; if (tinydir_open(&dir, folder.c_str()) == -1) { @@ -42,7 +42,7 @@ void ListPage::load() { } if (!file.is_dir) { - std::string np = file.path; + String np = file.path; files.push_back(np); } @@ -52,7 +52,8 @@ void ListPage::load() { tinydir_close(&dir); - std::sort(files.begin(), files.end()); + //todo + //std::sort(files.begin(), files.end()); for (uint32_t i = 0; i < files.size(); ++i) { FILE *f = fopen(files[i].c_str(), "r"); @@ -66,7 +67,7 @@ void ListPage::load() { long fsize = ftell(f); fseek(f, 0, SEEK_SET); /* same as rewind(f); */ - std::string fd; + String fd; fd.resize(fsize); fread(&fd[0], 1, fsize, f); diff --git a/modules/list_page/list_page.h b/modules/list_page/list_page.h index 6d9b7a6..250ff03 100644 --- a/modules/list_page/list_page.h +++ b/modules/list_page/list_page.h @@ -1,8 +1,8 @@ #ifndef LIST_PAGE_H #define LIST_PAGE_H -#include -#include +#include "core/containers/vector.h" +#include "core/string.h" #include "core/object.h" @@ -20,8 +20,8 @@ public: ListPage(); ~ListPage(); - std::vector list_entries; - std::string folder; + Vector list_entries; + String folder; }; #endif \ No newline at end of file diff --git a/modules/message_page/message_page.cpp b/modules/message_page/message_page.cpp index 3c08712..da1f3ef 100644 --- a/modules/message_page/message_page.cpp +++ b/modules/message_page/message_page.cpp @@ -13,13 +13,13 @@ void MessagePage::index(Request *request) { Ref res = db->query(b->query_result); - std::vector msgs; + Vector msgs; while (res->next_row()) { msgs.push_back(res->get_cell(0)); } - std::string r = ""; + String r = ""; for (uint32_t i = 0; i < messages.size(); ++i) { r += "

" + messages[i] + "


"; diff --git a/modules/message_page/message_page.h b/modules/message_page/message_page.h index 4d884af..bf30ed9 100644 --- a/modules/message_page/message_page.h +++ b/modules/message_page/message_page.h @@ -1,8 +1,8 @@ #ifndef MESSAGE_PAGE_H #define MESSAGE_PAGE_H -#include -#include +#include "core/containers/vector.h" +#include "core/string.h" #include "core/object.h" @@ -20,7 +20,7 @@ public: MessagePage(); ~MessagePage(); - std::vector messages; + Vector messages; }; #endif \ No newline at end of file diff --git a/modules/paged_article/paged_article.cpp b/modules/paged_article/paged_article.cpp index cc60fb1..55cedf7 100644 --- a/modules/paged_article/paged_article.cpp +++ b/modules/paged_article/paged_article.cpp @@ -11,7 +11,7 @@ #include "core/http/web_application.h" void PagedArticle::index(Request *request) { - const std::string r = request->get_current_path_segment(); + const String r = request->get_current_path_segment(); Article *s = pages[r]; @@ -22,13 +22,13 @@ void PagedArticle::index(Request *request) { request->push_path(); - const std::string rp = request->get_current_path_segment(); + const String rp = request->get_current_path_segment(); if (request->get_remaining_segment_count() > 1 && rp == "files") { - std::string file_name = "/" + request->get_path_segment(request->get_current_segment_index() + 1); + String file_name = "/" + request->get_path_segment(request->get_current_segment_index() + 1); if (s->file_cache->wwwroot_has_file(file_name)) { - std::string fp = s->file_cache->wwwroot + file_name; + String fp = s->file_cache->wwwroot + file_name; request->send_file(fp); return; @@ -44,7 +44,7 @@ void PagedArticle::index(Request *request) { return; } - const std::string *page = s->pages[rp]; + const String *page = s->pages[rp]; if (page == nullptr) { //bad url @@ -90,17 +90,17 @@ void PagedArticle::load() { continue; } - std::string np = file.path; - std::string fn = file.name; + String np = file.path; + String fn = file.name; - std::string ff = folder + "/" + fn; - std::string wp = base_path + "/" + fn; + String ff = folder + "/" + fn; + String wp = base_path + "/" + fn; Article *a = load_folder(np, wp); if (a) { - std::string p = file.name; + String p = file.name; a->url = p; pages[p] = a; @@ -118,10 +118,10 @@ void PagedArticle::load() { generate_summaries(); } -Article *PagedArticle::load_folder(const std::string &folder, const std::string &path) { +Article *PagedArticle::load_folder(const String &folder, const String &path) { printf("PagedArticle: loading: %s\n", folder.c_str()); - std::vector files; + Vector files; tinydir_dir dir; if (tinydir_open(&dir, folder.c_str()) == -1) { @@ -137,7 +137,7 @@ Article *PagedArticle::load_folder(const std::string &folder, const std::string } if (!file.is_dir) { - std::string np = file.name; + String np = file.name; files.push_back(np); } @@ -151,12 +151,13 @@ Article *PagedArticle::load_folder(const std::string &folder, const std::string return nullptr; } - std::sort(files.begin(), files.end()); + //todo + //std::sort(files.begin(), files.end()); Article *article = new Article(); for (uint32_t i = 0; i < files.size(); ++i) { - std::string file_path = folder; + String file_path = folder; if (file_path.size() > 0 && file_path[file_path.size() - 1] != '/') file_path += "/"; @@ -174,7 +175,7 @@ Article *PagedArticle::load_folder(const std::string &folder, const std::string long fsize = ftell(f); fseek(f, 0, SEEK_SET); /* same as rewind(f); */ - std::string fd; + String fd; fd.resize(fsize); fread(&fd[0], 1, fsize, f); @@ -182,11 +183,11 @@ Article *PagedArticle::load_folder(const std::string &folder, const std::string Utils::markdown_to_html(&fd); - std::string pagination; + String pagination; pagination = Utils::get_pagination_links(path, files, i); - std::string *finals = new std::string(); + String *finals = new String(); (*finals) += pagination; (*finals) += fd; @@ -203,7 +204,7 @@ Article *PagedArticle::load_folder(const std::string &folder, const std::string } void PagedArticle::generate_summaries() { - for (std::map::iterator it = pages.begin(); it != pages.end(); ++it) { + for (std::map::iterator it = pages.begin(); it != pages.end(); ++it) { generate_summary((*it).second); } } @@ -213,8 +214,8 @@ void PagedArticle::generate_summary(Article *article) { return; } - for (std::map::iterator it = article->pages.begin(); it != article->pages.end(); ++it) { - std::string *s = (*it).second; + for (std::map::iterator it = article->pages.begin(); it != article->pages.end(); ++it) { + String *s = (*it).second; if (s != nullptr) { article->summary_page = (*s); diff --git a/modules/paged_article/paged_article.h b/modules/paged_article/paged_article.h index cdfef23..dab5a99 100644 --- a/modules/paged_article/paged_article.h +++ b/modules/paged_article/paged_article.h @@ -2,8 +2,8 @@ #define PAGED_ARTICLE_H #include -#include -#include +#include "core/containers/vector.h" +#include "core/string.h" #include "core/file_cache.h" #include "core/object.h" @@ -11,16 +11,16 @@ #include "core/http/request.h" struct Article { - std::string url; - std::string summary_page; - std::map pages; + String url; + String summary_page; + std::map pages; FileCache *file_cache; Article() { file_cache = new FileCache(); } ~Article() { - for (std::map::iterator it = pages.begin(); it != pages.end(); ++it) { + for (std::map::iterator it = pages.begin(); it != pages.end(); ++it) { delete ((*it).second); } @@ -37,16 +37,16 @@ public: void index(Request *request); void load(); - Article *load_folder(const std::string &folder, const std::string &path); + Article *load_folder(const String &folder, const String &path); void generate_summaries(); void generate_summary(Article *article); PagedArticle(); ~PagedArticle(); - std::map pages; - std::string folder; - std::string base_path; + std::map pages; + String folder; + String base_path; }; #endif \ No newline at end of file diff --git a/modules/paged_list/paged_list.cpp b/modules/paged_list/paged_list.cpp index c3c3472..6af49c7 100644 --- a/modules/paged_list/paged_list.cpp +++ b/modules/paged_list/paged_list.cpp @@ -13,7 +13,7 @@ #include "core/html/html_builder.h" void PagedList::index(Request *request) { - const std::string path = request->get_current_path_segment(); + const String path = request->get_current_path_segment(); if (request->get_remaining_segment_count() == 0) { main_page->index(request); diff --git a/modules/paged_list/paged_list.h b/modules/paged_list/paged_list.h index 481f54a..0b2f760 100644 --- a/modules/paged_list/paged_list.h +++ b/modules/paged_list/paged_list.h @@ -3,7 +3,7 @@ #include "core/object.h" -#include +#include "core/string.h" #include "modules/list_page/list_page.h" #include "modules/paged_article/paged_article.h" @@ -19,8 +19,8 @@ public: ListPage *main_page; PagedArticle *articles; - std::string folder; - std::string base_path; + String folder; + String base_path; }; #endif \ No newline at end of file diff --git a/modules/users/user.cpp b/modules/users/user.cpp index 2b6b54d..9836174 100644 --- a/modules/users/user.cpp +++ b/modules/users/user.cpp @@ -22,7 +22,7 @@ #include "core/utils.h" #include "user_model.h" -std::string User::to_json(rapidjson::Document *into) { +String User::to_json(rapidjson::Document *into) { rapidjson::Document *document; if (into) { @@ -53,14 +53,14 @@ std::string User::to_json(rapidjson::Document *into) { rapidjson::Writer writer(buffer); document->Accept(writer); - std::string s = buffer.GetString(); + String s = buffer.GetString(); delete document; return s; } -void User::from_json(const std::string &p_data) { +void User::from_json(const String &p_data) { rapidjson::Document data; data.Parse(p_data.c_str()); diff --git a/modules/users/user.h b/modules/users/user.h index 5c11e2d..43cc970 100644 --- a/modules/users/user.h +++ b/modules/users/user.h @@ -1,10 +1,11 @@ #ifndef USER_H #define USER_H +#include "core/string.h" + #include "core/resource.h" #include -#include class Request; class FormValidator; @@ -13,18 +14,18 @@ class User : public Resource { RCPP_OBJECT(User, Resource); public: - std::string name_user_input; - std::string email_user_input; + String name_user_input; + String email_user_input; int rank; - std::string pre_salt; - std::string post_salt; - std::string password_hash; + String pre_salt; + String post_salt; + String password_hash; bool banned; - std::string password_reset_token; + String password_reset_token; bool locked; - std::string to_json(rapidjson::Document *into = nullptr); - void from_json(const std::string &data); + String to_json(rapidjson::Document *into = nullptr); + void from_json(const String &data); User(); ~User();