diff options
| author | Thomas Schmucker <ts@its1.de> | 2026-07-15 21:05:19 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2026-07-15 21:05:19 +0200 |
| commit | 4f3760fc3b647fa166da4ab777eb612b14ac500b (patch) | |
| tree | 2f9faece24cac31f8dcb842572cdd9a486402637 | |
| parent | 0cf833840b19e603f480dead5ca211f947503f9b (diff) | |
| download | expat++-4f3760fc3b647fa166da4ab777eb612b14ac500b.tar.gz expat++-4f3760fc3b647fa166da4ab777eb612b14ac500b.tar.bz2 expat++-4f3760fc3b647fa166da4ab777eb612b14ac500b.zip | |
Umbau auf modernes C++
| -rw-r--r-- | .clang-format | 46 | ||||
| -rw-r--r-- | .clang-tidy | 21 | ||||
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | bin/.gitkeep | 0 | ||||
| -rw-r--r-- | compile_flags.txt | 8 | ||||
| -rw-r--r-- | config.mk | 2 | ||||
| -rw-r--r-- | include/xml.hpp | 45 | ||||
| -rw-r--r-- | makefile | 50 | ||||
| -rw-r--r-- | obj/.gitkeep | 0 | ||||
| -rw-r--r-- | src/xml.cpp | 50 | ||||
| -rw-r--r-- | src/xml_test.cpp | 92 |
11 files changed, 203 insertions, 113 deletions
diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..67bca59 --- /dev/null +++ b/.clang-format | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | --- | ||
| 2 | AccessModifierOffset: -4 | ||
| 3 | AlignConsecutiveAssignments: 'true' | ||
| 4 | AlignConsecutiveDeclarations: 'true' | ||
| 5 | AlignEscapedNewlines: Left | ||
| 6 | AlignTrailingComments: 'true' | ||
| 7 | AlwaysBreakAfterReturnType: TopLevelDefinitions | ||
| 8 | BreakBeforeBraces: Stroustrup | ||
| 9 | BreakConstructorInitializers: BeforeComma | ||
| 10 | BreakInheritanceList: BeforeComma | ||
| 11 | ColumnLimit: '0' | ||
| 12 | CompactNamespaces: 'false' | ||
| 13 | Cpp11BracedListStyle: 'false' | ||
| 14 | FixNamespaceComments: 'true' | ||
| 15 | IncludeBlocks: Regroup | ||
| 16 | IncludeCategories: | ||
| 17 | - Regex: '^.*(precomp|pch|stdafx)' | ||
| 18 | Priority: -1 | ||
| 19 | - Regex: '^<.*>' | ||
| 20 | Priority: 1 | ||
| 21 | - Regex: '^".*"' | ||
| 22 | Priority: 2 | ||
| 23 | - Regex: '.*' | ||
| 24 | Priority: 3 | ||
| 25 | IndentCaseLabels: 'false' | ||
| 26 | IndentPPDirectives: AfterHash | ||
| 27 | IndentWidth: '4' | ||
| 28 | IndentWrappedFunctionNames: 'false' | ||
| 29 | KeepEmptyLinesAtTheStartOfBlocks: 'false' | ||
| 30 | PointerAlignment: Left | ||
| 31 | SortIncludes: 'true' | ||
| 32 | SpaceAfterCStyleCast: 'true' | ||
| 33 | SpaceAfterTemplateKeyword: 'false' | ||
| 34 | SpaceBeforeAssignmentOperators: 'true' | ||
| 35 | SpaceBeforeParens: ControlStatements | ||
| 36 | SpaceBeforeRangeBasedForLoopColon: 'false' | ||
| 37 | SpaceInEmptyParentheses: 'false' | ||
| 38 | SpacesInAngles: 'false' | ||
| 39 | SpacesInCStyleCastParentheses: 'false' | ||
| 40 | SpacesInConditionalStatement: 'true' | ||
| 41 | SpacesInParentheses: 'false' | ||
| 42 | Standard: Auto | ||
| 43 | TabWidth: '4' | ||
| 44 | UseTab: ForIndentation | ||
| 45 | ... | ||
| 46 | |||
diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..635ef64 --- /dev/null +++ b/.clang-tidy | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | --- | ||
| 2 | Checks: "*, | ||
| 3 | -abseil-*, | ||
| 4 | -altera-*, | ||
| 5 | -android-*, | ||
| 6 | -fuchsia-*, | ||
| 7 | -google-*, | ||
| 8 | -llvm*, | ||
| 9 | -modernize-use-trailing-return-type, | ||
| 10 | -zircon-*, | ||
| 11 | -readability-else-after-return, | ||
| 12 | -readability-static-accessed-through-instance, | ||
| 13 | -readability-avoid-const-params-in-decls, | ||
| 14 | -cppcoreguidelines-non-private-member-variables-in-classes, | ||
| 15 | -misc-non-private-member-variables-in-classes, | ||
| 16 | -cppcoreguidelines-pro-type-reinterpret-cast, | ||
| 17 | -cppcoreguidelines-pro-bounds-pointer-arithmetic | ||
| 18 | " | ||
| 19 | WarningsAsErrors: '' | ||
| 20 | HeaderFilterRegex: '' | ||
| 21 | FormatStyle: none | ||
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1746e32 --- /dev/null +++ b/.gitignore | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | bin | ||
| 2 | obj | ||
diff --git a/bin/.gitkeep b/bin/.gitkeep deleted file mode 100644 index e69de29..0000000 --- a/bin/.gitkeep +++ /dev/null | |||
diff --git a/compile_flags.txt b/compile_flags.txt new file mode 100644 index 0000000..86c17bb --- /dev/null +++ b/compile_flags.txt | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | -Wall | ||
| 2 | -Werror | ||
| 3 | -Wextra | ||
| 4 | -Wconversion | ||
| 5 | -Wdouble-promotion | ||
| 6 | -pedantic | ||
| 7 | -std=c++20 | ||
| 8 | -Iinclude | ||
diff --git a/config.mk b/config.mk new file mode 100644 index 0000000..209f15c --- /dev/null +++ b/config.mk | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | CXXFLAGS=-Wall -Werror -Wextra -Wconversion -Wdouble-promotion -pedantic -std=c++20 -Iinclude | ||
| 2 | LDFLAGS=-lexpat | ||
diff --git a/include/xml.hpp b/include/xml.hpp index cc593a3..bc1fe47 100644 --- a/include/xml.hpp +++ b/include/xml.hpp | |||
| @@ -1,28 +1,33 @@ | |||
| 1 | #ifndef XML_HPP_INCLUDED | 1 | #pragma once |
| 2 | #define XML_HPP_INCLUDED | ||
| 3 | 2 | ||
| 4 | // Standard C++ | 3 | // Standard C++ |
| 5 | #include <iostream> | 4 | #include <istream> |
| 6 | #include <string> | 5 | #include <span> |
| 6 | #include <string_view> | ||
| 7 | #include <vector> | 7 | #include <vector> |
| 8 | 8 | ||
| 9 | class XML { | 9 | class XML { |
| 10 | public: | 10 | public: |
| 11 | void parse(std::istream& in); | 11 | XML() = default; |
| 12 | virtual ~XML() = default; | ||
| 13 | XML(const XML&) = default; | ||
| 14 | XML(XML&&) = default; | ||
| 15 | XML& operator=(const XML&) = default; | ||
| 16 | XML& operator=(XML&&) = default; | ||
| 12 | 17 | ||
| 13 | protected: | 18 | void parse(std::istream& ins); |
| 14 | virtual void handle_start(const std::string& name, const std::vector<std::pair<std::string, std::string>>& attr) = 0; | ||
| 15 | virtual void handle_end(const std::string& name) = 0; | ||
| 16 | virtual void handle_cdata_start() {} | ||
| 17 | virtual void handle_cdata_end() {} | ||
| 18 | virtual void handle_data(const std::vector<char>&) {} | ||
| 19 | 19 | ||
| 20 | private: | 20 | protected: |
| 21 | static void tag_start(void* me, const char* name, const char* attr[]); | 21 | virtual void handle_start(std::string_view name, const std::vector<std::tuple<std::string_view, std::string_view>>& attr) = 0; |
| 22 | static void tag_end(void* me, const char* name); | 22 | virtual void handle_end(std::string_view name) = 0; |
| 23 | static void cdata_start(void* me); | 23 | virtual void handle_cdata_start() {} |
| 24 | static void cdata_end(void* me); | 24 | virtual void handle_cdata_end() {} |
| 25 | static void data(void* me, const char* dat, int len); | 25 | virtual void handle_character_data(std::span<const char> /* data */) {} |
| 26 | }; | ||
| 27 | 26 | ||
| 28 | #endif | 27 | private: |
| 28 | static void tag_start(void* self, const char* name, const char** attr); | ||
| 29 | static void tag_end(void* self, const char* name); | ||
| 30 | static void cdata_start(void* self); | ||
| 31 | static void cdata_end(void* self); | ||
| 32 | static void data(void* self, const char* dat, int len); | ||
| 33 | }; | ||
| @@ -1,47 +1,19 @@ | |||
| 1 | EXE = bin/xml.exe | 1 | include config.mk |
| 2 | 2 | ||
| 3 | all: $(EXE) | 3 | all: bin/xml_test |
| 4 | 4 | ||
| 5 | EMPTY = | 5 | OBJS = obj/xml.obj obj/xml_test.obj |
| 6 | 6 | ||
| 7 | CONTRIB = ../contrib | 7 | bin/xml_test: $(OBJS) | bin |
| 8 | c++ $(LDFLAGS) -o $@ $(OBJS) | ||
| 8 | 9 | ||
| 9 | EXPAT_DIR = $(CONTRIB)/expat-2.2.0 | 10 | obj/%.obj: src/%.cpp | obj |
| 10 | EXPAT_INCLUDE_DIR = $(EXPAT_DIR)/lib | 11 | c++ $(CXXFLAGS) -c $< -o $@ |
| 11 | EXPAT_LIB_DIR = $(EXPAT_DIR)/win32/bin/Release | ||
| 12 | EXPAT_LIBS = libexpatMT.lib | ||
| 13 | 12 | ||
| 14 | OBJS = \ | 13 | bin obj: |
| 15 | obj/xml.obj \ | 14 | mkdir -p $@ |
| 16 | obj/xml_test.obj \ | ||
| 17 | $(EMPTY) | ||
| 18 | |||
| 19 | WIN32_LIBS = kernel32.lib comdlg32.lib user32.lib gdi32.lib ole32.lib wsock32.lib Ws2_32.lib comctl32.lib shell32.lib oleaut32.lib advapi32.lib uuid.lib Winspool.lib | ||
| 20 | |||
| 21 | # === Compilereinstellungen === | ||
| 22 | |||
| 23 | CPPFLAGS=-nologo -EHsc -Ox -MT -W4 -WX -Iinclude -I$(EXPAT_INCLUDE_DIR) -DWIN32 -D_WINDOWS -D_CRT_NOFORCE_MANIFEST -D_CRT_SECURE_NO_WARNINGS -DNDEBUG | ||
| 24 | |||
| 25 | # === Linkereinstellungen === | ||
| 26 | |||
| 27 | LDFLAGS=-nologo -MANIFEST:NO -LIBPATH:$(EXPAT_LIB_DIR) | ||
| 28 | LDFLAGS2=$(WIN32_LIBS) $(EXPAT_LIBS) | ||
| 29 | |||
| 30 | $(EXE): $(OBJS) | ||
| 31 | @echo link $@ | ||
| 32 | @link.exe $(LDFLAGS) -out:$@ $(LDFLAGS2) $(OBJS) | ||
| 33 | |||
| 34 | obj/%.obj: src/%.cpp | ||
| 35 | @echo compiling $@ | ||
| 36 | cl $(CPPFLAGS) -Tp$< -c -Fo$@ | ||
| 37 | |||
| 38 | obj/%.obj: src/%.c | ||
| 39 | @echo compiling $@ | ||
| 40 | @cl $(CPPFLAGS) -Tc$< -c -Fo$@ | ||
| 41 | 15 | ||
| 42 | clean: | 16 | clean: |
| 43 | @echo cleaning... | 17 | rm -rf bin obj |
| 44 | @del bin\*.exe | ||
| 45 | @del obj\** | ||
| 46 | @del *.pdb | ||
| 47 | 18 | ||
| 19 | .PHONY: all clean | ||
diff --git a/obj/.gitkeep b/obj/.gitkeep deleted file mode 100644 index e69de29..0000000 --- a/obj/.gitkeep +++ /dev/null | |||
diff --git a/src/xml.cpp b/src/xml.cpp index 44b423a..451ee18 100644 --- a/src/xml.cpp +++ b/src/xml.cpp | |||
| @@ -1,18 +1,19 @@ | |||
| 1 | // Standard C++ | 1 | // Standard C++ |
| 2 | #include <array> | ||
| 2 | #include <memory> | 3 | #include <memory> |
| 3 | #include <sstream> | 4 | #include <sstream> |
| 5 | #include <span> | ||
| 4 | 6 | ||
| 5 | // Projekt | 7 | // Project |
| 6 | #include "xml.hpp" | 8 | #include "xml.hpp" |
| 7 | 9 | ||
| 8 | // Expat XML Parser | 10 | // Expat XML Parser |
| 9 | #define XML_STATIC // force static linkage | ||
| 10 | #include "expat.h" | 11 | #include "expat.h" |
| 11 | 12 | ||
| 12 | using namespace std; | 13 | using namespace std; |
| 13 | 14 | ||
| 14 | void | 15 | void |
| 15 | XML::parse(istream& in) | 16 | XML::parse(istream& ins) |
| 16 | { | 17 | { |
| 17 | unique_ptr<XML_ParserStruct, decltype(&XML_ParserFree)> parser(XML_ParserCreate(nullptr), XML_ParserFree); | 18 | unique_ptr<XML_ParserStruct, decltype(&XML_ParserFree)> parser(XML_ParserCreate(nullptr), XML_ParserFree); |
| 18 | 19 | ||
| @@ -22,54 +23,57 @@ XML::parse(istream& in) | |||
| 22 | XML_SetCdataSectionHandler(parser.get(), cdata_start, cdata_end); | 23 | XML_SetCdataSectionHandler(parser.get(), cdata_start, cdata_end); |
| 23 | XML_SetCharacterDataHandler(parser.get(), data); | 24 | XML_SetCharacterDataHandler(parser.get(), data); |
| 24 | 25 | ||
| 25 | char buffer[2048]; | 26 | static const auto buffer_size = 2048; |
| 26 | for ( bool done = false; !done ; ) { | ||
| 27 | in.read(buffer, sizeof buffer); | ||
| 28 | done = (in.gcount() < sizeof buffer); | ||
| 29 | 27 | ||
| 30 | if ( XML_Parse(parser.get(), buffer, static_cast<int>(in.gcount()), done) != XML_STATUS_OK ) { | 28 | array<char, buffer_size> buffer{}; |
| 29 | |||
| 30 | for ( bool done = false; !done; ) { | ||
| 31 | ins.read(buffer.data(), buffer_size); | ||
| 32 | done = ins.gcount() < buffer_size; | ||
| 33 | |||
| 34 | if ( XML_Parse(parser.get(), buffer.data(), static_cast<int>(ins.gcount()), static_cast<int>(done)) != XML_STATUS_OK ) { | ||
| 31 | stringstream msg; | 35 | stringstream msg; |
| 32 | 36 | ||
| 33 | msg << XML_ErrorString(XML_GetErrorCode(parser.get())); | 37 | msg << XML_ErrorString(XML_GetErrorCode(parser.get())); |
| 34 | msg << " in line " << XML_GetCurrentLineNumber(parser.get()); | 38 | msg << " in line " << XML_GetCurrentLineNumber(parser.get()); |
| 35 | 39 | ||
| 36 | throw runtime_error( msg.str() ); | 40 | throw runtime_error(msg.str()); |
| 37 | } | 41 | } |
| 38 | } | 42 | } |
| 39 | } | 43 | } |
| 40 | 44 | ||
| 41 | void | 45 | void |
| 42 | XML::tag_start(void* me, const char* name, const char* attr[]) | 46 | XML::tag_start(void* self, const char* name, const char** attr) |
| 43 | { | 47 | { |
| 44 | vector<pair<string, string>> attr_temp; | 48 | vector<tuple<string_view, string_view>> attr_temp; |
| 45 | 49 | ||
| 46 | for ( size_t i = 0; attr[i]; i += 2 ) | 50 | for ( auto i = 0; attr[i] != nullptr; i += 2 ) { |
| 47 | attr_temp.push_back( make_pair<string, string>(attr[i], attr[i+1]) ); | 51 | attr_temp.emplace_back(attr[i], attr[i + 1]); |
| 52 | } | ||
| 48 | 53 | ||
| 49 | reinterpret_cast<XML*>(me)->handle_start(name, attr_temp); | 54 | static_cast<XML*>(self)->handle_start(name, attr_temp); |
| 50 | } | 55 | } |
| 51 | 56 | ||
| 52 | void | 57 | void |
| 53 | XML::tag_end(void* me, const char* name) | 58 | XML::tag_end(void* self, const char* name) |
| 54 | { | 59 | { |
| 55 | reinterpret_cast<XML*>(me)->handle_end(name); | 60 | static_cast<XML*>(self)->handle_end(name); |
| 56 | } | 61 | } |
| 57 | 62 | ||
| 58 | void | 63 | void |
| 59 | XML::cdata_start(void* me) | 64 | XML::cdata_start(void* self) |
| 60 | { | 65 | { |
| 61 | reinterpret_cast<XML*>(me)->handle_cdata_start(); | 66 | static_cast<XML*>(self)->handle_cdata_start(); |
| 62 | } | 67 | } |
| 63 | 68 | ||
| 64 | void | 69 | void |
| 65 | XML::cdata_end(void* me) | 70 | XML::cdata_end(void* self) |
| 66 | { | 71 | { |
| 67 | reinterpret_cast<XML*>(me)->handle_cdata_end(); | 72 | static_cast<XML*>(self)->handle_cdata_end(); |
| 68 | } | 73 | } |
| 69 | 74 | ||
| 70 | void | 75 | void |
| 71 | XML::data(void* me, const char* dat, int len) | 76 | XML::data(void* self, const char* dat, int len) |
| 72 | { | 77 | { |
| 73 | reinterpret_cast<XML*>(me)->handle_data(vector<char>(dat, dat+len)); | 78 | static_cast<XML*>(self)->handle_character_data({ dat, dat + len }); |
| 74 | } | 79 | } |
| 75 | |||
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 | } |
