diff options
| author | Thomas Schmucker <ts@its1.de> | 2025-03-09 22:26:25 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2025-03-09 22:26:25 +0100 |
| commit | 0cf833840b19e603f480dead5ca211f947503f9b (patch) | |
| tree | 9b146c5644e1bb93ed0307275a6930bffe11d0ad /src | |
| download | expat++-0cf833840b19e603f480dead5ca211f947503f9b.tar.gz expat++-0cf833840b19e603f480dead5ca211f947503f9b.tar.bz2 expat++-0cf833840b19e603f480dead5ca211f947503f9b.zip | |
Initial commit
Diffstat (limited to 'src')
| -rw-r--r-- | src/xml.cpp | 75 | ||||
| -rw-r--r-- | src/xml_test.cpp | 58 |
2 files changed, 133 insertions, 0 deletions
diff --git a/src/xml.cpp b/src/xml.cpp new file mode 100644 index 0000000..44b423a --- /dev/null +++ b/src/xml.cpp | |||
| @@ -0,0 +1,75 @@ | |||
| 1 | // Standard C++ | ||
| 2 | #include <memory> | ||
| 3 | #include <sstream> | ||
| 4 | |||
| 5 | // Projekt | ||
| 6 | #include "xml.hpp" | ||
| 7 | |||
| 8 | // Expat XML Parser | ||
| 9 | #define XML_STATIC // force static linkage | ||
| 10 | #include "expat.h" | ||
| 11 | |||
| 12 | using namespace std; | ||
| 13 | |||
| 14 | void | ||
| 15 | XML::parse(istream& in) | ||
| 16 | { | ||
| 17 | unique_ptr<XML_ParserStruct, decltype(&XML_ParserFree)> parser(XML_ParserCreate(nullptr), XML_ParserFree); | ||
| 18 | |||
| 19 | // setup expat | ||
| 20 | XML_SetUserData(parser.get(), this); | ||
| 21 | XML_SetElementHandler(parser.get(), tag_start, tag_end); | ||
| 22 | XML_SetCdataSectionHandler(parser.get(), cdata_start, cdata_end); | ||
| 23 | XML_SetCharacterDataHandler(parser.get(), data); | ||
| 24 | |||
| 25 | char buffer[2048]; | ||
| 26 | for ( bool done = false; !done ; ) { | ||
| 27 | in.read(buffer, sizeof buffer); | ||
| 28 | done = (in.gcount() < sizeof buffer); | ||
| 29 | |||
| 30 | if ( XML_Parse(parser.get(), buffer, static_cast<int>(in.gcount()), done) != XML_STATUS_OK ) { | ||
| 31 | stringstream msg; | ||
| 32 | |||
| 33 | msg << XML_ErrorString(XML_GetErrorCode(parser.get())); | ||
| 34 | msg << " in line " << XML_GetCurrentLineNumber(parser.get()); | ||
| 35 | |||
| 36 | throw runtime_error( msg.str() ); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | void | ||
| 42 | XML::tag_start(void* me, const char* name, const char* attr[]) | ||
| 43 | { | ||
| 44 | vector<pair<string, string>> attr_temp; | ||
| 45 | |||
| 46 | for ( size_t i = 0; attr[i]; i += 2 ) | ||
| 47 | attr_temp.push_back( make_pair<string, string>(attr[i], attr[i+1]) ); | ||
| 48 | |||
| 49 | reinterpret_cast<XML*>(me)->handle_start(name, attr_temp); | ||
| 50 | } | ||
| 51 | |||
| 52 | void | ||
| 53 | XML::tag_end(void* me, const char* name) | ||
| 54 | { | ||
| 55 | reinterpret_cast<XML*>(me)->handle_end(name); | ||
| 56 | } | ||
| 57 | |||
| 58 | void | ||
| 59 | XML::cdata_start(void* me) | ||
| 60 | { | ||
| 61 | reinterpret_cast<XML*>(me)->handle_cdata_start(); | ||
| 62 | } | ||
| 63 | |||
| 64 | void | ||
| 65 | XML::cdata_end(void* me) | ||
| 66 | { | ||
| 67 | reinterpret_cast<XML*>(me)->handle_cdata_end(); | ||
| 68 | } | ||
| 69 | |||
| 70 | void | ||
| 71 | XML::data(void* me, const char* dat, int len) | ||
| 72 | { | ||
| 73 | reinterpret_cast<XML*>(me)->handle_data(vector<char>(dat, dat+len)); | ||
| 74 | } | ||
| 75 | |||
diff --git a/src/xml_test.cpp b/src/xml_test.cpp new file mode 100644 index 0000000..0af5b51 --- /dev/null +++ b/src/xml_test.cpp | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | #include <iostream> | ||
| 2 | #include <string> | ||
| 3 | using namespace std; | ||
| 4 | |||
| 5 | #include "xml.hpp" | ||
| 6 | |||
| 7 | class MyXML : public XML { | ||
| 8 | public: | ||
| 9 | explicit MyXML(ostream& out, size_t indent = 4) | ||
| 10 | : pout_(&out) | ||
| 11 | , depth_(0) | ||
| 12 | , indent_(indent) | ||
| 13 | { | ||
| 14 | } | ||
| 15 | |||
| 16 | protected: | ||
| 17 | void handle_start(const string& name, const vector<pair<string, string>>& attr) | ||
| 18 | { | ||
| 19 | *pout_ << string(depth_++ * indent_, ' ') | ||
| 20 | << "<" << name; | ||
| 21 | |||
| 22 | auto it = cbegin(attr); | ||
| 23 | auto end = cend(attr); | ||
| 24 | if ( it != end ) { | ||
| 25 | *pout_ << " " << it->first << "=\"" << it->second << "\""; | ||
| 26 | |||
| 27 | ++it; // skip first Element | ||
| 28 | for ( ; it != end; ++it ) | ||
| 29 | *pout_ << ", " << it->first << "=\"" << it->second << "\""; | ||
| 30 | } | ||
| 31 | |||
| 32 | *pout_ << ">" << endl; | ||
| 33 | } | ||
| 34 | |||
| 35 | void handle_end(const string& name) | ||
| 36 | { | ||
| 37 | *pout_ << string(--depth_ * indent_, ' ') | ||
| 38 | << "</" << name << ">" | ||
| 39 | << endl; | ||
| 40 | } | ||
| 41 | |||
| 42 | private: | ||
| 43 | ostream* pout_; | ||
| 44 | size_t depth_; | ||
| 45 | size_t indent_; | ||
| 46 | }; | ||
| 47 | |||
| 48 | int | ||
| 49 | main() | ||
| 50 | { | ||
| 51 | try { | ||
| 52 | MyXML myxml(cout); | ||
| 53 | myxml.parse(cin); | ||
| 54 | } | ||
| 55 | catch ( exception& e ) { | ||
| 56 | cerr << "Fehler: " << e.what() << endl; | ||
| 57 | } | ||
| 58 | } | ||
