From 6a01c57c362c49b9e8d06a1f55a9540abbe65acb Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 26 Dec 2020 17:49:59 +0100 Subject: [PATCH] Added a new Utils class containing helper methods. --- core/utils.cpp | 127 +++++++++++++++++++++++++++++++++++++++++++++++++ core/utils.h | 23 +++++++++ 2 files changed, 150 insertions(+) create mode 100644 core/utils.cpp create mode 100644 core/utils.h diff --git a/core/utils.cpp b/core/utils.cpp new file mode 100644 index 0000000..cda394d --- /dev/null +++ b/core/utils.cpp @@ -0,0 +1,127 @@ +#include "utils.h" + +void Utils::newline_to_br(std::string *str) { + str_replace(str, "\r\n", "
"); + str_replace(str, "\n", "
"); +} + +void Utils::bbcode_evaluate_simple(std::string *str) { + bbcpp::BBDocumentPtr doc = bbcode((*str)); + + bbcpp::BBNodeList n = doc->getChildren(); + + str->clear(); + + for (uint32_t i = 0; i < n.size(); ++i) { + eval_node(str, n[i]); + } +} + +bbcpp::BBDocumentPtr Utils::bbcode(const std::string &str) { + bbcpp::BBDocumentPtr doc = bbcpp::BBDocument::create(); + + doc->load(str); + + return doc; +} + +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(); + } +} + +void Utils::eval_node(std::string *str, bbcpp::BBNodePtr node) { + switch (node->getNodeType()) { + case bbcpp::BBNode::NodeType::TEXT: { + bbcpp::BBTextPtr t = node->downCast(); + + (*str) += t->getText(); + break; + } + case bbcpp::BBNode::NodeType::ELEMENT: { + bbcpp::BBElementPtr e = node->downCast(); + + eval_element(str, e); + + break; + } + case bbcpp::BBNode::NodeType::ATTRIBUTE: + break; + case bbcpp::BBNode::NodeType::DOCUMENT: + break; + } + + bbcpp::BBNodeList n = node->getChildren(); + + for (uint32_t i = 0; i < n.size(); ++i) { + eval_node(str, n[i]); + } +} + +void Utils::eval_element(std::string *str, bbcpp::BBElementPtr element) { + switch (element->getElementType()) { + case bbcpp::BBElement::ElementType::SIMPLE: { + + if (element->getNodeName() == "b") { + (*str) += ""; + return; + } + + if (element->getNodeName() == "i") { + (*str) += ""; + return; + } + + if (element->getNodeName() == "u") { + (*str) += ""; + return; + } + + if (element->getNodeName() == "s") { + (*str) += ""; + return; + } + + + break; + } + case bbcpp::BBElement::ElementType::VALUE: { + //NYI + break; + } + case bbcpp::BBElement::ElementType::PARAMETER: { + break; + } + case bbcpp::BBElement::ElementType::CLOSING: { + if (element->getNodeName() == "b") { + (*str) += ""; + return; + } + + if (element->getNodeName() == "i") { + (*str) += ""; + return; + } + + if (element->getNodeName() == "u") { + (*str) += ""; + return; + } + + if (element->getNodeName() == "s") { + (*str) += ""; + return; + } + + + + break; + } + } +} \ No newline at end of file diff --git a/core/utils.h b/core/utils.h new file mode 100644 index 0000000..35b853c --- /dev/null +++ b/core/utils.h @@ -0,0 +1,23 @@ +#ifndef UTILS_H +#define UTILS_H + +#include + +#include + +class Utils { + public: + static void newline_to_br(std::string *str); + //htmlspecialchars + + static void bbcode_evaluate_simple(std::string *str); + static bbcpp::BBDocumentPtr bbcode(const std::string &str); + + static void str_replace(std::string *str, const std::string& from, const std::string& to); +protected: + static void eval_node(std::string *str, bbcpp::BBNodePtr node); + static void eval_element(std::string *str, bbcpp::BBElementPtr node); +}; + + +#endif \ No newline at end of file