Debian Codegraph @

src/lib/ABWXMLHelper.cpp

raw
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libabw project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10 #include <string.h>
11 #include <libxml/xmlIO.h>
12 #include <libxml/xmlmemory.h>
13 #include <librevenge-stream/librevenge-stream.h>
14 #include "ABWXMLHelper.h"
15 #include "libabw_internal.h"
16
17 namespace libabw
18 {
19
20 namespace
21 {
22
23 extern "C" {
24
25 static int abwxmlInputCloseFunc(void *)
26 {
27 return 0;
28 }
29
30 static int abwxmlInputReadFunc(void *context, char *buffer, int len)
31 {
32 auto *input = (librevenge::RVNGInputStream *)context;
33
34 if ((!input) || (!buffer) || (len < 0))
35 return -1;
36
37 if (input->isEnd())
38 return 0;
39
40 unsigned long tmpNumBytesRead = 0;
41 const unsigned char *tmpBuffer = input->read((unsigned long) len, tmpNumBytesRead);
42
43 if (tmpBuffer && tmpNumBytesRead)
44 memcpy(buffer, tmpBuffer, tmpNumBytesRead);
45 return int(tmpNumBytesRead);
46 }
47
48 #ifdef DEBUG
49 static void abwxmlReaderErrorFunc(void *arg, const char *message, xmlParserSeverities severity, xmlTextReaderLocatorPtr)
50 #else
51 static void abwxmlReaderErrorFunc(void *arg, const char *, xmlParserSeverities severity, xmlTextReaderLocatorPtr)
52 #endif
53 {
54 const auto watcher = reinterpret_cast<ABWXMLProgressWatcher *>(arg);
55 switch (severity)
56 {
57 case XML_PARSER_SEVERITY_VALIDITY_WARNING:
58 ABW_DEBUG_MSG(("Found xml parser severity validity warning %s\n", message));
59 break;
60 case XML_PARSER_SEVERITY_VALIDITY_ERROR:
61 ABW_DEBUG_MSG(("Found xml parser severity validity error %s\n", message));
62 break;
63 case XML_PARSER_SEVERITY_WARNING:
64 ABW_DEBUG_MSG(("Found xml parser severity warning %s\n", message));
65 break;
66 case XML_PARSER_SEVERITY_ERROR:
67 ABW_DEBUG_MSG(("Found xml parser severity error %s\n", message));
68 if (watcher)
69 watcher->signalError();
70 break;
71 default:
72 break;
73 }
74 }
75
76 } // extern "C"
77
78 } // anonymous namespace
79
80 ABWXMLString::ABWXMLString(xmlChar *xml)
81 : m_xml(xml, xmlFree)
82 {
83 }
84
85 const xmlChar *ABWXMLString::get() const
86 {
87 return m_xml.get();
88 }
89
90 ABWXMLString::operator const char *() const
91 {
92 return reinterpret_cast<const char *>(m_xml.get());
93 }
94
95 ABWXMLProgressWatcher::ABWXMLProgressWatcher()
96 : m_reader(nullptr)
97 , m_line(0)
98 , m_col(0)
99 , m_wasError(false)
100 , m_isStuck(false)
101 {
102 }
103
104 void ABWXMLProgressWatcher::setReader(xmlTextReaderPtr reader)
105 {
106 m_reader = reader;
107 }
108
109 bool ABWXMLProgressWatcher::isStuck() const
110 {
111 return m_isStuck;
112 }
113
114 void ABWXMLProgressWatcher::signalError()
115 {
116 if (m_reader && !m_isStuck)
117 {
118 const int line = m_line;
119 const int col = m_col;
120 const bool checkStuck = m_wasError;
121 m_wasError = true;
122 m_line = xmlTextReaderGetParserLineNumber(m_reader);
123 m_col = xmlTextReaderGetParserColumnNumber(m_reader);
124 if (checkStuck)
125 m_isStuck = line == m_line && col == m_col;
126 }
127 }
128
129 // xmlTextReader helper function
130
131 std::unique_ptr<xmlTextReader, void(*)(xmlTextReaderPtr)> xmlReaderForStream(librevenge::RVNGInputStream *input, ABWXMLProgressWatcher *watcher)
132 {
133 std::unique_ptr<xmlTextReader, void(*)(xmlTextReaderPtr)> reader(
134 xmlReaderForIO(abwxmlInputReadFunc, abwxmlInputCloseFunc, (void *)input, nullptr, nullptr,
135 XML_PARSE_NOBLANKS|XML_PARSE_NONET|XML_PARSE_RECOVER),
136 xmlFreeTextReader);
137 if (watcher)
138 watcher->setReader(reader.get());
139 if (reader)
140 xmlTextReaderSetErrorHandler(reader.get(), abwxmlReaderErrorFunc, watcher);
141 return reader;
142 }
143
144 }
145
146 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */