From 4f3760fc3b647fa166da4ab777eb612b14ac500b Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Wed, 15 Jul 2026 21:05:19 +0200 Subject: Umbau auf modernes C++ --- src/xml_test.cpp | 92 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 61 insertions(+), 31 deletions(-) (limited to 'src/xml_test.cpp') diff --git a/src/xml_test.cpp b/src/xml_test.cpp index 0af5b51..0b978a9 100644 --- a/src/xml_test.cpp +++ b/src/xml_test.cpp @@ -1,48 +1,78 @@ #include #include +#include using namespace std; #include "xml.hpp" class MyXML : public XML { - public: - explicit MyXML(ostream& out, size_t indent = 4) - : pout_(&out) - , depth_(0) - , indent_(indent) - { - } +public: + explicit MyXML(ostream& out, size_t indent = 4) + : pout_(&out) + , indent_(indent) + { + } - protected: - void handle_start(const string& name, const vector>& attr) - { - *pout_ << string(depth_++ * indent_, ' ') - << "<" << name; +protected: + void handle_start(string_view name, const vector>& attr) override + { + *pout_ << string(depth_++ * indent_, ' ') + << "<" << name; - auto it = cbegin(attr); - auto end = cend(attr); - if ( it != end ) { - *pout_ << " " << it->first << "=\"" << it->second << "\""; + auto start = cbegin(attr); + auto end = cend(attr); + if ( start != end ) { + *pout_ << " " << get<0>(*start) << "=\"" << get<1>(*start) << "\""; - ++it; // skip first Element - for ( ; it != end; ++it ) - *pout_ << ", " << it->first << "=\"" << it->second << "\""; + ++start; // skip first Element + for ( ; start != end; ++start ) { + *pout_ << ", " << get<0>(*start) << "=\"" << get<1>(*start) << "\""; } - - *pout_ << ">" << endl; } - void handle_end(const string& name) - { - *pout_ << string(--depth_ * indent_, ' ') - << "" - << endl; + *pout_ << ">" << '\n'; + } + + void handle_end(string_view name) override + { + *pout_ << string(--depth_ * indent_, ' ') + << "" + << '\n'; + } + void handle_cdata_start() override + { + *pout_ << string(depth_ * indent_, ' ') + << "" << '\n'; + } + + static bool contains_only_whitespace(span data) + { + return ranges::all_of(data, [](unsigned char chr) { + return std::isspace(chr); + }); + } + + void handle_character_data(span data) override + { + if ( contains_only_whitespace(data) ) { + return; } - private: - ostream* pout_; - size_t depth_; - size_t indent_; + *pout_ << string(depth_ * indent_, ' ') << "Data: "; + pout_->write(data.data(), static_cast(data.size())); + *pout_ << '\n'; + } + +private: + ostream* pout_; + size_t depth_{}; + size_t indent_; }; int @@ -53,6 +83,6 @@ main() myxml.parse(cin); } catch ( exception& e ) { - cerr << "Fehler: " << e.what() << endl; + cerr << "Fehler: " << e.what() << '\n'; } } -- cgit v1.3