Debian Codegraph @

src/lib/RagTimeSpreadsheet.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 <iomanip>
35 #include <iostream>
36 #include <limits>
37 #include <map>
38 #include <set>
39 #include <sstream>
40
41 #include <librevenge/librevenge.h>
42
43 #include "MWAWCell.hxx"
44 #include "MWAWFont.hxx"
45 #include "MWAWFontConverter.hxx"
46 #include "MWAWParagraph.hxx"
47 #include "MWAWPictMac.hxx"
48 #include "MWAWPosition.hxx"
49 #include "MWAWSpreadsheetEncoder.hxx"
50 #include "MWAWSpreadsheetListener.hxx"
51 #include "MWAWTextListener.hxx"
52
53 #include "RagTimeParser.hxx"
54 #include "RagTimeStruct.hxx"
55
56 #include "RagTimeSpreadsheet.hxx"
57
58 /** Internal: the structures of a RagTimeSpreadsheet */
59 namespace RagTimeSpreadsheetInternal
60 {
61 struct Cell;
62 //! Internal: date/time format of a RagTimeSpreadsheet
63 struct DateTime {
64 //! constructor
65 DateTime()
66 : m_isDate(true)
67 , m_DTFormat("")
68 {
69 }
70 //! operator<<
71 friend std::ostream &operator<<(std::ostream &o, DateTime const &dt)
72 {
73 o << dt.m_DTFormat;
74 if (!dt.m_isDate) o << "[time],";
75 else o << ",";
76 return o;
77 }
78 //! true if this is a date field
79 bool m_isDate;
80 //! the date time format
81 std::string m_DTFormat;
82 };
83
84 //! Internal: cell number format of a RagTimeSpreadsheet (SpVa block)
85 struct CellFormat {
86 //! constructor
87 CellFormat()
88 : m_numeric()
89 , m_dateTime()
90 , m_align(MWAWCell::HALIGN_DEFAULT)
91 , m_rotation(0)
92 , m_flags(0)
93 , m_extra("")
94 {
95 }
96 //! update the cell format if needed
97 void update(Cell &cell) const;
98 //! operator<<
99 friend std::ostream &operator<<(std::ostream &o, CellFormat const &form)
100 {
101 if (form.m_rotation) o << "rot=" << form.m_rotation << ",";
102 if (form.m_flags&2) o << "protect[all],";
103 if (form.m_flags&4) o << "protect[format,formula],";
104 if (form.m_flags&0x8) o << "invisible,";
105 if (form.m_flags&0x20) o << "zero[dontshow],";
106 if (form.m_flags&0x40) o << "prec[variable],"; // precision depend on display
107 if (form.m_flags&0x80) o << "print[no],";
108
109 if (form.m_flags&0xFF11) o << "fl=" << std::hex << (form.m_flags&0xFF11) << std::dec << ",";
110 o << form.m_extra;
111 return o;
112 }
113 //! the numeric format
114 MWAWCell::Format m_numeric;
115 //! the date/time format
116 DateTime m_dateTime;
117 //! the cell's alignment
118 MWAWCell::HorizontalAlignment m_align;
119 //! the rotation angle
120 int m_rotation;
121 //! some flags
122 int m_flags;
123 //! extra data (for debugging)
124 std::string m_extra;
125 };
126
127 //! Internal: cell border of a RagTimeSpreadsheet (SpVa block)
128 struct CellBorder {
129 //! constructor
130 CellBorder()
131 : m_extra("")
132 {
133 }
134 //! returns true if the cell has some border
135 bool hasBorders() const
136 {
137 return !m_borders[0].isEmpty() && !m_borders[1].isEmpty();
138 }
139 //! update the cell border if need
140 void update(Cell &cell) const;
141 //! operator<<
142 friend std::ostream &operator<<(std::ostream &o, CellBorder const &border)
143 {
144 for (int i=0; i<2; ++i) {
145 if (border.m_borders[i]!=MWAWBorder())
146 o << (i==0 ? "top=" : "left=") << border.m_borders[i] << ",";
147 }
148 o << border.m_extra;
149 return o;
150 }
151 //! the top and left border
152 MWAWBorder m_borders[2];
153 //! extra data
154 std::string m_extra;
155 };
156
157 //! Internal: extra cell format of a RagTimeSpreadsheet (SpCe block)
158 struct CellExtra {
159 //! constructor
160 CellExtra()
161 : m_isTransparent(false)
162 , m_color(MWAWColor::white())
163 , m_extra("")
164 {
165 }
166 //! update the cell color if need
167 void update(Cell &cell) const;
168 //! operator<<
169 friend std::ostream &operator<<(std::ostream &o, CellExtra const &st)
170 {
171 if (st.m_isTransparent) o << "noColor,";
172 else if (!st.m_color.isWhite()) o << "color[back]=" << st.m_color << ",";
173 o << st.m_extra;
174 return o;
175 }
176 //! true if the cell is transparent
177 bool m_isTransparent;
178 //! the background color
179 MWAWColor m_color;
180 //! extra data
181 std::string m_extra;
182 };
183
184 //! Internal: header of a complex block of a RagTimeSpreadsheet
185 struct ComplexBlock {
186 //! constructor
187 ComplexBlock()
188 : m_zones()
189 , m_intList()
190 {
191 }
192 //! a small zone of a ComplexBlock
193 struct Zone {
194 //! constructor
195 Zone()
196 : m_pos(0)
197 {
198 for (auto &data : m_data) data=0;
199 }
200 //! operator<<
201 friend std::ostream &operator<<(std::ostream &o, Zone const &z)
202 {
203 o << "pos=" << std::hex << z.m_pos << std::dec << ",";
204 for (int i=0; i<3; ++i) {
205 if (!z.m_data[i]) continue;
206 char const *wh[]= {"firstVal","N", "Nsub"};
207 o << wh[i] << "=" << z.m_data[i] << ",";
208 }
209 return o;
210 }
211 //! the zone position
212 long m_pos;
213 //! three unknown int
214 int m_data[3];
215 };
216 //! the list of zone
217 std::vector<Zone> m_zones;
218 //! a list of unknown counter
219 std::vector<int> m_intList;
220 };
221
222 //! Internal: a cell of a RagTimeSpreadsheet
223 struct Cell final : public MWAWCell {
224 //! constructor
225 explicit Cell(MWAWVec2i pos=MWAWVec2i(0,0))
226 : MWAWCell()
227 , m_content()
228 , m_textEntry()
229 {
230 setPosition(pos);
231 }
232 Cell(Cell const &)=default;
233 Cell &operator=(Cell const &)=default;
234 Cell &operator=(Cell &&)=default;
235 //! destructor
236 ~Cell() final;
237 //! test if we can use or not the formula
238 bool validateFormula()
239 {
240 if (m_content.m_formula.empty()) return false;
241 for (auto &instr : m_content.m_formula) {
242 bool ok=true;
243 // fixme: cell to other spreadsheet
244 if (instr.m_type==MWAWCellContent::FormulaInstruction::F_Cell ||
245 instr.m_type==MWAWCellContent::FormulaInstruction::F_CellList)
246 ok=instr.m_sheet[0].empty();
247 // fixme: replace operator or by function
248 else if (instr.m_type==MWAWCellContent::FormulaInstruction::F_Function)
249 ok=(instr.m_content!="Or"&&instr.m_content!="And"&&instr.m_content!="Not");
250 if (!ok) {
251 m_content.m_formula.resize(0);
252 return false;
253 }
254 }
255 return true;
256 }
257 //! the cell content
258 MWAWCellContent m_content;
259 //! the text entry if the cell is a zone of text zone
260 MWAWEntry m_textEntry;
261 };
262
263 Cell::~Cell()
264 {
265 }
266
267 void CellFormat::update(Cell &cell) const
268 {
269 MWAWCell::Format format=cell.getFormat();
270 if (format.m_format==MWAWCell::F_NUMBER)
271 format=m_numeric;
272 else if (format.m_format==MWAWCell::F_DATE) {
273 format.m_DTFormat=m_dateTime.m_DTFormat;
274 if (!m_dateTime.m_isDate)
275 format.m_format=MWAWCell::F_TIME;
276 }
277 cell.setFormat(format);
278 cell.setHAlignment(m_align);
279 if (m_flags&6) cell.setProtected(true);
280 cell.setRotation(m_rotation);
281 }
282
283 void CellBorder::update(Cell &cell) const
284 {
285 if (!m_borders[0].isEmpty()) cell.setBorders(libmwaw::TopBit, m_borders[0]);
286 if (!m_borders[1].isEmpty()) cell.setBorders(libmwaw::LeftBit, m_borders[1]);
287 }
288
289 void CellExtra::update(Cell &cell) const
290 {
291 if (!m_isTransparent) cell.setBackgroundColor(m_color);
292 }
293
294 //! Internal: a spreadsheet's zone of a RagTimeSpreadsheet
295 struct Spreadsheet {
296 //! a map a cell sorted by row
297 typedef std::map<MWAWVec2i,Cell,MWAWVec2i::PosSizeLtY> Map;
298 //! constructor
299 Spreadsheet()
300 : m_rows(0)
301 , m_columns(0)
302 , m_widthDefault(72)
303 , m_widthCols()
304 , m_heightDefault(12)
305 , m_heightRows()
306 , m_cellsBegin(0)
307 , m_cellsMap()
308 , m_rowPositionsList()
309 , m_name("Sheet0")
310 , m_isSent(false)
311 {
312 }
313 //! returns the row size in point
314 float getRowHeight(int row) const
315 {
316 if (row>=0&&row<static_cast<int>(m_heightRows.size())&&m_heightRows[size_t(row)]>0)
317 return m_heightRows[size_t(row)];
318 return m_heightDefault;
319 }
320 //! returns the height of a row in point and updated repeated row
321 float getRowHeight(int row, int &numRepeated) const
322 {
323 float res=getRowHeight(row);
324 numRepeated=1;
325 if (row<0 || row>=static_cast<int>(m_heightRows.size()))
326 numRepeated=1000;
327 else {
328 for (auto r=size_t(row+1); r<m_heightRows.size(); ++r) {
329 float nextH=getRowHeight(row+1);
330 if (nextH<res || nextH>res)
331 break;
332 ++numRepeated;
333 }
334 }
335 return res;
336 }
337 /** returns the columns dimension in point */
338 std::vector<float> getColumnsWidth() const
339 {
340 auto numCols=size_t(getRightBottomPosition()[0]+1);
341 std::vector<float> res(numCols, float(m_widthDefault));
342 if (m_widthCols.size()<numCols)
343 numCols=m_widthCols.size();
344 for (size_t i=0; i<numCols; ++i) {
345 if (m_widthCols[i] > 0)
346 res[i] = m_widthCols[i];
347 }
348 return res;
349 }
350 /** returns the spreadsheet dimension */
351 MWAWVec2i getRightBottomPosition() const
352 {
353 MWAWVec2i res(0,0);
354 for (auto const &it : m_cellsMap) {
355 Cell const &cell=it.second;
356 if (cell.position()[0] >= res[0])
357 res[0]=cell.position()[0]+1;
358 if (cell.position()[1] >= res[1])
359 res[1]=cell.position()[1]+1;
360 }
361 return res;
362 }
363 /** the number of row */
364 int m_rows;
365 /** the number of col */
366 int m_columns;
367 /** the default column width */
368 float m_widthDefault;
369 /** the column size in points */
370 std::vector<float> m_widthCols;
371 /** the default row height */
372 float m_heightDefault;
373 /** the row height in points */
374 std::vector<float> m_heightRows;
375 /** the positions of the cells in the file */
376 long m_cellsBegin;
377 /** the map cell position to not empty cells */
378 Map m_cellsMap;
379 /** the positions of row in the file */
380 std::vector<long> m_rowPositionsList;
381 /** the sheet name */
382 std::string m_name;
383 /** true if the sheet is sent to the listener */
384 mutable bool m_isSent;
385 };
386
387 ////////////////////////////////////////
388 //! Internal: the state of a RagTimeSpreadsheet
389 struct State {
390 //! constructor
391 State()
392 : m_version(-1)
393 , m_numericFormatList()
394 , m_dateTimeList()
395 , m_cellFontList()
396 , m_cellFormatList()
397 , m_cellBorderList()
398 , m_cellExtraList()
399 , m_cellDEList()
400 , m_idSpreadsheetMap()
401 {
402 }
403
404 //! the file version
405 mutable int m_version;
406 //! a list of numeric format
407 std::vector<MWAWCell::Format> m_numericFormatList;
408 //! a list dateTimeFormatId -> dateTimeFormat;
409 std::vector<DateTime> m_dateTimeList;
410 //! a list SpTe -> font
411 std::vector<MWAWFont> m_cellFontList;
412 //! a list SpVaId -> cellFormat
413 std::vector<CellFormat> m_cellFormatList;
414 //! a list SpBoId -> cellBorder
415 std::vector<CellBorder> m_cellBorderList;
416 //! a list SpCeId -> cellExtra
417 std::vector<CellExtra> m_cellExtraList;
418 //! a list SpDEId -> unknown data
419 std::vector<std::string> m_cellDEList;
420 //! map id -> spreadsheet
421 std::map<int, std::shared_ptr<Spreadsheet> > m_idSpreadsheetMap;
422 };
423
424 }
425
426 ////////////////////////////////////////////////////////////
427 // constructor/destructor, ...
428 ////////////////////////////////////////////////////////////
429 RagTimeSpreadsheet::RagTimeSpreadsheet(RagTimeParser &parser)
430 : m_parserState(parser.getParserState())
431 , m_state(new RagTimeSpreadsheetInternal::State)
432 , m_mainParser(&parser)
433 {
434 }
435
436 RagTimeSpreadsheet::~RagTimeSpreadsheet()
437 { }
438
439 int RagTimeSpreadsheet::version() const
440 {
441 if (m_state->m_version < 0)
442 m_state->m_version = m_parserState->m_version;
443 return m_state->m_version;
444 }
445
446 ////////////////////////////////////////////////////////////
447 // resource
448 ////////////////////////////////////////////////////////////
449 bool RagTimeSpreadsheet::getDateTimeFormat(int dtId, std::string &dtFormat) const
450 {
451 if (dtId<0||dtId>=int(m_state->m_dateTimeList.size())) {
452 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::getDateTimeFormat: can not find the date/format %d\n", dtId));
453 return false;
454 }
455 dtFormat=m_state->m_dateTimeList[size_t(dtId)].m_DTFormat;
456 return !dtFormat.empty();
457 }
458
459 bool RagTimeSpreadsheet::readNumericFormat(MWAWEntry &entry)
460 {
461 MWAWInputStreamPtr input = m_parserState->m_input;
462 long pos=entry.begin();
463 if (pos<=0 || !input->checkPosition(pos+2+0x26)) {
464 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readNumericFormat: the position seems bad\n"));
465 return false;
466 }
467 entry.setParsed(true);
468 input->seek(pos, librevenge::RVNG_SEEK_SET);
469 libmwaw::DebugStream f;
470 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
471 f << "Entries(" << entry.type() << ")[" << entry.id() << "]:";
472 auto dSz=static_cast<int>(input->readULong(2));
473 long endPos=pos+2+dSz;
474 auto headerSz=static_cast<int>(input->readULong(2));
475 auto fSz=static_cast<int>(input->readULong(2));
476 auto N=static_cast<int>(input->readULong(2));
477 f << "N=" << N << ",";
478 if (headerSz<0x20 || fSz<6 || dSz<headerSz+long(N+1)*fSz || !input->checkPosition(endPos)) {
479 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readNumericFormat: the size seems bad\n"));
480 f << "###";
481 ascFile.addPos(pos);
482 ascFile.addNote(f.str().c_str());
483 return false;
484 }
485 input->seek(pos+2+headerSz, librevenge::RVNG_SEEK_SET);
486 ascFile.addPos(pos);
487 ascFile.addNote(f.str().c_str());
488
489 std::set<long> posSet;
490 posSet.insert(endPos);
491 for (int i=0; i<=N; ++i) {
492 pos=input->tell();
493 f.str("");
494 f << entry.type() << "-" << i << ":";
495 auto val=static_cast<int>(input->readLong(2)); // 0 (except last)
496 if (val) f << "f0=" << val << ",";
497 val=static_cast<int>(input->readLong(2)); // always 1 ?
498 if (val!=1) f << "f1=" << val << ",";
499 auto fPos=static_cast<int>(input->readULong(2));
500 f << "pos[def]=" << std::hex << entry.begin()+2+fPos << std::dec << ",";
501 posSet.insert(entry.begin()+2+fPos);
502 input->seek(pos+fSz, librevenge::RVNG_SEEK_SET);
503 ascFile.addPos(pos);
504 ascFile.addNote(f.str().c_str());
505 }
506
507 for (auto it=posSet.begin(); it!=posSet.end();) {
508 pos=*(it++);
509 if (pos>=endPos) break;
510 long nextPos=it==posSet.end() ? endPos : *it;
511 f.str("");
512 f << entry.type() << "[def]:";
513 input->seek(pos, librevenge::RVNG_SEEK_SET);
514 int depl[4];
515 f << "depl=[";
516 for (int &i : depl) {
517 i=static_cast<int>(input->readULong(1));
518 if (i) f << i << ",";
519 else f << "_,";
520 }
521 f << "],";
522 if (entry.id()==0) {
523 MWAWCell::Format format;
524 format.m_format=MWAWCell::F_NUMBER;
525 format.m_numberFormat=MWAWCell::F_NUMBER_DECIMAL;
526 if (depl[0]<16 || pos+depl[0]>endPos) {
527 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readNumericFormat: the first zone seems bad\n"));
528 f << "#header,";
529 }
530 else {
531 auto val=static_cast<int>(input->readULong(1));
532 if (val==1) {
533 format.m_numberFormat=MWAWCell::F_NUMBER_GENERIC;
534 f << "standart,";
535 }
536 else if (val) f << "#standart=" << val << ",";
537 format.m_digits=static_cast<int>(input->readULong(1));
538 for (int i=0; i<10; ++i) {
539 val=static_cast<int>(input->readULong(1));
540 if (val) f << "f" << i << "=" << val << ",";
541 }
542 }
543 if (depl[0]<depl[1] && pos+depl[1]<endPos) {
544 input->seek(pos+depl[0], librevenge::RVNG_SEEK_SET);
545 ascFile.addDelimiter(input->tell(),'|');
546 f << "format=[";
547 for (int i=depl[0]; i<depl[1]; ++i) {
548 auto c=static_cast<int>(input->readULong(1));
549 switch (c) {
550 case 1: // end
551 break;
552 case 6:
553 f << "#";
554 break;
555 case 7: // int
556 f << "0";
557 break;
558 case 9: // fix
559 f << ",";
560 break;
561 case 0xa:
562 format.m_thousandHasSeparator=true;
563 f << " ";
564 break;
565 case 0xc:
566 format.m_numberFormat=MWAWCell::F_NUMBER_SCIENTIFIC;
567 f << "E+";
568 break;
569 case 0xe:
570 format.m_numberFormat=MWAWCell::F_NUMBER_PERCENT;
571 f << "%";
572 break;
573 case 0xf:
574 format.m_numberFormat=MWAWCell::F_NUMBER_GENERIC;
575 f << "[standart]";
576 break;
577 default:
578 if (c<0x1f) {
579 f << "#[" << std::hex << c << std::dec << "]";
580 }
581 else {
582 format.m_numberFormat=MWAWCell::F_NUMBER_CURRENCY; // only probable
583 f << char(c);
584 }
585 }
586 }
587 f << "],";
588 }
589 else {
590 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readNumericFormat: the compress format zone seems bad\n"));
591 f << "#format,";
592 }
593 f << format;
594 m_state->m_numericFormatList.push_back(format);
595 }
596 else if (depl[3]<8 || pos+depl[3]>endPos) {
597 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readNumericFormat: the first zone seems bad\n"));
598 f << "#header,";
599 m_state->m_dateTimeList.push_back(RagTimeSpreadsheetInternal::DateTime());
600 }
601 else {
602 f << "depl2=[";
603 for (int i=0; i<4; ++i) { // f0=small number, f1=f2=f3=0
604 auto val=static_cast<int>(input->readULong(1));
605 if (val) f << val << ",";
606 else f << "_,";
607 }
608 f << "],";
609 bool ok=true, isDate=false;;
610 std::string dtFormat("");
611 for (int i=8; i<depl[3]; ++i) {
612 auto c=static_cast<int>(input->readULong(1));
613 switch (c) {
614 case 0: // probably next field...
615 break;
616 case 1: // end
617 break;
618 case 7:
619 dtFormat+="%S";
620 break;
621 case 9:
622 dtFormat+="%M";
623 break;
624 case 0xa:
625 dtFormat+="%H";
626 break;
627 case 0xc:
628 case 0xd: // with 2 digits
629 dtFormat+="%d";
630 isDate=true;
631 break;
632 case 0xe: // checkme
633 dtFormat+="%a";
634 isDate=true;
635 break;
636 case 0xf:
637 dtFormat+="%A";
638 isDate=true;
639 break;
640 case 0x10:
641 case 0x11: // with 2 digits
642 dtFormat+="%m";
643 isDate=true;
644 break;
645 case 0x12:
646 dtFormat+="%b";
647 isDate=true;
648 break;
649 case 0x13:
650 dtFormat+="%B";
651 isDate=true;
652 break;
653 case 0x14:
654 dtFormat+="%y";
655 isDate=true;
656 break;
657 case 0x15:
658 dtFormat+="%Y";
659 isDate=true;
660 break;
661 default:
662 if (c<0x1f) {
663 std::stringstream s;
664 s << "#[" << std::hex << c << std::dec << "]";
665 dtFormat+=s.str();
666 ok=false;
667 }
668 else
669 dtFormat+=char(c);
670 }
671 }
672 f << "format=[" << dtFormat << "],";
673 if (ok) {
674 RagTimeSpreadsheetInternal::DateTime dt;
675 dt.m_DTFormat=dtFormat;
676 dt.m_isDate=isDate;
677 m_state->m_dateTimeList.push_back(dt);
678 }
679 else
680 m_state->m_dateTimeList.push_back(RagTimeSpreadsheetInternal::DateTime());
681 }
682 // fixme: read the 3 other potential header zones
683 headerSz=depl[3];
684 if ((headerSz&1)) ++headerSz;
685 input->seek(pos+headerSz, librevenge::RVNG_SEEK_SET);
686 auto cSz=static_cast<int>(input->readULong(1));
687 if (headerSz<4||cSz<=0||pos+headerSz+1+cSz!=nextPos) {
688 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readNumericFormat: can not read a format\n"));
689 f << "###";
690 }
691 else {
692 ascFile.addDelimiter(input->tell()-1,'|');
693 std::string name("");
694 for (int i=0; i<cSz; ++i) name += char(input->readULong(1));
695 f << name << ",";
696 }
697 ascFile.addPos(pos);
698 ascFile.addNote(f.str().c_str());
699 }
700 return true;
701 }
702
703 bool RagTimeSpreadsheet::readResource(MWAWEntry &entry)
704 {
705 if (entry.begin()<=0) return false;
706
707 std::string const &type=entry.type();
708 if (type.length()!=8 || type.compare(0,6,"rsrcSp")!=0)
709 return false;
710 if (entry.type()=="rsrcSpDI")
711 return readRsrcSpDI(entry);
712 if (entry.type()=="rsrcSpDo")
713 return readRsrcSpDo(entry);
714
715 auto resType=RagTimeStruct::ResourceList::Undef;
716 for (int i=RagTimeStruct::ResourceList::SpBo; i<=RagTimeStruct::ResourceList::SpVa; ++i) {
717 std::string name("rsrc");
718 name+=RagTimeStruct::ResourceList::getName(RagTimeStruct::ResourceList::Type(i));
719 if (entry.type()!=name) continue;
720 resType=RagTimeStruct::ResourceList::Type(i);
721 break;
722 }
723 if (resType==RagTimeStruct::ResourceList::Undef)
724 return false;
725
726 entry.setParsed(true);
727 MWAWInputStreamPtr input = m_parserState->m_input;
728 long pos=entry.begin();
729 input->seek(pos, librevenge::RVNG_SEEK_SET);
730 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
731 libmwaw::DebugStream f;
732 f << "Entries(" << entry.type() << ")[" << entry.id() << "]:";
733 RagTimeStruct::ResourceList zone;
734 if (!zone.read(input, entry)) {
735 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readResource: the size seems bad\n"));
736 f << "###";
737 ascFile.addPos(pos);
738 ascFile.addNote(f.str().c_str());
739 return false;
740 }
741 f << zone;
742 ascFile.addPos(pos);
743 ascFile.addNote(f.str().c_str());
744
745 input->seek(zone.m_dataPos, librevenge::RVNG_SEEK_SET);
746 if (zone.m_type!=resType && zone.m_dataNumber) {
747 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readResource: find unexpected data size\n"));
748 for (int i=0; i<zone.m_dataNumber; ++i) {
749 pos=input->tell();
750 f.str("");
751 f << entry.type() << "-" << i << ":##";
752 input->seek(pos+zone.m_dataSize, librevenge::RVNG_SEEK_SET);
753 ascFile.addPos(pos);
754 ascFile.addNote(f.str().c_str());
755 }
756 }
757 else {
758 int val;
759 for (int i=0; i<zone.m_dataNumber; ++i) {
760 pos=input->tell();
761 f.str("");
762 switch (resType) {
763 case RagTimeStruct::ResourceList::SpBo: {
764 RagTimeSpreadsheetInternal::CellBorder borders;
765 libmwaw::DebugStream f2;
766 val=static_cast<int>(input->readLong(2)); // always 0
767 if (val) f << "f0=" << val << ",";
768 val=static_cast<int>(input->readLong(2));
769 if (val!=1) f << "used=" << val << ",";
770 for (int j=0; j<2; ++j) {
771 MWAWBorder border;
772 val=static_cast<int>(input->readULong(2));
773 f2.str("");
774 // val&0x8000: def in cell
775 if (val&0x4000) if (j) f2 << "third[bottom],";
776 switch (val&3) {
777 case 0:
778 f2 << "pos[exterior],";
779 break;
780 case 1:
781 f2 << "pos[interior],";
782 break;
783 case 2: // normal
784 break;
785 default:
786 f2 << "##pos=3,";
787 break;
788 }
789 val &= 0x3FFC;
790 if (val) f2 << "fl=" << std::hex << val << std::dec << ",";
791 border.m_width=double(input->readLong(4))/65536.;
792 val=static_cast<int>(input->readLong(2));
793 if (val && !m_mainParser->getColor(val-1, border.m_color)) {
794 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readResource: can not find border color %d\n", val-1));
795 f2 << "#color=" << val-1 << ",";
796 }
797 border.m_extra=f2.str();
798 borders.m_borders[j]=border;
799 }
800 borders.m_extra=f.str();
801 m_state->m_cellBorderList.push_back(borders);
802 f.str("");
803 f << borders;
804 break;
805 }
806 case RagTimeStruct::ResourceList::SpCe: {
807 RagTimeSpreadsheetInternal::CellExtra extra;
808 val=static_cast<int>(input->readLong(2)); // always 0
809 if (val) f << "f0=" << val << ",";
810 val=static_cast<int>(input->readLong(2));
811 if (val!=1) f << "used=" << val << ",";
812 val=static_cast<int>(input->readULong(2));
813 if (val&0x8000) extra.m_isTransparent=true;
814 if (val&0x4000) f << "bottom[color],";
815 if (val&0x2000) f << "top[color],";
816 val &= 0x1FFF;
817 if (val) f << "fl=" << std::hex << val << std::dec << ",";
818 val=static_cast<int>(input->readLong(2));
819 if (val && !m_mainParser->getColor(val-1, extra.m_color)) {
820 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readResource: can not find color %d\n", val-1));
821 f << "#color=" << val-1 << ",";
822 }
823 extra.m_extra=f.str();
824 m_state->m_cellExtraList.push_back(extra);
825 f.str("");
826 f << extra;
827 break;
828 }
829 case RagTimeStruct::ResourceList::SpDE:
830 for (int j=0; j<2; ++j) { // f0=2, f1=1-b
831 val=static_cast<int>(input->readLong(2));
832 if (val) f << "f" << j << "=" << val << ",";
833 }
834 for (int j=0; j<2; ++j) { // two big number ?
835 val=static_cast<int>(input->readULong(2));
836 if (val) f << "g" << j << "=" << std::hex << val << std::dec << ",";
837 }
838 val=static_cast<int>(input->readLong(2));
839 if (val) f << "f2=" << val << ",";
840 m_state->m_cellDEList.push_back(f.str());
841 break;
842 case RagTimeStruct::ResourceList::SpTe: {
843 MWAWFont font;
844 val=static_cast<int>(input->readLong(2)); // always 0
845 if (val) f << "f0=" << val << ",";
846 val=static_cast<int>(input->readLong(2));
847 if (val!=1) f << "used=" << val << ",";
848 val=static_cast<int>(input->readLong(2));
849 if (val>0 && !m_mainParser->getCharStyle(val-1, font)) {
850 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readResource: can not find a char format\n"));
851 f << "##id[CharId]=" << val << ",";
852 }
853 val=static_cast<int>(input->readLong(1));
854 if (val) font.setDeltaLetterSpacing(float(val));
855 val=static_cast<int>(input->readLong(1)); // 1|5
856 if (val!=1) f << "f1=" << val << ",";
857 val=static_cast<int>(input->readLong(1));
858 if (val) font.set(MWAWFont::Script(-float(val),librevenge::RVNG_POINT));
859 val=static_cast<int>(input->readULong(1));
860 f << "lang=" << val << ",";
861 font.m_extra=f.str();
862 m_state->m_cellFontList.push_back(font);
863 f.str("");
864 f << font.getDebugString(m_parserState->m_fontConverter);
865 break;
866 }
867 case RagTimeStruct::ResourceList::SpVa: {
868 RagTimeSpreadsheetInternal::CellFormat format;
869 val=static_cast<int>(input->readLong(2)); // always 0
870 if (val) f << "f0=" << val << ",";
871 val=static_cast<int>(input->readLong(2));
872 if (val!=1) f << "used=" << val << ",";
873 format.m_flags=static_cast<int>(input->readULong(2)); // small number
874 val=static_cast<int>(input->readLong(2));
875 if (val<=0 || val>int(m_state->m_numericFormatList.size())) {
876 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readResource: can not find a numeric format\n"));
877 f << "##id[NumFormat]=" << val << ",";
878 }
879 else {
880 format.m_numeric=m_state->m_numericFormatList[size_t(val-1)];
881 if (val!=1) // default
882 f << format.m_numeric << ",";
883 }
884 val=static_cast<int>(input->readLong(2));
885 if (val<=0 || val>int(m_state->m_dateTimeList.size())) {
886 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readResource: can not find a date/time format\n"));
887 f << "##id[DTFormat]=" << val << ",";
888 }
889 else {
890 format.m_dateTime=m_state->m_dateTimeList[size_t(val-1)];
891 if (val!=1)
892 f << format.m_dateTime << ",";
893 }
894 val=static_cast<int>(input->readLong(1));
895 switch (val) {
896 case 1: // normal
897 break;
898 case 2:
899 format.m_align=MWAWCell::HALIGN_LEFT;
900 f << "left,";
901 break;
902 case 3:
903 format.m_align=MWAWCell::HALIGN_CENTER;
904 f << "center,";
905 break;
906 case 4:
907 format.m_align=MWAWCell::HALIGN_RIGHT;
908 f << "right,";
909 break;
910 case 5:
911 f << "comma[justify],";
912 break;
913 case 6:
914 f << "repeat,";
915 break;
916 default:
917 f << "#align=" << val << ",";
918 break;
919 }
920 val=static_cast<int>(input->readLong(1));
921 if (val>=1&&val<=4)
922 format.m_rotation=90*(val-1);
923 else
924 f << "#rotation=" << val << ",";
925
926 format.m_extra=f.str();
927 m_state->m_cellFormatList.push_back(format);
928 f.str("");
929 f << format;
930 break;
931 }
932 case RagTimeStruct::ResourceList::BuSl:
933 case RagTimeStruct::ResourceList::BuGr:
934 case RagTimeStruct::ResourceList::gray:
935 case RagTimeStruct::ResourceList::colr:
936 case RagTimeStruct::ResourceList::res_:
937 case RagTimeStruct::ResourceList::Undef:
938 #if !defined(__clang__)
939 default:
940 #endif
941 break;
942 }
943 std::string data=f.str();
944 f.str("");
945 f << entry.type() << "-" << i << ":" << data;
946 if (input->tell()>pos+zone.m_dataSize) {
947 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readResource: can not find the entry size seems bad\n"));
948 f << "###";
949 ascFile.addPos(pos);
950 ascFile.addNote(f.str().c_str());
951 return false;
952 }
953 input->seek(pos+zone.m_dataSize, librevenge::RVNG_SEEK_SET);
954 ascFile.addPos(pos);
955 ascFile.addNote(f.str().c_str());
956 }
957 }
958 // last field is always empty
959 pos=input->tell();
960 ascFile.addPos(pos);
961 ascFile.addNote("_");
962 input->seek(pos+zone.m_dataSize, librevenge::RVNG_SEEK_SET);
963
964 if (input->tell()!=zone.m_endPos) {
965 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readResource: find some extra data\n"));
966 f.str("");
967 f << entry.type() << "-end:";
968 ascFile.addPos(input->tell());
969 ascFile.addNote(f.str().c_str());
970 }
971 return true;
972 }
973
974 bool RagTimeSpreadsheet::readRsrcSpDI(MWAWEntry &entry)
975 {
976 MWAWInputStreamPtr input = m_parserState->m_input;
977 long pos=entry.begin();
978 if (pos<=0 || !input->checkPosition(pos+2+0x20)) {
979 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readRsrcSpDI: the position seems bad\n"));
980 return false;
981 }
982 entry.setParsed(true);
983 input->seek(pos, librevenge::RVNG_SEEK_SET);
984 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
985 libmwaw::DebugStream f;
986 f << "Entries(" << entry.type() << ")[" << entry.id() << "]:";
987 auto dSz=static_cast<int>(input->readULong(2));
988 long endPos=pos+2+dSz;
989 auto headerSz=static_cast<int>(input->readULong(2));
990 auto fSz=static_cast<int>(input->readULong(2));
991 auto N=static_cast<int>(input->readULong(2));
992 f << "N=" << N << ",";
993 if (headerSz<0x20 || fSz<0x8 || dSz<headerSz+long(N+1)*fSz || !input->checkPosition(endPos)) {
994 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readRsrcSpDI: the size seems bad\n"));
995 f << "###";
996 ascFile.addPos(pos);
997 ascFile.addNote(f.str().c_str());
998 return false;
999 }
1000 input->seek(pos+2+headerSz, librevenge::RVNG_SEEK_SET);
1001 ascFile.addPos(pos);
1002 ascFile.addNote(f.str().c_str());
1003
1004 std::set<long> posSet;
1005 posSet.insert(endPos);
1006 for (int i=0; i<=N; ++i) {
1007 pos=input->tell();
1008 f.str("");
1009 f << entry.type() << "-" << i << ":";
1010 auto val=static_cast<int>(input->readLong(2)); // 0 (except last)
1011 if (val) f << "f0=" << val << ",";
1012 auto fPos=static_cast<int>(input->readULong(2));
1013 if (fPos) {
1014 f << "pos[def]=" << std::hex << entry.begin()+2+fPos << std::dec << ",";
1015 posSet.insert(entry.begin()+2+fPos);
1016 }
1017 input->seek(pos+fSz, librevenge::RVNG_SEEK_SET);
1018 ascFile.addPos(pos);
1019 ascFile.addNote(f.str().c_str());
1020 }
1021 for (auto const &dPos : posSet) {
1022 if (dPos>=endPos) break;
1023 f.str("");
1024 f << entry.type() << "[data]:";
1025 ascFile.addPos(dPos);
1026 ascFile.addNote(f.str().c_str());
1027 }
1028 return true;
1029 }
1030
1031 bool RagTimeSpreadsheet::readRsrcSpDo(MWAWEntry &entry)
1032 {
1033 MWAWInputStreamPtr input = m_parserState->m_input;
1034 long pos=entry.begin();
1035 if (pos<=0 || !input->checkPosition(pos+2+0x4a)) {
1036 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readRsrcSpDo: the position seems bad\n"));
1037 return false;
1038 }
1039 entry.setParsed(true);
1040 input->seek(pos, librevenge::RVNG_SEEK_SET);
1041 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
1042 libmwaw::DebugStream f;
1043 f << "Entries(rsrcSpDo)[" << entry.id() << "]:";
1044 auto dSz=static_cast<int>(input->readULong(2));
1045 long endPos=pos+2+dSz;
1046 if (dSz<0x4a || !input->checkPosition(endPos)) {
1047 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readRsrcSpDo: the size seems bad\n"));
1048 f << "###";
1049 ascFile.addPos(pos);
1050 ascFile.addNote(f.str().c_str());
1051 return false;
1052 }
1053 for (int i=0; i<2; ++i) { // f1=0|80
1054 auto val=static_cast<int>(input->readLong(2));
1055 if (val) f << "f" << i << "=" << std::hex << val << std::dec << ",";
1056 }
1057 int val0=0; // to make clang analyzer happy
1058 for (int i=0; i<10; ++i) { // find g0=3+10*k, g1=10+g0, g2=10+g1, g3=10+g2, other 0|3+10*k1
1059 auto val = static_cast<int>(input->readLong(4));
1060 if (i==0) {
1061 val0=val;
1062 f << "g0=" << val << ",";
1063 }
1064 else if (i<4 && val!=val0+10*i)
1065 f << "g" << i << "=" << val << ",";
1066 else if (i>=4 && val)
1067 f << "g" << i << "=" << val << ",";
1068 }
1069 for (int i=0; i<9; ++i) {
1070 auto val = static_cast<int>(input->readULong(2));
1071 static int const expected[]= {0,0,0,0x64,0x3ff5,0x8312,0x6e97,0x8d4f,0xdf3b};
1072 if (val!=expected[i])
1073 f << "h" << i << "=" << std::hex << val << std::dec << ",";
1074 }
1075 int const numVal= int(endPos-4-input->tell())/2;
1076 for (int i=0; i<numVal; ++i) { // k0=small int, k1=k0+1, k2=k3=k4=0, k5=0|b7|e9|f3
1077 auto val = static_cast<int>(input->readLong(2));
1078 if (val) f << "k" << i << "=" << val << ",";
1079 }
1080 input->seek(endPos-4, librevenge::RVNG_SEEK_SET);
1081 f << "id?=" << std::hex << input->readULong(4) << std::dec << ","; // a big number
1082 ascFile.addPos(pos);
1083 ascFile.addNote(f.str().c_str());
1084 return true;
1085 }
1086
1087
1088 ////////////////////////////////////////////////////////////
1089 // Intermediate level
1090 ////////////////////////////////////////////////////////////
1091 bool RagTimeSpreadsheet::readBlockHeader(MWAWEntry const &entry, RagTimeSpreadsheetInternal::ComplexBlock &block)
1092 {
1093 MWAWInputStreamPtr input = m_parserState->m_input;
1094 long pos=entry.begin();
1095 long endPos=entry.end();
1096 if (pos<=0 || entry.length()<6 || !input->checkPosition(endPos)) {
1097 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readBlockHeader: the position seems bad\n"));
1098 return false;
1099 }
1100
1101 libmwaw::DebugStream f;
1102 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
1103 ascFile.addPos(endPos);
1104 ascFile.addNote("_");
1105
1106 input->seek(pos, librevenge::RVNG_SEEK_SET);
1107 f << "Entries(" << entry.type() << "):";
1108 auto N=static_cast<int>(input->readLong(2));
1109 f << "N=" << N << ",";
1110 auto dSz=static_cast<int>(input->readULong(4));
1111 if (dSz<N*2+10 || pos+2+dSz>endPos) {
1112 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readBlockHeader: the data size seems bad\n"));
1113 f << "###";
1114 ascFile.addPos(pos);
1115 ascFile.addNote(f.str().c_str());
1116 return false;
1117 }
1118 auto val=static_cast<int>(input->readLong(2));
1119 f << "f0=" << val << ",";
1120 f << "flags=[";
1121 for (int i=0; i<=N; ++i) {
1122 val=static_cast<int>(input->readLong(2));
1123 block.m_intList.push_back(val);
1124 if (val) f << val << ",";
1125 else f << "_,";
1126 }
1127 f << "],";
1128 if (dSz!=N*2+10) {
1129 ascFile.addDelimiter(input->tell(),'|');
1130 input->seek(pos+dSz, librevenge::RVNG_SEEK_SET);
1131 }
1132 ascFile.addPos(pos);
1133 ascFile.addNote(f.str().c_str());
1134
1135 pos=input->tell();
1136 f.str("");
1137 f << entry.type() << ":";
1138 N=static_cast<int>(input->readULong(2));
1139 f << "N[zones]=" << N << ",";
1140 if (pos+2+10*N>endPos) {
1141 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readBlockHeader: the zone block seems bad\n"));
1142 f << "###";
1143 ascFile.addPos(pos);
1144 ascFile.addNote(f.str().c_str());
1145 return true;
1146 }
1147 ascFile.addPos(pos);
1148 ascFile.addNote(f.str().c_str());
1149 for (int i=0; i<N; ++i) {
1150 RagTimeSpreadsheetInternal::ComplexBlock::Zone zone;
1151 pos=input->tell();
1152 f.str("");
1153 f << entry.type() << "-Z" << i << "[def]:";
1154 for (auto &data : zone.m_data) data=static_cast<int>(input->readLong(2));
1155 auto dataPos=long(input->readULong(4));
1156 if (dataPos<N*2+10 || dataPos>entry.length()) {
1157 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readBlockHeader: the zone position seems bad\n"));
1158 f << "###dataPos" << std::hex << entry.begin()+dataPos << std::dec << ",";
1159 }
1160 else
1161 zone.m_pos=entry.begin()+dataPos;
1162 block.m_zones.push_back(zone);
1163 f << zone;
1164 input->seek(pos+10, librevenge::RVNG_SEEK_SET);
1165 ascFile.addPos(pos);
1166 ascFile.addNote(f.str().c_str());
1167 }
1168
1169 return true;
1170 }
1171
1172 bool RagTimeSpreadsheet::readPositionsList(MWAWEntry const &entry, std::vector<long> &posList, long &lastDataPos)
1173 {
1174 posList.resize(0);
1175 if (version()<2) {
1176 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readPositionsList: must not be called for v1-2... file\n"));
1177 return false;
1178 }
1179
1180 MWAWInputStreamPtr input = m_parserState->m_input;
1181 long pos=input->tell();
1182 long endPos=entry.end();
1183 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
1184 libmwaw::DebugStream f;
1185 f << entry.type() << "[PosList]:";
1186 auto dSz=static_cast<int>(input->readULong(4));
1187 auto N=static_cast<int>(input->readULong(2));
1188 f << "N=" << N << ",";
1189 if (!input->checkPosition(endPos) || dSz<10+2*N || pos+dSz>endPos) {
1190 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readPositionsList: can not find the second block size\n"));
1191 f << "###";
1192 ascFile.addPos(pos);
1193 ascFile.addNote(f.str().c_str());
1194 return false;
1195 }
1196 auto dataSize=long(input->readULong(2));
1197 f << "lPos=" << std::hex << pos+dSz+dataSize << std::dec << ",";
1198 if (dataSize&1) ++dataSize;
1199 lastDataPos=pos+dSz+dataSize;
1200 if (lastDataPos>endPos) {
1201 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readPositionsList: the last position seems bad\n"));
1202 f << "###";
1203 ascFile.addPos(pos);
1204 ascFile.addNote(f.str().c_str());
1205 return false;
1206 }
1207 f << "pos=[";
1208 for (int i=0; i<N; ++i) {
1209 long newPos=pos+dSz+long(input->readULong(2));
1210 f << std::hex << newPos << std::dec << ",";
1211 if (newPos>lastDataPos) {
1212 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readPositionsList: find some bad position\n"));
1213 f << "###]";
1214 ascFile.addPos(pos);
1215 ascFile.addNote(f.str().c_str());
1216 input->seek(pos+dSz, librevenge::RVNG_SEEK_SET);
1217 return true;
1218 }
1219 posList.push_back(newPos);
1220 }
1221 f << "],";
1222 ascFile.addPos(pos);
1223 ascFile.addNote(f.str().c_str());
1224 return true;
1225 }
1226
1227 ////////////////////////////////////////////////////////////
1228 // spreadsheet zone v3...
1229 ////////////////////////////////////////////////////////////
1230 bool RagTimeSpreadsheet::readSpreadsheet(MWAWEntry &entry)
1231 {
1232 if (version()<2) {
1233 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheet: must not be called for v1-2... file\n"));
1234 return false;
1235 }
1236 MWAWInputStreamPtr input = m_parserState->m_input;
1237 long pos=entry.begin();
1238 int dataFieldSize=m_mainParser->getZoneDataFieldSize(entry.id());
1239 if (pos<=0 || !input->checkPosition(pos+dataFieldSize+0x64)) {
1240 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheet: the position seems bad\n"));
1241 return false;
1242 }
1243 entry.setParsed(true);
1244 input->seek(pos, librevenge::RVNG_SEEK_SET);
1245 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
1246 libmwaw::DebugStream f;
1247 f << "Entries(SpreadsheetZone):";
1248 auto dSz=static_cast<int>(input->readULong(dataFieldSize));
1249 long endPos=pos+dataFieldSize+dSz;
1250 if (dSz<0x62 || !input->checkPosition(endPos)) {
1251 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheet: the size seems bad\n"));
1252 f << "###";
1253 ascFile.addPos(pos);
1254 ascFile.addNote(f.str().c_str());
1255 return false;
1256 }
1257 int val;
1258 for (int i=0; i<6; ++i) { // f0=0, f1=4|6|44, f2=1-8, f3=1-f, f4=1-5, f5=1-3
1259 val=static_cast<int>(input->readLong(2));
1260 if (val) f << "f" << i << "=" << val << ",";
1261 }
1262 for (int i=0; i<6; ++i) // g0~40, g1=g2=2, g3~16, g4=[b-10], g5=[8-c]|800d
1263 f << "g" << i << "=" << double(input->readLong(4))/65536. << ",";
1264 long zoneBegin[11];
1265 zoneBegin[10]=endPos;
1266 for (int i=0; i<10; ++i) {
1267 zoneBegin[i]=long(input->readULong(4));
1268 if (!zoneBegin[i]) continue;
1269 f << "zone" << i << "=" << std::hex << pos+dataFieldSize+zoneBegin[i] << std::dec << ",";
1270 if (pos+2+zoneBegin[i]>endPos) {
1271 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheet: the zone %d seems bad\n",i));
1272 zoneBegin[i]=0;
1273 f << "###";
1274 continue;
1275 }
1276 zoneBegin[i]+=pos+dataFieldSize;
1277 }
1278 f << "fl?=["; // or some big number
1279 for (int i=0; i<8; ++i) {
1280 val=static_cast<int>(input->readULong(2));
1281 if (val)
1282 f << std::hex << val << std::dec << ",";
1283 else
1284 f << "_,";
1285 }
1286 f << "],";
1287 for (int i=0; i<3; ++i) { // h0=0-4, h1=h2=0
1288 val=static_cast<int>(input->readULong(2));
1289 if (val)
1290 f << "h" << i << "=" << val << ",";
1291 }
1292 ascFile.addPos(pos);
1293 ascFile.addNote(f.str().c_str());
1294
1295 // now read the different zone, first set the endPosition
1296 for (int i=9; i>=0; --i) {
1297 if (zoneBegin[i]==0)
1298 zoneBegin[i]=zoneBegin[i+1];
1299 else if (zoneBegin[i]>zoneBegin[i+1]) {
1300 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheet: the zone %d seems bad(II)\n",i));
1301 zoneBegin[i]=zoneBegin[i+1];
1302 }
1303 }
1304 std::shared_ptr<RagTimeSpreadsheetInternal::Spreadsheet> sheet(new RagTimeSpreadsheetInternal::Spreadsheet);
1305 for (int i=0; i<10; ++i) {
1306 if (zoneBegin[i+1]<=zoneBegin[i]) continue;
1307 MWAWEntry zone;
1308 zone.setBegin(zoneBegin[i]);
1309 zone.setEnd(zoneBegin[i+1]);
1310 zone.setId(i);
1311
1312 char const *what[]= {
1313 "SpreadsheetContent", "SpreadsheetCFormat", "SpreadsheetFormula", "SpreadsheetCondition",
1314 "SpreadsheetCUseIn", "SpreadsheetUnknown5", "SpreadsheetUnknown6", "SpreadsheetCExtraUseIn",
1315 "SpreadsheetCRef", "SpreadsheetUnknown9",
1316 };
1317 zone.setType(what[i]);
1318
1319 bool ok=true;
1320 switch (i) {
1321 case 0: // content
1322 case 1: // format
1323 case 2: // formula
1324 case 3: // condition
1325 case 4: // list of cell which used which have a ref the current cell
1326 ok=readSpreadsheetComplexStructure(zone, *sheet);
1327 if (ok && i<=2)
1328 m_state->m_idSpreadsheetMap[entry.id()]=sheet;
1329 break;
1330 case 5:
1331 // case 6: never seens
1332 case 7: // list of extra spreadsheet which have a ref to the current cell
1333 case 8: // link to cell in other extra spreadsheet(we must probably read it)
1334 ok=readSpreadsheetSimpleStructure(zone, *sheet);
1335 break;
1336 case 9:
1337 ok=readSpreadsheetZone9(zone, *sheet);
1338 break;
1339 default:
1340 ok=false;
1341 break;
1342 }
1343 if (ok) continue;
1344 f.str("");
1345 f << "Entries(" << zone.type() << "):";
1346 ascFile.addPos(zoneBegin[i]);
1347 ascFile.addNote(f.str().c_str());
1348 // SpreadsheetZone-9: sz+N+N*14
1349 }
1350 return true;
1351 }
1352
1353 bool RagTimeSpreadsheet::readSpreadsheetCellDimension(MWAWVec2i const &cellPos, long endPos, RagTimeSpreadsheetInternal::Spreadsheet &sheet)
1354 {
1355 if (cellPos[0] && cellPos[1]) {
1356 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellDimension: expect cellPos[0] or cellPos[1] = 0\n"));
1357 return false;
1358 }
1359 MWAWInputStreamPtr input = m_parserState->m_input;
1360 libmwaw::DebugStream f;
1361 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
1362
1363 long pos=input->tell();
1364 f << "SpreadsheetContent-";
1365 if (cellPos[1]==0) {
1366 f << "colDim[" << cellPos[0]-1 << "]:";
1367 if (pos+16>endPos || cellPos[0]<=0) {
1368 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellDimension: can not read a row height\n"));
1369 f << "##";
1370 }
1371 else {
1372 f << "dim=[";
1373 // col dim, followed by some margins?, no sure if v3~15/16 is or not a dim
1374 for (int j=0; j<4; ++j) {
1375 auto dim=static_cast<uint32_t>(input->readULong(4));
1376 f << float(dim&0x7FFFFFFF)/65536.f;
1377 if (dim&0x80000000) f << "/h";
1378 f << ",";
1379 if (j) continue;
1380 if (cellPos[0]>int(sheet.m_widthCols.size()))
1381 sheet.m_widthCols.resize(size_t(cellPos[0]),0);
1382 sheet.m_widthCols[size_t(cellPos[0]-1)]=float(dim&0x7FFFFFFF)/65536.f;
1383 }
1384 f << "],";
1385 }
1386 if ((input->tell()+1==endPos && input->readLong(1)!=0) || input->tell()!=endPos)
1387 f<<"#";
1388 ascFile.addPos(pos);
1389 ascFile.addNote(f.str().c_str());
1390 return true;
1391 }
1392
1393 f << "-rowDim[" << cellPos[1] << "]:";
1394 if (pos+8>endPos||cellPos[1]<=0) {
1395 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellDimension: can not read a row height\n"));
1396 f << "##";
1397 }
1398 else {
1399 f << "dim=[";
1400 for (int j=0; j<2; ++j) { // row dim, followed by cell height
1401 auto dim=static_cast<uint32_t>(input->readULong(4));
1402 f << float(dim&0x7FFFFFFF)/65536.f;
1403 if (dim&0x80000000) f << "/h";
1404 f << ",";
1405 if (j) continue;
1406 if (cellPos[1]>int(sheet.m_heightRows.size()))
1407 sheet.m_heightRows.resize(size_t(cellPos[1]),0);
1408 sheet.m_heightRows[size_t(cellPos[1]-1)]=float(dim&0x7FFFFFFF)/65536.f;
1409 }
1410 f << "],";
1411 }
1412 if ((input->tell()+1==endPos && input->readLong(1)!=0) || input->tell()!=endPos)
1413 f<<"#";
1414 ascFile.addPos(pos);
1415 ascFile.addNote(f.str().c_str());
1416 return true;
1417 }
1418
1419 bool RagTimeSpreadsheet::readSpreadsheetCellContent(RagTimeSpreadsheetInternal::Cell &cell, long endPos)
1420 {
1421 MWAWVec2i const &cellPos=cell.position();
1422 if (cellPos[0]<0 || cellPos[1]<0) {
1423 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellContent: expect cellPos[0] and cellPos[1] >= 0\n"));
1424 return false;
1425 }
1426
1427 MWAWInputStreamPtr input = m_parserState->m_input;
1428 libmwaw::DebugStream f;
1429 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
1430
1431 long pos=input->tell();
1432
1433 f << "SpreadsheetContent-C" << cellPos<< "]:";
1434 bool ok=true;
1435 auto format=cell.getFormat();
1436 MWAWCellContent &content=cell.m_content;
1437 int val, type=static_cast<int>(input->readULong(1));
1438 switch (type) {
1439 case 0: // not frequent, but can happens
1440 break;
1441 case 0x40:
1442 case 0x44: // find with 44244131
1443 case 0x80:
1444 if (type==0x80)
1445 f << "Nan[eval],";
1446 else
1447 f << "Nan[circ],";
1448 format.m_format=MWAWCell::F_NUMBER;
1449 format.m_numberFormat=MWAWCell::F_NUMBER_GENERIC;
1450 content.m_contentType=MWAWCellContent::C_NUMBER;
1451 content.setValue(std::numeric_limits<double>::quiet_NaN());
1452 break;
1453 case 0x81:
1454 f << "float81,";
1455 MWAW_FALLTHROUGH;
1456 case 3:
1457 MWAW_FALLTHROUGH;
1458 case 1: {
1459 if (type==3) {
1460 format.m_format=MWAWCell::F_DATE; // we need the format to choose between date and time
1461 f << "date/time,";
1462 }
1463 else {
1464 format.m_format=MWAWCell::F_NUMBER;
1465 format.m_numberFormat=MWAWCell::F_NUMBER_GENERIC;
1466 }
1467 content.m_contentType=MWAWCellContent::C_NUMBER;
1468 if (pos+11>endPos) {
1469 ok=false;
1470 break;
1471 }
1472 double res;
1473 bool isNan;
1474 if (input->readDouble10(res, isNan)) {
1475 content.setValue(res);
1476 f << res << ",";
1477 }
1478 else
1479 f << "#value,";
1480 break;
1481 }
1482 case 0x24:
1483 f << "text2,";
1484 MWAW_FALLTHROUGH;
1485 case 4: {
1486 format.m_format=MWAWCell::F_TEXT;
1487 content.m_textEntry.setBegin(input->tell());
1488 content.m_textEntry.setEnd(endPos);
1489
1490 std::string text("");
1491 for (long j=0; j<endPos-1-pos; ++j) {
1492 auto c=char(input->readLong(1));
1493 if (c==0) {
1494 content.m_textEntry.setEnd(input->tell()-1);
1495 break;
1496 }
1497 text+=c;
1498 }
1499 f << text << ",";
1500 break;
1501 }
1502 case 5:
1503 if (pos+2>endPos) {
1504 ok=false;
1505 break;
1506 }
1507 val=static_cast<int>(input->readLong(1));
1508 f << val << ",";
1509 break;
1510 case 6:
1511 case 0x14: {
1512 f << "textZone,";
1513 cell.m_textEntry.setBegin(input->tell());
1514 cell.m_textEntry.setEnd(endPos);
1515 cell.m_textEntry.setId(m_mainParser->getNewZoneId());
1516 cell.m_textEntry.setType("SpreadsheetText");
1517 format.m_format=MWAWCell::F_TEXT;
1518 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1519 break;
1520 }
1521 case 0x51:
1522 f << "long51,";
1523 MWAW_FALLTHROUGH;
1524 case 0x11: // or 2 int?
1525 if (pos+5>endPos) {
1526 ok=false;
1527 break;
1528 }
1529 val=static_cast<int>(input->readLong(4));
1530 format.m_format=MWAWCell::F_NUMBER;
1531 format.m_numberFormat=MWAWCell::F_NUMBER_GENERIC;
1532 content.m_contentType=MWAWCellContent::C_NUMBER;
1533 content.setValue(double(val));
1534 f << val << ",";
1535 break;
1536 default:
1537 ok=false;
1538 break;
1539 }
1540 cell.setFormat(format);
1541 if (!ok) f<< "##";
1542 else if ((input->tell()+1==endPos && input->readLong(1)!=0) || input->tell()!=endPos)
1543 f<<"#";
1544 ascFile.addPos(pos);
1545 ascFile.addNote(f.str().c_str());
1546 return ok;
1547 }
1548
1549 bool RagTimeSpreadsheet::readSpreadsheetCellFormat(MWAWVec2i const &cellPos, long endPos, RagTimeSpreadsheetInternal::Cell &cell)
1550 {
1551 MWAWInputStreamPtr input = m_parserState->m_input;
1552 long pos=input->tell();
1553 libmwaw::DebugStream f;
1554 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
1555
1556 f << "SpreadsheetCFormat-";
1557 if (cellPos[1]==0) f << "col:";
1558 else if (cellPos[0]==0) f << "row:";
1559 else f << cell.position() << ":";
1560 if (pos+8>endPos) {
1561 f << "###";
1562 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellFormat: a block definition seems bad\n"));
1563 ascFile.addPos(pos);
1564 ascFile.addNote(f.str().c_str());
1565 return true;
1566 }
1567 for (int j=0; j<4; ++j) {
1568 auto val=static_cast<int>(input->readULong(2));
1569 if (val==0) {
1570 if (cellPos[0] && cellPos[1]) {
1571 char const *wh[] = {"SpTe","SpVa", "SpBo", "SpCe"};
1572 f << wh[j] << "=#undef,";
1573 }
1574 continue;
1575 }
1576 switch (j) {
1577 case 0: {
1578 if (val<=0 || val >static_cast<int>(m_state->m_cellFontList.size())) {
1579 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellFormat: find unexpected SpTe index\n"));
1580 f << "id[SpTe]=##" << val-1 << ",";
1581 break;
1582 }
1583
1584 MWAWFont const &font=m_state->m_cellFontList[size_t(val-1)];
1585 cell.setFont(font);
1586 if (val==1) break;
1587 f << "te=[" << font.getDebugString(m_parserState->m_fontConverter) << "],";
1588 break;
1589 }
1590 case 1: {
1591 if (val<=0 || val>static_cast<int>(m_state->m_cellFormatList.size())) {
1592 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellFormat: find unexpected SpVa index\n"));
1593 f << "id[SpVa]=##" << val-1 << ",";
1594 break;
1595 }
1596 auto const &cFormat=m_state->m_cellFormatList[size_t(val-1)];
1597 cFormat.update(cell);
1598 if (val==1) break;
1599 f << "va=[" << cFormat << "],";
1600 break;
1601 }
1602 case 2: {
1603 if (val <= 0 || val>static_cast<int>(m_state->m_cellBorderList.size())) {
1604 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellFormat: find unexpected SpBoindex\n"));
1605 f << "id[SpBo]=##" << val-1 << ",";
1606 break;
1607 }
1608 auto const &borders=m_state->m_cellBorderList[size_t(val-1)];
1609 if (val==1) break;
1610 borders.update(cell);
1611 f << "bo=[" << borders << "],";
1612 break;
1613 }
1614 case 3: {
1615 if (val<=0 || val>static_cast<int>(m_state->m_cellExtraList.size())) {
1616 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellFormat: find unexpected SpCe index\n"));
1617 f << "id[SpCe]=##" << val-1 << ",";
1618 break;
1619 }
1620 auto const &extras=m_state->m_cellExtraList[size_t(val-1)];
1621 extras.update(cell);
1622 if (val==1) break;
1623 f << "ce=[" << extras << "],";
1624 break;
1625 }
1626 default:
1627 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellFormat: find unexpected type\n"));
1628 f << "###";
1629 break;
1630 }
1631
1632 }
1633 ascFile.addPos(pos);
1634 ascFile.addNote(f.str().c_str());
1635
1636 return true;
1637 }
1638
1639 bool RagTimeSpreadsheet::readSpreadsheetZone9(MWAWEntry const &entry, RagTimeSpreadsheetInternal::Spreadsheet &/*sheet*/)
1640 {
1641 MWAWInputStreamPtr input = m_parserState->m_input;
1642 long pos=entry.begin();
1643 long endPos=entry.end();
1644 if (pos<=0 || entry.length()<3 || !input->checkPosition(endPos)) {
1645 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetZone9: the position seems bad\n"));
1646 return false;
1647 }
1648
1649 libmwaw::DebugStream f;
1650 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
1651 ascFile.addPos(endPos);
1652 ascFile.addNote("_");
1653
1654 input->seek(pos, librevenge::RVNG_SEEK_SET);
1655 f << "Entries(" << entry.type() << "):";
1656 auto dSz=static_cast<int>(input->readULong(4));
1657 auto N=static_cast<int>(input->readULong(2));
1658 f << "N=" << N << ",";
1659 if (pos+4+dSz>endPos||dSz!=2+14*N) {
1660 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetZone9: the data size seems bad\n"));
1661 f << "###";
1662 ascFile.addPos(pos);
1663 ascFile.addNote(f.str().c_str());
1664 return false;
1665 }
1666 ascFile.addPos(pos);
1667 ascFile.addNote(f.str().c_str());
1668 for (int i=0; i<N; ++i) {
1669 pos=input->tell();
1670 f.str("");
1671 f << entry.type() << "-A" << i << ":";
1672 input->seek(pos+14, librevenge::RVNG_SEEK_SET);
1673 ascFile.addPos(pos);
1674 ascFile.addNote(f.str().c_str());
1675 }
1676 pos=input->tell();
1677 if (pos!=endPos) {
1678 f.str("");
1679 f << entry.type() << "-extra:";
1680 ascFile.addPos(pos);
1681 ascFile.addNote(f.str().c_str());
1682 }
1683 return true;
1684 }
1685
1686 bool RagTimeSpreadsheet::readSpreadsheetSimpleStructure(MWAWEntry const &entry, RagTimeSpreadsheetInternal::Spreadsheet &/*sheet*/)
1687 {
1688 MWAWInputStreamPtr input = m_parserState->m_input;
1689 long pos=entry.begin();
1690 long endPos=entry.end();
1691 if (pos<=0 || entry.length()<8 || !input->checkPosition(endPos)) {
1692 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetSimpleStructure: the position seems bad\n"));
1693 return false;
1694 }
1695
1696 libmwaw::DebugStream f;
1697 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
1698 ascFile.addPos(endPos);
1699 ascFile.addNote("_");
1700
1701 input->seek(pos, librevenge::RVNG_SEEK_SET);
1702 f << "Entries(" << entry.type() << "):";
1703 auto dSz=static_cast<int>(input->readULong(4));
1704 auto headerSz=static_cast<int>(input->readULong(2));
1705 if (pos+4+dSz>endPos||headerSz<18||headerSz>dSz) {
1706 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetSimpleStructure: the data size seems bad\n"));
1707 f << "###";
1708 ascFile.addPos(pos);
1709 ascFile.addNote(f.str().c_str());
1710 return false;
1711 }
1712 auto fSz=static_cast<int>(input->readULong(2));
1713 auto N=static_cast<int>(input->readULong(2));
1714 f << "N=" << N << "[" << fSz << "],";
1715 for (int i=0; i<2; ++i) { // f0=4|c
1716 auto val=static_cast<int>(input->readLong(2));
1717 if (val) f << "f" << i << "=" << val << ",";
1718 }
1719 auto ptrSz=static_cast<int>(input->readLong(2));
1720 if (ptrSz==2||ptrSz==4) f << "ptr[sz]=" << ptrSz << ",";
1721 else if (ptrSz) {
1722 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetSimpleStructure: the ptr size seems bad\n"));
1723 f << "###ptrSz" << ptrSz << ",";
1724 }
1725 auto dataSz=static_cast<int>(input->readLong(4));
1726 if (headerSz+long(N+1)*fSz+dataSz>dSz || fSz < 0) {
1727 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetSimpleStructure: the data size seems bad\n"));
1728 f << "###";
1729 ascFile.addPos(pos);
1730 ascFile.addNote(f.str().c_str());
1731 return false;
1732 }
1733 ascFile.addDelimiter(input->tell(),'|');
1734 ascFile.addPos(pos);
1735 ascFile.addNote(f.str().c_str());
1736 input->seek(pos+4+headerSz, librevenge::RVNG_SEEK_SET);
1737 std::set<long> dataPosSet;
1738 for (int i=0; i<=N; ++i) {
1739 pos=input->tell();
1740 f.str("");
1741 f << entry.type() << "-A" << i << ":";
1742 if (ptrSz) {
1743 const long eSz = long(input->readULong(ptrSz));
1744 long dPos = eSz < std::numeric_limits<long>::max()-entry.begin()-4 ? entry.begin()+4+eSz : std::numeric_limits<long>::max();
1745 f << "pos=" << std::hex << dPos << std::dec << ",";
1746 if (dPos>endPos) {
1747 f << "###";
1748 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetSimpleStructure: the data pos seems bad\n"));
1749 }
1750 else if (dPos<endPos)
1751 dataPosSet.insert(dPos);
1752 }
1753 input->seek(pos+fSz, librevenge::RVNG_SEEK_SET);
1754 ascFile.addPos(pos);
1755 ascFile.addNote(f.str().c_str());
1756 }
1757 f.str("");
1758 f << entry.type() << "-data:";
1759 for (auto dPos : dataPosSet) {
1760 ascFile.addPos(dPos);
1761 ascFile.addNote(f.str().c_str());
1762 }
1763 return true;
1764 }
1765
1766 bool RagTimeSpreadsheet::readSpreadsheetComplexStructure(MWAWEntry const &entry, RagTimeSpreadsheetInternal::Spreadsheet &sheet)
1767 {
1768 RagTimeSpreadsheetInternal::ComplexBlock block;
1769 if (!readBlockHeader(entry, block)) return false;
1770
1771 MWAWInputStreamPtr input = m_parserState->m_input;
1772 long endPos=entry.end();
1773
1774 libmwaw::DebugStream f;
1775 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
1776 ascFile.addPos(endPos);
1777 ascFile.addNote("_");
1778
1779 /* first sort data by first value ( as we need to parse first the
1780 column definition before parsing a cell content ) */
1781 std::map<int,size_t> sortZones;
1782 for (size_t z=0; z< block.m_zones.size(); ++z) {
1783 int const fValue=block.m_zones[z].m_data[0];
1784 if (sortZones.find(fValue)!=sortZones.end()) {
1785 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetComplexStructure: oops, a zone with firstVal=%d already exists\n", fValue));
1786 f.str("");
1787 f << entry.type() << "-unparsed:###";
1788 ascFile.addPos(block.m_zones[z].m_pos);
1789 ascFile.addNote(f.str().c_str());
1790 }
1791 else
1792 sortZones[fValue]=z;
1793 }
1794
1795 long lastEndPos=input->tell(); // end of last block to check for unparsed data
1796 for (auto const &it : sortZones) {
1797 size_t const z=it.second;
1798 auto &contentZone=block.m_zones[z];
1799 long contentEndPos=(block.m_zones.size()>z+1 && block.m_zones[z+1].m_pos<entry.begin()) ?
1800 block.m_zones[z+1].m_pos : endPos;
1801
1802 long pos=contentZone.m_pos;
1803 if (pos<entry.begin() || pos>=contentEndPos) continue;
1804 input->seek(pos, librevenge::RVNG_SEEK_SET);
1805
1806 std::vector<long> posList;
1807 long lastDataPos;
1808 if (!readPositionsList(entry, posList, lastDataPos))
1809 return true;
1810
1811 int const numCellsByRows=contentZone.m_data[2];
1812 if (!posList.empty() && numCellsByRows<=0) {
1813 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetComplexStructure: can not find determine the cell's positions\n"));
1814 continue;
1815 }
1816
1817 int row=contentZone.m_data[0]-1, prevActiveRow=-1;
1818 for (size_t i=0; i<posList.size(); ++i) {
1819 f.str("");
1820 int col=int(i)%numCellsByRows;
1821 if (int(i)/numCellsByRows!=prevActiveRow) {
1822 while (1) {
1823 if (++row >= int(block.m_intList.size()) || row<0) {
1824 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetComplexStructure: can not find determine a cell's row\n"));
1825 f << "###";
1826 break;
1827 }
1828 if (!block.m_intList[size_t(row)]) continue;
1829 // checkme: do we need to check the other block.m_intList[size_t(row)] values
1830 break;
1831 }
1832 prevActiveRow=int(i)/numCellsByRows;
1833 }
1834
1835 long zEndPos=(i+1==posList.size()) ? lastDataPos:posList[i+1];
1836 if (!posList[i] || posList[i]>=zEndPos) continue;
1837
1838 input->seek(posList[i], librevenge::RVNG_SEEK_SET);
1839 MWAWVec2i cellPos(col,row);
1840 bool ok=true;
1841 RagTimeSpreadsheetInternal::Cell emptyCell;
1842 RagTimeSpreadsheetInternal::Cell *cell = nullptr;
1843 if (col>0 && row>0 && entry.id()<=2) {
1844 if (sheet.m_cellsMap.find(cellPos-MWAWVec2i(1,1))==sheet.m_cellsMap.end())
1845 sheet.m_cellsMap[cellPos-MWAWVec2i(1,1)]=RagTimeSpreadsheetInternal::Cell();
1846 cell=&sheet.m_cellsMap.find(cellPos-MWAWVec2i(1,1))->second;
1847 }
1848 if (!cell) cell=&emptyCell;
1849 cell->setPosition(cellPos-MWAWVec2i(1,1));
1850 switch (entry.id()) {
1851 case 0:
1852 if (cellPos[0]<=0 || cellPos[1]<=0)
1853 ok=readSpreadsheetCellDimension(cellPos, zEndPos, sheet);
1854 else
1855 ok=readSpreadsheetCellContent(*cell, zEndPos);
1856 break;
1857 case 1:
1858 ok=readSpreadsheetCellFormat(cellPos, zEndPos, *cell);
1859 break;
1860 case 2: // formula
1861 case 3: { // condition
1862 f << entry.type() << "-C" << col << "x" << row << "]:";
1863 std::string extra("");
1864 std::vector<MWAWCellContent::FormulaInstruction> formula;
1865 auto val=static_cast<int>(input->readULong(1));
1866 if (val) f << "f0=" << std::hex << val << std::dec << ",";
1867 ok=readFormula(cellPos-MWAWVec2i(1,1), formula, zEndPos, extra);
1868 if (ok && entry.id()==2) {
1869 MWAWCellContent &content=cell->m_content;
1870 content.m_formula=formula;
1871 if (cell->validateFormula())
1872 content.m_contentType=MWAWCellContent::C_FORMULA;
1873 }
1874 if (!ok) f << "###";
1875 f << "formula=[";
1876 for (auto const &form : formula)
1877 f << form;
1878 f << "]";
1879 if (!extra.empty()) f << ":" << extra;
1880 f << ",";
1881 ascFile.addPos(posList[i]);
1882 ascFile.addNote(f.str().c_str());
1883 ok=true;
1884 break;
1885 }
1886 // case 4: list of cell which reference this cell, ok to ignore
1887 default:
1888 ok=false;
1889 break;
1890 }
1891 if (!ok) {
1892 f << entry.type() << "-C" << col << "x" << row << "]:";
1893 if (row==0) f << "col,";
1894 else if (col==0) f << "row,";
1895
1896 ascFile.addPos(posList[i]);
1897 ascFile.addNote(f.str().c_str());
1898 }
1899 }
1900 if (lastDataPos>lastEndPos) lastEndPos=lastDataPos;
1901 }
1902
1903 if (lastEndPos<endPos) {
1904 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetComplexStructure: find some extra data\n"));
1905 f.str("");
1906 f << entry.type() << "-extra:###";
1907 ascFile.addPos(lastEndPos);
1908 ascFile.addNote(f.str().c_str());
1909 }
1910 input->seek(lastEndPos, librevenge::RVNG_SEEK_SET);
1911 return true;
1912 }
1913
1914 ////////////////////////////////////////////////////////////
1915 // spreadsheet zone v2...
1916 ////////////////////////////////////////////////////////////
1917 bool RagTimeSpreadsheet::readSpreadsheetV2(MWAWEntry &entry)
1918 {
1919 MWAWInputStreamPtr input = m_parserState->m_input;
1920 long pos=entry.begin();
1921 if (pos<=0 || !input->checkPosition(pos+6)) {
1922 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetV2: the position seems bad\n"));
1923 return false;
1924 }
1925 if (version()>=2) {
1926 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetV2: must not be called for v3... file\n"));
1927 return false;
1928 }
1929 entry.setParsed(true);
1930 input->seek(pos, librevenge::RVNG_SEEK_SET);
1931 libmwaw::DebugStream f;
1932 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
1933 f << "Entries(SpreadsheetZone):";
1934 auto dSz=static_cast<int>(input->readULong(2));
1935 long endPos=pos+2+dSz;
1936 if (dSz<4 || !input->checkPosition(endPos)) {
1937 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetV2: the size seems bad\n"));
1938 f << "###";
1939 ascFile.addPos(pos);
1940 ascFile.addNote(f.str().c_str());
1941 return false;
1942 }
1943 ascFile.addPos(endPos);
1944 ascFile.addNote("_");
1945 long zonesList[2]= {0,endPos};
1946 for (int i=0; i<2; ++i) {
1947 long ptr=pos+6+long(input->readULong(2));
1948 f << "ptr[" << i << "]=" << std::hex << ptr << std::dec << ",";
1949 if (ptr>=endPos) {
1950 f << "###";
1951 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetV2: the zone begin seems bad%d\n", i));
1952 continue;
1953 }
1954 zonesList[i]=ptr;
1955 }
1956 ascFile.addPos(pos);
1957 ascFile.addNote(f.str().c_str());
1958
1959 std::shared_ptr<RagTimeSpreadsheetInternal::Spreadsheet> sheet(new RagTimeSpreadsheetInternal::Spreadsheet);
1960 std::stringstream s;
1961 s << "Sheet" << entry.id();
1962 sheet->m_name=s.str();
1963 // first read the last data, which contains the begin of row positions
1964 MWAWEntry extra;
1965 extra.setBegin(zonesList[1]);
1966 extra.setEnd(endPos);
1967 sheet->m_cellsBegin=zonesList[0];
1968 if (!readSpreadsheetExtraV2(extra, *sheet))
1969 return false;
1970
1971 MWAWEntry cells;
1972 cells.setBegin(zonesList[0]);
1973 cells.setEnd(zonesList[1]);
1974 if (!readSpreadsheetCellsV2(cells, *sheet))
1975 return false;
1976 m_state->m_idSpreadsheetMap[entry.id()]=sheet;
1977 return true;
1978 }
1979
1980 bool RagTimeSpreadsheet::readSpreadsheetCellsV2(MWAWEntry &entry, RagTimeSpreadsheetInternal::Spreadsheet &sheet)
1981 {
1982 MWAWInputStreamPtr input = m_parserState->m_input;
1983 long pos=entry.begin();
1984 long endPos=entry.end();
1985 if (pos<=0 || !input->checkPosition(endPos)) {
1986 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellsV2: the position seems bad\n"));
1987 return false;
1988 }
1989
1990 libmwaw::DebugStream f;
1991 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
1992 ascFile.addPos(endPos);
1993 ascFile.addNote("_");
1994
1995 int n=0;
1996 input->seek(pos, librevenge::RVNG_SEEK_SET);
1997 for (size_t row=0; row<sheet.m_rowPositionsList.size(); ++row) {
1998 pos=sheet.m_rowPositionsList[row];
1999 long rEndPos=row+1==sheet.m_rowPositionsList.size() ? endPos : sheet.m_rowPositionsList[row+1];
2000 if (pos<entry.begin() || rEndPos>entry.end()) {
2001 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellsV2: the position of the row cells %d is odd\n", int(row)));
2002 continue;
2003 }
2004 if (pos+2>=rEndPos) continue;
2005 input->seek(pos, librevenge::RVNG_SEEK_SET);
2006 while (1) {
2007 pos=input->tell();
2008 if (pos+2>rEndPos) break;
2009 int col=int(input->readULong(1))-1;
2010 MWAWVec2i cellPos(col,int(row));
2011 auto dSz=static_cast<int>(input->readULong(1));
2012 long zEndPos=pos+6+dSz;
2013 RagTimeSpreadsheetInternal::Cell cell;
2014 cell.setPosition(cellPos);
2015 f.str("");
2016 f << "Entries(SpreadsheetCell)[" << n++ << "]:";
2017 if (zEndPos>endPos) {
2018 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellsV2: problem reading some cells\n"));
2019 f << "###";
2020 ascFile.addPos(pos);
2021 ascFile.addNote(f.str().c_str());
2022 break;
2023 }
2024 else if (!readSpreadsheetCellV2(cell, zEndPos) || cellPos[0]<0||cellPos[1]<0) {
2025 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellsV2: small pb reading a cell\n"));
2026 f << "###";
2027 ascFile.addPos(pos);
2028 ascFile.addNote(f.str().c_str());
2029 }
2030 else if (sheet.m_cellsMap.find(cellPos) != sheet.m_cellsMap.end()) {
2031 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellsV2: already find a cell in (%d,%d)\n", cellPos[0],cellPos[1]));
2032 ascFile.addPos(pos);
2033 ascFile.addNote("###duplicated");
2034 }
2035 else
2036 sheet.m_cellsMap[cellPos]=cell;
2037 if ((dSz%2)==1) ++zEndPos;
2038 input->seek(zEndPos, librevenge::RVNG_SEEK_SET);
2039 }
2040 }
2041 return true;
2042 }
2043
2044 bool RagTimeSpreadsheet::readSpreadsheetCellV2(RagTimeSpreadsheetInternal::Cell &cell, long endPos)
2045 {
2046 MWAWInputStreamPtr input = m_parserState->m_input;
2047 long pos=input->tell();
2048 libmwaw::DebugStream f;
2049 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
2050
2051 f << "Entries(SpreadsheetCell)[C" << cell.position() << "]:";
2052 if (pos+4>endPos) {
2053 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellV2: the zone seems too short\n"));
2054 f << "###";
2055 ascFile.addPos(pos-2);
2056 ascFile.addNote(f.str().c_str());
2057 return false;
2058 }
2059
2060 auto val=static_cast<int>(input->readULong(1));
2061 int type=(val>>4);
2062 MWAWCell::Format format;
2063 switch (type) {
2064 case 0:
2065 format.m_format=MWAWCell::F_NUMBER;
2066 f << "empty,";
2067 break;
2068 case 3:
2069 format.m_format=MWAWCell::F_NUMBER;
2070 format.m_numberFormat=MWAWCell::F_NUMBER_GENERIC;
2071 f << "number,";
2072 break;
2073 case 7:
2074 format.m_format=MWAWCell::F_DATE;
2075 f << "date,";
2076 break;
2077 case 9:
2078 format.m_format=MWAWCell::F_TEXT;
2079 f << "text,";
2080 break;
2081 case 11:
2082 format.m_format=MWAWCell::F_NUMBER;
2083 format.m_numberFormat=MWAWCell::F_NUMBER_GENERIC;
2084 f << "nan,";
2085 break;
2086 default:
2087 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellV2: find unknown type %d\n", type));
2088 f << "##type=" << type << ",";
2089 break;
2090 }
2091 bool hasFont=(val&1);
2092 bool hasAlign=(val&2);
2093 bool hasFormula=(val&4);
2094 bool hasPreFormula=(val&8);
2095 if (hasFormula)
2096 f << "formula,";
2097 if (hasPreFormula)
2098 f << "preFormula,";
2099
2100 val=static_cast<int>(input->readULong(1));
2101 bool hasNumberFormat=false;
2102 if (val&0x80) {
2103 f << "digits[set],";
2104 val&=0x7F;
2105 hasNumberFormat=true;
2106 }
2107 // fl0&30: no change
2108 if (val) f << "fl0=" << std::hex << val << std::dec << ",";
2109 val=static_cast<int>(input->readULong(1));
2110 if (val&0xF0) { // checkme
2111 int borders=0;
2112 f << "bord=";
2113 if (val&0x10) {
2114 borders|=libmwaw::LeftBit;
2115 f << "L";
2116 }
2117 if (val&0x20) {
2118 borders|=libmwaw::RightBit;
2119 f << "R";
2120 }
2121 if (val&0x40) {
2122 borders|=libmwaw::TopBit;
2123 f << "T";
2124 }
2125 if (val&0x80) {
2126 borders|=libmwaw::BottomBit;
2127 f << "B";
2128 }
2129 f << ",";
2130 cell.setBorders(borders, MWAWBorder());
2131 }
2132 if (val&0xF) f << "fl1=" << std::hex << (val&0xf) << std::dec << ",";
2133 val=static_cast<int>(input->readULong(1));
2134 if (val) f << "fl2=" << std::hex << val << std::dec << ",";
2135 long actPos;
2136 if (hasNumberFormat) {
2137 val=static_cast<int>(input->readULong(1));
2138 actPos=input->tell();
2139 bool ok=true, hasDigits=true;
2140 switch (val>>5) {
2141 case 1: // unknown
2142 f << "type1,";
2143 break;
2144 case 3:
2145 f << "currency,";
2146 format.m_numberFormat=MWAWCell::F_NUMBER_CURRENCY;
2147 break;
2148 case 6:
2149 f << "percent,";
2150 format.m_numberFormat=MWAWCell::F_NUMBER_PERCENT;
2151 break;
2152 case 4:
2153 f << "scientific,";
2154 format.m_numberFormat=MWAWCell::F_NUMBER_SCIENTIFIC;
2155 break;
2156 case 2:
2157 f << "decimal,";
2158 format.m_numberFormat=MWAWCell::F_NUMBER_DECIMAL;
2159 break;
2160 case 0:
2161 hasDigits=false;
2162 break;
2163 default:
2164 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellV2: problem read numbering flags\n"));
2165 f << "##type=" << (val>>5) << ",";
2166 hasDigits=ok=false;
2167 break;
2168 }
2169 if (ok) {
2170 val &= 0x1F;
2171 if (hasDigits && actPos+1>endPos) {
2172 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellV2: can not read a digit\n"));
2173 f << "##digits,";
2174 ok=false;
2175 }
2176 else if (hasDigits) {
2177 auto digits=static_cast<int>(input->readULong(1));
2178 if (digits&0xC0) {
2179 f << "digits[high]=" << (digits>>6) << ",";
2180 digits &= 0x3f;
2181 }
2182 format.m_digits=digits;
2183 f << "digits=" << digits << ",";
2184 }
2185 }
2186 else
2187 f << "##";
2188 if (val)
2189 f << "fl3=" << std::hex << val << std::dec << ",";
2190 if (!ok) {
2191 ascFile.addPos(pos-2);
2192 ascFile.addNote(f.str().c_str());
2193 return true;
2194 }
2195 }
2196 if (hasFont) {
2197 if (input->tell()+4>endPos) {
2198 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellV2: problem reading font format\n"));
2199 f << "##font,";
2200 ascFile.addPos(pos-2);
2201 ascFile.addNote(f.str().c_str());
2202 return true;
2203 }
2204 MWAWFont font;
2205 auto size=static_cast<int>(input->readULong(1));
2206 auto flag = static_cast<int>(input->readULong(1));
2207 uint32_t flags=0;
2208 if (flag&0x1) flags |= MWAWFont::boldBit;
2209 if (flag&0x2) flags |= MWAWFont::italicBit;
2210 if (flag&0x4) font.setUnderlineStyle(MWAWFont::Line::Simple);
2211 if (flag&0x8) flags |= MWAWFont::embossBit;
2212 if (flag&0x10) flags |= MWAWFont::shadowBit;
2213 if (flag&0x20) font.setDeltaLetterSpacing(-1);
2214 if (flag&0x40) font.setDeltaLetterSpacing(1);
2215 if (flag&0x80) font.set(MWAWFont::Script::super100());
2216 if (size&0x80) {
2217 font.set(MWAWFont::Script::sub100());
2218 size&=0x7f;
2219 }
2220 font.setSize(float(size));
2221 font.setFlags(flags);
2222 font.setId(m_mainParser->getFontId(static_cast<int>(input->readULong(2))));
2223 cell.setFont(font);
2224 f << "font=[" << font.getDebugString(m_parserState->m_fontConverter) << "],";
2225 }
2226 MWAWCellContent &content=cell.m_content;
2227 if (hasPreFormula) {
2228 std::string extra("");
2229 auto &formula=content.m_formula;
2230 bool ok=readFormulaV2(cell.position(), formula, endPos, extra);
2231 f << "formula=[";
2232 for (auto const &form : formula)
2233 f << form;
2234 f << "]";
2235 if (!extra.empty()) f << ":" << extra;
2236 f << ",";
2237 if (!ok) {
2238 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellV2: can not read a preFormula\n"));
2239 f << "###formula,";
2240 ascFile.addPos(pos-2);
2241 ascFile.addNote(f.str().c_str());
2242 return true;
2243 }
2244 if (cell.validateFormula())
2245 content.m_contentType=MWAWCellContent::C_FORMULA;
2246 if (input->tell()!=endPos) ascFile.addDelimiter(input->tell(),'|');
2247 }
2248 val= hasAlign ? static_cast<int>(input->readULong(1)) : 0;
2249 int align= val&7;
2250 switch (align) {
2251 case 0:
2252 break;
2253 case 2:
2254 cell.setHAlignment(MWAWCell::HALIGN_LEFT);
2255 f << "left,";
2256 break;
2257 case 3:
2258 cell.setHAlignment(MWAWCell::HALIGN_CENTER);
2259 f << "center,";
2260 break;
2261 case 4:
2262 cell.setHAlignment(MWAWCell::HALIGN_RIGHT);
2263 f << "right,";
2264 break;
2265 case 5: // full(repeat)
2266 cell.setHAlignment(MWAWCell::HALIGN_LEFT);
2267 f << "repeat,";
2268 break;
2269 default:
2270 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellV2: find unknown alignment\n"));
2271 f << "##align=" << align << ",";
2272 break;
2273 }
2274 val&=0xF8;
2275 if (val) f << "align[high]=" << std::hex << val << std::dec << ",";
2276 if (hasFormula) {
2277 std::string extra("");
2278 std::vector<MWAWCellContent::FormulaInstruction> condition;
2279 auto &formula=hasPreFormula ? condition : content.m_formula;
2280 bool ok=readFormulaV2(cell.position(), formula, endPos, extra);
2281 if (hasPreFormula)
2282 f << "condition=[";
2283 else
2284 f << "formula=[";
2285 for (auto const &form : formula)
2286 f << form;
2287 f << "]";
2288 if (!extra.empty()) f << ":" << extra;
2289 f << ",";
2290 if (!ok) {
2291 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellV2: can not read a formula\n"));
2292 f << "###formula,";
2293 ascFile.addPos(pos-2);
2294 ascFile.addNote(f.str().c_str());
2295 return true;
2296 }
2297 if (!hasPreFormula && cell.validateFormula())
2298 content.m_contentType=MWAWCellContent::C_FORMULA;
2299 if (input->tell()!=endPos) ascFile.addDelimiter(input->tell(),'|');
2300 }
2301
2302 actPos=input->tell();
2303 switch (type) {
2304 case 0:
2305 if (actPos!=endPos) {
2306 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellV2: something look bad\n"));
2307 f << "###data";
2308 break;
2309 }
2310 break;
2311 case 3: {
2312 if (actPos+10!=endPos) {
2313 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellV2: can not read a number\n"));
2314 f << "###number";
2315 break;
2316 }
2317 if (format.m_format==MWAWCell::F_UNKNOWN)
2318 format.m_format=MWAWCell::F_NUMBER;
2319 if (content.m_contentType!=MWAWCellContent::C_FORMULA)
2320 content.m_contentType=MWAWCellContent::C_NUMBER;
2321 double res;
2322 bool isNan;
2323 if (!input->readDouble10(res, isNan))
2324 f << "#value,";
2325 else {
2326 content.setValue(res);
2327 f << res << ",";
2328 }
2329 break;
2330 }
2331 case 7: {
2332 if (actPos+4!=endPos) {
2333 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellV2: can not read a date\n"));
2334 f << "###number";
2335 break;
2336 }
2337 if (format.m_format==MWAWCell::F_UNKNOWN)
2338 format.m_format=MWAWCell::F_DATE;
2339 if (content.m_contentType!=MWAWCellContent::C_FORMULA)
2340 content.m_contentType=MWAWCellContent::C_NUMBER;
2341 auto Y=static_cast<int>(input->readULong(2));
2342 auto M=static_cast<int>(input->readULong(1));
2343 auto D=static_cast<int>(input->readULong(1));
2344 f << M << "/" << D << "/" << Y << ",";
2345 double res;
2346 if (!MWAWCellContent::date2Double(Y,M,D,res))
2347 f << "#date,";
2348 else
2349 content.setValue(res);
2350 break;
2351 }
2352 case 9: {
2353 auto sSz=static_cast<int>(input->readULong(1));
2354 if (actPos+1+sSz!=endPos) {
2355 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellV2: can not read a text\n"));
2356 f << "###text";
2357 break;
2358 }
2359 if (format.m_format==MWAWCell::F_UNKNOWN)
2360 format.m_format=MWAWCell::F_TEXT;
2361 if (content.m_contentType!=MWAWCellContent::C_FORMULA)
2362 content.m_contentType=MWAWCellContent::C_TEXT;
2363 content.m_textEntry.setBegin(input->tell());
2364 content.m_textEntry.setLength(sSz);
2365 std::string text("");
2366 for (int i=0; i<sSz; ++i) text+=char(input->readULong(1));
2367 f << text << ",";
2368 break;
2369 }
2370 case 11: {
2371 if (actPos+1!=endPos) {
2372 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetCellV2: can not read a nan number\n"));
2373 f << "###nan";
2374 break;
2375 }
2376 if (format.m_format==MWAWCell::F_UNKNOWN)
2377 format.m_format=MWAWCell::F_NUMBER;
2378 if (content.m_contentType!=MWAWCellContent::C_FORMULA)
2379 content.m_contentType=MWAWCellContent::C_NUMBER;
2380 cell.m_content.setValue(std::numeric_limits<double>::quiet_NaN());
2381 val=static_cast<int>(input->readULong(1));
2382 f << "nan=" << val << ",";
2383 break;
2384 }
2385 default:
2386 break;
2387 }
2388 cell.setFormat(format);
2389 actPos=input->tell();
2390 if (actPos!=endPos)
2391 ascFile.addDelimiter(actPos,'|');
2392 ascFile.addPos(pos-2);
2393 ascFile.addNote(f.str().c_str());
2394 return true;
2395 }
2396
2397 bool RagTimeSpreadsheet::readSpreadsheetExtraV2(MWAWEntry &entry, RagTimeSpreadsheetInternal::Spreadsheet &sheet)
2398 {
2399 MWAWInputStreamPtr input = m_parserState->m_input;
2400 long pos=entry.begin();
2401 long endPos=entry.end();
2402 if (pos<=0 || !input->checkPosition(endPos)) {
2403 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetExtraV2: the position seems bad\n"));
2404 return false;
2405 }
2406
2407 input->seek(pos, librevenge::RVNG_SEEK_SET);
2408 libmwaw::DebugStream f;
2409 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
2410 ascFile.addPos(endPos);
2411 ascFile.addNote("_");
2412
2413 for (int i=0; i<2; ++i) {
2414 pos=input->tell();
2415 f.str("");
2416 static char const *what[]= {"SpreadsheetRow", "SpreadsheetCol"};
2417 f << "Entries(" << what[i] << "):";
2418 auto n=static_cast<int>(input->readULong(2));
2419 f << "N=" << n << ",";
2420 static int const dataSize[]= {20,14};
2421 if (pos+2+dataSize[i]*n>endPos) {
2422 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetExtraV2: problem reading some spreadsheetZone Col/Row field\n"));
2423 f << "###";
2424 ascFile.addPos(pos);
2425 ascFile.addNote(f.str().c_str());
2426 return false;
2427 }
2428 ascFile.addPos(pos);
2429 ascFile.addNote(f.str().c_str());
2430 int prevDim=0;
2431 std::vector<float> &dims=i==0 ? sheet.m_heightRows : sheet.m_widthCols;
2432 for (int j=0; j<n; ++j) {
2433 pos=input->tell();
2434 f.str("");
2435 f << what[i] << "-" << j << ":";
2436 int val;
2437 for (int k=0; k<7; ++k) { // f0=0|80, f1=0|8|10, f4=0|20|60|61, f5=2|42|82,f6=1|3
2438 val=static_cast<int>(input->readULong(1));
2439 if (val) f << "f" << k << "=" << std::hex << val << std::dec << ",";
2440 }
2441 f << "font[";
2442 f << "sz=" << input->readLong(2) << ",";
2443 val=static_cast<int>(input->readULong(1));
2444 if (val) f << "fl=" << std::hex << val << std::dec << ",";
2445 f << "id=" << input->readULong(2) << ",";
2446 f << "],";
2447 auto dim=static_cast<int>(input->readULong(2));
2448 if (dim<prevDim) {
2449 f << "###dim, ";
2450 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readSpreadsheetExtraV2: problem reading some position\n"));
2451 dims.push_back(0);
2452 }
2453 else {
2454 dims.push_back(float(dim-prevDim));
2455 prevDim=dim;
2456 }
2457 f << "dim=" << float(dim-prevDim) << ",";
2458 if (i==0) {
2459 f << "height=" << input->readULong(2) << ",";
2460 long rowPos=sheet.m_cellsBegin+long(input->readULong(4));
2461 sheet.m_rowPositionsList.push_back(rowPos);
2462 f << "pos?=" << std::hex << rowPos << std::dec << ",";
2463 }
2464 input->seek(pos+dataSize[i], librevenge::RVNG_SEEK_SET);
2465 ascFile.addPos(pos);
2466 ascFile.addNote(f.str().c_str());
2467 }
2468 }
2469
2470 /* finally something like
2471 86000000200201000c000014003c00030c090000
2472 or
2473 86000000204201000a000003004000060d0a000000000ce5410000010000000000000000688f688f688f0000000000000000688f688f688f000000000000
2474 font + ?
2475 */
2476 ascFile.addPos(input->tell());
2477 ascFile.addNote("SpreadsheetZone[end]:");
2478 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2479 return true;
2480 }
2481
2482 ////////////////////////////////////////////////////////////
2483 // read a zone of spreadsheet
2484 ////////////////////////////////////////////////////////////
2485
2486 ////////////////////////////////////////////////////////////
2487 //
2488 // Low level
2489 //
2490 ////////////////////////////////////////////////////////////
2491
2492 ////////////////////////////////////////////////////////////
2493 // send data to the listener
2494 ////////////////////////////////////////////////////////////
2495 bool RagTimeSpreadsheet::send(RagTimeSpreadsheetInternal::Spreadsheet &sheet, MWAWSpreadsheetListenerPtr listener)
2496 {
2497 if (!listener) {
2498 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::send: I can not find the listener\n"));
2499 return false;
2500 }
2501 sheet.m_isSent=true;
2502 std::vector<float> colWidth=sheet.getColumnsWidth();
2503 listener->openSheet(colWidth, librevenge::RVNG_POINT, std::vector<int>(), sheet.m_name);
2504
2505 MWAWInputStreamPtr &input=m_parserState->m_input;
2506 int prevRow = -1;
2507 float rowHeight=0;
2508 for (auto const &cIt : sheet.m_cellsMap) {
2509 RagTimeSpreadsheetInternal::Cell cell= cIt.second;
2510 if (cell.position()[1]>prevRow+1) {
2511 while (cell.position()[1] > prevRow+1) {
2512 if (prevRow != -1) listener->closeSheetRow();
2513 int numRepeat;
2514 float h=sheet.getRowHeight(prevRow+1, numRepeat);
2515 if (cell.position()[1]<prevRow+1+numRepeat)
2516 numRepeat=cell.position()[1]-1-prevRow;
2517 listener->openSheetRow(h, librevenge::RVNG_POINT, numRepeat);
2518 prevRow+=numRepeat;
2519 }
2520 }
2521 if (cell.position()[1] > prevRow) {
2522 if (prevRow != -1) listener->closeSheetRow();
2523 listener->openSheetRow(sheet.getRowHeight(++prevRow), librevenge::RVNG_POINT);
2524 }
2525 MWAWCellContent content=cell.m_content;
2526 // change the reference date from 1/1/1904 to 1/1/1900
2527 if (cell.getFormat().m_format==MWAWCell::F_DATE && content.isValueSet())
2528 content.setValue(content.m_value+1460.);
2529 listener->openSheetCell(cell, content);
2530 if (cell.m_textEntry.valid()) {
2531 listener->setFont(cell.getFont());
2532 int width=0;
2533 if ((cell.rotation()>45 && cell.rotation()<145)||
2534 (cell.rotation()>225 && cell.rotation()<315))
2535 width=int(rowHeight);
2536 else if (cell.position()[0]>=0 || cell.position()[0]<int(colWidth.size()))
2537 width=int(colWidth[size_t(cell.position()[0])]);
2538 else {
2539 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::send: can not determine the text width zone\n"));
2540 }
2541 if (m_mainParser->readTextZone(cell.m_textEntry, width))
2542 m_mainParser->sendText(cell.m_textEntry.id(), listener);
2543 else {
2544 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::send: can not find a text zone\n"));
2545 }
2546 }
2547 else if (cell.m_content.m_textEntry.valid()) {
2548 listener->setFont(cell.getFont());
2549 input->seek(cell.m_content.m_textEntry.begin(), librevenge::RVNG_SEEK_SET);
2550 while (!input->isEnd() && input->tell()<cell.m_content.m_textEntry.end()) {
2551 auto c=static_cast<unsigned char>(input->readULong(1));
2552 if (c==0xd)
2553 listener->insertEOL();
2554 else
2555 listener->insertCharacter(c);
2556 }
2557 }
2558 listener->closeSheetCell();
2559 }
2560 if (prevRow!=-1) listener->closeSheetRow();
2561 listener->closeSheet();
2562 return true;
2563 }
2564
2565 bool RagTimeSpreadsheet::send(int zId, MWAWPosition const &pos)
2566 {
2567 MWAWListenerPtr listener=m_parserState->getMainListener();
2568 if (!listener) {
2569 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::send: I can not find the listener\n"));
2570 return false;
2571 }
2572 auto it=m_state->m_idSpreadsheetMap.find(zId);
2573 if (it==m_state->m_idSpreadsheetMap.end() || !it->second) {
2574 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::send: can not find the spreadsheet %d\n", zId));
2575 return false;
2576 }
2577 auto &sheet=*it->second;
2578 MWAWBox2f box=MWAWBox2f(MWAWVec2f(0,0), pos.size());
2579 MWAWSpreadsheetEncoder spreadsheetEncoder;
2580 MWAWSpreadsheetListenerPtr spreadsheetListener(new MWAWSpreadsheetListener(*m_parserState, box, &spreadsheetEncoder));
2581 spreadsheetListener->startDocument();
2582 send(sheet, spreadsheetListener);
2583 spreadsheetListener->endDocument();
2584 MWAWEmbeddedObject object;
2585 if (spreadsheetEncoder.getBinaryResult(object))
2586 listener->insertPicture(pos, object);
2587 return true;
2588 }
2589
2590 void RagTimeSpreadsheet::flushExtra()
2591 {
2592 MWAWListenerPtr listener=m_parserState->getMainListener();
2593 if (!listener) {
2594 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::flushExtra: can not find the listener\n"));
2595 return;
2596 }
2597 for (auto const &it : m_state->m_idSpreadsheetMap) {
2598 if (!it.second) continue;
2599 auto const &zone=*it.second;
2600 if (zone.m_isSent) continue;
2601 static bool first=true;
2602 if (first) {
2603 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::flushExtra: find some unsend zone\n"));
2604 first=false;
2605 }
2606 MWAWPosition pos(MWAWVec2f(0,0), MWAWVec2f(200,200), librevenge::RVNG_POINT);
2607 pos.m_anchorTo=MWAWPosition::Char;
2608 send(it.first, pos);
2609 listener->insertEOL();
2610 }
2611 }
2612
2613 ////////////////////////////////////////////////////////////
2614 // formula
2615 ////////////////////////////////////////////////////////////
2616 bool RagTimeSpreadsheet::readCellInFormula(MWAWVec2i const &cellPos, bool canBeList, MWAWCellContent::FormulaInstruction &instr, long endPos, std::string &extra)
2617 {
2618 bool isList=canBeList;
2619 MWAWInputStreamPtr input=m_parserState->m_input;
2620 libmwaw::DebugStream f;
2621
2622 instr.m_type= isList ? MWAWCellContent::FormulaInstruction::F_CellList :
2623 MWAWCellContent::FormulaInstruction::F_Cell;
2624 f << "Cell=";
2625 long pos=input->tell();
2626 if (pos+2>endPos) {
2627 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormula: the size seems bad\n"));
2628 f << "#size,";
2629 extra=f.str();
2630 return false;
2631 }
2632 auto val=static_cast<int>(input->readULong(1));
2633 if (val&0x30) { // does not seem to have any sens
2634 f << "fl=" << std::hex << (val&0x30) << std::dec << ",";
2635 val &= 0xCF;
2636 }
2637 if (isList) {
2638 if ((val&0x40)==0)
2639 f << "noFlList,";
2640 val &= 0xBF;
2641 }
2642 if (val<0x80 || (!isList && val>0x83) || (isList && val>0x8f)) {
2643 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormula: find unknown begin\n"));
2644 f << "#f0=" << std::hex << val << std::dec << ",";
2645 extra=f.str();
2646 return false;
2647 }
2648 instr.m_positionRelative[0][0]=(val&2);
2649 instr.m_positionRelative[0][1]=(val&1);
2650 instr.m_positionRelative[1][0]=(val&8);
2651 instr.m_positionRelative[1][1]=(val&4);
2652 val=static_cast<int>(input->readULong(1));
2653 int type0=(val>>5)&7;
2654 int type1=(val>>2)&7;
2655 if (type0&4) {
2656 type0&=3;
2657 instr.m_type=MWAWCellContent::FormulaInstruction::F_CellList;
2658 isList=true;
2659 }
2660 else { // no cell 0, ie a simple cell list reduced to a cell
2661 instr.m_type=MWAWCellContent::FormulaInstruction::F_Cell;
2662 isList=false;
2663 }
2664 for (int c=0; c<2; ++c) {
2665 switch (type0) {
2666 case 0:
2667 if (instr.m_positionRelative[c][1]==false) {
2668 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormula: find unexpected type0 cell\n"));
2669 f << "#type0[abs],";
2670 extra=f.str();
2671 return false;
2672 }
2673 instr.m_position[c][1]=cellPos[1];
2674 if (instr.m_positionRelative[c][0]==true) {
2675 if (val<0x10)
2676 instr.m_position[c][0]=cellPos[0]+val;
2677 else if (val<0x20)
2678 instr.m_position[c][0]=cellPos[0]+val-0x20;
2679 }
2680 else
2681 instr.m_position[c][0]=val-5;
2682 break;
2683 case 1:
2684 if (instr.m_positionRelative[c][0]==false) {
2685 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormula: find unexpected type0 cell\n"));
2686 f << "#type0=1[abs],";
2687 extra=f.str();
2688 return false;
2689 }
2690 val &= 0x1F;
2691 instr.m_position[c][0]=cellPos[0];
2692 if (instr.m_positionRelative[c][1]==true) {
2693 if (val<0x10)
2694 instr.m_position[c][1]=cellPos[1]+val;
2695 else if (val<0x20)
2696 instr.m_position[c][1]=cellPos[1]+val-0x20;
2697 }
2698 else
2699 instr.m_position[c][1]=val-5;
2700 break;
2701 case 2: {
2702 int firstValue=((val>>2)&0x7);
2703 int secondValue=(val&0x3);
2704 if (instr.m_positionRelative[c][0]==true) {
2705 if (instr.m_positionRelative[c][1]==false) {
2706 if (firstValue==0) {
2707 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormula: can not determine the row\n"));
2708 f << "#type0=2[row],";
2709 extra=f.str();
2710 return false;
2711 }
2712 instr.m_position[c][1]=firstValue-1;
2713 }
2714 else if (firstValue&4)
2715 instr.m_position[c][1]=cellPos[1]+firstValue-8;
2716 else
2717 instr.m_position[c][1]=cellPos[1]+firstValue;
2718 if (secondValue&2)
2719 instr.m_position[c][0]=cellPos[0]+secondValue-4;
2720 else
2721 instr.m_position[c][0]=cellPos[0]+secondValue;
2722 }
2723 else if (instr.m_positionRelative[c][1]==true) {
2724 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormula: find unexpected type0 cell\n"));
2725 f << "#type0=2[rel/abs],";
2726 extra=f.str();
2727 return false;
2728 }
2729 else {
2730 std::stringstream s;
2731 s << "Ref" << firstValue << ",";
2732 instr.m_sheet[0]=s.str().c_str();
2733 return true;
2734 }
2735 break;
2736 }
2737 case 3: {
2738 if (type1>5) {
2739 f << "type0=" << type0 << ",type1=" << type1 << ",";
2740 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormula: unexpected number of bytes for type1\n"));
2741 extra=f.str();
2742 return false;
2743 }
2744 int nExpectedBits=2*type1+3+(type1>=4?3:0); // between 3 and 16
2745 int nBytesToRead=(nExpectedBits-2+7)/8; // between 1 and 2
2746 if (input->tell()+nBytesToRead>endPos) {
2747 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormula: unexpected end\n"));
2748 f << "#end[row],";
2749 extra=f.str();
2750 return false;
2751 }
2752 int readValue=((val&3)<<(nBytesToRead*8))+static_cast<int>(input->readULong(nBytesToRead));
2753 int nBitRemain=2+8*nBytesToRead-nExpectedBits;
2754 int secondValue=(readValue&((1<<nBitRemain)-1));
2755
2756 int firstValue=(readValue>>nBitRemain);
2757 if (instr.m_positionRelative[c][1]==true) {
2758 if (firstValue>=(1<<(nExpectedBits-1)))
2759 firstValue-=(1<<nExpectedBits);
2760 firstValue+=cellPos[1];
2761 }
2762 else
2763 --firstValue;
2764 instr.m_position[c][1]=firstValue;
2765
2766 if ((instr.m_positionRelative[c][0]==false && secondValue==0) ||
2767 (instr.m_positionRelative[c][0]==true && nBitRemain<=2)) {
2768 if (input->tell()>=endPos) {
2769 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormula: unexpected end\n"));
2770 f << "#end[col],";
2771 extra=f.str();
2772 return false;
2773 }
2774 secondValue=static_cast<int>(input->readULong(1));
2775 nBitRemain=8;
2776 }
2777 if (instr.m_positionRelative[c][0]==false) {
2778 if (secondValue<5) {
2779 std::stringstream s;
2780 s << "Ref" << firstValue+1 << ",";
2781 instr.m_position[c][1]=0;
2782 instr.m_sheet[0]=s.str().c_str();
2783 return true;
2784 }
2785 else
2786 instr.m_position[c][0]=secondValue-5;
2787 }
2788 // checkme: seems to work, but I am very unsure of this code
2789 else if (nBitRemain!=8 && (secondValue&(1<<(nBitRemain-1))))
2790 instr.m_position[c][0]=(256+secondValue+cellPos[0]-(1<<nBitRemain))%256;
2791 else
2792 instr.m_position[c][0]=(secondValue+cellPos[0])%256;
2793 break;
2794 }
2795 default:
2796 f << "type0=" << type0 << ",type1=" << type1 << ",";
2797 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormula: I only know how to read type0 field\n"));
2798 extra=f.str();
2799 return false;
2800 }
2801
2802 if (!isList || c==1) break;
2803 if (input->tell()>=endPos) {
2804 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormula: can not read the second cell\n"));
2805 f << "#end[cell]=" << instr.m_position[0] << ",";
2806 extra=f.str();
2807 return false;
2808 }
2809
2810 val=static_cast<int>(input->readULong(1));
2811 type0=(val>>5)&7;
2812 type1=(val>>2)&7;
2813 }
2814 if (instr.m_position[0][0]<0||instr.m_position[0][1]<0||
2815 (instr.m_type==MWAWCellContent::FormulaInstruction::F_CellList &&
2816 (instr.m_position[1][0]<0||instr.m_position[1][1]<0))) {
2817 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormula: bads cell\n"));
2818 f << "#cell=" << instr << ",";
2819 extra=f.str();
2820 return false;
2821 }
2822 return true;
2823 }
2824
2825 bool RagTimeSpreadsheet::readCellInFormulaV2(MWAWVec2i const &cellPos, bool canBeList, MWAWCellContent::FormulaInstruction &instr, long endPos, std::string &extra)
2826 {
2827 MWAWInputStreamPtr input=m_parserState->m_input;
2828 libmwaw::DebugStream f;
2829
2830 instr.m_type=MWAWCellContent::FormulaInstruction::F_Cell;
2831 f << "Cell=";
2832 bool ok=true;
2833 int which=0;
2834 int page=-1, frame=-1;
2835 while (!input->isEnd()) {
2836 long pos=input->tell();
2837 if (pos+2>endPos) break;
2838 auto what=static_cast<int>(input->readULong(1));
2839 if (canBeList && (what==0x83 || what==0x84) && which==0) {
2840 instr.m_type=MWAWCellContent::FormulaInstruction::F_CellList;
2841 which=1;
2842 what&=0xF;
2843 instr.m_position[1]=instr.m_position[0];
2844 instr.m_positionRelative[1]=instr.m_positionRelative[0];
2845 }
2846 if (what < 3 || what > 6) {
2847 ok=false;
2848 f << "##marker=" << what << ",";
2849 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormulaV2: find unknown marker %d\n", what));
2850 break;
2851 }
2852 auto val = static_cast<int>(input->readULong(1));
2853 int flag=0;
2854 if (val==0x80 || val==0xc0 || val==0xFF) {
2855 flag=val;
2856 val = static_cast<int>(input->readULong(1));
2857 }
2858 if (input->tell()>endPos) {
2859 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormulaV2: the %d marker data seems odd\n", what));
2860 f << "###market[data]=" << what << ",";
2861 ok=false;
2862 break;
2863 }
2864 bool absolute=false;
2865 if ((flag==0&&!(val&0xC0)) || (flag==0x80 && (val&0xC0)))
2866 absolute=true;
2867 else if (flag==0&&(val&0xe0)==0x60) {
2868 if (what>=5) {
2869 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormulaV2: find relative position for sheet\n"));
2870 f << "###";
2871 }
2872 else
2873 val += 1-0x80+cellPos[4-what];
2874 }
2875 else if (flag==0&&(val&0xe0)==0x40) {
2876 if (what>=5) {
2877 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormulaV2: find relative position for sheet\n"));
2878 f << "###";
2879 }
2880 else
2881 val += 1-0x40+cellPos[4-what];
2882 }
2883 else if (flag==0xc0) {
2884 if (what>=5) {
2885 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormulaV2: find relative position for sheet\n"));
2886 f << "###";
2887 }
2888 else
2889 val += 1+cellPos[4-what];
2890 }
2891 else if (flag==0xff) {
2892 if (what>=5) {
2893 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormulaV2: find relative position for sheet\n"));
2894 f << "###";
2895 }
2896 else
2897 val += -0xff+cellPos[4-what];
2898 }
2899 else {
2900 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormulaV2: can not read a cell position position for sheet\n"));
2901 f << "###v" << what << "=" << std::hex << val << "[" << flag << "]" << std::dec;
2902 ok=false;
2903 break;
2904 }
2905 if (what==3||what==4) {
2906 instr.m_position[which][4-what]=(val-1);
2907 instr.m_positionRelative[which][4-what]=!absolute;
2908 }
2909 else if (what==5 || what==6) {
2910 if (what==5) frame=val;
2911 else page=val;
2912 std::stringstream s;
2913 s << "Sheet";
2914 if (page>=0) s << "P" << page;
2915 if (frame>=0) s << "F" << frame;
2916 instr.m_sheet[0]=s.str().c_str();
2917 }
2918 // coverity[dead_error_line : FALSE]: intended as sanity check
2919 else {
2920 f << "##marker=" << what << ",";
2921 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormulaV2: find unexpected marker %d\n", what));
2922 break;
2923 }
2924 }
2925 if (ok && input->tell()!=endPos) {
2926 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormulaV2: find extra data at the end of a cell\n"));
2927 f << "###cell[extra],";
2928 ok=false;
2929 }
2930 if (ok && (instr.m_position[0][0]<0||instr.m_position[0][0]>255 ||
2931 instr.m_position[0][1]<0||instr.m_position[0][1]>255)) {
2932 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormulaV2: something go wrong\n"));
2933 f << "###cell[position],";
2934 ok=false;
2935 }
2936 if (ok && instr.m_type==MWAWCellContent::FormulaInstruction::F_CellList &&
2937 (instr.m_position[1][0]<0||instr.m_position[1][0]>255 ||
2938 instr.m_position[1][1]<0||instr.m_position[1][1]>255)) {
2939 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readCellInFormulaV2: something go wrong\n"));
2940 f << "###cell[position2],";
2941 ok=false;
2942 }
2943 extra=f.str();
2944 return ok;
2945 }
2946
2947 bool RagTimeSpreadsheet::readFormula(MWAWVec2i const &cellPos, std::vector<MWAWCellContent::FormulaInstruction> &formula, long endPos, std::string &extra)
2948 {
2949 formula.resize(0);
2950
2951 MWAWInputStreamPtr input=m_parserState->m_input;
2952 if (!input || !input->checkPosition(endPos)) {
2953 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readFormula: the input seems bad\n"));
2954 extra="badPos###";
2955 return false;
2956 }
2957 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
2958 libmwaw::DebugStream f;
2959 bool ok=true, lastIsClosePara=false;
2960 while (!input->isEnd()) {
2961 long pos=input->tell();
2962 if (pos >= endPos)
2963 break;
2964 int val, type=static_cast<int>(input->readULong(1));
2965 MWAWCellContent::FormulaInstruction instr;
2966 if (type==0 && pos+1==endPos) // rare but seems ok
2967 break;
2968 switch (type) {
2969 case 5:
2970 if (pos+1+2 > endPos) {
2971 ok = false;
2972 break;
2973 }
2974 val = static_cast<int>(input->readLong(2));
2975 instr.m_type=MWAWCellContent::FormulaInstruction::F_Long;
2976 instr.m_longValue=val;
2977 break;
2978 case 7:
2979 case 8: { // date
2980 if (pos+1+10 > endPos) {
2981 ok = false;
2982 break;
2983 }
2984 double value;
2985 bool isNan;
2986 if (input->readDouble10(value, isNan)) {
2987 instr.m_type=MWAWCellContent::FormulaInstruction::F_Double;
2988 instr.m_doubleValue=value;
2989 }
2990 else
2991 f << "#value,";
2992 break;
2993 }
2994 case 9: {
2995 auto sSz=static_cast<int>(input->readULong(1));
2996 if (pos+1+2+sSz > endPos) {
2997 ok = false;
2998 break;
2999 }
3000 val=char(input->readULong(1));
3001 if (val!='"' && val!='\'') {
3002 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readFormula: the first string char seems odd\n"));
3003 f << "##fChar=" << val << ",";
3004 }
3005 std::string text("");
3006 for (int i=0; i<sSz; ++i) text+=char(input->readULong(1));
3007 instr.m_type=MWAWCellContent::FormulaInstruction::F_Text;
3008 instr.m_content=text;
3009 break;
3010 }
3011 case 0xf: // if delimiter with unknown value
3012 if (pos+1+1 > endPos) {
3013 ok = false;
3014 break;
3015 }
3016 val = static_cast<int>(input->readLong(1));
3017 f << "unkn[sep]=" << val << ",";
3018 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
3019 instr.m_content=";";
3020 break;
3021 case 0x32: {
3022 if (pos+1+1 > endPos) {
3023 ok = false;
3024 break;
3025 }
3026 val=static_cast<int>(input->readULong(1));
3027 std::string funct("");
3028 if (val>=0 && val<0x60) {
3029 static char const *s_functions[] = {
3030 // 0
3031 "Abs(", "Sign(", "Rand()", "Sqrt(", "Sum(", "SumSq(", "Max(", "Min(",
3032 "Average(", "StDev(", "Pi()", "Sin(", "ASin(", "Cos(", "ACos(", "Tan(",
3033 // 1
3034 "ATan(", "Exp(", "Exp1(" /* fixme: exp(x+1)*/, "Ln(", "Ln1(" /* fixme: ln(n+1) */, "Log10(", "Annuity(", "Rate(",
3035 "PV(", "If(", "True()", "False()", "Len(", "Mid(", "Rept(", "Int(",
3036 // 2
3037 "Round(", "Text("/* add a format*/, "Dollar(", "Value(", "Number("/* of cell in list*/, "Row()", "Column()", "Index(",
3038 "Find(", "", "Page()", "Frame()" /* frame?*/, "IsError(", "IsNA(","NA()", "Day(",
3039 // 3
3040 "Month(", "Year("/* or diffyear*/, "DayOfYear("/* checkme*/, "SetDay(", "SetMonth(", "SetYear(",
3041 "AddMonth(", "AddYear(", "Today()", "PrintCyle()"/*print ?*/, "PrintStop("/*print stop*/, "Choose("/*checkme or select */, "Type(", "Find(", "SetFileName(", "Today()"/*hour*/,
3042 // 4
3043 "Today()"/*Minute*/, "", "Second(", "Minute(", "Hour(", "DayOfWeek(", "WeekOf(", "Slope(",
3044 "Intersect(", "LogSlope(", "LogIntersect(", "Mailing(", "Cell(", "Button(", "Today()"/*Now*/, "TotalPage()",
3045
3046 // 5
3047 "", "DayOfWeekUS(", "WeekOfUS(", "", "", "", "", "",
3048 "", "", "", "", "", "", "", "",
3049 };
3050 funct=s_functions[val];
3051 }
3052 if (funct.empty()) {
3053 std::stringstream s;
3054 s << "Funct" << std::hex << val << std::dec << "(";
3055 funct=s.str();
3056 }
3057 instr.m_type=MWAWCellContent::FormulaInstruction::F_Function;
3058 instr.m_content=funct;
3059 size_t functionLength=funct.length();
3060 if (functionLength>2 && instr.m_content[functionLength-1]==')') {
3061 instr.m_content.resize(functionLength-2);
3062 formula.push_back(instr);
3063 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
3064 instr.m_content="(";
3065 formula.push_back(instr);
3066 instr.m_content=")";
3067 }
3068 else if (functionLength>1 && instr.m_content[functionLength-1]=='(') {
3069 instr.m_content.resize(functionLength-1);
3070 formula.push_back(instr);
3071 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
3072 instr.m_content="(";
3073 }
3074
3075 break;
3076 }
3077 default: {
3078 if ((type&0xCC)==0x80) { // checkme: maybe (type&0xC0)==0x80
3079 std::string error("");
3080 input->seek(-1, librevenge::RVNG_SEEK_CUR);
3081 ok=readCellInFormula(cellPos, false, instr, endPos, error);
3082 if (!ok) f << "cell=[" << error << "],";
3083 break;
3084 }
3085 if ((type&0x80)==0x80) {
3086 std::string error("");
3087 input->seek(-1, librevenge::RVNG_SEEK_CUR);
3088 ok=readCellInFormula(cellPos, true, instr, endPos, error);
3089 if (!ok) f << "cell[list]=[" << error << "],";
3090 break;
3091 }
3092 static char const *s_operators[] = {
3093 // 0
3094 "", "", "", "", "", "", "", "",
3095 "", "", "", "", "(", ")", ";", "",
3096 // 1
3097 /* fixme: we need to reconstruct the formula for operator or, and, ... */
3098 "", "", "^", "", "", "*", "/", "",
3099 "And", "", "", "", "", "+", "-", "Or",
3100 // 2
3101 "&", "", "", "", "=", "<", ">", "<=",
3102 ">=", "<>", "", "", "", "", "", "",
3103 };
3104 std::string op("");
3105 if (type>=0 && type< 0x30)
3106 op=s_operators[type];
3107 if (op.empty()) {
3108 ok=false;
3109 break;
3110 }
3111 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
3112 instr.m_content=op;
3113 break;
3114 }
3115 }
3116 if (!ok) {
3117 ascFile.addDelimiter(pos,'#');
3118 f << "###";
3119 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readFormula: can not read a formula\n"));
3120 break;
3121 }
3122 // a cell ref followed Today(), Page(), ... so we must ignored it
3123 if (lastIsClosePara && (instr.m_type==MWAWCellContent::FormulaInstruction::F_Cell ||
3124 instr.m_type==MWAWCellContent::FormulaInstruction::F_CellList)) {
3125 lastIsClosePara=false;
3126 continue;
3127 }
3128 lastIsClosePara= instr.m_type==MWAWCellContent::FormulaInstruction::F_Operator &&
3129 instr.m_content==")";
3130 formula.push_back(instr);
3131 }
3132 extra=f.str();
3133 input->seek(endPos, librevenge::RVNG_SEEK_SET);
3134 if (ok) return true;
3135
3136 f.str("");
3137 for (auto const &form : formula)
3138 f << form;
3139 f << "," << extra;
3140 extra=f.str();
3141 formula.resize(0);
3142 return true;
3143 }
3144
3145 bool RagTimeSpreadsheet::readFormulaV2(MWAWVec2i const &cellPos, std::vector<MWAWCellContent::FormulaInstruction> &formula, long endPos, std::string &extra)
3146 {
3147 formula.resize(0);
3148
3149 MWAWInputStreamPtr input=m_parserState->m_input;
3150 long pos=input->tell();
3151 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
3152 libmwaw::DebugStream f;
3153 long formulaEndPos=pos+1+long(input->readULong(1));
3154 if (formulaEndPos>endPos || !input->checkPosition(endPos)) {
3155 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readFormulaV2: the formula size seems bad\n"));
3156 return false;
3157 }
3158 ascFile.addDelimiter(pos,'|');
3159 bool ok=true;
3160 while (!input->isEnd()) {
3161 pos=input->tell();
3162 if (pos >= formulaEndPos)
3163 break;
3164 int val, type=static_cast<int>(input->readULong(1));
3165 MWAWCellContent::FormulaInstruction instr;
3166 switch (type) {
3167 case 5:
3168 if (pos+1+2 > formulaEndPos) {
3169 ok = false;
3170 break;
3171 }
3172 val = static_cast<int>(input->readLong(2));
3173 instr.m_type=MWAWCellContent::FormulaInstruction::F_Long;
3174 instr.m_longValue=val;
3175 break;
3176 case 6: {
3177 if (pos+1+10 > formulaEndPos) {
3178 ok = false;
3179 break;
3180 }
3181 double value;
3182 bool isNan;
3183 if (!input->readDouble10(value, isNan)) {
3184 instr.m_type=MWAWCellContent::FormulaInstruction::F_Double;
3185 instr.m_doubleValue=value;
3186 }
3187 else
3188 f << "#value,";
3189 break;
3190 }
3191 case 8: {
3192 if (pos+1+4 > formulaEndPos) {
3193 ok = false;
3194 break;
3195 }
3196 auto Y=static_cast<int>(input->readULong(2));
3197 auto M=static_cast<int>(input->readULong(1));
3198 auto D=static_cast<int>(input->readULong(1));
3199 double value;
3200 if (!MWAWCellContent::date2Double(Y,M,D,value)) {
3201 f << "#date,";
3202 break;
3203 }
3204 instr.m_type=MWAWCellContent::FormulaInstruction::F_Double;
3205 instr.m_doubleValue=value;
3206 break;
3207 }
3208 case 9: {
3209 auto sSz=static_cast<int>(input->readULong(1));
3210 if (pos+1+2+sSz > formulaEndPos) {
3211 ok = false;
3212 break;
3213 }
3214 val=char(input->readULong(1));
3215 if (val!='"' && val!='\'') {
3216 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readFormulaV2: the first string char seems odd\n"));
3217 f << "##fChar=" << val << ",";
3218 }
3219 std::string text("");
3220 for (int i=0; i<sSz; ++i) text+=char(input->readULong(1));
3221 instr.m_type=MWAWCellContent::FormulaInstruction::F_Text;
3222 instr.m_content=text;
3223 break;
3224 }
3225 case 0xf: // if delimiter with unknown value
3226 if (pos+1+1 > formulaEndPos) {
3227 ok = false;
3228 break;
3229 }
3230 val = static_cast<int>(input->readLong(1));
3231 f << "unkn[sep]=" << val << ",";
3232 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
3233 instr.m_content=";";
3234 break;
3235 default: {
3236 if ((type&0xF0)==0x80) {
3237 long endCellPos=pos+1+(type-0x80);
3238 if (endCellPos > formulaEndPos) {
3239 ok = false;
3240 break;
3241 }
3242 std::string error("");
3243 ok=readCellInFormulaV2(cellPos, false, instr, endCellPos, error);
3244 if (!ok) f << "cell=[" << error << "],";
3245 break;
3246 }
3247 if ((type&0xF0)==0xc0 || (type&0xF0)==0xd0) {
3248 long endCellPos=pos+1+(type-0xc0);
3249 if (endCellPos > formulaEndPos) {
3250 ok = false;
3251 break;
3252 }
3253 std::string error("");
3254 ok=readCellInFormulaV2(cellPos, true, instr, endCellPos, error);
3255 if (!ok) f << "cell=[" << error << "],";
3256 break;
3257 }
3258 static char const *s_functions[] = {
3259 // 0
3260 "", "", "", "", "", "", "", "",
3261 "", "", "", "", "(", ")", ";", "",
3262 // 1
3263 /* fixme: we need to reconstruct the formula for operator or, and, ... */
3264 "", "", "^", "", "", "*", "/", "",
3265 "And", "", "", "", "", "+", "-", "Or",
3266 // 2
3267 "", "", "", "", "=", "<", ">", "<=",
3268 ">=", "<>", "", "", "", "", "", "",
3269 // 3
3270 "", "", "Abs(", "Sign(", "Rand()", "Sqrt(", "Sum(", "SumSq(",
3271 "Max(", "Min(", "Average(", "StDev(", "Pi()", "Sin(", "ASin(", "Cos(",
3272 // 4
3273 "ACos(", "Tan(", "ATan(", "Exp(", "Exp1(" /* fixme: exp(x+1)*/, "Ln(", "Ln1(" /* fixme: ln(n+1) */, "Log10(",
3274 "Annuity(", "Rate(", "PV(", "If(", "True()", "False()", "Len(", "Mid(",
3275 // 5
3276 "Rept(", "Int(", "Round(", "Text("/* add a format*/, "Dollar(", "Value(", "Number(", "Row()",
3277 "Column()", "Index(", "Find(", "", "Page()", "Frame()" /* frame?*/, "IsError(", "IsNA(",
3278 // 6
3279 "NA()", "Day(", "Month(", "Year(", "DayOfYear("/* checkme*/, "SetDay(", "SetMonth(", "SetYear(",
3280 "AddMonth(", "AddYear(", "Today()", "Funct6b()"/*print ?*/, "Funct6c("/*print stop*/, "Choose("/*checkme or select */, "Type(", "Find(",
3281 // 70
3282 "SetFileName(", "", "", "", "", "", "", "",
3283 "", "", "", "", "", "", "", "",
3284 };
3285 if (type>=0 && type < 0x80)
3286 instr.m_content=s_functions[type];
3287 size_t functionLength=instr.m_content.length();
3288 if (functionLength==0) {
3289 ok=false;
3290 break;
3291 }
3292 if (instr.m_content[0] >= 'A' && instr.m_content[0] <= 'Z') {
3293 instr.m_type=MWAWCellContent::FormulaInstruction::F_Function;
3294 if (functionLength>2 && instr.m_content[functionLength-1]==')') {
3295 instr.m_content.resize(functionLength-2);
3296 formula.push_back(instr);
3297 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
3298 instr.m_content="(";
3299 formula.push_back(instr);
3300 instr.m_content=")";
3301 }
3302 else if (instr.m_content[functionLength-1]=='(') {
3303 instr.m_content.resize(functionLength-1);
3304 formula.push_back(instr);
3305 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
3306 instr.m_content="(";
3307 }
3308 }
3309 else
3310 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
3311 break;
3312 }
3313 }
3314 if (!ok) {
3315 ascFile.addDelimiter(pos,'#');
3316 f << "###";
3317 MWAW_DEBUG_MSG(("RagTimeSpreadsheet::readFormulaV2: can not read a formula\n"));
3318 break;
3319 }
3320 formula.push_back(instr);
3321 }
3322 extra=f.str();
3323 input->seek(formulaEndPos, librevenge::RVNG_SEEK_SET);
3324 if (ok) return true;
3325
3326 f.str("");
3327 for (auto const &form : formula)
3328 f << form;
3329 f << "," << extra;
3330 extra=f.str();
3331 formula.resize(0);
3332 return true;
3333 }
3334
3335 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: