Coverage Report

Created: 2026-07-20 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libmwaw/src/lib/ClarisWksParser.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 "MWAWTextListener.hxx"
45
#include "MWAWFont.hxx"
46
#include "MWAWFontConverter.hxx"
47
#include "MWAWHeader.hxx"
48
#include "MWAWPosition.hxx"
49
#include "MWAWPictMac.hxx"
50
#include "MWAWPrinter.hxx"
51
#include "MWAWSection.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 "ClarisWksParser.hxx"
64
65
/** Internal: the structures of a ClarisWksParser */
66
namespace ClarisWksParserInternal
67
{
68
69
////////////////////////////////////////
70
//! Internal: the state of a ClarisWksParser
71
struct State {
72
  //! constructor
73
  State()
74
809k
    : m_actPage(0)
75
809k
    , m_numPages(0)
76
809k
  {
77
809k
  }
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 ClarisWksParser
84
class SubDocument final : public MWAWSubDocument
85
{
86
public:
87
  SubDocument(ClarisWksParser &pars, MWAWInputStreamPtr const &input, int zoneId, MWAWPosition const &pos=MWAWPosition())
88
4.95M
    : MWAWSubDocument(&pars, input, MWAWEntry())
89
4.95M
    , m_id(zoneId)
90
4.95M
    , m_position(pos) {}
91
92
  //! destructor
93
4.95M
  ~SubDocument() final {}
94
95
  //! operator!=
96
  bool operator!=(MWAWSubDocument const &doc) const final
97
236k
  {
98
236k
    if (MWAWSubDocument::operator!=(doc)) return true;
99
234k
    auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
100
234k
    if (!sDoc) return true;
101
0
    if (m_id != sDoc->m_id) return true;
102
0
    return false;
103
0
  }
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
752k
{
117
752k
  if (!listener.get()) {
118
0
    MWAW_DEBUG_MSG(("ClarisWksParserInternal::SubDocument::parse: no listener\n"));
119
0
    return;
120
0
  }
121
752k
  if (m_id == -1) { // a number used to send linked frame
122
0
    listener->insertChar(' ');
123
0
    return;
124
0
  }
125
752k
  if (m_id == 0) {
126
0
    MWAW_DEBUG_MSG(("ClarisWksParserInternal::SubDocument::parse: unknown zone\n"));
127
0
    return;
128
0
  }
129
130
752k
  if (!m_parser) {
131
0
    MWAW_DEBUG_MSG(("ClarisWksParserInternal::SubDocument::parse: can not find the parser\n"));
132
0
    return;
133
0
  }
134
752k
  static_cast<ClarisWksParser *>(m_parser)->m_document->sendZone(m_id, listener, m_position);
135
752k
}
136
}
137
138
////////////////////////////////////////////////////////////
139
// constructor/destructor, ...
140
////////////////////////////////////////////////////////////
141
ClarisWksParser::ClarisWksParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
142
327k
  : MWAWTextParser(input, rsrcParser, header)
143
327k
  , m_state()
144
327k
  , m_document()
145
327k
{
146
327k
  init();
147
327k
}
148
149
ClarisWksParser::~ClarisWksParser()
150
327k
{
151
327k
}
152
153
void ClarisWksParser::init()
154
327k
{
155
327k
  resetTextListener();
156
327k
  setAsciiName("main-1");
157
158
327k
  m_state.reset(new ClarisWksParserInternal::State);
159
327k
  m_document.reset(new ClarisWksDocument(*this));
160
327k
  m_document->m_newPage=static_cast<ClarisWksDocument::NewPage>(&ClarisWksParser::newPage);
161
327k
  m_document->m_sendFootnote=static_cast<ClarisWksDocument::SendFootnote>(&ClarisWksParser::sendFootnote);
162
  // reduce the margin (in case, the page is not defined)
163
327k
  getPageSpan().setMargins(0.1);
164
327k
}
165
166
////////////////////////////////////////////////////////////
167
// new page
168
////////////////////////////////////////////////////////////
169
void ClarisWksParser::newPage(int number, bool soft)
170
210k
{
171
210k
  if (number <= m_state->m_actPage || number > m_state->m_numPages)
172
46.6k
    return;
173
174
158M
  while (m_state->m_actPage < number) {
175
158M
    m_state->m_actPage++;
176
158M
    if (!getTextListener() || m_state->m_actPage == 1)
177
139k
      continue;
178
158M
    if (soft)
179
0
      getTextListener()->insertBreak(MWAWTextListener::SoftPageBreak);
180
158M
    else
181
158M
      getTextListener()->insertBreak(MWAWTextListener::PageBreak);
182
158M
  }
183
163k
}
184
185
////////////////////////////////////////////////////////////
186
// interface with the different parser
187
////////////////////////////////////////////////////////////
188
void ClarisWksParser::sendFootnote(int zoneId)
189
4.95M
{
190
4.95M
  if (!getTextListener()) return;
191
192
4.95M
  MWAWSubDocumentPtr subdoc(new ClarisWksParserInternal::SubDocument(*this, getInput(), zoneId));
193
4.95M
  getTextListener()->insertNote(MWAWNote(MWAWNote::FootNote), subdoc);
194
4.95M
}
195
196
bool ClarisWksParser::checkHeader(MWAWHeader *header, bool strict)
197
482k
{
198
482k
  *m_state = ClarisWksParserInternal::State();
199
482k
  if (!m_document->checkHeader(header, strict))
200
115
    return false;
201
482k
  return getParserState()->m_kind==MWAWDocument::MWAW_K_TEXT ||
202
420k
         getParserState()->m_kind==MWAWDocument::MWAW_K_DRAW;
203
482k
}
204
205
////////////////////////////////////////////////////////////
206
// the parser
207
////////////////////////////////////////////////////////////
208
void ClarisWksParser::parse(librevenge::RVNGTextInterface *docInterface)
209
154k
{
210
154k
  if (!getInput().get() || !checkHeader(nullptr))  throw(libmwaw::ParseException());
211
154k
  bool ok = true;
212
154k
  try {
213
    // create the asciiFile
214
154k
    ascii().setStream(getInput());
215
154k
    ascii().open(asciiName());
216
217
154k
    checkHeader(nullptr);
218
154k
    ok = m_document->createZones();
219
154k
    if (ok) {
220
139k
      createDocument(docInterface);
221
139k
      auto const &mainZonesList=m_document->getMainZonesList();
222
139k
      if (getParserState()->m_kind==MWAWDocument::MWAW_K_DRAW) {
223
153M
        for (int i=0; i<m_state->m_numPages; ++i)
224
153M
          m_document->getGraphParser()->sendMaster(i);
225
121k
      }
226
139k
      for (auto const zone : mainZonesList)
227
443k
        m_document->getGraphParser()->sendPageGraphics(zone);
228
139k
      if (getParserState()->m_kind==MWAWDocument::MWAW_K_TEXT) {
229
18.2k
        auto mainMap = m_document->getZone(1);
230
18.2k
        if (mainMap && mainMap->m_fileType==1 && !mainMap->m_parsed)
231
7.62k
          m_document->sendZone(1, MWAWListenerPtr(), MWAWPosition());
232
10.6k
        else {
233
10.6k
          MWAW_DEBUG_MSG(("ClarisWksParser::parse: find a problem with the main text zones\n"));
234
10.6k
        }
235
18.2k
      }
236
139k
      newPage(m_state->m_numPages, false);
237
#ifdef DEBUG
238
      m_document->getGraphParser()->flushExtra();
239
#endif
240
139k
      m_document->getTableParser()->flushExtra();
241
139k
      m_document->getTextParser()->flushExtra();
242
139k
    }
243
154k
    ascii().reset();
244
154k
  }
245
154k
  catch (...) {
246
49
    MWAW_DEBUG_MSG(("ClarisWksParser::parse: exception catched when parsing\n"));
247
49
    ok = false;
248
49
  }
249
250
154k
  resetTextListener();
251
154k
  if (!ok) throw(libmwaw::ParseException());
252
154k
}
253
254
////////////////////////////////////////////////////////////
255
// create the document
256
////////////////////////////////////////////////////////////
257
void ClarisWksParser::createDocument(librevenge::RVNGTextInterface *documentInterface)
258
139k
{
259
139k
  if (!documentInterface) return;
260
139k
  if (getTextListener()) {
261
0
    MWAW_DEBUG_MSG(("ClarisWksParser::createDocument: listener already exist\n"));
262
0
    return;
263
0
  }
264
265
  // update the page
266
139k
  m_state->m_actPage = 0;
267
139k
  m_state->m_numPages = m_document->numPages();
268
269
  // create the page list
270
139k
  std::vector<MWAWPageSpan> pageList;
271
139k
  m_document->updatePageSpanList(pageList);
272
  //
273
139k
  MWAWTextListenerPtr listen(new MWAWTextListener(*getParserState(), pageList, documentInterface));
274
139k
  setTextListener(listen);
275
139k
  listen->setDocumentMetaData(m_document->getDocumentMetaData());
276
139k
  listen->startDocument();
277
139k
}
278
279
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: