Debian Codegraph @

src/lib/MsWksDBParser.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 <sstream>
38
39 #include <librevenge/librevenge.h>
40
41
42 #include "MWAWCell.hxx"
43 #include "MWAWFont.hxx"
44 #include "MWAWFontConverter.hxx"
45 #include "MWAWHeader.hxx"
46 #include "MWAWOLEParser.hxx"
47 #include "MWAWPrinter.hxx"
48 #include "MWAWSpreadsheetListener.hxx"
49 #include "MWAWSubDocument.hxx"
50
51 #include "MsWksGraph.hxx"
52 #include "MsWks3Text.hxx"
53 #include "MsWksDocument.hxx"
54
55 #include "MsWksDBParser.hxx"
56
57 /** Internal: the structures of a MsWksDBParser */
58 namespace MsWksDBParserInternal
59 {
60 ////////////////////////////////////////////////////////////
61 // the data ( and the list view )
62 ////////////////////////////////////////////////////////////
63
64 /** a class to store the serial data which code a auto increment
65 column with potential prefix and suffix in v4. */
66 class SerialFormula
67 {
68 public:
69 //! the constructor
70 SerialFormula()
71 : m_increment(0)
72 , m_nextValue(0)
73 , m_prefix("")
74 , m_suffix("")
75 {
76 }
77
78 //! operator<<
79 friend std::ostream &operator<<(std::ostream &o, SerialFormula const &form);
80
81 //! update a content (knowing the value)
82 void updateContent(double value, MWAWCellContent &content) const;
83
84 //! the increment
85 long m_increment;
86 //! the first value
87 long m_nextValue;
88
89 //! the prefix
90 std::string m_prefix;
91
92 //! the suffix
93 std::string m_suffix;
94 };
95
96 void SerialFormula::updateContent(double value, MWAWCellContent &content) const
97 {
98 auto &formula=content.m_formula;
99 formula.resize(0);
100 content.m_contentType=MWAWCellContent::C_FORMULA;
101 MWAWCellContent::FormulaInstruction instr;
102 if (m_prefix.empty() && m_suffix.empty()) {
103 instr.m_type=MWAWCellContent::FormulaInstruction::F_Long;
104 instr.m_longValue=value;
105 formula.push_back(instr);
106 return;
107 }
108 instr.m_type=MWAWCellContent::FormulaInstruction::F_Function;
109 instr.m_content="Concatenate";
110 formula.push_back(instr);
111 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
112 instr.m_content="(";
113 formula.push_back(instr);
114 if (!m_prefix.empty()) {
115 instr.m_type=MWAWCellContent::FormulaInstruction::F_Text;
116 instr.m_content=m_prefix;
117 formula.push_back(instr);
118 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
119 instr.m_content=";";
120 formula.push_back(instr);
121 }
122 instr.m_type=MWAWCellContent::FormulaInstruction::F_Long;
123 instr.m_longValue=value;
124 formula.push_back(instr);
125 if (!m_suffix.empty()) {
126 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
127 instr.m_content=";";
128 formula.push_back(instr);
129 instr.m_type=MWAWCellContent::FormulaInstruction::F_Text;
130 instr.m_content=m_suffix;
131 formula.push_back(instr);
132 }
133 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
134 instr.m_content=")";
135 formula.push_back(instr);
136 }
137
138 //! operator<<
139 std::ostream &operator<<(std::ostream &o, SerialFormula const &form)
140 {
141 if (form.m_prefix.length()) o << "\"" << form.m_prefix << "\".";
142 o << "(" << form.m_nextValue << "+" << form.m_increment << "*Row)";
143 if (form.m_suffix.length()) o << ".\"" << form.m_suffix << "\"";
144 return o;
145 }
146
147 /** the type of each field */
148 class FieldType final : public MWAWCell
149 {
150 public:
151 //! constructor
152 FieldType()
153 : MWAWCell()
154 , m_content()
155 , m_name("")
156 , m_used(true)
157 , m_isSerial(false)
158 , m_serialId(0)
159 , m_serialFormula()
160 , m_height(0)
161 , m_extra("")
162 {
163 }
164 FieldType(FieldType const &)=default;
165 FieldType &operator=(FieldType const &)=default;
166 //! destructor
167 ~FieldType() final;
168
169 //! operator<<
170 friend std::ostream &operator<<(std::ostream &o, FieldType const &fType);
171
172 //! returns true if the field has no content
173 bool empty() const
174 {
175 return !m_isSerial && m_content.empty();
176 }
177
178 //! update a field with the record data
179 void updateWithContent(MWAWVec2i const &pos, MWAWCellContent const &content);
180
181 /** the cell content */
182 MWAWCellContent m_content;
183 /** the field name */
184 std::string m_name;
185
186 /** a flag to know if the field is used or not */
187 bool m_used;
188 /** a flag to deferentiate a serial formula to a formula */
189 bool m_isSerial;
190 /** serialized field identificator */
191 int m_serialId;
192 /** the serial formula ( if the field is serialized ) */
193 SerialFormula m_serialFormula;
194
195 /** the height */
196 int m_height;
197 /** extra data */
198 std::string m_extra;
199 };
200
201 FieldType::~FieldType()
202 {
203 }
204
205 std::ostream &operator<<(std::ostream &o, FieldType const &fType)
206 {
207 if (!fType.m_used) {
208 o << "unused,";
209 return o;
210 }
211 o << static_cast<MWAWCell const &>(fType);
212 o << fType.m_content;
213 if (fType.m_name.length())
214 o << "name=\"" << fType.m_name << "\",";
215
216 if (fType.m_isSerial && fType.m_serialId) o << "serialId=" << fType.m_serialId << ",";
217 if (fType.m_height) o << "h=" << fType.m_height << ",";
218
219 o << fType.m_extra;
220 return o;
221 }
222
223 void FieldType::updateWithContent(MWAWVec2i const &pos, MWAWCellContent const &content)
224 {
225 setPosition(pos);
226 if (m_isSerial) {
227 m_serialFormula.updateContent(content.m_value, m_content);
228 return;
229 }
230 auto const &format=getFormat();
231 if (!content.empty()) {
232 if (m_content.m_contentType==MWAWCellContent::C_FORMULA) {
233 // special case, we need to conserve the formula
234 if (content.m_contentType==MWAWCellContent::C_NUMBER)
235 m_content.setValue(content.m_value);
236 else
237 m_content.m_textEntry=content.m_textEntry;
238 }
239 else
240 m_content=content;
241 }
242 // change the reference date from 1/1/1904 to 1/1/1900
243 if (format.m_format==MWAWCell::F_DATE && m_content.isValueSet())
244 m_content.setValue(m_content.m_value+1460.);
245 // and try to update the 1D formula in 2D
246 auto &formula=m_content.m_formula;
247 for (auto &instr : formula) {
248 if (instr.m_type==MWAWCellContent::FormulaInstruction::F_Cell) {
249 --instr.m_position[0][0];
250 instr.m_position[0][1]=pos[1];
251 }
252 else if (instr.m_type==MWAWCellContent::FormulaInstruction::F_CellList) {
253 --instr.m_position[0][0];
254 --instr.m_position[1][0];
255 instr.m_position[0][1]=instr.m_position[1][1]=pos[1];
256 }
257 }
258 }
259
260 /** the database */
261 class DataBase
262 {
263 public:
264 /** constructor */
265 DataBase()
266 : m_numFields(0)
267 , m_numRecords(0)
268 , m_listFieldTypes()
269 , m_listRecords()
270 {
271 }
272 //! convert the m_widthCols in a vector of of point size
273 std::vector<float> convertInPoint(std::vector<int> const &list, float defSize) const
274 {
275 size_t numCols=m_listFieldTypes.size();
276 std::vector<float> res;
277 res.resize(numCols);
278 for (size_t i = 0; i < numCols; i++) {
279 if (i>=list.size() || list[i] < 0) res[i] = defSize;
280 else res[i] = float(list[i]);
281 }
282 return res;
283 }
284
285 /** the number of fields */
286 int m_numFields;
287
288 /** the number of records */
289 int m_numRecords;
290 /** the list of field type
291
292 \note which begins by an unused field */
293 std::vector<FieldType> m_listFieldTypes;
294
295 /** the list of record by row */
296 std::vector<std::vector<MWAWCellContent> > m_listRecords;
297
298 };
299
300 ////////////////////////////////////////////////////////////
301 // the forms
302 ////////////////////////////////////////////////////////////
303 enum FormVisibility { V_VALUE, V_NAMEVALUE, V_HIDDEN, V_HEADER, V_UNDEF, V_UNKNOWN };
304
305
306 /**a class used to store a form */
307 class FormType
308 {
309 public:
310 /** the constructor */
311 FormType()
312 : m_fieldId(-1)
313 , m_visible(V_UNKNOWN)
314 , m_font()
315 , m_backColor(0)
316 , m_extra("")
317 {
318 }
319
320 //! operator<<
321 friend std::ostream &operator<<(std::ostream &o, FormType const &form);
322
323 //! the associated field
324 int m_fieldId;
325
326 //! the form visibility
327 FormVisibility m_visible;
328
329 //! the font
330 MWAWFont m_font;
331
332 //! the backgroun color
333 int m_backColor;
334
335 //! two bdbox one for content and field name
336 MWAWBox2f m_bdbox[2];
337
338 //! extra data
339 std::string m_extra;
340 };
341
342 //! operator<<
343 std::ostream &operator<<(std::ostream &o, FormType const &form)
344 {
345 if (form.m_fieldId != -1) o << "Field" << form.m_fieldId << ",";
346 switch (form.m_visible) {
347 case V_VALUE:
348 o << "value,";
349 break;
350 case V_NAMEVALUE:
351 break;
352 case V_HIDDEN:
353 o << "hidden,";
354 break;
355 case V_HEADER:
356 o << "header,";
357 break;
358 case V_UNDEF:
359 o << "undef,";
360 break;
361 case V_UNKNOWN:
362 o << "unknown[visible],";
363 break;
364 #if !defined(__clang__)
365 default:
366 o << "###visible=" << form.m_visible << ",";
367 break;
368 #endif
369 }
370
371 if (form.m_visible == V_VALUE || form.m_visible == V_NAMEVALUE)
372 o << "bdbox(value)=" << form.m_bdbox[0] << ",";
373 if (form.m_visible == V_NAMEVALUE)
374 o << "bdbox(fName)=" << form.m_bdbox[1] << ",";
375 o << form.m_extra;
376 return o;
377 }
378 /** a class used to store a form */
379 class Form
380 {
381 public:
382 //! the constructor
383 Form()
384 : m_name("")
385 , m_listTypes()
386 {
387 }
388
389 //! operator<<
390 friend std::ostream &operator<<(std::ostream &o, Form const &form);
391
392 //! the form name
393 std::string m_name;
394
395 /** three bdbox : the last is the page bdbox*/
396 MWAWBox2f m_bdbox[3];
397
398 /** the list of fields type */
399 std::vector<FormType> m_listTypes;
400 };
401
402 //! operator<<
403 std::ostream &operator<<(std::ostream &o, Form const &form)
404 {
405 o << "name=\"" << form.m_name << "\",";
406 o << "bdbox0=" << form.m_bdbox[0] << ",";
407 o << "points=" << form.m_bdbox[1] << ",";
408 o << "bdbox(page)=" << form.m_bdbox[2] << ",";
409 return o;
410 }
411
412 /** a class used to store a list of forms */
413 class Forms
414 {
415 public:
416 /** the constructor */
417 Forms()
418 : m_numForms(0)
419 , m_font()
420 , m_backColor(0)
421 , m_listForms()
422 , m_extra("")
423 {
424 }
425 //! operator<<
426 friend std::ostream &operator<<(std::ostream &o, Forms const &form);
427 /** the number of forms */
428 int m_numForms;
429
430 /** the default font */
431 MWAWFont m_font;
432
433 /** the default font back color */
434 int m_backColor;
435
436 /** two bdbox */
437 MWAWBox2f m_bdbox[2];
438
439 /** the list of forms */
440 std::vector<Form> m_listForms;
441 /** extra data */
442 std::string m_extra;
443 };
444
445 //! operator<<
446 std::ostream &operator<<(std::ostream &o, Forms const &form)
447 {
448 if (form.m_backColor) o << "#bkCol?=" << form.m_backColor << ",";
449 o << "bdbox0=" << form.m_bdbox[0] << ",";
450 o << "bdbox1=" << form.m_bdbox[1] << ",";
451 o << "nForms=" << form.m_numForms << ",";
452 o << form.m_extra;
453 return o;
454 }
455 ////////////////////////////////////////
456 //! Internal: the state of a MsWksDBParser
457 struct State {
458 //! constructor
459 State()
460 : m_database()
461 , m_widthCols()
462 , m_forms()
463 , m_numReports()
464 , m_actPage(0)
465 , m_numPages(0)
466 , m_pageLength(-1)
467 {
468 }
469
470 //! the database
471 DataBase m_database;
472 /** the column size in pixels(?) */
473 std::vector<int> m_widthCols;
474 /** the forms */
475 Forms m_forms;
476 /** the number of report */
477 int m_numReports;
478
479 int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */;
480
481 //! the page length in point (if known)
482 int m_pageLength;
483 };
484
485 ////////////////////////////////////////
486 //! Internal: the subdocument of a MsWksDBParser
487 class SubDocument final : public MWAWSubDocument
488 {
489 public:
490 SubDocument(MsWksDBParser &pars, MWAWInputStreamPtr const &input, int zoneId)
491 : MWAWSubDocument(&pars, input, MWAWEntry())
492 , m_id(zoneId)
493 {
494 }
495
496 //! destructor
497 ~SubDocument() final {}
498
499 //! operator!=
500 bool operator!=(MWAWSubDocument const &doc) const final;
501
502 //! the parser function
503 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
504
505 protected:
506 /** the subdocument id*/
507 int m_id;
508 };
509
510 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/)
511 {
512 if (!listener.get()) {
513 MWAW_DEBUG_MSG(("MsWksDBParser::SubDocument::parse: no listener\n"));
514 return;
515 }
516
517 long pos = m_input->tell();
518 //MsWksDBParser *parser = static_cast<MsWksDBParser *>(m_parser);
519 //parser->sendNote(m_id);
520 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
521 }
522
523 bool SubDocument::operator!=(MWAWSubDocument const &doc) const
524 {
525 if (MWAWSubDocument::operator!=(doc)) return true;
526 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
527 if (!sDoc) return true;
528 if (m_id != sDoc->m_id) return true;
529 return false;
530 }
531 }
532
533
534 ////////////////////////////////////////////////////////////
535 // constructor/destructor, ...
536 ////////////////////////////////////////////////////////////
537 MsWksDBParser::MsWksDBParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
538 : MWAWSpreadsheetParser(input, rsrcParser, header)
539 , m_state()
540 , m_listZones()
541 , m_document()
542 {
543 MWAWInputStreamPtr mainInput=input;
544 if (!input) return;
545 if (input->isStructured()) {
546 MWAWInputStreamPtr mainOle = input->getSubStreamByName("MN0");
547 if (mainOle)
548 mainInput=mainOle;
549 }
550 m_document.reset(new MsWksDocument(mainInput, *this));
551 init();
552 }
553
554 MsWksDBParser::~MsWksDBParser()
555 {
556 }
557
558 void MsWksDBParser::init()
559 {
560 resetSpreadsheetListener();
561 setAsciiName("main-1");
562
563 m_state.reset(new MsWksDBParserInternal::State);
564
565 // reduce the margin (in case, the page is not defined)
566 getPageSpan().setMargins(0.1);
567 }
568
569 ////////////////////////////////////////////////////////////
570 // the parser
571 ////////////////////////////////////////////////////////////
572 void MsWksDBParser::parse(librevenge::RVNGSpreadsheetInterface *docInterface)
573 {
574 if (!m_document || !m_document->getInput() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
575 bool ok = true;
576 try {
577 #ifdef DEBUG
578 MWAWInputStreamPtr &input= getInput();
579 if (input->isStructured()) {
580 std::shared_ptr<MWAWOLEParser> oleParser(new MWAWOLEParser("MN0", getParserState()->m_fontConverter, 3));
581 oleParser->parse(input);
582 }
583 #endif
584 // create the asciiFile
585 m_document->initAsciiFile(asciiName());
586
587 checkHeader(nullptr);
588 ok=createZones();
589 if (ok) {
590 createDocument(docInterface);
591 sendDatabase();
592 }
593 }
594 catch (...) {
595 MWAW_DEBUG_MSG(("MsWksDBParser::parse: exception catched when parsing\n"));
596 ok = false;
597 }
598 m_document->ascii().reset();
599
600 resetSpreadsheetListener();
601 if (!ok) throw(libmwaw::ParseException());
602 }
603
604 ////////////////////////////////////////////////////////////
605 // create the document
606 ////////////////////////////////////////////////////////////
607 void MsWksDBParser::createDocument(librevenge::RVNGSpreadsheetInterface *documentInterface)
608 {
609 if (!documentInterface) return;
610 if (getSpreadsheetListener()) {
611 MWAW_DEBUG_MSG(("MsWksDBParser::createDocument: listener already exist\n"));
612 return;
613 }
614
615 std::vector<MWAWPageSpan> pageList;
616 m_state->m_actPage = 0;
617 m_document->getPageSpanList(pageList, m_state->m_numPages);
618 MWAWSpreadsheetListenerPtr listen(new MWAWSpreadsheetListener(*getParserState(), pageList, documentInterface));
619 setSpreadsheetListener(listen);
620 listen->startDocument();
621 // time to send page information the graph parser and the text parser
622 m_document->getGraphParser()->setPageLeftTop
623 (MWAWVec2f(72.f*float(getPageSpan().getMarginLeft()),
624 72.f*float(getPageSpan().getMarginTop())+m_document->getHeaderFooterHeight(true)));
625 }
626
627
628 ////////////////////////////////////////////////////////////
629 //
630 // Intermediate level
631 //
632 ////////////////////////////////////////////////////////////
633 bool MsWksDBParser::createZones()
634 {
635 if (getInput()->isStructured())
636 m_document->createOLEZones(getInput());
637 MWAWInputStreamPtr input = m_document->getInput();
638 libmwaw::DebugFile &ascFile = m_document->ascii();
639 if (!readDataBase()) return false;
640 long pos = input->tell();
641 if (!input->isEnd() && !readForms()) {
642 MWAW_DEBUG_MSG(("MsWksDBParser::createZones: can not read the forms\n"));
643 ascFile.addPos(pos);
644 ascFile.addNote("Entries(DBForm):###");
645 return true;
646 }
647 pos = input->tell();
648 if (!input->isEnd() && !readReports()) {
649 MWAW_DEBUG_MSG(("MsWksDBParser::createZones: can not read the report\n"));
650 ascFile.addPos(pos);
651 ascFile.addNote("Entries(DBReport):###");
652 return true;
653 }
654 auto &typeZoneMap=m_document->getTypeZoneMap();
655 // fixme: getNewZoneId here
656 int mainId=MsWksDocument::Z_MAIN;
657 typeZoneMap.insert(std::multimap<int,MsWksDocument::Zone>::value_type
658 (MsWksDocument::Z_MAIN,MsWksDocument::Zone(MsWksDocument::Z_MAIN, mainId)));
659 pos = input->tell();
660 if (input->isEnd() || input->readLong(2)!=0)
661 input->seek(pos, librevenge::RVNG_SEEK_SET);
662 else {
663 MWAWEntry group;
664 group.setId(mainId);
665 group.setName("RBDR");
666 if (!m_document->m_graphParser->readRB(input,group,1)) {
667 MWAW_DEBUG_MSG(("MsWksDBParser::createZones: can not read RBDR group\n"));
668 ascFile.addPos(pos+2);
669 ascFile.addNote("Entries(RBDR):###");
670 input->seek(pos, librevenge::RVNG_SEEK_SET);
671 }
672 }
673
674 // normally, the file is now parsed, let check for potential remaining zones
675 while (!input->isEnd()) {
676 MWAW_DEBUG_MSG(("MsWksDBParser::createZones: find some unexpected data\n"));
677 pos = input->tell();
678 MsWksDocument::Zone unknown;
679 if (!m_document->readZone(unknown) || input->tell() <= pos) {
680 ascFile.addPos(pos);
681 ascFile.addNote("Entries(End)");
682 ascFile.addPos(pos+100);
683 ascFile.addNote("_");
684 break;
685 }
686 }
687
688 // ok, prepare the data
689 m_state->m_numPages = 1;
690 std::vector<int> linesH, pagesH;
691 m_document->getGraphParser()->computePositions(mainId, linesH, pagesH);
692 return true;
693 }
694
695 ////////////////////////////////////////////////////////////
696 // read a database zone
697 ////////////////////////////////////////////////////////////
698 bool MsWksDBParser::readDataBase()
699 {
700 m_state->m_database=MsWksDBParserInternal::DataBase();
701
702 MWAWInputStreamPtr input=m_document->getInput();
703 int const vers=version();
704 long pos = input->tell();
705 int headerSize = vers <= 2 ? 14 : 80;
706 long endHeader = pos+headerSize;
707 if (!input->checkPosition(endHeader)) {
708 MWAW_DEBUG_MSG(("MsWksDBParser::readDataBase: Segment A is too short\n"));
709 return false;
710 }
711 libmwaw::DebugFile &ascFile = m_document->ascii();
712 libmwaw::DebugStream f;
713 f << "Entries(DataBase):";
714 m_state->m_database.m_numFields=static_cast<int>(input->readLong(2));
715 m_state->m_database.m_numRecords=static_cast<int>(input->readLong(2));
716 f << "nFields=" << m_state->m_database.m_numFields << ",";
717 f << "nRecord=" << m_state->m_database.m_numRecords << ",";
718 int val;
719 if (vers <= 2) {
720 auto numReports = static_cast<int>(input->readLong(2)); // 0 or 1
721 m_state->m_numReports = numReports;
722 if (numReports) f << "nReports=" << numReports << ",";
723 }
724 f << "unk1=[";
725 for (int i = 0; i < 2; i++) f << input->readLong(1) << ",";
726 f << "],";
727
728 if (vers > 2) {
729 auto numReports = static_cast<int>(input->readLong(2)); // 0 or 1
730 auto unkn0 = static_cast<int>(input->readLong(2));
731 auto unkn1 = static_cast<int>(input->readLong(2));
732 auto numForms = static_cast<int>(input->readLong(2)); // 1 or 2
733 auto actualForms = static_cast<int>(input->readLong(2)); // 1 or 2
734
735 m_state->m_numReports = numReports;
736 m_state->m_forms.m_numForms = numForms>actualForms ? numForms:actualForms;
737 if (numReports) f << "nReports=" << numReports << ",";
738 if (numForms) f << "nForms=" << numForms << ",";
739 if (actualForms) f << "aForm=" << actualForms << ",";
740 f << "unkn2=[" << unkn0 << "," << unkn1 << "],";
741
742 }
743 /* I find: f0: numReports, f3=numForms or f4=actualForms
744 unk=[0,1,],f1=1,f2=7,f3=1,f4=1,
745 unk=[0,4,],f2=6,f3=1,f4=1,
746 unk=[0,4,],f2=7,f3=1,f4=1,
747 unk=[3,1,],f1=1,f2=7,f3=1,f4=1,
748 */
749 int nbElt = int(endHeader - input->tell())/2;
750 for (int i = 0; i < nbElt; i++) {
751 val = static_cast<int>(input->readLong(2));
752 if (val) f << "f" << i << "=" << val << ",";
753 }
754
755
756 input->seek(endHeader, librevenge::RVNG_SEEK_SET);
757 ascFile.addPos(pos);
758 ascFile.addNote(f.str().c_str());
759
760 if (vers <= 2) {
761 if (!readFormV2()) return false;
762 m_state->m_forms.m_numForms = static_cast<int>(m_state->m_forms.m_listForms.size());
763
764 if (!readFieldTypesV2()) return false;
765 if (!readUnknownV2()) return false;
766
767 /* the formula*/
768 pos = input->tell();
769 if (!readFormula()) {
770 input->seek(pos, librevenge::RVNG_SEEK_SET);
771 auto sz = static_cast<int>(input->readLong(2));
772 if (!input->checkPosition(pos+2+sz)) {
773 input->seek(pos, librevenge::RVNG_SEEK_SET);
774 MWAW_DEBUG_MSG(("MsWksDBParser::readDataBase: Segment default is too short\n"));
775 return false;
776 }
777 ascFile.addPos(pos);
778 ascFile.addNote("Entries(Formula):###");
779 input->seek(pos+2+sz, librevenge::RVNG_SEEK_SET);
780 }
781 pos = input->tell();
782 if (m_state->m_numReports && !readReportV2()) {
783 input->seek(pos, librevenge::RVNG_SEEK_SET);
784 auto sz = static_cast<int>(input->readLong(2));
785 if (!input->checkPosition(pos+2+sz)) {
786 input->seek(pos, librevenge::RVNG_SEEK_SET);
787 MWAW_DEBUG_MSG(("MsWksDBParser::readDataBase: Segment Form is too short\n"));
788 return false;
789 }
790
791 ascFile.addPos(pos);
792 ascFile.addNote("Entries(DBReport):###");
793
794 input->seek(pos+2+sz, librevenge::RVNG_SEEK_SET);
795 }
796
797 if (!readRecords(false)) return false;
798 return true;
799 }
800
801 long debRecord = input->tell();
802 if (!readRecords(true)) return false;
803 if (!readFieldTypes()) return false;
804
805 // ok, we can now read the record
806 pos = input->tell();
807 input->seek(debRecord, librevenge::RVNG_SEEK_SET);
808 if (!readRecords(false)) return false;
809 input->seek(pos, librevenge::RVNG_SEEK_SET);
810
811 pos = input->tell();
812 f.str("");
813 f << "Entries(DBUnkn0):";
814 // an odd segment empty content or with f0=12,f1=12,f14=8000
815 val = static_cast<int>(input->readLong(2));
816 if (val) f << "##unkn=" << val << ",";
817
818 auto sz=long(input->readULong(2));
819 f << "sz=" << sz << ",";
820
821 if (!input->checkPosition(pos+4+sz)) {
822 input->seek(pos, librevenge::RVNG_SEEK_SET);
823 MWAW_DEBUG_MSG(("MsWksDBParser::readDataBase: Segment Unkn0 is too short\n"));
824 return false;
825 }
826 for (long i = 0; i < sz/2; i++) {
827 val = static_cast<int>(input->readULong(2));
828 if (!val) continue;
829 if (i > 3) f << "f" << i << "=" << std::hex << val << std::dec << ",";
830 else f << "f" << i << "=" << val << ",";
831 }
832 ascFile.addPos(pos);
833 ascFile.addNote(f.str().c_str());
834
835 input->seek(pos+sz+4, librevenge::RVNG_SEEK_SET);
836
837 int numData = vers==3 ? 4 : 6;
838 for (int i = 0; i < numData; i++) {
839 pos = input->tell();
840 f.str("");
841 bool ok = false;
842 switch (i) {
843 case 0:
844 ok = readFormula();
845 break;
846 case 1:
847 ok=readDefaultValues();
848 break;
849 case 2:
850 if (vers == 4) ok = readSerialFormula();
851 else ok = readFilters();
852 break;
853 case 3:
854 if (vers == 4) ok = readFilters();
855 break;
856 case 4:
857 // normally a classic version 2 document entry
858 ok = input->readLong(2)==0 && m_document->readDocumentInfo(long(input->readULong(2)));
859 break;
860 case 5: // probably similar to case 3(v3), but I always find a size=0
861 break;
862 default:
863 MWAW_DEBUG_MSG(("MsWksDBParser::readDataBase: find some unknown type\n"));
864 f << "##type=" << i << ",";
865 }
866
867 if (ok) continue;
868
869 input->seek(pos, librevenge::RVNG_SEEK_SET);
870 if (input->readLong(2) != 0) {
871 input->seek(-2, librevenge::RVNG_SEEK_CUR);
872 break;
873 }
874 sz = long(input->readULong(2));
875
876 if (!input->checkPosition(pos+4+sz)) {
877 input->seek(pos, librevenge::RVNG_SEEK_SET);
878 break;
879 }
880 ascFile.addPos(pos);
881 if (sz)
882 f << "Entries(DBUnkn1)[" << i << "]:";
883 else
884 f << "_";
885 ascFile.addNote(f.str().c_str());
886 }
887
888 return true;
889 }
890
891 ////////////////////////////////////////////////////////////
892 // The form
893 ////////////////////////////////////////////////////////////
894 bool MsWksDBParser::readFormV2()
895 {
896 if (version() > 2) return false;
897 if (!readColSize(m_state->m_widthCols)) return false;
898
899 MWAWInputStreamPtr input=m_document->getInput();
900 long pos = input->tell(), sz;
901 libmwaw::DebugFile &ascFile = m_document->ascii();
902 libmwaw::DebugStream f;
903
904 sz = 0x1E2;
905 if (!input->checkPosition(pos+sz)) {
906 input->seek(pos, librevenge::RVNG_SEEK_SET);
907 MWAW_DEBUG_MSG(("MsWksDBParser::readFormV2 segment size is odd\n"));
908 return false;
909 }
910
911 f << "Entries(DBForm):";
912 for (int i = 0; i < 3; i++) { // always 0
913 auto val = static_cast<int>(input->readLong(2));
914 if (val) f << "f" << i << "=" << val << ",";
915 }
916 ascFile.addPos(pos);
917 ascFile.addNote(f.str().c_str());
918
919 auto const &colSize = m_state->m_widthCols;
920 size_t numColSize = colSize.size();
921 MsWksDBParserInternal::Form form;
922 for (size_t elt = 0; elt < 60; elt++) {
923 MsWksDBParserInternal::FormType fType;
924 pos = input->tell();
925 f.str("");
926 float dim[2];
927 for (auto &d : dim) d= float(input->readLong(2))/72.0f;
928
929 // constant size ?
930 float normSize[] = { 96.f/72.f, 16.f/72.f };
931 fType.m_bdbox[1] = MWAWBox2f(MWAWVec2f(dim[0],dim[1]), MWAWVec2f(dim[0]+normSize[0], dim[1]+normSize[1]));
932
933 float fieldDim = elt < numColSize ? float(colSize[elt])/72.f : normSize[0];
934 fType.m_bdbox[0] = MWAWBox2f(MWAWVec2f(dim[0]+normSize[0],dim[1]),
935 MWAWVec2f(dim[0]+normSize[0]+fieldDim, dim[1]+normSize[1]));
936
937
938 auto val = static_cast<int>(input->readULong(2));
939 if (val) // a dim, some flag ?
940 f << "#unkn=" << std::hex << val << "," << std::dec;
941 // I find followed by 0x2800 or 0x5000
942 auto what = static_cast<int>(input->readULong(2));
943
944 fType.m_extra=f.str();
945 f.str("");
946 f << "DBForm(Type:" << elt << "):";
947 if (what) {
948 // ok we keep it
949 fType.m_visible = MsWksDBParserInternal::V_NAMEVALUE;
950 fType.m_fieldId = int(elt)+1;
951 form.m_listTypes.push_back(fType);
952 f << fType << "what=" << std::hex << what << std::dec << ",";
953 }
954 else
955 f << "unused,";
956
957 ascFile.addPos(pos);
958 ascFile.addNote(f.str().c_str());
959 }
960 m_state->m_forms.m_listForms.push_back(form);
961
962 return true;
963 }
964
965
966 bool MsWksDBParser::readForms()
967 {
968 if (version() <= 2) return false;
969
970 if (!readColSize(m_state->m_widthCols)) return false;
971
972 MWAWInputStreamPtr input=m_document->getInput();
973 long pos = input->tell();
974 libmwaw::DebugFile &ascFile = m_document->ascii();
975 libmwaw::DebugStream f;
976
977 long endPos = pos+44;
978 if (!input->checkPosition(endPos)) {
979 MWAW_DEBUG_MSG(("MsWksDBParser::readForms: first zone is too short\n"));
980 return false;
981 }
982
983 auto &forms = m_state->m_forms;
984 // I find f2=[0|1], f5=[0|8|24|169|201|260],f7=1, f8=1
985 for (int i = 0; i < 9; i++) {
986 auto val = static_cast<int>(input->readLong(2));
987 if (val) f << "f" << i << "=" << val << ",";
988 }
989 MWAWFont font;
990 font.setId(static_cast<int>(input->readULong(2)));
991 font.setSize(float(input->readLong(2)));
992 auto val = static_cast<int>(input->readLong(2));
993 if (val) f << "#unkn=" << val << ",";
994
995 int col[2];
996 for (auto &c : col) c = static_cast<int>(input->readULong(1));
997 forms.m_backColor = col[0];
998 MWAWColor color;
999 if (col[1] != 0xFF && m_document->getColor(col[1], color, 3))
1000 font.setColor(color);
1001 // some flags ?
1002 f << "fFlags?=" << input->readULong(1) << ","; // always 1
1003 val = static_cast<int>(input->readLong(1)); // always 0
1004 if (val) f << "#unkn1=" << val << ",";
1005 forms.m_font = font;
1006
1007 for (auto &bd : forms.m_bdbox) { // two bdbox
1008 float dim[4];
1009 for (auto &d : dim) d = float(input->readLong(2))/1440.f;
1010 bd = MWAWBox2f(MWAWVec2f(dim[0],dim[1]), MWAWVec2f(dim[2], dim[3]));
1011 }
1012 forms.m_extra=f.str();
1013
1014 f.str("");
1015 f << "Entries(DBForm):" << forms;
1016 f << "font=[" << forms.m_font.getDebugString(getParserState()->m_fontConverter) << "],";
1017 ascFile.addPos(pos);
1018 ascFile.addNote(f.str().c_str());
1019
1020 // report form can have 0 entry, so..
1021 if (input->isEnd()) return true;
1022
1023 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1024 for (int i = 0; i < forms.m_numForms; i++) {
1025 if (!readForm()) return false;
1026 }
1027 return true;
1028 }
1029
1030 bool MsWksDBParser::readForm()
1031 {
1032 MWAWInputStreamPtr input=m_document->getInput();
1033 int const vers=version();
1034 long pos = input->tell();
1035 libmwaw::DebugFile &ascFile = m_document->ascii();
1036 libmwaw::DebugStream f, f2;
1037
1038 int expLength = (vers == 3) ? 108 : 84;
1039
1040 long endPos = pos+expLength;
1041 if (!input->checkPosition(endPos)) {
1042 MWAW_DEBUG_MSG(("MsWksDBParser::readForm: first zone is too short\n"));
1043 return false;
1044 }
1045
1046 auto &forms = m_state->m_forms;
1047 auto numForm = static_cast<int>(forms.m_listForms.size());
1048 MsWksDBParserInternal::Form form;
1049
1050 auto val = static_cast<int>(input->readLong(2));
1051 if (val) f << "unkn=" << val << ",";
1052 f << "f0=" << input->readLong(2) << ",";
1053
1054 auto sz = long(input->readULong(1));
1055 if (sz > 31) {
1056 input->seek(pos, librevenge::RVNG_SEEK_SET);
1057 MWAW_DEBUG_MSG(("MsWksDBParser::readForm: field name is too long\n"));
1058 return false;
1059 }
1060 std::string name("");
1061 for (long i = 0; i < sz; i++) name += char(input->readULong(1));
1062 form.m_name = name;
1063
1064 input->seek(pos+4+32, librevenge::RVNG_SEEK_SET);
1065
1066 float dim[4];
1067 for (int bd = 0; bd < 2; bd++) { // two bdbox
1068 for (auto &d : dim) d = float(input->readLong(2))/1440.f;
1069 form.m_bdbox[bd] = MWAWBox2f(MWAWVec2f(dim[0],dim[1]), MWAWVec2f(dim[2], dim[3]));
1070 }
1071
1072 if (vers == 4) {
1073 // [16|1d], [348a|a412|b169] : headerId?
1074 f << "unk2=[" << input->readLong(2) << ",";
1075 f << std::hex << input->readULong(2) << std::dec << "],";
1076 }
1077
1078 // page bdbox
1079 for (float &i : dim) i = float(input->readLong(2))/72.f;
1080 form.m_bdbox[2] = MWAWBox2f(MWAWVec2f(dim[0],dim[1]), MWAWVec2f(dim[2], dim[3]));
1081
1082 int numData = int(endPos-input->tell())/2;
1083 // g3 = [1,2,3], g4=[0,1,7], g6=[0,2,6], g7=[0,103,104,206]
1084 // g4 = 1 : header ?
1085 for (int i = 0; i < numData; i++) {
1086 val = static_cast<int>(input->readLong(2));
1087 if (val) f << "g" << i << "=" << std::hex << val << std::dec << ",";
1088 }
1089
1090 f2 << "DBForm-" << numForm << ":" << form << "," << f.str();
1091
1092 ascFile.addPos(pos);
1093 ascFile.addNote(f2.str().c_str());
1094 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1095
1096 if (!readFormTypes(form)) return false;
1097 forms.m_listForms.push_back(form);
1098
1099 // a form document entry followed
1100 pos = input->tell();
1101 if (input->readLong(2) != 0 || !m_document->readDocumentInfo(long(input->readULong(2)))) {
1102 MWAW_DEBUG_MSG(("MsWksDBParser::readForm: can not find document info\n"));
1103 ascFile.addPos(pos);
1104 ascFile.addNote("DBForm:end###");
1105 return false;
1106 }
1107
1108 if (vers == 3) { // version3 file have 4 int here
1109 for (int i = 0; i < 2; i++) {
1110 pos = input->tell();
1111 f.str("");
1112 sz = long(input->readULong(4));
1113 endPos = pos+4+sz;
1114
1115 if (!input->checkPosition(endPos)) {
1116 input->seek(pos, librevenge::RVNG_SEEK_SET);
1117 MWAW_DEBUG_MSG(("MsWksDBParser::readForm: Segment v3 is too long\n"));
1118 break;
1119 }
1120 f << "DBForm(v3-" << i << "):";
1121 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1122 ascFile.addPos(pos);
1123 ascFile.addNote(f.str().c_str());
1124 }
1125 }
1126 // finally the list of graphics
1127 MWAWEntry group;
1128 group.setId(1);
1129 group.setName("RBDR");
1130
1131 if (!m_document->m_graphParser->readRB(input,group,1)) {
1132 MWAW_DEBUG_MSG(("MsWksDBParser::readForm: can not read RBDR group\n"));
1133 ascFile.addPos(pos);
1134 ascFile.addNote("Entries(RBDR):###");
1135 return false;
1136 }
1137
1138 return true;
1139 }
1140
1141 bool MsWksDBParser::readFormTypes(MsWksDBParserInternal::Form &form)
1142 {
1143 form.m_listTypes.resize(0);
1144
1145 int const vers=version();
1146 MWAWInputStreamPtr input=m_document->getInput();
1147 long pos = input->tell();
1148 libmwaw::DebugFile &ascFile = m_document->ascii();
1149 libmwaw::DebugStream f;
1150
1151 int expDataSize = (vers == 3) ? 28 : 42;
1152
1153 int numFields = m_state->m_database.m_numFields;
1154 int numTypes = vers == 3 ? 254 : numFields;
1155 if (vers==4 && numTypes<4) numTypes=4; // at least 4?
1156
1157 if (!input->checkPosition(pos+expDataSize*numTypes)) {
1158 MWAW_DEBUG_MSG(("MsWksDBParser::readFormTypes: is too short\n"));
1159 return false;
1160 }
1161
1162 for (int nType = 0; nType < numTypes; nType++) {
1163 MsWksDBParserInternal::FormType fType;
1164 auto visible = MsWksDBParserInternal::V_UNKNOWN;
1165 pos = input->tell();
1166 f.str("");
1167
1168 // in v3 fls[0] = 30=hidden ?, 40 = empty, 50=unused, 70=visible ? 80=selection
1169 // in v4 fls[0] = 38, 58, 78 ?
1170 // fls[1] seems constant in each file 0, a4, b3
1171 auto flag= static_cast<int>(input->readULong(1));
1172 int hiFlag = flag >> 4, lowFlag = flag & 0xF;
1173 if (hiFlag & 0x8) f << "[sel]"; // the selection
1174 switch (hiFlag & 7) {
1175 case 3:
1176 visible = MsWksDBParserInternal::V_VALUE;
1177 break;
1178 case 4:
1179 visible = MsWksDBParserInternal::V_UNDEF;
1180 break;
1181 case 5:
1182 visible = MsWksDBParserInternal::V_HIDDEN;
1183 break;
1184 case 7:
1185 visible = MsWksDBParserInternal::V_NAMEVALUE;
1186 break;
1187 case 0:
1188 if (nType == 0) {
1189 visible = MsWksDBParserInternal::V_HEADER;
1190 break;
1191 }
1192 MWAW_FALLTHROUGH;
1193 default:
1194 f << "##unkn[visible]=" << hiFlag << ",";
1195 break;
1196 }
1197 fType.m_visible = visible;
1198
1199 if (vers == 4) {
1200 if ((lowFlag & 8) != 8 && nType != 0) f << "###flagNoV4,";
1201 lowFlag &= 7;
1202 }
1203 if (lowFlag) f << "###VisFlag=" << lowFlag << ",";
1204 f << "flg=" << std::hex << input->readULong(1) << std::dec << ",";
1205
1206 // font,sz
1207 MWAWFont font;
1208 font.setId(static_cast<int>(input->readULong(2)));
1209 font.setSize(float(input->readULong(2)));
1210 auto fontColor = static_cast<int>(input->readULong(1));
1211 auto backColor = static_cast<int>(input->readULong(1));
1212 auto fontFlags = static_cast<int>(input->readULong(1));
1213 uint32_t fflags = 0;
1214 if (fontFlags) { // checkme
1215 if (fontFlags & 0x1) fflags |= MWAWFont::boldBit;
1216 if (fontFlags & 0x2) fflags |= MWAWFont::italicBit;
1217 if (fontFlags & 0x4) font.setUnderlineStyle(MWAWFont::Line::Simple);
1218 if (fontFlags & 0x8) fflags |= MWAWFont::embossBit;
1219 if (fontFlags & 0x10) fflags |= MWAWFont::shadowBit;
1220 if (fontFlags &0xE0) f << "fFlags=" << std::hex << (fontFlags &0xE0) << std::dec << ",";
1221 }
1222 font.setFlags(fflags);
1223
1224 MWAWColor color;
1225 if (fontColor != 255 && m_document->getColor(fontColor, color, 3))
1226 font.setColor(color);
1227 fType.m_font = font;
1228 fType.m_backColor = backColor;
1229
1230 auto val = static_cast<int>(input->readLong(1));
1231 // font flags
1232 if (val != fontFlags) f << "#fFlagsView=" << val << ",";
1233
1234 if (vers == 4) {
1235 // always 0101 ?
1236 f << "unkn1=" << std::hex << input->readLong(2) << std::dec << ",";
1237
1238 /** [ff,5c,_,-8,_,-9] or [ff,a8,_,-8,_,-9] or [_,5c,_,-8,_,-9]**/
1239 f << "unkn2=[";
1240 for (int i = 0; i < 2; i++) {
1241 val = static_cast<int>(input->readULong(1));
1242 if (val) f << std::hex << val << std::dec << ",";
1243 else f << "_,";
1244 }
1245 for (int i = 0; i < 4; i++) {
1246 val = static_cast<int>(input->readLong(2));
1247 if (val) f << val << ",";
1248 else f << "_,";
1249 }
1250 f << "],";
1251 }
1252
1253 float dim[4];
1254 for (auto &bd : fType.m_bdbox) { // two bdbox
1255 for (auto &d : dim) d = float(input->readLong(2))/72.f;
1256 bd = MWAWBox2f(MWAWVec2f(dim[1],dim[0]), MWAWVec2f(dim[3], dim[2]));
1257 }
1258
1259 // 0, 0 in v3, in v4 : two small number < 6
1260 for (int i = 0; i < 2; i++) {
1261 val = static_cast<int>(input->readLong(1));
1262 if (val) f << "g" << i << "=" << val << ",";
1263 }
1264
1265
1266 if (vers == 4)
1267 f << "unkn3=" << std::hex << input->readULong(2) << std::dec;
1268
1269 if (visible == MsWksDBParserInternal::V_NAMEVALUE ||
1270 visible == MsWksDBParserInternal::V_VALUE) {
1271 // ok we keep it
1272 fType.m_fieldId = nType;
1273 form.m_listTypes.push_back(fType);
1274 }
1275
1276 input->seek(pos+expDataSize, librevenge::RVNG_SEEK_SET);
1277
1278 libmwaw::DebugStream f2;
1279 f2 << "DBForm(Type:" << nType << "):";
1280 // sometimes all types are filed, sometimes only the used fields
1281 if (nType < numFields) {
1282 f2 << fType;
1283 f2 << "font=[" << fType.m_font.getDebugString(getParserState()->m_fontConverter) << "],";
1284 f2 << f.str();
1285 }
1286 ascFile.addPos(pos);
1287 ascFile.addNote(f2.str().c_str());
1288 }
1289
1290 return true;
1291 }
1292
1293 ////////////////////////////////////////////////////////////
1294 // The records
1295 ////////////////////////////////////////////////////////////
1296
1297 // reads the fields name and the fields value :
1298 bool MsWksDBParser::readRecords(bool onlyCheck)
1299 {
1300 int numRecord = m_state->m_database.m_numRecords;
1301 int const vers=version();
1302 if (numRecord < 0) {
1303 MWAW_DEBUG_MSG(("MsWksDBParser::readRecords: numRecords can not be negatifn"));
1304 return false;
1305 }
1306
1307 MWAWInputStreamPtr input=m_document->getInput();
1308
1309 long pos = input->tell();
1310 auto &listFields = m_state->m_database.m_listFieldTypes;
1311 auto numFieldsHeader = static_cast<int>(listFields.size());
1312 int numFields = m_state->m_database.m_numFields; // sometimes there is another field
1313
1314 if (!onlyCheck && numFieldsHeader+1 < numFields) {
1315 MWAW_DEBUG_MSG(("MsWksDBParser::readRecords: Warning database.listFieldTypes is too short, some fields will be loose\n"));
1316 }
1317
1318 libmwaw::DebugFile &ascFile = m_document->ascii();
1319 libmwaw::DebugStream f;
1320 int ptrSize = vers <= 2 ? 1 : 2;
1321 auto sz = long(input->readULong(2*ptrSize));
1322 f.str("");
1323 f << "Entries(FieldName): sz = " << sz;
1324 long endPos = pos+2*ptrSize+sz;
1325
1326 if (!input->checkPosition(endPos)) {
1327 input->seek(pos, librevenge::RVNG_SEEK_SET);
1328 MWAW_DEBUG_MSG(("MsWksDBParser::readRecords: Segment FieldName is too short\n"));
1329 return false;
1330 }
1331
1332 if (!onlyCheck) {
1333 ascFile.addPos(pos);
1334 ascFile.addNote(f.str().c_str());
1335 }
1336
1337 // une liste de string
1338 for (int nField = 0; nField <= numFields; nField++) {
1339 pos = input->tell();
1340 f.str("");
1341 auto sLen = static_cast<int>(input->readULong(1));
1342 if (sLen == 255 && nField == numFields) {
1343 input->seek(-1, librevenge::RVNG_SEEK_CUR);
1344 break;
1345 }
1346 if (pos+sLen > endPos-ptrSize) {
1347 MWAW_DEBUG_MSG(("MsWksDBParser::readRecords: error reading segment FieldName\n"));
1348 input->seek(-1, librevenge::RVNG_SEEK_CUR);
1349 return false;
1350 }
1351 std::string str;
1352 for (int i = 0; i < sLen; i++) str+= char(input->readULong(1));
1353
1354 if (nField == numFields) {
1355 MWAW_DEBUG_MSG(("MsWksDBParser::readRecords: WARNING: numFields must be increased to correspond to the fName's list\n"));
1356 numFields++;
1357 m_state->m_database.m_numFields++;
1358 }
1359 if (onlyCheck) continue;
1360
1361 if (nField+1 < numFieldsHeader)
1362 listFields[size_t(nField+1)].m_name = str;
1363 else {
1364 static bool first = true;
1365 if (first) {
1366 MWAW_DEBUG_MSG(("MsWksDBParser::readRecords: warning FieldName=%d ignored\n", nField));
1367 first = false;
1368 }
1369 }
1370 f << "FieldName-" << nField << "=" << str;
1371 ascFile.addPos(pos);
1372 ascFile.addNote(f.str().c_str());
1373
1374 }
1375
1376 pos = input->tell();
1377 bool ok = input->readULong(1) == 255;
1378 if (ok && ptrSize == 2) ok = input->readULong(1) == 0;
1379 if (ok && input->tell()<endPos) {
1380 ascFile.addDelimiter(input->tell(),'#');
1381 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1382 }
1383
1384 if (!ok || input->tell() != endPos) {
1385 input->seek(pos, librevenge::RVNG_SEEK_SET);
1386 MWAW_DEBUG_MSG(("MsWksDBParser::readRecords: End of Segment FieldName is odd\n"));
1387 return false;
1388 }
1389
1390 if (!onlyCheck) {
1391 ascFile.addDelimiter(pos,'|');
1392 m_state->m_database.m_listRecords.resize(size_t(numRecord));
1393 }
1394
1395 for (int rec = 0; rec < numRecord; rec++) {
1396 pos = input->tell();
1397 f.str("");
1398
1399 f << "Entries(DBRecord)["<< rec << "]:";
1400 sz =long(input->readULong(2*ptrSize));
1401 f << "sz=" << sz << ",";
1402
1403 endPos = pos+2*ptrSize+sz;
1404 if (!input->checkPosition(endPos)) {
1405 input->seek(pos, librevenge::RVNG_SEEK_SET);
1406 break;
1407 }
1408
1409 std::vector<MWAWCellContent> *row = nullptr;
1410 if (!onlyCheck) {
1411 row = &m_state->m_database.m_listRecords[size_t(rec)];
1412 row->resize(size_t(numFields));
1413
1414 ascFile.addPos(pos);
1415 ascFile.addNote(f.str().c_str());
1416 }
1417 for (int nField = 0; nField < numFields; nField++) {
1418 pos = input->tell();
1419 f.str("");
1420 f << "DBRecord["<< rec << "-" << nField << "]:";
1421
1422 auto fSz = static_cast<int>(input->readULong(1));
1423 // only for v2 or can we find it for v2 or v3 ?
1424 if (fSz == 254) {
1425 auto skip = static_cast<int>(input->readLong(1));
1426 if (skip > 0 && skip+nField < numFields) {
1427 nField+=skip-1;
1428 f << "skip=" << skip;
1429 ascFile.addPos(pos);
1430 ascFile.addNote(f.str().c_str());
1431 continue;
1432 }
1433 }
1434 else if (fSz == 255) {
1435 input->seek(-1, librevenge::RVNG_SEEK_CUR);
1436 break;
1437 }
1438 long ePos = pos+1+fSz;
1439 if (ePos > endPos) {
1440 MWAW_DEBUG_MSG(("MsWksDBParser::readRecords: Record Content is too short\n"));
1441 input->seek(-1, librevenge::RVNG_SEEK_CUR);
1442 return false;
1443 }
1444 if (onlyCheck) {
1445 input->seek(ePos, librevenge::RVNG_SEEK_SET);
1446 continue;
1447 }
1448
1449 ok = false;
1450 f.str("");
1451 f << "DBRecord["<< rec << "-" << nField << "]:";
1452 MWAWCellContent record;
1453 if (fSz == 0) ok = true;
1454 else if (nField < numFieldsHeader) {
1455 double value;
1456 bool isNan;
1457 std::string textValue;
1458 if (listFields[size_t(nField)].getFormat().m_format == MWAWCell::F_TEXT) {
1459 record.m_textEntry.setBegin(pos+1);
1460 record.m_textEntry.setLength(fSz);
1461 record.m_contentType=MWAWCellContent::C_TEXT;
1462 ok = m_document->readDBString(ePos, textValue);
1463 }
1464 else if (m_document->readDBNumber(ePos, value, isNan, textValue)) {
1465 record.setValue(value);
1466 record.m_contentType=MWAWCellContent::C_NUMBER;
1467 f << value << ",";
1468 ok=true;
1469 }
1470 if (!textValue.empty()) f << "\"" << textValue << "\",";
1471 }
1472
1473 if (!ok) {
1474 f << "###";
1475 static bool first = true;
1476 if (first) {
1477 MWAW_DEBUG_MSG(("MsWksDBParser::readRecords: warning Record=%d:%d ignored\n", rec, nField));
1478 first = false;
1479 }
1480 input->seek(ePos, librevenge::RVNG_SEEK_SET);
1481 }
1482
1483 (*row)[size_t(nField)] = record;
1484
1485 f << record;
1486 ascFile.addPos(pos);
1487 ascFile.addNote(f.str().c_str());
1488 }
1489
1490 pos = input->tell();
1491 ok = input->readULong(1) == 255;
1492 if (ok && ptrSize == 2) {
1493 auto val=static_cast<int>(input->readULong(1));
1494 if (!onlyCheck && val != 0) {
1495 // it is related to the selection : 0x80=filter, 0x40=match
1496 ascFile.addPos(pos);
1497 f.str("");
1498 f << "DBRecord["<< rec << "]:sel=" << std::hex << val << std::dec;
1499 ascFile.addNote(f.str().c_str());
1500 }
1501 }
1502
1503 if (ok && input->tell()<endPos) {
1504 ascFile.addDelimiter(input->tell(),'#');
1505 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1506 }
1507 if (!ok || input->tell()!=endPos) {
1508 input->seek(pos, librevenge::RVNG_SEEK_SET);
1509 MWAW_DEBUG_MSG(("MsWksDBParser::readRecords: End of Record is odd\n"));
1510 return false;
1511 }
1512 if (!onlyCheck) ascFile.addDelimiter(pos,'|');
1513 }
1514
1515 return true;
1516 }
1517
1518 ////////////////////////////////////////
1519 // list of the fields type
1520 ////////////////////////////////////////
1521 bool MsWksDBParser::readFieldTypes()
1522 {
1523 MWAWInputStreamPtr input=m_document->getInput();
1524
1525 long pos = input->tell();
1526
1527 libmwaw::DebugFile &ascFile = m_document->ascii();
1528 libmwaw::DebugStream f;
1529
1530 f << "Entries(FieldType):";
1531 auto val = static_cast<int>(input->readLong(2));
1532 if (val) f << "##unkn=" << val << ",";
1533
1534 auto sz =long(input->readULong(2));
1535 f << "sz=" << sz << ",";
1536 long endFieldTypes = pos+4+sz;
1537
1538 if (sz != 255*14 || !input->checkPosition(endFieldTypes)) {
1539 input->seek(pos, librevenge::RVNG_SEEK_SET);
1540 MWAW_DEBUG_MSG(("MsWksDBParser::readFieldTypes: Segment length is odd %ld\n", sz));
1541 return false;
1542 }
1543 ascFile.addPos(pos);
1544 ascFile.addNote(f.str().c_str());
1545
1546 auto &listFields = m_state->m_database.m_listFieldTypes;
1547 listFields.resize(255);
1548 for (size_t elt = 0; elt < 255; elt++) {
1549 MsWksDBParserInternal::FieldType fieldType;
1550 MWAWCell::Format format;
1551 MWAWCellContent &content=fieldType.m_content;
1552 f.str("");
1553 pos = input->tell();
1554 val = static_cast<int>(input->readULong(2));
1555 int type = (val>>13) & 3;
1556 int inputType = (val>>6) & 0x3;
1557 int subformat = ((val>>8) & 0x7);
1558 format.m_digits = (val>>2) & 0xF;
1559 switch ((val&3)) {
1560 case 0:
1561 fieldType.setHAlignment(MWAWCell::HALIGN_LEFT);
1562 break;
1563 case 1:
1564 fieldType.setHAlignment(MWAWCell::HALIGN_RIGHT);
1565 break;
1566 case 2:
1567 fieldType.setHAlignment(MWAWCell::HALIGN_CENTER);
1568 break;
1569 default:
1570 f << "#align=3,";
1571 break;
1572 }
1573 val &= 0x9800;
1574
1575 switch (type) {
1576 case 0:
1577 content.m_contentType=MWAWCellContent::C_TEXT;
1578 format.m_format=MWAWCell::F_TEXT;
1579 break;
1580 case 1:
1581 format.m_format=MWAWCell::F_NUMBER;
1582 format.m_numberFormat=MWAWCell::F_NUMBER_GENERIC;
1583 content.m_contentType=MWAWCellContent::C_NUMBER;
1584 switch (subformat) { // checkme
1585 case 0:
1586 break;
1587 case 1:
1588 format.m_numberFormat=MWAWCell::F_NUMBER_DECIMAL;
1589 break;
1590 case 2:
1591 format.m_numberFormat=MWAWCell::F_NUMBER_CURRENCY;
1592 break;
1593 case 3:
1594 format.m_thousandHasSeparator=true;
1595 break;
1596 case 4:
1597 format.m_numberFormat=MWAWCell::F_NUMBER_CURRENCY;
1598 format.m_thousandHasSeparator=true;
1599 break;
1600 case 5:
1601 format.m_numberFormat=MWAWCell::F_NUMBER_SCIENTIFIC;
1602 break;
1603 case 6:
1604 format.m_numberFormat=MWAWCell::F_NUMBER_PERCENT;
1605 break;
1606 case 7:
1607 format.m_numberFormat=MWAWCell::F_NUMBER_PERCENT;
1608 format.m_thousandHasSeparator=true;
1609 break;
1610 default:
1611 f << "subform=##unkn" << subformat << ",";
1612 break;
1613 }
1614 subformat=0;
1615 break;
1616 case 2:
1617 format.m_format=MWAWCell::F_DATE;
1618 content.m_contentType=MWAWCellContent::C_NUMBER;
1619 if (subformat>=0 && subformat < 5) { // checkme
1620 static char const *wh[]= {"%m/%d/%y", "%b %d, %Y", "%b, %d", "%b, %Y", "%a, %d %b, %Y" };
1621 format.m_DTFormat=wh[subformat];
1622 subformat=0;
1623 }
1624 break;
1625 case 3:
1626 format.m_format=MWAWCell::F_TIME;
1627 content.m_contentType=MWAWCellContent::C_NUMBER;
1628 if (subformat >= 0 && subformat < 4) { // checkme
1629 static char const *wh[]= {"%I:%M:%S %p", "%I:%M %p", "%H:%M:%S", "%H:%M"};
1630 format.m_DTFormat=wh[subformat];
1631 subformat=0;
1632 }
1633 break;
1634 default:
1635 break;
1636 }
1637 switch (inputType) {
1638 default:
1639 case 0:
1640 break; /* none */
1641 case 1:
1642 fieldType.m_isSerial=true;
1643 content.m_contentType=MWAWCellContent::C_FORMULA;
1644 break;
1645 case 2:
1646 content.m_contentType=MWAWCellContent::C_FORMULA;
1647 break;
1648 case 3:
1649 f << "###inputType(unknown),";
1650 break;
1651 }
1652 if (subformat) f << "format[extra]=#" << subformat << ",";
1653 MWAWFont font;
1654 font.setId(static_cast<int>(input->readULong(2)));
1655 font.setSize(float(input->readULong(2)));
1656 auto fontUnkn = static_cast<int>(input->readLong(2)); // 0x200 and in one field 0x300
1657 auto fontFlags = static_cast<int>(input->readLong(1));
1658 auto fColor = static_cast<int>(input->readULong(1));
1659 auto unkn = static_cast<int>(input->readULong(1)); // always 0 ? back color?
1660 uint32_t fflags = 0;
1661 if (fontFlags) { // checkme
1662 if (fontFlags & 0x1) fflags |= MWAWFont::boldBit;
1663 if (fontFlags & 0x2) fflags |= MWAWFont::italicBit;
1664 if (fontFlags & 0x4) font.setUnderlineStyle(MWAWFont::Line::Simple);
1665 if (fontFlags & 0x8) fflags |= MWAWFont::embossBit;
1666 if (fontFlags & 0x10) fflags |= MWAWFont::shadowBit;
1667 if (fontFlags &0xE0) f << "fFlags=" << std::hex << (fontFlags &0xE0) << std::dec << ",";
1668 }
1669 font.setFlags(fflags);
1670 MWAWColor color;
1671 if (fColor != 255 && m_document->getColor(fColor, color, 3))
1672 font.setColor(color);
1673 fieldType.setFont(font);
1674 fieldType.m_height = static_cast<int>(input->readULong(1));
1675 fieldType.m_serialId = static_cast<int>(input->readULong(1));
1676 auto what = static_cast<int>(input->readLong(1));
1677 switch (what) {
1678 case 0:
1679 fieldType.m_used=false;
1680 break;
1681 case 0x60:
1682 break;
1683 default:
1684 f << "##unknWhat=" << std::hex << what << std::dec << ",";
1685 }
1686 if (fontUnkn != 0x200) f << "##unkn0="<< fontUnkn << ",";
1687 if (unkn) f << "###unkn1="<< unkn << ",";
1688 if (val) f << "###flags=" << val;
1689 fieldType.m_extra=f.str();
1690 fieldType.setFormat(format);
1691
1692 f.str("");
1693 if (elt) {
1694 f << "FieldType-" << elt-1 << ":" << fieldType;
1695 listFields[elt-1] = fieldType;
1696 }
1697 else
1698 f << "FieldType-def:" << fieldType;
1699 f << "font=[" << font.getDebugString(getParserState()->m_fontConverter) << "],";
1700
1701 ascFile.addPos(pos);
1702 ascFile.addNote(f.str().c_str());
1703 }
1704
1705 input->seek(endFieldTypes, librevenge::RVNG_SEEK_SET);
1706 return true;
1707 }
1708
1709 bool MsWksDBParser::readFieldTypesV2()
1710 {
1711 MWAWInputStreamPtr input=m_document->getInput();
1712
1713 long pos = input->tell();
1714
1715 libmwaw::DebugFile &ascFile = m_document->ascii();
1716 libmwaw::DebugStream f;
1717
1718 f << "Entries(FieldType):";
1719
1720 long sz =60*4+4;
1721 long endFieldTypes = pos+sz;
1722
1723 if (!input->checkPosition(endFieldTypes)) {
1724 input->seek(pos, librevenge::RVNG_SEEK_SET);
1725 MWAW_DEBUG_MSG(("MsWksDBParser::readFieldTypesV2: Segment length is odd %ld\n", sz));
1726 return false;
1727 }
1728 for (int i = 0; i < 2; i++)
1729 f << input->readLong(2) << ",";
1730 ascFile.addPos(pos);
1731 ascFile.addNote(f.str().c_str());
1732
1733
1734 auto &listFields = m_state->m_database.m_listFieldTypes;
1735 listFields.resize(61);
1736 for (int elt = 0; elt < 60; elt++) {
1737 MsWksDBParserInternal::FieldType fieldType;
1738 auto &content=fieldType.m_content;
1739 MWAWCell::Format format;
1740 f.str("");
1741 pos = input->tell();
1742 auto val = static_cast<int>(input->readULong(2));
1743 int type = (val>>14) & 3;
1744 format.m_digits = (val>>8) & 0xF;
1745 int subFormat = val & 7;
1746 switch (((val>>5) & 3)) {
1747 case 0:
1748 fieldType.setHAlignment(MWAWCell::HALIGN_LEFT);
1749 break;
1750 case 1:
1751 fieldType.setHAlignment(MWAWCell::HALIGN_CENTER);
1752 break;
1753 case 2:
1754 fieldType.setHAlignment(MWAWCell::HALIGN_RIGHT);
1755 break;
1756 default:
1757 f << "#align=3,";
1758 break;
1759 }
1760 if (val & 0x18) {
1761 MWAWFont font;
1762 if (val & 8) font.setUnderlineStyle(MWAWFont::Line::Simple);
1763 if (val & 0x10) font.setFlags(MWAWFont::boldBit);
1764 fieldType.setFont(font);
1765 }
1766 if (val & 0x1000) f << "comma,";
1767 val &= 0x2080;
1768 if (val) f << "#f0=" << std::hex << val << std::dec << ",";
1769 switch (type) {
1770 default:
1771 case 0:
1772 format.m_format=MWAWCell::F_TEXT;
1773 content.m_contentType=MWAWCellContent::C_TEXT;
1774 break;
1775 case 1:
1776 format.m_format=MWAWCell::F_NUMBER;
1777 format.m_numberFormat=MWAWCell::F_NUMBER_GENERIC;
1778 content.m_contentType=MWAWCellContent::C_NUMBER;
1779 break;
1780 case 2:
1781 format.m_format=MWAWCell::F_NUMBER;
1782 format.m_numberFormat=MWAWCell::F_NUMBER_GENERIC;
1783 content.m_contentType=MWAWCellContent::C_FORMULA;
1784 break;
1785 case 3:
1786 f << "##unknType,";
1787 break;
1788 }
1789 auto unkn = static_cast<int>(input->readULong(1)); // always 0 ?
1790 if (unkn) f << "##unkn=" << std::hex << unkn << std::dec << ",";
1791 auto what = static_cast<int>(input->readULong(1));
1792
1793 if (format.m_format==MWAWCell::F_NUMBER) {
1794 bool ok = true;
1795 switch (subFormat) {
1796 case 0:
1797 format.m_numberFormat=MWAWCell::F_NUMBER_DECIMAL;
1798 break;
1799 case 1:
1800 format.m_numberFormat=MWAWCell::F_NUMBER_CURRENCY;
1801 break;
1802 case 2:
1803 format.m_numberFormat=MWAWCell::F_NUMBER_PERCENT;
1804 break;
1805 case 3:
1806 break;
1807 case 4:
1808 format.m_numberFormat=MWAWCell::F_NUMBER_SCIENTIFIC;
1809 break;
1810 case 5:
1811 format.m_format=MWAWCell::F_DATE;
1812 format.m_DTFormat="%m/%d/%y";
1813 break;
1814 case 6: {
1815 format.m_format=MWAWCell::F_DATE;
1816 static char const *wh[]= {"%B %d, %Y", "%b %d, %Y", "%A, %d %B, %Y", "%a, %d %b, %Y" };
1817 format.m_DTFormat=wh[((what>>5)&3)];
1818 what &= 0x9F;
1819 break;
1820 }
1821 case 7:
1822 format.m_format=MWAWCell::F_TIME;
1823 break;
1824 default:
1825 ok = false;
1826 }
1827 if (ok) subFormat = 0;
1828 }
1829 if (subFormat) f << "#format[extra]=" << subFormat << ",";
1830 if (what != 0x80)
1831 f << "##unknWhat=" << std::hex << what << std::dec << ",";
1832 fieldType.m_extra=f.str();
1833 fieldType.setFormat(format);
1834
1835 f.str("");
1836 f << "FieldType-" << elt << ":" << fieldType;
1837 listFields[size_t(elt)] = fieldType;
1838
1839 ascFile.addPos(pos);
1840 ascFile.addNote(f.str().c_str());
1841 }
1842
1843 input->seek(endFieldTypes, librevenge::RVNG_SEEK_SET);
1844 return true;
1845 }
1846
1847 ////////////////////////////////////////////////////////////
1848 // the filters
1849 ////////////////////////////////////////////////////////////
1850 bool MsWksDBParser::readFilters()
1851 {
1852 MWAWInputStreamPtr input=m_document->getInput();
1853
1854 long pos = input->tell();
1855 if (input->readLong(2) != 0) {
1856 MWAW_DEBUG_MSG(("MsWksDBParser::readFilters: size is odd\n"));
1857 return false;
1858 }
1859
1860 libmwaw::DebugFile &ascFile = m_document->ascii();
1861 libmwaw::DebugStream f;
1862 f << "Entries(Filter):";
1863
1864 auto sz = long(input->readULong(2));
1865 f << "sz=" << sz << ",";
1866
1867 long endPos = pos+4+sz;
1868 if ((sz%514) != 8 || !input->checkPosition(endPos)) {
1869 input->seek(pos, librevenge::RVNG_SEEK_SET);
1870 MWAW_DEBUG_MSG(("MsWksDBParser::readFilters: Segment length is odd %ld\n", sz));
1871 return false;
1872 }
1873
1874 auto numFilters = int(sz/514);
1875 f << "numFilters=" << numFilters << ",";
1876
1877 auto val = static_cast<int>(input->readLong(2));
1878 if (val != numFilters) f << "###val=" << val << ",";
1879 for (int i = 0; i < 3; i++) f << input->readLong(2) << ",";
1880
1881 ascFile.addPos(pos);
1882 ascFile.addNote(f.str().c_str());
1883
1884 for (int filt = 0; filt < numFilters ; filt++) {
1885 pos = input->tell();
1886 f.str("");
1887 f << "Filter-" << filt << ":";
1888
1889 sz = long(input->readLong(1));
1890 if (sz <= 0 || sz > 31) {
1891 input->seek(pos, librevenge::RVNG_SEEK_SET);
1892 MWAW_DEBUG_MSG(("MsWksDBParser::readFilters: filter name length is odd %ld\n", sz));
1893 return false;
1894 }
1895 std::string name("");
1896 for (long i = 0; i < sz; i++)
1897 name += char(input->readULong(1));
1898 f << "name=\"" << name << "\",";
1899 input->seek(pos+32, librevenge::RVNG_SEEK_SET);
1900
1901 val = static_cast<int>(input->readLong(1));
1902 switch (val) {
1903 case 0:
1904 break;
1905 case 1:
1906 f << "inverted,";
1907 break;
1908 default:
1909 f << "###unknOp=" << val<<",";
1910 break;
1911 }
1912 val = static_cast<int>(input->readLong(1));
1913 if (val) f << "###unkn=" << val;
1914
1915 ascFile.addPos(pos);
1916 ascFile.addNote(f.str().c_str());
1917
1918 for (int i = 0; i < 6; i++) {
1919 pos = input->tell();
1920 f.str("");
1921 f << "Filter" << filt << "-" << i << ":";
1922
1923 auto field = static_cast<int>(input->readLong(1));
1924 auto op = static_cast<int>(input->readLong(1));
1925 auto op2 = static_cast<int>(input->readLong(1));
1926
1927 if (op < 0 || op >= 14 || op2 <= 0 || op2 > 2) {
1928 input->seek(pos, librevenge::RVNG_SEEK_SET);
1929 MWAW_DEBUG_MSG(("MsWksDBParser::readFilters: unknown op=%d op2=%d\n", op, op2));
1930 return false;
1931 }
1932
1933 ascFile.addDelimiter(input->tell(), '|');
1934 input->seek(pos+16, librevenge::RVNG_SEEK_SET);
1935 ascFile.addDelimiter(input->tell(), '|');
1936 sz = input->readLong(1);
1937 if (sz < 0 || sz > 63) {
1938 input->seek(pos, librevenge::RVNG_SEEK_SET);
1939 MWAW_DEBUG_MSG(("MsWksDBParser::readFilters: val length is odd:%ld\n", sz));
1940 return false;
1941 }
1942
1943 if (sz || op == 3 || op == 13) {
1944 std::string value;
1945 for (long c = 0; c < sz; c++) value += char(input->readLong(1));
1946 if (i) {
1947 if (op2==1) f << "And ";
1948 else f << "Or ";
1949 }
1950 f << "Field" << field;
1951 const char *opName[] = {
1952 "eq", "neq", "gt", "isBlank(not)", "geq", "lt", "leq", "contains",
1953 "contains(not)", "ends with", "ends with(not)",
1954 "begins with", "begins with(not)", "isBlank"
1955 };
1956 f << " " << opName[op] << " " << value;
1957 }
1958 input->seek(pos+80, librevenge::RVNG_SEEK_SET);
1959 ascFile.addPos(pos);
1960 ascFile.addNote(f.str().c_str());
1961 }
1962 }
1963
1964 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1965 return true;
1966 }
1967
1968 ////////////////////////////////////////////////////////////
1969 // The report
1970 ////////////////////////////////////////////////////////////
1971 bool MsWksDBParser::readReports()
1972 {
1973 if (m_state->m_numReports == 0) return true;
1974
1975 int const vers=version();
1976 MWAWInputStreamPtr input=m_document->getInput();
1977 long pos;
1978 libmwaw::DebugFile &ascFile = m_document->ascii();
1979 libmwaw::DebugStream f;
1980
1981 int const numData=vers==4 ? 7 : 5;
1982 for (int rep = 0; rep < m_state->m_numReports; rep++) {
1983 for (int step = 0; step < numData; step++) {
1984 // 0 header
1985 // 1 list of field
1986 // 2-3 ?
1987 // 4 documentEntry
1988 // 5-6 RBDR
1989 pos = input->tell();
1990 f.str("");
1991 if (input->isEnd()) break;
1992 long sz = input->readLong(4);
1993 if (sz == -1) sz = 0; // seems normal
1994 if (!input->checkPosition(pos+4+sz)) {
1995 input->seek(pos, librevenge::RVNG_SEEK_SET);
1996 MWAW_DEBUG_MSG(("MsWksDBParser::readReports: report%d-%d is too long\n",rep,step));
1997 return false;
1998 }
1999
2000 input->seek(pos, librevenge::RVNG_SEEK_SET);
2001 switch (step) {
2002 case 0:
2003 if (readReportHeader()) continue;
2004 break;
2005 case 1:
2006 if (vers == 3) {
2007 if (input->readLong(2) != 0 || !m_document->readDocumentInfo(long(input->readULong(2))))
2008 break;
2009 continue;
2010 }
2011 break;
2012 case 4:
2013 if (vers == 4) {
2014 if (input->readLong(2) != 0 || !m_document->readDocumentInfo(long(input->readULong(2))))
2015 break;
2016 continue;
2017 }
2018 break;
2019 default: {
2020 if (step < 4) break;
2021 MWAWEntry group;
2022 group.setId(1);
2023 group.setName("RBDR");
2024
2025 if (m_document->m_graphParser->readRB(input,group,1)) continue;
2026 break;
2027 }
2028 }
2029
2030 long endPos = pos+4+sz;
2031 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
2032 if (step == 0) f << "Entries(DBReport):";
2033 else f << "DBReport-" << step << ":";
2034 ascFile.addPos(pos);
2035 ascFile.addNote(f.str().c_str());
2036
2037 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2038 }
2039 if (vers == 4 || input->isEnd())
2040 continue;
2041
2042 // this seems to be followed by a form: incomplete?
2043 pos=input->tell();
2044 if (!readForms()) {
2045 MWAW_DEBUG_MSG(("MsWksDBParser::readReports: can not read forms size\n"));
2046 ascFile.addPos(pos);
2047 ascFile.addNote("DBReport-forms:###");
2048 input->seek(pos, librevenge::RVNG_SEEK_SET);
2049 return false;
2050 }
2051 }
2052 return true;
2053 }
2054
2055 ////////////////////
2056 ////////////////////
2057 bool MsWksDBParser::readReportHeader()
2058 {
2059 if (version() != 4) return false;
2060 MWAWInputStreamPtr input=m_document->getInput();
2061 long pos = input->tell();
2062 libmwaw::DebugFile &ascFile = m_document->ascii();
2063 libmwaw::DebugStream f;
2064
2065 auto sz = long(input->readULong(4));
2066 long endPos = pos+4+sz;
2067 if (sz < 32 || !input->checkPosition(endPos)) {
2068 input->seek(pos, librevenge::RVNG_SEEK_SET);
2069 MWAW_DEBUG_MSG(("MsWksDBParser: readReportHeader: segment size is odd\n"));
2070 return false;
2071 }
2072
2073 f << "Entries(DBReport):";
2074 auto val=static_cast<int>(input->readLong(1));
2075 if (val < 0 || val > 31) {
2076 input->seek(pos, librevenge::RVNG_SEEK_SET);
2077 MWAW_DEBUG_MSG(("MsWksDBParser: readReportHeader: name size is odd=%d\n", val));
2078 return false;
2079 }
2080 std::string name("");
2081 for (int i = 0; i < val; i++) name += char(input->readULong(1));
2082 f << "\"" << name << "\",";
2083 input->seek(pos+4+32, librevenge::RVNG_SEEK_SET);
2084
2085 val = static_cast<int>(input->readLong(2));
2086 if (val) f << "###unkn0=" << val << ",";
2087
2088 // font,sz
2089 MWAWFont font;
2090 font.setId(static_cast<int>(input->readULong(2)));
2091 font.setSize(float(input->readULong(2)));
2092 auto fontColor = static_cast<int>(input->readULong(1));
2093 auto backColor = static_cast<int>(input->readULong(1));
2094 auto fontFlags = static_cast<int>(input->readULong(1));
2095 uint32_t fflags = 0;
2096 if (fontFlags) { // checkme
2097 if (fontFlags & 0x1) fflags |= MWAWFont::boldBit;
2098 if (fontFlags & 0x2) fflags |= MWAWFont::italicBit;
2099 if (fontFlags & 0x4) font.setUnderlineStyle(MWAWFont::Line::Simple);
2100 if (fontFlags & 0x8) fflags |= MWAWFont::embossBit;
2101 if (fontFlags & 0x10) fflags |= MWAWFont::shadowBit;
2102 if (fontFlags &0xE0) f << "fFlags=" << std::hex << (fontFlags &0xE0) << std::dec << ",";
2103 }
2104 font.setFlags(fflags);
2105
2106 MWAWColor color;
2107 if (fontColor != 255 && m_document->getColor(fontColor, color, 3))
2108 font.setColor(color);
2109 f << "font=[" << font.getDebugString(getParserState()->m_fontConverter);
2110 if (backColor) f << "backcolor=" << backColor;
2111 f << "],";
2112 val = static_cast<int>(input->readLong(1));
2113 // font flags
2114 if (val != fontFlags) f << "#fFlagsView=" << val << ",";
2115
2116 // a bdbox ?
2117 float dim[4];
2118 for (auto &d : dim) d = float(input->readLong(2))/72.f;
2119 f << "bdbox(title)=" << MWAWBox2f(MWAWVec2f(dim[0],dim[1]), MWAWVec2f(dim[2], dim[3])) << ",";
2120
2121 f << "unk0=["; // always _, _, 1, _, _, 1
2122 for (int i = 0; i < 6; i++) {
2123 val = static_cast<int>(input->readLong(1));
2124 if (val) f << val << ",";
2125 else f << "_,";
2126 }
2127 f << "],pos?=[";
2128 for (int i = 0; i < 2; i++) f << float(input->readLong(2))/1440.f << ",";
2129 f << "],";
2130 // checkMe
2131 auto numReportData = static_cast<int>(input->readLong(2));
2132 if (numReportData) f << "numReportData=" << numReportData << ",";
2133 for (int i = 0; i < 2; i++) dim[i] = float(input->readLong(2))/1440.f;
2134 bool hasPos = dim[0]>0 || dim[1]>0;
2135 if ((hasPos && numReportData== 0) || (!hasPos && numReportData)) {
2136 MWAW_DEBUG_MSG(("MsWksDBParser: readReportHeader: potential problem with ReportData definition\n"));
2137 f << "###ReportData,";
2138 }
2139 if (hasPos) f << "posReportData?=[" << dim[0] << "," << dim[1] << "],";
2140 f << "unk1=["; // always [_,_,_,_,_,_,_,1,_,_,_,_,] ?
2141 for (int i = 0; i < 12; i++) {
2142 val = static_cast<int>(input->readLong(1));
2143 if (val) f << val << ",";
2144 else f << "_,";
2145 }
2146 f << "],";
2147
2148 // another bdbox : related to data size
2149 for (auto &d : dim) d = float(input->readLong(2))/1440.f;
2150 if (dim[0]>0 || dim[1]>0 || dim[2]>0 || dim[3]>0)
2151 f << "bdbox2=" << MWAWBox2f(MWAWVec2f(dim[0],dim[1]), MWAWVec2f(dim[2], dim[3])) << ",";
2152
2153 f << "unk2=["; // always [_,_,_,_,_,_,_,_] ?
2154 for (int i = 0; i < 8; i++) {
2155 val = static_cast<int>(input->readLong(1));
2156 if (val) f << val << ",";
2157 else f << "_,";
2158 }
2159 f << "],";
2160
2161 // 3 other bdbox ?
2162 for (int bd = 0; bd < 3; bd++) {
2163 for (auto &d : dim) d = float(input->readLong(2))/72.f;
2164 f << "bdbox" << 3+bd << "=" << MWAWBox2f(MWAWVec2f(dim[0],dim[1]), MWAWVec2f(dim[2], dim[3])) << ",";
2165 }
2166
2167 f << "unk3=["; // always [_,_,_,_,_,_,_,_,_, ...]
2168 for (int i = 0; i < 16; i++) {
2169 val = static_cast<int>(input->readLong(1));
2170 if (val) f << val << ",";
2171 else f << "_,";
2172 }
2173 f << "],";
2174
2175 f << "unk4=["; // 1f,7929 or 2b,a3ca
2176 for (int i = 0; i < 2; i++) f << std::hex << input->readULong(2) << std::dec << ",";
2177 f << "],";
2178 for (auto &d : dim) d = float(input->readLong(2))/1440.f;
2179 f << "bdbox6" <<"=" << MWAWBox2f(MWAWVec2f(dim[0],dim[1]), MWAWVec2f(dim[2], dim[3])) << ",";
2180
2181 for (int bd = 0; bd < 3; bd++) {
2182 for (auto &d : dim) d = float(input->readLong(2))/72.f;
2183 f << "bdboxII(" << bd << ")=" << MWAWBox2f(MWAWVec2f(dim[0],dim[1]), MWAWVec2f(dim[2], dim[3])) << ",";
2184 val = static_cast<int>(input->readLong(2));
2185 if (val) {
2186 switch (bd) {
2187 case 0:
2188 f << "#pos=" << float(val)/72.f << ",";
2189 break;
2190 case 1:
2191 f << "mainYBegin=" << float(val)/72.f << ",";
2192 break;
2193 case 2:
2194 f << "mainYEnd=" << float(val)/72.f << ",";
2195 break;
2196 default:
2197 f << "##pos=" << val << ",";
2198 }
2199 }
2200 f << ",";
2201 }
2202 for (int bd = 0; bd < 3; bd++) {
2203 for (auto &d : dim) d = float(input->readLong(2))/72.f;
2204 f << "bdboxIII(" << bd << ")=" << MWAWBox2f(MWAWVec2f(dim[0],dim[1]), MWAWVec2f(dim[2], dim[3])) <<",";
2205 }
2206 val = static_cast<int>(input->readLong(2));
2207 if (val) f << "#unkn1=" << val << ",";
2208 f << "pos(III)=" << float(input->readLong(2))/72.f << ",";
2209 auto numReportData2 = static_cast<int>(input->readLong(2));
2210 if (numReportData2) f << "numReportData2=" << numReportData2 << ",";
2211 f << "unkn2=" << input->readLong(2) << ",";
2212 f << "id?=" << input->readLong(1) << ",";
2213 f << "unk5=[" << std::hex;
2214 for (int i = 0; i < 9; i++) {
2215 val = static_cast<int>(input->readULong(1));
2216 if (val) f << val << ",";
2217 else f << "_,";
2218 }
2219 f << std::dec << "],";
2220 f << "unk6=["; // 1f,7929 or 2b,a3ca
2221 for (int i = 0; i < 2; i++) f << std::hex << input->readULong(2) << std::dec << ",";
2222 f << "],";
2223 // g1 = 1
2224 for (int i = 0; i < 12; i++) {
2225 val = static_cast<int>(input->readULong(2));
2226 if (val) f << "g" << i << "=" << val << ",";
2227 }
2228
2229 ascFile.addPos(pos);
2230 ascFile.addNote(f.str().c_str());
2231
2232 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2233
2234 return true; // 2, 5
2235 }
2236
2237 bool MsWksDBParser::readReportV2()
2238 {
2239 if (version() > 2) return false;
2240
2241 MWAWInputStreamPtr input=m_document->getInput();
2242 long pos = input->tell();
2243 libmwaw::DebugFile &ascFile = m_document->ascii();
2244 libmwaw::DebugStream f;
2245
2246 auto sz = static_cast<int>(input->readLong(2));
2247 long endPos = pos+2+sz;
2248 if (sz < 490 || !input->checkPosition(endPos)) {
2249 MWAW_DEBUG_MSG(("MsWksDBParser::readReportV2: first zone is too short\n"));
2250 return false;
2251 }
2252
2253 f << "Entries(DBReport):";
2254 for (int i = 0; i < 2; i++) {
2255 auto val = static_cast<int>(input->readLong(2));
2256 if (val) f << "f" << i << "=" << val << ",";
2257 }
2258 ascFile.addPos(pos);
2259 ascFile.addNote(f.str().c_str());
2260
2261 pos = input->tell();
2262 std::vector<int> colSize;
2263 if (!readColSize(colSize)) {
2264 input->seek(pos, librevenge::RVNG_SEEK_SET);
2265 MWAW_DEBUG_MSG(("MsWksDBParser::readReportV2: can not read colSize\n"));
2266 return false;
2267 }
2268
2269 pos = input->tell();
2270 f.str("");
2271 f << "DBReport(I):";
2272 for (int i = 0; i < 119; i++) {
2273 auto val = static_cast<int>(input->readLong(2));
2274 if (val) f << "f" << i << "=" << val << ",";
2275 }
2276 ascFile.addPos(pos);
2277 ascFile.addNote(f.str().c_str());
2278
2279 pos = input->tell();
2280 if (!m_document->readDocumentInfo(0x15e)) {
2281 input->seek(pos, librevenge::RVNG_SEEK_SET);
2282 }
2283
2284 pos = input->tell();
2285 f.str("");
2286 f << "DBReport(II):";
2287 // 003e0003000911556e7469746c6564205265706f727420310000000000000000.....
2288 // int, int, int, name ?
2289 ascFile.addPos(pos);
2290 ascFile.addNote(f.str().c_str());
2291
2292 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2293 return true;
2294 }
2295
2296 ////////////////////////////////////////////////////////////
2297 ////////////////////////////////////////////////////////////
2298 bool MsWksDBParser::sendDatabase()
2299 {
2300 MWAWSpreadsheetListenerPtr listener=getSpreadsheetListener();
2301 if (!listener) {
2302 MWAW_DEBUG_MSG(("MsWksDBParser::sendDatabase: I can not find the listener\n"));
2303 return false;
2304 }
2305 MWAWInputStreamPtr input=m_document->getInput();
2306 auto const &database=m_state->m_database;
2307 auto const &fields = database.m_listFieldTypes;
2308 size_t numFields=fields.size();
2309 auto const &records=database.m_listRecords;
2310 size_t numRecords=records.size();
2311 listener->openSheet(database.convertInPoint(m_state->m_widthCols,76), librevenge::RVNG_POINT, std::vector<int>(), "Sheet0");
2312 int height=12;
2313 for (auto const &field : fields)
2314 if (field.m_height > height) height=field.m_height;
2315 for (size_t r=0; r<numRecords; ++r) {
2316 auto const &row=records[r];
2317 listener->openSheetRow(float(height), librevenge::RVNG_POINT);
2318 for (size_t c=0; c<row.size(); ++c) {
2319 if (c>=numFields) break;
2320 auto field=fields[c];
2321 field.updateWithContent(MWAWVec2i(int(c),int(r)), row[c]);
2322 if (field.empty()) continue;
2323
2324 MWAWCellContent const &content=field.m_content;
2325 listener->openSheetCell(field, content);
2326 if (content.m_contentType==MWAWCellContent::C_TEXT && content.m_textEntry.valid()) {
2327 listener->setFont(field.getFont());
2328 input->seek(content.m_textEntry.begin(), librevenge::RVNG_SEEK_SET);
2329 while (!input->isEnd() && input->tell()<content.m_textEntry.end()) {
2330 auto ch=static_cast<unsigned char>(input->readULong(1));
2331 if (ch==0xd)
2332 listener->insertEOL();
2333 else
2334 listener->insertCharacter(ch);
2335 }
2336 }
2337 listener->closeSheetCell();
2338 }
2339 listener->closeSheetRow();
2340 }
2341 listener->closeSheet();
2342 return true;
2343 }
2344
2345 ////////////////////////////////////////////////////////////
2346 //
2347 // Low level
2348 //
2349 ////////////////////////////////////////////////////////////
2350 // the formula
2351 bool MsWksDBParser::readFormula()
2352 {
2353 MWAWInputStreamPtr input=m_document->getInput();
2354 long pos = input->tell();
2355 int ptrSz = version() <= 2 ? 1 : 2;
2356 if (ptrSz == 2 && input->readLong(2) != 0) {
2357 MWAW_DEBUG_MSG(("MsWksDBParser::readFormula: size is odd\n"));
2358 return false;
2359 }
2360
2361 libmwaw::DebugFile &ascFile = m_document->ascii();
2362 libmwaw::DebugStream f;
2363 f << "Entries(Formula):";
2364
2365 auto sz =long(input->readULong(2));
2366 f << "sz=" << sz << ",";
2367
2368 long endPos = pos+2*ptrSz+sz;
2369 if (!input->checkPosition(endPos)) {
2370 input->seek(pos, librevenge::RVNG_SEEK_SET);
2371 MWAW_DEBUG_MSG(("MsWksDBParser::readFormula: Segment length is odd %ld\n", sz));
2372 return false;
2373 }
2374 ascFile.addPos(pos);
2375 ascFile.addNote(f.str().c_str());
2376
2377 int numFields = m_state->m_database.m_numFields;
2378 auto &listFields = m_state->m_database.m_listFieldTypes;
2379 auto numFieldsHeader = static_cast<int>(listFields.size());
2380
2381 if (numFieldsHeader < numFields) {
2382 MWAW_DEBUG_MSG(("MsWksDBParser::readFormula: the number of fields header are to small\n"));
2383 if (version() > 2) {
2384 input->seek(pos, librevenge::RVNG_SEEK_SET);
2385 return false;
2386 }
2387 listFields.resize(size_t(numFields)+1);
2388 }
2389 for (int nField = 0; nField < numFields; nField++) {
2390 pos = input->tell();
2391 if (pos+ptrSz == endPos) break;
2392 f.str("");
2393
2394 f << "Formula-" << nField << ":";
2395
2396 auto fVal = static_cast<int>(input->readLong(1));
2397 bool ok = false;
2398 if (fVal == -2) { // skip
2399 auto skip = static_cast<int>(input->readLong(1));
2400 if (skip > 0 && skip+nField < numFields) {
2401 nField+=skip-1;
2402 f << "skip=" << skip;
2403 ok = true;
2404 }
2405 }
2406 else if (fVal == -1) { // end
2407 input->seek(-1, librevenge::RVNG_SEEK_CUR);
2408 ascFile.addPos(pos);
2409 ascFile.addNote(f.str().c_str());
2410 break;
2411 }
2412 else if (fVal < 0); // error
2413 else if (fVal == 0) ok = true;
2414 else {
2415 auto &field = listFields[size_t(nField)];
2416 std::string extra;
2417 if (pos+1+fVal<=endPos && !field.m_isSerial && field.m_content.m_contentType==MWAWCellContent::C_FORMULA &&
2418 m_document->readFormula(pos+1+fVal, field.m_content, extra))
2419 f << field << extra;
2420 else {
2421 MWAW_DEBUG_MSG(("MsWksDBParser::readFormula: can not read formula for field:%d\n", nField));
2422 f << field << "###";
2423 }
2424 input->seek(pos+1+fVal, librevenge::RVNG_SEEK_SET);
2425 ok = true;
2426 }
2427 if (!ok || input->tell() > endPos) {
2428 input->seek(pos, librevenge::RVNG_SEEK_SET);
2429 MWAW_DEBUG_MSG(("MsWksDBParser::readFormula: can not read defValue for field:%d\n", nField));
2430 return false;
2431 }
2432 ascFile.addPos(pos);
2433 ascFile.addNote(f.str().c_str());
2434 }
2435 ascFile.addDelimiter(input->tell(),'|');
2436 bool ok = input->readULong(1) == 255;
2437 if (ptrSz==2 && ok) ok = input->readULong(1) == 0;
2438
2439 if (ok && input->tell()<endPos) {
2440 ascFile.addDelimiter(input->tell(),'#');
2441 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2442 }
2443 if (!ok || input->tell() != endPos) {
2444 input->seek(pos, librevenge::RVNG_SEEK_SET);
2445 MWAW_DEBUG_MSG(("MsWksDBParser::readFormula: End of Record is odd\n"));
2446 return false;
2447 }
2448
2449 return true;
2450 }
2451
2452 /** reads the serial formula */
2453 bool MsWksDBParser::readSerialFormula()
2454 {
2455 MWAWInputStreamPtr input=m_document->getInput();
2456
2457 long pos = input->tell();
2458 if (input->readLong(2) != 0) {
2459 MWAW_DEBUG_MSG(("MsWksDBParser::readSerialFormula: size is odd\n"));
2460 return false;
2461 }
2462
2463 libmwaw::DebugFile &ascFile = m_document->ascii();
2464 libmwaw::DebugStream f;
2465 f << "Entries(Serial):";
2466
2467 auto sz =long(input->readULong(2));
2468 f << "sz=" << sz << ",";
2469
2470 if ((sz%30) || !input->checkPosition(pos+2+sz)) {
2471 input->seek(pos, librevenge::RVNG_SEEK_SET);
2472 MWAW_DEBUG_MSG(("MsWksDBParser::readSerialFormula: Segment length is odd %ld\n", sz));
2473 return false;
2474 }
2475 ascFile.addPos(pos);
2476 ascFile.addNote(f.str().c_str());
2477
2478 auto nbSer = int(sz/30);
2479 std::vector<MsWksDBParserInternal::SerialFormula> listSerial;
2480 for (int ser = 0; ser < nbSer; ser++) {
2481 MsWksDBParserInternal::SerialFormula serial;
2482
2483 std::string prefSuff[2];
2484 pos = input->tell();
2485 f.str("");
2486 f << "Serial-" << ser << ":";
2487 for (int i = 0; i < 2; i++) {
2488 sz = long(input->readLong(1));
2489 if (sz < 0 || sz > 10) {
2490 MWAW_DEBUG_MSG(("MsWksDBParser::readSerialFormula: string size is too long:%ld\n", sz));
2491 return false;
2492 }
2493 prefSuff[i] = "";
2494 for (long l = 0; l < sz; l++)
2495 prefSuff[i] += char(input->readULong(1));
2496 input->seek(pos+11*(i+1), librevenge::RVNG_SEEK_SET);
2497 }
2498 serial.m_prefix = prefSuff[0];
2499 serial.m_suffix = prefSuff[1];
2500 serial.m_increment = long(input->readULong(2));
2501 // the initial value seems to 48 bytes,
2502 // but we can suppose that the high 32 bytes are
2503 // almost always 0...
2504 unsigned long orig = (input->readULong(2) << 16) << 16;
2505 orig += input->readULong(4);
2506 serial.m_nextValue = long(orig);
2507
2508 f << serial;
2509 listSerial.push_back(serial);
2510 ascFile.addPos(pos);
2511 ascFile.addNote(f.str().c_str());
2512 }
2513
2514 // ok, we can now associated the serial to the data type
2515 auto &listFields = m_state->m_database.m_listFieldTypes;
2516 #ifdef DEBUG
2517 int n=0;
2518 #endif
2519 for (auto &field : listFields) {
2520 #ifdef DEBUG
2521 ++n;
2522 #endif
2523 if (!field.m_isSerial) continue;
2524 if (field.m_serialId <= 0 || field.m_serialId > nbSer) {
2525 MWAW_DEBUG_MSG(("MsWksDBParser::readSerialFormula: can not find serial for field :%d\n", n-1));
2526 continue;
2527 }
2528 field.m_serialFormula = listSerial[size_t(field.m_serialId-1)];
2529 }
2530
2531 return true;
2532 }
2533
2534 // the default values
2535 bool MsWksDBParser::readDefaultValues()
2536 {
2537 MWAWInputStreamPtr input=m_document->getInput();
2538
2539 long pos = input->tell();
2540 if (input->readLong(2) != 0) {
2541 MWAW_DEBUG_MSG(("MsWksDBParser::readDefaultValues: size is odd\n"));
2542 return false;
2543 }
2544
2545 libmwaw::DebugFile &ascFile = m_document->ascii();
2546 libmwaw::DebugStream f;
2547 f << "Entries(DefValues):";
2548
2549 auto sz =long(input->readULong(2));
2550 f << "sz=" << sz << ",";
2551
2552 long endPos = pos+4+sz;
2553 if (!input->checkPosition(endPos)) {
2554 input->seek(pos, librevenge::RVNG_SEEK_SET);
2555 MWAW_DEBUG_MSG(("MsWksDBParser::readDefaultValues: Segment length is odd %ld\n", sz));
2556 return false;
2557 }
2558 ascFile.addPos(pos);
2559 ascFile.addNote(f.str().c_str());
2560
2561 int numFields = m_state->m_database.m_numFields;
2562 auto &listFields = m_state->m_database.m_listFieldTypes;
2563
2564 if (static_cast<int>(listFields.size())+1 < numFields) {
2565 input->seek(pos, librevenge::RVNG_SEEK_SET);
2566 MWAW_DEBUG_MSG(("MsWksDBParser::readDefaultValues: the number of fields header are to small\n"));
2567 return false;
2568 }
2569 for (int nField = 0; nField < numFields; nField++) {
2570 pos = input->tell();
2571 if (pos+2 == endPos) break;
2572 f.str("");
2573
2574 f << "DefValues-" << nField << ":";
2575
2576 auto fVal = static_cast<int>(input->readLong(1));
2577 bool ok = false;
2578 if (fVal == -2) { // skip
2579 auto skip = static_cast<int>(input->readLong(1));
2580 if (skip > 0 && skip+nField < numFields) {
2581 nField+=skip-1;
2582 f << "skip=" << skip;
2583 ok = true;
2584 }
2585 }
2586 else if (fVal < 0); // error
2587 else if (fVal == 0) ok = true;
2588 else {
2589 auto &field = listFields[size_t(nField)];
2590 auto const &format=field.getFormat();
2591 MWAWCellContent &content=field.m_content;
2592 double value;
2593 bool isNan;
2594 std::string text("");
2595 if (format.m_format == MWAWCell::F_TEXT) {
2596 content.m_textEntry.setBegin(pos+1);
2597 content.m_textEntry.setLength(fVal);
2598 ok = m_document->readDBString(pos+1+fVal, text);
2599 if (!text.empty()) f << "\"" << text << "\",";
2600 }
2601 else if (m_document->readDBNumber(pos+1+fVal, value, isNan, text)) {
2602 field.m_content.setValue(value);
2603 f << value << ",";
2604 ok = true;
2605 }
2606 if (ok) {
2607 if (content.m_contentType == MWAWCellContent::C_UNKNOWN)
2608 content.m_contentType = format.m_format == MWAWCell::F_TEXT ?
2609 MWAWCellContent::C_TEXT : MWAWCellContent::C_NUMBER;
2610 f << field << "," << text;
2611 }
2612 }
2613 if (!ok || input->tell() >= endPos) {
2614 input->seek(pos, librevenge::RVNG_SEEK_SET);
2615 MWAW_DEBUG_MSG(("MsWksDBParser::readDefaultValues: can not read defValue for field:%d\n", nField));
2616 return false;
2617 }
2618 ascFile.addPos(pos);
2619 ascFile.addNote(f.str().c_str());
2620 }
2621 ascFile.addDelimiter(input->tell(),'|');
2622 bool ok = input->readULong(1) == 255;
2623 if (ok) ok = input->readULong(1) == 0;
2624
2625 if (!ok || input->tell() != endPos) {
2626 input->seek(pos, librevenge::RVNG_SEEK_SET);
2627 MWAW_DEBUG_MSG(("MsWksDBParser::readDefaultValues: End of Record is odd\n"));
2628 return false;
2629 }
2630
2631 return true;
2632 }
2633
2634 // reads the columns size
2635 bool MsWksDBParser::readColSize(std::vector<int> &colSize)
2636 {
2637 MWAWInputStreamPtr input=m_document->getInput();
2638 int const vers=version();
2639 long pos = input->tell();
2640 colSize.resize(0);
2641
2642 libmwaw::DebugFile &ascFile = m_document->ascii();
2643 libmwaw::DebugStream f;
2644 f << "Entries(ColSize):";
2645 int numElt = vers<=2 ? 62 : 257;
2646 int prevPos = 0;
2647 for (int i = 0; i < numElt; i++) {
2648 auto val = static_cast<int>(input->readLong(2));
2649 auto n = static_cast<int>(input->readULong(2));
2650 if (prevPos >= 0 && val >= prevPos) {
2651 if (prevPos) colSize.push_back(val-prevPos);
2652 prevPos = val;
2653 }
2654 else
2655 prevPos = -1;
2656 f << val;
2657 if (n != i+1) f << "[#" << n << "]";
2658 f << ",";
2659 }
2660
2661 auto lastVal = static_cast<int>(input->readLong(2));
2662 if (lastVal<-1 || lastVal>numElt) { // normally 0|-1 but I also find 9(number of elements?)
2663 input->seek(pos, librevenge::RVNG_SEEK_SET);
2664 MWAW_DEBUG_MSG(("MsWksDBParser::readColSize: end of colSize is odd\n"));
2665 return false;
2666 }
2667 if (lastVal) f << "unkn=" << lastVal << ",";
2668 ascFile.addPos(pos);
2669 ascFile.addNote(f.str().c_str());
2670 return true;
2671 }
2672
2673 ////////////////////////////////////////////////////////////
2674 // The unknown structure
2675 ////////////////////////////////////////////////////////////
2676 bool MsWksDBParser::readUnknownV2()
2677 {
2678 if (version() > 2) return false;
2679
2680 MWAWInputStreamPtr input=m_document->getInput();
2681 long pos = input->tell();
2682 libmwaw::DebugFile &ascFile = m_document->ascii();
2683 libmwaw::DebugStream f;
2684
2685 if (!input->checkPosition(pos+0x114)) {
2686 MWAW_DEBUG_MSG(("MsWksDBParser::readUnknownV2: first zone is too short\n"));
2687 return false;
2688 }
2689
2690 f << "Entries(DBUnknown):";
2691
2692 for (int i = 0; i < 6; i++) {
2693 auto val = static_cast<int>(input->readLong(2));
2694 if (val) f << "f" << i << "=" << val << ",";
2695 }
2696 auto numUnkn0 = static_cast<int>(input->readLong(2));
2697 auto numUnkn1 = static_cast<int>(input->readLong(2));
2698 f << "unkn0=[" << numUnkn0 << "," << numUnkn1 << "],";
2699
2700 for (int i = 0; i < 120; i++) {
2701 auto val = static_cast<int>(input->readLong(2));
2702 if (val) f << "g" << i << "=" << val << ",";
2703 }
2704
2705 for (int st = 0; st < 2; st++) {
2706 MWAWFont font;
2707 font.setId(static_cast<int>(input->readLong(2)));
2708 font.setSize(float(input->readLong(2)));
2709 f << "font" << st << "=[" << font.getDebugString(getParserState()->m_fontConverter);
2710 auto val = static_cast<int>(input->readLong(2));
2711 if (val) f << "#unk0=" << val << ",";
2712 val = static_cast<int>(input->readLong(2));
2713 if (val) f << "#unk1=" << val << ",";
2714 f << "],";
2715 }
2716
2717 // 2, 18|14
2718 f << "unkn1=[" << input->readLong(2) << ",";
2719 f << input->readLong(2) << "],";
2720
2721 ascFile.addPos(pos);
2722 ascFile.addNote(f.str().c_str());
2723
2724 if (numUnkn0 || numUnkn1) {
2725 // not sure which one is followed by a 0x76 zone
2726 pos = input->tell();
2727 f.str("");
2728 f << "DBUnknown-A:";
2729 if (numUnkn0!=1 || numUnkn1 !=1) {
2730 MWAW_DEBUG_MSG(("MsWksDBParser::readUnknownV2: Checkme, potential problem\n"));
2731 f << "####";
2732 }
2733 for (int i = 0; i < 59; i++) {
2734 auto val = static_cast<int>(input->readLong(2));
2735 if (val) f << "f" << i << "=" << val << ",";
2736 }
2737 ascFile.addPos(pos);
2738 ascFile.addNote(f.str().c_str());
2739 }
2740 return true;
2741 }
2742
2743 ////////////////////////////////////////////////////////////
2744 // read the header
2745 ////////////////////////////////////////////////////////////
2746 bool MsWksDBParser::checkHeader(MWAWHeader *header, bool strict)
2747 {
2748 *m_state = MsWksDBParserInternal::State();
2749 if (!m_document || !m_document->checkHeader3(header, strict)) return false;
2750 if (m_document->getKind() != MWAWDocument::MWAW_K_DATABASE)
2751 return false;
2752 #ifndef DEBUG
2753 if (version() == 1)
2754 return false;
2755 #endif
2756 return true;
2757 }
2758
2759 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: