Debian Codegraph @

src/lib/WingzParser.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 <cstring>
35 #include <iomanip>
36 #include <iostream>
37 #include <limits>
38 #include <map>
39 #include <sstream>
40
41 #include <librevenge/librevenge.h>
42
43 #include "MWAWCell.hxx"
44 #include "MWAWFont.hxx"
45 #include "MWAWFontConverter.hxx"
46 #include "MWAWHeader.hxx"
47 #include "MWAWParagraph.hxx"
48 #include "MWAWPosition.hxx"
49 #include "MWAWPictMac.hxx"
50 #include "MWAWPrinter.hxx"
51 #include "MWAWSpreadsheetListener.hxx"
52 #include "MWAWStringStream.hxx"
53
54 #include "WingzGraph.hxx"
55
56 #include "WingzParser.hxx"
57
58 /** Internal: the structures of a WingzParser */
59 namespace WingzParserInternal
60 {
61
62 ////////////////////////////////////////
63 //! Internal: the cell style of a WingzParser
64 struct Style {
65 //! constructor
66 Style()
67 : m_font()
68 , m_backgroundColor(MWAWColor::white())
69 , m_lineColor(MWAWColor::black())
70 , m_format()
71 {
72 }
73 //! the font
74 MWAWFont m_font;
75 //! the cell background color
76 MWAWColor m_backgroundColor;
77 //! the line color (if needed)
78 MWAWColor m_lineColor;
79 //! the cell custom format (if known)
80 std::string m_format;
81 };
82
83 //! Internal: the cell of a WingzParser
84 struct Cell final : public MWAWCell {
85 //! constructor
86 explicit Cell(MWAWVec2i pos=MWAWVec2i(0,0))
87 : MWAWCell()
88 , m_content()
89 , m_formula(-1)
90 {
91 setPosition(pos);
92 }
93 Cell(Cell const &)=default;
94 //! destructor
95 ~Cell() final;
96 //! the cell content
97 MWAWCellContent m_content;
98 //! the formula id
99 int m_formula;
100 };
101
102 Cell::~Cell()
103 {
104 }
105
106 ////////////////////////////////////////
107 //! Internal: the spreadsheet data of a WingzParser
108 struct Spreadsheet {
109 //! constructor
110 Spreadsheet()
111 : m_widthDefault(74)
112 , m_widthCols()
113 , m_heightDefault(12)
114 , m_heightRows()
115 , m_cells()
116 , m_cellIdPosMap()
117 , m_formulaMap()
118 , m_styleMap()
119 , m_name("Sheet0")
120 {
121 }
122 //! returns the row size in point
123 float getRowHeight(int row) const
124 {
125 if (row>=0&&row<static_cast<int>(m_heightRows.size()))
126 return m_heightRows[size_t(row)];
127 return m_heightDefault;
128 }
129 //! returns the height of a row in point and updated repeated row
130 float getRowHeight(int row, int &numRepeated) const
131 {
132 float res=getRowHeight(row);
133 numRepeated=1;
134 if (row<0 || row>=static_cast<int>(m_heightRows.size()))
135 numRepeated=1000;
136 else {
137 for (auto r=size_t(row+1); r<m_heightRows.size(); ++r) {
138 float nextH=getRowHeight(row+1);
139 if (nextH<res || nextH>res)
140 break;
141 ++numRepeated;
142 }
143 }
144 return res;
145 }
146 //! convert the m_widthCols in a vector of of point size
147 std::vector<float> convertInPoint(std::vector<float> const &list) const
148 {
149 auto numCols=size_t(getRightBottomPosition()[0]+1);
150 std::vector<float> res;
151 res.resize(numCols);
152 for (size_t i = 0; i < numCols; i++) {
153 if (i>=list.size() || list[i] < 0) res[i] = m_widthDefault;
154 else res[i] = float(list[i]);
155 }
156 return res;
157 }
158 //! returns the page position corresponding to a cell and its relative position(in percent)
159 MWAWVec2f getPosition(MWAWVec2i const &cell, MWAWVec2f const &relPos) const
160 {
161 if (cell[0]<0 || cell[1]<0 || cell[0]>256 || cell[1]>10000) {
162 MWAW_DEBUG_MSG(("WingzParserInternal::Spreadsheet::getPosition: the cell's position (%d,%d) seem bads\n", cell[0], cell[1]));
163 return MWAWVec2f(0,0);
164 }
165 float cPos=0;
166 for (size_t i = 0; i<=size_t(cell[0]); ++i) {
167 float w=(i<m_widthCols.size() && m_widthCols[i]>=0) ? m_widthCols[i] : m_widthDefault;
168 if (i<size_t(cell[0]))
169 cPos += w;
170 else
171 cPos+=w*relPos[0];
172 }
173 float rPos=0;
174 for (size_t i = 0; i<=size_t(cell[1]); ++i) {
175 float w=(i<m_heightRows.size() && m_heightRows[i]>=0) ? m_heightRows[i] : m_heightDefault;
176 if (i<size_t(cell[1]))
177 rPos += w;
178 else
179 rPos+=w*relPos[1];
180 }
181 return MWAWVec2f(cPos,rPos);
182 }
183 //! update the cell, ie. look if there is a avalaible formula, ...
184 void update(Cell &cell) const;
185 /** the default column width */
186 float m_widthDefault;
187 /** the column size in points */
188 std::vector<float> m_widthCols;
189 /** the default row height */
190 float m_heightDefault;
191 /** the row height in points */
192 std::vector<float> m_heightRows;
193 /** the list of not empty cells */
194 std::vector<Cell> m_cells;
195 //! the map cellId to cellPos
196 std::map<int, MWAWCellContent::FormulaInstruction> m_cellIdPosMap;
197 //! the list of formula
198 std::map<int, std::vector<MWAWCellContent::FormulaInstruction> > m_formulaMap;
199 //! the list of style
200 std::map<int, Style> m_styleMap;
201 /** the spreadsheet name */
202 std::string m_name;
203 protected:
204 /** returns the last Right Bottom cell position */
205 MWAWVec2i getRightBottomPosition() const
206 {
207 int maxX = 0, maxY = 0;
208 for (auto const &cell : m_cells) {
209 MWAWVec2i const &p = cell.position();
210 if (p[0] > maxX) maxX = p[0];
211 if (p[1] > maxY) maxY = p[1];
212 }
213 return MWAWVec2i(maxX, maxY);
214 }
215 };
216
217 void Spreadsheet::update(Cell &cell) const
218 {
219 // checkme: is cell.m_formula==0 really a cell with a formula ?
220 if (cell.m_formula < 0 || m_formulaMap.find(cell.m_formula)==m_formulaMap.end())
221 return;
222 // first, we need to update the relative position
223 auto formula=m_formulaMap.find(cell.m_formula)->second;
224 MWAWVec2i cPos=cell.position();
225 for (auto &instr : formula) {
226 int numToCheck=0;
227 if (instr.m_type==MWAWCellContent::FormulaInstruction::F_Cell)
228 numToCheck=1;
229 else if (instr.m_type==MWAWCellContent::FormulaInstruction::F_CellList)
230 numToCheck=2;
231 for (int j=0; j<numToCheck; ++j) {
232 for (int c=0; c<2; ++c) {
233 if (instr.m_positionRelative[j][c])
234 instr.m_position[j][c]+=cPos[c];
235 if (instr.m_position[j][c]<0) {
236 if (cell.m_formula!=0) {
237 MWAW_DEBUG_MSG(("WingzParserInternal::Spreadsheet::update: find some bad cell position\n"));
238 }
239 return;
240 }
241 }
242 }
243 }
244 cell.m_content.m_contentType=MWAWCellContent::C_FORMULA;
245 cell.m_content.m_formula=formula;
246 }
247
248 ////////////////////////////////////////
249 //! Internal: the state of a WingzParser
250 struct State {
251 //! constructor
252 State()
253 : m_encrypted(false)
254 , m_spreadsheet()
255 , m_numPages(0)
256 , m_headerHeight(0)
257 , m_footerHeight(0)
258 {
259 }
260
261 //! returns the pattern percent corresponding to an id and a version
262 static bool getPatternPercent(int patId, int vers, float &percent);
263
264 //! a flag to know if the data is encrypted
265 bool m_encrypted;
266 //! the spreadsheet
267 Spreadsheet m_spreadsheet;
268 int m_numPages /** the number of page of the final document */;
269
270 int m_headerHeight /** the header height if known */,
271 m_footerHeight /** the footer height if known */;
272 };
273
274 bool State::getPatternPercent(int patId, int vers, float &perc)
275 {
276 if (vers==2) {
277 if (patId<0 || patId>38) {
278 MWAW_DEBUG_MSG(("WingzParserInternal::State::getPatternPercent: can not find patId=%d\n", patId));
279 return false;
280 }
281 static float const percent[]= { 0/*none*/, 1, 0.9f, 0.7f, 0.5f, 0.7f, 0.5f, 0.7f, 0.2f, 0.3f, 0.1f, 0.3f, 0.3f,
282 0.04f, 0.1f, 0.2f, 0.5f, 0.2f, 0.2f, 0.4f, 0, 0.1f, 0.2f, 0.3f, 0.3f, 0.5f,
283 0.3f, 0.3f, 0.2f, 0.2f, 0.2f, 0.3f, 0.3f, 0.2f, 0.3f, 0.4f, 0.4f, 0.5f, 0.4f
284 };
285 perc=percent[patId];
286 return true;
287 }
288
289 if (patId<0 || patId>=64) {
290 MWAW_DEBUG_MSG(("WingzParserInternal::State::getPatternPercent: can not find patId=%d\n", patId));
291 return false;
292 }
293 static float const percent[]= {
294 0.0f, 1.0f, 0.968750f, 0.93750f, 0.875f, 0.750f, 0.5f, 0.250f,
295 0.250f, 0.18750f, 0.1875f, 0.1250f, 0.0625f, 0.0625f, 0.031250f, 0.0f,
296 0.75f, 0.5f, 0.25f, 0.3750f, 0.25f, 0.1250f, 0.25f, 0.1250f,
297 0.75f, 0.5f, 0.25f, 0.3750f, 0.25f, 0.1250f, 0.25f, 0.1250f,
298 0.75f, 0.5f, 0.5f, 0.5f, 0.5f, 0.25f, 0.25f, 0.234375f,
299 0.6250f, 0.3750f, 0.1250f, 0.25f, 0.218750f, 0.218750f, 0.1250f, 0.093750f,
300 0.5f, 0.5625f, 0.4375f, 0.3750f, 0.218750f, 0.281250f, 0.1875f, 0.093750f,
301 0.593750f, 0.5625f, 0.515625f, 0.343750f, 0.3125f, 0.25f, 0.25f, 0.234375f
302 };
303 perc=percent[patId];
304 return true;
305 }
306 }
307
308 ////////////////////////////////////////////////////////////
309 // constructor/destructor, ...
310 ////////////////////////////////////////////////////////////
311 WingzParser::WingzParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
312 : MWAWSpreadsheetParser(input, rsrcParser, header)
313 , m_state()
314 , m_graphParser(new WingzGraph(*this))
315 {
316 init();
317 }
318
319 WingzParser::~WingzParser()
320 {
321 }
322
323 void WingzParser::init()
324 {
325 resetSpreadsheetListener();
326 setAsciiName("main-1");
327
328 m_state.reset(new WingzParserInternal::State);
329
330 // reduce the margin (in case, the page is not defined)
331 getPageSpan().setMargins(0.1);
332 }
333
334 MWAWVec2f WingzParser::getPosition(MWAWVec2i const &cell, MWAWVec2f const &relPos) const
335 {
336 return m_state->m_spreadsheet.getPosition(cell, relPos);
337 }
338
339 ////////////////////////////////////////////////////////////
340 // the parser
341 ////////////////////////////////////////////////////////////
342 void WingzParser::parse(librevenge::RVNGSpreadsheetInterface *docInterface)
343 {
344 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
345 bool ok = true;
346 try {
347 if (m_state->m_encrypted)
348 ok = decodeEncrypted();
349 if (ok) {
350 // create the asciiFile
351 ascii().setStream(getInput());
352 ascii().open(asciiName());
353 checkHeader(nullptr);
354 ok = createZones();
355 }
356 if (ok) {
357 createDocument(docInterface);
358 sendSpreadsheet();
359 }
360 }
361 catch (...) {
362 MWAW_DEBUG_MSG(("WingzParser::parse: exception catched when parsing\n"));
363 ok = false;
364 }
365
366 ascii().reset();
367 resetSpreadsheetListener();
368 if (!ok) throw(libmwaw::ParseException());
369 }
370
371 ////////////////////////////////////////////////////////////
372 // create the document
373 ////////////////////////////////////////////////////////////
374 void WingzParser::createDocument(librevenge::RVNGSpreadsheetInterface *documentInterface)
375 {
376 if (!documentInterface) return;
377 if (getSpreadsheetListener()) {
378 MWAW_DEBUG_MSG(("WingzParser::createDocument: listener already exist\n"));
379 return;
380 }
381
382 // create the page list
383 m_state->m_numPages = 1;
384 MWAWPageSpan ps(getPageSpan());
385 ps.setPageSpan(1);
386 std::vector<MWAWPageSpan> pageList(1,ps);
387 //
388 MWAWSpreadsheetListenerPtr listen(new MWAWSpreadsheetListener(*getParserState(), pageList, documentInterface));
389 setSpreadsheetListener(listen);
390 listen->startDocument();
391 }
392
393
394 ////////////////////////////////////////////////////////////
395 //
396 // Intermediate level
397 //
398 ////////////////////////////////////////////////////////////
399 bool WingzParser::createZones()
400 {
401 MWAWInputStreamPtr input = getInput();
402 input->setReadInverted(true);
403 input->seek(13, librevenge::RVNG_SEEK_SET);
404 if (!readPreferences()) {
405 ascii().addPos(input->tell());
406 ascii().addNote("Entries(Loose)");
407 if (!findNextZone())
408 return false;
409 }
410 if (!readSpreadsheet()) return false;
411 if (!input->isEnd()) {
412 ascii().addPos(input->tell());
413 ascii().addNote("Entries(Loose)");
414 }
415 return true;
416 }
417
418 ////////////////////////////////////////////////////////////
419 //
420 // Low level
421 //
422 ////////////////////////////////////////////////////////////
423
424 ////////////////////////////////////////////////////////////
425 // read the preferences zone
426 ////////////////////////////////////////////////////////////
427
428 bool WingzParser::readPreferences()
429 {
430 MWAWInputStreamPtr input = getInput();
431 long pos=input->tell();
432 int const vers=version();
433 if (!input->checkPosition(pos+172+2*vers)) {
434 MWAW_DEBUG_MSG(("WingzParser::readPreferences: the zone seems to short\n"));
435 return false;
436 }
437
438 libmwaw::DebugStream f;
439 f << "Entries(Preferences):";
440 auto type=static_cast<int>(input->readULong(1));
441 auto val=static_cast<int>(input->readULong(1));
442 auto dSz=static_cast<int>(input->readULong(2));
443 auto id=static_cast<int>(input->readULong(2));
444 long endPos=pos+4+dSz;
445 if (type!=0 || !input->checkPosition(endPos)) return false;
446 if (val!=0x80) f << "f0=" << val << ",";
447 if (id) f << "id=" << id << ",";
448 ascii().addDelimiter(input->tell(), '|');
449 ascii().addPos(pos);
450 ascii().addNote(f.str().c_str());
451 input->seek(pos+18+2*vers, librevenge::RVNG_SEEK_SET);
452
453 for (int i=0; i < 4; ++i) {
454 pos=input->tell();
455 f.str("");
456 f << "Preferences-" << i << ":";
457 int const sz[] = { 42, 42, 30, 51};
458 input->seek(pos+sz[i], librevenge::RVNG_SEEK_SET);
459 ascii().addPos(pos);
460 ascii().addNote(f.str().c_str());
461 }
462
463 // now a font (or maybe a font list)
464 pos=input->tell();
465 f.str("");
466 f << "Preferences-Fonts:";
467 val=static_cast<int>(input->readULong(1));
468 if (val) f << "f0=" << val << ",";
469 auto sz=long(input->readULong(1));
470 long fontEndPos=pos+sz;
471 if (!input->checkPosition(pos+sz)) {
472 MWAW_DEBUG_MSG(("WingzParser::readPreferences: the fonts zone seems to short\n"));
473 return false;
474 }
475 auto N=static_cast<int>(input->readULong(1));
476 f << "N=" << N << ",";
477 ascii().addPos(pos);
478 ascii().addNote(f.str().c_str());
479 for (int i=0; i < N; ++i) {
480 f.str("");
481 f << "Preferences-Font" << i << ":";
482 pos=input->tell();
483 auto fSz=static_cast<int>(input->readULong(1));
484 if (pos+1+fSz>fontEndPos) {
485 MWAW_DEBUG_MSG(("WingzParser::readPreferences: the %d font size seems bad\n", i));
486 f << "###";
487 ascii().addPos(pos);
488 ascii().addNote(f.str().c_str());
489 input->seek(fontEndPos, librevenge::RVNG_SEEK_SET);
490 return true;
491 }
492 std::string name("");
493 for (int c=0; c<fSz; ++c) name+=char(input->readULong(1));
494 f << name;
495 ascii().addPos(pos);
496 ascii().addNote(f.str().c_str());
497 }
498 if (input->tell()!=fontEndPos) {
499 ascii().addPos(input->tell());
500 ascii().addNote("Preferences-Fontsend");
501 MWAW_DEBUG_MSG(("WingzParser::readPreferences: find extra data\n"));
502 input->seek(fontEndPos, librevenge::RVNG_SEEK_SET);
503 }
504
505 // last unknown
506 pos=input->tell();
507 if (!input->checkPosition(pos+237)) {
508 MWAW_DEBUG_MSG(("WingzParser::readPreferences: the last zone seems to short\n"));
509 return false;
510 }
511
512 f.str("");
513 f << "Preferences-B0:";
514 val=static_cast<int>(input->readLong(2));
515 if (val) f << "f0=" << val << ",";
516 val=static_cast<int>(input->readLong(2));
517 if (val) f << "graph[num]=" << val << ",";
518 for (int i=0; i < 10; ++i) { // f7,f8 related to page size?
519 val=static_cast<int>(input->readLong(2));
520 if (val)
521 f << "f" << i+1 << "=" << val << ",";
522 }
523 auto sSz=static_cast<int>(input->readULong(1));
524 if (!input->checkPosition(pos+25+sSz)) {
525 MWAW_DEBUG_MSG(("WingzParser::readPreferences: auto save name seems bad\n"));
526 f << "####";
527 ascii().addPos(pos);
528 ascii().addNote(f.str().c_str());
529 return false;
530 }
531 std::string name("");
532 for (int i=0; i < sSz; ++i)
533 name += char(input->readULong(1));
534 f << name << ",";
535 for (int i=0; i < 8; ++i) {
536 val=static_cast<int>(input->readLong(2));
537 if (val)
538 f << "g" << i << "=" << val << ",";
539 }
540 int dim[4];
541 for (auto &d : dim) d=static_cast<int>(input->readLong(2));
542 f << "select?=" << dim[0] << "x" << dim[1] << "<->" << dim[2] << "x" << dim[3] << ",";
543 ascii().addPos(pos);
544 ascii().addNote(f.str().c_str());
545
546 pos=input->tell();
547 ascii().addPos(pos);
548 ascii().addNote("Preferences-B1");
549 input->seek(pos+96, librevenge::RVNG_SEEK_SET);
550
551 pos=input->tell();
552 ascii().addPos(pos);
553 ascii().addNote("Preferences-B2");
554 input->seek(pos+58, librevenge::RVNG_SEEK_SET);
555
556 pos=input->tell();
557 f.str("");
558 f << "Preferences[passwd]";
559 for (int i=0; i<2; ++i) {
560 input->seek(pos+i*17, librevenge::RVNG_SEEK_SET);
561 auto len=static_cast<int>(input->readULong(1));
562 if (!len) continue;
563 if (len>16) {
564 MWAW_DEBUG_MSG(("WingzParser::readPreferences: passwd size seems bad\n"));
565 f << "###len" << i << "=" << len << ",";
566 break;
567 }
568 std::string passwd("");
569 for (int c=0; c<len; ++c) passwd += char(input->readULong(1));
570 f << passwd << ",";
571 }
572 ascii().addPos(pos);
573 ascii().addNote(f.str().c_str());
574
575 if (vers==1)
576 input->seek(endPos, librevenge::RVNG_SEEK_SET);
577 else
578 input->seek(pos+34, librevenge::RVNG_SEEK_SET);
579
580 return true;
581 }
582
583
584 ////////////////////////////////////////////////////////////
585 // spreadsheet
586 ////////////////////////////////////////////////////////////
587 bool WingzParser::readSpreadsheet()
588 {
589 MWAWInputStreamPtr input = getInput();
590 libmwaw::DebugStream f;
591 int const vers=version();
592 int const headerSize=vers==1?4:6;
593 while (!input->isEnd()) {
594 long pos=input->tell();
595 auto type=static_cast<int>(input->readULong(1));
596 auto val=static_cast<int>(input->readULong(1));
597 auto dSz=static_cast<int>(input->readULong(2));
598 if (type!=0xFF && input->isEnd()) {
599 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheet: can not read some zone\n"));
600 input->seek(pos, librevenge::RVNG_SEEK_SET);
601 break;
602 }
603 std::string name("");
604 if (type<=0x10) {
605 static char const *wh[]= {
606 "", "SheetSize", "SheetSize", "", "", "", "", "CellName",
607 "Formula", "Style", "SheetErr", "Sheet2Err", "", "SheetMcro", "Graphic", "",
608 "PrintInfo"
609 };
610 name=wh[type];
611 }
612 if (name.empty()) {
613 std::stringstream s;
614 s << "ZSheet" << type;
615 name = s.str();
616 }
617 f.str("");
618 f << "Entries(" << name << "):";
619 if (val!=0x80) f << "fl=" << std::hex << val << std::dec << ",";
620
621 bool ok=true;
622 switch (type) {
623 case 1: // col size
624 case 2: // row size
625 input->seek(pos, librevenge::RVNG_SEEK_SET);
626 ok=readSpreadsheetSize();
627 break;
628 case 18:
629 case 19:
630 input->seek(pos, librevenge::RVNG_SEEK_SET);
631 ok=readSpreadsheetPBreak();
632 break;
633 case 3: // a list of int?
634 ok=input->checkPosition(pos+headerSize+dSz);
635 if (!ok) break;
636 if (vers>1) {
637 val=static_cast<int>(input->readLong(2));
638 if (val) f << "id=" << val << ",";
639 }
640 if ((dSz%2)) {
641 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheet: find some data in zone3\n"));
642 f << "###";
643 ascii().addDelimiter(pos+headerSize,'|');
644 }
645 else if (dSz) {
646 f << "val=[";
647 for (int i=0; i<dSz/2; ++i) f << input->readLong(2) << ",";
648 f << "],";
649 }
650 input->seek(pos+headerSize+dSz, librevenge::RVNG_SEEK_SET);
651 ascii().addPos(pos);
652 ascii().addNote(f.str().c_str());
653 break;
654 case 4: // never find this block with data ...
655 ok=input->checkPosition(pos+headerSize+dSz);
656 if (!ok) break;
657 if (vers>1) {
658 val=static_cast<int>(input->readLong(2));
659 if (val) f << "id=" << val << ",";
660 }
661 if (dSz) {
662 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheet: find some data in zone4\n"));
663 f << "###";
664 ascii().addDelimiter(pos+headerSize,'|');
665 }
666 input->seek(pos+headerSize+dSz, librevenge::RVNG_SEEK_SET);
667 ascii().addPos(pos);
668 ascii().addNote(f.str().c_str());
669 break;
670 case 5:
671 input->seek(pos, librevenge::RVNG_SEEK_SET);
672 ok=readSpreadsheetZone5();
673 break;
674 case 6: { // only in v1?
675 ok=input->checkPosition(pos+headerSize+(vers==1 ? 2 : 0) +dSz);
676 if (!ok) break;
677 val=static_cast<int>(input->readLong(2));
678 if (val) f << "id=" << val << ",";
679 auto fSz=static_cast<int>(input->readULong(1));
680 if (dSz<1 || dSz!=1+fSz) {
681 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheet: zone 6 size seems bad\n"));
682 f << "###";
683 }
684 else {
685 std::string text("");
686 for (int i=0; i<fSz; ++i) text += char(input->readULong(1));
687 f << text << ",";
688 }
689 input->seek(pos+headerSize+(vers==1 ? 2 : 0)+dSz, librevenge::RVNG_SEEK_SET);
690 ascii().addPos(pos);
691 ascii().addNote(f.str().c_str());
692 break;
693 }
694 case 7:
695 input->seek(pos, librevenge::RVNG_SEEK_SET);
696 ok=readSpreadsheetCellName();
697 break;
698 case 8:
699 input->seek(pos, librevenge::RVNG_SEEK_SET);
700 ok=readFormula();
701 break;
702 case 9:
703 input->seek(pos, librevenge::RVNG_SEEK_SET);
704 ok=readSpreadsheetStyle();
705 break;
706 case 0xa: { // seems related to error in formula 0x1a
707 ok=input->checkPosition(pos+6+dSz);
708 if (!ok) break;
709 val=static_cast<int>(input->readLong(2));
710 if (val) f << "id=" << val << ",";
711 auto fSz=static_cast<int>(input->readULong(1));
712 if (dSz<1 || dSz!=1+fSz) {
713 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheet: SheetErr size seems bad\n"));
714 f << "###";
715 }
716 else {
717 std::string text("");
718 for (int i=0; i<fSz; ++i) text += char(input->readULong(1));
719 f << text << ",";
720 }
721 input->seek(pos+6+dSz, librevenge::RVNG_SEEK_SET);
722 ascii().addPos(pos);
723 ascii().addNote(f.str().c_str());
724 break;
725 }
726 case 0xb: { // seems related to error in formula 0x1a
727 ok=input->checkPosition(pos+6+dSz);
728 if (!ok) break;
729 val=static_cast<int>(input->readLong(2));
730 if (val) f << "id=" << val << ",";
731 if ((vers==1&&dSz<2) || (vers==2&&dSz<4)) {
732 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheet: Sheet2Err size seems bad\n"));
733 f << "###";
734 }
735 else {
736 f << "pos=" << input->readULong(1) << "x" << input->readULong(1) << ",";
737 val = static_cast<int>(input->readLong(1));
738 if (val) f << "#g0=" << val << ",";
739 std::string text("");
740 for (int i=0; i<dSz-3; ++i) text += char(input->readULong(1));
741 f << text << ",";
742 }
743 input->seek(pos+6+dSz, librevenge::RVNG_SEEK_SET);
744 ascii().addPos(pos);
745 ascii().addNote(f.str().c_str());
746 break;
747 }
748 case 0xc:
749 input->seek(pos, librevenge::RVNG_SEEK_SET);
750 ok=readSpreadsheetCellList();
751 break;
752 case 0xd:
753 val=static_cast<int>(input->readLong(2));
754 if (val) f << "id=" << val << ",";
755 ok=readMacro();
756 if (!ok) break;
757 ascii().addPos(pos);
758 ascii().addNote(f.str().c_str());
759 break;
760 case 0xe:
761 input->seek(pos, librevenge::RVNG_SEEK_SET);
762 ok=m_graphParser->readGraphic();
763 if (!ok) {
764 ascii().addPos(pos);
765 ascii().addNote("Entries(Graphic):###");
766 ok=findNextZone(0xe) && input->tell()>pos+46;
767 }
768 break;
769 case 0xf:
770 input->seek(pos, librevenge::RVNG_SEEK_SET);
771 ok=m_graphParser->readEndGroup();
772 break;
773 case 0x10:
774 input->seek(pos, librevenge::RVNG_SEEK_SET);
775 ok=readPrintInfo();
776 break;
777 case 0xff: // end
778 if (val==0xf && dSz==0) {
779 ascii().addPos(pos);
780 ascii().addNote("_");
781 return true;
782 }
783 ok=false;
784 break;
785 default:
786 ok=false;
787 }
788 if (ok) continue;
789 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
790 if (vers==1) {
791 if (type==6 || type==7 || type==0x11) dSz+=2;
792 }
793 else {
794 if (type==0xc) dSz+=4;
795 else if (type==0xe) dSz+=2;
796 else if (type==0x10) dSz+=14;
797 }
798 if (!type || (vers==2 && !val) || (type>24 && !(vers==1&&type>100 && type<104)) ||
799 (val&0x3F) || !input->checkPosition(pos+headerSize+dSz)) {
800 input->seek(pos, librevenge::RVNG_SEEK_SET);
801 break;
802 }
803 auto id=static_cast<int>(input->readLong(2));
804 if (id) f << "id=" << id << ",";
805 if (input->tell() != pos+headerSize+dSz)
806 ascii().addDelimiter(input->tell(), '|');
807 input->seek(pos+headerSize+dSz, librevenge::RVNG_SEEK_SET);
808 ascii().addPos(pos);
809 ascii().addNote(f.str().c_str());
810 }
811 return true;
812 }
813
814 bool WingzParser::readSpreadsheetCellList()
815 {
816 MWAWInputStreamPtr input = getInput();
817 int const vers=version();
818 long pos=input->tell();
819 auto type=static_cast<int>(input->readULong(1));
820 if (type!=12) return false;
821 auto val=static_cast<int>(input->readULong(1));
822 auto dSz= static_cast<int>(input->readULong(2));
823 auto row=static_cast<int>(input->readLong(2));
824 auto firstCol=static_cast<int>(input->readLong(2));
825 long endPos=pos+(vers==1 ? 6 : 10)+dSz;
826 libmwaw::DebugStream f;
827 f << "Entries(SheetCell)[row=" << row << "]:";
828 if (firstCol) f << "first[col]=" << firstCol << ",";
829 if (val!=0x40) f << "fl=" << std::hex << val << std::dec << ",";
830 if (!input->checkPosition(endPos)) {
831 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheetCellList: find bad size for data\n"));
832 return false;
833 }
834 val=static_cast<int>(input->readLong(2));
835 if (val) f << "f0=" << val << ",";
836 ascii().addPos(pos);
837 ascii().addNote(f.str().c_str());
838
839 while (!input->isEnd()) {
840 MWAWVec2i cellPos(firstCol++, row);
841 pos=input->tell();
842 if (pos>=endPos) break;
843 type=static_cast<int>(input->readULong(1));
844 f.str("");
845 f << "SheetCell[" << cellPos << "]:type=" << (type&0xf);
846 if (type&0xf0) {
847 f << "[high=" << (type>>4) << "]";
848 type &=0xf;
849 }
850 f << ",";
851 if (type==0) { // empty cell
852 ascii().addPos(pos);
853 ascii().addNote(f.str().c_str());
854 continue;
855 }
856 if (pos+(vers==1 ? 4 : 6)>endPos) {
857 input->seek(pos, librevenge::RVNG_SEEK_SET);
858 break;
859 }
860 WingzParserInternal::Cell cell(cellPos);
861 val=static_cast<int>(input->readULong(1));
862 if (val&0xF) {
863 int borders=0;
864 f << "bord=";
865 if (val&1) {
866 borders|=libmwaw::LeftBit;
867 f << "L";
868 }
869 if (val&2) {
870 borders|=libmwaw::RightBit;
871 f << "R";
872 }
873 if (val&4) {
874 borders|=libmwaw::TopBit;
875 f << "T";
876 }
877 if (val&8) {
878 borders|=libmwaw::BottomBit;
879 f << "B";
880 }
881 f << ",";
882 cell.setBorders(borders, MWAWBorder());
883 }
884 if (val&0xF0) f << "f0=" << (val>>4) << ",";
885
886 MWAWCell::Format format;
887 val=static_cast<int>(input->readULong(1));
888 format.m_digits= val&0xf;
889 if (format.m_digits!=2) f << "digits=" << format.m_digits << ",";
890 switch (val>>4) {
891 case 0: // general
892 break;
893 case 1:
894 format.m_format=MWAWCell::F_NUMBER;
895 format.m_numberFormat=MWAWCell::F_NUMBER_DECIMAL;
896 break;
897 case 2:
898 format.m_format=MWAWCell::F_NUMBER;
899 format.m_numberFormat=MWAWCell::F_NUMBER_CURRENCY;
900 break;
901 case 3:
902 format.m_format=MWAWCell::F_NUMBER;
903 format.m_numberFormat=MWAWCell::F_NUMBER_PERCENT;
904 break;
905 case 4:
906 format.m_format=MWAWCell::F_NUMBER;
907 format.m_numberFormat=MWAWCell::F_NUMBER_SCIENTIFIC;
908 break;
909 case 5:
910 format.m_format=MWAWCell::F_DATE;
911 format.m_DTFormat="%b %d %y";
912 break;
913 case 6:
914 format.m_format=MWAWCell::F_DATE;
915 format.m_DTFormat="%b %d";
916 break;
917 case 7:
918 format.m_format=MWAWCell::F_DATE;
919 format.m_DTFormat="%b %y";
920 break;
921 case 8:
922 format.m_format=MWAWCell::F_DATE;
923 format.m_DTFormat="%m/%d/%y";
924 break;
925 case 9:
926 format.m_format=MWAWCell::F_DATE;
927 format.m_DTFormat="%m/%d";
928 break;
929 case 10:
930 format.m_format=MWAWCell::F_TIME;
931 format.m_DTFormat="%I:%M:%S %p";
932 break;
933 case 11:
934 format.m_format=MWAWCell::F_TIME;
935 format.m_DTFormat="%I:%M %p";
936 break;
937 case 12:
938 format.m_format=MWAWCell::F_TIME;
939 format.m_DTFormat="%H:%M:%S";
940 break;
941 case 13:
942 format.m_format=MWAWCell::F_TIME;
943 format.m_DTFormat="%H:%M";
944 break;
945 case 14:
946 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheetCellList: find cell with custom format\n"));
947 f << "format=custom,";
948 break;
949 default:
950 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheetCellList: find cell with format=15\n"));
951 f << "##format=15,";
952 break;
953 }
954 val=static_cast<int>(input->readULong(1));
955 bool ok=true;
956 switch ((val>>4)&7) {
957 case 0: // general
958 break;
959 case 1:
960 cell.setHAlignment(MWAWCell::HALIGN_LEFT);
961 f << "align=left,";
962 break;
963 case 2:
964 cell.setHAlignment(MWAWCell::HALIGN_CENTER);
965 f << "align=center,";
966 break;
967 case 3:
968 cell.setHAlignment(MWAWCell::HALIGN_RIGHT);
969 f << "align=right,";
970 break;
971 default:
972 break;
973 }
974 if (val&0x8F) f << "f1=" << std::hex << (val&0x8F) << std::dec << ",";
975 val=static_cast<int>(input->readLong(2));
976 f << "style=" << val << ",";
977 if (m_state->m_spreadsheet.m_styleMap.find(val)==m_state->m_spreadsheet.m_styleMap.end()) {
978 f << "#style,";
979 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheetStyle: can not find a style\n"));
980 }
981 else {
982 auto const &style=m_state->m_spreadsheet.m_styleMap.find(val)->second;
983 cell.setFont(style.m_font);
984 if (!style.m_backgroundColor.isWhite())
985 cell.setBackgroundColor(style.m_backgroundColor);
986 }
987 WingzParserInternal::Style style;
988 f << "format=[" << format << "],";
989 if (type!=1) {
990 cell.m_formula=static_cast<int>(input->readLong(2));
991 // checkme: does cell.m_formula==0 means also no formula ?
992 if (cell.m_formula!=-1)
993 f << "formula=" << cell.m_formula << ",";
994 }
995 MWAWCellContent &content=cell.m_content;
996 long dPos=input->tell();
997 switch (type) {
998 case 1: // only style
999 break;
1000 case 2:
1001 case 3: {
1002 ok=false;
1003 if (dPos+1>endPos)
1004 break;
1005 auto fSz=static_cast<int>(input->readULong(1));
1006 if (dPos+1+fSz>endPos)
1007 break;
1008 if (format.m_format==MWAWCell::F_UNKNOWN)
1009 format.m_format=MWAWCell::F_TEXT;
1010 if (content.m_contentType!=MWAWCellContent::C_FORMULA)
1011 content.m_contentType=MWAWCellContent::C_TEXT;
1012 content.m_textEntry.setBegin(input->tell());
1013 content.m_textEntry.setLength(fSz);
1014 std::string text("");
1015 for (int i=0; i<fSz; ++i) text += char(input->readULong(1));
1016 f << text;
1017 input->seek(dPos+1+fSz, librevenge::RVNG_SEEK_SET);
1018 ok=true;
1019 break;
1020 }
1021 case 4:
1022 if (dPos+2>endPos) {
1023 ok=false;
1024 break;
1025 }
1026 if (format.m_format==MWAWCell::F_UNKNOWN)
1027 format.m_format=MWAWCell::F_NUMBER;
1028 if (content.m_contentType!=MWAWCellContent::C_FORMULA)
1029 content.m_contentType=MWAWCellContent::C_NUMBER;
1030 content.setValue(std::numeric_limits<double>::quiet_NaN());
1031 f << "nan" << input->readLong(2) << ",";
1032 break;
1033 case 5: { // style + double
1034 if (dPos+8>endPos) {
1035 ok=false;
1036 break;
1037 }
1038 double value;
1039 bool isNAN;
1040 if (format.m_format==MWAWCell::F_UNKNOWN)
1041 format.m_format=MWAWCell::F_NUMBER;
1042 if (content.m_contentType!=MWAWCellContent::C_FORMULA)
1043 content.m_contentType=MWAWCellContent::C_NUMBER;
1044 if (input->readDoubleReverted8(value, isNAN))
1045 content.setValue(value);
1046 f << value;
1047 input->seek(dPos+8, librevenge::RVNG_SEEK_SET);
1048 break;
1049 }
1050 default:
1051 ok=false;
1052 break;
1053 }
1054 cell.setFormat(format);
1055 // change the reference date from 1/1/1904 to 1/1/1900
1056 if (format.m_format==MWAWCell::F_DATE && content.isValueSet())
1057 content.setValue(content.m_value+1460.);
1058
1059 m_state->m_spreadsheet.m_cells.push_back(cell);
1060 if (!ok) {
1061 input->seek(pos, librevenge::RVNG_SEEK_SET);
1062 break;
1063 }
1064 ascii().addPos(pos);
1065 ascii().addNote(f.str().c_str());
1066 }
1067 pos=input->tell();
1068 if (pos==endPos) return true;
1069 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheetCellList: find some extra data\n"));
1070 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1071 ascii().addPos(pos);
1072 ascii().addNote("SheetCell-end:");
1073 return true;
1074 }
1075
1076 // style
1077 bool WingzParser::readSpreadsheetCellName()
1078 {
1079 MWAWInputStreamPtr input = getInput();
1080 long pos=input->tell();
1081 auto type=static_cast<int>(input->readULong(1));
1082 if (type!=7) return false;
1083 auto val=static_cast<int>(input->readULong(1));
1084 auto dSz= static_cast<int>(input->readULong(2));
1085 auto id=static_cast<int>(input->readLong(2));
1086 long endPos=pos+6+dSz;
1087 libmwaw::DebugStream f;
1088 f << "Entries(CellName)[" << id << "]:";
1089 if (val!=0x40) f << "fl=" << std::hex << val << std::dec << ",";
1090 if (dSz<10 || !input->checkPosition(endPos)) {
1091 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheetCellName: find bad size for data\n"));
1092 return false;
1093 }
1094 val=static_cast<int>(input->readLong(2));
1095 if (val!=-1) f << "f0=" << val << ",";
1096 auto sSz=static_cast<int>(input->readULong(1));
1097 if ((sSz!=7&&sSz!=12) || input->tell()+sSz>endPos) {
1098 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheetCellName: can not determine the block type\n"));
1099 f << "###";
1100 ascii().addPos(pos);
1101 ascii().addNote(f.str().c_str());
1102 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1103 return true;
1104 }
1105 MWAWCellContent::FormulaInstruction instr;
1106 if (sSz==7) { // a cell
1107 val=static_cast<int>(input->readLong(1));
1108 if (val!=7) f << "f1=" << val << ",";
1109 int cell[2];
1110 for (auto &c : cell) c=static_cast<int>(input->readLong(2));
1111 instr.m_type=MWAWCellContent::FormulaInstruction::F_Cell;
1112 instr.m_position[0]=MWAWVec2i(cell[1],cell[0]);
1113 instr.m_positionRelative[0]=MWAWVec2b(false,false);
1114 f << "cell=" << instr.m_position[0] << ",";
1115 }
1116 else { // a cell list
1117 for (int i=0; i< 2; ++i) {
1118 val=static_cast<int>(input->readLong(1));
1119 if (val!=7) f << "f" << i+1 << "=" << val << ",";
1120 }
1121 int cell[4];
1122 for (auto &c : cell) c=static_cast<int>(input->readLong(2));
1123 instr.m_type=MWAWCellContent::FormulaInstruction::F_CellList;
1124 instr.m_position[0]=MWAWVec2i(cell[2], cell[0]);
1125 instr.m_position[1]=MWAWVec2i(cell[3], cell[1]);
1126 instr.m_positionRelative[0]=instr.m_positionRelative[1]=MWAWVec2b(false,false);
1127 f << "cell=" << instr.m_position[0] << "<->" << instr.m_position[1] << ",";
1128 }
1129 val=static_cast<int>(input->readLong(1));
1130 if (val!=-1) f << "g0=" << val << ",";
1131
1132 sSz=static_cast<int>(input->readULong(1));
1133 if (input->tell()+sSz>endPos) {
1134 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheetCellName: style name seems bad\n"));
1135 f << "###";
1136 }
1137 else {
1138 std::string name("");
1139 for (int i=0; i<sSz; ++i) name+=char(input->readULong(1));
1140 f << name << ",";
1141 if (input->tell()!=endPos) {
1142 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheetCellName: find some extra data\n"));
1143 ascii().addDelimiter(input->tell(), '|');
1144 }
1145 }
1146 m_state->m_spreadsheet.m_cellIdPosMap[id]=instr;
1147 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1148 ascii().addPos(pos);
1149 ascii().addNote(f.str().c_str());
1150 return true;
1151 }
1152
1153 bool WingzParser::readSpreadsheetStyle()
1154 {
1155 MWAWInputStreamPtr input = getInput();
1156 long pos=input->tell();
1157 auto type=static_cast<int>(input->readULong(1));
1158 if (type!=9) return false;
1159 auto val=static_cast<int>(input->readULong(1));
1160 auto dSz= static_cast<int>(input->readULong(2));
1161 auto id=static_cast<int>(input->readLong(2));
1162 long endPos=pos+6+dSz;
1163
1164 libmwaw::DebugStream f;
1165 f << "Entries(Style)[" << id << "]:";
1166 if (val!=0x40) f << "fl=" << std::hex << val << std::dec << ",";
1167 if (dSz<26 || !input->checkPosition(endPos)) {
1168 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheetStyle: find bad size for data\n"));
1169 return false;
1170 }
1171 WingzParserInternal::Style style;
1172 val=static_cast<int>(input->readLong(2));
1173 if (val!=1) f << "used=" << val << ",";
1174 val=static_cast<int>(input->readLong(2)); // always 0?
1175 if (val) f << "f0=" << val << ",";
1176 MWAWFont &font=style.m_font;
1177 font.setSize(float(input->readULong(2)));
1178 auto flag=static_cast<int>(input->readULong(2));
1179 uint32_t flags=0;
1180 if (flag&0x1) flags |= MWAWFont::boldBit;
1181 if (flag&0x2) flags |= MWAWFont::italicBit;
1182 if (flag&0x4) font.setUnderlineStyle(MWAWFont::Line::Simple);
1183 if (flag&0x8) flags |= MWAWFont::embossBit;
1184 if (flag&0x10) flags |= MWAWFont::shadowBit;
1185 if (flag&0xFF60)
1186 f << "#font[flag]=" << std::hex << (flag&0xFF60) << std::dec << ",";
1187 font.setFlags(flags);
1188
1189 val=static_cast<int>(input->readLong(1));
1190 if (val==1)
1191 f << "hasCustomFmt,";
1192 else if (val) f << "#hasCustomFmt=" << val << ",";
1193 bool hasCustomFormat=val==1;
1194
1195 int patId=0;
1196 MWAWColor colors[4];
1197 for (int i=0; i<4; ++i) { // front, back, unknown,font color
1198 val=static_cast<int>(input->readULong(4));
1199 int col=((val>>16)&0xFF)|(val&0xFF00)|((val&0xFF)<<16);
1200 int high=(val>>24);
1201 colors[i]=MWAWColor(uint32_t(col)|0xFF000000);
1202 switch (i) {
1203 case 0:
1204 patId=high;
1205 if (patId) f << "patId=" << patId << ",";
1206 if (col) f << "backColor=" << std::hex << col << std::dec << ",";
1207 break;
1208 case 1:
1209 if (col!=0xFFFFFF) f << "frontColor=" << std::hex << col << std::dec << ",";
1210 if (high) f << "g0=" << high << ",";
1211 break;
1212 case 2:
1213 style.m_lineColor=colors[i];
1214 if (col) f << "lineColor=" << std::hex << col << std::dec << ",";
1215 if (high!=1) f << "g1=" << high << ",";
1216 break;
1217 case 3:
1218 font.setColor(MWAWColor(uint32_t(col)));
1219 if (high) f << "g2=" << high << ",";
1220 break;
1221 default:
1222 break;
1223 }
1224 }
1225 float percent;
1226 if (patId && m_state->getPatternPercent(patId, version(), percent)) {
1227 style.m_backgroundColor=MWAWColor::barycenter(percent, colors[0], 1.f-percent, colors[1]);
1228 if (!style.m_backgroundColor.isWhite())
1229 f << "cellColor=" << style.m_backgroundColor << ",";
1230 }
1231 auto nSz=static_cast<int>(input->readULong(1));
1232 if (26+nSz>dSz) {
1233 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheetStyle: the name size seems bad\n"));
1234 f << "###";
1235 }
1236 else {
1237 std::string name("");
1238 for (int i=0; i < nSz; ++i) name+=char(input->readULong(1));
1239 font.setId(getParserState()->m_fontConverter->getId(name));
1240 }
1241 f << font.getDebugString(getParserState()->m_fontConverter) << ",";
1242
1243 if (hasCustomFormat && input->tell()!=endPos) {
1244 long actPos=input->tell();
1245 auto fSz=static_cast<int>(input->readULong(1));
1246 if (actPos+1+fSz<=endPos) {
1247 std::stringstream form("");
1248 for (int i=0; i<fSz; ++i) {
1249 // list of code defining either a variable, special char or normal char
1250 auto c=static_cast<int>(input->readULong(1));
1251 switch (c) {
1252 case 1:
1253 form << "\\";
1254 break;
1255 case 8:
1256 form << "[day]";
1257 break;
1258 case 0x1a:
1259 form << "%";
1260 break;
1261 case 0x2d:
1262 form << ":";
1263 break;
1264 default:
1265 if (c>0x30) form << char(c);
1266 else form << "[0x" << std::hex << c << std::dec << "]";
1267 }
1268 }
1269 f << "form=\"" << form.str() << "\",";
1270 }
1271 else {
1272 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheetStyle: can not read custom format\n"));
1273 f << "##format,";
1274 input->seek(actPos, librevenge::RVNG_SEEK_SET);
1275 }
1276 }
1277
1278 if (m_state->m_spreadsheet.m_styleMap.find(id)!=m_state->m_spreadsheet.m_styleMap.end()) {
1279 f << "#id,";
1280 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheetStyle: the style %d already exists\n", id));
1281 }
1282 else
1283 m_state->m_spreadsheet.m_styleMap[id]=style;
1284 if (input->tell()!=endPos) {
1285 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheetStyle: find some extra data\n"));
1286 ascii().addDelimiter(input->tell(), '|');
1287 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1288 }
1289 ascii().addPos(pos);
1290 ascii().addNote(f.str().c_str());
1291 return true;
1292 }
1293
1294 ////////////////////////////////////////////////////////////
1295 // formula
1296 ////////////////////////////////////////////////////////////
1297 namespace WingzParserInternal
1298 {
1299 struct Functions {
1300 char const *m_name;
1301 int m_arity;
1302 };
1303
1304 static Functions const s_listFunctions[] = {
1305 { "", -2} /*cell*/,{ "", -2} /*cell*/,{ "", -2} /*cell*/,{ "", -2} /*cell*/,
1306 { "", -2} /*cell*/,{ "", -2} /*cell*/,{ "", -2} /*cell*/,{ "", -2} /*cell*/,
1307 { "", -2} /*list cell*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
1308 { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
1309 // 10
1310 { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
1311 { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
1312 { "", -2} /*double*/,{ "", -2} /* text*/,{ "", -2} /*1a entry error */,{ "", -2} /*UNKN*/,
1313 { "", -2}/*if separator?*/,{ "", -2} /*if separator?*/,{ "", -2} /* convert to?*/,{ "", -2} /*convert to bool*/,
1314 // 20
1315 { "", -2} /*UNKN*/, { "", -2},{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
1316 { "", -2} /*UNKN*/,{ "(", 1} /*spec()*/,{ "", -2}/*UNKN*/,{ "", -2} /*UNKN*/,
1317 { "", -2} /*convert to?*/,{ "", -2} /*UNKN*/,{ "", -2} /*+number*/,{ "", -2} /*-number*/,
1318 { "", -2} /* *number*/,{ "", -2} /*/number*/,{ "+", 2},{ "-", 2},
1319 // 30
1320 { "*", 2},{ "/", 2}, { "-", 1},{ "", -2} /*UNKN*/,
1321 { "^", 2},{ "Concatenate", 2},{ "And", 2},{ "Or", 2},
1322 { "Not", 1},{ "=", 2},{ "<", 2},{ "<=", 2},
1323 { ">", 2},{ ">=", 2},{ "<>", 2},{ "", -2} /*UNKN*/,
1324 // 40
1325 { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
1326 { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
1327 { "", -2} /*UNKN*/,{ "False", 0},{ "True", 0}, { "", -2} /*UNKN*/,
1328 { "", -2} /*UNKN*/,{ "E", 0}/*exp 1*/,{ "Pi", 0},{ "IsErr", 1},
1329 // 50
1330 { "IsNA", 1},{ "IsNumber", 1},{ "IsString", 1},{ "IsBlank", 1},
1331 { "", -2} /*UNKN*/,{ "DAverage", 3},{ "DCount", 3},{ "DMax", 3},
1332 { "DMin", 3},{ "DStDev", 3},{ "DStDevP", 3}/*checkme*/,{ "DSum", 3},
1333 { "DSumSq", 3},{ "DVar", 3},{ "DVarP", 3}/*checkme*/,{ "Now", 0},
1334 // 60
1335 { "CMonth", 1},{ "CWeekday", 1},{ "DateValue", 1},{ "Day", 1},
1336 { "DayName", 1},{ "Month", 1},{ "MonthName", 1},{ "Year", 1},
1337 { "ADate", 2},{ "AddDays", 2},{ "EDate", 2}/* add month*/,{ "AddYears", 2},
1338 { "Date", 3},{ "Hour", 1},{ "Minute", 1},{ "Second", 1},
1339 // 70
1340 { "TimeValue", 1},{ "AddHours", 2},{ "AddMinutes",2},{ "AddSeconds", 2},
1341 { "Atime", 2},{ "Time", 3},{ "CTERM", 3},{ "FV", 3},
1342 { "FVL", 3},{ "Interest", 3},{ "LoanTerm", 3},{ "PMT", 3},
1343 { "Principal", 3},{ "PV", 3},{ "PVL", 3},{ "Rate", 3},
1344 // 80
1345 { "SLN", 3},{ "", -2} /*UNKN*/,{ "DDB", 4},{ "SYD", 4},
1346 { "BondPrice", 5},{ "BondYTM", 5},{ "IRR", -1},{ "NPV", -1},
1347 { "Acosh", 1},{ "Asinh", 1} /*UNKN*/,{ "Atanh", 1},{ "Cosh", 1},
1348 { "Sinh", 1},{ "Tanh", 1},{ "If", 3},{ "Choose", -1},
1349 // 90
1350 { "NA", 0} /*Err*/,{ "NA", 0},{ "Guess", 0},{ "Abs", 1},
1351 { "Factorial", 1},{ "Int", 1},{ "Sign", 1},{ "", -2} /*UNKN*/,
1352 { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "Mod", 2},{ "Round", 2},
1353 { "Goal", 3},{ "Rand", 0},{ "Exponential", 1} /*odd: maybe set*/,{ "Normal", 1},
1354 // a0
1355 { "Uniform", 1},{ "Average", -1},{ "Count", -1},{ "Max", -1},
1356 { "Min", -1},{ "StD", -1},{ "StDev", -1},{ "Sum", -1},
1357 { "SumSq", -1},{ "Var", -1},{ "VarP", -1}/*checkme*/,{ "Char", 1},
1358 { "Code", 1},{ "Length", 1},{ "Lower", 1},{ "", -2} /*UNKN*/,
1359 // b0
1360 { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "Currency", 1},{ "Proper", 1},
1361 { "", -2} /*UNKN*/,{ "Exact", 2},{ "NFormat", 2},{ "Left", 2},
1362 { "Right", 2},{ "", -2} /*UNKN*/,{ "Collate", 2},{ "Rept", 2},
1363 { "Find", 3},{ "Match", 3},{ "MID", 3},{ "Replace", 4},
1364 // c0
1365 { "Exp", 1},{ "Ln", 1},{ "Log", 1},{ "Logn", 2},
1366 { "Sqrt", 1},{ "Acos", 1},{ "Asin", 1},{ "Atan", 1},
1367 { "Cos", 1},{ "Degrees", 1},{ "Radians", 1},{ "Sin", 1},
1368 { "Tan", 1},{ "Atan2", 2},{ "Col", 0},{ "Row", 0},
1369 // d0
1370 { "Cols", 1},{ "", -2} /*UNKN*/,{ "Indirect", 1},{ "Range", 1},
1371 { "MakeCell", 2},{ "HLookUp", 3},{ "Index", 3},{ "", -2} /*UNKN*/,
1372 { "MakeRange", 4},{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
1373 { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
1374 // e0
1375 { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
1376 { "FunctE4", 2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
1377 { "N", 1},{ "Cell", 0},{ "Contains", 2},{ "", -2} /*UNKN*/,
1378 { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
1379 // f0
1380 { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
1381 { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
1382 { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
1383 { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
1384 };
1385 }
1386
1387 bool WingzParser::readFormula()
1388 {
1389 MWAWInputStreamPtr input = getInput();
1390 long pos=input->tell(), debPos=pos;
1391 auto type=static_cast<int>(input->readULong(1));
1392 if (type!=8) return false;
1393 auto val=static_cast<int>(input->readULong(1));
1394 auto dSz= static_cast<int>(input->readULong(2));
1395 long endPos=pos+6+dSz;
1396 if (dSz<7 || !input->checkPosition(endPos)) {
1397 MWAW_DEBUG_MSG(("WingzParser::readFormula: find bad size for data\n"));
1398 return false;
1399 }
1400 auto id=static_cast<int>(input->readLong(2));
1401 libmwaw::DebugStream f;
1402 f << "Entries(Formula)[" << id << "]:";
1403 if (val!=0x40) f << "fl=" << std::hex << val << std::dec << ",";
1404 for (int i=0; i<2; ++i) { // f0=1|2, f1=0
1405 val=static_cast<int>(input->readLong(2));
1406 if (val) f << "f" << i << "=" << val << ",";
1407 }
1408 val=static_cast<int>(input->readLong(1)); // always 0, maybe related to the equal sign
1409 if (val) f << "f2=" << val << ",";
1410 bool ok = true;
1411 std::vector<std::vector<MWAWCellContent::FormulaInstruction> > stack;
1412 std::string error("");
1413 while (long(input->tell()) != endPos) {
1414 pos = input->tell();
1415 if (pos > endPos) return false;
1416 auto wh = static_cast<int>(input->readULong(1));
1417 if (wh==0xFF) break; // operator=
1418 double value;
1419 int arity = 0;
1420 bool isNan;
1421 MWAWCellContent::FormulaInstruction instr;
1422 bool noneInstr=false;
1423 switch (wh) {
1424 case 0:
1425 case 1:
1426 case 2:
1427 case 3:
1428 case 4:
1429 case 5:
1430 case 6:
1431 case 7: { // difference with 0-3 ?
1432 if (pos+1+4>endPos) {
1433 error="#cell";
1434 ok=false;
1435 break;
1436 }
1437 int cPos[2];
1438 for (auto &c : cPos) c=static_cast<int>(input->readLong(2));
1439 instr.m_type=MWAWCellContent::FormulaInstruction::F_Cell;
1440 instr.m_position[0]=MWAWVec2i(cPos[1], cPos[0]);
1441 instr.m_positionRelative[0]=MWAWVec2b((wh&1)==0, (wh&2)==0);
1442 break;
1443 }
1444 case 8: {
1445 auto typ=static_cast<int>(input->readULong(1));
1446 if (typ>0xF || pos+1+9>endPos) {
1447 error="#listCell";
1448 ok=false;
1449 break;
1450 }
1451 int cPos[4];
1452 for (auto &c : cPos) c=static_cast<int>(input->readLong(2));
1453 instr.m_type=MWAWCellContent::FormulaInstruction::F_CellList;
1454 instr.m_position[0]=MWAWVec2i(cPos[2], cPos[0]);
1455 instr.m_position[1]=MWAWVec2i(cPos[3], cPos[1]);
1456 instr.m_positionRelative[0]=MWAWVec2b((typ&2)==0, (typ&1)==0);
1457 instr.m_positionRelative[1]=MWAWVec2b((typ&8)==0, (typ&4)==0);
1458 break;
1459 }
1460 case 0x9:
1461 case 0xa: {
1462 if (pos+1+2>endPos) {
1463 ok=false;
1464 break;
1465 }
1466 auto cId=static_cast<int>(input->readLong(2));
1467 if (m_state->m_spreadsheet.m_cellIdPosMap.find(cId)==m_state->m_spreadsheet.m_cellIdPosMap.end()) {
1468 MWAW_DEBUG_MSG(("WingzParser::readFormula: can not find cell with id\n"));
1469 std::stringstream s;
1470 s << "##cellId=" << cId << ",";
1471 error=s.str();
1472 ok=false;
1473 break;
1474 }
1475 instr=m_state->m_spreadsheet.m_cellIdPosMap.find(cId)->second;
1476 break;
1477 }
1478 case 0x1a: // error related to Sheet2Err
1479 if (pos+1+2>endPos) {
1480 ok=false;
1481 break;
1482 }
1483 f << "f1a=" << input->readLong(1) << "x" << input->readLong(1) <<",";
1484 instr.m_type=MWAWCellContent::FormulaInstruction::F_Function;
1485 instr.m_content="NA";
1486 break;
1487 case 0x1c:
1488 case 0x1d: // appear with if, maybe to separate condition to other ?
1489 if (pos+1+2>endPos) {
1490 ok=false;
1491 break;
1492 }
1493 noneInstr=true;
1494 f << "f" << std::hex << wh << std::dec << "=" << input->readLong(2) << ",";
1495 break;
1496 case 0x1e: // convert to ?
1497 case 0x1f: // convert to bool
1498 case 0x26:
1499 case 0x27:
1500 case 0x28: // convert to ?
1501 noneInstr=true;
1502 break;
1503 case 0x18:
1504 case 0x2a:
1505 case 0x2b:
1506 case 0x2c:
1507 case 0x2d:
1508 if (endPos-pos<9 || !input->readDoubleReverted8(value, isNan)) {
1509 error="#number";
1510 ok = false;
1511 break;
1512 }
1513 instr.m_type=MWAWCellContent::FormulaInstruction::F_Double;
1514 instr.m_doubleValue=value;
1515 if (wh>=0x2a&&wh<=0x2d) { // mixed operator + number
1516 stack.push_back(std::vector<MWAWCellContent::FormulaInstruction>(1,instr));
1517 static char const *what[]= {"+","-","*","/"};
1518
1519 instr=MWAWCellContent::FormulaInstruction();
1520 instr.m_type=MWAWCellContent::FormulaInstruction::F_Function;
1521 instr.m_content=what[wh-0x2a];
1522 arity=2;
1523 }
1524 break;
1525 case 0x19: {
1526 instr.m_type=MWAWCellContent::FormulaInstruction::F_Text;
1527 auto fSz=static_cast<int>(input->readULong(1));
1528 if (pos+1+fSz > endPos) {
1529 ok=false;
1530 break;
1531 }
1532 for (int i=0; i<fSz; ++i) {
1533 auto c = char(input->readULong(1));
1534 if (c==0) {
1535 ok = i+1==fSz;
1536 break;
1537 }
1538 instr.m_content += c;
1539 }
1540 break;
1541 }
1542 case 0xfe: { // checkme
1543 if (pos+1+1 > endPos) {
1544 ok=false;
1545 break;
1546 }
1547 wh=static_cast<int>(input->readULong(1));
1548 switch (wh) {
1549 case 0x25:
1550 instr.m_type=MWAWCellContent::FormulaInstruction::F_Function;
1551 instr.m_content="CellText";
1552 arity=1;
1553 break;
1554 case 0x27:
1555 instr.m_type=MWAWCellContent::FormulaInstruction::F_Function;
1556 instr.m_content="IsRange";
1557 arity=1;
1558 break;
1559 case 0x92:
1560 instr.m_type=MWAWCellContent::FormulaInstruction::F_Function;
1561 instr.m_content="ColOf";
1562 arity=1;
1563 break;
1564 case 0x93:
1565 instr.m_type=MWAWCellContent::FormulaInstruction::F_Function;
1566 instr.m_content="RowOf";
1567 arity=1;
1568 break;
1569 default: {
1570 std::stringstream s;
1571 s << "##FunctExtra" << std::hex << wh << std::dec << ",";
1572 error=s.str();
1573 ok = false;
1574 break;
1575 }
1576 }
1577 break;
1578 }
1579 default:
1580 if (wh < 0xf0 && WingzParserInternal::s_listFunctions[wh].m_arity > -2) {
1581 instr.m_content=WingzParserInternal::s_listFunctions[wh].m_name;
1582 instr.m_type=MWAWCellContent::FormulaInstruction::F_Function;
1583 arity=WingzParserInternal::s_listFunctions[wh].m_arity;
1584 }
1585 if (instr.m_content.empty()) {
1586 std::stringstream s;
1587 s << "##Funct" << std::hex << wh << std::dec << ",";
1588 error=s.str();
1589 ok = false;
1590 }
1591 if (arity==-1) arity=static_cast<int>(input->readULong(1));
1592 break;
1593 }
1594 if (!ok) {
1595 input->seek(pos, librevenge::RVNG_SEEK_SET);
1596 break;
1597 }
1598 if (noneInstr) continue;
1599 std::vector<MWAWCellContent::FormulaInstruction> child;
1600 if (instr.m_type!=MWAWCellContent::FormulaInstruction::F_Function) {
1601 child.push_back(instr);
1602 stack.push_back(child);
1603 continue;
1604 }
1605 size_t numElt = stack.size();
1606 if (static_cast<int>(numElt) < arity) {
1607 std::stringstream s;
1608 s << instr.m_content << "[##" << arity << "]";
1609 error=s.str();
1610 ok = false;
1611 break;
1612 }
1613 // special case: AddDays(x,y) => (x+y)
1614 if (arity==2 && instr.m_content=="AddDays") {
1615 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
1616 instr.m_content="(";
1617 child.push_back(instr);
1618 for (int i = 0; i < arity; i++) {
1619 if (i) {
1620 instr.m_content="+";
1621 child.push_back(instr);
1622 }
1623 auto const &node=stack[size_t(static_cast<int>(numElt)-arity+i)];
1624 child.insert(child.end(), node.begin(), node.end());
1625 }
1626 instr.m_content=")";
1627 child.push_back(instr);
1628
1629 stack.resize(size_t(static_cast<int>(numElt)-arity+1));
1630 stack[size_t(static_cast<int>(numElt)-arity)] = child;
1631 continue;
1632 }
1633 // special case: AddYears(x,y) => Date(Year(x)+y,Month(x),Day(x))
1634 if (arity==2 && instr.m_content=="AddYears") {
1635 instr.m_type=MWAWCellContent::FormulaInstruction::F_Function;
1636 instr.m_content="Date";
1637 child.push_back(instr);
1638 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
1639 instr.m_content="(";
1640 child.push_back(instr);
1641 //
1642 auto const &date=stack[size_t(static_cast<int>(numElt)-2)];
1643 auto const &addYear=stack.back();
1644 instr.m_type=MWAWCellContent::FormulaInstruction::F_Function;
1645 instr.m_content="Year";
1646 child.push_back(instr);
1647 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
1648 instr.m_content="(";
1649 child.push_back(instr);
1650 child.insert(child.end(), date.begin(), date.end());
1651 instr.m_content="+";
1652 child.push_back(instr);
1653 child.insert(child.end(), addYear.begin(), addYear.end());
1654 instr.m_content=")";
1655 child.push_back(instr);
1656
1657 instr.m_content=";";
1658 child.push_back(instr);
1659
1660 instr.m_type=MWAWCellContent::FormulaInstruction::F_Function;
1661 instr.m_content="Month";
1662 child.push_back(instr);
1663 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
1664 instr.m_content="(";
1665 child.push_back(instr);
1666 child.insert(child.end(), date.begin(), date.end());
1667 instr.m_content=")";
1668 child.push_back(instr);
1669
1670 instr.m_content=";";
1671 child.push_back(instr);
1672
1673 instr.m_type=MWAWCellContent::FormulaInstruction::F_Function;
1674 instr.m_content="Day";
1675 child.push_back(instr);
1676 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
1677 instr.m_content="(";
1678 child.push_back(instr);
1679 child.insert(child.end(), date.begin(), date.end());
1680 instr.m_content=")";
1681 child.push_back(instr);
1682
1683 //
1684 instr.m_content=")";
1685 child.push_back(instr);
1686
1687 stack.resize(size_t(static_cast<int>(numElt)-arity+1));
1688 stack[size_t(static_cast<int>(numElt)-arity)] = child;
1689 continue;
1690 }
1691 if ((instr.m_content[0] >= 'A' && instr.m_content[0] <= 'Z') || instr.m_content[0] == '(') {
1692 if (instr.m_content[0] != '(')
1693 child.push_back(instr);
1694
1695 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
1696 instr.m_content="(";
1697 child.push_back(instr);
1698 for (int i = 0; i < arity; i++) {
1699 if (i) {
1700 instr.m_content=";";
1701 child.push_back(instr);
1702 }
1703 auto const &node=stack[size_t(static_cast<int>(numElt)-arity+i)];
1704 child.insert(child.end(), node.begin(), node.end());
1705 }
1706 instr.m_content=")";
1707 child.push_back(instr);
1708
1709 stack.resize(size_t(static_cast<int>(numElt)-arity+1));
1710 stack[size_t(static_cast<int>(numElt)-arity)] = child;
1711 continue;
1712 }
1713 if (arity==1) {
1714 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
1715 stack[numElt-1].insert(stack[numElt-1].begin(), instr);
1716 continue;
1717 }
1718 if (arity==2) {
1719 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
1720 stack[numElt-2].push_back(instr);
1721 stack[numElt-2].insert(stack[numElt-2].end(), stack[numElt-1].begin(), stack[numElt-1].end());
1722 stack.resize(numElt-1);
1723 continue;
1724 }
1725 ok=false;
1726 error = "### unexpected arity";
1727 }
1728 pos=input->tell();
1729 if (pos!=endPos || !ok || stack.size()!=1 || stack[0].empty()) {
1730 MWAW_DEBUG_MSG(("WingzParser::readFormula: can not read a formula\n"));
1731 ascii().addDelimiter(pos, '|');
1732 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1733
1734 for (auto const &i : stack) {
1735 for (auto const &j : i)
1736 f << j << ",";
1737 }
1738 if (!error.empty())
1739 f << error;
1740 else
1741 f << "##unknownError,";
1742 ascii().addPos(debPos);
1743 ascii().addNote(f.str().c_str());
1744 return true;
1745 }
1746
1747 m_state->m_spreadsheet.m_formulaMap[id]=stack[0];
1748 for (auto const &i : stack[0])
1749 f << i;
1750 f << ",";
1751 ascii().addPos(debPos);
1752 ascii().addNote(f.str().c_str());
1753 return true;
1754 }
1755
1756 // column/row dim
1757 bool WingzParser::readSpreadsheetSize()
1758 {
1759 MWAWInputStreamPtr input = getInput();
1760 int const vers=version();
1761 long pos=input->tell();
1762 auto type=static_cast<int>(input->readULong(1));
1763 if (type!=1 && type!=2) return false;
1764 libmwaw::DebugStream f;
1765 f << "Entries(SheetSize)[" << (type==1 ? "col" : "row") << "]:";
1766 auto val=static_cast<int>(input->readULong(1));
1767 if (val!=0x80) f << "fl=" << std::hex << val << std::dec << ",";
1768 auto dSz= static_cast<int>(input->readULong(2));
1769 if (dSz%4 || !input->checkPosition(pos+(vers==1 ? 4 : 6)+dSz)) {
1770 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheetSize: find bad size for data\n"));
1771 return false;
1772 }
1773 if (vers>1) {
1774 auto id=static_cast<int>(input->readLong(2));
1775 if (id) f << "id=" << id << ",";
1776 }
1777 f << "pos=[";
1778 std::vector<float> &dimList=type==1 ? m_state->m_spreadsheet.m_widthCols :
1779 m_state->m_spreadsheet.m_heightRows;
1780 for (int i=0; i<dSz/4; ++i) {
1781 auto cell=static_cast<int>(input->readULong(2)); // the row/col number
1782 float dim=float(input->readULong(2))/20.f; // in TWIP
1783 if (cell==0xFFFF) f << "-inf";
1784 else if (cell==0x7FFF) {
1785 if (type==1) m_state->m_spreadsheet.m_widthDefault=dim;
1786 else m_state->m_spreadsheet.m_heightDefault=dim;
1787 f << "inf";
1788 }
1789 else {
1790 if (cell < int(dimList.size()) || cell > int(dimList.size())+1000) {
1791 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheetSize: the cell seems bad\n"));
1792 f << "###";
1793 }
1794 else
1795 dimList.resize(size_t(cell)+1, dim);
1796 f << cell;
1797 }
1798 f << ":" << dim << "pt,";
1799 }
1800 f << "],";
1801 ascii().addPos(pos);
1802 ascii().addNote(f.str().c_str());
1803
1804 return true;
1805 }
1806
1807 // page break pos
1808 bool WingzParser::readSpreadsheetPBreak()
1809 {
1810 MWAWInputStreamPtr input = getInput();
1811 int const vers=version();
1812 long pos=input->tell();
1813 auto type=static_cast<int>(input->readULong(1));
1814 if (type!=18 && type!=19) return false;
1815 libmwaw::DebugStream f;
1816 f << "Entries(SheetPbrk)[" << (type==18 ? "col" : "row") << "]:";
1817 auto val=static_cast<int>(input->readULong(1));
1818 if (val!=0x80) f << "fl=" << std::hex << val << std::dec << ",";
1819 auto dSz= static_cast<int>(input->readULong(2));
1820 if (dSz%4 || !input->checkPosition(pos+(vers==1 ? 4 : 6)+dSz)) {
1821 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheetPBreak: find bad size for data\n"));
1822 return false;
1823 }
1824 if (vers==2) {
1825 auto id=static_cast<int>(input->readLong(2));
1826 if (id) f << "id=" << id << ",";
1827 }
1828 f << "pos=[";
1829 for (int i=0; i<dSz/4; ++i) {
1830 auto cell=static_cast<int>(input->readULong(2)); // the row/col number
1831 if (cell==0xFFFF) f << "-inf";
1832 else if (cell==0x7FFF) f << "inf";
1833 else f << cell;
1834 f << "[sz=" << input->readULong(2) << "],"; // num row/column
1835 }
1836 f << "],";
1837 ascii().addPos(pos);
1838 ascii().addNote(f.str().c_str());
1839
1840 return true;
1841 }
1842
1843 // unknown zone
1844 bool WingzParser::readSpreadsheetZone5()
1845 {
1846 MWAWInputStreamPtr input = getInput();
1847 long pos=input->tell();
1848 auto type=static_cast<int>(input->readULong(1));
1849 if (type!=5) return false;
1850 auto val=static_cast<int>(input->readULong(1));
1851 auto dSz= static_cast<int>(input->readULong(2));
1852 long endPos=pos+6+dSz;
1853 auto id=static_cast<int>(input->readLong(2));
1854
1855 libmwaw::DebugStream f;
1856 f << "Entries(ZSheet5)[" << id << "]:";
1857 if (val!=0x40) f << "fl=" << std::hex << val << std::dec << ",";
1858 if (dSz<2 || !input->checkPosition(endPos)) {
1859 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheetZone5: find bad size for data\n"));
1860 return false;
1861 }
1862 val=static_cast<int>(input->readULong(2));
1863 if (val!=dSz) f << "#dSz=" << val << ",";
1864 ascii().addPos(pos);
1865 ascii().addNote(f.str().c_str());
1866
1867 while (!input->isEnd()) {
1868 pos=input->tell();
1869 if (pos >= endPos) break;
1870 type=static_cast<int>(input->readLong(1));
1871 bool ok=true;
1872 f.str("");
1873 f << "ZSheet5-" << std::hex << type << std::dec << ":";
1874 switch (type) {
1875 case 0: // nothing
1876 case 0x4:
1877 break;
1878 case 0x3:
1879 if (pos+4>endPos) {
1880 ok=false;
1881 break;
1882 }
1883 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1884 break;
1885 case 0x5: // the row?
1886 if (pos+5>endPos) {
1887 ok=false;
1888 break;
1889 }
1890 input->seek(pos+5, librevenge::RVNG_SEEK_SET);
1891 break;
1892 case 0x1:
1893 case 0x2:
1894 if (pos+3>endPos) {
1895 ok=false;
1896 break;
1897 }
1898 input->seek(pos+3, librevenge::RVNG_SEEK_SET);
1899 break;
1900 default:
1901 ok=false;
1902 break;
1903 }
1904 if (!ok) {
1905 input->seek(pos, librevenge::RVNG_SEEK_SET);
1906 break;
1907 }
1908 ascii().addPos(pos);
1909 ascii().addNote(f.str().c_str());
1910 }
1911 pos=input->tell();
1912 if (pos==endPos)
1913 return true;
1914 MWAW_DEBUG_MSG(("WingzParser::readSpreadsheetZone5: find some extra data\n"));
1915 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1916 ascii().addPos(pos);
1917 ascii().addNote("ZSheet5-end:###");
1918 return true;
1919 }
1920
1921 // retrieve a next spreadheet zone (used when parsing stop for some problem )
1922 bool WingzParser::findNextZone(int lastType)
1923 {
1924 MWAWInputStreamPtr input = getInput();
1925 bool lastCheck=true;
1926 while (!input->isEnd()) {
1927 long pos=input->tell();
1928 if (!input->checkPosition(pos+8))
1929 return false;
1930 auto val=static_cast<int>(input->readULong(2));
1931 int type=val&0xFF;
1932 if (type==0x80) {
1933 if (!lastCheck) {
1934 input->seek(-3, librevenge::RVNG_SEEK_CUR);
1935 lastCheck=true;
1936 }
1937 continue;
1938 }
1939 lastCheck=false;
1940 if ((val&0xff00)!=0x8000 || (lastType==0 && type!=1) || type>=0x14 || type<lastType)
1941 continue;
1942 auto dSz=long(input->readULong(2));
1943 if (type==0xc) dSz+=4;
1944 else if (type==0x10) dSz+=4;
1945 else if (type==0xe) {
1946 if (dSz < 0x80) dSz += 2;
1947 else if (input->checkPosition(pos+0x40)) {
1948 input->seek(pos, librevenge::RVNG_SEEK_SET);
1949 return true;
1950 }
1951 else {
1952 input->seek(pos+2, librevenge::RVNG_SEEK_SET);
1953 continue;
1954 }
1955 }
1956 if (input->checkPosition(pos+6+dSz+2)) {
1957 input->seek(pos+6+dSz+1,librevenge::RVNG_SEEK_SET);
1958 val=static_cast<int>(input->readULong(1));
1959 if ((val&0xC0) && !(val&0x3F)) {
1960 input->seek(pos, librevenge::RVNG_SEEK_SET);
1961 return true;
1962 }
1963 }
1964 input->seek(pos+2, librevenge::RVNG_SEEK_SET);
1965 }
1966 return false;
1967 }
1968
1969 ////////////////////////////////////////////////////////////
1970 // macros
1971 ////////////////////////////////////////////////////////////
1972 bool WingzParser::readMacro()
1973 {
1974 MWAWInputStreamPtr input = getInput();
1975 long pos = input->tell();
1976 if (!input->checkPosition(pos+76)) {
1977 MWAW_DEBUG_MSG(("WingzParser::readMacro: the zone seems too short\n"));
1978 input->seek(pos, librevenge::RVNG_SEEK_SET);
1979 return false;
1980 }
1981
1982 libmwaw::DebugStream f;
1983 f << "Entries(Macro):";
1984 auto textSize=long(input->readULong(4));
1985 f << "textSize=" << std::hex << textSize << std::dec << ",";
1986 auto scriptSize=long(input->readULong(4));
1987 f << "scriptSize=" << std::hex << scriptSize << std::dec << ",";
1988 for (int i=0; i<3; ++i) {
1989 auto sz=long(input->readULong(4));
1990 if (sz!=scriptSize)
1991 f << "sel" << i << "=" << std::hex << sz << std::dec << ",";
1992 }
1993 for (int i=0; i<28; ++i) { // f22=f24=0|1|2
1994 auto val=static_cast<int>(input->readLong(2));
1995 if (val) f << "f" << i << "=" << val << ",";
1996 }
1997 ascii().addPos(pos);
1998 ascii().addNote(f.str().c_str());
1999
2000 pos=input->tell();
2001 if (!scriptSize || !input->checkPosition(pos+scriptSize)) {
2002 MWAW_DEBUG_MSG(("WingzParser::readMacro: the script size seems bad\n"));
2003 input->seek(pos, librevenge::RVNG_SEEK_SET);
2004 return false;
2005 }
2006 f.str("");
2007 f << "Macro[script]:";
2008 ascii().addPos(pos);
2009 ascii().addNote(f.str().c_str());
2010 input->seek(pos+scriptSize, librevenge::RVNG_SEEK_SET);
2011
2012 pos=input->tell();
2013 if (!input->checkPosition(pos+textSize)) {
2014 MWAW_DEBUG_MSG(("WingzParser::readMacro: the text size seems bad\n"));
2015 input->seek(pos, librevenge::RVNG_SEEK_SET);
2016 return false;
2017 }
2018 f.str("");
2019 f << "Macro[text]:";
2020 std::string text("");
2021 for (long i=0; i<textSize; ++i) text+=char(input->readULong(1));
2022 f << text;
2023 ascii().addPos(pos);
2024 ascii().addNote(f.str().c_str());
2025 input->seek(pos+textSize, librevenge::RVNG_SEEK_SET);
2026 return true;
2027 }
2028
2029 ////////////////////////////////////////////////////////////
2030 // decode an encrypted file
2031 ////////////////////////////////////////////////////////////
2032 bool WingzParser::decodeEncrypted()
2033 {
2034 MWAWInputStreamPtr input = getInput();
2035 long length=input->size();
2036 if (length<=13) {
2037 MWAW_DEBUG_MSG(("WingzParser::decodeEncrypted: the file seems too short\n"));
2038 return false;
2039 }
2040 input->seek(0, librevenge::RVNG_SEEK_SET);
2041 unsigned long read;
2042 uint8_t const *data=input->read(size_t(length), read);
2043 if (!data || length!=long(read)) {
2044 MWAW_DEBUG_MSG(("WingzParser::decodeEncrypted: can not read the buffer\n"));
2045 return false;
2046 }
2047 std::unique_ptr<uint8_t[]> buffer{new uint8_t[size_t(length)]};
2048 if (!buffer) {
2049 MWAW_DEBUG_MSG(("WingzParser::decodeEncrypted: can not allocate a buffer\n"));
2050 return false;
2051 }
2052 // the first 12 bytes encoded
2053 for (size_t i=0; i<12; i++) buffer[i]=data[i];
2054 // reset the crypt flag to avoid problem
2055 buffer[12]=0;
2056 // the next data are encrypted using a basic xor method...
2057 int delta=0;
2058 for (long i=13; i<length; ++i) {
2059 uint8_t const codeString[]= { 0x53, 0x66, 0xA5, 0x35, 0x5A, 0xAA, 0x55, 0xE3 };
2060 auto v=uint8_t((codeString[(delta&7)]+delta)&0xFF);
2061 buffer[size_t(i)]=(data[i]^v);
2062 delta++;
2063 }
2064
2065 // finally replace the actual input with a new input
2066 std::shared_ptr<librevenge::RVNGInputStream> newInput
2067 (new MWAWStringStream(buffer.get(), static_cast<unsigned int>(length)));
2068 getParserState()->m_input.reset(new MWAWInputStream(newInput, false));
2069 return true;
2070 }
2071
2072 ////////////////////////////////////////////////////////////
2073 // read the header
2074 ////////////////////////////////////////////////////////////
2075 bool WingzParser::checkHeader(MWAWHeader *header, bool /*strict*/)
2076 {
2077 *m_state = WingzParserInternal::State();
2078
2079 MWAWInputStreamPtr input = getInput();
2080 if (!input || !input->hasDataFork())
2081 return false;
2082
2083 int const headerSize=13;
2084 if (!input->checkPosition(headerSize)) {
2085 MWAW_DEBUG_MSG(("WingzParser::checkHeader: file is too short\n"));
2086 return false;
2087 }
2088 input->seek(0,librevenge::RVNG_SEEK_SET);
2089 int values[4];
2090 for (auto &val : values) val=static_cast<int>(input->readULong(2));
2091 bool isWingz=true;
2092 if (values[0]==0x574e && values[1]==0x475a && values[2]==0x575a && values[3]==0x5353) // WNGZWZSS
2093 isWingz=true;
2094 else if (values[0]==0x4241 && values[1]==0x545F && values[2]==0x4254 && values[3]==0x5353) // BAT_BTSS
2095 isWingz=false;
2096 else
2097 return false;
2098 setVersion(isWingz ? 2 : 1);
2099 input->setReadInverted(true);
2100 libmwaw::DebugStream f;
2101 f << "FileHeader:";
2102 std::string name(""); // 0110: version number
2103 for (int i=0; i<4; ++i)
2104 name += char(input->readULong(1));
2105 f << "vers=" << name << ",";
2106 auto val=static_cast<int>(input->readLong(1));
2107 if (val==1) { // SgB
2108 MWAW_DEBUG_MSG(("WingzParser::checkHeader: Find an encrypted file...\n"));
2109 m_state->m_encrypted=true;
2110 }
2111 else if (val) {
2112 MWAW_DEBUG_MSG(("WingzParser::checkHeader: Find unknown encryped flag...\n"));
2113 return false;
2114 }
2115 ascii().addPos(0);
2116 ascii().addNote(f.str().c_str());
2117 if (header)
2118 header->reset(isWingz ? MWAWDocument::MWAW_T_WINGZ : MWAWDocument::MWAW_T_CLARISRESOLVE, 1, MWAWDocument::MWAW_K_SPREADSHEET);
2119 input->seek(12,librevenge::RVNG_SEEK_SET);
2120 input->setReadInverted(false);
2121 return true;
2122 }
2123
2124 ////////////////////////////////////////////////////////////
2125 // read the print info
2126 ////////////////////////////////////////////////////////////
2127 bool WingzParser::readPrintInfo()
2128 {
2129 MWAWInputStreamPtr input = getInput();
2130 int const vers=version();
2131 long pos = input->tell();
2132 auto type=static_cast<int>(input->readULong(1));
2133 if (type!=0x10) return false;
2134 auto val=static_cast<int>(input->readULong(1));
2135 auto dSz=static_cast<int>(input->readULong(2));
2136 int id=vers==1 ? 0 : static_cast<int>(input->readULong(2));
2137 int expectedSize=vers==1 ? 0x8a : 0x7c;
2138 long endPos=pos+(vers==1 ? 4+0x8a : 20+0x7c);
2139 if (dSz!=expectedSize || !input->checkPosition(endPos)) {
2140 MWAW_DEBUG_MSG(("WingzParser::readPrintInfo: the header seem bad\n"));
2141 return false;
2142 }
2143 libmwaw::DebugStream f;
2144 f << "Entries(PrintInfo):";
2145 if (val!=0x80) f << "fl=" << std::hex << val << std::dec << ",";
2146 if (id) f << "id=" << id << ",";
2147 for (int i=0; i<3; ++i) {
2148 int dim[2];
2149 for (auto &d: dim) d=static_cast<int>(input->readULong(2));
2150 if (i==2)
2151 f << "unit=" << dim[0] << "x" << dim[1] << ",";
2152 else
2153 f << "dim" << i << "=" << dim[0] << "x" << dim[1] << ",";
2154 }
2155 // 3 small number 0x78,4,6|7
2156 for (int i=0; i<3; ++i) {
2157 val=static_cast<int>(input->readULong(2));
2158 if (val) f << "f" << i << "=" << val << ",";
2159 }
2160 // print info
2161 libmwaw::PrinterInfo info;
2162 input->setReadInverted(false);
2163 bool ok=info.read(input);
2164 input->setReadInverted(true);
2165 if (!ok) return false;
2166 f << info;
2167
2168 MWAWVec2i paperSize = info.paper().size();
2169 MWAWVec2i pageSize = info.page().size();
2170 if (pageSize.x() <= 0 || pageSize.y() <= 0 ||
2171 paperSize.x() <= 0 || paperSize.y() <= 0) return false;
2172
2173 // define margin from print info
2174 MWAWVec2i lTopMargin= -1 * info.paper().pos(0);
2175 MWAWVec2i rBotMargin=info.paper().size() - info.page().size();
2176
2177 // move margin left | top
2178 int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0;
2179 int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0;
2180 lTopMargin -= MWAWVec2i(decalX, decalY);
2181 rBotMargin += MWAWVec2i(decalX, decalY);
2182
2183 // decrease right | bottom
2184 int rightMarg = rBotMargin.x() -50;
2185 if (rightMarg < 0) rightMarg=0;
2186 int botMarg = rBotMargin.y() -50;
2187 if (botMarg < 0) botMarg=0;
2188
2189 getPageSpan().setMarginTop(lTopMargin.y()/72.0);
2190 getPageSpan().setMarginBottom(botMarg/72.0);
2191 getPageSpan().setMarginLeft(lTopMargin.x()/72.0);
2192 getPageSpan().setMarginRight(rightMarg/72.0);
2193 getPageSpan().setFormLength(paperSize.y()/72.);
2194 getPageSpan().setFormWidth(paperSize.x()/72.);
2195
2196 ascii().addPos(pos);
2197 ascii().addNote(f.str().c_str());
2198
2199 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2200 return true;
2201 }
2202
2203 ////////////////////////////////////////////////////////////
2204 // send spreadsheet
2205 ////////////////////////////////////////////////////////////
2206 bool WingzParser::sendSpreadsheet()
2207 {
2208 MWAWSpreadsheetListenerPtr listener=getSpreadsheetListener();
2209 MWAWInputStreamPtr &input= getInput();
2210 if (!listener) {
2211 MWAW_DEBUG_MSG(("WingzParser::sendSpreadsheet: I can not find the listener\n"));
2212 return false;
2213 }
2214 auto &sheet = m_state->m_spreadsheet;
2215 listener->openSheet(sheet.convertInPoint(sheet.m_widthCols), librevenge::RVNG_POINT, std::vector<int>(), sheet.m_name);
2216 m_graphParser->sendPageGraphics();
2217
2218 int prevRow = -1;
2219 for (auto &cell : sheet.m_cells) {
2220 if (cell.position()[1]>prevRow+1) {
2221 while (cell.position()[1] > prevRow+1) {
2222 if (prevRow != -1) listener->closeSheetRow();
2223 int numRepeat;
2224 float h=sheet.getRowHeight(prevRow+1, numRepeat);
2225 if (cell.position()[1]<prevRow+1+numRepeat)
2226 numRepeat=cell.position()[1]-1-prevRow;
2227 listener->openSheetRow(h, librevenge::RVNG_POINT, numRepeat);
2228 prevRow+=numRepeat;
2229 }
2230 }
2231 if (cell.position()[1] > prevRow) {
2232 if (prevRow != -1) listener->closeSheetRow();
2233 listener->openSheetRow(sheet.getRowHeight(++prevRow), librevenge::RVNG_POINT);
2234 }
2235 sheet.update(cell);
2236 listener->openSheetCell(cell, cell.m_content);
2237 if (cell.m_content.m_textEntry.valid()) {
2238 listener->setFont(cell.getFont());
2239 input->seek(cell.m_content.m_textEntry.begin(), librevenge::RVNG_SEEK_SET);
2240 while (!input->isEnd() && input->tell()<cell.m_content.m_textEntry.end()) {
2241 auto c=static_cast<unsigned char>(input->readULong(1));
2242 if (c==0xd)
2243 listener->insertEOL();
2244 else
2245 listener->insertCharacter(c);
2246 }
2247 }
2248 listener->closeSheetCell();
2249 }
2250 if (prevRow!=-1) listener->closeSheetRow();
2251 listener->closeSheet();
2252 return true;
2253 }
2254
2255 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: