Did most of the remaining std::string and vector to String and Vector conversions.

This commit is contained in:
Relintai 2021-11-01 19:53:35 +01:00
parent 3f190e9a53
commit f7b5952d2a
20 changed files with 183 additions and 119 deletions

View File

@ -4,15 +4,15 @@
#include <iostream>
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];

View File

@ -1,7 +1,8 @@
#ifndef FILE_CACHE_H
#define FILE_CACHE_H
#include <string>
#include "core/string.h"
#include <set>
#include <map>
#include <chrono>
@ -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<std::string> registered_files;
std::set<String> 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<std::string, CacheEntry *> cache_map;
std::map<String, CacheEntry *> cache_map;
private:
static FileCache *_instance;

View File

@ -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() :

View File

@ -1,7 +1,7 @@
#ifndef RESOURCE_H
#define RESOURCE_H
#include <string>
#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();

View File

@ -3,7 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
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());

View File

@ -1,7 +1,7 @@
#ifndef SETTINGS_H
#define SETTINGS_H
#include <string>
#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();

View File

@ -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;

View File

@ -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);

View File

@ -6,12 +6,12 @@
#include <sstream>
#include <string>
void Utils::newline_to_br(std::string *str) {
str_replace(str, "\r\n", "<br>");
str_replace(str, "\n", "<br>");
void Utils::newline_to_br(String *str) {
str->replace("\r\n", "<br>");
str->replace("\n", "<br>");
}
void Utils::markdown_to_html(std::string *str) {
void Utils::markdown_to_html(String *str) {
std::shared_ptr<maddy::ParserConfig> config = std::make_shared<maddy::ParserConfig>();
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<std::string> &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<String> &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();
}
}

View File

@ -1,20 +1,18 @@
#ifndef UTILS_H
#define UTILS_H
#include <string>
#include <vector>
#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<std::string> &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<String> &links, const uint32_t current_index, const uint32_t max_visible_links = 10);
protected:
};

View File

@ -9,7 +9,7 @@
#include <iostream>
void ListPage::index(Request *request) {
std::string r = "";
String r = "";
for (uint32_t i = 0; i < list_entries.size(); ++i) {
r += "<div class=\"list_entry\">" + list_entries[i] + "</div>";
@ -26,7 +26,7 @@ void ListPage::load() {
return;
}
std::vector<std::string> files;
std::vector<String> 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);

View File

@ -1,8 +1,8 @@
#ifndef LIST_PAGE_H
#define LIST_PAGE_H
#include <vector>
#include <string>
#include "core/containers/vector.h"
#include "core/string.h"
#include "core/object.h"
@ -20,8 +20,8 @@ public:
ListPage();
~ListPage();
std::vector<std::string> list_entries;
std::string folder;
Vector<String> list_entries;
String folder;
};
#endif

View File

@ -13,13 +13,13 @@ void MessagePage::index(Request *request) {
Ref<QueryResult> res = db->query(b->query_result);
std::vector<std::string> msgs;
Vector<String> msgs;
while (res->next_row()) {
msgs.push_back(res->get_cell(0));
}
std::string r = "<html><body>";
String r = "<html><body>";
for (uint32_t i = 0; i < messages.size(); ++i) {
r += "<p>" + messages[i] + "</p><br>";

View File

@ -1,8 +1,8 @@
#ifndef MESSAGE_PAGE_H
#define MESSAGE_PAGE_H
#include <vector>
#include <string>
#include "core/containers/vector.h"
#include "core/string.h"
#include "core/object.h"
@ -20,7 +20,7 @@ public:
MessagePage();
~MessagePage();
std::vector<std::string> messages;
Vector<String> messages;
};
#endif

View File

@ -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<std::string> files;
Vector<String> 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<std::string, Article *>::iterator it = pages.begin(); it != pages.end(); ++it) {
for (std::map<String, Article *>::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<std::string, std::string *>::iterator it = article->pages.begin(); it != article->pages.end(); ++it) {
std::string *s = (*it).second;
for (std::map<String, String *>::iterator it = article->pages.begin(); it != article->pages.end(); ++it) {
String *s = (*it).second;
if (s != nullptr) {
article->summary_page = (*s);

View File

@ -2,8 +2,8 @@
#define PAGED_ARTICLE_H
#include <map>
#include <string>
#include <vector>
#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<std::string, std::string *> pages;
String url;
String summary_page;
std::map<String, String *> pages;
FileCache *file_cache;
Article() {
file_cache = new FileCache();
}
~Article() {
for (std::map<std::string, std::string *>::iterator it = pages.begin(); it != pages.end(); ++it) {
for (std::map<String, String *>::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<std::string, Article *> pages;
std::string folder;
std::string base_path;
std::map<String, Article *> pages;
String folder;
String base_path;
};
#endif

View File

@ -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);

View File

@ -3,7 +3,7 @@
#include "core/object.h"
#include <string>
#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

View File

@ -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<rapidjson::StringBuffer> 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());

View File

@ -1,10 +1,11 @@
#ifndef USER_H
#define USER_H
#include "core/string.h"
#include "core/resource.h"
#include <mutex>
#include <string>
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();