GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 92.3% 12 / 0 / 13
Functions: 100.0% 5 / 0 / 5
Branches: 40.0% 2 / 0 / 5

src/utilities/xml/XMLUtils.hpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #ifndef UTILITIES_XML_XMLUTILS_HPP
7 #define UTILITIES_XML_XMLUTILS_HPP
8
9 #include <climits>
10 #include <libxml/globals.h>
11 #include <libxml/xmlstring.h>
12 #include <stdexcept>
13 #include <string>
14
15 using xmlError = struct _xmlError;
16
17 namespace openstudio {
18
19 namespace detail {
20
21 10 inline const xmlChar* xml_string(const std::string& s) {
22 10 return reinterpret_cast<const xmlChar*>(s.c_str());
23 }
24
25 // Cast size_t to int safely, i.e. throw an exception in case of an overflow.
26 inline int checked_int_cast(std::size_t len) {
27 if (len > INT_MAX) {
28 throw std::invalid_argument("data too long");
29 }
30
31 return static_cast<int>(len);
32 }
33
34 // Cast int to size_t safely, checking that it's non-negative (we assume that
35 // size_t is always big enough to contain INT_MAX, which is true for all
36 // currently existing architectures).
37 4 inline std::size_t checked_size_t_cast(int len) {
38
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (len < 0) {
39 ✗ throw std::runtime_error("length value unexpectedly negative");
40 }
41
42 4 return static_cast<std::size_t>(len);
43 }
44
45 // exception safe wrapper around xmlChar*s that are returned from some
46 // of the libxml functions that the user must free.
47 class xmlchar_helper
48 {
49 public:
50 8 xmlchar_helper(xmlChar* ptr) : ptr_(ptr) {}
51
52 8 ~xmlchar_helper() {
53
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (ptr_) {
54 8 xmlFree(ptr_);
55 }
56 8 }
57
58 8 const char* get() const {
59 8 return reinterpret_cast<const char*>(ptr_);
60 }
61
62 private:
63 xmlChar* ptr_;
64 };
65
66 } // namespace detail
67 } // namespace openstudio
68
69 #endif // UTILITIES_XML_XMLUTILS_HPP
70