| 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 "MWAWTextListener.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 "RagTime5Parser.hxx" |
| 53 |
|
| 54 |
/** Internal: the structures of a RagTime5Parser */ |
| 55 |
namespace RagTime5ParserInternal |
| 56 |
{ |
| 57 |
|
| 58 |
//////////////////////////////////////// |
| 59 |
//! Internal: the state of a RagTime5Parser |
| 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 RagTime5Parser |
| 73 |
class SubDocument final : public MWAWSubDocument |
| 74 |
{ |
| 75 |
public: |
| 76 |
SubDocument(RagTime5Parser &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(("RagTime5ParserInternal::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(("RagTime5ParserInternal::SubDocument::parse: unknown zone\n")); |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
if (!m_parser) { |
| 120 |
MWAW_DEBUG_MSG(("RagTime5ParserInternal::SubDocument::parse: can not find the parser\n")); |
| 121 |
return; |
| 122 |
} |
| 123 |
MWAW_DEBUG_MSG(("RagTime5ParserInternal::SubDocument::parse: not implemented\n")); |
| 124 |
//static_cast<RagTime5Parser *>(m_parser)->m_document->sendZone(m_id, listener, m_position); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
//////////////////////////////////////////////////////////// |
| 129 |
// constructor/destructor, ... |
| 130 |
//////////////////////////////////////////////////////////// |
| 131 |
RagTime5Parser::RagTime5Parser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header) |
| 132 |
: MWAWTextParser(input, rsrcParser, header) |
| 133 |
, m_state() |
| 134 |
, m_document() |
| 135 |
{ |
| 136 |
init(); |
| 137 |
} |
| 138 |
|
| 139 |
RagTime5Parser::~RagTime5Parser() |
| 140 |
{ |
| 141 |
} |
| 142 |
|
| 143 |
void RagTime5Parser::init() |
| 144 |
{ |
| 145 |
resetTextListener(); |
| 146 |
setAsciiName("main-1"); |
| 147 |
|
| 148 |
m_state.reset(new RagTime5ParserInternal::State); |
| 149 |
m_document.reset(new RagTime5Document(*this)); |
| 150 |
m_document->m_newPage=static_cast<RagTime5Document::NewPage>(&RagTime5Parser::newPage); |
| 151 |
m_document->m_sendFootnote=static_cast<RagTime5Document::SendFootnote>(&RagTime5Parser::sendFootnote); |
| 152 |
// reduce the margin (in case, the page is not defined) |
| 153 |
getPageSpan().setMargins(0.1); |
| 154 |
} |
| 155 |
|
| 156 |
//////////////////////////////////////////////////////////// |
| 157 |
// new page |
| 158 |
//////////////////////////////////////////////////////////// |
| 159 |
void RagTime5Parser::newPage(int number, bool soft) |
| 160 |
{ |
| 161 |
if (number <= m_state->m_actPage || number > m_state->m_numPages) |
| 162 |
return; |
| 163 |
|
| 164 |
while (m_state->m_actPage < number) { |
| 165 |
m_state->m_actPage++; |
| 166 |
if (!getTextListener() || m_state->m_actPage == 1) |
| 167 |
continue; |
| 168 |
if (soft) |
| 169 |
getTextListener()->insertBreak(MWAWTextListener::SoftPageBreak); |
| 170 |
else |
| 171 |
getTextListener()->insertBreak(MWAWTextListener::PageBreak); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
//////////////////////////////////////////////////////////// |
| 176 |
// interface with the different parser |
| 177 |
//////////////////////////////////////////////////////////// |
| 178 |
void RagTime5Parser::sendFootnote(int zoneId) |
| 179 |
{ |
| 180 |
if (!getTextListener()) return; |
| 181 |
MWAWSubDocumentPtr subdoc(new RagTime5ParserInternal::SubDocument(*this, getInput(), zoneId)); |
| 182 |
getTextListener()->insertNote(MWAWNote(MWAWNote::FootNote), subdoc); |
| 183 |
} |
| 184 |
|
| 185 |
bool RagTime5Parser::checkHeader(MWAWHeader *header, bool strict) |
| 186 |
{ |
| 187 |
*m_state = RagTime5ParserInternal::State(); |
| 188 |
if (!m_document->checkHeader(header, strict)) |
| 189 |
return false; |
| 190 |
return getParserState()->m_kind==MWAWDocument::MWAW_K_TEXT || |
| 191 |
getParserState()->m_kind==MWAWDocument::MWAW_K_SPREADSHEET; |
| 192 |
} |
| 193 |
|
| 194 |
//////////////////////////////////////////////////////////// |
| 195 |
// the parser |
| 196 |
//////////////////////////////////////////////////////////// |
| 197 |
void RagTime5Parser::parse(librevenge::RVNGTextInterface *docInterface) |
| 198 |
{ |
| 199 |
if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException()); |
| 200 |
bool ok = true; |
| 201 |
try { |
| 202 |
// create the asciiFile |
| 203 |
ascii().setStream(getInput()); |
| 204 |
ascii().open(asciiName()); |
| 205 |
|
| 206 |
checkHeader(nullptr); |
| 207 |
ok = m_document->createZones(); |
| 208 |
if (ok) { |
| 209 |
createDocument(docInterface); |
| 210 |
m_document->sendZones(getMainListener()); |
| 211 |
#ifdef DEBUG |
| 212 |
m_document->flushExtra(getMainListener()); |
| 213 |
#endif |
| 214 |
} |
| 215 |
ascii().reset(); |
| 216 |
} |
| 217 |
catch (...) { |
| 218 |
MWAW_DEBUG_MSG(("RagTime5Parser::parse: exception catched when parsing\n")); |
| 219 |
ok = false; |
| 220 |
} |
| 221 |
|
| 222 |
resetTextListener(); |
| 223 |
if (!ok) throw(libmwaw::ParseException()); |
| 224 |
} |
| 225 |
|
| 226 |
//////////////////////////////////////////////////////////// |
| 227 |
// create the document |
| 228 |
//////////////////////////////////////////////////////////// |
| 229 |
void RagTime5Parser::createDocument(librevenge::RVNGTextInterface *documentInterface) |
| 230 |
{ |
| 231 |
if (!documentInterface) return; |
| 232 |
if (getTextListener()) { |
| 233 |
MWAW_DEBUG_MSG(("RagTime5Parser::createDocument: listener already exist\n")); |
| 234 |
return; |
| 235 |
} |
| 236 |
|
| 237 |
// update the page |
| 238 |
m_state->m_actPage = 0; |
| 239 |
m_state->m_numPages = m_document->numPages(); |
| 240 |
|
| 241 |
// create the page list |
| 242 |
std::vector<MWAWPageSpan> pageList; |
| 243 |
m_document->updatePageSpanList(pageList); |
| 244 |
// |
| 245 |
MWAWTextListenerPtr listen(new MWAWTextListener(*getParserState(), pageList, documentInterface)); |
| 246 |
setTextListener(listen); |
| 247 |
listen->setDocumentMetaData(m_document->getDocumentMetaData()); |
| 248 |
listen->startDocument(); |
| 249 |
} |
| 250 |
|
| 251 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |