GCC Code Coverage Report


Directory: ./
File: src/msrproto/expat_wrapper_impl.h
Date: 2024-03-27 13:09:52
Exec Total Coverage
Lines: 43 54 79.6%
Branches: 24 62 38.7%

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 132 inline void XmlParserDeleter::operator()(XML_ParserStruct *s) noexcept
34 {
35
1/2
✓ Branch 0 taken 132 times.
✗ Branch 1 not taken.
132 if (s)
36 132 XML_ParserFree(s);
37 132 }
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 1640 static void XMLCALL call(void *userdata, Args... args)
57 {
58 1640 auto &This =
59 *reinterpret_cast<ExpatWrapper<Handler, useExpatBuffer> *>(
60 userdata);
61 // increment (start handler only)
62 1640 This.level_ += level_change == 1;
63
3/4
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 594 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1044 times.
1640 if (This.pending_exception_)
64 2 return;
65 try {
66
3/4
✓ Branch 4 taken 594 times.
✗ Branch 5 not taken.
✓ Branch 10 taken 1042 times.
✓ Branch 11 taken 2 times.
1638 (static_cast<Handler &>(This).*callback)(args...);
67 }
68 4 catch (...) {
69 2 This.pending_exception_ = std::current_exception();
70
1/4
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
2 XML_StopParser(This.parser_.get(), false);
71 }
72 // decrement (end handler only)
73 1638 This.level_ -= level_change == -1;
74 }
75 };
76 };
77
78 template <typename Handler, bool useExpatBuffer>
79 124 inline ExpatWrapper<Handler, useExpatBuffer>::ExpatWrapper(const char *encoding)
80 {
81
1/2
✓ Branch 2 taken 124 times.
✗ Branch 3 not taken.
124 reset(encoding);
82 124 }
83
84 template <typename Handler, bool useExpatBuffer>
85 132 inline void ExpatWrapper<Handler, useExpatBuffer>::reset(const char *encoding)
86 {
87 132 pending_exception_ = nullptr;
88 132 level_ = 0;
89 132 parser_.reset(XML_ParserCreate(encoding));
90
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 132 times.
132 if (!parser_)
91 throw PdCom::Exception("Could not create XML parser");
92
93 using W1 = typename CallbackWrapper::template Impl<
94 decltype(&Handler::startElement), &Handler::startElement, 1>;
95 using W2 = typename CallbackWrapper::template Impl<
96 decltype(&Handler::endElement), &Handler::endElement, -1>;
97
98 132 XML_SetUserData(parser_.get(), this);
99 132 XML_SetStartElementHandler(parser_.get(), &W1::call);
100 132 XML_SetEndElementHandler(parser_.get(), &W2::call);
101 132 }
102
103 template <typename Handler, bool useExpatBuffer>
104 template <typename T>
105 inline typename std::
106 enable_if<!useExpatBuffer && std::is_same<T, char>::value, bool>::type
107 6786 ExpatWrapper<Handler, useExpatBuffer>::parse(
108 const T *s,
109 std::size_t n,
110 bool final)
111 {
112
1/2
✓ Branch 3 taken 6786 times.
✗ Branch 4 not taken.
6786 if (XML_STATUS_OK == XML_Parse(parser_.get(), s, n, final))
113 6786 return true;
114
115 const XML_Error err_code = XML_GetErrorCode(parser_.get());
116 if (err_code == XML_ERROR_ABORTED && pending_exception_) {
117 auto pe = pending_exception_;
118 pending_exception_ = nullptr;
119 std::rethrow_exception(pe);
120 }
121
122 static_cast<Handler *>(this)->xmlError(XML_ErrorString(err_code));
123 return false;
124 }
125
126
127 template <typename Handler, bool useExpatBuffer>
128 template <std::size_t bufsize>
129 422 ExpatWrapper<Handler, useExpatBuffer>::ExpatBuffer<bufsize>::ExpatBuffer(
130 ExpatWrapper<Handler, useExpatBuffer> &wrapper)
131 {
132 static_assert(bufsize > 0, "Buffer size of 0 makes no sense.");
133 static_assert(useExpatBuffer, "Don't make a buffer when not using it");
134 422 buffer_ = XML_GetBuffer(wrapper.parser_.get(), bufsize);
135
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 357 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 65 times.
422 if (!buffer_)
136 throw std::bad_alloc();
137 422 }
138
139 template <typename Handler, bool useExpatBuffer>
140 template <std::size_t bufsize>
141 inline typename std::enable_if<(useExpatBuffer && bufsize > 0), bool>::type
142 411 ExpatWrapper<Handler, useExpatBuffer>::parse(
143 ExpatWrapper<Handler, useExpatBuffer>::ExpatBuffer<
144 bufsize> /* buffer */,
145 std::size_t n,
146 bool final)
147 {
148
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 346 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 65 times.
411 if (n > bufsize)
149 throw PdCom::InvalidArgument(
150 "buffer overflow, wrote more bytes than buffer size.");
151
152
3/4
✓ Branch 3 taken 343 times.
✓ Branch 4 taken 3 times.
✓ Branch 8 taken 65 times.
✗ Branch 9 not taken.
411 if (XML_STATUS_OK == XML_ParseBuffer(parser_.get(), n, final))
153 408 return true;
154
155 3 const XML_Error err_code = XML_GetErrorCode(parser_.get());
156
5/12
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
3 if (err_code == XML_ERROR_ABORTED && pending_exception_) {
157 4 auto pe = pending_exception_;
158 2 pending_exception_ = nullptr;
159 4 std::rethrow_exception(pe);
160 }
161
162
1/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
1 static_cast<Handler *>(this)->xmlError(XML_ErrorString(err_code));
163 return false;
164 }
165
166 }}} // namespace PdCom::impl::MsrProto
167
168 #endif // MSRPROTO_EXPAT_WRAPPER_H
169