Debian Codegraph @

src/lib/ClarisWksDatabase.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 <limits>
38 #include <map>
39 #include <sstream>
40
41 #include <librevenge/librevenge.h>
42
43 #include "MWAWCell.hxx"
44 #include "MWAWFont.hxx"
45 #include "MWAWSpreadsheetListener.hxx"
46 #include "MWAWParser.hxx"
47 #include "MWAWTable.hxx"
48
49 #include "ClarisWksDbaseContent.hxx"
50 #include "ClarisWksDocument.hxx"
51 #include "ClarisWksStruct.hxx"
52 #include "ClarisWksStyleManager.hxx"
53
54 #include "ClarisWksDatabase.hxx"
55
56 /** Internal: the structures of a ClarisWksDatabase */
57 namespace ClarisWksDatabaseInternal
58 {
59 struct Field {
60 // the type
61 enum Type { F_Unknown, F_Text, F_Number, F_Date, F_Time,
62 F_Formula, F_FormulaSum,
63 F_Checkbox, F_PopupMenu, F_RadioButton, F_ValueList,
64 F_Multimedia
65 };
66 Field()
67 : m_type(F_Unknown)
68 , m_defType(-1)
69 , m_resType(0)
70 , m_name("")
71 , m_default("")
72 , m_valuesList()
73 , m_formula()
74 {
75 }
76
77 //! operator<<
78 friend std::ostream &operator<<(std::ostream &o, Field const &field)
79 {
80 switch (field.m_type) {
81 case F_Text :
82 o << "text,";
83 break;
84 case F_Number :
85 o << "number,";
86 break;
87 case F_Date :
88 o << "date,";
89 break;
90 case F_Time :
91 o << "time,";
92 break;
93 case F_Formula :
94 o << "formula,";
95 break;
96 case F_FormulaSum :
97 o << "formula(summary),";
98 break;
99 case F_Checkbox :
100 o << "checkbox,";
101 break;
102 case F_PopupMenu :
103 o << "popupMenu,";
104 break;
105 case F_RadioButton :
106 o << "radioButton,";
107 break;
108 case F_ValueList:
109 o << "valueList,";
110 break;
111 case F_Multimedia :
112 o << "multimedia,";
113 break;
114 case F_Unknown :
115 #if !defined(__clang__)
116 default:
117 #endif
118 o << "type=#unknown,";
119 break;
120 }
121 switch (field.m_resType) {
122 case 0:
123 o << "text[format],";
124 break;
125 case 1:
126 o << "number[format],";
127 break;
128 case 2:
129 o << "date[format],";
130 break;
131 case 3:
132 o << "time[format],";
133 break;
134 default:
135 o << "##res[format]=" << field.m_resType << ",";
136 break;
137 }
138 o << "'" << field.m_name << "',";
139 switch (field.m_defType) {
140 case -1:
141 break;
142 case 0:
143 break;
144 case 3:
145 o << "recordInfo,";
146 break;
147 case 7:
148 o << "serial";
149 break;
150 case 8:
151 o << "hasDef,";
152 break; // text with default
153 case 9:
154 o << "popup/radio/control,";
155 break; // with default value ?
156 default:
157 o << "#defType=" << field.m_defType << ",";
158 break;
159 }
160 if (field.m_default.length())
161 o << "defaultVal='" << field.m_default << "',";
162 return o;
163 }
164
165 bool isText() const
166 {
167 return m_type == F_Text;
168 }
169 bool isFormula() const
170 {
171 return m_type == F_Formula || m_type == F_FormulaSum;
172 }
173
174 int getNumDefault(int version) const
175 {
176 switch (m_type) {
177 case F_Text :
178 if (version >= 4)
179 return 1;
180 if (m_defType == 8) return 1;
181 return 0;
182 case F_Number :
183 case F_Date :
184 case F_Time :
185 case F_Multimedia :
186 return 0;
187 case F_Formula :
188 case F_FormulaSum :
189 return 1;
190 case F_Checkbox :
191 return 1;
192 case F_PopupMenu :
193 case F_RadioButton :
194 return 2;
195 case F_ValueList :
196 return (version >= 3) ? 2 : 1;
197 case F_Unknown :
198 #if !defined(__clang__)
199 default:
200 #endif
201 break;
202 }
203 return 0;
204 }
205
206 Type m_type;
207 /** the local definition type */
208 int m_defType;
209 /** the result type */
210 int m_resType;
211 /** the field name */
212 std::string m_name;
213 /** the default value */
214 std::string m_default;
215 /** list of different value list */
216 std::vector<MWAWEntry> m_valuesList;
217 /** the formula */
218 std::vector<MWAWCellContent::FormulaInstruction> m_formula;
219 };
220
221 ////////////////////////////////////////
222 ////////////////////////////////////////
223
224 //! Internal: the database of a ClarisWksDatabase
225 struct Database final : public ClarisWksStruct::DSET {
226 //! constructor
227 explicit Database(ClarisWksStruct::DSET const &dset = ClarisWksStruct::DSET())
228 : ClarisWksStruct::DSET(dset)
229 , m_fields()
230 , m_content()
231 {
232 }
233 //! destructor
234 ~Database() final;
235 //! operator<<
236 friend std::ostream &operator<<(std::ostream &o, Database const &doc)
237 {
238 o << static_cast<ClarisWksStruct::DSET const &>(doc);
239 return o;
240 }
241 //! the list of field
242 std::vector<Field> m_fields;
243 //! the data
244 std::shared_ptr<ClarisWksDbaseContent> m_content;
245 };
246
247 Database::~Database()
248 {
249 }
250
251 ////////////////////////////////////////
252 //! Internal: the state of a ClarisWksDatabase
253 struct State {
254 //! constructor
255 State()
256 : m_databaseMap()
257 {
258 }
259
260 std::map<int, std::shared_ptr<Database> > m_databaseMap;
261 };
262
263 }
264
265 ////////////////////////////////////////////////////////////
266 // constructor/destructor, ...
267 ////////////////////////////////////////////////////////////
268 ClarisWksDatabase::ClarisWksDatabase(ClarisWksDocument &document)
269 : m_document(document)
270 , m_parserState(document.m_parserState)
271 , m_state(new ClarisWksDatabaseInternal::State)
272 , m_mainParser(&document.getMainParser())
273 {
274 }
275
276 ClarisWksDatabase::~ClarisWksDatabase()
277 { }
278
279 int ClarisWksDatabase::version() const
280 {
281 return m_parserState->m_version;
282 }
283
284 // fixme
285 int ClarisWksDatabase::numPages() const
286 {
287 return 1;
288 }
289 ////////////////////////////////////////////////////////////
290 // Intermediate level
291 ////////////////////////////////////////////////////////////
292
293 ////////////////////////////////////////////////////////////
294 // a document part
295 ////////////////////////////////////////////////////////////
296 std::shared_ptr<ClarisWksStruct::DSET> ClarisWksDatabase::readDatabaseZone
297 (ClarisWksStruct::DSET const &zone, MWAWEntry const &entry, bool &complete)
298 {
299 complete = false;
300 if (!entry.valid() || zone.m_fileType != 3 || entry.length() < 32)
301 return std::shared_ptr<ClarisWksStruct::DSET>();
302 long pos = entry.begin();
303 MWAWInputStreamPtr &input= m_parserState->m_input;
304 input->seek(pos+8+16, librevenge::RVNG_SEEK_SET); // avoid header+8 generic number
305 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
306 libmwaw::DebugStream f;
307 std::shared_ptr<ClarisWksDatabaseInternal::Database>
308 databaseZone(new ClarisWksDatabaseInternal::Database(zone));
309
310 f << "Entries(DatabaseDef):" << *databaseZone << ",";
311 ascFile.addDelimiter(input->tell(), '|');
312 ascFile.addPos(pos);
313 ascFile.addNote(f.str().c_str());
314
315 // read the last part
316 long data0Length = zone.m_dataSz;
317 long N = zone.m_numData;
318 if (entry.length() -8-12 != data0Length*N + zone.m_headerSz) {
319 if (data0Length == 0 && N) {
320 MWAW_DEBUG_MSG(("ClarisWksDatabase::readDatabaseZone: can not find definition size\n"));
321 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
322 return std::shared_ptr<ClarisWksStruct::DSET>();
323 }
324
325 MWAW_DEBUG_MSG(("ClarisWksDatabase::readDatabaseZone: unexpected size for zone definition, try to continue\n"));
326 }
327
328 long dataEnd = entry.end()-N*data0Length;
329 int numLast = -1;
330 int const vers=version();
331 switch (vers) {
332 case 1:
333 case 2:
334 case 3:
335 case 4:
336 numLast = 0;
337 break;
338 case 5:
339 numLast = 4;
340 break;
341 case 6:
342 numLast = 8;
343 break;
344 default:
345 MWAW_DEBUG_MSG(("ClarisWksDatabase::readDatabaseZone: unexpected version\n"));
346 break;
347 }
348 if (numLast >= 0 && long(input->tell()) + data0Length + numLast <= dataEnd) {
349 ascFile.addPos(dataEnd-data0Length-numLast);
350 ascFile.addNote("DatabaseDef-_");
351 if (numLast) {
352 ascFile.addPos(dataEnd-numLast);
353 ascFile.addNote("DatabaseDef-extra");
354 }
355 }
356 input->seek(dataEnd, librevenge::RVNG_SEEK_SET);
357
358 for (long i = 0; i < N; i++) {
359 pos = input->tell();
360
361 f.str("");
362 f << "DatabaseDef-" << i;
363 ascFile.addPos(pos);
364 ascFile.addNote(f.str().c_str());
365 input->seek(pos+data0Length, librevenge::RVNG_SEEK_SET);
366 }
367
368 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
369
370 if (m_state->m_databaseMap.find(databaseZone->m_id) != m_state->m_databaseMap.end()) {
371 MWAW_DEBUG_MSG(("ClarisWksDatabase::readDatabaseZone: zone %d already exists!!!\n", databaseZone->m_id));
372 /* can only happen if we did not read completly the header, and so
373 we have previously read some old saved part of the database,
374 which has remained in the junk zones. */
375 if (databaseZone->m_id==1)
376 m_state->m_databaseMap[databaseZone->m_id] = databaseZone;
377 }
378 else
379 m_state->m_databaseMap[databaseZone->m_id] = databaseZone;
380
381 databaseZone->m_otherChilds.push_back(databaseZone->m_id+1);
382
383 pos = input->tell();
384 bool ok = readFields(*databaseZone);
385
386 if (ok) {
387 ok = readDefaults(*databaseZone);
388 pos = input->tell();
389 }
390 if (ok) {
391 pos = input->tell();
392 ok = ClarisWksStruct::readStructZone(*m_parserState, "DatabaseListUnkn0", false);
393 }
394 if (ok) {
395 pos = input->tell();
396 // probably: field number followed by 1 : increasing, 2 : decreasing
397 ok = ClarisWksStruct::readStructZone(*m_parserState, "DatabaseSortFunction", false);
398 }
399 if (ok) {
400 pos = input->tell();
401 std::shared_ptr<ClarisWksDbaseContent> content(new ClarisWksDbaseContent(m_document, false));
402 ok = content->readContent();
403 if (ok) databaseZone->m_content=content;
404 }
405 std::vector<int> listLayout;
406 if (ok) {
407 pos = input->tell();
408 ok = ClarisWksStruct::readIntZone(*m_parserState, "DatabaseLayout", false, 4, listLayout);
409 }
410 if (ok) {
411 for (size_t i=0; i<listLayout.size(); ++i) {
412 pos = input->tell();
413 if (!readLayout(*databaseZone)) {
414 MWAW_DEBUG_MSG(("ClarisWksDatabase::readDatabaseZone: can not read some ListLayout data file\n"));
415 ok=false;
416 input->seek(pos, librevenge::RVNG_SEEK_SET);
417 ascFile.addPos(pos);
418 ascFile.addNote("DatabaseLayout:###");
419 break;
420 }
421 }
422 }
423 if (ok) {
424 pos = input->tell();
425 // in v1-v4 list of id block?, in v5-v6 list of block id+?
426 ok = ClarisWksStruct::readStructZone(*m_parserState, "DatabaseListUnkn3", false);
427 }
428
429 if (ok) { // never seems,
430 pos=input->tell();
431 auto sz=long(input->readULong(4));
432 if (input->checkPosition(pos+4+sz)) {
433 input->seek(pos+4+sz, librevenge::RVNG_SEEK_SET);
434 ascFile.addPos(pos);
435 if (sz) {
436 MWAW_DEBUG_MSG(("ClarisWksDatabase::readDatabaseZone: find a Unkn4 block\n"));
437 ascFile.addNote("Entries(DatabaseListUnkn4):");
438 }
439 else
440 ascFile.addNote("_");
441 }
442 else {
443 ok=false;
444 MWAW_DEBUG_MSG(("ClarisWksDatabase::readDatabaseZone: find a Unkn4 block does not know how to read it\n"));
445 input->seek(pos, librevenge::RVNG_SEEK_SET);
446 }
447 }
448 if (ok && vers>1) {
449 pos=input->tell();
450 std::vector<std::string> listString;
451 ok=m_document.readStringList("DatabaseListString", false, listString);
452 }
453 if (ok) {
454 pos = input->tell();
455 ok = ClarisWksStruct::readStructZone(*m_parserState, "DatabaseUnkn5", false);
456 }
457 if (ok && vers>=4) {
458 // version 4 can contains more block: list of int+flag?
459 pos=input->tell();
460 ok = ClarisWksStruct::readStructZone(*m_parserState, "DatabaseUnkn6", false);
461 }
462 // now the following seems to be different
463 if (!ok)
464 input->seek(pos, librevenge::RVNG_SEEK_SET);
465
466 return databaseZone;
467 }
468
469 ////////////////////////////////////////////////////////////
470 //
471 // Intermediate level
472 //
473 ////////////////////////////////////////////////////////////
474 bool ClarisWksDatabase::readFields(ClarisWksDatabaseInternal::Database &dBase)
475 {
476 MWAWInputStreamPtr &input= m_parserState->m_input;
477 long pos = input->tell();
478 ClarisWksStruct::Struct header;
479 if (!header.readHeader(input,true) || (header.m_size && header.m_dataSize<28)) {
480 MWAW_DEBUG_MSG(("ClarisWksDatabase::readFields: can not read the header\n"));
481 return false;
482 }
483 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
484 libmwaw::DebugStream f;
485 if (header.m_size==0) {
486 ascFile.addPos(pos);
487 ascFile.addNote("Nop");
488 return true;
489 }
490 long endPos = pos+4+header.m_size;
491 f << "Entries(DatabaseField):" << header;
492 if (header.m_headerSize) {
493 ascFile.addDelimiter(input->tell(),'|');
494 input->seek(header.m_headerSize, librevenge::RVNG_SEEK_CUR);
495 }
496 ascFile.addPos(pos);
497 ascFile.addNote(f.str().c_str());
498
499 dBase.m_fields.resize(size_t(header.m_numData));
500 int n=0;
501 for (auto &field : dBase.m_fields) {
502 pos = input->tell();
503 f.str("");
504 f << "DatabaseField-" << n++ << ":";
505
506 int const fNameMaxSz = 64;
507 std::string name("");
508 auto sz = long(input->readULong(1));
509 if (sz > fNameMaxSz-1 || sz > header.m_dataSize-1) {
510 input->seek(pos, librevenge::RVNG_SEEK_SET);
511 MWAW_DEBUG_MSG(("ClarisWksDatabase::readFields: find odd field name\n"));
512 return false;
513 }
514 for (long j = 0; j < sz; j++)
515 name += char(input->readULong(1));
516 field.m_name = name;
517
518 input->seek(pos+fNameMaxSz, librevenge::RVNG_SEEK_SET);
519 auto type = static_cast<int>(input->readULong(1));
520 bool ok = true;
521 switch (type) {
522 // or name
523 case 0:
524 field.m_type = ClarisWksDatabaseInternal::Field::F_Text;
525 break;
526 case 1:
527 field.m_type = ClarisWksDatabaseInternal::Field::F_Number;
528 break;
529 case 2:
530 field.m_type = ClarisWksDatabaseInternal::Field::F_Date;
531 break;
532 case 3:
533 field.m_type = ClarisWksDatabaseInternal::Field::F_Time;
534 break;
535 case 4:
536 if (version() <= 2)
537 field.m_type = ClarisWksDatabaseInternal::Field::F_Formula;
538 else
539 field.m_type = ClarisWksDatabaseInternal::Field::F_PopupMenu;
540 break;
541 case 5:
542 if (version() <= 2)
543 field.m_type = ClarisWksDatabaseInternal::Field::F_FormulaSum;
544 else
545 field.m_type = ClarisWksDatabaseInternal::Field::F_Checkbox;
546 break;
547 case 6:
548 field.m_type = ClarisWksDatabaseInternal::Field::F_RadioButton;
549 break;
550 case 7:
551 if (version() == 4)
552 field.m_type = ClarisWksDatabaseInternal::Field::F_Formula;
553 else
554 field.m_type = ClarisWksDatabaseInternal::Field::F_Multimedia;
555 break;
556 case 8:
557 if (version() == 4)
558 field.m_type = ClarisWksDatabaseInternal::Field::F_FormulaSum;
559 else
560 ok = false;
561 break;
562 case 10:
563 field.m_type = ClarisWksDatabaseInternal::Field::F_Formula;
564 break;
565 case 11:
566 field.m_type = ClarisWksDatabaseInternal::Field::F_FormulaSum;
567 break;
568 default:
569 ok = false;
570 break;
571 }
572 if (!ok)
573 f << "#type=" << type << ",";
574 auto val = static_cast<int>(input->readULong(1));
575 if (val)
576 f << "#unkn=" << val << ",";
577 unsigned long ptr = input->readULong(4);
578 if (ptr) // set for formula
579 f << "ptr=" << std::hex << ptr << std::dec << ",";
580 field.m_resType=static_cast<int>(input->readLong(1));
581 f << "fl?=[" << std::hex;
582 f << input->readULong(1) << ",";
583 f << input->readULong(1) << ",";
584 for (int j = 0; j < 6; j++) {
585 // some int which seems constant on the database...
586 val = static_cast<int>(input->readULong(2));
587 f << val << ",";
588 }
589 f << std::dec << "],";
590
591 if (version() > 1) {
592 for (int j = 0; j < 16; j++) {
593 /** find f1=600 for a number
594 f16 = 0[checkbox, ... ], 2[number or text],3 [name field], 82[value list],
595 f16 & 8: can not be empty
596 */
597 val = static_cast<int>(input->readLong(2));
598 if (val) f << "f" << j << "=" << std::hex << val << std::dec << ",";
599 }
600 auto subType = static_cast<int>(input->readULong(2));
601 if (version() == 2) {
602 if ((subType & 0x80) && field.m_type == ClarisWksDatabaseInternal::Field::F_Text) {
603 field.m_type = ClarisWksDatabaseInternal::Field::F_ValueList;
604 subType &= 0xFF7F;
605 }
606 if (subType) f << "f17=" << std::hex << subType << std::dec << ",";
607 }
608 else {
609 if ((subType & 0x80) && field.m_type == ClarisWksDatabaseInternal::Field::F_Text) {
610 field.m_type = ClarisWksDatabaseInternal::Field::F_ValueList;
611 subType &= 0xFF7F;
612 }
613 ok = true;
614 switch (subType) {
615 case 0:
616 ok = field.m_type == ClarisWksDatabaseInternal::Field::F_Checkbox ||
617 field.m_type == ClarisWksDatabaseInternal::Field::F_PopupMenu ||
618 field.m_type == ClarisWksDatabaseInternal::Field::F_RadioButton ||
619 field.m_type == ClarisWksDatabaseInternal::Field::F_Multimedia;
620 break;
621 case 2: // basic
622 break;
623 case 3:
624 ok = field.m_type == ClarisWksDatabaseInternal::Field::F_Text;
625 if (ok) f << "name[field],";
626 break;
627 case 6:
628 ok = version() == 4 && field.m_type == ClarisWksDatabaseInternal::Field::F_ValueList;;
629 break;
630 default:
631 ok = false;
632 }
633 if (!ok) f << "#unkSubType=" << std::hex << subType << std::dec << ",";
634 }
635 val = static_cast<int>(input->readULong(2));
636 if (val==0x8000)
637 f << "recordInfo";
638 else if (val)
639 f << "#unk1=" << std::hex << val << std::dec << ",";
640 field.m_defType = static_cast<int>(input->readULong(1));
641 // default, followed by a number/ptr/... : 7fff ( mean none)
642 }
643 f << field << ",";
644 long actPos = input->tell();
645 if (actPos != pos && actPos != pos+header.m_dataSize)
646 ascFile.addDelimiter(actPos, '|');
647 ascFile.addPos(pos);
648 ascFile.addNote(f.str().c_str());
649 input->seek(pos+header.m_dataSize, librevenge::RVNG_SEEK_SET);
650 }
651
652 input->seek(endPos, librevenge::RVNG_SEEK_SET);
653 return true;
654 }
655
656 bool ClarisWksDatabase::readDefaults(ClarisWksDatabaseInternal::Database &dBase)
657 {
658 int vers = version();
659 MWAWInputStreamPtr &input= m_parserState->m_input;
660 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
661 libmwaw::DebugStream f;
662
663 int n=0;
664 for (auto &field : dBase.m_fields) {
665 int numExpected = field.getNumDefault(vers);
666
667 bool formField = field.isFormula();
668 bool valueList = field.m_type == ClarisWksDatabaseInternal::Field::F_ValueList;
669 for (int fi = 0; fi < numExpected; fi++) {
670 // actually we guess which one are ok
671 long pos = input->tell();
672 auto sz = long(input->readULong(4));
673
674 long endPos = pos+4+sz;
675 if (!input->checkPosition(endPos)) {
676 MWAW_DEBUG_MSG(("ClarisWksDatabase::readDefaults: can not find value for field: %d\n", fi));
677 input->seek(pos, librevenge::RVNG_SEEK_SET);
678 return false;
679 }
680 long length = (vers <= 2 && field.isText()) ? sz : static_cast<int>(input->readULong(1));
681 f.str("");
682 f << "Entries(DatabaseDft)[" << n++ << "]:";
683 if (formField) {
684 if (length != sz-1) {
685 MWAW_DEBUG_MSG(("ClarisWksDatabase::readDefaults: can not find formula for field: %ld\n", long(n)));
686 input->seek(pos, librevenge::RVNG_SEEK_SET);
687 return false;
688 }
689 f << "formula,";
690 std::vector<MWAWCellContent::FormulaInstruction> formula;
691 std::string error;
692 if (!dBase.m_content)
693 dBase.m_content.reset(new ClarisWksDbaseContent(m_document, false));
694 if (!dBase.m_content->readFormula(MWAWVec2i(fi,0), endPos, formula, error)) {
695 MWAW_DEBUG_MSG(("ClarisWksDatabase::readDefaults: can not find formula for field: %ld\n", long(n)));
696 }
697 else
698 field.m_formula=formula;
699 for (auto const &fo : formula) f << fo;
700 f << error;
701 }
702 else {
703 bool listField = (valueList && fi == 1) || (!valueList && fi==0 && numExpected==2);
704 if (listField)
705 f << "listString,";
706 else
707 f << "string,";
708 if (vers > 2 && !listField && length != sz-1) {
709 MWAW_DEBUG_MSG(("ClarisWksDatabase::readDefaults: can not find strings for field: %ld\n", long(n)));
710 input->seek(pos, librevenge::RVNG_SEEK_SET);
711 return false;
712 }
713 while (1) {
714 long actPos = input->tell();
715 if (actPos+length > endPos) {
716 MWAW_DEBUG_MSG(("ClarisWksDatabase::readDefaults: can not find strings for field: %ld\n", long(n)));
717 ascFile.addPos(pos);
718 ascFile.addNote("DatabaseDft:###");
719
720 input->seek(pos, librevenge::RVNG_SEEK_SET);
721 return true;
722 }
723 if (listField) {
724 MWAWEntry entry;
725 entry.setBegin(actPos);
726 entry.setLength(length);
727 field.m_valuesList.push_back(entry);
728 }
729 std::string name("");
730 for (long c = 0; c < length; c++)
731 name += char(input->readULong(1));
732 f << "'" << name << "',";
733 if (long(input->tell()) == endPos)
734 break;
735 length = long(input->readULong(1));
736 }
737 }
738 ascFile.addPos(pos);
739 ascFile.addNote(f.str().c_str());
740 input->seek(endPos, librevenge::RVNG_SEEK_SET);
741 }
742 }
743 return true;
744 }
745
746
747 bool ClarisWksDatabase::readLayout(ClarisWksDatabaseInternal::Database &dBase)
748 {
749 MWAWInputStreamPtr &input= m_parserState->m_input;
750 long pos = input->tell();
751 ClarisWksStruct::Struct header;
752 if (!header.readHeader(input,true) || header.m_headerSize<52 || header.m_dataSize<6) {
753 MWAW_DEBUG_MSG(("ClarisWksDatabase::readLayout: can not read the header\n"));
754 return false;
755 }
756 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
757 libmwaw::DebugStream f;
758 if (header.m_size==0) {
759 ascFile.addPos(pos);
760 ascFile.addNote("Nop");
761 return true;
762 }
763 f << "DatabaseLayout-Part:" << header;
764 auto val = static_cast<int>(input->readLong(2));
765 if (val) f << "f3=" << val << ",";
766 auto childId=int(input->readULong(2));
767 f << "childId=" << childId << ",";
768 dBase.m_otherChilds.push_back(childId);
769 for (int i = 0; i < 2; ++i) { // f4=1-3, f5=0|c6|12a
770 val = static_cast<int>(input->readLong(2));
771 if (val) f << "f" << i+4 << "=" << val << ",";
772 }
773 for (int i = 0; i<4; ++i) { // always 0|1
774 val = static_cast<int>(input->readLong(1));
775 if (val==1) f << "fl" << i << ",";
776 else if (val) f << "#fl" << i << "=" << val << ",";
777 }
778 auto sSz=static_cast<int>(input->readULong(1));
779 if (sSz>31) {
780 MWAW_DEBUG_MSG(("ClarisWksDatabase::readLayout: find odd string size\n"));
781 f << "#sSz=" << sSz << ",";
782 }
783 else {
784 std::string name("");
785 for (int i=0; i<sSz; ++i) name+=char(input->readULong(1));
786 f << "\"" << name << "\",";
787 }
788 input->seek(pos+60, librevenge::RVNG_SEEK_SET);
789 val = static_cast<int>(input->readLong(2)); // always 0
790 if (val) f << "g0=" << val << ",";
791 childId=static_cast<int>(input->readULong(2));
792 f << "childId2=" << childId << ",";
793 dBase.m_otherChilds.push_back(childId);
794
795 ascFile.addDelimiter(input->tell(),'|');
796 ascFile.addPos(pos);
797 ascFile.addNote(f.str().c_str());
798
799 input->seek(pos+4+12+header.m_headerSize, librevenge::RVNG_SEEK_SET);
800 for (long i = 0; i < header.m_numData; i++) {
801 pos=input->tell();
802 f.str("");
803 f << "DatabaseLayout-Part" << i << ":";
804
805 ascFile.addPos(pos);
806 ascFile.addNote(f.str().c_str());
807 input->seek(pos+header.m_dataSize, librevenge::RVNG_SEEK_SET);
808 }
809
810 pos=input->tell();
811 if (!ClarisWksStruct::readStructZone(*m_parserState, "DatabaseLayout", false)) {
812 MWAW_DEBUG_MSG(("ClarisWksDatabase::readLayout: can not read the layout second part\n"));
813 ascFile.addPos(pos);
814 ascFile.addNote("DatabaseLayout-B:###");
815 return false;
816 }
817 return true;
818 }
819
820 ////////////////////////////////////////////////////////////
821 //
822 // send data
823 //
824 ////////////////////////////////////////////////////////////
825 bool ClarisWksDatabase::sendDatabase(int zId, MWAWListenerPtr listener)
826 {
827 if (!listener)
828 listener=m_parserState->m_spreadsheetListener;
829 if (!listener) {
830 MWAW_DEBUG_MSG(("ClarisWksDatabase::sendDatabase: called without any listener\n"));
831 return false;
832 }
833 if (listener->getType()!=MWAWListener::Spreadsheet ||
834 (m_parserState->m_kind==MWAWDocument::MWAW_K_DATABASE && zId!=1)) {
835 MWAW_DEBUG_MSG(("ClarisWksDatabase::sendDatabase: sending a database is not implemented\n"));
836 return false;
837 }
838
839 auto *sheetListener=static_cast<MWAWSpreadsheetListener *>(listener.get());
840 auto it=m_state->m_databaseMap.find(zId);
841 if (it == m_state->m_databaseMap.end() || !it->second) {
842 MWAW_DEBUG_MSG(("ClarisWksDatabase::sendDatabase: can not find zone %d!!!\n", zId));
843 return false;
844 }
845 auto &dbase=*it->second;
846 MWAWVec2i minData, maxData;
847 std::vector<int> recordsPos;
848 if (!dbase.m_content || !dbase.m_content->getExtrema(minData,maxData) ||
849 !dbase.m_content->getRecordList(recordsPos)) {
850 MWAW_DEBUG_MSG(("ClarisWksDatabase::sendDatabase: can not find any content\n"));
851 return false;
852 }
853 size_t numDataFields=dbase.m_fields.size();
854 int numFields = std::max(maxData[0]+1,int(numDataFields));
855 std::vector<ClarisWksStyleManager::CellFormat> formats;
856 formats.resize(size_t(numFields), ClarisWksStyleManager::CellFormat());
857 bool hasMultimedia=false;
858 for (size_t f=0; f < dbase.m_fields.size(); ++f) {
859 switch (dbase.m_fields[f].m_type) {
860 case ClarisWksDatabaseInternal::Field::F_Number:
861 formats[f].m_format=MWAWCell::F_NUMBER;
862 formats[f].m_numberFormat=MWAWCell::F_NUMBER_GENERIC;
863 break;
864 case ClarisWksDatabaseInternal::Field::F_Date:
865 formats[f].m_format=MWAWCell::F_DATE;
866 break;
867 case ClarisWksDatabaseInternal::Field::F_Time:
868 formats[f].m_format=MWAWCell::F_TIME;
869 break;
870 case ClarisWksDatabaseInternal::Field::F_Checkbox:
871 formats[f].m_format=MWAWCell::F_BOOLEAN;
872 break;
873 case ClarisWksDatabaseInternal::Field::F_Multimedia:
874 formats[f].m_format=MWAWCell::F_TEXT;
875 hasMultimedia=true;
876 break;
877 case ClarisWksDatabaseInternal::Field::F_Unknown:
878 case ClarisWksDatabaseInternal::Field::F_Text:
879 case ClarisWksDatabaseInternal::Field::F_Formula:
880 case ClarisWksDatabaseInternal::Field::F_FormulaSum:
881 case ClarisWksDatabaseInternal::Field::F_PopupMenu:
882 case ClarisWksDatabaseInternal::Field::F_RadioButton:
883 case ClarisWksDatabaseInternal::Field::F_ValueList: // ok, the text is stored
884 #if !defined(__clang__)
885 default:
886 #endif
887 switch (dbase.m_fields[f].m_resType) {
888 case 1:
889 formats[f].m_format=MWAWCell::F_NUMBER;
890 formats[f].m_numberFormat=MWAWCell::F_NUMBER_GENERIC;
891 break;
892 case 2:
893 formats[f].m_format=MWAWCell::F_DATE;
894 break;
895 case 3:
896 formats[f].m_format=MWAWCell::F_TIME;
897 break;
898 default:
899 break;
900 }
901 break;
902 }
903 }
904 dbase.m_content->setDatabaseFormats(formats);
905
906 std::vector<float> colSize(size_t(numFields),72);
907 sheetListener->openSheet(colSize, librevenge::RVNG_POINT);
908 MWAWInputStreamPtr &input= m_parserState->m_input;
909 MWAWFont const defFont;
910 // increase the row height, if we can have some picture
911 float const rowHeight=!hasMultimedia ? 14.f : 72.f;
912 for (size_t r=0; r < recordsPos.size(); ++r) {
913 sheetListener->openSheetRow(rowHeight, librevenge::RVNG_POINT);
914 for (int c=0; c < numFields; ++c) {
915 ClarisWksDbaseContent::Record rec;
916 if (!dbase.m_content->get(MWAWVec2i(c,recordsPos[r]),rec)) continue;
917 sheetListener->setFont(defFont);
918 MWAWCell cell;
919 cell.setPosition(MWAWVec2i(c,int(r)));
920 cell.setFormat(rec.m_format);
921 cell.setHAlignment(rec.m_hAlign);
922 bool isMultimedia=false;
923 if (c<int(numDataFields)) {
924 auto const &field=dbase.m_fields[size_t(c)];
925 if (field.m_type==ClarisWksDatabaseInternal::Field::F_Multimedia)
926 isMultimedia=true;
927 else if (field.m_type==ClarisWksDatabaseInternal::Field::F_Formula && !field.m_formula.empty()) {
928 rec.m_content.m_formula=field.m_formula;
929 for (auto &d : rec.m_content.m_formula) {
930 if (d.m_type == MWAWCellContent::FormulaInstruction::F_Cell)
931 d.m_position[0][1]=int(r);
932 }
933 }
934 else if (field.m_type==ClarisWksDatabaseInternal::Field::F_PopupMenu ||
935 field.m_type==ClarisWksDatabaseInternal::Field::F_RadioButton) {
936 if (rec.m_content.isValueSet()) {
937 auto enumId = int(rec.m_content.m_value+0.5);
938 // checkme: if the enum list is a list of float, the enum value can be stored as value:-~
939 if (enumId > 0 && enumId <= int(field.m_valuesList.size()) &&
940 double(enumId)-0.01 < rec.m_content.m_value &&
941 double(enumId)+0.01 > rec.m_content.m_value) {
942 rec.m_format.m_format=MWAWCell::F_TEXT;
943 rec.m_content.m_textEntry=field.m_valuesList[size_t(enumId-1)];
944 rec.m_content.m_valueSet=false;
945 cell.setFormat(rec.m_format);
946 }
947 }
948 }
949 }
950 // change the reference date from 1/1/1904 to 1/1/1900
951 if (rec.m_format.m_format==MWAWCell::F_DATE && rec.m_content.isValueSet())
952 rec.m_content.setValue(rec.m_content.m_value+1460);
953 else if (isMultimedia) // do not export the picture id
954 rec.m_content.m_valueSet=false;
955 sheetListener->openSheetCell(cell, rec.m_content);
956 if (isMultimedia) {
957 auto pictId = int(rec.m_content.m_value+0.5); // pictId is saved as float, converts it back to an int
958 if (pictId>0) {
959 MWAWPosition pos(MWAWVec2f(0,0), MWAWVec2f(72,72), librevenge::RVNG_POINT);
960 pos.m_anchorTo=MWAWPosition::Cell;
961 // we have only one sheet, so compute the cell name by hand
962 std::string endCellName=std::string("Sheet0.")+MWAWCell::getBasicCellName(MWAWVec2i(int(c+1),int(r+1)));
963 pos.m_anchorCellName=endCellName.c_str();
964 m_document.sendDatabasePictZone(pictId, listener, pos);
965 }
966 }
967 else if (rec.m_content.m_textEntry.valid()) {
968 long fPos = input->tell();
969 input->seek(rec.m_content.m_textEntry.begin(), librevenge::RVNG_SEEK_SET);
970 long endPos = rec.m_content.m_textEntry.end();
971 int cPos=0;
972 while (!input->isEnd() && input->tell() < endPos) {
973 if (rec.m_posToFontMap.find(cPos) != rec.m_posToFontMap.end())
974 sheetListener->setFont(rec.m_posToFontMap.find(cPos)->second);
975 auto ch=static_cast<unsigned char>(input->readULong(1));
976 if (ch==9)
977 sheetListener->insertTab();
978 else if (ch==0xa || ch==0xd)
979 sheetListener->insertEOL();
980 else
981 sheetListener->insertCharacter(ch, input, endPos);
982 ++cPos;
983 }
984 input->seek(fPos,librevenge::RVNG_SEEK_SET);
985 }
986 sheetListener->closeSheetCell();
987 }
988 sheetListener->closeSheetRow();
989 }
990 sheetListener->closeSheet();
991 return true;
992 }
993 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: