/src/libe-book/src/lib/ZVRParser.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 <algorithm> |
11 | | |
12 | | #include <librevenge/librevenge.h> |
13 | | #include <librevenge-stream/librevenge-stream.h> |
14 | | |
15 | | #include "libebook_utils.h" |
16 | | #include "EBOOKMemoryStream.h" |
17 | | #include "ZVRParser.h" |
18 | | |
19 | | using std::shared_ptr; |
20 | | |
21 | | using std::string; |
22 | | |
23 | | namespace libebook |
24 | | { |
25 | | |
26 | | namespace |
27 | | { |
28 | | |
29 | | const char ZVR_SIGNATURE[] = "!!Compressed!!\n"; |
30 | | |
31 | | } |
32 | | |
33 | | ZVRParser::ZVRParser(librevenge::RVNGInputStream *input, librevenge::RVNGTextInterface *document) |
34 | 0 | : m_input(input) |
35 | 0 | , m_document(document) |
36 | 0 | { |
37 | | // ignore the trailing \0 |
38 | 0 | const size_t length = EBOOK_NUM_ELEMENTS(ZVR_SIGNATURE) - 1; |
39 | 0 | input->seek(0, librevenge::RVNG_SEEK_SET); |
40 | 0 | const auto *const sig = reinterpret_cast<const char *>(readNBytes(input, length)); |
41 | 0 | if (!std::equal(sig, sig + length, ZVR_SIGNATURE)) |
42 | 0 | throw UnsupportedFormat(); |
43 | 0 | } |
44 | | |
45 | | bool ZVRParser::parse() |
46 | 0 | { |
47 | 0 | readReplacementTable(); |
48 | |
|
49 | 0 | const std::shared_ptr<librevenge::RVNGInputStream> input = uncompress(); |
50 | |
|
51 | 0 | m_document->startDocument(librevenge::RVNGPropertyList()); |
52 | 0 | m_document->openPageSpan(getDefaultPageSpanPropList()); |
53 | |
|
54 | 0 | writeText(input); |
55 | |
|
56 | 0 | m_document->closePageSpan(); |
57 | 0 | m_document->endDocument(); |
58 | |
|
59 | 0 | return false; |
60 | 0 | } |
61 | | |
62 | | void ZVRParser::readReplacementTable() |
63 | 0 | { |
64 | 0 | for (size_t i = 1; i != EBOOK_NUM_ELEMENTS(m_replacementTable); ++i) |
65 | 0 | { |
66 | 0 | string replacement; |
67 | |
|
68 | 0 | while (!m_input->isEnd()) |
69 | 0 | { |
70 | 0 | unsigned char c = readU8(m_input); |
71 | 0 | if ('\n' == c) |
72 | 0 | { |
73 | 0 | if (replacement.empty()) |
74 | 0 | m_replacementTable[i] = static_cast<char>(i); |
75 | 0 | else |
76 | 0 | { |
77 | 0 | m_replacementTable[i] = replacement; |
78 | 0 | replacement.clear(); |
79 | 0 | } |
80 | 0 | break; |
81 | 0 | } |
82 | 0 | else |
83 | 0 | replacement.push_back(char(c)); |
84 | 0 | } |
85 | 0 | } |
86 | 0 | } |
87 | | |
88 | | shared_ptr<librevenge::RVNGInputStream> ZVRParser::uncompress() |
89 | 0 | { |
90 | 0 | string text; |
91 | |
|
92 | 0 | while (!m_input->isEnd()) |
93 | 0 | { |
94 | 0 | const unsigned char c = readU8(m_input); |
95 | 0 | text.append(m_replacementTable[c]); |
96 | 0 | } |
97 | |
|
98 | 0 | shared_ptr<librevenge::RVNGInputStream> strm(new EBOOKMemoryStream(reinterpret_cast<const unsigned char *>(text.data()), (unsigned) text.size())); |
99 | 0 | return strm; |
100 | 0 | } |
101 | | |
102 | | void ZVRParser::writeText(const shared_ptr<librevenge::RVNGInputStream> input) |
103 | 0 | { |
104 | 0 | string text; |
105 | |
|
106 | 0 | while (!input->isEnd()) |
107 | 0 | { |
108 | 0 | const unsigned char c = readU8(input.get()); |
109 | 0 | if ('\n' == c) |
110 | 0 | { |
111 | 0 | m_document->openParagraph(librevenge::RVNGPropertyList()); |
112 | 0 | if (!text.empty()) |
113 | 0 | { |
114 | 0 | m_document->openSpan(librevenge::RVNGPropertyList()); |
115 | 0 | m_document->insertText(librevenge::RVNGString(text.c_str())); |
116 | 0 | m_document->closeSpan(); |
117 | |
|
118 | 0 | text.clear(); |
119 | 0 | } |
120 | 0 | m_document->closeParagraph(); |
121 | 0 | } |
122 | 0 | else |
123 | 0 | text.push_back((char) c); |
124 | 0 | } |
125 | 0 | } |
126 | | |
127 | | } |
128 | | |
129 | | /* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |