Skip to content

Commit

Permalink
feat(printable.hpp): print_style && implement print for compacted style
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamska1008 committed Sep 18, 2024
1 parent c7b7d9b commit 97ab34e
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 35 deletions.
19 changes: 13 additions & 6 deletions include/myxml/printable.hpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
#pragma once
#include <string>
#include <optional>
#include <variant>

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<compacted, formatted>;

struct print_config
{
public:
bool entity_encoding;
bool platform_specific_newline;
std::optional<format_config> fconfig;
print_style style;
print_config();
};

class printable
{
protected:
print_config _config;
print_config _print_config;

public:
virtual ~printable() = default;
Expand Down
10 changes: 9 additions & 1 deletion src/cdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ namespace myxml

void cdata_impl::print(std::ostream &os) const
{
os << "<![CDATA[" << _inner << "]]>\n";
if (auto f = std::get_if<formatted>(&_print_config.style))
{
// TODO: implement it
}
else
{
os << "<![CDATA[" << _inner << "]]>";
return;
}
}
}
10 changes: 9 additions & 1 deletion src/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ namespace myxml

void document::print(std::ostream &os) const
{
os << this->_decl << this->_root;
if (auto f = std::get_if<formatted>(&_print_config.style))
{
// TODO: implement it
}
else
{
os << this->_decl << this->_root;
return;
}
}
void document::entity_encoding(bool flag)
{
Expand Down
40 changes: 24 additions & 16 deletions src/element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<formatted>(&_print_config.style))
{
os << " ";
for (const auto &[key, value] : this->_attributes)
// TODO: implement it
}
else if (std::holds_alternative<compacted>(_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 << "</" << this->_name << ">";
}
os << "</" << this->_name << ">";
throw std::logic_error("should never execute code in file element.cpp, line " + __LINE__);
}

namespace literals
Expand Down
8 changes: 4 additions & 4 deletions src/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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> node::next_sibiling()
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/printable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace myxml
print_config::print_config()
: entity_encoding(true),
platform_specific_newline(false),
fconfig(std::nullopt)
style(compacted{})
{
}

Expand All @@ -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()
Expand Down
16 changes: 12 additions & 4 deletions src/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, char, std::less<>> entityMap = {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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<formatted>(&_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())
{
Expand All @@ -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')
{
Expand All @@ -143,6 +147,10 @@ namespace myxml
}
}
os << this->inner.substr(start, len - start);
if (std::holds_alternative<formatted>(_print_config.style))
{
// TODO: implement it
}
}
}

Expand Down

0 comments on commit 97ab34e

Please sign in to comment.