Also cleaned up and added my BBCodeParser to the build.

This commit is contained in:
Relintai 2022-07-03 18:40:05 +02:00
parent 29480135c1
commit f3a290e005
3 changed files with 45 additions and 46 deletions

View File

@ -26,6 +26,7 @@ sources = [
"html/html_builder.cpp", "html/html_builder.cpp",
"html/paginator.cpp", "html/paginator.cpp",
"html/html_parser.cpp", "html/html_parser.cpp",
"html/bbcode_parser.cpp",
"http_server_simple/http_server_simple.cpp", "http_server_simple/http_server_simple.cpp",
"http_server_simple/web_server_simple.cpp", "http_server_simple/web_server_simple.cpp",

View File

@ -1,4 +1,5 @@
#include "bbcode_parser.h" #include "bbcode_parser.h"
#include "core/error_macros.h" #include "core/error_macros.h"
bool BBCodeParserAttribute::match_attrib(const String &attrib) { bool BBCodeParserAttribute::match_attrib(const String &attrib) {
@ -20,7 +21,7 @@ String BBCodeParserAttribute::to_string() {
return attribute; return attribute;
} }
if (data.find('"' == -1)) { if (data.find_char('"' == -1)) {
return attribute + "=\"" + data + "\""; return attribute + "=\"" + data + "\"";
} else { } else {
return attribute + "=\'" + data + "\'"; return attribute + "=\'" + data + "\'";
@ -28,7 +29,7 @@ String BBCodeParserAttribute::to_string() {
} }
void BBCodeParserAttribute::print() { void BBCodeParserAttribute::print() {
to_string().print(); ERR_PRINT(to_string());
} }
BBCodeParserAttribute::BBCodeParserAttribute() { BBCodeParserAttribute::BBCodeParserAttribute() {
@ -163,12 +164,12 @@ void BBCodeParserTag::process() {
tag_text = data.substr(1, data.size() - 2); tag_text = data.substr(1, data.size() - 2);
} }
int fspc_index = tag_text.find(' '); int fspc_index = tag_text.find_char(' ');
if (fspc_index == -1) { if (fspc_index == -1) {
// no args // no args
int feq_ind = tag_text.find('='); int feq_ind = tag_text.find_char('=');
if (feq_ind == -1) { if (feq_ind == -1) {
tag = tag_text; tag = tag_text;
return; return;
@ -196,7 +197,7 @@ void BBCodeParserTag::process() {
parse_args(args); parse_args(args);
} }
int tag_end_index = data.find(' ', start_index); int tag_end_index = data.find_char(' ', start_index);
if (tag_end_index == -1) { if (tag_end_index == -1) {
// simple tag // simple tag
@ -216,9 +217,9 @@ void BBCodeParserTag::parse_args(const String &args) {
continue; continue;
} }
int equals_index = args.find('=', i); int equals_index = args.find_char('=', i);
BBCodeParserAttribute *a = new BBCodeParserAttribute(); BBCodeParserAttribute *a = memnew(BBCodeParserAttribute);
if (equals_index == -1) { if (equals_index == -1) {
a->attribute = args.substr(i, args.size() - i); a->attribute = args.substr(i, args.size() - i);
@ -260,7 +261,7 @@ void BBCodeParserTag::parse_args(const String &args) {
find_char = c; find_char = c;
} }
int end_index = args.find(find_char, next_char_index); int end_index = args.find_char(find_char, next_char_index);
if (end_index == -1) { if (end_index == -1) {
// missing closing ' or " if c is ' or " // missing closing ' or " if c is ' or "
@ -281,13 +282,13 @@ void BBCodeParserTag::parse_args(const String &args) {
String BBCodeParserTag::to_string(const int level) { String BBCodeParserTag::to_string(const int level) {
String s; String s;
s.append_repeat(" ", level); s += String(" ").repeat(level);
if (type == BBCODE_PARSER_TAG_TYPE_CONTENT) { if (type == BBCODE_PARSER_TAG_TYPE_CONTENT) {
s += data + "\n"; s += data + "\n";
if (tags.size() != 0) { if (tags.size() != 0) {
s.append_repeat(" ", level); s += String(" ").repeat(level);
s += "(!CONTENT TAG HAS TAGS!)\n"; s += "(!CONTENT TAG HAS TAGS!)\n";
for (int i = 0; i < tags.size(); ++i) { for (int i = 0; i < tags.size(); ++i) {
@ -309,7 +310,7 @@ String BBCodeParserTag::to_string(const int level) {
s += tags[i]->to_string(ln); s += tags[i]->to_string(ln);
} }
s.append_repeat(" ", level); s += String(" ").repeat(level);
s += "[/" + tag + "]\n"; s += "[/" + tag + "]\n";
} else if (type == BBCODE_PARSER_TAG_TYPE_CLOSING_TAG) { } else if (type == BBCODE_PARSER_TAG_TYPE_CLOSING_TAG) {
@ -318,7 +319,7 @@ String BBCodeParserTag::to_string(const int level) {
s += "[/" + tag + "(!)]"; s += "[/" + tag + "(!)]";
if (tags.size() != 0) { if (tags.size() != 0) {
s.append_repeat(" ", level); s += String(" ").repeat(level);
s += "(!CLOSING TAG HAS TAGS!)\n"; s += "(!CLOSING TAG HAS TAGS!)\n";
for (int i = 0; i < tags.size(); ++i) { for (int i = 0; i < tags.size(); ++i) {
@ -335,7 +336,7 @@ String BBCodeParserTag::to_string(const int level) {
s += "/]\n"; s += "/]\n";
if (tags.size() != 0) { if (tags.size() != 0) {
s.append_repeat(" ", level); s += String(" ").repeat(level);
for (int i = 0; i < tags.size(); ++i) { for (int i = 0; i < tags.size(); ++i) {
s += tags[i]->to_string(level + 1) + "\n"; s += tags[i]->to_string(level + 1) + "\n";
@ -344,14 +345,14 @@ String BBCodeParserTag::to_string(const int level) {
} else if (type == BBCODE_PARSER_TAG_TYPE_NONE) { } else if (type == BBCODE_PARSER_TAG_TYPE_NONE) {
for (int i = 0; i < tags.size(); ++i) { for (int i = 0; i < tags.size(); ++i) {
s += tags[i]->to_string(level) + "\n"; s += tags[i]->to_string(level) + "\n";
s.append_repeat(" ", level); s += String(" ").repeat(level);
} }
} }
return s; return s;
} }
void BBCodeParserTag::print() { void BBCodeParserTag::print() {
to_string().print(); ERR_PRINT(to_string());
} }
BBCodeParserTag::BBCodeParserTag() { BBCodeParserTag::BBCodeParserTag() {
@ -360,11 +361,11 @@ BBCodeParserTag::BBCodeParserTag() {
BBCodeParserTag::~BBCodeParserTag() { BBCodeParserTag::~BBCodeParserTag() {
for (int i = 0; i < tags.size(); ++i) { for (int i = 0; i < tags.size(); ++i) {
delete tags[i]; memdelete(tags[i]);
} }
for (int i = 0; i < attributes.size(); ++i) { for (int i = 0; i < attributes.size(); ++i) {
delete attributes[i]; memdelete(attributes[i]);
} }
} }
@ -377,7 +378,7 @@ void BBCodeParser::parse(const String &data) {
// tag // tag
for (int j = i + 1; j < data.size(); ++j) { for (int j = i + 1; j < data.size(); ++j) {
if (data[j] == ']') { if (data[j] == ']') {
BBCodeParserTag *t = new BBCodeParserTag(); BBCodeParserTag *t = memnew(BBCodeParserTag);
t->data = data.substr(i, j - i + 1); t->data = data.substr(i, j - i + 1);
t->process(); t->process();
@ -393,7 +394,7 @@ void BBCodeParser::parse(const String &data) {
for (int j = i + 1; j < data.size(); ++j) { for (int j = i + 1; j < data.size(); ++j) {
if (data[j] == '[') { if (data[j] == '[') {
BBCodeParserTag *t = new BBCodeParserTag(); BBCodeParserTag *t = memnew(BBCodeParserTag);
t->data = data.substr(i, j - i); t->data = data.substr(i, j - i);
t->type = BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_CONTENT; t->type = BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_CONTENT;
@ -408,30 +409,27 @@ void BBCodeParser::parse(const String &data) {
} }
if (root) { if (root) {
delete root; memdelete(root);
} }
root = new BBCodeParserTag(); root = memnew(BBCodeParserTag);
// process tags into hierarchical order // process tags into hierarchical order
Vector<BBCodeParserTag *> tag_stack; Vector<BBCodeParserTag *> tag_stack;
for (int i = 0; i < tags.size(); ++i) { for (int i = 0; i < tags.size(); ++i) {
BBCodeParserTag *t = tags[i]; BBCodeParserTag *t = tags[i];
if (t == nullptr) { ERR_CONTINUE_MSG(t == nullptr, "BBCodeParser::parse: t == nullptr!");
RLOG_ERR("BBCodeParser::parse: t == nullptr!");
continue;
}
if (t->type == BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_NONE) { if (t->type == BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_NONE) {
RLOG_ERR("BBCodeParser::parse: t->type == BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_NONE!"); ERR_PRINT("BBCodeParser::parse: t->type == BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_NONE!");
delete t; memdelete(t);
tags[i] = nullptr; tags.write[i] = nullptr;
continue; continue;
} else if (t->type == BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_OPENING_TAG) { } else if (t->type == BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_OPENING_TAG) {
tag_stack.push_back(t); tag_stack.push_back(t);
tags[i] = nullptr; tags.write[i] = nullptr;
continue; continue;
} else if (t->type == BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_SELF_CLOSING_TAG) { } else if (t->type == BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_SELF_CLOSING_TAG) {
if (tag_stack.size() == 0) { if (tag_stack.size() == 0) {
@ -440,7 +438,7 @@ void BBCodeParser::parse(const String &data) {
tag_stack[tag_stack.size() - 1]->tags.push_back(t); tag_stack[tag_stack.size() - 1]->tags.push_back(t);
} }
tags[i] = nullptr; tags.write[i] = nullptr;
continue; continue;
} else if (t->type == BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_CONTENT) { } else if (t->type == BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_CONTENT) {
if (tag_stack.size() == 0) { if (tag_stack.size() == 0) {
@ -449,12 +447,12 @@ void BBCodeParser::parse(const String &data) {
tag_stack[tag_stack.size() - 1]->tags.push_back(t); tag_stack[tag_stack.size() - 1]->tags.push_back(t);
} }
tags[i] = nullptr; tags.write[i] = nullptr;
continue; continue;
} else if (t->type == BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_CLOSING_TAG) { } else if (t->type == BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_CLOSING_TAG) {
if (tag_stack.size() == 0) { if (tag_stack.size() == 0) {
delete t; memdelete(t);
tags[i] = nullptr; tags.write[i] = nullptr;
// ill-formed html // ill-formed html
continue; continue;
@ -492,8 +490,8 @@ void BBCodeParser::parse(const String &data) {
tag_stack[tag_stack.size() - 1]->tags.push_back(opening_tag); tag_stack[tag_stack.size() - 1]->tags.push_back(opening_tag);
} }
delete t; memdelete(t);
tags[i] = nullptr; tags.write[i] = nullptr;
continue; continue;
} }
@ -508,10 +506,10 @@ void BBCodeParser::parse(const String &data) {
BBCodeParserTag *t = tags[i]; BBCodeParserTag *t = tags[i];
if (t != nullptr) { if (t != nullptr) {
RLOG_ERR("BBCodeParser::parse(const String &data): tag was not processed!\n"); ERR_PRINT("BBCodeParser::parse(const String &data): tag was not processed!\n");
t->print(); t->print();
delete t; memdelete(t);
} }
} }
} }
@ -535,6 +533,6 @@ BBCodeParser::BBCodeParser() {
BBCodeParser::~BBCodeParser() { BBCodeParser::~BBCodeParser() {
if (root) { if (root) {
delete root; memdelete(root);
} }
} }

View File

@ -1,8 +1,8 @@
#ifndef BBCODE_PARSER_H #ifndef BBCODE_PARSER_H
#define BBCODE_PARSER_H #define BBCODE_PARSER_H
#include "core/containers/vector.h" #include "core/ustring.h"
#include "core/string.h" #include "core/vector.h"
class BBCodeParserAttribute { class BBCodeParserAttribute {
public: public: