Coverage Report

Created: 2026-06-13 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libmwaw/src/lib/GreatWksBMParser.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 <iomanip>
35
#include <iostream>
36
#include <limits>
37
#include <set>
38
#include <sstream>
39
40
#include <librevenge/librevenge.h>
41
42
#include "MWAWGraphicListener.hxx"
43
#include "MWAWHeader.hxx"
44
#include "MWAWPictData.hxx"
45
#include "MWAWPosition.hxx"
46
47
#include "GreatWksBMParser.hxx"
48
49
/** Internal: the structures of a GreatWksBMParser */
50
namespace GreatWksBMParserInternal
51
{
52
////////////////////////////////////////
53
//! Internal: the state of a GreatWksBMParser
54
struct State {
55
  //! constructor
56
  State()
57
0
    : m_picture()
58
0
  {
59
0
  }
60
  //! the picture entry (v2)
61
  MWAWEntry m_picture;
62
};
63
64
}
65
66
////////////////////////////////////////////////////////////
67
// constructor/destructor, ...
68
////////////////////////////////////////////////////////////
69
GreatWksBMParser::GreatWksBMParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
70
0
  : MWAWGraphicParser(input, rsrcParser, header), m_state()
71
0
{
72
0
  init();
73
0
}
74
75
GreatWksBMParser::~GreatWksBMParser()
76
0
{
77
0
}
78
79
void GreatWksBMParser::init()
80
0
{
81
0
  resetGraphicListener();
82
0
  setAsciiName("main-1");
83
84
0
  m_state.reset(new GreatWksBMParserInternal::State);
85
86
0
  getPageSpan().setMargins(0.1);
87
0
}
88
89
////////////////////////////////////////////////////////////
90
// the parser
91
////////////////////////////////////////////////////////////
92
void GreatWksBMParser::parse(librevenge::RVNGDrawingInterface *docInterface)
93
0
{
94
0
  if (!getInput().get() || !checkHeader(nullptr))  throw(libmwaw::ParseException());
95
0
  bool ok = false;
96
0
  try {
97
    // create the asciiFile
98
0
    ascii().setStream(getInput());
99
0
    ascii().open(asciiName());
100
0
    checkHeader(nullptr);
101
0
    ok = createZones();
102
0
    if (ok) {
103
0
      createDocument(docInterface);
104
0
      sendPicture();
105
0
    }
106
0
    ascii().reset();
107
0
  }
108
0
  catch (...) {
109
0
    MWAW_DEBUG_MSG(("GreatWksBMParser::parse: exception catched when parsing\n"));
110
0
    ok = false;
111
0
  }
112
113
0
  resetGraphicListener();
114
0
  if (!ok) throw(libmwaw::ParseException());
115
0
}
116
117
////////////////////////////////////////////////////////////
118
// create the document
119
////////////////////////////////////////////////////////////
120
void GreatWksBMParser::createDocument(librevenge::RVNGDrawingInterface *documentInterface)
121
0
{
122
0
  if (!documentInterface) return;
123
0
  if (getGraphicListener()) {
124
0
    MWAW_DEBUG_MSG(("GreatWksBMParser::createDocument: listener already exist\n"));
125
0
    return;
126
0
  }
127
128
  // create the page list
129
0
  MWAWPageSpan ps(getPageSpan());
130
0
  ps.setPageSpan(1);
131
0
  std::vector<MWAWPageSpan> pageList(1,ps);
132
0
  MWAWGraphicListenerPtr listen(new MWAWGraphicListener(*getParserState(), pageList, documentInterface));
133
0
  setGraphicListener(listen);
134
0
  listen->startDocument();
135
0
}
136
137
138
////////////////////////////////////////////////////////////
139
//
140
// Intermediate level
141
//
142
////////////////////////////////////////////////////////////
143
bool GreatWksBMParser::createZones()
144
0
{
145
0
  MWAWInputStreamPtr input = getInput();
146
0
  if (input->size()<512) return false;
147
#ifdef DEBUG
148
  libmwaw::DebugStream f;
149
  f << "FileHeader:";
150
  input->seek(0, librevenge::RVNG_SEEK_SET);
151
  for (int i=0; i<256; i++) { // normally 0
152
    auto val=static_cast<int>(input->readLong(2));
153
    if (val)
154
      f << "f" << i << "=" << val << ",";
155
  }
156
  ascii().addPos(0);
157
  ascii().addNote(f.str().c_str());
158
#endif
159
0
  m_state->m_picture.setBegin(512);
160
0
  m_state->m_picture.setEnd(input->size());
161
0
  return true;
162
0
}
163
164
////////////////////////////////////////////////////////////
165
// send data
166
////////////////////////////////////////////////////////////
167
// the picture
168
bool GreatWksBMParser::sendPicture()
169
0
{
170
0
  MWAWListenerPtr listener=getGraphicListener();
171
0
  if (!listener) {
172
0
    MWAW_DEBUG_MSG(("GreatWksBMParser::sendPicture: can not find the listener\n"));
173
0
    return false;
174
0
  }
175
0
  MWAWEntry const &entry=m_state->m_picture;
176
0
  if (!entry.valid()) {
177
0
    MWAW_DEBUG_MSG(("GreatWksBMParser::sendPicture: can not find the picture entry\n"));
178
0
    return false;
179
0
  }
180
181
0
  MWAWInputStreamPtr input = getInput();
182
0
  input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
183
0
  std::shared_ptr<MWAWPict> thePict(MWAWPictData::get(input, static_cast<int>(entry.length())));
184
0
  if (!thePict) {
185
0
    MWAW_DEBUG_MSG(("GreatWksBMParser::sendPicture: can not retrieve the picture\n"));
186
0
    return false;
187
0
  }
188
0
  MWAWEmbeddedObject picture;
189
0
  if (!thePict->getBinary(picture)) {
190
0
    MWAW_DEBUG_MSG(("GreatWksBMParser::sendPicture: can not retrieve the picture data\n"));
191
0
    return false;
192
0
  }
193
0
  MWAWPageSpan const &page=getPageSpan();
194
0
  MWAWPosition pos(MWAWVec2f(float(page.getMarginLeft()),float(page.getMarginRight())),
195
0
                   MWAWVec2f(float(page.getPageWidth()),float(page.getPageLength())), librevenge::RVNG_INCH);
196
0
  pos.setRelativePosition(MWAWPosition::Page);
197
0
  pos.m_wrapping = MWAWPosition::WNone;
198
0
  listener->insertPicture(pos, picture);
199
200
#ifdef DEBUG_WITH_FILES
201
  ascii().skipZone(entry.begin(), entry.end()-1);
202
  librevenge::RVNGBinaryData file;
203
  input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
204
  input->readDataBlock(entry.length(), file);
205
  static int volatile pictName = 0;
206
  libmwaw::DebugStream f;
207
  f << "PICT-" << ++pictName;
208
  libmwaw::Debug::dumpFile(file, f.str().c_str());
209
#endif
210
0
  return true;
211
0
}
212
213
////////////////////////////////////////////////////////////
214
// read the header
215
////////////////////////////////////////////////////////////
216
bool GreatWksBMParser::checkHeader(MWAWHeader *header, bool strict)
217
0
{
218
0
  *m_state = GreatWksBMParserInternal::State();
219
0
  MWAWInputStreamPtr input = getInput();
220
0
  if (!input || !input->hasDataFork() || !input->checkPosition(512+10))
221
0
    return false;
222
223
0
  std::string type, creator;
224
  /** no finder info, may be ok, but this means
225
      that we may have a basic pict file, so... */
226
0
  if (!input->getFinderInfo(type, creator))
227
0
    return false;
228
0
  if (creator!="ZEBR")
229
0
    return false;
230
0
  int vers=2;
231
0
  if (type!="ZPNT")
232
0
    return false;
233
0
  if (strict) {
234
0
    input->seek(512, librevenge::RVNG_SEEK_SET);
235
0
    MWAWBox2f box;
236
0
    if (MWAWPictData::check(input, static_cast<int>(input->size()-512), box)==MWAWPict::MWAW_R_BAD)
237
0
      return false;
238
0
  }
239
0
  setVersion(vers);
240
0
  if (header)
241
0
    header->reset(MWAWDocument::MWAW_T_GREATWORKS, vers, MWAWDocument::MWAW_K_PAINT);
242
243
0
  return true;
244
0
}
245
246
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: