/src/libmwaw/src/lib/RagTime5SSParser.cxx
Line | Count | Source |
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 (wrlach@gmail.com) |
18 | | * Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net) |
19 | | * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch) |
20 | | * Copyright (C) 2006, 2007 Andrew Ziem |
21 | | * Copyright (C) 2011, 2012 Alonso Laurent (alonso@loria.fr) |
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 | 0 | : m_actPage(0) |
64 | 0 | , m_numPages(0) |
65 | 0 | { |
66 | 0 | } |
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 | 0 | : MWAWSubDocument(&pars, input, MWAWEntry()) |
78 | 0 | , m_id(zoneId) |
79 | 0 | , m_position(pos) {} |
80 | | |
81 | | //! destructor |
82 | 0 | ~SubDocument() final {} |
83 | | |
84 | | //! operator!= |
85 | | bool operator!=(MWAWSubDocument const &doc) const final |
86 | 0 | { |
87 | 0 | if (MWAWSubDocument::operator!=(doc)) return true; |
88 | 0 | auto const *sDoc = dynamic_cast<SubDocument const *>(&doc); |
89 | 0 | if (!sDoc) return true; |
90 | 0 | if (m_id != sDoc->m_id) return true; |
91 | 0 | return false; |
92 | 0 | } |
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 | 0 | { |
106 | 0 | if (!listener.get()) { |
107 | 0 | MWAW_DEBUG_MSG(("RagTime5SSParserInternal::SubDocument::parse: no listener\n")); |
108 | 0 | return; |
109 | 0 | } |
110 | 0 | if (m_id == -1) { // a number used to send linked frame |
111 | 0 | listener->insertChar(' '); |
112 | 0 | return; |
113 | 0 | } |
114 | 0 | if (m_id == 0) { |
115 | 0 | MWAW_DEBUG_MSG(("RagTime5SSParserInternal::SubDocument::parse: unknown zone\n")); |
116 | 0 | return; |
117 | 0 | } |
118 | | |
119 | 0 | if (!m_parser) { |
120 | 0 | MWAW_DEBUG_MSG(("RagTime5SSParserInternal::SubDocument::parse: can not find the parser\n")); |
121 | 0 | return; |
122 | 0 | } |
123 | 0 | MWAW_DEBUG_MSG(("RagTime5SSParserInternal::SubDocument::parse: not implemented\n")); |
124 | | //static_cast<RagTime5SSParser *>(m_parser)->m_document->sendZone(m_id, listener, m_position); |
125 | 0 | } |
126 | | } |
127 | | |
128 | | //////////////////////////////////////////////////////////// |
129 | | // constructor/destructor, ... |
130 | | //////////////////////////////////////////////////////////// |
131 | | RagTime5SSParser::RagTime5SSParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header) |
132 | 0 | : MWAWSpreadsheetParser(input, rsrcParser, header) |
133 | 0 | , m_state() |
134 | 0 | , m_document() |
135 | 0 | { |
136 | 0 | init(); |
137 | 0 | } |
138 | | |
139 | | RagTime5SSParser::~RagTime5SSParser() |
140 | 0 | { |
141 | 0 | } |
142 | | |
143 | | void RagTime5SSParser::init() |
144 | 0 | { |
145 | 0 | resetSpreadsheetListener(); |
146 | 0 | setAsciiName("main-1"); |
147 | |
|
148 | 0 | m_state.reset(new RagTime5SSParserInternal::State); |
149 | 0 | m_document.reset(new RagTime5Document(*this)); |
150 | 0 | m_document->m_sendFootnote=static_cast<RagTime5Document::SendFootnote>(&RagTime5SSParser::sendFootnote); |
151 | | // reduce the margin (in case, the page is not defined) |
152 | 0 | getPageSpan().setMargins(0.1); |
153 | 0 | } |
154 | | |
155 | | //////////////////////////////////////////////////////////// |
156 | | // interface with the different parser |
157 | | //////////////////////////////////////////////////////////// |
158 | | void RagTime5SSParser::sendFootnote(int zoneId) |
159 | 0 | { |
160 | 0 | if (!getSpreadsheetListener()) return; |
161 | 0 | MWAWSubDocumentPtr subdoc(new RagTime5SSParserInternal::SubDocument(*this, getInput(), zoneId)); |
162 | 0 | getSpreadsheetListener()->insertNote(MWAWNote(MWAWNote::FootNote), subdoc); |
163 | 0 | } |
164 | | |
165 | | bool RagTime5SSParser::checkHeader(MWAWHeader *header, bool strict) |
166 | 0 | { |
167 | 0 | *m_state = RagTime5SSParserInternal::State(); |
168 | 0 | if (!m_document->checkHeader(header, strict)) |
169 | 0 | return false; |
170 | 0 | return getParserState()->m_kind==MWAWDocument::MWAW_K_SPREADSHEET; |
171 | 0 | } |
172 | | |
173 | | //////////////////////////////////////////////////////////// |
174 | | // the parser |
175 | | //////////////////////////////////////////////////////////// |
176 | | void RagTime5SSParser::parse(librevenge::RVNGSpreadsheetInterface *docInterface) |
177 | 0 | { |
178 | 0 | if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException()); |
179 | 0 | bool ok = true; |
180 | 0 | try { |
181 | | // create the asciiFile |
182 | 0 | ascii().setStream(getInput()); |
183 | 0 | ascii().open(asciiName()); |
184 | |
|
185 | 0 | checkHeader(nullptr); |
186 | 0 | ok = m_document->createZones(); |
187 | 0 | if (ok) { |
188 | 0 | createDocument(docInterface); |
189 | 0 | m_document->sendSpreadsheet(getMainListener()); |
190 | | #ifdef DEBUG |
191 | | m_document->flushExtra(getMainListener(), true); |
192 | | #endif |
193 | 0 | } |
194 | 0 | ascii().reset(); |
195 | 0 | } |
196 | 0 | catch (...) { |
197 | 0 | MWAW_DEBUG_MSG(("RagTime5SSParser::parse: exception catched when parsing\n")); |
198 | 0 | ok = false; |
199 | 0 | } |
200 | |
|
201 | 0 | resetSpreadsheetListener(); |
202 | 0 | if (!ok) throw(libmwaw::ParseException()); |
203 | 0 | } |
204 | | |
205 | | //////////////////////////////////////////////////////////// |
206 | | // create the document |
207 | | //////////////////////////////////////////////////////////// |
208 | | void RagTime5SSParser::createDocument(librevenge::RVNGSpreadsheetInterface *documentInterface) |
209 | 0 | { |
210 | 0 | if (!documentInterface) return; |
211 | 0 | if (getSpreadsheetListener()) { |
212 | 0 | MWAW_DEBUG_MSG(("RagTime5SSParser::createDocument: listener already exist\n")); |
213 | 0 | return; |
214 | 0 | } |
215 | | |
216 | | // update the page |
217 | 0 | m_state->m_actPage = 0; |
218 | 0 | m_state->m_numPages = 1; |
219 | | |
220 | | // create the page list |
221 | 0 | std::vector<MWAWPageSpan> pageList; |
222 | 0 | m_document->updatePageSpanList(pageList); |
223 | | // |
224 | 0 | MWAWSpreadsheetListenerPtr listen(new MWAWSpreadsheetListener(*getParserState(), pageList, documentInterface)); |
225 | 0 | setSpreadsheetListener(listen); |
226 | 0 | listen->setDocumentMetaData(m_document->getDocumentMetaData()); |
227 | 0 | listen->startDocument(); |
228 | 0 | } |
229 | | |
230 | | // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |