diff options
Diffstat (limited to 'src/xml_test.cpp')
| -rw-r--r-- | src/xml_test.cpp | 92 |
1 files changed, 61 insertions, 31 deletions
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 @@ | |||
| 1 | #include <iostream> | 1 | #include <iostream> |
| 2 | #include <string> | 2 | #include <string> |
| 3 | #include <tuple> | ||
| 3 | using namespace std; | 4 | using namespace std; |
| 4 | 5 | ||
| 5 | #include "xml.hpp" | 6 | #include "xml.hpp" |
| 6 | 7 | ||
| 7 | class MyXML : public XML { | 8 | class MyXML : public XML { |
| 8 | public: | 9 | public: |
| 9 | explicit MyXML(ostream& out, size_t indent = 4) | 10 | explicit MyXML(ostream& out, size_t indent = 4) |
| 10 | : pout_(&out) | 11 | : pout_(&out) |
| 11 | , depth_(0) | 12 | , indent_(indent) |
| 12 | , indent_(indent) | 13 | { |
| 13 | { | 14 | } |
| 14 | } | ||
| 15 | 15 | ||
| 16 | protected: | 16 | protected: |
| 17 | void handle_start(const string& name, const vector<pair<string, string>>& attr) | 17 | void handle_start(string_view name, const vector<tuple<string_view, string_view>>& attr) override |
| 18 | { | 18 | { |
| 19 | *pout_ << string(depth_++ * indent_, ' ') | 19 | *pout_ << string(depth_++ * indent_, ' ') |
| 20 | << "<" << name; | 20 | << "<" << name; |
| 21 | 21 | ||
| 22 | auto it = cbegin(attr); | 22 | auto start = cbegin(attr); |
| 23 | auto end = cend(attr); | 23 | auto end = cend(attr); |
| 24 | if ( it != end ) { | 24 | if ( start != end ) { |
| 25 | *pout_ << " " << it->first << "=\"" << it->second << "\""; | 25 | *pout_ << " " << get<0>(*start) << "=\"" << get<1>(*start) << "\""; |
| 26 | 26 | ||
| 27 | ++it; // skip first Element | 27 | ++start; // skip first Element |
| 28 | for ( ; it != end; ++it ) | 28 | for ( ; start != end; ++start ) { |
| 29 | *pout_ << ", " << it->first << "=\"" << it->second << "\""; | 29 | *pout_ << ", " << get<0>(*start) << "=\"" << get<1>(*start) << "\""; |
| 30 | } | 30 | } |
| 31 | |||
| 32 | *pout_ << ">" << endl; | ||
| 33 | } | 31 | } |
| 34 | 32 | ||
| 35 | void handle_end(const string& name) | 33 | *pout_ << ">" << '\n'; |
| 36 | { | 34 | } |
| 37 | *pout_ << string(--depth_ * indent_, ' ') | 35 | |
| 38 | << "</" << name << ">" | 36 | void handle_end(string_view name) override |
| 39 | << endl; | 37 | { |
| 38 | *pout_ << string(--depth_ * indent_, ' ') | ||
| 39 | << "</" << name << ">" | ||
| 40 | << '\n'; | ||
| 41 | } | ||
| 42 | void handle_cdata_start() override | ||
| 43 | { | ||
| 44 | *pout_ << string(depth_ * indent_, ' ') | ||
| 45 | << "<![CDATA[" << '\n'; | ||
| 46 | } | ||
| 47 | |||
| 48 | void handle_cdata_end() override | ||
| 49 | { | ||
| 50 | *pout_ << string(depth_ * indent_, ' ') | ||
| 51 | << "]]>" << '\n'; | ||
| 52 | } | ||
| 53 | |||
| 54 | static bool contains_only_whitespace(span<const char> data) | ||
| 55 | { | ||
| 56 | return ranges::all_of(data, [](unsigned char chr) { | ||
| 57 | return std::isspace(chr); | ||
| 58 | }); | ||
| 59 | } | ||
| 60 | |||
| 61 | void handle_character_data(span<const char> data) override | ||
| 62 | { | ||
| 63 | if ( contains_only_whitespace(data) ) { | ||
| 64 | return; | ||
| 40 | } | 65 | } |
| 41 | 66 | ||
| 42 | private: | 67 | *pout_ << string(depth_ * indent_, ' ') << "Data: "; |
| 43 | ostream* pout_; | 68 | pout_->write(data.data(), static_cast<streamsize>(data.size())); |
| 44 | size_t depth_; | 69 | *pout_ << '\n'; |
| 45 | size_t indent_; | 70 | } |
| 71 | |||
| 72 | private: | ||
| 73 | ostream* pout_; | ||
| 74 | size_t depth_{}; | ||
| 75 | size_t indent_; | ||
| 46 | }; | 76 | }; |
| 47 | 77 | ||
| 48 | int | 78 | int |
| @@ -53,6 +83,6 @@ main() | |||
| 53 | myxml.parse(cin); | 83 | myxml.parse(cin); |
| 54 | } | 84 | } |
| 55 | catch ( exception& e ) { | 85 | catch ( exception& e ) { |
| 56 | cerr << "Fehler: " << e.what() << endl; | 86 | cerr << "Fehler: " << e.what() << '\n'; |
| 57 | } | 87 | } |
| 58 | } | 88 | } |
