/src/libe-book/src/lib/libebook_xml.cpp
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* |
3 | | * This file is part of the libe-book 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 "libebook_xml.h" |
11 | | |
12 | | #include <cstring> |
13 | | |
14 | | #include <librevenge-stream/librevenge-stream.h> |
15 | | |
16 | | extern "C" |
17 | | { |
18 | | |
19 | | static int ebookXMLReadFromStream(void *context, char *buffer, int len) |
20 | 353k | { |
21 | 353k | if (len<0) |
22 | 0 | return -1; |
23 | 353k | try |
24 | 353k | { |
25 | 353k | auto *const input = reinterpret_cast<librevenge::RVNGInputStream *>(context); |
26 | | |
27 | 353k | unsigned long bytesRead = 0; |
28 | 353k | const unsigned char *const bytes = input->read((unsigned long) len, bytesRead); |
29 | | |
30 | 353k | if (!bytes || !bytesRead) |
31 | 49.4k | return 0; // CHECKME: do we want to return -1 here ? |
32 | | |
33 | 303k | std::memcpy(buffer, bytes, size_t(bytesRead)); |
34 | 303k | return static_cast<int>(bytesRead); |
35 | 353k | } |
36 | 353k | catch (...) |
37 | 353k | { |
38 | 0 | } |
39 | | |
40 | 0 | return -1; |
41 | 353k | } |
42 | | |
43 | | static int ebookXMLCloseStream(void *) |
44 | 73.0k | { |
45 | 73.0k | return 0; |
46 | 73.0k | } |
47 | | |
48 | | } |
49 | | |
50 | | namespace libebook |
51 | | { |
52 | | |
53 | | std::unique_ptr<xmlTextReader, void(*)(xmlTextReaderPtr)> makeXmlReader(librevenge::RVNGInputStream *input) |
54 | 73.0k | { |
55 | 73.0k | return std::unique_ptr<xmlTextReader, void(*)(xmlTextReaderPtr)>( |
56 | 73.0k | xmlReaderForIO(ebookXMLReadFromStream, ebookXMLCloseStream, input, "", nullptr, |
57 | 73.0k | XML_PARSE_NOBLANKS | XML_PARSE_NONET | XML_PARSE_RECOVER), |
58 | 73.0k | xmlFreeTextReader |
59 | 73.0k | ); |
60 | 73.0k | } |
61 | | |
62 | | const char *char_cast(const char *const c) |
63 | 0 | { |
64 | 0 | return c; |
65 | 0 | } |
66 | | |
67 | | const char *char_cast(const signed char *const c) |
68 | 0 | { |
69 | 0 | return reinterpret_cast<const char *>(c); |
70 | 0 | } |
71 | | |
72 | | const char *char_cast(const unsigned char *const c) |
73 | 8.20k | { |
74 | 8.20k | return reinterpret_cast<const char *>(c); |
75 | 8.20k | } |
76 | | |
77 | | const char **char_array_cast(const char **const c) |
78 | 0 | { |
79 | 0 | return c; |
80 | 0 | } |
81 | | |
82 | | const char **char_array_cast(const signed char **const c) |
83 | 0 | { |
84 | 0 | return reinterpret_cast<const char **>(c); |
85 | 0 | } |
86 | | |
87 | | const char **char_array_cast(const unsigned char **const c) |
88 | 0 | { |
89 | 0 | return reinterpret_cast<const char **>(c); |
90 | 0 | } |
91 | | |
92 | | } |
93 | | |
94 | | /* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |