Debian Codegraph @

src/lib/MsWksParser.cxx

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 #include <iomanip>
35 #include <iostream>
36 #include <limits>
37 #include <sstream>
38
39 #include <librevenge/librevenge.h>
40
41
42 #include "MWAWTextListener.hxx"
43 #include "MWAWHeader.hxx"
44 #include "MWAWFont.hxx"
45 #include "MWAWFontConverter.hxx"
46 #include "MWAWPictMac.hxx"
47 #include "MWAWPrinter.hxx"
48 #include "MWAWSubDocument.hxx"
49
50 #include "MsWksGraph.hxx"
51 #include "MsWksDocument.hxx"
52 #include "MsWks3Text.hxx"
53 #include "MsWks4Zone.hxx"
54
55 #include "MsWksParser.hxx"
56
57 /** Internal: the structures of a MsWksParser */
58 namespace MsWksParserInternal
59 {
60 ////////////////////////////////////////
61 //! Internal: the state of a MsWksParser
62 struct State {
63 //! constructor
64 State()
65 : m_mn0Parser()
66 , m_actPage(0)
67 , m_numPages(0)
68 {
69 }
70
71 std::shared_ptr<MsWks4Zone> m_mn0Parser /**parser of main text ole (v4 document)*/;
72 int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */;
73 };
74 }
75
76
77 ////////////////////////////////////////////////////////////
78 // constructor/destructor, ...
79 ////////////////////////////////////////////////////////////
80 MsWksParser::MsWksParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
81 : MWAWTextParser(input, rsrcParser, header)
82 , m_state(new MsWksParserInternal::State)
83 , m_listZones()
84 , m_document()
85 {
86 if (input && input->isStructured()) { // potential v4 file
87 MWAWInputStreamPtr mainOle = getInput()->getSubStreamByName("MN0");
88 if (!mainOle) return;
89
90 m_state->m_mn0Parser.reset(new MsWks4Zone(mainOle, getParserState(), *this, "MN0"));
91 m_document=m_state->m_mn0Parser->m_document;
92 }
93 else
94 m_document.reset(new MsWksDocument(input, *this));
95 m_document->m_newPage=static_cast<MsWksDocument::NewPage>(&MsWksParser::newPage);
96 init();
97 }
98
99 MsWksParser::~MsWksParser()
100 {
101 }
102
103 void MsWksParser::init()
104 {
105 resetTextListener();
106 setAsciiName("main-1");
107
108 // reduce the margin (in case, the page is not defined)
109 getPageSpan().setMargins(0.1);
110 }
111
112 ////////////////////////////////////////////////////////////
113 // new page
114 ////////////////////////////////////////////////////////////
115 void MsWksParser::newPage(int number, bool softBreak)
116 {
117 if (m_state->m_mn0Parser) // v4 document
118 return m_state->m_mn0Parser->newPage(number, softBreak);
119
120 if (number <= m_state->m_actPage || number > m_state->m_numPages)
121 return;
122
123 while (m_state->m_actPage < number) {
124 m_state->m_actPage++;
125 if (!getTextListener() || m_state->m_actPage == 1)
126 continue;
127 if (softBreak)
128 getTextListener()->insertBreak(MWAWTextListener::SoftPageBreak);
129 else
130 getTextListener()->insertBreak(MWAWTextListener::PageBreak);
131 }
132 }
133
134 ////////////////////////////////////////////////////////////
135 // the parser
136 ////////////////////////////////////////////////////////////
137 void MsWksParser::parse(librevenge::RVNGTextInterface *docInterface)
138 {
139 if (!checkHeader(nullptr) || !m_document || !m_document->getInput()) throw(libmwaw::ParseException());
140
141 int const vers=version();
142 bool ok = true;
143 try {
144 // create the asciiFile for v1-v3 document
145 if (vers<=3)
146 m_document->initAsciiFile(asciiName());
147
148 checkHeader(nullptr);
149 ok = createZones();
150 if (ok) {
151 createDocument(docInterface);
152 if (vers<=3)
153 m_document->sendZone(MsWksDocument::Z_MAIN);
154 else
155 m_state->m_mn0Parser->readContentZones(MWAWEntry(), true);
156 flushExtra();
157 }
158 m_document->ascii().reset();
159 }
160 catch (...) {
161 MWAW_DEBUG_MSG(("MsWksParser::parse: exception catched when parsing\n"));
162 ok = false;
163 }
164
165 resetTextListener();
166 if (!ok) throw(libmwaw::ParseException());
167 }
168
169 ////////////////////////////////////////////////////////////
170 // flush the extra data
171 ////////////////////////////////////////////////////////////
172 void MsWksParser::flushExtra()
173 {
174 MWAWTextListenerPtr listener=getTextListener();
175 if (!listener) return;
176
177 if (version()<=3) {
178 m_document->getTextParser3()->flushExtra();
179 m_document->getGraphParser()->flushExtra();
180 return;
181 }
182
183 // send the final data
184 std::vector<std::string> unparsedOLEs=m_document->getUnparsedOLEZones();
185 if (unparsedOLEs.empty()) return;
186
187 bool first = true;
188 for (auto &name : unparsedOLEs) {
189 MWAWInputStreamPtr ole = getInput()->getSubStreamByName(name.c_str());
190 if (!ole.get()) {
191 MWAW_DEBUG_MSG(("MsWksParser::flushExtra: error: can not find OLE part: \"%s\"\n", name.c_str()));
192 continue;
193 }
194
195 std::shared_ptr<MsWks4Zone> newParser(new MsWks4Zone(ole, getParserState(), *this, name));
196 bool ok = true;
197 try {
198 ok = newParser->createZones(false);
199 if (ok) {
200 // FIXME: add a message here
201 if (first) {
202 first = false;
203 listener->setFont(MWAWFont(20,20));
204 librevenge::RVNGString message("--------- The original document has some extra ole: -------- ");
205 listener->insertUnicodeString(message);
206 listener->insertEOL();
207 }
208 newParser->readContentZones(MWAWEntry(), false);
209 }
210 }
211 catch (...) {
212 ok = false;
213 }
214
215 if (ok) continue;
216 MWAW_DEBUG_MSG(("MsWksParser::flushExtra: error: can not parse OLE: \"%s\"\n", name.c_str()));
217 }
218 }
219
220 ////////////////////////////////////////////////////////////
221 // create the document
222 ////////////////////////////////////////////////////////////
223 void MsWksParser::createDocument(librevenge::RVNGTextInterface *documentInterface)
224 {
225 if (!documentInterface || getTextListener()) {
226 MWAW_DEBUG_MSG(("MsWksParser::createDocument: listener already exist\n"));
227 throw (libmwaw::ParseException());
228 }
229
230 if (m_state->m_mn0Parser) {
231 MWAWTextListenerPtr listener = m_state->m_mn0Parser->createListener(documentInterface);
232 if (!listener) {
233 MWAW_DEBUG_MSG(("MsWksParser::parse: can not create a listener\n"));
234 throw (libmwaw::ParseException());
235 }
236 setTextListener(listener);
237 listener->startDocument();
238 return;
239 }
240
241 std::vector<MWAWPageSpan> pageList;
242 m_state->m_actPage = 0;
243 m_document->getPageSpanList(pageList, m_state->m_numPages);
244 MWAWTextListenerPtr listen(new MWAWTextListener(*getParserState(), pageList, documentInterface));
245 setTextListener(listen);
246 listen->startDocument();
247 // time to send page information the graph parser and the text parser
248 m_document->getGraphParser()->setPageLeftTop
249 (MWAWVec2f(72.f*float(getPageSpan().getMarginLeft()),
250 72.f*float(getPageSpan().getMarginTop())+m_document->getHeaderFooterHeight(true)));
251 }
252
253 ////////////////////////////////////////////////////////////
254 //
255 // Intermediate level
256 //
257 ////////////////////////////////////////////////////////////
258 bool MsWksParser::createZones()
259 {
260 MWAWInputStreamPtr input = m_document->getInput();
261 if (!input) return false;
262
263 int const vers=version();
264 if (vers==4) {
265 if (!m_state->m_mn0Parser || !m_state->m_mn0Parser->createZones(true))
266 return false;
267 m_state->m_mn0Parser->m_document->createOLEZones(getInput());
268 return true;
269 }
270
271 long pos = input->tell();
272 if (vers>=3) {
273 bool ok = true;
274 if (m_document->hasHeader())
275 ok = m_document->readGroupHeaderFooter(true,99);
276 if (ok) pos = input->tell();
277 else input->seek(pos, librevenge::RVNG_SEEK_SET);
278 if (ok && m_document->hasFooter())
279 ok = m_document->readGroupHeaderFooter(false,99);
280 if (!ok) input->seek(pos, librevenge::RVNG_SEEK_SET);
281 }
282
283 auto &typeZoneMap=m_document->getTypeZoneMap();
284 auto mainId=int(typeZoneMap.size()); // FIXME: use m_document->getNewZoneId();
285 typeZoneMap.insert(std::multimap<int,MsWksDocument::Zone>::value_type
286 (MsWksDocument::Z_MAIN,MsWksDocument::Zone(MsWksDocument::Z_MAIN, mainId)));
287 auto &mainZone = typeZoneMap.find(MsWksDocument::Z_MAIN)->second;
288
289 libmwaw::DebugFile &ascFile = m_document->ascii();
290 /* now normally
291 in v1: a list of pictures, document info zone, ..., without any way to know the limit
292 in v2: a group zone (which regroup the graphic) and a document info zone
293 in v3: a document info zone and a group zone (the graphic zone)
294 */
295 while (!input->isEnd()) {
296 pos = input->tell();
297 if (!m_document->readZone(mainZone)) {
298 input->seek(pos, librevenge::RVNG_SEEK_SET);
299 break;
300 }
301 if (input->tell() <= pos) {
302 MWAW_DEBUG_MSG(("MsWksParser::createZones: no data were read from zone at pos %ld\n", pos));
303 break;
304 }
305 }
306
307 mainZone.m_textId = m_document->getTextParser3()->createZones(-1, true);
308
309 pos = input->tell();
310
311 if (!input->isEnd()) {
312 ascFile.addPos(pos);
313 ascFile.addNote("Entries(End)");
314 ascFile.addPos(pos+100);
315 ascFile.addNote("_");
316 }
317
318 // ok, prepare the data
319 m_state->m_numPages = 1;
320 std::vector<int> linesH, pagesH;
321 if (m_document->getTextParser3()->getLinesPagesHeight(mainZone.m_textId, linesH, pagesH))
322 m_document->getGraphParser()->computePositions(mainId, linesH, pagesH);
323
324 return true;
325 }
326
327
328 ////////////////////////////////////////////////////////////
329 //
330 // Low level
331 //
332 ////////////////////////////////////////////////////////////
333
334 ////////////////////////////////////////////////////////////
335 // read the header
336 ////////////////////////////////////////////////////////////
337 bool MsWksParser::checkHeader(MWAWHeader *header, bool strict)
338 {
339 MWAWInputStreamPtr &input= getInput();
340 if (!input || !input->hasDataFork() || !m_document)
341 return false;
342
343 if (!input->isStructured()) {
344 // first check v1-v3 file
345 if (!m_document->checkHeader3(header, strict)) return false;
346 if (m_document->getKind() != MWAWDocument::MWAW_K_TEXT)
347 return false;
348 if (version() < 1 || version() > 3)
349 return false;
350 return true;
351 }
352
353 // now check for version 4 file
354 MWAWInputStreamPtr mmOle = input->getSubStreamByName("MM");
355 if (!mmOle) return false;
356 mmOle->seek(0, librevenge::RVNG_SEEK_SET);
357 if (mmOle->readULong(2) != 0x444e) return false;
358
359 MWAWInputStreamPtr mainOle = m_document->getInput();
360 if (!mainOle) return false;
361 mainOle->seek(0, librevenge::RVNG_SEEK_SET);
362 if (mainOle->readULong(4)!=0x43484e4b) // CHNKINK
363 return false;
364 setVersion(4);
365 if (header)
366 header->reset(MWAWDocument::MWAW_T_MICROSOFTWORKS, 4);
367 return true;
368
369 }
370
371 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: