Debian Codegraph @

src/lib/MultiplanParser.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 <cmath>
35 #include <cstring>
36 #include <iomanip>
37 #include <iostream>
38 #include <limits>
39 #include <map>
40 #include <set>
41 #include <sstream>
42
43 #include <librevenge/librevenge.h>
44
45 #include "MWAWCell.hxx"
46 #include "MWAWFont.hxx"
47 #include "MWAWFontConverter.hxx"
48 #include "MWAWHeader.hxx"
49 #include "MWAWParagraph.hxx"
50 #include "MWAWPosition.hxx"
51 #include "MWAWSpreadsheetListener.hxx"
52 #include "MWAWSubDocument.hxx"
53
54 #include "MultiplanParser.hxx"
55
56 /** Internal: the structures of a MultiplanParser */
57 namespace MultiplanParserInternal
58 {
59 ////////////////////////////////////////
60 //! Internal: the state of a MultiplanParser
61 struct State {
62 //! constructor
63 State()
64 : m_font()
65 , m_maximumCell()
66 , m_columnPositions()
67 , m_cellPositions()
68 , m_cellPositionsSet()
69 , m_posToLinkMap()
70 , m_posToNameMap()
71 , m_posToSharedDataSeen()
72 {
73 }
74 //! returns the column width in point
75 std::vector<float> getColumnsWidth() const;
76 //! the default font
77 MWAWFont m_font;
78 //! the maximumCell
79 MWAWVec2i m_maximumCell;
80 //! the columns begin position in point
81 std::vector<int> m_columnPositions;
82 //! the header/footer/printer message entries
83 MWAWEntry m_hfpEntries[3];
84 //! the positions of each cell: a vector for each row
85 std::vector<std::vector<int> > m_cellPositions;
86 //! the list of all position (use for checking)
87 std::set<int> m_cellPositionsSet;
88 //! the different main spreadsheet zones
89 MWAWEntry m_entries[9];
90 //! the list of link instruction
91 std::map<int, MWAWCellContent::FormulaInstruction> m_posToLinkMap;
92 //! the map name's pos to name's cell instruction
93 std::map<int, MWAWCellContent::FormulaInstruction> m_posToNameMap;
94 //! a set a shared data already seen
95 std::set<int> m_posToSharedDataSeen;
96 };
97
98 std::vector<float> State::getColumnsWidth() const
99 {
100 std::vector<float> res;
101 bool first=true;
102 int lastPos=0;
103 float const defWidth=64.f;
104 for (auto p : m_columnPositions) {
105 if (first) {
106 first=false;
107 continue;
108 }
109 if (p<lastPos)
110 res.push_back(defWidth);
111 else
112 res.push_back(float(p-lastPos));
113 lastPos=p;
114 }
115 if (res.size()<64) res.resize(64, defWidth);
116 return res;
117 }
118
119 ////////////////////////////////////////
120 //! Internal: the subdocument of a MultiplanParserInternal
121 class SubDocument final : public MWAWSubDocument
122 {
123 public:
124 SubDocument(MultiplanParser &parser, MWAWInputStreamPtr const &input, MWAWEntry const &entry)
125 : MWAWSubDocument(&parser, input, entry)
126 , m_multiParser(parser)
127 {
128 }
129
130 //! destructor
131 ~SubDocument() final {}
132
133 //! operator!=
134 bool operator!=(MWAWSubDocument const &doc) const final;
135
136 //! the parser function
137 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
138
139 protected:
140 /** the main parser */
141 MultiplanParser &m_multiParser;
142 };
143
144 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/)
145 {
146 if (!listener.get()) {
147 MWAW_DEBUG_MSG(("MultiplanParser::SubDocument::parse: no listener\n"));
148 return;
149 }
150
151 long pos = m_input->tell();
152 m_multiParser.sendText(m_zone);
153 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
154 }
155
156 bool SubDocument::operator!=(MWAWSubDocument const &doc) const
157 {
158 if (MWAWSubDocument::operator!=(doc)) return true;
159 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
160 if (!sDoc) return true;
161 if (&m_multiParser != &sDoc->m_multiParser) return true;
162 return false;
163 }
164 }
165
166 ////////////////////////////////////////////////////////////
167 // constructor/destructor, ...
168 ////////////////////////////////////////////////////////////
169 MultiplanParser::MultiplanParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
170 : MWAWSpreadsheetParser(input, rsrcParser, header)
171 , m_state(new MultiplanParserInternal::State)
172 {
173 setAsciiName("main-1");
174 // reduce the margin (in case, the page is not defined)
175 getPageSpan().setMargins(0.1);
176 }
177
178 MultiplanParser::~MultiplanParser()
179 {
180 }
181
182 ////////////////////////////////////////////////////////////
183 // the parser
184 ////////////////////////////////////////////////////////////
185 void MultiplanParser::parse(librevenge::RVNGSpreadsheetInterface *docInterface)
186 {
187 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
188 bool ok = true;
189 try {
190 // create the asciiFile
191 ascii().setStream(getInput());
192 ascii().open(asciiName());
193 checkHeader(nullptr);
194 ok = createZones();
195 if (ok) {
196 createDocument(docInterface);
197 sendSpreadsheet();
198 }
199 }
200 catch (...) {
201 MWAW_DEBUG_MSG(("MultiplanParser::parse: exception catched when parsing\n"));
202 ok = false;
203 }
204
205 ascii().reset();
206 resetSpreadsheetListener();
207 if (!ok) throw(libmwaw::ParseException());
208 }
209
210 ////////////////////////////////////////////////////////////
211 // create the document
212 ////////////////////////////////////////////////////////////
213 void MultiplanParser::createDocument(librevenge::RVNGSpreadsheetInterface *documentInterface)
214 {
215 if (!documentInterface) return;
216 if (getSpreadsheetListener()) {
217 MWAW_DEBUG_MSG(("MultiplanParser::createDocument: listener already exist\n"));
218 return;
219 }
220
221 // create the page list
222 MWAWPageSpan ps(getPageSpan());
223 ps.setPageSpan(1);
224 for (int i=0; i<2; ++i) {
225 if (!m_state->m_hfpEntries[i].valid()) continue;
226 MWAWHeaderFooter header(i==0 ? MWAWHeaderFooter::HEADER : MWAWHeaderFooter::FOOTER, MWAWHeaderFooter::ALL);
227 header.m_subDocument.reset
228 (new MultiplanParserInternal::SubDocument
229 (*this, getInput(), m_state->m_hfpEntries[i]));
230 ps.setHeaderFooter(header);
231 }
232 std::vector<MWAWPageSpan> pageList(1,ps);
233 //
234 MWAWSpreadsheetListenerPtr listen(new MWAWSpreadsheetListener(*getParserState(), pageList, documentInterface));
235 setSpreadsheetListener(listen);
236 listen->startDocument();
237 }
238
239
240 ////////////////////////////////////////////////////////////
241 //
242 // Intermediate level
243 //
244 ////////////////////////////////////////////////////////////
245 bool MultiplanParser::createZones()
246 {
247 if (!readPrinterMessage() || !readZoneB()) return false;
248 if (!readColumnsPos() || !readPrinterInfo()) return false;
249 if (!readHeaderFooter() || !readZoneC()) return false;
250 if (!readZonesList()) return false;
251 MWAWInputStreamPtr input = getInput();
252 if (!input->isEnd()) {
253 MWAW_DEBUG_MSG(("MultiplanParser::createZones: find extra data\n"));
254 ascii().addPos(input->tell());
255 ascii().addNote("Entries(Unknown):###extra");
256 }
257 return true;
258 }
259
260 ////////////////////////////////////////////////////////////
261 //
262 // Low level
263 //
264 ////////////////////////////////////////////////////////////
265
266 ////////////////////////////////////////////////////////////
267 // spreadsheet
268 ////////////////////////////////////////////////////////////
269 bool MultiplanParser::readHeaderFooter()
270 {
271 MWAWInputStreamPtr input = getInput();
272 long pos = input->tell();
273 if (!input->checkPosition(pos+2*256)) {
274 MWAW_DEBUG_MSG(("MultiplanParser::readHeaderFooter: the zone seems too short\n"));
275 return false;
276 }
277 libmwaw::DebugStream f;
278 for (int i=0; i<2; ++i) {
279 pos = input->tell();
280 f.str("");
281 f << "Entries(HF)[" << (i==0 ? "header" : "footer") << "]:";
282 int sSz=int(input->readULong(1));
283 m_state->m_hfpEntries[i].setBegin(pos+1);
284 m_state->m_hfpEntries[i].setLength(sSz);
285 std::string name;
286 for (int c=0; c<sSz; ++c) name+=char(input->readULong(1));
287 f << name;
288 ascii().addPos(pos);
289 ascii().addNote(f.str().c_str());
290 input->seek(pos+256, librevenge::RVNG_SEEK_SET);
291 }
292 return true;
293 }
294
295 bool MultiplanParser::readPrinterInfo()
296 {
297 MWAWInputStreamPtr input = getInput();
298 long pos = input->tell();
299 if (!input->checkPosition(pos+0xbc)) {
300 MWAW_DEBUG_MSG(("MultiplanParser::readPrinterInfo: the zone seems too short\n"));
301 return false;
302 }
303 libmwaw::DebugStream f;
304 f << "Entries(PrinterInfo):";
305 int val=int(input->readULong(2));
306 if (val!=0x7fff) f << "f0=" << val << ",";
307 val=int(input->readULong(2));
308 if (val) f << "f1=" << val << ",";
309 f << "left[margin]=" << input->readULong(1) << ",";
310 f << "width=" << input->readULong(1) << ",";
311 f << "right[margin]=" << input->readULong(1) << ",";
312 f << "length=" << input->readULong(1) << ",";
313 // then 0 and a string?
314 ascii().addDelimiter(input->tell(),'|');
315 ascii().addPos(pos);
316 ascii().addNote(f.str().c_str());
317 input->seek(pos+130, librevenge::RVNG_SEEK_SET);
318
319 pos=input->tell();
320 f.str("");
321 f << "PrinterInfo[II]:";
322 f << "row[pbBreak]=[";
323 for (int i=0; i<32; ++i) {
324 val=int(input->readULong(1));
325 if (!val) continue;
326 for (int d=0, depl=1; d<8; ++d, depl<<=1) {
327 if (val&depl) f << i*8+d << ",";
328 }
329 }
330 f << "],";
331 f << "col[pbBreak]=[";
332 for (int i=0; i<8; ++i) {
333 val=int(input->readULong(1));
334 if (!val) continue;
335 for (int d=0, depl=1; d<8; ++d, depl<<=1) {
336 if (val&depl) f << i*8+d << ",";
337 }
338 }
339 f << "],";
340 for (int i=0; i<7; ++i) {
341 val=int(input->readULong(2));
342 int const expected[]= {0x48,0x48,0x36,0x36,1,1,0};
343 if (val==expected[i]) continue;
344 if (i==4) {
345 if (val==0)
346 f << "print[col,row,number]=no,";
347 else
348 f << "##print[col,row,number]=" << val << ",";
349 }
350 else
351 f << "g" << i << "=" << val << ",";
352 }
353 m_state->m_font.setId(int(input->readULong(2)));
354 m_state->m_font.setSize(float(input->readULong(2)));
355 f << "font=[" << m_state->m_font.getDebugString(getFontConverter()) << "],";
356 ascii().addPos(pos);
357 ascii().addNote(f.str().c_str());
358 input->seek(pos+58, librevenge::RVNG_SEEK_SET);
359 return true;
360 }
361
362 bool MultiplanParser::readPrinterMessage()
363 {
364 MWAWInputStreamPtr input = getInput();
365 long pos = input->tell();
366 if (!input->checkPosition(pos+256)) {
367 MWAW_DEBUG_MSG(("MultiplanParser::readPrinterMessage: the zone seems too short\n"));
368 return false;
369 }
370 libmwaw::DebugStream f;
371 f << "Entries(HF)[printerMessage]:";
372 int sSz=int(input->readULong(1));
373 m_state->m_hfpEntries[2].setBegin(pos+1);
374 m_state->m_hfpEntries[2].setLength(sSz);
375 std::string name;
376 for (int c=0; c<sSz; ++c) name+=char(input->readULong(1));
377 f << name;
378 ascii().addPos(pos);
379 ascii().addNote(f.str().c_str());
380 input->seek(pos+256, librevenge::RVNG_SEEK_SET);
381 return true;
382 }
383
384 bool MultiplanParser::readColumnsPos()
385 {
386 MWAWInputStreamPtr input = getInput();
387 long pos = input->tell();
388 if (!input->checkPosition(pos+256)) {
389 MWAW_DEBUG_MSG(("MultiplanParser::readColumnsPos: the zone seems too short\n"));
390 return false;
391 }
392 libmwaw::DebugStream f;
393 f << "Entries(ColPos):pos=[";
394 for (int i=0; i<64; ++i) {
395 m_state->m_columnPositions.push_back(int(input->readULong(2)));
396 f << m_state->m_columnPositions.back() << ",";
397 }
398 f << "],";
399 ascii().addPos(pos);
400 ascii().addNote(f.str().c_str());
401 return true;
402 }
403
404 bool MultiplanParser::readZonesList()
405 {
406 MWAWInputStreamPtr input = getInput();
407 long pos = input->tell();
408 if (!input->checkPosition(pos+20)) {
409 MWAW_DEBUG_MSG(("MultiplanParser::readZonesList: the zone seems too short\n"));
410 return false;
411 }
412 libmwaw::DebugStream f;
413 f << "Entries(ZonesList):";
414 int lastPos=0;
415 f << "zones=[";
416 for (int i=0, w=0; i<10; ++i) {
417 int newPos=int(input->readULong(2));
418 if (i==6) newPos+=lastPos; // length
419 if (i==7) {
420 lastPos=newPos;
421 continue;
422 }
423 if (newPos>lastPos) {
424 if (!input->checkPosition(pos+20+newPos)) {
425 MWAW_DEBUG_MSG(("MultiplanParser::readZonesList: find a bad position"));
426 f << "###";
427 }
428 else {
429 m_state->m_entries[w].setBegin(pos+20+lastPos);
430 m_state->m_entries[w].setEnd(pos+20+newPos);
431 }
432 f << std::hex << lastPos << "<->" << newPos << std::dec << ",";
433 lastPos=newPos;
434 }
435 else
436 f << "_,";
437 ++w;
438 }
439 f << "],";
440 ascii().addPos(pos);
441 ascii().addNote(f.str().c_str());
442 for (int i=0; i<9; ++i) {
443 if (!m_state->m_entries[i].valid()) continue;
444 bool ok=false;
445 std::string name;
446 switch (i) {
447 case 1:
448 ok=readZone1(m_state->m_entries[i]);
449 break;
450 case 3:
451 ok=readCellDataPosition(m_state->m_entries[i]);
452 break;
453 case 4:
454 name="Link";
455 break;
456 case 5:
457 name="Link";
458 break;
459 case 6:
460 name="DataCell";
461 break;
462 case 7: // the data are normally read in zone 6
463 name="SharedData";
464 break;
465 case 8:
466 name="Names";
467 break;
468 default:
469 ok=false;
470 break;
471 }
472 if (ok)
473 continue;
474 f.str("");
475 if (!name.empty())
476 f << "Entries(" << name << "):";
477 else
478 f << "Entries(Zone" << i << "):";
479 ascii().addPos(m_state->m_entries[i].begin());
480 ascii().addNote(f.str().c_str());
481 ascii().addPos(m_state->m_entries[i].end());
482 ascii().addNote("_");
483 input->seek(m_state->m_entries[i].end(), librevenge::RVNG_SEEK_SET);
484 }
485 return true;
486 }
487
488 bool MultiplanParser::readZone1(MWAWEntry const &entry)
489 {
490 if (entry.length()%30) {
491 MWAW_DEBUG_MSG(("MultiplanParser::readZone1: the zone size seems bad\n"));
492 return false;
493 }
494 MWAWInputStreamPtr input = getInput();
495 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
496 libmwaw::DebugStream f;
497 f << "Entries(Zone1):";
498 ascii().addPos(entry.begin());
499 ascii().addNote(f.str().c_str());
500 int N=int(entry.length()/30);
501 for (int i=0; i<N; ++i) {
502 /* find something like that
503 0000000000fb01d80012001c00fb01d800000000000d0007ffe4ffeec000
504 00000000005500550012001c005500550000000000040001ffe4ffeec000
505 */
506 long pos=input->tell();
507 f.str("");
508 f << "Zone1-" << i << ":";
509 ascii().addPos(pos);
510 ascii().addNote(f.str().c_str());
511 input->seek(pos+30, librevenge::RVNG_SEEK_SET);
512 }
513 return true;
514 }
515
516 bool MultiplanParser::readCellDataPosition(MWAWEntry const &entry)
517 {
518 if (m_state->m_maximumCell[0]<=0 || m_state->m_maximumCell[1]<=0 || entry.length()/m_state->m_maximumCell[0]/2<m_state->m_maximumCell[1]) {
519 MWAW_DEBUG_MSG(("MultiplanParser::readCellDataPosition: the zone seems bad\n"));
520 return false;
521 }
522 MWAWInputStreamPtr input = getInput();
523 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
524 libmwaw::DebugStream f;
525 f << "Entries(DataPos):";
526 m_state->m_cellPositions.resize(size_t(m_state->m_maximumCell[0]));
527 auto &posSet=m_state->m_cellPositionsSet;
528 for (int i=0; i<m_state->m_maximumCell[0]; ++i) {
529 f << "[" << std::hex;
530 auto &cellPos=m_state->m_cellPositions[size_t(i)];
531 for (int j=0; j<m_state->m_maximumCell[1]; ++j) {
532 cellPos.push_back(int(input->readLong(2)));
533 posSet.insert(cellPos.back());
534 if (cellPos.back())
535 f << cellPos.back() << ",";
536 else
537 f << "_,";
538 }
539 f << std::dec << "],";
540 }
541 if (input->tell()!=entry.end()) {
542 MWAW_DEBUG_MSG(("MultiplanParser::readCellDataPosition: find extra data\n"));
543 f << "###extra";
544 ascii().addDelimiter(input->tell(),'|');
545 }
546 ascii().addPos(entry.begin());
547 ascii().addNote(f.str().c_str());
548 return true;
549 }
550
551 bool MultiplanParser::readLink(int pos, MWAWCellContent::FormulaInstruction &instr)
552 {
553 auto it=m_state->m_posToLinkMap.find(pos);
554 if (it!=m_state->m_posToLinkMap.end()) {
555 instr=it->second;
556 return true;
557 }
558 auto const &entry=m_state->m_entries[4];
559 if (!entry.valid() || pos<0 || pos+12>entry.length()) {
560 MWAW_DEBUG_MSG(("MultiplanParser::readLink: the pos %d seems bad\n", pos));
561 return false;
562 }
563 auto input = getInput();
564 long actPos=input->tell();
565 long begPos=entry.begin()+pos;
566 input->seek(begPos, librevenge::RVNG_SEEK_SET);
567 libmwaw::DebugStream f;
568 f << "Link-" << std::hex << pos << std::dec << "[pos]:";
569 int dSz=int(input->readULong(1));
570 if (dSz<0 || pos+12+dSz > entry.end()) {
571 MWAW_DEBUG_MSG(("MultiplanParser::readLink: the pos %d seems bad\n", pos));
572 input->seek(actPos, librevenge::RVNG_SEEK_SET);
573 return false;
574 }
575 int type=int(input->readULong(1));
576 f << "type=" << type << ",";
577 int lPos=int(input->readULong(2));
578 if (!readLinkFilename(lPos, instr))
579 f << "###";
580 f << "pos=" << std::hex << lPos << std::dec << ",";
581 int val;
582 for (int j=0; j<2; ++j) {
583 val=int(input->readULong(1));
584 int const expected[]= {0x1a,0x1a};
585 if (val!=expected[j]) f << "f" << j+2 << "=" << val << ",";
586 }
587 for (int j=0; j<3; ++j) { // f4=1|821
588 val=int(input->readULong(2));
589 if (val)
590 f << "f" << j+4 << "=" << std::hex << val << std::dec << ",";
591 }
592 bool ok=false;
593 switch (type) {
594 case 0: {
595 ok=true;
596 auto fontConverter=getFontConverter();
597 auto const fId=m_state->m_font.id();
598 librevenge::RVNGString name=instr.m_fileName;
599 name.append(':');
600 for (int i=0; i<dSz; ++i) {
601 auto ch=static_cast<unsigned char>(input->readULong(1));
602 int unicode = fontConverter->unicode(fId, static_cast<unsigned char>(ch));
603 if (unicode!=-1)
604 libmwaw::appendUnicode(uint32_t(unicode), name);
605 else if (ch==0x9 || ch > 0x1f)
606 libmwaw::appendUnicode(static_cast<uint32_t>(ch), name);
607 else {
608 f << "##";
609 MWAW_DEBUG_MSG(("MultiplanParser::readLink: name seems bad\n"));
610 }
611 }
612 instr.m_type=instr.F_Text;
613 instr.m_content=name.cstr();
614 break;
615 }
616 case 1: {
617 if (dSz<4)
618 break;
619 ok=true;
620 int rows[2], cols[2];
621 for (auto &r : rows) r=int(input->readULong(1));
622 for (auto &c : cols) c=int(input->readULong(1));
623 for (int j=0; j<2; ++j) {
624 instr.m_position[j]=MWAWVec2i(cols[j],rows[j]);
625 instr.m_positionRelative[j]=MWAWVec2b(false,false);
626 }
627 instr.m_type=instr.m_position[0]==instr.m_position[1] ? instr.F_Cell : instr.F_CellList;
628 f << instr << ",";
629 break;
630 }
631 default:
632 MWAW_DEBUG_MSG(("MultiplanParser::readLink: find unknown type %d\n", type));
633 break;
634 }
635 if (!ok) {
636 MWAW_DEBUG_MSG(("MultiplanParser::readLink: can not read link at pos %d\n", pos));
637 f << "###";
638 }
639 else
640 m_state->m_posToLinkMap[pos]=instr;
641 ascii().addPos(begPos);
642 ascii().addNote(f.str().c_str());
643 input->seek(actPos, librevenge::RVNG_SEEK_SET);
644 return ok;
645 }
646
647 bool MultiplanParser::readLinkFilename(int pos, MWAWCellContent::FormulaInstruction &instr)
648 {
649 MWAWInputStreamPtr input = getInput();
650 auto const &entry=m_state->m_entries[5];
651 if (!entry.valid() || pos<0 || pos+10>entry.length() || !input->checkPosition(entry.end())) {
652 MWAW_DEBUG_MSG(("MultiplanParser::readLinkFilename: the pos %d seems bad\n", pos));
653 return false;
654 }
655 long actPos=input->tell();
656 long begPos=entry.begin()+pos;
657 input->seek(begPos, librevenge::RVNG_SEEK_SET);
658 libmwaw::DebugStream f;
659 f << "Link-" << std::hex << pos << std::dec << ":";
660 for (int i=0; i<2; ++i) {
661 int val=int(input->readLong(2));
662 if (val!=1-i) f << "f" << i << "=" << val << ",";
663 }
664 f << "unkn=" << std::hex << input->readULong(4) << std::dec << ","; // d66aa996 | d66aab1e maybe dirId, fileId
665 int dSz=int(input->readULong(1));
666 if (begPos+9+dSz>entry.end()) {
667 MWAW_DEBUG_MSG(("MultiplanParser::readLinkFilename: the pos %d seems bad\n", pos));
668 input->seek(actPos, librevenge::RVNG_SEEK_SET);
669 return false;
670 }
671 librevenge::RVNGString filename;
672 auto fontConverter=getFontConverter();
673 auto const fId=m_state->m_font.id();
674 for (int i=0; i<dSz; ++i) {
675 auto ch=static_cast<unsigned char>(input->readULong(1));
676 int unicode = fontConverter->unicode(fId, static_cast<unsigned char>(ch));
677 if (unicode!=-1)
678 libmwaw::appendUnicode(uint32_t(unicode), filename);
679 else if (ch==0x9 || ch > 0x1f)
680 libmwaw::appendUnicode(static_cast<uint32_t>(ch), filename);
681 else {
682 f << "##";
683 MWAW_DEBUG_MSG(("MultiplanParser::readLinkFilename: dir seems bad\n"));
684 }
685 }
686 instr.m_fileName=filename;
687 f << instr.m_fileName.cstr() << ",";
688 instr.m_sheet[0]="Sheet0";
689 ascii().addPos(begPos);
690 ascii().addNote(f.str().c_str());
691 input->seek(actPos, librevenge::RVNG_SEEK_SET);
692 return true;
693 }
694
695 bool MultiplanParser::readSharedData(int pos, int cellType, MWAWVec2i const &cellPos, MWAWCellContent &content)
696 {
697 auto const &entry=m_state->m_entries[7];
698 MWAWInputStreamPtr input = getInput();
699 if (!entry.valid() || pos<0 || pos+3>entry.length() || !input->checkPosition(entry.end())) {
700 MWAW_DEBUG_MSG(("MultiplanParser::readSharedData: the pos %d seems bad\n", pos));
701 return false;
702 }
703 long actPos=input->tell();
704 long begPos=entry.begin()+pos;
705 input->seek(begPos, librevenge::RVNG_SEEK_SET);
706 libmwaw::DebugStream f;
707 f << "SharedData-" << std::hex << pos << std::dec << ":";
708 int type=int(input->readULong(2));
709 f << "type=" << (type&3) << ",";
710 int N=(type/4);
711 if (N!=2) f << "used=" << N << ",";
712 int dSz=int(input->readULong(1));
713 long endPos=begPos+3+dSz;
714 if (endPos>entry.end()) {
715 MWAW_DEBUG_MSG(("MultiplanParser::readSharedData: the pos %d seems bad\n", pos));
716 input->seek(actPos, librevenge::RVNG_SEEK_SET);
717 return false;
718 }
719 bool ok=true;
720 switch (type&3) {
721 case 0:
722 switch (cellType&3) {
723 case 0: {
724 double value;
725 if (dSz!=8 || !readDouble(value))
726 ok=false;
727 else {
728 content.m_contentType=content.C_NUMBER;
729 content.setValue(value);
730 f << value << ",";
731 }
732 break;
733 }
734 case 1: {
735 content.m_contentType=content.C_TEXT;
736 content.m_textEntry.setBegin(input->tell());
737 content.m_textEntry.setLength(dSz);
738 std::string name;
739 for (int c=0; c<dSz; ++c) name+=char(input->readULong(1));
740 f << name << ",";
741 break;
742 }
743 case 2:
744 if (dSz!=8)
745 ok=false;
746 else {
747 f << "Nan" << input->readULong(1) << ",";
748 input->seek(7, librevenge::RVNG_SEEK_CUR);
749 content.m_contentType=content.C_NUMBER;
750 content.setValue(std::nan(""));
751 }
752 break;
753 case 3:
754 default:
755 if (dSz!=8)
756 ok=false;
757 else {
758 int val=int(input->readULong(1));
759 content.m_contentType=content.C_NUMBER;
760 content.setValue(val);
761 if (val==0)
762 f << "false,";
763 else if (val==1)
764 f << "true,";
765 else
766 f << "##bool=" << val << ",";
767 input->seek(7, librevenge::RVNG_SEEK_CUR);
768 }
769 break;
770 }
771 break;
772 case 1: {
773 std::string err;
774 if (!readFormula(cellPos, content.m_formula, endPos, err))
775 f << "###";
776 else
777 content.m_contentType=content.C_FORMULA;
778 for (auto const &fo : content.m_formula) f << fo;
779 f << ",";
780 f << err;
781 break;
782 }
783 default:
784 ok=false;
785 break;
786 }
787 if (!ok) {
788 MWAW_DEBUG_MSG(("MultiplanParser::readSharedData: can not read data for the pos %d\n", pos));
789 f << "###";
790 }
791 if (m_state->m_posToSharedDataSeen.find(pos)==m_state->m_posToSharedDataSeen.end()) {
792 m_state->m_posToSharedDataSeen.insert(pos);
793 if (input->tell()!=endPos)
794 ascii().addDelimiter(input->tell(),'|');
795 ascii().addPos(begPos);
796 ascii().addNote(f.str().c_str());
797 }
798 input->seek(actPos, librevenge::RVNG_SEEK_SET);
799 return true;
800 }
801
802 bool MultiplanParser::readName(int pos, MWAWCellContent::FormulaInstruction &instruction)
803 {
804 auto it=m_state->m_posToNameMap.find(pos);
805 if (it!=m_state->m_posToNameMap.end()) {
806 instruction=it->second;
807 return true;
808 }
809 auto const &entry=m_state->m_entries[8]; // the named entry
810 if (!entry.valid() || pos<0 || pos+10>=entry.length()) {
811 MWAW_DEBUG_MSG(("MultiplanParser::readName: the pos %d seeems bad\n", pos));
812 return false;
813 }
814 MWAWInputStreamPtr input = getInput();
815 long actPos=input->tell();
816 long begPos=entry.begin()+pos;
817 input->seek(begPos, librevenge::RVNG_SEEK_SET);
818 libmwaw::DebugStream f;
819 f << "Names-" << std::hex << pos << std::dec << ":";
820 int val=int(input->readULong(1));
821 int dSz=(val>>3);
822 if (dSz<=0 || begPos+10+dSz>entry.end()) {
823 input->seek(actPos, librevenge::RVNG_SEEK_SET);
824 MWAW_DEBUG_MSG(("MultiplanParser::readName: the pos %d seeems bad\n", pos));
825 return false;
826 }
827 if (val&3) f << "f0=" << val << ",";
828 val=int(input->readULong(1)); // 40|60
829 if (val) f << "f1=" << std::hex << val << std::dec << ",";
830 int rows[2];
831 for (auto &r : rows) r=int(input->readULong(1));
832 val=int(input->readULong(2));
833 int cols[2]= {(val>>10),(val>>4)&0x3f};
834 for (int i=0; i<2; ++i) {
835 instruction.m_position[i]=MWAWVec2i(cols[i], rows[i]);
836 instruction.m_positionRelative[i]=MWAWVec2b(false,false);
837 }
838 instruction.m_type=instruction.m_position[0]==instruction.m_position[1] ? instruction.F_Cell : instruction.F_CellList;
839 f << instruction << ",";
840 m_state->m_posToNameMap[pos]=instruction;
841 if (val&0xf) f << "f2=" << (val&0xf) << ","; // 0|2
842 for (int i=0; i<2; ++i) { // 0
843 val=int(input->readLong(2));
844 if (val) f << "f" << i+2 << "=" << val << ",";
845 }
846 std::string name;
847 for (int c=0; c<dSz; ++c) name+=char(input->readULong(1));
848 f << name << ",";
849 ascii().addPos(begPos);
850 ascii().addNote(f.str().c_str());
851
852 input->seek(actPos, librevenge::RVNG_SEEK_SET);
853 return true;
854 }
855
856 bool MultiplanParser::readZoneB()
857 {
858 MWAWInputStreamPtr input = getInput();
859 long pos = input->tell();
860 if (!input->checkPosition(pos+82)) {
861 MWAW_DEBUG_MSG(("MultiplanParser::readZoneB: the zone seems too short\n"));
862 return false;
863 }
864 libmwaw::DebugStream f;
865 f << "Entries(ZoneB):";
866 int dim[2];
867 for (auto &d : dim) d=int(input->readULong(2));
868 m_state->m_maximumCell=MWAWVec2i(dim[0],dim[1]);
869 f << "cell[max]=" << m_state->m_maximumCell << ",";
870 int val;
871 for (int i=0; i<7; ++i) {
872 val=int(input->readLong(2));
873 int const expected[]= {0,0,0x7fff,0x47,0xc,0x1e7,0x10a};
874 if (val!=expected[i])
875 f << "f" << i << "=" << val << ",";
876 }
877 for (int i=0; i<15; ++i) {
878 val=int(input->readLong(2));
879 if (!val) continue;
880 f << "g" << i << "=" << val << ",";
881 }
882 ascii().addPos(pos);
883 ascii().addNote(f.str().c_str());
884
885 pos=input->tell();
886 f.str("");
887 f << "ZoneB[II]:";
888 for (int i=0; i<2; ++i) {
889 val=int(input->readLong(1));
890 if (val!=1-i) f << "f" << i << "=" << val << ",";
891 }
892 int dim4[4];
893 for (auto &d : dim4) d=int(input->readULong(1));
894 f << "selection=" << MWAWBox2i(MWAWVec2i(dim4[0],dim4[1]),MWAWVec2i(dim4[2],dim4[3])) << ",";
895 for (int i=0; i<19; ++i) { // 0
896 val=int(input->readLong(1));
897 if (val) f << "g" << i << "=" << val << ",";
898 }
899 ascii().addPos(pos);
900 ascii().addNote(f.str().c_str());
901
902 input->seek(pos+82, librevenge::RVNG_SEEK_SET);
903 return true;
904 }
905
906 bool MultiplanParser::readZoneC()
907 {
908 MWAWInputStreamPtr input = getInput();
909 long pos = input->tell();
910 if (!input->checkPosition(pos+22)) {
911 MWAW_DEBUG_MSG(("MultiplanParser::readZoneC: the zone seems too short\n"));
912 return false;
913 }
914 libmwaw::DebugStream f;
915 f << "Entries(ZoneC):";
916 int val;
917 f << "unkn=[";
918 for (int i=0; i<4; ++i) { // small number
919 val=int(input->readLong(2));
920 if (val)
921 f << val << ",";
922 else
923 f << "_,";
924 }
925 f << "],";
926 val=int(input->readLong(2));
927 if (val==1)
928 f << "protected,";
929 else if (val)
930 f << "protected=#" << val << ",";
931 val=int(input->readULong(2));
932 if (val) f << "passwd[crypted]=" << std::hex << val << std::dec << ",";
933 for (int i=0; i<5; ++i) {
934 val=int(input->readLong(2));
935 int const expected[]= {0,0,0,2,1};
936 if (val!=expected[i]) f << "g" << i << "=" << val << ",";
937 }
938 ascii().addPos(pos);
939 ascii().addNote(f.str().c_str());
940 return true;
941 }
942
943 ////////////////////////////////////////////////////////////
944 // double
945 ////////////////////////////////////////////////////////////
946 bool MultiplanParser::readDouble(double &value)
947 {
948 MWAWInputStreamPtr input = getInput();
949 long pos=input->tell();
950 value=0;
951 if (!input->checkPosition(input->tell()+8)) {
952 MWAW_DEBUG_MSG(("MultiplanParser::readDouble: the zone is too short\n"));
953 return false;
954 }
955 int exponant=int(input->readULong(1));
956 double sign=1;
957 if (exponant&0x80) {
958 exponant&=0x7f;
959 sign=-1;
960 }
961 bool ok=true;
962 double factor=1;
963 for (int i=0; i<7; ++i) {
964 int val=int(input->readULong(1));
965 for (int d=0; d<2; ++d) {
966 int v= d==0 ? (val>>4) : (val&0xf);
967 if (v>=10) {
968 MWAW_DEBUG_MSG(("MultiplanParser::readDouble: oops find a bad digits\n"));
969 ok=false;
970 break;
971 }
972 factor/=10.;
973 value+=factor*v;
974 }
975 if (!ok) break;
976 }
977 value *= sign*std::pow(10.,exponant-0x40);
978 input->seek(pos+8, librevenge::RVNG_SEEK_SET);
979 return ok;
980 }
981
982 ////////////////////////////////////////////////////////////
983 // formula
984 ////////////////////////////////////////////////////////////
985 namespace MultiplanParserInternal
986 {
987 struct Functions {
988 char const *m_name;
989 int m_arity;
990 };
991
992 static Functions const s_listOperators[] = {
993 // 0
994 { "", -2}, { "", -2}, { "", -2}, { "", -2},
995 { "", -2}, { "", -2}, { "", -2}, { "", -2},
996 { "", -2}, { "", -2}, { "", -2}, { "", -2},
997 { "", -2}, { "", -2}, { "", -2}, { "", -2},
998 // 1
999 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1000 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1001 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1002 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1003 // 2
1004 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1005 { "", -2}, { ":", 2}, { "", -2}, { "", -2},
1006 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1007 { "", -2}, { ":", 2}, { "", -2}, { "", -2},
1008 // 3
1009 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1010 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1011 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1012 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1013 // 4
1014 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1015 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1016 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1017 { "", -2}, { ":", 2}, { "", -2}, { "", -2},
1018 // 5
1019 { "&", 2}, { "", -2}, { "", -2}, { "", -2},
1020 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1021 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1022 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1023 // 6
1024 { "<", 2}, { "", -2}, { "<=", 2}, { "", -2},
1025 { "=", 2}, { "", -2}, { ">=", 2}, { "", -2},
1026 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1027 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1028 // 7
1029 { ">", 2}, { "", -2}, { "<>", 2}, { "", -2},
1030 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1031 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1032 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1033 // 8
1034 { "", -2}, { "", -2}, { "+", 2}, { "", -2},
1035 { "-", 2}, { "", -2}, { "*", 2}, { "", -2},
1036 { "/", 2}, { "", -2}, { "^", 2}, { "", -2},
1037 { "", -2}, { "", -2}, { "-", 1}, { "", -2},
1038 // 9
1039 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1040 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1041 { "%", 1}, { "", -2}, { "", -2}, { "", -2},
1042 { "", -2}, { "", -2}, { "", -2}, { "", -2},
1043 };
1044
1045 static char const *s_listFunctions[]= {
1046 // 0
1047 "Count", "If", "IsNA", "IsError",
1048 "Sum", "Average", "Min", "Max",
1049 "Row", "Column", "NA", "NPV",
1050 "Stdev", "Dollar", "Fixed", "Sin",
1051 // 1
1052 "Cos", "Tan", "Atan", "Pi",
1053 "Sqrt", "Exp", "Ln", "Log",
1054 "Abs", "Int", "Sign", "Round",
1055 "Lookup", "Index", "Rept", "Mid",
1056 // 2
1057 "Length", "Value", "True", "False",
1058 "And", "Or", "Not", "Mod",
1059 "IterCnt", "Delta", "PV", "FV",
1060 "NPer", "PMT", "Rate", "MIRR",
1061 // 3
1062 "Irr", nullptr, nullptr, nullptr,
1063 nullptr, nullptr, nullptr, nullptr,
1064 nullptr, nullptr, nullptr, nullptr,
1065 nullptr, nullptr, nullptr, nullptr,
1066 };
1067 }
1068
1069 bool MultiplanParser::readFormula(MWAWVec2i const &cellPos, std::vector<MWAWCellContent::FormulaInstruction> &formula, long endPos, std::string &error)
1070 {
1071 formula.clear();
1072 MWAWInputStreamPtr input = getInput();
1073 if (!input || !input->checkPosition(endPos)) {
1074 MWAW_DEBUG_MSG(("MultiplanParser::readFormula: bad position\n"));
1075 error="badPos###";
1076 return false;
1077 }
1078 std::vector<std::vector<MWAWCellContent::FormulaInstruction> > stack;
1079 auto const &listOperators=MultiplanParserInternal::s_listOperators;
1080 int const numOperators=int(MWAW_N_ELEMENTS(MultiplanParserInternal::s_listOperators));
1081 auto const &listFunctions=MultiplanParserInternal::s_listFunctions;
1082 int const numFunctions=int(MWAW_N_ELEMENTS(MultiplanParserInternal::s_listFunctions));
1083 bool ok=true;
1084 int closeDelayed=0;
1085 bool checkForClose=false;
1086 while (input->tell()<=endPos) {
1087 long pos=input->tell();
1088 int wh=pos==endPos ? -1 : int(input->readULong(1));
1089 bool needCloseParenthesis=closeDelayed && (checkForClose || pos==endPos);
1090 ok=true;
1091 if (closeDelayed && !needCloseParenthesis && wh!=0x3c)
1092 needCloseParenthesis=wh>=numOperators || listOperators[wh].m_arity!=2;
1093 while (needCloseParenthesis && closeDelayed>0) {
1094 auto len=stack.size();
1095 if (len<2) {
1096 error="##closedParenthesis,";
1097 ok=false;
1098 break;
1099 }
1100 auto &dParenthesisFunc=stack[len-2];
1101 if (dParenthesisFunc.size()!=1 || dParenthesisFunc[0].m_type!=dParenthesisFunc[0].F_Operator ||
1102 dParenthesisFunc[0].m_content!="(") {
1103 error="##closedParenthesis,";
1104 ok=false;
1105 break;
1106 }
1107 dParenthesisFunc.insert(dParenthesisFunc.end(),stack.back().begin(), stack.back().end());
1108 MWAWCellContent::FormulaInstruction instr;
1109 instr.m_type=instr.F_Operator;
1110 instr.m_content=")";
1111 dParenthesisFunc.push_back(instr);
1112 stack.resize(len-1);
1113 --closeDelayed;
1114 }
1115 if (!ok || pos==endPos)
1116 break;
1117 int arity=0;
1118 MWAWCellContent::FormulaInstruction instr;
1119 ok=false;
1120 bool noneInstr=false, closeFunction=false;
1121 switch (wh) {
1122 case 0:
1123 if (pos+3>endPos || !readLink(int(input->readULong(2)), instr))
1124 break;
1125 ok=true;
1126 break;
1127 case 0x12: {
1128 if (pos+2>endPos)
1129 break;
1130 ok=true;
1131 instr.m_type=instr.F_Function;
1132 int id=int(input->readULong(1));
1133 if (id<numFunctions && listFunctions[id])
1134 instr.m_content=listFunctions[id];
1135 else {
1136 std::stringstream s;
1137 s << "Funct" << std::hex << id << std::dec;
1138 instr.m_content=s.str();
1139 }
1140 std::vector<MWAWCellContent::FormulaInstruction> child;
1141 child.push_back(instr);
1142 stack.push_back(child);
1143 instr.m_type=instr.F_Operator;
1144 instr.m_content="(";
1145 break;
1146 }
1147 case 0x51:
1148 case 0x71:
1149 case 0x91:
1150 case 0xd1:
1151 case 0xf1:
1152 closeFunction=ok=true;
1153 break;
1154 case 0x1c: // use before %
1155 case 0x1e: // use for <> A 1e B "code <>"
1156 case 0x34: // use for <=,>= ... A 34 B "code <=,..."
1157 case 0x36: // use before -unary
1158 noneInstr=ok=true;
1159 break;
1160 case 0x3a:
1161 ok=true;
1162 instr.m_type=instr.F_Operator;
1163 instr.m_content=";";
1164 break;
1165 case 0x3c:
1166 noneInstr=ok=true;
1167 ++closeDelayed;
1168 break;
1169 case 0x3e:
1170 ok=true;
1171 instr.m_type=instr.F_Operator;
1172 instr.m_content="(";
1173 break;
1174 case 0x56: {
1175 int dSz=int(input->readULong(1));
1176 if (pos+2+dSz>endPos)
1177 break;
1178 instr.m_type=instr.F_Text;
1179 auto fontConverter=getFontConverter();
1180 auto const fId=m_state->m_font.id();
1181 librevenge::RVNGString content;
1182 for (int i=0; i<dSz; ++i) {
1183 auto ch=static_cast<unsigned char>(input->readULong(1));
1184 int unicode = fontConverter->unicode(fId, static_cast<unsigned char>(ch));
1185 if (unicode!=-1)
1186 libmwaw::appendUnicode(uint32_t(unicode), content);
1187 else if (ch==0x9 || ch > 0x1f)
1188 libmwaw::appendUnicode(static_cast<uint32_t>(ch), content);
1189 else {
1190 MWAW_DEBUG_MSG(("MultiplanParser::readFormula: content seen bad seems bad\n"));
1191 error="##content";
1192 }
1193 }
1194 instr.m_content=content.cstr();
1195 ok=true;
1196 break;
1197 }
1198 case 0x21: // double
1199 case 0xe1:
1200 case 0x8f: // simple
1201 case 0xef:
1202 if (pos+3>endPos)
1203 break;
1204 instr.m_type=instr.F_Cell;
1205 instr.m_positionRelative[0]=MWAWVec2b(false,false);
1206 instr.m_position[0][1]=int(input->readULong(1));
1207 instr.m_position[0][0]=int(input->readULong(1));
1208 ok=(instr.m_position[0][0]<63) && (instr.m_position[0][1]<255);
1209 if (!ok) {
1210 error="###RorC";
1211 MWAW_DEBUG_MSG(("MultiplanParser::readFormula: find only row/column reference\n"));
1212 }
1213 break;
1214 case 0x29: // example C2 R1
1215 MWAW_DEBUG_MSG(("MultiplanParser::readFormula: find union operator\n"));
1216 error="###union";
1217 ok=false;
1218 break;
1219 case 0x37: // use for list cell
1220 case 0x53:
1221 case 0x73:
1222 case 0x93: // basic cell
1223 case 0xf3: { // difference ?
1224 if (pos+3>endPos)
1225 break;
1226 instr.m_type=instr.F_Cell;
1227 instr.m_positionRelative[0]=MWAWVec2b(true,true);
1228 int val=int(input->readULong(2));
1229 auto &newPos=instr.m_position[0];
1230 if (val&0x80)
1231 newPos[1]=cellPos[1]-(val>>8);
1232 else
1233 newPos[1]=cellPos[1]+(val>>8);
1234 if (val&0x40)
1235 newPos[0]=cellPos[0]-(val&0x3f);
1236 else
1237 newPos[0]=cellPos[0]+(val&0x3f);
1238 ok=newPos[0]>=0 && newPos[1]>=0;
1239 break;
1240 }
1241 case 0x94:
1242 if (pos+9>endPos || !readDouble(instr.m_doubleValue))
1243 break;
1244 instr.m_type=instr.F_Double;
1245 ok=true;
1246 break;
1247 case 0xf5:
1248 if (pos+3>endPos || !readName(int(input->readULong(2)), instr))
1249 break;
1250 ok=true;
1251 break;
1252 default:
1253 if (wh<numOperators && listOperators[wh].m_arity!=-2) {
1254 instr.m_content=listOperators[wh].m_name;
1255 instr.m_type=instr.F_Function;
1256 arity=listOperators[wh].m_arity;
1257 }
1258 if (instr.m_content.empty()) {
1259 MWAW_DEBUG_MSG(("MultiplanParser::readFormula: find unknown type %x\n", wh));
1260 std::stringstream s;
1261 s << "##unkn[func]=" << std::hex << wh << std::dec << ",";
1262 error=s.str();
1263 break;
1264 }
1265 ok=true;
1266 break;
1267 }
1268 if (!ok) {
1269 input->seek(pos, librevenge::RVNG_SEEK_SET);
1270 break;
1271 }
1272 checkForClose=!noneInstr && closeDelayed>0;
1273 if (noneInstr) continue;
1274 if (closeFunction) {
1275 ok=false;
1276 if (stack.empty()) {
1277 error="##closed,";
1278 break;
1279 }
1280 auto it=stack.end();
1281 --it;
1282 for (; it!=stack.begin(); --it) {
1283 if (it->size()!=1) continue;
1284 auto const &dInstr=(*it)[0];
1285 if (dInstr.m_type!=dInstr.F_Operator || dInstr.m_content!="(") continue;
1286 auto fIt=it;
1287 --fIt;
1288 auto &functionStack=*fIt;
1289 if (functionStack.size()!=1 || functionStack[0].m_type!=functionStack[0].F_Function) continue;
1290 ok=true;
1291 for (; it!=stack.end(); ++it)
1292 functionStack.insert(functionStack.end(), it->begin(), it->end());
1293 ++fIt;
1294 stack.erase(fIt, stack.end());
1295 break;
1296 }
1297 if (!ok) {
1298 error="##closed";
1299 break;
1300 }
1301 instr.m_type=instr.F_Operator;
1302 instr.m_content=")";
1303 stack.back().push_back(instr);
1304 continue;
1305 }
1306 if (instr.m_type!=MWAWCellContent::FormulaInstruction::F_Function) {
1307 std::vector<MWAWCellContent::FormulaInstruction> child;
1308 child.push_back(instr);
1309 stack.push_back(child);
1310 continue;
1311 }
1312 size_t numElt = stack.size();
1313 if (static_cast<int>(numElt) < arity) {
1314 std::stringstream s;
1315 s << instr.m_content << "[##" << arity << "]";
1316 error=s.str();
1317 input->seek(pos, librevenge::RVNG_SEEK_SET);
1318 ok=false;
1319 break;
1320 }
1321 if (arity==1) {
1322 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
1323 if (instr.m_content=="%")
1324 stack[numElt-1].push_back(instr);
1325 else
1326 stack[numElt-1].insert(stack[numElt-1].begin(), instr);
1327 continue;
1328 }
1329 if (arity==2) {
1330 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
1331 stack[numElt-2].push_back(instr);
1332 stack[numElt-2].insert(stack[numElt-2].end(), stack[numElt-1].begin(), stack[numElt-1].end());
1333 stack.resize(numElt-1);
1334 continue;
1335 }
1336 ok=false;
1337 error = "### unexpected arity";
1338 input->seek(pos, librevenge::RVNG_SEEK_SET);
1339 break;
1340 }
1341 long pos=input->tell();
1342 if (pos!=endPos || !ok || closeDelayed || stack.size()!=1 || stack[0].empty()) {
1343 MWAW_DEBUG_MSG(("MultiplanParser::readFormula: can not read a formula\n"));
1344 ascii().addDelimiter(pos, '|');
1345 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1346
1347 std::stringstream s;
1348 if (!error.empty())
1349 s << error;
1350 else
1351 s << "##unknownError";
1352 s << "[";
1353 for (auto const &i : stack) {
1354 for (auto const &j : i)
1355 s << j << ",";
1356 }
1357 s << "],";
1358 error=s.str();
1359 return true;
1360 }
1361 formula=stack[0];
1362 return true;
1363 }
1364
1365 ////////////////////////////////////////////////////////////
1366 // read the header
1367 ////////////////////////////////////////////////////////////
1368 bool MultiplanParser::checkHeader(MWAWHeader *header, bool strict)
1369 {
1370 *m_state = MultiplanParserInternal::State();
1371
1372 MWAWInputStreamPtr input = getInput();
1373 if (!input || !input->hasDataFork())
1374 return false;
1375
1376 if (!input->checkPosition(0x778)) {
1377 MWAW_DEBUG_MSG(("MultiplanParser::checkHeader: file is too short\n"));
1378 return false;
1379 }
1380 input->seek(0,librevenge::RVNG_SEEK_SET);
1381 if (input->readULong(2)!=0x11ab || input->readULong(2)!=0 ||
1382 input->readULong(2)!=0x13e8 || input->readULong(2)!=0)
1383 return false;
1384 libmwaw::DebugStream f;
1385 f << "FileHeader:";
1386 for (int i=0; i<2; ++i) {
1387 int val=int(input->readLong(2));
1388 if (val)
1389 f << "f" << i << "=" << val << ",";
1390 }
1391 ascii().addPos(0);
1392 ascii().addNote(f.str().c_str());
1393 if (strict) {
1394 // read the last zone list position and check that it corresponds to a valid position
1395 input->seek(0x758, librevenge::RVNG_SEEK_SET);
1396 int val=int(input->readULong(2));
1397 if (val<0x3c || !input->checkPosition(0x75a+val)) {
1398 MWAW_DEBUG_MSG(("MultiplanParser::checkHeader: can not find last spreadsheet position\n"));
1399 return false;
1400 }
1401 }
1402 /* checkme: MsMultiplan DOS begins by a list of 8 potential filename
1403 linked to this files (with length 0x1f) maybe something like
1404 that... */
1405 input->seek(0x30,librevenge::RVNG_SEEK_SET);
1406 ascii().addPos(0x30);
1407 ascii().addNote("Entries(ZoneA):");
1408 for (int i=0; i<4; ++i) {
1409 long pos=input->tell();
1410 f.str("");
1411 f << "ZoneA" << i << ":";
1412 ascii().addPos(pos);
1413 ascii().addNote(f.str().c_str());
1414 input->seek(pos+0x80,librevenge::RVNG_SEEK_SET);
1415 }
1416 long pos=input->tell();
1417 ascii().addPos(pos);
1418 ascii().addNote("ZoneA4");
1419 input->seek(0x272,librevenge::RVNG_SEEK_SET);
1420 if (header)
1421 header->reset(MWAWDocument::MWAW_T_MICROSOFTMULTIPLAN, 1, MWAWDocument::MWAW_K_SPREADSHEET);
1422 return true;
1423 }
1424
1425 ////////////////////////////////////////////////////////////
1426 // send spreadsheet
1427 ////////////////////////////////////////////////////////////
1428 bool MultiplanParser::sendText(MWAWEntry const &entry)
1429 {
1430 auto listener=getMainListener();
1431 if (!listener) {
1432 MWAW_DEBUG_MSG(("MultiplanParser::sendText: can not find the listener\n"));
1433 return false;
1434 }
1435 listener->setFont(m_state->m_font);
1436 auto input=getInput();
1437 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1438 for (long l=0; l<entry.length(); ++l) {
1439 if (input->isEnd()) {
1440 MWAW_DEBUG_MSG(("MultiplanParser::sendText: oops, can not read a character\n"));
1441 break;
1442 }
1443 auto c=static_cast<unsigned char>(input->readULong(1));
1444 if (c==0x9)
1445 listener->insertTab();
1446 else if (c==0xa || c==0xd)
1447 listener->insertEOL();
1448 else
1449 listener->insertCharacter(c);
1450 }
1451 return true;
1452 }
1453
1454 bool MultiplanParser::sendCell(MWAWVec2i const &cellPos, int p)
1455 {
1456 MWAWSpreadsheetListenerPtr listener=getSpreadsheetListener();
1457 if (!listener) {
1458 MWAW_DEBUG_MSG(("MultiplanParser::sendCell: I can not find the listener\n"));
1459 return false;
1460 }
1461 auto const &entry=m_state->m_entries[6];
1462 if (p<=0 || p>entry.length()) {
1463 MWAW_DEBUG_MSG(("MultiplanParser::sendCell: unexpected position %d\n", p));
1464 return false;
1465 }
1466 MWAWCell cell;
1467 MWAWCellContent content;
1468 MWAWCell::Format format;
1469 cell.setPosition(cellPos);
1470 cell.setFont(m_state->m_font);
1471 libmwaw::DebugStream f;
1472 f << "DataCell[C" << cellPos[0]+1 << "R" << cellPos[1]+1 << "]:";
1473 long pos=entry.begin()+p;
1474 auto it=m_state->m_cellPositionsSet.find(p);
1475 ++it;
1476 long endPos=it!=m_state->m_cellPositionsSet.end() ? entry.begin()+*it : entry.end();
1477 auto input=getInput();
1478 if (endPos-pos<4 || !input->checkPosition(endPos)) {
1479 MWAW_DEBUG_MSG(("MultiplanParser::sendCell: a cell %d seems to short\n", p));
1480 f << "###";
1481 ascii().addPos(pos);
1482 ascii().addNote(f.str().c_str());
1483 return false;
1484 }
1485 input->seek(pos, librevenge::RVNG_SEEK_SET);
1486 int formSize=int(input->readULong(1));
1487 if (formSize) f << "form[size]=" << std::hex << formSize << std::dec << ",";
1488 int val=int(input->readULong(1));
1489 int digits=(val&0xf);
1490 if (digits) f << "decimal=" << digits << ",";
1491 int form=(val>>4)&7;
1492 format.m_numberFormat=MWAWCell::F_NUMBER_GENERIC;
1493 switch (form) {
1494 case 2:
1495 format.m_numberFormat=MWAWCell::F_NUMBER_SCIENTIFIC;
1496 format.m_digits=digits;
1497 f << "scientific,";
1498 break;
1499 case 3:
1500 format.m_numberFormat=MWAWCell::F_NUMBER_DECIMAL;
1501 format.m_digits=digits;
1502 f << "decimal,";
1503 break;
1504 case 4: // default
1505 break;
1506 case 5:
1507 format.m_numberFormat=MWAWCell::F_NUMBER_CURRENCY;
1508 format.m_digits=digits;
1509 f << "currency,";
1510 break;
1511 case 6: // a bar
1512 f << "bar,";
1513 break;
1514 case 7:
1515 format.m_numberFormat=MWAWCell::F_NUMBER_PERCENT;
1516 format.m_digits=digits;
1517 f << "percent,";
1518 break;
1519 default:
1520 f << "format=" << form << ",";
1521 break;
1522 }
1523 cell.setProtected((val&0x80)!=0);
1524 if ((val&0x80)==0) f << "no[protection],";
1525 val=int(input->readULong(1));
1526 int align=(val>>2)&7;
1527 switch (align) {
1528 case 1:
1529 cell.setHAlignment(cell.HALIGN_CENTER);
1530 f << "center,";
1531 break;
1532 case 0: // default
1533 case 2: // generic
1534 break;
1535 case 3:
1536 cell.setHAlignment(cell.HALIGN_LEFT);
1537 f << "left,";
1538 break;
1539 case 4:
1540 cell.setHAlignment(cell.HALIGN_RIGHT);
1541 f << "right,";
1542 break;
1543 default:
1544 f << "#align=" << (align) << ",";
1545 break;
1546 }
1547 switch (val&3) {
1548 case 0:
1549 f << "double,";
1550 format.m_format=MWAWCell::F_NUMBER;
1551 content.m_contentType=content.C_NUMBER;
1552 break;
1553 case 1:
1554 format.m_format=MWAWCell::F_TEXT;
1555 content.m_contentType=content.C_TEXT;
1556 f << "text,";
1557 break;
1558 case 2:
1559 format.m_format=MWAWCell::F_NUMBER;
1560 content.m_contentType=content.C_NUMBER;
1561 f << "nan,";
1562 break;
1563 case 3: // or nothing
1564 format.m_format=MWAWCell::F_BOOLEAN;
1565 content.m_contentType=content.C_NUMBER;
1566 f << "bool,";
1567 break;
1568 default: // impossible
1569 break;
1570 }
1571 cell.setFormat(format);
1572 if ((val&0x20)==0)
1573 f << "no20[f1],";
1574 if (val&0x40)
1575 f << "shared,";
1576 int type=(val&0xe3);
1577 if (val&0x80) f << "80[f1],";
1578 int dSz=int(input->readULong(1));
1579 if (endPos<pos+4+dSz) {
1580 MWAW_DEBUG_MSG(("MultiplanParser::sendCell: a cell seems to short\n"));
1581 f << "###";
1582 ascii().addPos(pos);
1583 ascii().addNote(f.str().c_str());
1584 return false;
1585 }
1586 if ((type&0x3)==0 && dSz==8) {
1587 double value;
1588 if (!readDouble(value))
1589 f << "###";
1590 else
1591 content.setValue(value);
1592 f << value << ",";
1593 }
1594 else if ((type&0x3)==1 && dSz && pos+4+dSz+((type&0x40) ? 2 : 0) <= endPos) {
1595 content.m_textEntry.setBegin(input->tell());
1596 content.m_textEntry.setLength(dSz);
1597 std::string name;
1598 for (int c=0; c<dSz; ++c) name+=char(input->readULong(1));
1599 f << name << ",";
1600 }
1601 else if ((type&0x3)==2 && dSz==8) {
1602 content.setValue(std::nan(""));
1603 f << "Nan" << input->readULong(1) << ",";
1604 input->seek(7, librevenge::RVNG_SEEK_CUR);
1605 }
1606 else if ((type&0x3)==3 && dSz==8) {
1607 val=int(input->readULong(1));
1608 content.setValue(val);
1609 if (val==0)
1610 f << "false,";
1611 else if (val==1)
1612 f << "true,";
1613 else
1614 f << "##bool=" << val << ",";
1615 input->seek(7, librevenge::RVNG_SEEK_CUR);
1616 }
1617 if ((type&0x40) && input->tell()+2<=endPos && (formSize==0 || formSize==2)) {
1618 if ((input->tell()-pos)%2)
1619 input->seek(1, librevenge::RVNG_SEEK_CUR);
1620 int nPos=int(input->readULong(2));
1621 if (!readSharedData(nPos, type, cellPos, content))
1622 f << "###";
1623 f << "sharedData-" << std::hex << nPos << std::dec << ",";
1624 }
1625 else if (!(type&0x40) && formSize && input->tell()+formSize<=endPos) {
1626 auto endFPos=input->tell()+formSize;
1627 std::string err;
1628 if (!readFormula(cellPos, content.m_formula, endFPos, err)) {
1629 ascii().addDelimiter(input->tell(),'|');
1630 f << "###";
1631 }
1632 else
1633 content.m_contentType=content.C_FORMULA;
1634
1635 for (auto const &fo : content.m_formula) f << fo;
1636 f << ",";
1637 f << err;
1638 input->seek(endFPos, librevenge::RVNG_SEEK_SET);
1639 }
1640 else if (formSize) {
1641 MWAW_DEBUG_MSG(("MultiplanParser::sendCell: can not read a formula\n"));
1642 f << "###form";
1643 }
1644 listener->openSheetCell(cell, content);
1645 if (content.m_textEntry.valid()) {
1646 listener->setFont(cell.getFont());
1647 input->seek(content.m_textEntry.begin(), librevenge::RVNG_SEEK_SET);
1648 while (!input->isEnd() && input->tell()<content.m_textEntry.end()) {
1649 auto c=static_cast<unsigned char>(input->readULong(1));
1650 if (c==0x9)
1651 listener->insertTab();
1652 else if (c==0xa || c==0xd)
1653 listener->insertEOL();
1654 else
1655 listener->insertCharacter(c);
1656 }
1657 }
1658 listener->closeSheetCell();
1659 if (input->tell()!=endPos)
1660 ascii().addDelimiter(input->tell(),'|');
1661 ascii().addPos(pos);
1662 ascii().addNote(f.str().c_str());
1663 return true;
1664 }
1665
1666 bool MultiplanParser::sendSpreadsheet()
1667 {
1668 MWAWSpreadsheetListenerPtr listener=getSpreadsheetListener();
1669 if (!listener) {
1670 MWAW_DEBUG_MSG(("MultiplanParser::sendSpreadsheet: I can not find the listener\n"));
1671 return false;
1672 }
1673 listener->openSheet(m_state->getColumnsWidth(), librevenge::RVNG_POINT, std::vector<int>(), "Sheet0");
1674 auto const &dataEntry=m_state->m_entries[6];
1675 m_state->m_cellPositionsSet.insert(int(dataEntry.length()));
1676 for (size_t r=0; r<m_state->m_cellPositions.size(); ++r) {
1677 auto const &row = m_state->m_cellPositions[r];
1678 listener->openSheetRow(-16.f, librevenge::RVNG_POINT);
1679 for (size_t col=0; col<row.size(); ++col) {
1680 auto p=row[col];
1681 if (p<0 || p>dataEntry.length()) {
1682 MWAW_DEBUG_MSG(("MultiplanParser::sendSpreadsheet: find some bad data\n"));
1683 continue;
1684 }
1685 if (!p) continue;
1686 MWAWVec2i cellPos(static_cast<int>(col), static_cast<int>(r));
1687 sendCell(cellPos, p);
1688 }
1689 listener->closeSheetRow();
1690 }
1691 listener->closeSheet();
1692 return true;
1693 }
1694 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: