From 97ab34efe356d9fdceed4599386a5a67347e117c Mon Sep 17 00:00:00 2001 From: Zijian Zang <2639980868@qq.com> Date: Wed, 18 Sep 2024 20:05:43 +0800 Subject: [PATCH] feat(printable.hpp): print_style && implement print for compacted style --- include/myxml/printable.hpp | 19 ++++++++++++------ src/cdata.cpp | 10 +++++++++- src/document.cpp | 10 +++++++++- src/element.cpp | 40 ++++++++++++++++++++++--------------- src/node.cpp | 8 ++++---- src/printable.cpp | 6 +++--- src/text.cpp | 16 +++++++++++---- 7 files changed, 74 insertions(+), 35 deletions(-) diff --git a/include/myxml/printable.hpp b/include/myxml/printable.hpp index beeb93c..df292bd 100644 --- a/include/myxml/printable.hpp +++ b/include/myxml/printable.hpp @@ -1,30 +1,37 @@ #pragma once #include #include +#include namespace myxml { - struct format_config + struct compacted { - std::size_t indent_level; - std::size_t indent_size; + }; + + struct formatted + { + int indent_size; + int indent_level; - format_config deeper() { return {indent_level + 1, indent_size}; } + formatted deeper() { return {indent_size, indent_level + 1}; } }; + using print_style = std::variant; + struct print_config { public: bool entity_encoding; bool platform_specific_newline; - std::optional fconfig; + print_style style; print_config(); }; class printable { protected: - print_config _config; + print_config _print_config; public: virtual ~printable() = default; diff --git a/src/cdata.cpp b/src/cdata.cpp index 90a06f9..79de765 100644 --- a/src/cdata.cpp +++ b/src/cdata.cpp @@ -41,6 +41,14 @@ namespace myxml void cdata_impl::print(std::ostream &os) const { - os << "\n"; + if (auto f = std::get_if(&_print_config.style)) + { + // TODO: implement it + } + else + { + os << ""; + return; + } } } diff --git a/src/document.cpp b/src/document.cpp index a2bc383..45f947c 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -40,7 +40,15 @@ namespace myxml void document::print(std::ostream &os) const { - os << this->_decl << this->_root; + if (auto f = std::get_if(&_print_config.style)) + { + // TODO: implement it + } + else + { + os << this->_decl << this->_root; + return; + } } void document::entity_encoding(bool flag) { diff --git a/src/element.cpp b/src/element.cpp index c83413f..1cacbcc 100644 --- a/src/element.cpp +++ b/src/element.cpp @@ -169,27 +169,35 @@ namespace myxml void element_impl::print(std::ostream &os) const { - os << "<" << this->_name; - if (!_attributes.empty()) + if (auto f = std::get_if(&_print_config.style)) { - os << " "; - for (const auto &[key, value] : this->_attributes) + // TODO: implement it + } + else if (std::holds_alternative(_print_config.style)) + { + os << "<" << this->_name; + if (!_attributes.empty()) { - os << "" << key << "=\"" << value << "\""; + os << " "; + for (const auto &[key, value] : this->_attributes) + { + os << "" << key << "=\"" << value << "\""; + } } - } - if (this->first_child() == nullptr) - { - os << " />"; - return; - } - os << ">"; - for (auto node = this->first_child(); node != nullptr; node = node->next_sibiling()) - { - node->print(os); + if (this->first_child() == nullptr) + { + os << " />"; + return; + } + os << ">"; + for (auto node = this->first_child(); node != nullptr; node = node->next_sibiling()) + { + node->print(os); + } + os << "_name << ">"; } - os << "_name << ">"; + throw std::logic_error("should never execute code in file element.cpp, line " + __LINE__); } namespace literals diff --git a/src/node.cpp b/src/node.cpp index 5aa1903..df8862a 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -6,12 +6,12 @@ namespace myxml { void node::entity_encoding(bool flag) { - this->_config.entity_encoding = flag; + this->_print_config.entity_encoding = flag; } void node::platform_specific_newline(bool flag) { - this->_config.platform_specific_newline = flag; + this->_print_config.platform_specific_newline = flag; } std::shared_ptr node::next_sibiling() @@ -140,7 +140,7 @@ namespace myxml void composite_node::entity_encoding(bool flag) { - this->_config.entity_encoding = flag; + this->_print_config.entity_encoding = flag; for (auto it = this->first_child(); it != nullptr; it = it->_next) { it->entity_encoding(flag); @@ -149,7 +149,7 @@ namespace myxml void composite_node::platform_specific_newline(bool flag) { - this->_config.platform_specific_newline = flag; + this->_print_config.platform_specific_newline = flag; for (auto it = this->first_child(); it != nullptr; it = it->_next) { it->platform_specific_newline(flag); diff --git a/src/printable.cpp b/src/printable.cpp index d4c58d7..e4453aa 100644 --- a/src/printable.cpp +++ b/src/printable.cpp @@ -6,7 +6,7 @@ namespace myxml print_config::print_config() : entity_encoding(true), platform_specific_newline(false), - fconfig(std::nullopt) + style(compacted{}) { } @@ -18,12 +18,12 @@ namespace myxml void printable::entity_encoding(bool flag) { - this->_config.entity_encoding = flag; + this->_print_config.entity_encoding = flag; } void printable::platform_specific_newline(bool flag) { - this->_config.platform_specific_newline = flag; + this->_print_config.platform_specific_newline = flag; } std::string printable::str() diff --git a/src/text.cpp b/src/text.cpp index 8138480..db04b7e 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -54,7 +54,7 @@ namespace myxml text_impl::text_impl(std::string_view input) { - if (_config.entity_encoding) + if (_print_config.entity_encoding) { // entity encoding static std::map> entityMap = { @@ -106,7 +106,7 @@ namespace myxml void text_impl::print(std::ostream &os) const { - if (!this->_config.entity_encoding && !this->_config.platform_specific_newline) + if (!this->_print_config.entity_encoding && !this->_print_config.platform_specific_newline) { os << this->inner; } @@ -121,9 +121,13 @@ namespace myxml }; std::size_t start = 0; // start of current segement std::size_t len = this->inner.length(); + if (auto f = std::get_if(&_print_config.style); f) + { + // TODO: implement it + } for (std::size_t i = 0; i < len; i++) { - if (this->_config.entity_encoding) + if (this->_print_config.entity_encoding) { if (auto it = entityMap.find(this->inner[i]); it != entityMap.end()) { @@ -132,7 +136,7 @@ namespace myxml start = i + 1; } } - if (this->_config.platform_specific_newline) + if (this->_print_config.platform_specific_newline) { if (this->inner[i] == '\n') { @@ -143,6 +147,10 @@ namespace myxml } } os << this->inner.substr(start, len - start); + if (std::holds_alternative(_print_config.style)) + { + // TODO: implement it + } } }