Directory: | ./ |
---|---|
File: | pdcom5/src/msrproto/expat_wrapper_impl.h |
Date: | 2025-01-19 04:08:20 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 39 | 44 | 88.6% |
Branches: | 20 | 52 | 38.5% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /***************************************************************************** | ||
2 | * | ||
3 | * Copyright (C) 2021 Bjarne von Horn (vh at igh dot de) | ||
4 | * | ||
5 | * This file is part of the PdCom library. | ||
6 | * | ||
7 | * The PdCom library is free software: you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU Lesser General Public License as published by | ||
9 | * the Free Software Foundation, either version 3 of the License, or (at your | ||
10 | * option) any later version. | ||
11 | * | ||
12 | * The PdCom library is distributed in the hope that it will be useful, but | ||
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
14 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | ||
15 | * License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public License | ||
18 | * along with the PdCom library. If not, see <http://www.gnu.org/licenses/>. | ||
19 | * | ||
20 | *****************************************************************************/ | ||
21 | |||
22 | #ifndef MSRPROTO_EXPAT_WRAPPER_IMPL_H | ||
23 | #define MSRPROTO_EXPAT_WRAPPER_IMPL_H | ||
24 | |||
25 | #include "expat_wrapper.h" | ||
26 | |||
27 | #include <expat.h> | ||
28 | #include <expat_external.h> | ||
29 | #include <pdcom5/Exception.h> | ||
30 | #include <stdexcept> | ||
31 | |||
32 | namespace PdCom { namespace impl { namespace MsrProto { | ||
33 | 156 | inline void XmlParserDeleter::operator()(XML_ParserStruct *s) noexcept | |
34 | { | ||
35 |
1/2✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
|
156 | if (s) |
36 | 156 | XML_ParserFree(s); | |
37 | 156 | } | |
38 | |||
39 | |||
40 | template <typename Handler, bool useExpatBuffer> | ||
41 | struct ExpatWrapper<Handler, useExpatBuffer>::CallbackWrapper | ||
42 | { | ||
43 | // TODO(ighvh): use template<auto> when C++17 is mandatory (so in about 10 | ||
44 | // years) | ||
45 | template <typename T, T, int = 0> | ||
46 | struct Impl; | ||
47 | |||
48 | // We use a partial specialisation of the struct above | ||
49 | // to extract the argument types (Args) from the Member function. | ||
50 | template < | ||
51 | typename... Args, | ||
52 | void (Handler::*callback)(Args...), | ||
53 | int level_change> | ||
54 | struct Impl<void (Handler::*)(Args...), callback, level_change> | ||
55 | { | ||
56 | 5453 | static void XMLCALL call(void *userdata, Args... args) | |
57 | { | ||
58 | 5453 | auto &This = | |
59 | *reinterpret_cast<ExpatWrapper<Handler, useExpatBuffer> *>( | ||
60 | userdata); | ||
61 | // increment (start handler only) | ||
62 | 5453 | This.level_ += level_change == 1; | |
63 |
3/4✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2647 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 2805 times.
|
5453 | if (This.pending_exception_) |
64 | 1 | return; | |
65 | try { | ||
66 |
3/4✓ Branch 4 taken 2647 times.
✗ Branch 5 not taken.
✓ Branch 10 taken 2804 times.
✓ Branch 11 taken 1 times.
|
5452 | (static_cast<Handler &>(This).*callback)(args...); |
67 | } | ||
68 | 2 | catch (...) { | |
69 | 1 | This.pending_exception_ = std::current_exception(); | |
70 |
1/4✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
|
1 | XML_StopParser(This.parser_.get(), false); |
71 | } | ||
72 | // decrement (end handler only) | ||
73 | 5452 | This.level_ -= level_change == -1; | |
74 | } | ||
75 | }; | ||
76 | }; | ||
77 | |||
78 | template <typename Handler, bool useExpatBuffer> | ||
79 | 157 | inline ExpatWrapper<Handler, useExpatBuffer>::ExpatWrapper(const char *encoding) | |
80 | { | ||
81 |
1/2✓ Branch 2 taken 157 times.
✗ Branch 3 not taken.
|
157 | reset(encoding); |
82 | 157 | } | |
83 | |||
84 | template <typename Handler, bool useExpatBuffer> | ||
85 | 157 | inline void ExpatWrapper<Handler, useExpatBuffer>::reset(const char *encoding) | |
86 | { | ||
87 | 157 | pending_exception_ = nullptr; | |
88 | 157 | level_ = 0; | |
89 | 157 | parser_.reset(XML_ParserCreate(encoding)); | |
90 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 157 times.
|
157 | if (!parser_) |
91 | ✗ | throw PdCom::Exception("Could not create XML parser"); | |
92 | |||
93 | #if HAVE_XML_SetReparseDeferralEnabled | ||
94 | if (XML_SetReparseDeferralEnabled(parser_.get(), XML_FALSE) != XML_TRUE) | ||
95 | throw PdCom::Exception("Could not set ReparseDeferralEnabled attribute " | ||
96 | "of XML parser."); | ||
97 | #endif | ||
98 | |||
99 | using W1 = typename CallbackWrapper::template Impl< | ||
100 | decltype(&Handler::startElement), &Handler::startElement, 1>; | ||
101 | using W2 = typename CallbackWrapper::template Impl< | ||
102 | decltype(&Handler::endElement), &Handler::endElement, -1>; | ||
103 | |||
104 | 157 | XML_SetUserData(parser_.get(), this); | |
105 | 157 | XML_SetStartElementHandler(parser_.get(), &W1::call); | |
106 | 157 | XML_SetEndElementHandler(parser_.get(), &W2::call); | |
107 | 157 | } | |
108 | |||
109 | template <typename Handler, bool useExpatBuffer> | ||
110 | template <typename T> | ||
111 | inline typename std:: | ||
112 | enable_if<!useExpatBuffer && std::is_same<T, char>::value, bool>::type | ||
113 | ExpatWrapper<Handler, useExpatBuffer>::parse( | ||
114 | const T *s, | ||
115 | std::size_t n, | ||
116 | bool final) | ||
117 | { | ||
118 | if (XML_STATUS_OK == XML_Parse(parser_.get(), s, n, final)) | ||
119 | return true; | ||
120 | |||
121 | const XML_Error err_code = XML_GetErrorCode(parser_.get()); | ||
122 | if (err_code == XML_ERROR_ABORTED && pending_exception_) { | ||
123 | auto pe = pending_exception_; | ||
124 | pending_exception_ = nullptr; | ||
125 | std::rethrow_exception(pe); | ||
126 | } | ||
127 | |||
128 | static_cast<Handler *>(this)->xmlError(XML_ErrorString(err_code)); | ||
129 | return false; | ||
130 | } | ||
131 | |||
132 | |||
133 | template <typename Handler, bool useExpatBuffer> | ||
134 | template <std::size_t bufsize> | ||
135 | 1297 | ExpatWrapper<Handler, useExpatBuffer>::ExpatBuffer<bufsize>::ExpatBuffer( | |
136 | ExpatWrapper<Handler, useExpatBuffer> &wrapper) | ||
137 | { | ||
138 | static_assert(bufsize > 0, "Buffer size of 0 makes no sense."); | ||
139 | static_assert(useExpatBuffer, "Don't make a buffer when not using it"); | ||
140 | 1297 | buffer_ = XML_GetBuffer(wrapper.parser_.get(), bufsize); | |
141 |
2/4✗ Branch 1 not taken.
✓ Branch 2 taken 1140 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 157 times.
|
1297 | if (!buffer_) |
142 | ✗ | throw std::bad_alloc(); | |
143 | 1297 | } | |
144 | |||
145 | template <typename Handler, bool useExpatBuffer> | ||
146 | template <std::size_t bufsize> | ||
147 | inline typename std::enable_if<(useExpatBuffer && bufsize > 0), bool>::type | ||
148 | 1280 | ExpatWrapper<Handler, useExpatBuffer>::parse( | |
149 | ExpatWrapper<Handler, useExpatBuffer>::ExpatBuffer< | ||
150 | bufsize> /* buffer */, | ||
151 | std::size_t n, | ||
152 | bool final) | ||
153 | { | ||
154 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 1123 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 157 times.
|
1280 | if (n > bufsize) |
155 | throw PdCom::InvalidArgument( | ||
156 | ✗ | "buffer overflow, wrote more bytes than buffer size."); | |
157 | |||
158 |
3/4✓ Branch 3 taken 1122 times.
✓ Branch 4 taken 1 times.
✓ Branch 8 taken 157 times.
✗ Branch 9 not taken.
|
1280 | if (XML_STATUS_OK == XML_ParseBuffer(parser_.get(), n, final)) |
159 | 1279 | return true; | |
160 | |||
161 | 1 | const XML_Error err_code = XML_GetErrorCode(parser_.get()); | |
162 |
3/12✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
|
1 | if (err_code == XML_ERROR_ABORTED && pending_exception_) { |
163 | 2 | auto pe = pending_exception_; | |
164 | 1 | pending_exception_ = nullptr; | |
165 | 2 | std::rethrow_exception(pe); | |
166 | } | ||
167 | |||
168 | ✗ | static_cast<Handler *>(this)->xmlError(XML_ErrorString(err_code)); | |
169 | ✗ | return false; | |
170 | } | ||
171 | |||
172 | }}} // namespace PdCom::impl::MsrProto | ||
173 | |||
174 | #endif // MSRPROTO_EXPAT_WRAPPER_H | ||
175 |