Debian Codegraph @

src/lib/ClarisWksSSParser.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 <algorithm>
35 #include <cstring>
36 #include <iomanip>
37 #include <iostream>
38 #include <limits>
39 #include <map>
40 #include <sstream>
41
42 #include <librevenge/librevenge.h>
43
44 #include "MWAWFont.hxx"
45 #include "MWAWFontConverter.hxx"
46 #include "MWAWHeader.hxx"
47 #include "MWAWPosition.hxx"
48 #include "MWAWPictMac.hxx"
49 #include "MWAWPrinter.hxx"
50 #include "MWAWSection.hxx"
51 #include "MWAWSpreadsheetListener.hxx"
52
53 #include "ClarisWksDatabase.hxx"
54 #include "ClarisWksDocument.hxx"
55 #include "ClarisWksGraph.hxx"
56 #include "ClarisWksPresentation.hxx"
57 #include "ClarisWksSpreadsheet.hxx"
58 #include "ClarisWksStruct.hxx"
59 #include "ClarisWksStyleManager.hxx"
60 #include "ClarisWksTable.hxx"
61 #include "ClarisWksText.hxx"
62
63 #include "ClarisWksSSParser.hxx"
64
65 /** Internal: the structures of a ClarisWksSSParser */
66 namespace ClarisWksSSParserInternal
67 {
68
69 ////////////////////////////////////////
70 //! Internal: the state of a ClarisWksSSParser
71 struct State {
72 //! constructor
73 State()
74 : m_actPage(0)
75 , m_numPages(0)
76 {
77 }
78
79 int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */;
80 };
81
82 ////////////////////////////////////////
83 //! Internal: the subdocument of a ClarisWksSSParser
84 class SubDocument final : public MWAWSubDocument
85 {
86 public:
87 SubDocument(ClarisWksSSParser &pars, MWAWInputStreamPtr const &input, int zoneId, MWAWPosition const &pos=MWAWPosition())
88 : MWAWSubDocument(&pars, input, MWAWEntry())
89 , m_id(zoneId)
90 , m_position(pos) {}
91
92 //! destructor
93 ~SubDocument() final {}
94
95 //! operator!=
96 bool operator!=(MWAWSubDocument const &doc) const final
97 {
98 if (MWAWSubDocument::operator!=(doc)) return true;
99 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
100 if (!sDoc) return true;
101 if (m_id != sDoc->m_id) return true;
102 return false;
103 }
104
105 //! the parser function
106 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
107
108 protected:
109 //! the subdocument id
110 int m_id;
111 //! the subdocument position if defined
112 MWAWPosition m_position;
113 };
114
115 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType)
116 {
117 if (!listener.get()) {
118 MWAW_DEBUG_MSG(("ClarisWksSSParserInternal::SubDocument::parse: no listener\n"));
119 return;
120 }
121 if (m_id == -1) { // a number used to send linked frame
122 listener->insertChar(' ');
123 return;
124 }
125 if (m_id == 0) {
126 MWAW_DEBUG_MSG(("ClarisWksSSParserInternal::SubDocument::parse: unknown zone\n"));
127 return;
128 }
129 auto *parser=dynamic_cast<ClarisWksSSParser *>(m_parser);
130 if (!parser) {
131 MWAW_DEBUG_MSG(("ClarisWksSSParserInternal::SubDocument::parse: can not find parser\n"));
132 return;
133 }
134 parser->m_document->sendZone(m_id, listener, m_position);
135 }
136 }
137
138 ////////////////////////////////////////////////////////////
139 // constructor/destructor, ...
140 ////////////////////////////////////////////////////////////
141 ClarisWksSSParser::ClarisWksSSParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
142 : MWAWSpreadsheetParser(input, rsrcParser, header)
143 , m_state()
144 , m_document()
145 {
146 init();
147 }
148
149 ClarisWksSSParser::~ClarisWksSSParser()
150 {
151 }
152
153 void ClarisWksSSParser::init()
154 {
155 resetSpreadsheetListener();
156 setAsciiName("main-1");
157
158 m_state.reset(new ClarisWksSSParserInternal::State);
159 m_document.reset(new ClarisWksDocument(*this));
160 m_document->m_sendFootnote=static_cast<ClarisWksDocument::SendFootnote>(&ClarisWksSSParser::sendFootnote);
161 // reduce the margin (in case, the page is not defined)
162 getPageSpan().setMargins(0.1);
163 }
164
165 ////////////////////////////////////////////////////////////
166 // interface with the different parser
167 ////////////////////////////////////////////////////////////
168 void ClarisWksSSParser::sendFootnote(int zoneId)
169 {
170 if (!getSpreadsheetListener()) return;
171
172 MWAWSubDocumentPtr subdoc(new ClarisWksSSParserInternal::SubDocument(*this, getInput(), zoneId));
173 getSpreadsheetListener()->insertNote(MWAWNote(MWAWNote::FootNote), subdoc);
174 }
175
176 bool ClarisWksSSParser::checkHeader(MWAWHeader *header, bool strict)
177 {
178 *m_state = ClarisWksSSParserInternal::State();
179 if (!m_document->checkHeader(header, strict))
180 return false;
181 return getParserState()->m_kind==MWAWDocument::MWAW_K_SPREADSHEET ||
182 getParserState()->m_kind==MWAWDocument::MWAW_K_DATABASE;
183 }
184
185 ////////////////////////////////////////////////////////////
186 // the parser
187 ////////////////////////////////////////////////////////////
188 void ClarisWksSSParser::parse(librevenge::RVNGSpreadsheetInterface *docInterface)
189 {
190 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
191 bool ok = true;
192 try {
193 // create the asciiFile
194 ascii().setStream(getInput());
195 ascii().open(asciiName());
196
197 checkHeader(nullptr);
198 ok = m_document->createZones();
199 // check that we have at least read the main zone
200 if (ok) {
201 auto zMap = m_document->getZone(1);
202 if (!zMap)
203 ok = false;
204 else if (getParserState()->m_kind==MWAWDocument::MWAW_K_SPREADSHEET)
205 ok = zMap->m_fileType==2;
206 else
207 ok = zMap->m_fileType==3;
208 }
209 if (ok) {
210 createDocument(docInterface);
211 m_document->sendZone(1);
212 }
213 ascii().reset();
214 }
215 catch (...) {
216 MWAW_DEBUG_MSG(("ClarisWksSSParser::parse: exception catched when parsing\n"));
217 ok = false;
218 }
219
220 resetSpreadsheetListener();
221 if (!ok) throw(libmwaw::ParseException());
222 }
223
224 ////////////////////////////////////////////////////////////
225 // create the document
226 ////////////////////////////////////////////////////////////
227 void ClarisWksSSParser::createDocument(librevenge::RVNGSpreadsheetInterface *documentInterface)
228 {
229 if (!documentInterface) return;
230 if (getSpreadsheetListener()) {
231 MWAW_DEBUG_MSG(("ClarisWksSSParser::createDocument: listener already exist\n"));
232 return;
233 }
234
235 m_document->m_graphParser->computePositions();
236
237 // update the page
238 m_state->m_actPage = 0;
239 m_state->m_numPages=1;
240
241 std::vector<MWAWPageSpan> pageList;
242 m_document->updatePageSpanList(pageList);
243 //
244 MWAWSpreadsheetListenerPtr listen(new MWAWSpreadsheetListener(*getParserState(), pageList, documentInterface));
245 setSpreadsheetListener(listen);
246 listen->setDocumentMetaData(m_document->getDocumentMetaData());
247 listen->startDocument();
248 }
249
250 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: