Debian Codegraph @

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