Debian Codegraph @

src/lib/GreatWksDBParser.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 <algorithm>
35 #include <iomanip>
36 #include <iostream>
37 #include <cmath>
38 #include <limits>
39 #include <set>
40 #include <sstream>
41
42 #include <librevenge/librevenge.h>
43
44 #include "MWAWFont.hxx"
45 #include "MWAWFontConverter.hxx"
46 #include "MWAWGraphicStyle.hxx"
47 #include "MWAWHeader.hxx"
48 #include "MWAWParagraph.hxx"
49 #include "MWAWPosition.hxx"
50 #include "MWAWPrinter.hxx"
51 #include "MWAWRSRCParser.hxx"
52 #include "MWAWSection.hxx"
53 #include "MWAWSpreadsheetListener.hxx"
54 #include "MWAWSubDocument.hxx"
55
56 #include "GreatWksDocument.hxx"
57 #include "GreatWksGraph.hxx"
58 #include "GreatWksText.hxx"
59
60 #include "GreatWksDBParser.hxx"
61
62 /** Internal: the structures of a GreatWksDBParser */
63 namespace GreatWksDBParserInternal
64 {
65 /**a big zone header of a GreatWksDBParser */
66 struct BlockHeader {
67 /** constructor */
68 BlockHeader()
69 : m_name("")
70 {
71 for (auto &ptr : m_ptr) ptr=0;
72 }
73 /** returns true if the block is empty */
74 bool isEmpty() const
75 {
76 for (auto ptr : m_ptr) if (ptr) return false;
77 return true;
78 }
79 /** returns true if the entry has many block */
80 bool isComplex() const
81 {
82 return m_ptr[0]!=m_ptr[1] || m_ptr[0]!=m_ptr[2];
83 }
84 //! operator<<
85 friend std::ostream &operator<<(std::ostream &o, BlockHeader const &entry)
86 {
87 o << std::hex << entry.m_ptr[0];
88 if (entry.isComplex()) o << "[" << entry.m_ptr[1] << "," << entry.m_ptr[2] << "]";
89 o << std::dec;
90 if (!entry.m_name.empty()) o << ":" << entry.m_name;
91 return o;
92 }
93
94 /** the list of pointer: first, next, end? */
95 long m_ptr[3];
96 /** the block name */
97 std::string m_name;
98 };
99
100 /** a big block of a GreatWksDBParser */
101 struct Block {
102 //! a small block of a GreatWksDBParserInternal::Block
103 struct Zone {
104 /** constructor */
105 Zone()
106 : m_ptr(0)
107 , m_N(0)
108 , m_dataSize(0)
109 {
110 }
111 /** the begin zone pointer */
112 long m_ptr;
113 /** the number of sub data */
114 int m_N;
115 /** the data size */
116 long m_dataSize;
117 };
118 //! constructor
119 explicit Block(BlockHeader const &header)
120 : m_header(header)
121 , m_zoneList()
122 {
123 }
124 //! constructor given a zone
125 Block(BlockHeader const &header, Zone &zone)
126 : m_header(header)
127 , m_zoneList()
128 {
129 m_zoneList.push_back(zone);
130 }
131 //! destructor
132 ~Block()
133 {
134 }
135 //! returns true if the zone list is empty
136 bool isEmpty() const
137 {
138 return m_zoneList.empty();
139 }
140 //! returns the number of zone
141 size_t getNumZones() const
142 {
143 return m_zoneList.size();
144 }
145 //! returns the ith zone
146 Zone const &getZone(size_t i) const
147 {
148 if (i>=m_zoneList.size()) {
149 MWAW_DEBUG_MSG(("GreatWksDBParserInternal::Block: can not find zone %d\n", int(i)));
150 static Zone empty;
151 return empty;
152 }
153 return m_zoneList[i];
154 }
155 //! the corresponding entry header
156 BlockHeader m_header;
157 //! the zone list
158 std::vector<Zone> m_zoneList;
159 private:
160 Block(Block const &orig) = delete;
161 Block &operator=(Block const &orig) = delete;
162 };
163
164 /** a cell of a GreatWksDBParser */
165 class Cell final : public MWAWCell
166 {
167 public:
168 /// constructor
169 Cell()
170 : m_content()
171 , m_pictEntry() { }
172 Cell(Cell const &)=default;
173 Cell &operator=(Cell const &)=default;
174 //! destructor
175 ~Cell() final;
176 //! returns true if the cell do contain any content
177 bool isEmpty() const
178 {
179 return m_content.empty() && !hasBorders() && !m_pictEntry.valid();
180 }
181
182 //! the cell content
183 MWAWCellContent m_content;
184 //! picture
185 MWAWEntry m_pictEntry;
186 };
187
188 Cell::~Cell()
189 {
190 }
191
192 /** a field of a GreatWksDBParser */
193 struct Field {
194 //! the file type
195 enum Type { F_Unknown, F_Text, F_Number, F_Date, F_Time, F_Memo, F_Picture, F_Formula, F_Summary };
196 //! constructor
197 Field()
198 : m_type(F_Unknown)
199 , m_id(-1)
200 , m_name("")
201 , m_format()
202 , m_linkZone(0)
203 , m_recordBlock()
204 , m_formula()
205 , m_summaryType(0)
206 , m_summaryField(0)
207 , m_isSequence(false)
208 , m_firstNumber(1)
209 , m_incrementNumber(1)
210 , m_extra("")
211 {
212 }
213 //! update the cell to correspond to the final data
214 bool updateCell(int row, int numRow, Cell &cell) const;
215 //! operator<<
216 friend std::ostream &operator<<(std::ostream &o, Field const &field);
217 //! the field type
218 Type m_type;
219 //! the field id
220 int m_id;
221 //! the field name
222 std::string m_name;
223 //! the field format
224 MWAWCell::Format m_format;
225 //! the file position which stores the position link to record zone
226 long m_linkZone;
227 //! the block file position which stores the position of the field's record
228 BlockHeader m_recordBlock;
229
230 // formula
231
232 //! the formula
233 std::vector<MWAWCellContent::FormulaInstruction> m_formula;
234
235 //! the summary type: 1:average, 2:count, 3:total, 4:minimum, 5:maximum
236 int m_summaryType;
237 //! the summary field
238 int m_summaryField;
239
240 // sequence (or unique val)
241
242 //! true if the number is a sequence
243 bool m_isSequence;
244 //! the first number (in case of progression sequence)
245 int m_firstNumber;
246 //! the increment number (in case of progression sequence)
247 int m_incrementNumber;
248
249 //! extra data
250 std::string m_extra;
251 };
252
253 bool Field::updateCell(int row, int numRow, Cell &cell) const
254 {
255 std::vector<MWAWCellContent::FormulaInstruction> &formula=cell.m_content.m_formula;
256 if (m_type==F_Formula) {
257 if (m_formula.size()==0)
258 return false;
259 cell.m_content.m_contentType=MWAWCellContent::C_FORMULA;
260 formula=m_formula;
261 }
262 else if (m_isSequence && !cell.m_content.isValueSet()) {
263 cell.m_content.m_contentType=MWAWCellContent::C_NUMBER;
264 cell.m_content.setValue(double(m_firstNumber+row*m_incrementNumber));
265 }
266 else if (m_summaryType>0 && m_summaryType<6) {
267 cell.m_content.m_contentType=MWAWCellContent::C_FORMULA;
268 MWAWCellContent::FormulaInstruction instr;
269 instr.m_type=MWAWCellContent::FormulaInstruction::F_Function;
270 char const *wh[]= {"Average", "Count", "Sum", "Min", "Max"};
271 instr.m_content=wh[m_summaryType-1];
272 formula.push_back(instr);
273 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
274 instr.m_content="(";
275 formula.push_back(instr);
276 instr.m_type=MWAWCellContent::FormulaInstruction::F_CellList;
277 instr.m_position[0][0]=instr.m_position[1][0]=m_summaryField;
278 instr.m_position[0][1]=0;
279 instr.m_position[1][1]=numRow-1;
280 instr.m_positionRelative[0]=instr.m_positionRelative[1]=MWAWVec2b(false,false);
281 formula.push_back(instr);
282 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
283 instr.m_content=")";
284 formula.push_back(instr);
285 return true;
286 }
287 // change the reference date from 1/1/1904 to 1/1/1900
288 if (m_type==F_Date && cell.m_content.isValueSet())
289 cell.m_content.setValue(cell.m_content.m_value+1460.);
290 // and try to update the 1D formula in 2D
291 for (auto &instr : formula) {
292 if (instr.m_type==MWAWCellContent::FormulaInstruction::F_Cell)
293 instr.m_position[0][1]=row;
294 else if (instr.m_type==MWAWCellContent::FormulaInstruction::F_CellList)
295 instr.m_position[0][1]=instr.m_position[1][1]=row;
296 }
297 return true;
298 }
299
300 std::ostream &operator<<(std::ostream &o, Field const &field)
301 {
302 switch (field.m_type) {
303 case Field::F_Text:
304 o << "text,";
305 break;
306 case Field::F_Number:
307 o << "number,";
308 break;
309 case Field::F_Date:
310 o << "date,";
311 break;
312 case Field::F_Time:
313 o << "time,";
314 break;
315 case Field::F_Picture:
316 o << "picture,";
317 break;
318 case Field::F_Memo:
319 o << "memo,";
320 break;
321 case Field::F_Formula:
322 o << "formula,";
323 break;
324 case Field::F_Summary:
325 o << "summary,";
326 break;
327 case Field::F_Unknown:
328 break;
329 #if !defined(__clang__)
330 default:
331 MWAW_DEBUG_MSG(("GreatWksDBParser::Field:operator<<: find unexpected type\n"));
332 break;
333 #endif
334 }
335 if (field.m_id>=0) o << "id=" << field.m_id << ",";
336 if (!field.m_name.empty()) o << "name=" << field.m_name << ",";
337 if (field.m_linkZone>0) o << "zone[link]=" << std::hex << field.m_linkZone << std::dec << ",";
338 if (!field.m_recordBlock.isEmpty()) o << "zone[record]=" << field.m_recordBlock << ",";
339 o << field.m_extra;
340 return o;
341 }
342
343 /** the database of a GreatWksDBParser */
344 class Database
345 {
346 public:
347 //! constructor
348 Database()
349 : m_numRecords(0)
350 , m_rowList()
351 , m_fieldList()
352 , m_widthDefault(75)
353 , m_widthCols()
354 , m_heightDefault(13)
355 , m_heightRows()
356 , m_rowCellsMap()
357 , m_name("Sheet0")
358 {
359 }
360 //! add a cell data in one given position
361 bool addCell(MWAWVec2i const &pos, Cell const &cell)
362 {
363 if (pos[0]<0 || pos[0]>=int(m_fieldList.size()) || pos[1]<0) {
364 MWAW_DEBUG_MSG(("GreatWksDBParserInternal::Database::addCell: the cell position seems bad\n"));
365 return false;
366 }
367 if (m_rowCellsMap.find(pos[1])==m_rowCellsMap.end())
368 m_rowCellsMap[pos[1]]=std::vector<Cell>();
369 auto &cells=m_rowCellsMap.find(pos[1])->second;
370 if (pos[0]>=static_cast<int>(cells.size()))
371 cells.resize(m_fieldList.size());
372 cells[size_t(pos[0])]=cell;
373 return true;
374 }
375 //! returns the row size in point
376 int getRowHeight(int row) const
377 {
378 if (row>=0&&row<static_cast<int>(m_heightRows.size()))
379 return m_heightRows[size_t(row)];
380 return m_heightDefault;
381 }
382 //! convert the m_widthCols in a vector of of point size
383 std::vector<float> convertInPoint(std::vector<int> const &list) const
384 {
385 size_t numCols=m_fieldList.size();
386 std::vector<float> res;
387 res.resize(numCols);
388 for (size_t i = 0; i < numCols; i++) {
389 if (i>=list.size() || list[i] < 0) res[i] = float(m_widthDefault);
390 else res[i] = float(list[i]);
391 }
392 return res;
393 }
394 //! the number of records
395 int m_numRecords;
396 //! the list of rows data
397 std::vector<MWAWEntry> m_rowList;
398 //! the list of field
399 std::vector<Field> m_fieldList;
400 /** the default column width */
401 int m_widthDefault;
402 /** the column size in points */
403 std::vector<int> m_widthCols;
404 /** the default row height */
405 int m_heightDefault;
406 /** the row height in points */
407 std::vector<int> m_heightRows;
408 /** the map row -> list of cells */
409 std::map<int, std::vector<Cell> > m_rowCellsMap;
410 /** the database name */
411 std::string m_name;
412 protected:
413 };
414
415 ////////////////////////////////////////
416 //! Internal: the state of a GreatWksDBParser
417 struct State {
418 //! constructor
419 State()
420 : m_database()
421 , m_idZonesMap()
422 , m_blocks()
423 , m_actPage(0)
424 , m_numPages(0)
425 , m_headerBlockHeader()
426 , m_footerBlockHeader()
427 , m_headerPrint(false)
428 , m_footerPrint(false)
429 , m_headerHeight(0)
430 , m_footerHeight(0)
431 {
432 }
433 /** the database */
434 Database m_database;
435 /** a map id to zone used to stored the small zones */
436 std::map<int, MWAWEntry> m_idZonesMap;
437 /** the list of big blocks */
438 std::vector<BlockHeader> m_blocks;
439 int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */;
440
441 MWAWEntry m_headerBlockHeader /** the header entry (in v2)*/, m_footerBlockHeader/**the footer entry (in v2)*/;
442 bool m_headerPrint /** the header is printed */, m_footerPrint /* the footer is printed */;
443 int m_headerHeight /** the header height if known */,
444 m_footerHeight /** the footer height if known */;
445 };
446
447 ////////////////////////////////////////
448 //! Internal: the subdocument of a GreatWksDBParser
449 class SubDocument final : public MWAWSubDocument
450 {
451 public:
452 SubDocument(GreatWksDBParser &pars, MWAWInputStreamPtr const &input, int zoneId)
453 : MWAWSubDocument(&pars, input, MWAWEntry())
454 , m_id(zoneId) {}
455
456 //! destructor
457 ~SubDocument() final {}
458
459 //! operator!=
460 bool operator!=(MWAWSubDocument const &doc) const final;
461
462 //! the parser function
463 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
464
465 protected:
466 //! the subdocument id
467 int m_id;
468 };
469
470 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type)
471 {
472 if (!listener.get()) {
473 MWAW_DEBUG_MSG(("GreatWksDBParserInternal::SubDocument::parse: no listener\n"));
474 return;
475 }
476 if (type!=libmwaw::DOC_HEADER_FOOTER) {
477 MWAW_DEBUG_MSG(("GreatWksDBParserInternal::SubDocument::parse: unknown type\n"));
478 return;
479 }
480
481 auto *parser=dynamic_cast<GreatWksDBParser *>(m_parser);
482 if (!parser) {
483 MWAW_DEBUG_MSG(("GreatWksDBParserInternal::SubDocument::parse: no parser\n"));
484 return;
485 }
486 long pos = m_input->tell();
487 parser->sendHF(m_id);
488 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
489 }
490
491 bool SubDocument::operator!=(MWAWSubDocument const &doc) const
492 {
493 if (MWAWSubDocument::operator!=(doc)) return true;
494 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
495 if (!sDoc) return true;
496 if (m_id != sDoc->m_id) return true;
497 return false;
498 }
499 }
500
501 ////////////////////////////////////////////////////////////
502 // constructor/destructor, ...
503 ////////////////////////////////////////////////////////////
504 GreatWksDBParser::GreatWksDBParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
505 : MWAWSpreadsheetParser(input, rsrcParser, header)
506 , m_state()
507 , m_document()
508 {
509 init();
510 }
511
512 GreatWksDBParser::~GreatWksDBParser()
513 {
514 }
515
516 void GreatWksDBParser::init()
517 {
518 resetSpreadsheetListener();
519 setAsciiName("main-1");
520
521 m_state.reset(new GreatWksDBParserInternal::State);
522
523 // reduce the margin (in case, the page is not defined)
524 getPageSpan().setMargins(0.1);
525
526 m_document.reset(new GreatWksDocument(*this));
527 }
528
529 ////////////////////////////////////////////////////////////
530 // interface with the text parser
531 ////////////////////////////////////////////////////////////
532 bool GreatWksDBParser::sendHF(int id)
533 {
534 return m_document->getTextParser()->sendTextbox(id==0 ? m_state->m_headerBlockHeader : m_state->m_footerBlockHeader);
535 }
536
537 ////////////////////////////////////////////////////////////
538 // the parser
539 ////////////////////////////////////////////////////////////
540 void GreatWksDBParser::parse(librevenge::RVNGSpreadsheetInterface *docInterface)
541 {
542 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
543 bool ok = false;
544 try {
545 // create the asciiFile
546 ascii().setStream(getInput());
547 ascii().open(asciiName());
548
549 checkHeader(nullptr);
550 ok = createZones();
551 if (ok) {
552 createDocument(docInterface);
553 sendDatabase();
554 }
555 ascii().reset();
556 }
557 catch (...) {
558 MWAW_DEBUG_MSG(("GreatWksDBParser::parse: exception catched when parsing\n"));
559 ok = false;
560 }
561
562 resetSpreadsheetListener();
563 if (!ok) throw(libmwaw::ParseException());
564 }
565
566 ////////////////////////////////////////////////////////////
567 // create the document
568 ////////////////////////////////////////////////////////////
569 void GreatWksDBParser::createDocument(librevenge::RVNGSpreadsheetInterface *documentInterface)
570 {
571 if (!documentInterface) return;
572 if (getSpreadsheetListener()) {
573 MWAW_DEBUG_MSG(("GreatWksDBParser::createDocument: listener already exist\n"));
574 return;
575 }
576
577 // update the page
578 m_state->m_actPage = 0;
579
580 // create the page list
581 int numPages = 1;
582 if (m_document->getGraphParser()->numPages() > numPages)
583 numPages = m_document->getGraphParser()->numPages();
584 if (m_document->getTextParser()->numPages() > numPages)
585 numPages = m_document->getTextParser()->numPages();
586 m_state->m_numPages = numPages;
587
588 MWAWPageSpan ps(getPageSpan());
589 std::vector<MWAWPageSpan> pageList;
590 if (m_state->m_headerBlockHeader.valid()) {
591 MWAWHeaderFooter header(MWAWHeaderFooter::HEADER, MWAWHeaderFooter::ALL);
592 header.m_subDocument.reset(new GreatWksDBParserInternal::SubDocument(*this, getInput(), 0));
593 ps.setHeaderFooter(header);
594 }
595 if (m_state->m_footerBlockHeader.valid()) {
596 MWAWHeaderFooter footer(MWAWHeaderFooter::FOOTER, MWAWHeaderFooter::ALL);
597 footer.m_subDocument.reset(new GreatWksDBParserInternal::SubDocument(*this, getInput(), 1));
598 ps.setHeaderFooter(footer);
599 }
600 ps.setPageSpan(numPages);
601 pageList.push_back(ps);
602 MWAWSpreadsheetListenerPtr listen(new MWAWSpreadsheetListener(*getParserState(), pageList, documentInterface));
603 setSpreadsheetListener(listen);
604 listen->startDocument();
605 }
606
607 ////////////////////////////////////////////////////////////
608 //
609 // Intermediate level
610 //
611 ////////////////////////////////////////////////////////////
612 bool GreatWksDBParser::createZones()
613 {
614 m_document->readRSRCZones();
615
616 if (!readHeader()) return false;
617
618 MWAWInputStreamPtr input = getInput();
619 libmwaw::DebugStream f;
620
621 for (size_t i=0; i< m_state->m_blocks.size() ; ++i) {
622 auto &header = m_state->m_blocks[i];
623 if (header.isEmpty()) continue;
624 auto block=createBlock(header);
625 if (block) {
626 bool ok=false;
627 switch (i) {
628 case 0: // unknown maybe some flag
629 ok = readFreeList(*block);
630 break;
631 case 1: // a list of record<->id
632 ok = readRowLinks(*block);
633 break;
634 case 2: // a list of 0<->id
635 ok = readBlockHeader2(*block);
636 break;
637 case 3: // the record list
638 ok = readRecordList(*block);
639 break;
640 default:
641 break;
642 }
643 if (ok)
644 continue;
645 }
646 f.str("");
647 f << "Entries(" << header.m_name << ")[" << header << "]:";
648 ascii().addPos(input->tell());
649 ascii().addNote(f.str().c_str());
650 if (!header.isComplex()) continue;
651 for (int j=1; j<3; ++j) {
652 if (!header.m_ptr[j]) continue;
653 ascii().addPos(header.m_ptr[j]);
654 ascii().addNote(f.str().c_str());
655 }
656 }
657
658 for (auto const &it : m_state->m_idZonesMap) {
659 MWAWEntry const &entry=it.second;
660 if (!entry.valid() || entry.isParsed()) continue;
661 bool ok=false;
662 switch (entry.id()) {
663 // zones which will be parsed in readDatabase
664 case 1: // the list of fields
665 case 10: // the link between field and a zone record
666 case 13: // a version 2 zone which seems to contains added data for each fields
667 ok=true;
668 break;
669 case 2:
670 ok=readFormLinks(it.second);
671 break;
672 case 5: // one time with list=[19, 20], list of computed fields ?
673 case 6: // one time with list=[21, 22], list of summary fields ?
674 case 14: { // only in v2, probably linked to Zone2 zones, find [1,1,1] or [2,2]
675 std::vector<int> list;
676 ok=readIntList(it.second, list);
677 if (!ok) break;
678 f.str("");
679 f << "Entries(" << entry.name() << "):";
680 if (list.size()) {
681 f << "lists=[";
682 for (int i : list) f << i << ",";
683 f << "],";
684 }
685 ascii().addPos(entry.begin());
686 ascii().addNote(f.str().c_str());
687 break;
688 }
689 case 7: // always find with size=0
690 if (entry.length()!=6) break;
691 ok=true;
692 f.str("");
693 f << "Entries(" << entry.name() << "):";
694 ascii().addPos(entry.begin());
695 ascii().addNote(f.str().c_str());
696 ascii().addPos(entry.end());
697 ascii().addNote("_");
698 break;
699 case 12:
700 ok=readZone12(entry);
701 break;
702 // zone3: never find with data, dSz=3C in v1 and dSz=3e in v2
703 // zone4: never find with data, dSz=112, seems one time in v2
704 // zone8: never seems
705 default:
706 ok=readSmallZone(entry);
707 break;
708 }
709 if (ok || entry.isParsed()) continue;
710 f.str("");
711 f << "Entries(" << entry.name() << "):#";
712 ascii().addPos(entry.begin());
713 ascii().addNote(f.str().c_str());
714 ascii().addPos(entry.end());
715 ascii().addNote("_");
716 }
717
718 if (!readDatabase())
719 return false;
720
721 return true;
722 }
723
724 ////////////////////////////////////////////////////////////
725 // read a database
726 ////////////////////////////////////////////////////////////
727 bool GreatWksDBParser::readDatabase()
728 {
729 MWAWInputStreamPtr input = getInput();
730 auto it=m_state->m_idZonesMap.find(1);
731 if (it==m_state->m_idZonesMap.end() || !readFields(it->second)) {
732 MWAW_DEBUG_MSG(("GreatWksDBParser::readDatabase: can not find the list of fields\n"));
733 return false;
734 }
735 if (version()==2) {
736 // zone13: a v2 zone with dSz=46, content seems constant in a file
737 it=m_state->m_idZonesMap.find(13);
738 if (it!=m_state->m_idZonesMap.end()) {
739 if (!readFieldAuxis(it->second))
740 readSmallZone(it->second);
741 }
742 }
743 auto &database=m_state->m_database;
744 /* First read the fields' records then the rows' records. As the
745 rows' records contain more information than the fields' records,
746 there will replace the field data if we success to read them
747 (and if not, we will keep the fields' records).
748 */
749 for (auto &field : database.m_fieldList) {
750 if (!field.m_linkZone) continue;
751 readFieldLinks(field);
752 }
753 for (auto const &row : database.m_rowList)
754 readRowRecords(row);
755 for (auto &field : database.m_fieldList) {
756 if (field.m_recordBlock.isEmpty()) continue;
757 readFieldRecords(field);
758 }
759 if (!database.m_rowCellsMap.empty())
760 return true;
761 // let check if we can reconstruct something
762 for (auto &field : database.m_fieldList) {
763 if (field.m_recordBlock.isEmpty()) continue;
764 if (field.m_isSequence) return true;
765 if (field.m_type==GreatWksDBParserInternal::Field::F_Summary) return true;
766 }
767 MWAW_DEBUG_MSG(("GreatWksDBParser::readDatabase: can not find any cellule\n"));
768 return false;
769 }
770
771 ////////////////////////////////////////////////////////////
772 // read the file header
773 ////////////////////////////////////////////////////////////
774 bool GreatWksDBParser::readHeader()
775 {
776 MWAWInputStreamPtr input = getInput();
777 if (!input->checkPosition(0x200))
778 return false;
779 int const vers=version();
780 libmwaw::DebugStream f;
781
782 f << "Entries(HeaderZone):";
783 long pos=16;
784 input->seek(pos, librevenge::RVNG_SEEK_SET);
785 for (int i=0; i<3; ++i) {
786 GreatWksDBParserInternal::BlockHeader block;
787 if (!readBlockHeader(block)) {
788 MWAW_DEBUG_MSG(("GreatWksDBParser::readHeader: can not read a entry\n"));
789 f << "###";
790 ascii().addPos(pos);
791 ascii().addNote(f.str().c_str());
792 return false;
793 }
794 static char const *wh[]= {"Free", "RecLink", "Block2"};
795 block.m_name=wh[i];
796 m_state->m_blocks.push_back(block);
797 f << block << ",";
798 for (int j=0; j<4; ++j) {
799 auto val=static_cast<int>(input->readULong(2));
800 if (val)
801 f << "f" << j << "[" << i << "]=" << val << ",";
802 }
803 }
804 // an odd entry block (only 2 ptr), does this means that this zone has a size less than 512?
805 GreatWksDBParserInternal::BlockHeader block;
806 for (int i=0; i<2; ++i) block.m_ptr[i]=long(input->readULong(4));
807 block.m_ptr[2]=block.m_ptr[1];
808 block.m_name="RecList";
809 m_state->m_blocks.push_back(block);
810 f << block << ",";
811
812 auto val=static_cast<int>(input->readLong(2)); // always 0
813 if (val) f << "f4=" << val << ",";
814 auto numRecords=static_cast<int>(input->readLong(2));
815 m_state->m_database.m_numRecords=numRecords;
816 f << "num[records]=" << numRecords << ",";
817 for (int i=0; i<16; ++i) { // find h1=h9=h11=h13=numRecords, h3=h7=1|11, h14=2, h15=1
818 val=static_cast<int>(input->readLong(2));
819 if (i==1 || i==9 || i==11 || i==13) {
820 if (val==numRecords) continue;
821 if (i==9) f << "act[record]=" << val << ",";
822 else f << "h" << i << "=" << val << ",";
823 }
824 else if (val) f << "h" << i << "=" << val << ",";
825 }
826 ascii().addPos(pos);
827 ascii().addNote(f.str().c_str());
828
829 pos=input->tell();
830 f.str("");
831 f << "Entries(Zones):";
832 std::vector<MWAWEntry> zones;
833 for (int i=0; i<9; ++i) {
834 val=static_cast<int>(input->readLong(2));
835 if (val) f << "f" << i << "=" << val << ",";
836 auto ptr=long(input->readULong(2));
837 MWAWEntry entry;
838 entry.setBegin(ptr);
839 zones.push_back(entry);
840 if (!ptr) continue;
841 // checkme: does the zone type can be deduced from i?
842 f << "Zone" << i << "A=" << std::hex << ptr << std::dec << ",";
843 }
844 if (vers==1) {
845 for (int i=0; i<40; ++i) {
846 val=static_cast<int>(input->readLong(2));
847 if (val!=-1) f << "g" << i << "=" << val << ",";
848 }
849 }
850 else {
851 // no sure what is the maximal number of potential added zones
852 for (int i=0; i<20; ++i) {
853 auto ptr=long(input->readULong(2));
854 val=static_cast<int>(input->readLong(2));
855 if (val) f << "g" << i << "=" << val << ",";
856 MWAWEntry entry;
857 entry.setBegin(ptr);
858 zones.push_back(entry);
859 if (!ptr || ptr==0xFFFF) continue;
860 f << "Zone" << 9+i << "A=" << std::hex << ptr << std::dec << ",";
861 }
862 }
863 ascii().addPos(pos);
864 ascii().addNote(f.str().c_str());
865
866 // from here, probably only junk in v1 and list of 0 + unkn number in v2
867 ascii().addPos(input->tell());
868 ascii().addNote("HeaderZone:end");
869
870 for (auto &entry : zones) {
871 if (!entry.begin() || entry.begin()==0xFFFF) continue;
872 if (!checkSmallZone(entry)) {
873 MWAW_DEBUG_MSG(("GreatWksDBParser::readHeader: find a bad zone %d\n", static_cast<int>(entry.id())));
874 entry.setLength(0);
875 continue;
876 }
877 int const id=entry.id();
878 if (m_state->m_idZonesMap.find(id)==m_state->m_idZonesMap.end())
879 m_state->m_idZonesMap[id]=entry;
880 else {
881 MWAW_DEBUG_MSG(("GreatWksDBParser::readHeader: the zone entry %d already exists\n", id));
882 f.str("");
883 f << "Entries(" << entry.name() << "):###";
884 ascii().addPos(entry.begin());
885 ascii().addNote(f.str().c_str());
886 ascii().addPos(entry.end());
887 ascii().addNote("_");
888 }
889 }
890
891 return true;
892 }
893
894 ////////////////////////////////////////////////////////////
895 //
896 // Low level
897 //
898 ////////////////////////////////////////////////////////////
899
900 ////////////////////////////////////////////////////////////
901 // block gestion
902 ////////////////////////////////////////////////////////////
903 bool GreatWksDBParser::readBlockHeader(GreatWksDBParserInternal::BlockHeader &entry)
904 {
905 MWAWInputStreamPtr input = getInput();
906 if (!input->checkPosition(input->tell()+12)) return false;
907 for (auto &ptr : entry.m_ptr) ptr=long(input->readULong(4));
908 return true;
909 }
910
911 std::shared_ptr<GreatWksDBParserInternal::Block> GreatWksDBParser::createBlock(GreatWksDBParserInternal::BlockHeader &entry)
912 {
913 std::shared_ptr<GreatWksDBParserInternal::Block> res;
914
915 MWAWInputStreamPtr input = getInput();
916 long pos=entry.m_ptr[0];
917 if (!pos || !input->checkPosition(pos+10)) return res;
918
919 libmwaw::DebugStream f;
920 std::set<long> seens;
921 while (pos) {
922 if (!input->checkPosition(pos+256) || seens.find(pos)!=seens.end()) {
923 MWAW_DEBUG_MSG(("GreatWksDBParser::createBlock: find an old position\n"));
924 ascii().addPos(pos);
925 ascii().addNote("Entries(BlockHeader):###");
926 return res;
927 }
928 seens.insert(pos);
929 f.str("");
930 f << "Entries(BlockHeader):";
931 input->seek(pos, librevenge::RVNG_SEEK_SET);
932 auto type=static_cast<int>(input->readULong(1));
933 f << "type=" << std::hex << type << std::dec << ",";
934 if ((type&0xF0) != 0x40) {
935 MWAW_DEBUG_MSG(("GreatWksDBParser::createBlock: find old block type\n"));
936 f << "####";
937 ascii().addPos(pos);
938 ascii().addNote(f.str().c_str());
939 return res;
940 }
941 auto val=static_cast<int>(input->readULong(1));
942 if (val!=0x7F) // find fl=0 with type=44
943 f << "fl=" << std::hex << val << std::dec << ",";
944 GreatWksDBParserInternal::Block::Zone zone;
945 zone.m_N = static_cast<int>(input->readULong(2));
946 if (zone.m_N)
947 f << "N=" << zone.m_N << ",";
948 if ((type&0x4)==0)
949 zone.m_dataSize = static_cast<int>(input->readULong(2));
950 if (zone.m_dataSize)
951 f << "dataSize=" << zone.m_dataSize << ",";
952 if (type==0x41 || type==0x44) { // link to type or to flag
953 val=static_cast<int>(input->readULong(4));
954 if (val) f << "prev=" << std::hex << val << std::dec << ",";
955 }
956 long next=static_cast<int>(input->readULong(4));
957 if (next) {
958 f << "next=" << std::hex << next << std::dec << ",";
959 if ((next&0x1FF)) {
960 MWAW_DEBUG_MSG(("GreatWksDBParser::createBlock: the next zone seems bad(ignored it)\n"));
961 f << "###";
962 next=0;
963 }
964 }
965
966 zone.m_ptr=input->tell();
967 if (!res) res.reset(new GreatWksDBParserInternal::Block(entry));
968 switch (type) {
969 case 0x40: // a root with no data?
970 ascii().addPos(input->tell());
971 ascii().addNote("_");
972 break;
973 case 0x41: // ok
974 case 0x44: // no dataSize
975 res->m_zoneList.push_back(zone);
976 break;
977 default:
978 res->m_zoneList.push_back(zone);
979 MWAW_DEBUG_MSG(("GreatWksDBParser::createBlock: find some unexpected type %d, try to continue\n", type));
980 f << "###";
981 // let's add this zone, but ignored potential follower
982 next=0;
983 break;
984 }
985 ascii().addPos(pos);
986 ascii().addNote(f.str().c_str());
987 pos=next;
988 }
989 return res;
990 }
991
992 ////////////////////////////////////////////////////////////
993 // functions to read the known block structure
994 ////////////////////////////////////////////////////////////
995 bool GreatWksDBParser::readRecordList(GreatWksDBParserInternal::Block const &block)
996 {
997 MWAWInputStreamPtr input = getInput();
998 auto const &header=block.m_header;
999 libmwaw::DebugStream f;
1000 for (size_t z=0; z<block.getNumZones(); ++z) {
1001 auto const &zone=block.getZone(z);
1002 long pos=zone.m_ptr;
1003 if (!pos || !input->checkPosition(pos+zone.m_N*4)) {
1004 MWAW_DEBUG_MSG(("GreatWksDBParser::readRecordList: the zone seems too short\n"));
1005 f.str("");
1006 f << "Entries(" << header.m_name << ")[" << header << "]:###";
1007 ascii().addPos(pos);
1008 ascii().addNote(f.str().c_str());
1009 continue;
1010 }
1011 input->seek(pos, librevenge::RVNG_SEEK_SET);
1012 f.str("");
1013 f << "Entries(" << header.m_name << "):list=[";
1014 for (int i=0; i< zone.m_N; ++i) f << input->readLong(4) << ",";
1015 f << "]";
1016 ascii().addPos(pos);
1017 ascii().addNote(f.str().c_str());
1018 ascii().addPos(input->tell());
1019 ascii().addNote("_");
1020 }
1021 return true;
1022 }
1023
1024 bool GreatWksDBParser::readFreeList(GreatWksDBParserInternal::Block &block)
1025 {
1026 MWAWInputStreamPtr input = getInput();
1027 auto const &header=block.m_header;
1028 libmwaw::DebugStream f;
1029 for (size_t z=0; z<block.getNumZones(); ++z) {
1030 auto const &zone=block.getZone(z);
1031 long pos=zone.m_ptr;
1032 if (!pos || !input->checkPosition(pos+zone.m_N*8)) {
1033 MWAW_DEBUG_MSG(("GreatWksDBParser::readFreeList: the zone seems too short\n"));
1034 f.str("");
1035 f << "Entries(" << header.m_name << ")[" << header << "]:###";
1036 ascii().addPos(pos);
1037 ascii().addNote(f.str().c_str());
1038 continue;
1039 }
1040 input->seek(pos, librevenge::RVNG_SEEK_SET);
1041 f.str("");
1042 f << "Entries(" << header.m_name << "):zones=[";
1043 for (int i=0; i<zone.m_N; ++i) {
1044 /* a ptr followed by 400000000|size or the oposite
1045 normally the content of a free block seems to be 0x8000, dataSz
1046 excepted on time where it is 8, dataSz
1047 */
1048 unsigned long values[2];
1049 for (auto &value : values) value=static_cast<unsigned long>(input->readULong(4));
1050 if ((values[0]&0xFF000000)==0x40000000)
1051 std::swap(values[0],values[1]);
1052 if ((values[0]&0xFF000000)==0 && (values[1]&0xFF000000)==0x40000000 &&
1053 input->checkPosition(long(values[0]+(values[1]&0xFFFFFF)))) {
1054 ascii().addPos(long(values[0]));
1055 ascii().addNote("Free");
1056 ascii().addPos(long(values[0]+(values[1]&0xFFFFFF)));
1057 ascii().addNote("_");
1058 f << std::hex << values[0] << ":" << (values[1]&0xFFFFFF) << std::dec << ",";
1059 }
1060 else {
1061 MWAW_DEBUG_MSG(("GreatWksDBParser::readFreeList: find an odd free zone\n"));
1062 f << "###" << std::hex << values[0] << ":" << values[1] << std::dec << ",";
1063 }
1064 }
1065 f << "]";
1066 ascii().addPos(pos);
1067 ascii().addNote(f.str().c_str());
1068 ascii().addPos(input->tell());
1069 ascii().addNote("_");
1070 }
1071 return true;
1072 }
1073
1074 bool GreatWksDBParser::readRowLinks(GreatWksDBParserInternal::Block &block)
1075 {
1076 MWAWInputStreamPtr input = getInput();
1077 auto const &header=block.m_header;
1078 auto &database=m_state->m_database;
1079 libmwaw::DebugStream f;
1080 for (size_t z=0; z<block.getNumZones(); ++z) {
1081 auto const &zone=block.getZone(z);
1082 long pos=zone.m_ptr;
1083 if (!pos || !input->checkPosition(pos+zone.m_N*8)) {
1084 MWAW_DEBUG_MSG(("GreatWksDBParser::readRowLinks: the zone seems too short\n"));
1085 f.str("");
1086 f << "Entries(" << header.m_name << ")[" << header << "]:###";
1087 ascii().addPos(pos);
1088 ascii().addNote(f.str().c_str());
1089 continue;
1090 }
1091 input->seek(pos, librevenge::RVNG_SEEK_SET);
1092 f.str("");
1093 f << "Entries(" << header.m_name << "):ptrs=[";
1094 for (int i=0; i<zone.m_N; ++i) {
1095 auto ptr=long(input->readULong(4));
1096 auto id=static_cast<int>(input->readLong(4));
1097 if (ptr) {
1098 MWAWEntry entry;
1099 entry.setBegin(ptr);
1100 entry.setId(id);
1101 database.m_rowList.push_back(entry);
1102 }
1103 else {
1104 MWAW_DEBUG_MSG(("GreatWksDBParser::readRowLinks: find an empty record zone\n"));
1105 f << "###";
1106 }
1107 f << std::hex << ptr << std::dec << ":" << id << ",";
1108 }
1109 f << "]";
1110 ascii().addPos(pos);
1111 ascii().addNote(f.str().c_str());
1112 ascii().addPos(input->tell());
1113 ascii().addNote("_");
1114 }
1115 return true;
1116 }
1117
1118 bool GreatWksDBParser::readBlockHeader2(GreatWksDBParserInternal::Block const &block)
1119 {
1120 MWAWInputStreamPtr input = getInput();
1121 auto const &header=block.m_header;
1122 libmwaw::DebugStream f;
1123 for (size_t z=0; z<block.getNumZones(); ++z) {
1124 auto const &zone=block.getZone(z);
1125 long pos=zone.m_ptr;
1126 if (!pos || !input->checkPosition(pos+zone.m_N*8)) {
1127 MWAW_DEBUG_MSG(("GreatWksDBParser::readBlockHeader2: the zone seems too short\n"));
1128 f.str("");
1129 f << "Entries(" << header.m_name << ")[" << header << "]:###";
1130 ascii().addPos(pos);
1131 ascii().addNote(f.str().c_str());
1132 continue;
1133 }
1134 input->seek(pos, librevenge::RVNG_SEEK_SET);
1135 f.str("");
1136 f << "Entries(" << header.m_name << "):unkn=[";
1137 for (int i=0; i<zone.m_N; ++i) {
1138 f << input->readULong(4) << ":"; // always 0?
1139 f << input->readLong(4) << ","; // ids
1140 }
1141 f << "]";
1142 ascii().addPos(pos);
1143 ascii().addNote(f.str().c_str());
1144 ascii().addPos(input->tell());
1145 ascii().addNote("_");
1146 }
1147 return true;
1148 }
1149
1150 bool GreatWksDBParser::readBlock(GreatWksDBParserInternal::Block const &block, int fieldSize)
1151 {
1152 MWAWInputStreamPtr input = getInput();
1153 auto const &header=block.m_header;
1154 libmwaw::DebugStream f;
1155 for (size_t z=0; z<block.getNumZones(); ++z) {
1156 auto const &zone=block.getZone(z);
1157 long pos=zone.m_ptr;
1158 if (fieldSize<=0 || !pos || !input->checkPosition(pos+zone.m_N*fieldSize)) {
1159 MWAW_DEBUG_MSG(("GreatWksDBParser::readBlock: the fieldSize seems bad\n"));
1160 f.str("");
1161 f << "Entries(" << header.m_name << ")[" << header << "]:###";
1162 ascii().addPos(pos);
1163 ascii().addNote(f.str().c_str());
1164 continue;
1165 }
1166 input->seek(pos, librevenge::RVNG_SEEK_SET);
1167 for (int i=0; i<zone.m_N; ++i) {
1168 pos=input->tell();
1169 f.str("");
1170 f << "Entries(" << header.m_name << ")[" << i << "]:";
1171 input->seek(pos+fieldSize, librevenge::RVNG_SEEK_SET);
1172 ascii().addPos(pos);
1173 ascii().addNote(f.str().c_str());
1174 }
1175 ascii().addPos(input->tell());
1176 ascii().addNote("_");
1177 }
1178 return true;
1179 }
1180
1181 ////////////////////////////////////////////////////////////
1182 // check if entry.begin() corresponds or not to a small zone
1183 ////////////////////////////////////////////////////////////
1184 bool GreatWksDBParser::checkSmallZone(MWAWEntry &entry)
1185 {
1186 if (entry.begin()<=0) return false;
1187 MWAWInputStreamPtr input = getInput();
1188 if (!input->checkPosition(entry.begin()+6)) return false;
1189
1190 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1191 auto id=static_cast<int>(input->readLong(2));
1192 entry.setId(id);
1193 /* the zone with id=10 is in fact a list of zone with id=10, so
1194 its length will be underestimate */
1195 entry.setLength(6+long(input->readULong(4)));
1196 if (id>=0 && id<15) {
1197 static char const *names[]= {
1198 "Zone0A", "Field", "FrmLink", "Zone3A", "Zone4A", "ListFrmula", "ListSummary", "Zone7A",
1199 "Zone8A", "Form", "FldLink", "Zone11A", "Zone12A", "FldAuxi", "Zone14A"
1200 };
1201 entry.setName(names[id]);
1202 }
1203 else {
1204 std::stringstream s;
1205 s << "Zone" << id << "A";
1206 entry.setName(s.str());
1207 }
1208 return input->checkPosition(entry.begin()+6);
1209 }
1210
1211 ////////////////////////////////////////////////////////////
1212 // read the row zone
1213 ////////////////////////////////////////////////////////////
1214 bool GreatWksDBParser::readRowRecords(MWAWEntry const &entry)
1215 {
1216 MWAWInputStreamPtr input = getInput();
1217 long pos=entry.begin();
1218 if (!pos || !input->checkPosition(pos+6)) {
1219 MWAW_DEBUG_MSG(("GreatWksDBParser::readRowRecords: can not find a row position\n"));
1220 if (pos) {
1221 ascii().addPos(pos);
1222 ascii().addNote("Entries(RowRec):###");
1223 }
1224 return false;
1225 }
1226
1227 input->seek(pos, librevenge::RVNG_SEEK_SET);
1228 libmwaw::DebugStream f;
1229 f << "Entries(RowRec):";
1230 // probably type=0 followed by ?
1231 auto val=static_cast<int>(input->readULong(2));
1232 if (val!=0xFF) f << "f0=" << std::hex << val << std::dec << ",";
1233 auto dSz=long(input->readULong(4));
1234 long endPos=pos+6+dSz;
1235 if (dSz<0 || !input->checkPosition(endPos)) {
1236 MWAW_DEBUG_MSG(("GreatWksDBParser::readRowRecords: the size seems bad\n"));
1237 f << "###";
1238 ascii().addPos(pos);
1239 ascii().addNote(f.str().c_str());
1240 return false;
1241 }
1242 ascii().addPos(pos);
1243 ascii().addNote(f.str().c_str());
1244 ascii().addPos(endPos);
1245 ascii().addNote("_");
1246
1247 auto &database=m_state->m_database;
1248 for (size_t fl=0; fl<database.m_fieldList.size(); ++fl) {
1249 pos=input->tell();
1250 f.str("");
1251 f << "RowRec-" << fl << ":";
1252 if (pos >= endPos) {
1253 MWAW_DEBUG_MSG(("GreatWksDBParser::readRowRecords: actual pos seems bad\n"));
1254 f << "###";
1255 ascii().addPos(pos);
1256 ascii().addNote(f.str().c_str());
1257 return true;
1258 }
1259 auto const &field=database.m_fieldList[fl];
1260 bool ok=true;
1261
1262 GreatWksDBParserInternal::Cell cell;
1263 cell.setFormat(field.m_format);
1264 switch (field.m_type) {
1265 case GreatWksDBParserInternal::Field::F_Text: {
1266 auto sSz=static_cast<int>(input->readULong(1));
1267 if (pos+1+sSz>endPos) {
1268 MWAW_DEBUG_MSG(("GreatWksDBParser::readRowRecords: the string length seems bad\n"));
1269 f << "###";
1270 ascii().addPos(pos);
1271 ascii().addNote(f.str().c_str());
1272 }
1273 cell.m_content.m_contentType=MWAWCellContent::C_TEXT;
1274 cell.m_content.m_textEntry.setBegin(input->tell());
1275 cell.m_content.m_textEntry.setLength(sSz);
1276 database.addCell(MWAWVec2i(field.m_id, entry.id()), cell);
1277
1278 std::string text("");
1279 for (int i=0; i<sSz; ++i) text+=char(input->readULong(1));
1280 f << text << ",";
1281 // realign to a multiple of 2
1282 if ((sSz%2)==0) input->seek(1,librevenge::RVNG_SEEK_CUR);
1283 break;
1284 }
1285 case GreatWksDBParserInternal::Field::F_Summary: {
1286 if (pos+10>endPos) {
1287 MWAW_DEBUG_MSG(("GreatWksDBParser::readRowRecords: can not read a summary zone\n"));
1288 f << "###";
1289 ascii().addPos(pos);
1290 ascii().addNote(f.str().c_str());
1291 return false;
1292 }
1293 // formula will be will later
1294 cell.m_content.m_contentType=MWAWCellContent::C_FORMULA;
1295 database.addCell(MWAWVec2i(field.m_id, entry.id()), cell);
1296 f << "summary,";
1297 input->seek(pos+10, librevenge::RVNG_SEEK_SET);
1298 break;
1299 }
1300 case GreatWksDBParserInternal::Field::F_Number:
1301 case GreatWksDBParserInternal::Field::F_Date:
1302 case GreatWksDBParserInternal::Field::F_Time: {
1303 if (pos+10>endPos) {
1304 MWAW_DEBUG_MSG(("GreatWksDBParser::readRowRecords: can not read a number zone\n"));
1305 f << "###";
1306 ascii().addPos(pos);
1307 ascii().addNote(f.str().c_str());
1308 return false;
1309 }
1310 double value;
1311 bool isNan;
1312 /*fixme: this does not work for time's field as value is a very
1313 big number and the double precision is too small, ie. we do not
1314 retrieve the decimal part -> time is always 0:0:0 */
1315 if (!input->readDouble10(value, isNan)) {
1316 static bool first=true;
1317 if (first) {
1318 // can be normal, ie. 7fff403200000000000000 means no value which generates an error
1319 MWAW_DEBUG_MSG(("GreatWksDBParser::readRowRecords: can not read a number, maybe a undef number\n"));
1320 first=false;
1321 }
1322 f << "#";
1323 }
1324 else {
1325 cell.m_content.m_contentType=MWAWCellContent::C_NUMBER;
1326 cell.m_content.setValue(value);
1327 database.addCell(MWAWVec2i(field.m_id, entry.id()), cell);
1328 f << value;
1329 }
1330 input->seek(pos+10, librevenge::RVNG_SEEK_SET);
1331 break;
1332 }
1333 case GreatWksDBParserInternal::Field::F_Picture: {
1334 long pSz=static_cast<int>(input->readULong(4));
1335 if (pos+4+pSz>endPos) {
1336 MWAW_DEBUG_MSG(("GreatWksDBParser::readRowRecords: the picture length seems bad\n"));
1337 f << "###";
1338 ascii().addPos(pos);
1339 ascii().addNote(f.str().c_str());
1340 }
1341 if (pSz) {
1342 cell.m_pictEntry.setBegin(pos+4);
1343 cell.m_pictEntry.setLength(pSz);
1344 database.addCell(MWAWVec2i(field.m_id, entry.id()), cell);
1345 #ifdef DEBUG_WITH_FILES
1346 ascii().skipZone(pos+4,pos+4+pSz-1);
1347 librevenge::RVNGBinaryData file;
1348 input->seek(pos+4,librevenge::RVNG_SEEK_SET);
1349 input->readDataBlock(pSz, file);
1350
1351 static int volatile pictName = 0;
1352 libmwaw::DebugStream f2;
1353 f2 << "DATA-" << ++pictName << ".pct";
1354 libmwaw::Debug::dumpFile(file, f2.str().c_str());
1355 #endif
1356 }
1357 input->seek(pos+4+pSz, librevenge::RVNG_SEEK_SET);
1358 break;
1359 }
1360 case GreatWksDBParserInternal::Field::F_Memo: {
1361 long mSz=static_cast<int>(input->readULong(4));
1362 if (pos+4+mSz>endPos) {
1363 MWAW_DEBUG_MSG(("GreatWksDBParser::readRowRecords: the memo length seems bad\n"));
1364 f << "###";
1365 ascii().addPos(pos);
1366 ascii().addNote(f.str().c_str());
1367 }
1368 cell.m_content.m_contentType=MWAWCellContent::C_TEXT;
1369 cell.m_content.m_textEntry.setBegin(input->tell());
1370 cell.m_content.m_textEntry.setLength(mSz);
1371 database.addCell(MWAWVec2i(field.m_id, entry.id()), cell);
1372 // probably a simple textbox ( see sendSimpleTextbox )
1373 if (mSz) f << "memo,";
1374 input->seek(pos+4+mSz, librevenge::RVNG_SEEK_SET);
1375 break;
1376 }
1377 case GreatWksDBParserInternal::Field::F_Formula: {
1378 std::string extra("");
1379 ok=readFormulaResult(endPos, cell, extra);
1380 if (ok)
1381 database.addCell(MWAWVec2i(field.m_id, entry.id()), cell);
1382 f << extra;
1383 break;
1384 }
1385 case GreatWksDBParserInternal::Field::F_Unknown:
1386 #if !defined(__clang__)
1387 default:
1388 #endif
1389 ok=false;
1390 break;
1391 }
1392 if (!ok)
1393 break;
1394 ascii().addPos(pos);
1395 ascii().addNote(f.str().c_str());
1396 }
1397 pos=input->tell();
1398 if (pos+2==endPos) {
1399 // seems ok to find 0 here
1400 val=static_cast<int>(input->readLong(2));
1401 if (val) {
1402 f.str("");
1403 f << "RowRec[end]:" << val << ",";
1404 ascii().addPos(pos);
1405 ascii().addNote(f.str().c_str());
1406 }
1407 else {
1408 ascii().addPos(pos);
1409 ascii().addNote("_");
1410 }
1411 }
1412 else if (pos!=endPos) {
1413 MWAW_DEBUG_MSG(("GreatWksDBParser::readRowRecords: find some extra data\n"));
1414 ascii().addPos(pos);
1415 ascii().addNote("RowRec[end]:###");
1416 }
1417
1418 return true;
1419 }
1420
1421 bool GreatWksDBParser::readFormula(long endPos, std::vector<MWAWCellContent::FormulaInstruction> &formula)
1422 {
1423 formula.resize(0);
1424 MWAWInputStreamPtr input = getInput();
1425 long pos=input->tell();
1426 auto dSz=long(input->readULong(2));
1427 long endHeader=pos+2+dSz;
1428 if (dSz<2 || endHeader > endPos) {
1429 MWAW_DEBUG_MSG(("GreatWksDBParser::readFormula: can not read a formula\n"));
1430 return false;
1431 }
1432 libmwaw::DebugStream f;
1433 f << "Entries(Formula):";
1434 std::string error("");
1435 if (!m_document->readFormula(MWAWVec2i(0,0),endHeader,formula, error))
1436 f << "###";
1437 f << "[";
1438 for (const auto &l : formula)
1439 f << l;
1440 f << "]" << error << ",";
1441
1442 input->seek(endHeader, librevenge::RVNG_SEEK_SET);
1443 auto N=static_cast<int>(input->readULong(2));
1444 if (endHeader+2*(N+1)>endPos) {
1445 MWAW_DEBUG_MSG(("GreatWksDBParser::readFormula: can not read a formula field(II)\n"));
1446 return false;
1447 }
1448 auto val=static_cast<int>(input->readLong(2)); // Here I find always 2, the field size ?
1449 if (val!=2) f << "g0=" << val << ",";
1450 std::vector<int> varList;
1451 if (N) {
1452 f << "list[varId]=[";
1453 for (int i=0; i<N; ++i) {
1454 val=static_cast<int>(input->readLong(2));
1455 varList.push_back(val);
1456 f << val << ",";
1457 }
1458 f << "],";
1459 }
1460 // time to update the formula
1461 int id=0;
1462 for (auto &instr : formula) {
1463 if (instr.m_type!=MWAWCellContent::FormulaInstruction::F_Cell)
1464 continue;
1465 if (id>=static_cast<int>(varList.size()) || varList[size_t(id)]<=0) {
1466 MWAW_DEBUG_MSG(("GreatWksDBParser::readFormula: can not associated a cell with field\n"));
1467 f << "###";
1468 formula.resize(0);
1469 break;
1470 }
1471 instr.m_position[0][0]=varList[size_t(id++)]-1;
1472 }
1473 ascii().addPos(pos);
1474 ascii().addNote(f.str().c_str());
1475 return true;
1476 }
1477
1478 /* read the result of a formula.
1479
1480 note: we fill the cell content, which can overidden later if we find a formula to associate with the cell */
1481 bool GreatWksDBParser::readFormulaResult(long endPos, GreatWksDBParserInternal::Cell &cell, std::string &extra)
1482 {
1483 libmwaw::DebugStream f;
1484 MWAWInputStreamPtr input = getInput();
1485 long pos=input->tell();
1486 if (pos+2>endPos || !input->checkPosition(endPos)) return false;
1487 auto type=static_cast<int>(input->readLong(2));
1488 switch (type) {
1489 case 0: // average
1490 case 1: // summary
1491 case 5: // number
1492 case 9: // date
1493 case 0xa: { // time
1494 if (pos+12>endPos) return false;
1495 double value;
1496 bool isNan;
1497 if (!input->readDouble10(value, isNan))
1498 f << "#double,";
1499 else {
1500 cell.m_content.m_contentType=MWAWCellContent::C_NUMBER;
1501 cell.m_content.setValue(value);
1502 f << value << ",";
1503 }
1504 input->seek(pos+12, librevenge::RVNG_SEEK_SET);
1505 break;
1506 }
1507 case 7: {
1508 // checkme
1509 auto sSz=static_cast<int>(input->readULong(1));
1510 if (pos+3+sSz>endPos) {
1511 MWAW_DEBUG_MSG(("GreatWksSSParser::readFormulaResult: can not read a string value\n"));
1512 return false;
1513 }
1514 cell.m_content.m_contentType=MWAWCellContent::C_TEXT;
1515 cell.m_content.m_textEntry.setBegin(input->tell());
1516 cell.m_content.m_textEntry.setLength(sSz);
1517 // also modify the cell format in text
1518 auto format=cell.getFormat();
1519 format.m_format=MWAWCell::F_TEXT;
1520 cell.setFormat(format);
1521 std::string text("");
1522 for (int i=0; i<sSz; ++i)
1523 text += char(input->readULong(1));
1524 f << "\"" << text << "\",";
1525 if ((sSz%2)==0)
1526 input->seek(1, librevenge::RVNG_SEEK_CUR);
1527 break;
1528 }
1529 case 8: { // bool or long
1530 if (pos+4>endPos) return false;
1531 auto val=static_cast<int>(input->readLong(2));
1532 cell.m_content.m_contentType=MWAWCellContent::C_NUMBER;
1533 cell.m_content.setValue(double(val));
1534 f << input->readLong(2) << ",";
1535 break;
1536 }
1537 case 0xf: // nan
1538 if (pos+6>endPos) return false;
1539 cell.m_content.m_contentType=MWAWCellContent::C_NUMBER;
1540 cell.m_content.setValue(std::numeric_limits<double>::quiet_NaN());
1541 f << "nan" << input->readLong(4) << ",";
1542 break;
1543 default:
1544 MWAW_DEBUG_MSG(("GreatWksDBParser::readFormulaResult: find unknown type %d\n", type));
1545 f << "#type=" << type << ",";
1546 extra=f.str();
1547 return false;
1548 }
1549 extra=f.str();
1550 return true;
1551 }
1552
1553 ////////////////////////////////////////////////////////////
1554 // read the fields zone
1555 ////////////////////////////////////////////////////////////
1556 bool GreatWksDBParser::readFields(MWAWEntry const &entry)
1557 {
1558 if (!entry.valid() || entry.length()<10) {
1559 MWAW_DEBUG_MSG(("GreatWksDBParser::readFields: the entry length seems bad\n"));
1560 return false;
1561 }
1562 int const vers=version();
1563 entry.setParsed(true);
1564 ascii().addPos(entry.end());
1565 ascii().addNote("_");
1566 MWAWInputStreamPtr input = getInput();
1567 libmwaw::DebugStream f;
1568 f << "Entries(Field):";
1569 input->seek(entry.begin()+6, librevenge::RVNG_SEEK_SET);
1570 auto N=static_cast<int>(input->readULong(2));
1571 f << "N=" << N << ",";
1572 auto dSz=static_cast<int>(input->readULong(2));
1573 if ((vers==1 && dSz<0x3c) || (vers==2 && dSz<0x3e) || entry.length()<10+long(N)*dSz) {
1574 MWAW_DEBUG_MSG(("GreatWksDBParser::readFields: the number of data seems bad\n"));
1575 f << "###";
1576 ascii().addPos(entry.begin());
1577 ascii().addNote(f.str().c_str());
1578 return false;
1579 }
1580 ascii().addPos(entry.begin());
1581 ascii().addNote(f.str().c_str());
1582 long pos;
1583 for (int i=0; i<N; ++i) {
1584 pos=input->tell();
1585 f.str("");
1586 f << "Field-" << i << ":";
1587 GreatWksDBParserInternal::Field field;
1588 if (readField(field))
1589 f << field;
1590 else {
1591 field = GreatWksDBParserInternal::Field();
1592 MWAW_DEBUG_MSG(("GreatWksDBParser::readFields: can not read a field\n"));
1593 f << "###";
1594 }
1595 m_state->m_database.m_fieldList.push_back(field);
1596 ascii().addPos(pos);
1597 ascii().addNote(f.str().c_str());
1598 input->seek(pos+dSz, librevenge::RVNG_SEEK_SET);
1599 }
1600 for (auto &field : m_state->m_database.m_fieldList) {
1601 pos=input->tell();
1602 if (field.m_type==GreatWksDBParserInternal::Field::F_Summary) {
1603 if (pos+18>entry.end()) {
1604 MWAW_DEBUG_MSG(("GreatWksDBParser::readFields: can not read a summary field\n"));
1605 break;
1606 }
1607 f.str("");
1608 f << "Field[summary]:";
1609 auto val=static_cast<int>(input->readULong(2)); // 10a or 180
1610 if (val!=0x10a) f << "fl=" << std::hex << val << std::dec << ",";
1611 field.m_summaryType=static_cast<int>(input->readULong(2));
1612 if (field.m_summaryType > 0 && field.m_summaryType < 6) {
1613 static char const *wh[] = { "", "average", "count", "total", "minimum", "maximum" };
1614 f << wh[field.m_summaryType] << ",";
1615 }
1616 else
1617 f << "#type=" << field.m_summaryType << ",";
1618 field.m_summaryField=static_cast<int>(input->readLong(2))-1;
1619 f << "field=" << field.m_summaryField << ",";
1620 val=static_cast<int>(input->readULong(2)); // 1e0 or 14
1621 if (val!=0x1e0) f << "f0=" << std::hex << val << std::dec << ",";
1622 for (int i=0; i<5; ++i) { // always 0
1623 val=static_cast<int>(input->readULong(2));
1624 if (val) f << "f" << i+1 << "=" << val << ",";
1625 }
1626
1627 ascii().addPos(pos);
1628 ascii().addNote(f.str().c_str());
1629 input->seek(pos+18, librevenge::RVNG_SEEK_SET);
1630 continue;
1631 }
1632 if (field.m_type!=GreatWksDBParserInternal::Field::F_Formula)
1633 continue;
1634 if (!readFormula(entry.end(), field.m_formula)) {
1635 input->seek(pos, librevenge::RVNG_SEEK_SET);
1636 break;
1637 }
1638 }
1639 pos=input->tell();
1640 if (input->tell()!=entry.end()) { // now some string or formula?
1641 ascii().addPos(pos);
1642 ascii().addNote("Field:end");
1643 }
1644
1645 return true;
1646 }
1647
1648 bool GreatWksDBParser::readField(GreatWksDBParserInternal::Field &field)
1649 {
1650 field=GreatWksDBParserInternal::Field();
1651 MWAWInputStreamPtr input = getInput();
1652 long pos=input->tell();
1653 if (!input->checkPosition(pos+0x3c)) return false;
1654 auto type=static_cast<int>(input->readLong(2));
1655 libmwaw::DebugStream f;
1656 auto &format = field.m_format;
1657 switch (type) {
1658 case 5:
1659 field.m_type=GreatWksDBParserInternal::Field::F_Number;
1660 format.m_format=MWAWCell::F_NUMBER;
1661 format.m_numberFormat=MWAWCell::F_NUMBER_GENERIC;
1662 break;
1663 case 7:
1664 field.m_type=GreatWksDBParserInternal::Field::F_Text;
1665 format.m_format=MWAWCell::F_TEXT;
1666 break;
1667 case 9:
1668 field.m_type=GreatWksDBParserInternal::Field::F_Date;
1669 format.m_format=MWAWCell::F_DATE;
1670 break;
1671 case 0xa:
1672 field.m_type=GreatWksDBParserInternal::Field::F_Time;
1673 format.m_format=MWAWCell::F_TIME;
1674 break;
1675 case 0xc:
1676 field.m_type=GreatWksDBParserInternal::Field::F_Memo;
1677 format.m_format=MWAWCell::F_TEXT;
1678 break;
1679 case 0xd:
1680 field.m_type=GreatWksDBParserInternal::Field::F_Picture;
1681 break;
1682 case 0xFF:
1683 field.m_type=GreatWksDBParserInternal::Field::F_Formula;
1684 format.m_format=MWAWCell::F_NUMBER;
1685 format.m_numberFormat=MWAWCell::F_NUMBER_GENERIC;
1686 break;
1687 case 0xFE:
1688 field.m_type=GreatWksDBParserInternal::Field::F_Summary;
1689 format.m_format=MWAWCell::F_NUMBER;
1690 format.m_numberFormat=MWAWCell::F_NUMBER_GENERIC;
1691 break;
1692 default:
1693 MWAW_DEBUG_MSG(("GreatWksDBParser::readField: find unknown file type\n"));
1694 f << "#type=" << type << ",";
1695 }
1696 int val;
1697 if (version()==2) { // always 5?
1698 val=static_cast<int>(input->readLong(2));
1699 if (val!=5) f << "f0=" << val << ",";
1700 }
1701 for (int i=0; i<8; ++i) { // fl0-6=0|1, fl7=2c|38|ee|c4
1702 val=static_cast<int>(input->readULong(1));
1703 if (val) f << "fl" << i << "=" << std::hex << val << std::dec << ",";
1704 }
1705 val=static_cast<int>(input->readLong(2)); // always 0
1706 if (val) f << "f1=" << val << ",";
1707 /* f2=f3=f4=0 excepted
1708 for formula find 8,5bb2b4,1b or 4,5bb200,5
1709 for summary find 0,0,05bb254 and 0,0,5bb268
1710 */
1711 val=static_cast<int>(input->readLong(2));
1712 if (val) f << "f2=" << val << ",";
1713 for (int i=0; i<2; ++i) {
1714 val=static_cast<int>(input->readULong(4));
1715 if (val) f << "f" << 3+i << "=" << std::hex << val << std::dec << ",";
1716 }
1717 val=static_cast<int>(input->readLong(2)); // always 0
1718 if (val) f << "f5=" << val << ",";
1719 field.m_linkZone=long(input->readULong(2));
1720 field.m_id=static_cast<int>(input->readLong(2))-1;
1721 auto fSz=static_cast<int>(input->readULong(1));
1722 if (fSz>31) {
1723 MWAW_DEBUG_MSG(("GreatWksDBParser::readField: the name field size seems to big\n"));
1724 f << "###";
1725 ascii().addDelimiter(input->tell(),'|');
1726 }
1727 else {
1728 std::string name("");
1729 for (int i=0; i<fSz; ++i) name+=char(input->readULong(1));
1730 field.m_name=name;
1731 }
1732 field.m_extra=f.str();
1733 return true;
1734 }
1735
1736 bool GreatWksDBParser::readFieldAuxis(MWAWEntry const &entry)
1737 {
1738 if (!entry.valid() || entry.length()<10) {
1739 MWAW_DEBUG_MSG(("GreatWksDBParser::readFieldAuxis: the entry length seems bad\n"));
1740 return false;
1741 }
1742 int const vers=version();
1743 entry.setParsed(true);
1744 ascii().addPos(entry.end());
1745 ascii().addNote("_");
1746 MWAWInputStreamPtr input = getInput();
1747 libmwaw::DebugStream f;
1748 f << "Entries(Field):";
1749 input->seek(entry.begin()+6, librevenge::RVNG_SEEK_SET);
1750 auto N=static_cast<int>(input->readULong(2));
1751 f << "N=" << N << ",";
1752 auto dSz=static_cast<int>(input->readULong(2));
1753 if (vers==1 || (vers==2 && dSz<0x46) || entry.length()<10+long(N)*dSz) {
1754 MWAW_DEBUG_MSG(("GreatWksDBParser::readFieldAuxis: the number of data seems bad\n"));
1755 f << "###";
1756 ascii().addPos(entry.begin());
1757 ascii().addNote(f.str().c_str());
1758 return false;
1759 }
1760 ascii().addPos(entry.begin());
1761 ascii().addNote(f.str().c_str());
1762 auto &database=m_state->m_database;
1763 if (N>static_cast<int>(database.m_fieldList.size())) {
1764 MWAW_DEBUG_MSG(("GreatWksDBParser::readFieldAuxis: the field list seems too short\n"));
1765 database.m_fieldList.resize(size_t(N));
1766 }
1767 int n=0;
1768 for (auto &field : database.m_fieldList) {
1769 long pos=input->tell();
1770 f.str("");
1771 f << "FldAuxi-" << n++ << ":";
1772 auto val=static_cast<int>(input->readLong(2));
1773 if (val) {
1774 // checkme: find val=10 for a sequence with increment=10, so...
1775 f << "type[increment]=" << val << ",";
1776 field.m_isSequence=true;
1777 }
1778 val=static_cast<int>(input->readLong(2));
1779 if (val==1) {
1780 f << "isUnique[number],";
1781 // let use a sequence
1782 field.m_isSequence=true;
1783 }
1784 else if (val)
1785 f << "isUnique[number]=" << val << ",";
1786 for (int j=0; j<2; ++j) { // always 0?
1787 val=static_cast<int>(input->readLong(2));
1788 if (val) f << "f" << j << "=" << val << ",";
1789 }
1790 field.m_firstNumber=static_cast<int>(input->readLong(2));
1791 if (field.m_firstNumber!=1)
1792 f << "first[sequence]=" << field.m_firstNumber << ",";
1793 val=static_cast<int>(input->readLong(2)); // always 0
1794 if (val) f << "f2=" << val << ",";
1795 field.m_incrementNumber=static_cast<int>(input->readLong(2));
1796 if (field.m_incrementNumber!=1)
1797 f << "increment[sequence]=" << field.m_incrementNumber << ",";
1798 for (int j=0; j < 28; ++j) { // always 0
1799 val=static_cast<int>(input->readLong(2));
1800 if (val) f << "f" << j << "=" << val << ",";
1801 }
1802 input->seek(pos+dSz, librevenge::RVNG_SEEK_SET);
1803 ascii().addPos(pos);
1804 ascii().addNote(f.str().c_str());
1805 }
1806 return true;
1807 }
1808 ////////////////////////////////////////////////////////////
1809 // read a list of records corresponding to a field
1810 ////////////////////////////////////////////////////////////
1811 bool GreatWksDBParser::readFieldRecords(GreatWksDBParserInternal::Field &field)
1812 {
1813 if (field.m_recordBlock.isEmpty()) {
1814 MWAW_DEBUG_MSG(("GreatWksDBParser::readFieldRecords: can not read a record zone\n"));
1815 return false;
1816 }
1817 auto block=createBlock(field.m_recordBlock);
1818 if (!block) {
1819 MWAW_DEBUG_MSG(("GreatWksDBParser::readFieldRecords: can not find the records input\n"));
1820 return false;
1821 }
1822 auto &database=m_state->m_database;
1823 MWAWInputStreamPtr input = getInput();
1824 libmwaw::DebugStream f;
1825 for (size_t z=0; z<block->getNumZones(); ++z) {
1826 auto const &zone=block->getZone(z);
1827 long pos=zone.m_ptr;
1828 f.str("");
1829 f << "Entries(FldRec)[" << field.m_id << "]:type=" << int(field.m_type) << ",";
1830 input->seek(pos, librevenge::RVNG_SEEK_SET);
1831 int const N=zone.m_N;
1832 auto const dSz=static_cast<int>(zone.m_dataSize);
1833 if (field.m_type==GreatWksDBParserInternal::Field::F_Text) {
1834 if (!input->checkPosition(pos+dSz+N*5)) {
1835 MWAW_DEBUG_MSG(("GreatWksDBParser::readFieldRecords: can not read a text zone\n"));
1836 f << "###";
1837 ascii().addPos(pos);
1838 ascii().addNote(f.str().c_str());
1839 continue;
1840 }
1841 for (int i=0; i<6; ++i) { // always 0?
1842 auto val=static_cast<int>(input->readLong(2));
1843 if (val) f << "f" << i << "=" << val << ",";
1844 }
1845 std::vector<int> positions, rows;
1846 // first read the list of positions
1847 input->seek(pos+dSz, librevenge::RVNG_SEEK_SET);
1848 for (int i=0; i<N; ++i)
1849 positions.push_back(static_cast<int>(input->readULong(1)));
1850 // now read a list of small int: the row number
1851 for (int i=0; i<N; ++i)
1852 rows.push_back(static_cast<int>(input->readULong(4)));
1853 input->seek(pos, librevenge::RVNG_SEEK_SET);
1854 int actPos=0;
1855 for (size_t i=0; i<size_t(N); ++i) {
1856 int nextPos= i+1==size_t(N) ? dSz : positions[i+1];
1857 if (actPos!=positions[i] || nextPos>dSz) {
1858 MWAW_DEBUG_MSG(("GreatWksDBParser::readFieldRecords: can not read a text zone, pb with a position\n"));
1859 f << "###";
1860 break;
1861 }
1862 GreatWksDBParserInternal::Cell cell;
1863 cell.setFormat(field.m_format);
1864 cell.m_content.m_contentType=MWAWCellContent::C_TEXT;
1865 cell.m_content.m_textEntry.setBegin(input->tell());
1866 std::string text("");
1867 bool findEmptyChar=false;
1868 while (actPos<nextPos) {
1869 auto c=char(input->readULong(1));
1870 // c==0 can indicate an empty field
1871 if (c) text += c;
1872 else findEmptyChar=true;
1873 ++actPos;
1874 }
1875 cell.m_content.m_textEntry.setEnd(input->tell()-(findEmptyChar ? 1 : 0));
1876 database.addCell(MWAWVec2i(field.m_id, rows[i]), cell);
1877 f << "\"" << text << "\":" << rows[i] << ",";
1878 }
1879 input->seek(pos+12+dSz+5*N, librevenge::RVNG_SEEK_SET);
1880 }
1881 else if (field.m_type==GreatWksDBParserInternal::Field::F_Number ||
1882 field.m_type==GreatWksDBParserInternal::Field::F_Date ||
1883 field.m_type==GreatWksDBParserInternal::Field::F_Time) {
1884 if (!input->checkPosition(pos+14*N)) {
1885 MWAW_DEBUG_MSG(("GreatWksDBParser::readFieldRecords: can not read a number zone\n"));
1886 f << "###";
1887 ascii().addPos(pos);
1888 ascii().addNote(f.str().c_str());
1889 continue;
1890 }
1891 for (int i=0; i<N; ++i) {
1892 long fPos=input->tell();
1893 auto row=static_cast<int>(input->readLong(4));
1894 double value;
1895 bool isNan;
1896 if (!input->readDouble10(value, isNan)) {
1897 static bool first=true;
1898 if (first) {
1899 // can be normal, ie. 7fff403200000000000000 means no value which generates an error
1900 MWAW_DEBUG_MSG(("GreatWksDBParser::readFieldRecords: can not read a number, maybe a undef number\n"));
1901 first=false;
1902 }
1903 f << "#";
1904 input->seek(fPos+14, librevenge::RVNG_SEEK_SET);
1905 }
1906 else {
1907 GreatWksDBParserInternal::Cell cell;
1908 cell.setFormat(field.m_format);
1909 cell.m_content.m_contentType=MWAWCellContent::C_NUMBER;
1910 cell.m_content.setValue(value);
1911 database.addCell(MWAWVec2i(field.m_id, row), cell);
1912 f << value;
1913 }
1914 f << ":" << row << ",";
1915 }
1916 }
1917 else {
1918 MWAW_DEBUG_MSG(("GreatWksDBParser::readFieldRecords: does not know how to read list for type=%d\n", int(field.m_type)));
1919 f << "###";
1920 }
1921 ascii().addPos(pos);
1922 ascii().addNote(f.str().c_str());
1923 ascii().addPos(input->tell());
1924 ascii().addNote("_");
1925 }
1926 return true;
1927 }
1928
1929 ////////////////////////////////////////////////////////////
1930 // read the links between fields and record zone
1931 ////////////////////////////////////////////////////////////
1932 bool GreatWksDBParser::readFieldLinks(GreatWksDBParserInternal::Field &field)
1933 {
1934 MWAWInputStreamPtr input = getInput();
1935 if (field.m_linkZone<=0 || !input->checkPosition(field.m_linkZone+32)) {
1936 MWAW_DEBUG_MSG(("GreatWksDBParser::readFieldLinks: can not read a link between field to records\n"));
1937 return false;
1938 }
1939 input->seek(field.m_linkZone, librevenge::RVNG_SEEK_SET);
1940 libmwaw::DebugStream f;
1941 f << "Entries(FldLink)[" << field.m_id << "]:";
1942 auto val=static_cast<int>(input->readLong(2));
1943 if (val!=0xa) f << "#type=" << val << ",";
1944 val=static_cast<int>(input->readLong(2)); // always 0
1945 if (val) f << "f0=" << val << ",";
1946 val=static_cast<int>(input->readLong(2));
1947 if (val!=0x14) f << "f1=" << val << ",";
1948 if (!readBlockHeader(field.m_recordBlock)) {
1949 MWAW_DEBUG_MSG(("GreatWksDBParser::readFieldLinks: the zone position seems bad\n"));
1950 f << "###block,";
1951 }
1952 f << "block=" << field.m_recordBlock << ",";
1953 ascii().addDelimiter(input->tell(),'|');
1954 ascii().addPos(field.m_linkZone);
1955 ascii().addNote(f.str().c_str());
1956 ascii().addPos(field.m_linkZone+32);
1957 ascii().addNote("_");
1958 return true;
1959 }
1960
1961 ////////////////////////////////////////////////////////////
1962 // read a list of int
1963 ////////////////////////////////////////////////////////////
1964 bool GreatWksDBParser::readIntList(MWAWEntry const &entry, std::vector<int> &list)
1965 {
1966 list.resize(0);
1967 if (!entry.valid() || entry.length()<10) {
1968 MWAW_DEBUG_MSG(("GreatWksDBParser::readIntList: the entry length seems bad\n"));
1969 return false;
1970 }
1971 MWAWInputStreamPtr input = getInput();
1972 input->seek(entry.begin()+6, librevenge::RVNG_SEEK_SET);
1973 auto N=static_cast<int>(input->readULong(2));
1974 auto dSz=static_cast<int>(input->readULong(2));
1975 if (dSz!=2 || entry.length()!=10+N*dSz) {
1976 MWAW_DEBUG_MSG(("GreatWksDBParser::readIntList: the N or dSz value seems bad\n"));
1977 return false;
1978 }
1979
1980 entry.setParsed(true);
1981 ascii().addPos(entry.end());
1982 ascii().addNote("_");
1983
1984 for (int i=0; i<N; ++i)
1985 list.push_back(static_cast<int>(input->readLong(2)));
1986
1987 return true;
1988 }
1989
1990 ////////////////////////////////////////////////////////////
1991 // read a list of form
1992 ////////////////////////////////////////////////////////////
1993 bool GreatWksDBParser::readFormLinks(MWAWEntry const &entry)
1994 {
1995 if (!entry.valid() || entry.length()<10) {
1996 MWAW_DEBUG_MSG(("GreatWksDBParser::readFormLinks: the entry length seems bad\n"));
1997 return false;
1998 }
1999 entry.setParsed(true);
2000 ascii().addPos(entry.end());
2001 ascii().addNote("_");
2002 MWAWInputStreamPtr input = getInput();
2003 libmwaw::DebugStream f;
2004 f << "Entries(" << entry.name() << "):";
2005 input->seek(entry.begin()+6, librevenge::RVNG_SEEK_SET);
2006 auto N=static_cast<int>(input->readULong(2));
2007 f << "N=" << N << ",";
2008 auto dSz=static_cast<int>(input->readULong(2));
2009 if (entry.length()!=10+long(N)*dSz || dSz<4) {
2010 MWAW_DEBUG_MSG(("GreatWksDBParser::readFormLinks: the number of data seems bad\n"));
2011 f << "###";
2012 ascii().addPos(entry.begin());
2013 ascii().addNote(f.str().c_str());
2014 return false;
2015 }
2016 ascii().addPos(entry.begin());
2017 ascii().addNote(f.str().c_str());
2018 std::vector<MWAWEntry> listZones;
2019 for (int i=0; i<N; ++i) {
2020 long pos=input->tell();
2021 f.str("");
2022 f << entry.name() << "-" << i << ":";
2023 auto val=static_cast<int>(input->readLong(2)); // always 0?
2024 if (val) f << "f0=" << val << ",";
2025 auto ptr=static_cast<int>(input->readULong(2));
2026 if (ptr) {
2027 MWAWEntry zone;
2028 zone.setBegin(ptr);
2029 if (checkSmallZone(zone))
2030 listZones.push_back(zone);
2031 else
2032 f << "###";
2033 f << "ptr" << i << "=" << std::hex << ptr << std::dec << ",";
2034 }
2035 input->seek(pos+dSz, librevenge::RVNG_SEEK_SET);
2036 ascii().addPos(pos);
2037 ascii().addNote(f.str().c_str());
2038 }
2039 for (auto const &zone : listZones)
2040 readForm(zone);
2041 return true;
2042 }
2043
2044 ////////////////////////////////////////////////////////////
2045 // read zone 2 link zone
2046 ////////////////////////////////////////////////////////////
2047 bool GreatWksDBParser::readForm(MWAWEntry const &entry)
2048 {
2049 int const vers=version();
2050 /* not really sure what may be the header size, ie.
2051 in v1: 264 seems ok,
2052 in v2: 276 seems ok,
2053 but some v1 file converted in v2 seems to keep intact the v1 structure :-~
2054
2055 This must be fixed if we want to export the form...
2056 */
2057 int const headerSize=vers==1 ? 264 : 276;
2058 MWAWInputStreamPtr input = getInput();
2059 long pos=entry.begin();
2060 libmwaw::DebugStream f;
2061 f << "Entries(" << entry.name() << "):";
2062 ascii().addPos(entry.end());
2063 ascii().addNote("_");
2064 if (entry.id()!=9 || entry.length() < 6+headerSize) {
2065 f << "###";
2066 MWAW_DEBUG_MSG(("GreatWksDBParser::readForm: the entry seems bad\n"));
2067 ascii().addPos(pos);
2068 ascii().addNote(f.str().c_str());
2069 return false;
2070 }
2071 input->seek(pos+6, librevenge::RVNG_SEEK_SET);
2072 // a big number 5bafac : an id?
2073 f << "f0=" << std::hex << input->readULong(4) << std::dec << ",";
2074 // small number between 1 and 4
2075 f << "id=" << input->readLong(2) << ",";
2076 auto sSz=static_cast<int>(input->readULong(1));
2077 if (sSz>32) {
2078 f << "###";
2079 MWAW_DEBUG_MSG(("GreatWksDBParser::readForm: the string size seems bad\n"));
2080 ascii().addPos(pos);
2081 ascii().addNote(f.str().c_str());
2082 return false;
2083 }
2084 std::string text("");
2085 for (int i=0; i<sSz; ++i) text+=char(input->readULong(1));
2086 f << text << ",";
2087 input->seek(pos+44, librevenge::RVNG_SEEK_SET);
2088 ascii().addDelimiter(input->tell(),'|');
2089 ascii().addPos(pos);
2090 ascii().addNote(f.str().c_str());
2091
2092 input->seek(pos+headerSize, librevenge::RVNG_SEEK_SET);
2093 pos=input->tell();
2094 if (!m_document->getGraphParser()->readPageFrames())
2095 input->seek(pos, librevenge::RVNG_SEEK_SET);
2096 pos=input->tell();
2097 if (pos!=entry.end()) {
2098 ascii().addPos(pos);
2099 ascii().addNote("Form:end");
2100 }
2101 return true;
2102 }
2103
2104 ////////////////////////////////////////////////////////////
2105 // read a unknown zone of zone
2106 ////////////////////////////////////////////////////////////
2107 bool GreatWksDBParser::readZone12(MWAWEntry const &entry)
2108 {
2109 int const vers=version();
2110 if (!entry.valid() || entry.length()!=10+2*vers) {
2111 MWAW_DEBUG_MSG(("GreatWksDBParser::readZone12: the entry length seems bad\n"));
2112 return false;
2113 }
2114 MWAWInputStreamPtr input = getInput();
2115 libmwaw::DebugStream f;
2116 f << "Entries(" << entry.name() << "):";
2117 input->seek(entry.begin()+6, librevenge::RVNG_SEEK_SET);
2118
2119 entry.setParsed(true);
2120 ascii().addPos(entry.end());
2121 ascii().addNote("_");
2122
2123 int val;
2124 for (int i=0; i<4; ++i) { // f0=f1=0, f2=f3=1
2125 val=static_cast<int>(input->readLong(1));
2126 if (val) f << "f" << i << "=" << val << ",";
2127 }
2128 if (vers==2) // b0|2a
2129 f << "g0=" << input->readLong(2) << ",";
2130 val=static_cast<int>(input->readLong(2));
2131 if (val) // 66|67|7a
2132 f << "g1=" << val << ",";
2133 ascii().addPos(entry.begin());
2134 ascii().addNote(f.str().c_str());
2135
2136 return true;
2137 }
2138
2139 ////////////////////////////////////////////////////////////
2140 // read unknown small zone
2141 ////////////////////////////////////////////////////////////
2142 bool GreatWksDBParser::readSmallZone(MWAWEntry const &entry)
2143 {
2144 if (!entry.valid() || entry.length()<10) {
2145 MWAW_DEBUG_MSG(("GreatWksDBParser::readSmallZone: the entry length seems bad\n"));
2146 return false;
2147 }
2148 entry.setParsed(true);
2149 ascii().addPos(entry.end());
2150 ascii().addNote("_");
2151 MWAWInputStreamPtr input = getInput();
2152 libmwaw::DebugStream f;
2153 f << "Entries(" << entry.name() << "):";
2154 input->seek(entry.begin()+6, librevenge::RVNG_SEEK_SET);
2155 auto N=static_cast<int>(input->readULong(2));
2156 f << "N=" << N << ",";
2157 long dSz=static_cast<int>(input->readULong(2));
2158 if (entry.length()!=10+long(N)*dSz) {
2159 MWAW_DEBUG_MSG(("GreatWksDBParser::readSmallZone: the number of data seems bad\n"));
2160 f << "###";
2161 ascii().addPos(entry.begin());
2162 ascii().addNote(f.str().c_str());
2163 return false;
2164 }
2165 ascii().addPos(entry.begin());
2166 ascii().addNote(f.str().c_str());
2167 for (int i=0; i<N; ++i) {
2168 long pos=input->tell();
2169 f.str("");
2170 f << entry.name() << "-" << i << ":";
2171 ascii().addPos(pos);
2172 ascii().addNote(f.str().c_str());
2173 input->seek(pos+dSz, librevenge::RVNG_SEEK_SET);
2174 }
2175
2176 return true;
2177 }
2178
2179 ////////////////////////////////////////////////////////////
2180 ////////////////////////////////////////////////////////////
2181 bool GreatWksDBParser::sendDatabase()
2182 {
2183 MWAWSpreadsheetListenerPtr listener=getSpreadsheetListener();
2184 if (!listener) {
2185 MWAW_DEBUG_MSG(("GreatWksDBParser::sendDatabase: I can not find the listener\n"));
2186 return false;
2187 }
2188 MWAWInputStreamPtr input=getInput();
2189 auto const &database=m_state->m_database;
2190 auto const &fields = database.m_fieldList;
2191 size_t numFields=fields.size();
2192 // fixme: use first layout colWidth here
2193 listener->openSheet(std::vector<float>(1,76), librevenge::RVNG_POINT, std::vector<int>(1,static_cast<int>(numFields)), "Sheet0");
2194 int r=-1, numRows=static_cast<int>(database.m_rowCellsMap.size());
2195 for (auto const &rIt : database.m_rowCellsMap) {
2196 auto const &row=rIt.second;
2197 listener->openSheetRow(12, librevenge::RVNG_POINT);
2198 ++r;
2199 for (size_t c=0; c< numFields; ++c) {
2200 auto const &field=fields[c];
2201 GreatWksDBParserInternal::Cell cell;
2202 if (c<row.size()) cell=row[c];
2203 field.updateCell(int(r), numRows, cell);
2204 if (cell.isEmpty()) continue;
2205
2206 auto const &content=cell.m_content;
2207 cell.setPosition(MWAWVec2i(int(c),r));
2208 listener->openSheetCell(cell, content);
2209 if (field.m_type==GreatWksDBParserInternal::Field::F_Text && content.m_textEntry.valid()) {
2210 input->seek(content.m_textEntry.begin(), librevenge::RVNG_SEEK_SET);
2211 while (!input->isEnd() && input->tell()<content.m_textEntry.end()) {
2212 auto ch=static_cast<unsigned char>(input->readULong(1));
2213 if (ch==0xd)
2214 listener->insertEOL();
2215 else if (ch<30) {
2216 MWAW_DEBUG_MSG(("GreatWksDBParser::sendDatabase: find some odd character\n"));
2217 break;
2218 }
2219 else
2220 listener->insertCharacter(ch);
2221 }
2222 }
2223 else if (field.m_type==GreatWksDBParserInternal::Field::F_Memo && content.m_textEntry.valid())
2224 m_document->getTextParser()->sendTextbox(content.m_textEntry, listener);
2225 else if (field.m_type==GreatWksDBParserInternal::Field::F_Picture && cell.m_pictEntry.valid()) {
2226 librevenge::RVNGBinaryData pict;
2227 input->seek(cell.m_pictEntry.begin(),librevenge::RVNG_SEEK_SET);
2228 input->readDataBlock(cell.m_pictEntry.length(), pict);
2229
2230 std::string cellName("Sheet0.");
2231 cellName+=MWAWCell::getBasicCellName(cell.position()+MWAWVec2i(1,1));
2232 MWAWPosition position(MWAWVec2f(0,0), MWAWVec2f(76,12), librevenge::RVNG_POINT); // changeme
2233 position.setAnchorToCell(librevenge::RVNGString(cellName.c_str()));
2234 listener->insertPicture(position, MWAWEmbeddedObject(pict));
2235 }
2236 listener->closeSheetCell();
2237 }
2238 listener->closeSheetRow();
2239 }
2240 listener->closeSheet();
2241 return true;
2242 }
2243
2244 ////////////////////////////////////////////////////////////
2245 // read the header
2246 ////////////////////////////////////////////////////////////
2247 bool GreatWksDBParser::checkHeader(MWAWHeader *header, bool strict)
2248 {
2249 MWAWInputStreamPtr input = getInput();
2250 *m_state = GreatWksDBParserInternal::State();
2251 if (!m_document->checkHeader(header, strict) || !input) return false;
2252 if (getParserState()->m_kind!=MWAWDocument::MWAW_K_DATABASE)
2253 return false;
2254 if (!strict) return true;
2255
2256 // let check that the 3 header are defined
2257 input->seek(16, librevenge::RVNG_SEEK_SET);
2258 for (int i=0; i<3; ++i) {
2259 GreatWksDBParserInternal::BlockHeader block;
2260 if (!readBlockHeader(block) || block.m_ptr[0]==0 || (block.m_ptr[0]&0xFF)) return false;
2261 input->seek(8, librevenge::RVNG_SEEK_CUR);
2262 }
2263 return true;
2264 }
2265
2266 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: