Debian Codegraph @

src/lib/BeagleWksDBParser.cxx

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 #include <cmath>
35 #include <iomanip>
36 #include <iostream>
37 #include <limits>
38 #include <set>
39 #include <sstream>
40
41 #include <librevenge/librevenge.h>
42
43 #include "MWAWCell.hxx"
44 #include "MWAWFontConverter.hxx"
45 #include "MWAWHeader.hxx"
46 #include "MWAWList.hxx"
47 #include "MWAWParagraph.hxx"
48 #include "MWAWPosition.hxx"
49 #include "MWAWPrinter.hxx"
50 #include "MWAWRSRCParser.hxx"
51 #include "MWAWSpreadsheetListener.hxx"
52 #include "MWAWSubDocument.hxx"
53
54 #include "BeagleWksStructManager.hxx"
55
56 #include "BeagleWksDBParser.hxx"
57
58 /** Internal: the structures of a BeagleWksDBParser */
59 namespace BeagleWksDBParserInternal
60 {
61 //! Internal: the cell of a BeagleWksDBParser
62 struct Cell final : public MWAWCell {
63 //! the cell type
64 enum Type { Text, Number, Date, Time, Picture, Formula, Memo, Unknown };
65 //! constructor
66 explicit Cell(MWAWVec2i pos=MWAWVec2i(0,0))
67 : MWAWCell()
68 , m_type(Unknown)
69 , m_name("")
70 , m_content()
71 , m_formula(-1)
72 , m_pictureId(-1)
73 , m_isEmpty(false)
74 {
75 setPosition(pos);
76 }
77 Cell(Cell const &)=default;
78 //! destructor
79 ~Cell() final;
80 //! returns true if the field has no content
81 bool empty() const
82 {
83 return m_content.empty() && (m_type!=Picture || m_pictureId<=0);
84 }
85 //! the cell type
86 Type m_type;
87 //! the field name
88 std::string m_name;
89 //! the cell content
90 MWAWCellContent m_content;
91 //! the formula id
92 int m_formula;
93 //! picture id
94 int m_pictureId;
95 //! flag to know if the cell is empty
96 bool m_isEmpty;
97 };
98
99 Cell::~Cell()
100 {
101 }
102
103 //! Internal: the spreadsheet of a BeagleWksDBParser
104 struct Database {
105 //! constructor
106 Database()
107 : m_numFields(0)
108 , m_fields()
109 , m_records()
110 , m_memos()
111 , m_name("Sheet0")
112 {
113 }
114 //! convert the m_widthCols, m_heightRows in a vector of of point size
115 static std::vector<float> convertInPoint(std::vector<int> const &list, float defSize)
116 {
117 size_t numElt = list.size();
118 std::vector<float> res;
119 res.resize(numElt);
120 for (size_t i = 0; i < numElt; i++) {
121 if (list[i] < 0) res[i] = defSize;
122 else res[i] = float(list[i]);
123 }
124 return res;
125 }
126 //! update a field with the record data
127 void updateWithContent(Cell &cell, MWAWVec2i const &pos, MWAWCellContent const &content) const;
128 //! the number of rows
129 int m_numFields;
130 //! the list of fields
131 std::vector<Cell> m_fields;
132 //! the list of not empty cells (one list by row )
133 std::vector<std::vector<MWAWCellContent> > m_records;
134 /** the list of memo strings entry */
135 std::vector<MWAWEntry> m_memos;
136 //! the database name
137 std::string m_name;
138 };
139
140 void Database::updateWithContent(Cell &cell, MWAWVec2i const &pos, MWAWCellContent const &content) const
141 {
142 cell.setPosition(pos);
143 switch (cell.m_type) {
144 case Cell::Formula:
145 cell.m_content.m_contentType=MWAWCellContent::C_FORMULA;
146 break;
147 case Cell::Number:
148 case Cell::Date:
149 case Cell::Time:
150 case Cell::Text:
151 cell.m_content=content;
152 break;
153 case Cell::Memo: {
154 if (!content.isValueSet()) break;
155 auto id=int(0.1+content.m_value);
156 if (id<1 || id>static_cast<int>(m_memos.size())) {
157 MWAW_DEBUG_MSG(("BeagleWksDBParserInternal::Database::updateWithContent: can not retrieve the memo content\n"));
158 break;
159 }
160 cell.m_content.m_contentType=MWAWCellContent::C_TEXT;
161 cell.m_content.m_textEntry=m_memos[size_t(id-1)];
162 break;
163 }
164 case Cell::Picture:
165 cell.m_pictureId=int(0.1+content.m_value);
166 break;
167 #if !defined(__clang__)
168 default:
169 #endif
170 case Cell::Unknown:
171 break;
172 }
173 auto const &format=cell.getFormat();
174 // change the reference date from 1/1/1904 to 1/1/1900
175 if (format.m_format==MWAWCell::F_DATE && cell.m_content.isValueSet())
176 cell.m_content.setValue(cell.m_content.m_value+1460.);
177 // and try to update the 1D formula in 2D
178 auto &formula=cell.m_content.m_formula;
179 for (auto &instr : formula) {
180 if (instr.m_type==MWAWCellContent::FormulaInstruction::F_Cell) {
181 --instr.m_position[0][0];
182 instr.m_position[0][1]=pos[1];
183 }
184 else if (instr.m_type==MWAWCellContent::FormulaInstruction::F_CellList) {
185 --instr.m_position[0][0];
186 --instr.m_position[1][0];
187 instr.m_position[0][1]=instr.m_position[1][1]=pos[1];
188 }
189 }
190 }
191
192 ////////////////////////////////////////
193 //! Internal: the state of a BeagleWksDBParser
194 struct State {
195 //! constructor
196 State()
197 : m_databaseBegin(-1)
198 , m_database()
199 , m_typeEntryMap()
200 , m_actPage(0)
201 , m_numPages(0)
202 , m_headerHeight(0)
203 , m_footerHeight(0)
204 {
205 }
206 /** the database begin position */
207 long m_databaseBegin;
208 /** the database */
209 Database m_database;
210 /** the type entry map */
211 std::multimap<std::string, MWAWEntry> m_typeEntryMap;
212 int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */;
213
214 int m_headerHeight /** the header height if known */,
215 m_footerHeight /** the footer height if known */;
216 };
217
218 ////////////////////////////////////////
219 //! Internal: the subdocument of a BeagleWksDBParser
220 class SubDocument final : public MWAWSubDocument
221 {
222 public:
223 SubDocument(BeagleWksDBParser &pars, MWAWInputStreamPtr const &input, MWAWEntry const &entry)
224 : MWAWSubDocument(&pars, input, entry)
225 {
226 }
227
228 //! destructor
229 ~SubDocument() final {}
230
231 //! operator!=
232 bool operator!=(MWAWSubDocument const &doc) const final
233 {
234 return MWAWSubDocument::operator!=(doc);
235 }
236
237 //! the parser function
238 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
239
240 protected:
241 private:
242 SubDocument(SubDocument const &orig) = delete;
243 SubDocument &operator=(SubDocument const &orig) = delete;
244 };
245
246 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/)
247 {
248 if (!listener.get()) {
249 MWAW_DEBUG_MSG(("BeagleWksDBParserInternal::SubDocument::parse: no listener\n"));
250 return;
251 }
252 auto *parser=dynamic_cast<BeagleWksDBParser *>(m_parser);
253 if (!parser) {
254 MWAW_DEBUG_MSG(("BeagleWksDBParserInternal::SubDocument::parse: can not find the parser\n"));
255 return;
256 }
257 long pos = m_input->tell();
258 listener->setFont(MWAWFont(3,12)); // fixme
259 parser->sendText(m_zone, true);
260 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
261 }
262
263 }
264
265
266 ////////////////////////////////////////////////////////////
267 // constructor/destructor, ...
268 ////////////////////////////////////////////////////////////
269 BeagleWksDBParser::BeagleWksDBParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
270 : MWAWSpreadsheetParser(input, rsrcParser, header)
271 , m_state()
272 , m_structureManager()
273 {
274 init();
275 }
276
277 BeagleWksDBParser::~BeagleWksDBParser()
278 {
279 }
280
281 void BeagleWksDBParser::init()
282 {
283 resetSpreadsheetListener();
284 setAsciiName("main-1");
285
286 m_state.reset(new BeagleWksDBParserInternal::State);
287 m_structureManager.reset(new BeagleWksStructManager(getParserState()));
288
289 // reduce the margin (in case, the page is not defined)
290 getPageSpan().setMargins(0.1);
291 }
292
293 MWAWInputStreamPtr BeagleWksDBParser::rsrcInput()
294 {
295 return getRSRCParser()->getInput();
296 }
297
298 libmwaw::DebugFile &BeagleWksDBParser::rsrcAscii()
299 {
300 return getRSRCParser()->ascii();
301 }
302
303 ////////////////////////////////////////////////////////////
304 // position and height
305 ////////////////////////////////////////////////////////////
306 MWAWVec2f BeagleWksDBParser::getPageLeftTop() const
307 {
308 return MWAWVec2f(float(getPageSpan().getMarginLeft()),
309 float(getPageSpan().getMarginTop()+m_state->m_headerHeight/72.0));
310 }
311
312 ////////////////////////////////////////////////////////////
313 // the parser
314 ////////////////////////////////////////////////////////////
315 void BeagleWksDBParser::parse(librevenge::RVNGSpreadsheetInterface *docInterface)
316 {
317 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
318 bool ok = false;
319 try {
320 // create the asciiFile
321 ascii().setStream(getInput());
322 ascii().open(asciiName());
323
324 checkHeader(nullptr);
325 ok = createZones();
326 if (ok) {
327 createDocument(docInterface);
328 sendDatabase();
329 }
330 ascii().reset();
331 }
332 catch (...) {
333 MWAW_DEBUG_MSG(("BeagleWksDBParser::parse: exception catched when parsing\n"));
334 ok = false;
335 }
336
337 resetSpreadsheetListener();
338 if (!ok) throw(libmwaw::ParseException());
339 }
340
341 ////////////////////////////////////////////////////////////
342 // create the document
343 ////////////////////////////////////////////////////////////
344 void BeagleWksDBParser::createDocument(librevenge::RVNGSpreadsheetInterface *documentInterface)
345 {
346 if (!documentInterface) return;
347 if (getSpreadsheetListener()) {
348 MWAW_DEBUG_MSG(("BeagleWksDBParser::createDocument: listener already exist\n"));
349 return;
350 }
351
352 // update the page
353 m_state->m_actPage = 0;
354
355 // create the page list
356 int numPages = 1;
357 m_state->m_numPages = numPages;
358
359 MWAWEntry header, footer;
360 m_structureManager->getHeaderFooterEntries(header,footer);
361 std::vector<MWAWPageSpan> pageList;
362 MWAWPageSpan ps(getPageSpan());
363 if (header.valid()) {
364 std::shared_ptr<BeagleWksDBParserInternal::SubDocument> subDoc
365 (new BeagleWksDBParserInternal::SubDocument(*this, getInput(), header));
366 MWAWHeaderFooter hf(MWAWHeaderFooter::HEADER, MWAWHeaderFooter::ALL);
367 hf.m_subDocument=subDoc;
368 ps.setHeaderFooter(hf);
369 }
370 if (footer.valid()) {
371 std::shared_ptr<BeagleWksDBParserInternal::SubDocument> subDoc
372 (new BeagleWksDBParserInternal::SubDocument(*this, getInput(), footer));
373 MWAWHeaderFooter hf(MWAWHeaderFooter::FOOTER, MWAWHeaderFooter::ALL);
374 hf.m_subDocument=subDoc;
375 ps.setHeaderFooter(hf);
376 }
377 ps.setPageSpan(numPages);
378 pageList.push_back(ps);
379
380 MWAWSpreadsheetListenerPtr listen(new MWAWSpreadsheetListener(*getParserState(), pageList, documentInterface));
381 setSpreadsheetListener(listen);
382 listen->startDocument();
383 }
384
385
386 ////////////////////////////////////////////////////////////
387 //
388 // Intermediate level
389 //
390 ////////////////////////////////////////////////////////////
391 bool BeagleWksDBParser::createZones()
392 {
393 readRSRCZones();
394 MWAWInputStreamPtr input = getInput();
395 if (input->seek(66, librevenge::RVNG_SEEK_SET) || !readPrintInfo())
396 return false;
397 long pos = input->tell();
398 if (!input->checkPosition(pos+70)) {
399 MWAW_DEBUG_MSG(("BeagleWksDBParser::createZones: the file can not contains Zones\n"));
400 return false;
401 }
402
403 // now read the list of zones
404 libmwaw::DebugStream f;
405 input->seek(pos, librevenge::RVNG_SEEK_SET);
406 f << "Entries(Zones):";
407 for (int i=0; i<7; ++i) { // checkme: at least 2 zones, maybe 7
408 MWAWEntry entry;
409 entry.setBegin(input->readLong(4));
410 entry.setLength(input->readLong(4));
411 entry.setId(static_cast<int>(input->readLong(2)));
412 if (entry.length()==0) continue;
413 entry.setType(i==1?"Frame":"Unknown");
414 f << entry.type() << "[" << entry.id() << "]="
415 << std::hex << entry.begin() << "<->" << entry.end() << ",";
416 if (!entry.valid() || !input->checkPosition(entry.end())) {
417 f << "###";
418 if (i<2) {
419 MWAW_DEBUG_MSG(("BeagleWksDBParser::createZones: can not read the header zone, stop\n"));
420 ascii().addPos(pos);
421 ascii().addNote(f.str().c_str());
422 return false;
423 }
424 MWAW_DEBUG_MSG(("BeagleWksDBParser::createZones: can not zones entry %d\n",i));
425 continue;
426 }
427 m_state->m_typeEntryMap.insert
428 (std::multimap<std::string, MWAWEntry>::value_type(entry.type(),entry));
429 }
430 ascii().addPos(pos);
431 ascii().addNote(f.str().c_str());
432
433 // now parse the different zones
434 auto it=m_state->m_typeEntryMap.find("FontNames");
435 if (it!=m_state->m_typeEntryMap.end())
436 m_structureManager->readFontNames(it->second);
437 it=m_state->m_typeEntryMap.find("Frame");
438 if (it!=m_state->m_typeEntryMap.end())
439 m_structureManager->readFrame(it->second);
440
441 // now parse the different zones
442 for (auto const &eIt : m_state->m_typeEntryMap) {
443 MWAWEntry const &entry=eIt.second;
444 if (entry.isParsed())
445 continue;
446 f.str("");
447 f << "Entries(" << entry.type() << ")[" << entry.id() << "]:";
448 ascii().addPos(entry.begin());
449 ascii().addNote(f.str().c_str());
450 ascii().addPos(entry.end());
451 ascii().addNote("_");
452 }
453
454 input->seek(m_state->m_databaseBegin, librevenge::RVNG_SEEK_SET);
455 pos = input->tell();
456 if (!m_structureManager->readDocumentInfo())
457 input->seek(pos, librevenge::RVNG_SEEK_SET);
458 pos = input->tell();
459 if (!m_structureManager->readDocumentPreferences())
460 input->seek(pos, librevenge::RVNG_SEEK_SET);
461 if (!readDatabase())
462 return m_state->m_database.m_records.size()!=0;
463 pos=input->tell();
464 auto N=static_cast<int>(input->readULong(2));
465 if (N==0) {
466 ascii().addPos(pos);
467 ascii().addNote("_");
468 }
469 else {
470 // unsure about the parsing of this zone: find N=2+000602000d02
471 f.str("");
472 f << "Entries(UnknZone0):";
473 if (!input->checkPosition(pos+2+3*N)) {
474 MWAW_DEBUG_MSG(("BeagleWksDBParser::createZones: can not read UnkZone0\n"));
475 f << "###";
476 ascii().addPos(pos);
477 ascii().addNote(f.str().c_str());
478 return true;
479 }
480 f << "unkn0=[";
481 for (int i=0; i< N; ++i)
482 f << input->readLong(2) << ",";
483 f << "],";
484 f << "unkn1=[";
485 for (int i=0; i< N; ++i)
486 f << input->readLong(1) << ",";
487 f << "],";
488 }
489 ascii().addPos(pos);
490 ascii().addNote(f.str().c_str());
491 pos=input->tell();
492 N=static_cast<int>(input->readULong(2));
493 if (N==0) {
494 ascii().addPos(pos);
495 ascii().addNote("_");
496 }
497 else {
498 MWAW_DEBUG_MSG(("BeagleWksDBParser::createZones: find data in UnkZone1\n"));
499 f.str("");
500 f << "Entries(UnknZone1):###";
501 ascii().addPos(pos);
502 ascii().addNote(f.str().c_str());
503 return true;
504 }
505
506 pos=input->tell();
507 f.str("");
508 f << "Entries(Memo):";
509 auto dSz=static_cast<long>(input->readULong(2));
510 long endPos=pos+2+dSz;
511 N=static_cast<int>(input->readULong(2));
512 if (dSz<2+2*long(N) || !input->checkPosition(endPos)) {
513 MWAW_DEBUG_MSG(("BeagleWksDBParser::createZones: find data in UnkZone1\n"));
514 f << "###";
515 ascii().addPos(pos);
516 ascii().addNote(f.str().c_str());
517 return true;
518 }
519 ascii().addPos(pos);
520 ascii().addNote(f.str().c_str());
521 for (int i=0; i<N; ++i) {
522 pos=input->tell();
523 f.str("");
524 f << "Memo-"<< i << ":";
525 auto val=static_cast<int>(input->readLong(1));
526 if (val) f << "f0=" << val << ",";
527 auto sSz=static_cast<int>(input->readULong(1));
528 if (pos+2+sSz>endPos) {
529 MWAW_DEBUG_MSG(("BeagleWksDBParser::createZones: can not read a memo\n"));
530 f << "###";
531 ascii().addPos(pos);
532 ascii().addNote(f.str().c_str());
533 return true;
534 }
535 MWAWEntry memo;
536 memo.setBegin(input->tell());
537 memo.setLength(sSz);
538 m_state->m_database.m_memos.push_back(memo);
539 std::string text("");
540 for (int c=0; c<sSz; ++c) text+=char(input->readULong(1));
541 f << text << ",";
542 ascii().addPos(pos);
543 ascii().addNote(f.str().c_str());
544 }
545 input->seek(endPos, librevenge::RVNG_SEEK_SET);
546
547 pos=input->tell();
548 f.str("");
549 f << "Entries(UnknZone2):";
550 dSz=long(input->readULong(2));
551 endPos=pos+6+dSz;
552 auto val=static_cast<int>(input->readULong(2));
553 if ((dSz%2) || val!=0xeb || !input->checkPosition(endPos)) {
554 MWAW_DEBUG_MSG(("BeagleWksDBParser::createZones: can not read zone2\n"));
555 f << "###";
556 ascii().addPos(pos);
557 ascii().addNote(f.str().c_str());
558 return true;
559 }
560 N=static_cast<int>(input->readULong(2));
561 if (N) f << "N=" << N << ",";
562 if (dSz==2*N) {
563 f << "unkn=[";
564 for (int i=0; i<N; ++i)
565 f << input->readLong(2) << ",";
566 f << "],";
567 }
568 else
569 f << "###,";
570 input->seek(endPos, librevenge::RVNG_SEEK_SET);
571 ascii().addPos(pos);
572 ascii().addNote(f.str().c_str());
573
574 /* now the formula zone */
575 auto &fields=m_state->m_database.m_fields;
576 int n=0;
577 for (auto &field : fields) {
578 ++n;
579 if (field.m_type!=BeagleWksDBParserInternal::Cell::Formula)
580 continue;
581 pos=input->tell();
582 f.str("");
583 f << "Entries(Formula):";
584 auto id=static_cast<int>(input->readLong(2));
585 if (id!=n-1) f << "#id=" << id << ",";
586 dSz=static_cast<int>(input->readULong(2));
587 endPos=pos+4+dSz;
588 if (dSz==0 || !input->checkPosition(pos+4+dSz)) {
589 MWAW_DEBUG_MSG(("BeagleWksDBParser::createZones: can not read a formula\n"));
590 f << "###";
591 ascii().addPos(pos);
592 ascii().addNote(f.str().c_str());
593 return true;
594 }
595 std::vector<MWAWCellContent::FormulaInstruction> formula;
596 std::string error("");
597 if (m_structureManager->readFormula(endPos, MWAWVec2i(id,9), formula, error)) {
598 field.m_content.m_formula = formula;
599 field.m_content.m_contentType = MWAWCellContent::C_FORMULA;
600 }
601 else
602 f << "###";
603 for (auto const &fo : formula)
604 f << fo;
605 input->seek(endPos, librevenge::RVNG_SEEK_SET);
606 ascii().addPos(pos);
607 ascii().addNote(f.str().c_str());
608 }
609 /*
610 now find
611 000000000001000100000000 or
612 000200000003000100020000
613 */
614 ascii().addPos(input->tell());
615 ascii().addNote("Entries(ZoneEnd)");
616 return true;
617 }
618
619 bool BeagleWksDBParser::readRSRCZones()
620 {
621 MWAWRSRCParserPtr rsrcParser = getRSRCParser();
622 if (!rsrcParser)
623 return true;
624
625 auto const &entryMap = rsrcParser->getEntriesMap();
626 // the 1 zone
627 char const *zNames[] = {"wPos", "DMPF" };
628 for (int z = 0; z < 2; ++z) {
629 auto it = entryMap.lower_bound(zNames[z]);
630 while (it != entryMap.end()) {
631 if (it->first != zNames[z])
632 break;
633 MWAWEntry const &entry = it++->second;
634 switch (z) {
635 case 0: // 1001
636 m_structureManager->readwPos(entry);
637 break;
638 case 1: // find in one file with id=4661 6a1f 4057
639 m_structureManager->readFontStyle(entry);
640 break;
641 /* find also
642 - edpt: see sendPicture
643 - DMPP: the paragraph style
644 - sect and alis: position?, alis=filesystem alias(dir, filename, path...)
645 */
646 default:
647 break;
648 }
649 }
650 }
651 return true;
652 }
653
654 ////////////////////////////////////////////////////////////
655 // read the print info
656 ////////////////////////////////////////////////////////////
657 bool BeagleWksDBParser::readPrintInfo()
658 {
659 MWAWInputStreamPtr input = getInput();
660 long pos = input->tell();
661 if (!input->checkPosition(pos+0x70))
662 return false;
663
664 libmwaw::DebugStream f;
665 // print info
666 libmwaw::PrinterInfo info;
667 if (!info.read(input)) return false;
668 f << "Entries(PrintInfo):"<< info;
669
670 MWAWVec2i paperSize = info.paper().size();
671 MWAWVec2i pageSize = info.page().size();
672 if (pageSize.x() <= 0 || pageSize.y() <= 0 ||
673 paperSize.x() <= 0 || paperSize.y() <= 0) return false;
674
675 // define margin from print info
676 MWAWVec2i lTopMargin= -1 * info.paper().pos(0);
677 MWAWVec2i rBotMargin=info.paper().pos(1) - info.page().pos(1);
678
679 // move margin left | top
680 int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0;
681 int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0;
682 lTopMargin -= MWAWVec2i(decalX, decalY);
683 rBotMargin += MWAWVec2i(decalX, decalY);
684
685 // decrease right | bottom
686 int rightMarg = rBotMargin.x() -10;
687 if (rightMarg < 0) rightMarg=0;
688 int botMarg = rBotMargin.y() -50;
689 if (botMarg < 0) botMarg=0;
690
691 getPageSpan().setMarginTop(lTopMargin.y()/72.0);
692 getPageSpan().setMarginBottom(botMarg/72.0);
693 getPageSpan().setMarginLeft(lTopMargin.x()/72.0);
694 getPageSpan().setMarginRight(rightMarg/72.0);
695 getPageSpan().setFormLength(paperSize.y()/72.);
696 getPageSpan().setFormWidth(paperSize.x()/72.);
697
698 ascii().addPos(pos);
699 ascii().addNote(f.str().c_str());
700 input->seek(pos+0x78, librevenge::RVNG_SEEK_SET);
701 if (long(input->tell()) != pos+0x78) {
702 MWAW_DEBUG_MSG(("BeagleWksDBParser::readPrintInfo: file is too short\n"));
703 return false;
704 }
705 ascii().addPos(input->tell());
706
707 return true;
708 }
709
710 ////////////////////////////////////////////////////////////
711 // database
712 ////////////////////////////////////////////////////////////
713 bool BeagleWksDBParser::readDatabase()
714 {
715 if (!readFields() || !readLayouts()) return false;
716
717 MWAWInputStreamPtr &input= getInput();
718 long pos=input->tell();
719 if (!input->checkPosition(pos+6)) {
720 MWAW_DEBUG_MSG(("BeagleWksDBParser::readDatabase: can not find the database header\n"));
721 return false;
722 }
723 libmwaw::DebugStream f;
724 f << "Entries(DbRow):";
725 int val;
726 for (int i=0; i<2; ++i) { // f0=0|1
727 val =static_cast<int>(input->readLong(2));
728 if (val) f << "f" << i << "=" << val << ",";
729 }
730 val =static_cast<int>(input->readLong(2));
731 if (val!=7) f << "f2=" << val << ",";
732 auto N=static_cast<int>(input->readLong(2));
733 f << "N=" << N << ",";
734 val =static_cast<int>(input->readLong(2));
735 if (val) f << "f3=" << val << ",";
736 ascii().addPos(pos);
737 ascii().addNote(f.str().c_str());
738
739 for (int i=0; i<=N; ++i) {
740 if (!readRow()) return false;
741 }
742
743 pos=input->tell();
744 f.str("");
745 f << "Entries(DbFld):";
746 val =static_cast<int>(input->readLong(2));
747 if (val) f << "f0=" << val << ",";
748 N=static_cast<int>(input->readULong(2));
749 f << "N=" << N << ",";
750 val =static_cast<int>(input->readLong(2));
751 if (val) f << "f1=" << val << ",";
752 auto dSz =static_cast<int>(input->readULong(2));
753 f << "dSz=" << dSz << ",";
754 if (dSz<14 || dSz>(input->size()-pos-8)/(N+1)) {
755 MWAW_DEBUG_MSG(("BeagleWksDBParser::readDatabase: can not find the database field format\n"));
756 f << "###";
757 ascii().addPos(pos);
758 ascii().addNote(f.str().c_str());
759 return false;
760 }
761 ascii().addPos(pos);
762 ascii().addNote(f.str().c_str());
763
764 auto &database=m_state->m_database;
765 size_t numFields=database.m_fields.size();
766 if (N+1>static_cast<int>(numFields)) {
767 MWAW_DEBUG_MSG(("BeagleWksDBParser::readDatabase: the number of field seems too small, must increase them\n"));
768 database.m_fields.resize(size_t(N+1));
769 }
770 std::string extra("");
771 for (int i=0; i<=N; ++i) {
772 pos=input->tell();
773 f.str("");
774 f << "DbFld" << i << ":";
775 val=static_cast<int>(input->readULong(2));
776 if (val!=1) f << "f0=" << val << ",";
777 val=static_cast<int>(input->readLong(2));
778 if (val!=0x4b) f << "f1=" << val << ",";
779 // checkme: maybe we must use the read id here...
780 auto &field=database.m_fields[size_t(i)];
781 if (readFormat(field, extra)) {
782 f << extra << ",";
783 if (dSz>14) { // find also dSz=18 which add c0000034 here
784 ascii().addDelimiter(input->tell(),'|');
785 input->seek(pos+dSz-2, librevenge::RVNG_SEEK_SET);
786 ascii().addDelimiter(input->tell(),'|');
787 }
788 val=static_cast<int>(input->readLong(2));
789 if (val!=i) f << "#id=" << val << ",";
790 }
791 input->seek(pos+dSz, librevenge::RVNG_SEEK_SET);
792 ascii().addPos(pos);
793 ascii().addNote(f.str().c_str());
794 }
795 return true;
796 }
797
798 /* read a cell format
799
800 \note: this function is very simillar to BeagleWksSSParser readCellSheet, maybe, we can merge them
801 */
802 bool BeagleWksDBParser::readFormat(MWAWCell &cell, std::string &extra)
803 {
804 MWAWInputStreamPtr &input= getInput();
805 long pos=input->tell();
806 if (!input->checkPosition(pos+8)) {
807 MWAW_DEBUG_MSG(("BeagleWksDBParser::readFormat: the zone is too short\n"));
808 return false;
809 }
810 libmwaw::DebugStream f;
811 MWAWFont font;
812 MWAWCell::Format format;
813 auto val=static_cast<int>(input->readULong(1));
814 if (val) f << "f0=" << std::hex << val << std::dec << ",";
815 val=static_cast<int>(input->readLong(1));
816 if (val>0)
817 font.setSize(float(val));
818 val=static_cast<int>(input->readLong(2));
819 if (val>=0)
820 font.setId(val);
821 auto flag=static_cast<int>(input->readULong(1));
822 uint32_t flags=0;
823 if (flag&0x8) flags |= MWAWFont::boldBit;
824 if (flag&0x10) flags |= MWAWFont::italicBit;
825 if (flag&0x20) font.setUnderlineStyle(MWAWFont::Line::Simple);
826 if (flag&0x40) flags |= MWAWFont::embossBit;
827 if (flag&0x80) flags |= MWAWFont::shadowBit;
828 font.setFlags(flags);
829 f << font.getDebugString(getParserState()->m_fontConverter);
830 if (flag&7) f << "flags=" << (flag&7) << ",";
831
832 auto form=static_cast<int>(input->readULong(1));
833 if (form) {
834 if (form & 0x10)
835 format.m_thousandHasSeparator=true;
836 switch (form>>5) {
837 case 0: // generic (or picture, memo, etc)
838 break;
839 case 1:
840 format.m_format=MWAWCell::F_NUMBER;
841 format.m_numberFormat=MWAWCell::F_NUMBER_CURRENCY;
842 break;
843 case 2:
844 format.m_format=MWAWCell::F_NUMBER;
845 format.m_numberFormat=MWAWCell::F_NUMBER_PERCENT;
846 break;
847 case 3:
848 format.m_format=MWAWCell::F_NUMBER;
849 format.m_numberFormat=MWAWCell::F_NUMBER_SCIENTIFIC;
850 break;
851 case 4:
852 format.m_format=MWAWCell::F_NUMBER;
853 format.m_numberFormat=MWAWCell::F_NUMBER_DECIMAL;
854 break;
855 case 5:
856 format.m_format=MWAWCell::F_DATE;
857 format.m_DTFormat="%m/%d/%y";
858 break;
859 case 6:
860 switch (form & 0x7) {
861 case 0:
862 format.m_format=MWAWCell::F_DATE;
863 format.m_DTFormat="%b %d, %Y";
864 break;
865 case 1:
866 format.m_format=MWAWCell::F_DATE;
867 format.m_DTFormat="%B %d, %Y";
868 break;
869 case 2:
870 format.m_format=MWAWCell::F_DATE;
871 format.m_DTFormat="%a, %b %d, %Y";
872 break;
873 case 3:
874 format.m_format=MWAWCell::F_DATE;
875 format.m_DTFormat="%A, %B %d, %Y";
876 break;
877 case 4:
878 format.m_format=MWAWCell::F_TIME;
879 format.m_DTFormat="%I:%M %p";
880 break;
881 case 5:
882 format.m_format=MWAWCell::F_TIME;
883 format.m_DTFormat="%I:%M:%S %p";
884 break;
885 case 6:
886 format.m_format=MWAWCell::F_TIME;
887 format.m_DTFormat="%H:%M";
888 break;
889 case 7:
890 format.m_format=MWAWCell::F_TIME;
891 format.m_DTFormat="%H:%M:%S";
892 break;
893 default:
894 break;
895 }
896 form &= 0x8;
897 break;
898 default:
899 f << "#form=7:";
900 break;
901 }
902 if (form & 0xf)
903 format.m_digits=(form & 0xf);
904 f << format;
905 }
906
907 val=static_cast<int>(input->readULong(1));
908 if (val&0xF0) f << "col?="<<std::hex << (val>>4) << std::dec << ",";
909 if (val&0xf) f << "bord?="<<std::hex << (val&0xF) << std::dec << ",";
910
911 val=static_cast<int>(input->readULong(1));
912 switch ((val>>5)&7) {
913 case 0:
914 cell.setHAlignment(MWAWCell::HALIGN_LEFT);
915 f << "left,";
916 break;
917 case 1:
918 cell.setHAlignment(MWAWCell::HALIGN_RIGHT);
919 f << "right,";
920 break;
921 case 2:
922 cell.setHAlignment(MWAWCell::HALIGN_CENTER);
923 f << "center,";
924 break;
925 case 3: // default
926 break;
927 case 4:
928 cell.setHAlignment(MWAWCell::HALIGN_LEFT);
929 f << "filled,";
930 break;
931 default:
932 f << "#align=" << ((val>>5)&7) << ",";
933 break;
934 }
935 cell.setFormat(format);
936 // checkme, this flags seem important, but I am not sure of their meanings
937 if (val&0x10)
938 cell.setFont(font);
939 val &= 0xF;
940 if (val!=0x3) f << "flags2=" << std::hex << val << std::dec << ",";
941 extra=f.str();
942 return true;
943 }
944
945
946 ////////////////////////////////////////////////////////////
947 // read the row data
948 ////////////////////////////////////////////////////////////
949 bool BeagleWksDBParser::readRow()
950 {
951 MWAWInputStreamPtr &input= getInput();
952 long pos=input->tell();
953 libmwaw::DebugStream f;
954
955 auto id=static_cast<int>(input->readLong(2));
956 f << "DbRow" << id << ":";
957 auto val=static_cast<int>(input->readLong(2));
958 if (val) f << "f0=" << val << ",";
959 auto dSz=long(input->readULong(2));
960 long endPos=pos+6+dSz;
961 if (dSz<18 || !input->checkPosition(endPos)) {
962 MWAW_DEBUG_MSG(("BeagleWksDBParser::readRow: can not find the database row %d\n", id));
963 f << "###";
964 ascii().addPos(pos);
965 ascii().addNote(f.str().c_str());
966 return false;
967 }
968 val=static_cast<int>(input->readLong(2)); // 0|78|86
969 if (val) f << "f1=" << val << ",";
970 val=static_cast<int>(input->readLong(2));
971 if (val!=-1) f << "f2=" << val << ",";
972 f << "fl?=[" << std::hex;
973 for (int i=0; i<4; ++i) {
974 val=static_cast<int>(input->readULong(2));
975 if (val) f << val << ",";
976 else f << "_,";
977 }
978 f << std::dec << "],";
979 // now a format
980 f << "fId=" << input->readULong(2) << ",";
981 f << "fSz=" << input->readULong(2) << ",";
982 val=static_cast<int>(input->readULong(2));
983 if (val!=dSz) f << "#dSz1=" << val << ",";
984
985 ascii().addPos(pos);
986 ascii().addNote(f.str().c_str());
987
988 auto &database=m_state->m_database;
989 database.m_records.resize(database.m_records.size()+1);
990 auto &records=database.m_records.back();
991 int fd=0;
992 for (auto const &field : database.m_fields) {
993 pos=input->tell();
994 if (pos>=endPos) break;
995 f.str("");
996 f << "DbRow" << id << "-" << fd++ << ":";
997 auto fSz=static_cast<int>(input->readULong(1));
998 if (fSz==0xFF) {
999 ascii().addPos(pos);
1000 ascii().addNote("_");
1001 break;
1002 }
1003 if (pos+fSz+2>endPos) {
1004 input->seek(pos, librevenge::RVNG_SEEK_SET);
1005 MWAW_DEBUG_MSG(("BeagleWksDBParser::readRow: file size seems bad\n"));
1006 break;
1007 }
1008 val=static_cast<int>(input->readULong(1));
1009 if (val!=0x20) f << "fl=" << std::hex << val << std::dec << ",";
1010 MWAWCellContent content;
1011 if (fSz && fSz<8) {
1012 MWAW_DEBUG_MSG(("BeagleWksDBParser::readRow: find some very short field\n"));
1013 f << "###sz=" << fSz << ",";
1014 }
1015 else if (fSz) {
1016 for (int i=0; i<4; ++i) {
1017 val=static_cast<int>(input->readULong(2));
1018 if (val) f << "f" << i << "=" << std::hex << val << std::dec << ",";
1019 }
1020 switch (field.m_type) {
1021 case BeagleWksDBParserInternal::Cell::Formula:
1022 if (fSz>20) break;
1023 val=static_cast<int>(input->readULong(2));
1024 if (val) f << "g0=" << std::hex << val << std::dec << ",";
1025 MWAW_FALLTHROUGH;
1026 case BeagleWksDBParserInternal::Cell::Memo: // memoId
1027 case BeagleWksDBParserInternal::Cell::Picture: // pictId
1028 case BeagleWksDBParserInternal::Cell::Number:
1029 case BeagleWksDBParserInternal::Cell::Date:
1030 case BeagleWksDBParserInternal::Cell::Time: {
1031 // will be changed by sendDatabase for memo, formula, ...
1032 content.m_contentType=MWAWCellContent::C_NUMBER;
1033 if (input->tell()+10>endPos) {
1034 MWAW_DEBUG_MSG(("BeagleWksDBParser::readRow: can not read some field\n"));
1035 f << "###";
1036 break;
1037 }
1038 double value;
1039 bool isNan;
1040 if (!input->readDouble10(value,isNan)) {
1041 f << "#" << value << ",";
1042 break;
1043 }
1044 content.setValue(value);
1045 f << value << ",";
1046 break;
1047 }
1048 case BeagleWksDBParserInternal::Cell::Text: {
1049 content.m_contentType=MWAWCellContent::C_TEXT;
1050 content.m_textEntry.setBegin(input->tell());
1051 content.m_textEntry.setEnd(pos+fSz+2);
1052 std::string text("");
1053 while (input->tell()<pos+fSz+2) text+=char(input->readULong(1));
1054 f << text << ",";
1055 }
1056 break;
1057 case BeagleWksDBParserInternal::Cell::Unknown:
1058 #if !defined(__clang__)
1059 default:
1060 #endif
1061 f << "type=" << static_cast<int>(field.m_type) << ",";
1062 break;
1063 }
1064 }
1065 records.push_back(content);
1066 if ((fSz%2)) ++fSz;
1067 input->seek(pos+fSz+2, librevenge::RVNG_SEEK_SET);
1068 ascii().addPos(pos);
1069 ascii().addNote(f.str().c_str());
1070 }
1071
1072 pos=input->tell();
1073 if (pos!=endPos) {
1074 MWAW_DEBUG_MSG(("BeagleWksDBParser::readRow: find some extra data\n"));
1075 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1076 ascii().addPos(pos);
1077 ascii().addNote("DbRow:#end");
1078 }
1079 return true;
1080 }
1081
1082 ////////////////////////////////////////////////////////////
1083 // read the fields
1084 ////////////////////////////////////////////////////////////
1085 bool BeagleWksDBParser::readFields()
1086 {
1087 MWAWInputStreamPtr &input= getInput();
1088 auto &database=m_state->m_database;
1089 long pos=input->tell();
1090 if (!input->checkPosition(pos+6)) {
1091 MWAW_DEBUG_MSG(("BeagleWksDBParser::readFields: can not find the field zone\n"));
1092 return false;
1093 }
1094 libmwaw::DebugStream f;
1095 f << "Entries(Field):";
1096 auto val=static_cast<int>(input->readLong(2));
1097 if (val) f << "f0=" << val << ",";
1098 val=static_cast<int>(input->readLong(2));
1099 if (val!=0x2c) // may a type
1100 f << "f1=" << val << ",";
1101 database.m_numFields=static_cast<int>(input->readULong(2));
1102 f << "num[fields]=" << database.m_numFields << ",";
1103 if (!input->checkPosition(pos+database.m_numFields*64)) {
1104 MWAW_DEBUG_MSG(("BeagleWksDBParser::readFields: can not find the fields zone\n"));
1105 f << "###";
1106 ascii().addPos(pos);
1107 ascii().addNote(f.str().c_str());
1108 return false;
1109 }
1110 ascii().addPos(pos);
1111 ascii().addNote(f.str().c_str());
1112
1113 for (int fld=0; fld<database.m_numFields; ++fld) {
1114 pos=input->tell();
1115 f.str("");
1116 f << "Field-" << fld << ":";
1117 BeagleWksDBParserInternal::Cell field;
1118 auto dSz=long(input->readULong(2));
1119 long endPos=pos+4+dSz;
1120 if (dSz<0x3c || !input->checkPosition(endPos)) {
1121 MWAW_DEBUG_MSG(("BeagleWksDBParser::readFields: can not read a field\n"));
1122 f << "###";
1123 ascii().addPos(pos);
1124 ascii().addNote(f.str().c_str());
1125 return false;
1126 }
1127 auto id=static_cast<int>(input->readLong(2));
1128 if (id) f << "id=" << id << ",";
1129 auto sSz=static_cast<int>(input->readULong(1));
1130 if (sSz+1>dSz) {
1131 MWAW_DEBUG_MSG(("BeagleWksDBParser::readFields: can not read a field\n"));
1132 f << "###";
1133 ascii().addPos(pos);
1134 ascii().addNote(f.str().c_str());
1135 return false;
1136 }
1137 for (int i=0; i<sSz; ++i) field.m_name += char(input->readULong(1));
1138 f << "\"" << field.m_name << "\",";
1139 ascii().addDelimiter(input->tell(),'|');
1140 input->seek(endPos-10, librevenge::RVNG_SEEK_SET);
1141 ascii().addDelimiter(input->tell(),'|');
1142
1143 MWAWCell::Format format;
1144 auto &content=field.m_content;
1145 auto type=static_cast<int>(input->readLong(1));
1146 switch (type) {
1147 case 0:
1148 field.m_type = BeagleWksDBParserInternal::Cell::Text;
1149 content.m_contentType=MWAWCellContent::C_TEXT;
1150 f << "text,";
1151 break;
1152 case 1:
1153 field.m_type = BeagleWksDBParserInternal::Cell::Number;
1154 content.m_contentType=MWAWCellContent::C_NUMBER;
1155 f << "number,";
1156 break;
1157 case 2:
1158 field.m_type = BeagleWksDBParserInternal::Cell::Date;
1159 content.m_contentType=MWAWCellContent::C_NUMBER;
1160 f << "date,";
1161 break;
1162 case 3:
1163 field.m_type = BeagleWksDBParserInternal::Cell::Time;
1164 content.m_contentType=MWAWCellContent::C_NUMBER;
1165 f << "time,";
1166 break;
1167 case 4:
1168 field.m_type = BeagleWksDBParserInternal::Cell::Picture;
1169 f << "picture,";
1170 break;
1171 case 5:
1172 field.m_type = BeagleWksDBParserInternal::Cell::Formula;
1173 content.m_contentType=MWAWCellContent::C_FORMULA;
1174 f << "formula,";
1175 break;
1176 case 6:
1177 field.m_type = BeagleWksDBParserInternal::Cell::Memo;
1178 content.m_contentType=MWAWCellContent::C_TEXT;
1179 f << "memo,";
1180 break;
1181 default:
1182 f << "#type=" << type << ",";
1183 break;
1184 }
1185 f << "form?=" << std::hex << input->readULong(1) << std::dec << ",";
1186 f << "id2=" << std::hex << input->readULong(4) << std::dec << ",";
1187 val=static_cast<int>(input->readLong(2)); // 0|-1
1188 if (val!=-1) f << "g0=" << val << ",";
1189 f << "g1=" << input->readLong(2) << ",";
1190 database.m_fields.push_back(field);
1191
1192 ascii().addPos(pos);
1193 ascii().addNote(f.str().c_str());
1194 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1195 }
1196 return true;
1197 }
1198
1199 ////////////////////////////////////////////////////////////
1200 // read the layouts
1201 ////////////////////////////////////////////////////////////
1202 bool BeagleWksDBParser::readLayouts()
1203 {
1204 MWAWInputStreamPtr &input= getInput();
1205 long pos=input->tell();
1206 if (!input->checkPosition(pos+6)) {
1207 MWAW_DEBUG_MSG(("BeagleWksDBParser::readLayouts: can not find the layout zone\n"));
1208 return false;
1209 }
1210 libmwaw::DebugStream f;
1211 f << "Entries(Layout):";
1212 auto val=static_cast<int>(input->readLong(2));
1213 if (val) f << "f0=" << val << ",";
1214 val=static_cast<int>(input->readLong(2));
1215 if (val!=0x29) // may a type
1216 f << "f1=" << val << ",";
1217 auto numLayouts=static_cast<int>(input->readULong(2));
1218 f << "num[layout]=" << numLayouts << ",";
1219 ascii().addPos(pos);
1220 ascii().addNote(f.str().c_str());
1221
1222 for (int layout=0; layout<numLayouts; ++layout) {
1223 if (!readLayout(layout)) return false;
1224 }
1225 return true;
1226 }
1227
1228 bool BeagleWksDBParser::readLayout(int id)
1229 {
1230 MWAWInputStreamPtr &input= getInput();
1231 libmwaw::DebugStream f;
1232 f << "Layout-" << id << "[A]:";
1233
1234 long pos=input->tell();
1235 auto readId=static_cast<int>(input->readULong(1));
1236 auto dSz=long(input->readULong(2));
1237 long endPos=pos+1+dSz;
1238 if (dSz<100 || !input->checkPosition(endPos)) {
1239 MWAW_DEBUG_MSG(("BeagleWksDBParser::readLayouts: can find a layout\n"));
1240 f << "###";
1241 ascii().addPos(pos);
1242 ascii().addNote(f.str().c_str());
1243 return false;
1244 }
1245 if (readId!=id) f << "#id=" << readId << ",";
1246 auto val=static_cast<int>(input->readLong(2));
1247 if (val) f << "f0=" << val << ",";
1248 val=static_cast<int>(input->readULong(1));
1249 if (val!=readId) f << "#id1=" << val << ",";
1250 auto sSz=static_cast<int>(input->readULong(1));
1251 if (sSz>30) {
1252 MWAW_DEBUG_MSG(("BeagleWksDBParser::readLayouts: can find layout string\n"));
1253 f << "###sSz=" << sSz << ",";
1254 ascii().addPos(pos);
1255 ascii().addNote(f.str().c_str());
1256 return false;
1257 }
1258 std::string name("");
1259 for (int i=0; i<sSz; ++i) name+=char(input->readULong(1));
1260 f << name << ",";
1261 input->seek(pos+37, librevenge::RVNG_SEEK_SET);
1262 f << "ids=[" << std::hex;
1263 for (int i=0; i<3; ++i) f << input->readULong(4) << ",";
1264 f << std::dec << "],";
1265 val=static_cast<int>(input->readLong(2)); // small number
1266 f << "N=" << val << ",";
1267 for (int i=0; i<6; ++i) { // f4=0|78, f5=0|68
1268 static int const expected[]= {0x100, 0, 0, 0, 0, 0xffff };
1269 val = static_cast<int>(input->readULong(2));
1270 if (val!=expected[i])
1271 f << "f" << i+2 << "=" << std::hex << val << std::dec << ",";
1272 }
1273 f << "g0=" << std::hex << input->readULong(4) << std::dec << ",";
1274 f << "id2s=[" << std::hex;
1275 for (int i=0; i<4; ++i) {
1276 val=static_cast<int>(input->readULong(i==2 ? 2 : 4));
1277 if (val)
1278 f << val << ",";
1279 else
1280 f << "_,";
1281 }
1282 f << std::dec << "],";
1283 ascii().addPos(pos);
1284 ascii().addNote(f.str().c_str());
1285
1286 pos=input->tell();
1287 f.str("");
1288 f << "Layout-" << id << "[B]:";
1289 double margins[4];
1290 f << "margins=[";
1291 for (double &margin : margins) {
1292 margin=double(input->readLong(4))/72.;
1293 f << margin << ",";
1294 }
1295 f << "],";
1296 for (int i=0; i<3; ++i) {
1297 val=static_cast<int>(input->readLong(2));
1298 if (val) f << "f" << i << "=" << val << ",";
1299 }
1300 f << "id=" << std::hex << input->readULong(4) << std::dec << ",";
1301 ascii().addDelimiter(input->tell(),'|');
1302 ascii().addPos(pos);
1303 ascii().addNote(f.str().c_str());
1304
1305 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1306 pos=input->tell();
1307 f.str("");
1308 f << "Layout-" << id << "[C]:";
1309 for (int i=0; i<8; i++) {
1310 val=static_cast<int>(input->readLong(1));
1311 if (val==1) f << "fl" << i << ",";
1312 else if (val) f << "fl" << i << "=" << val << ",";
1313 }
1314 ascii().addPos(pos);
1315 ascii().addNote(f.str().c_str());
1316 //--- now some big unknown zones
1317 pos=input->tell();
1318 f.str("");
1319 f << "Layout-" << id << "[C0]:";
1320 ascii().addPos(pos);
1321 ascii().addNote(f.str().c_str());
1322 input->seek(pos+244, librevenge::RVNG_SEEK_SET);
1323
1324 pos=input->tell();
1325 f.str("");
1326 f << "Layout-" << id << "[C1]:";
1327 ascii().addPos(pos);
1328 ascii().addNote(f.str().c_str());
1329 input->seek(pos+178, librevenge::RVNG_SEEK_SET);
1330
1331 /* in some positions there seems be some blocks with size 12
1332 so let's try this decomposition ( which clearly does not works )..
1333 */
1334 for (int i=0; i<87; ++i) {
1335 pos=input->tell();
1336 f.str("");
1337 f << "Layout-" << id << "[C2:" << i << "]:";
1338 ascii().addPos(pos);
1339 ascii().addNote(f.str().c_str());
1340 input->seek(pos+12, librevenge::RVNG_SEEK_SET);
1341 }
1342
1343 pos=input->tell();
1344 f.str("");
1345 f << "Layout-" << id << "[C3]:";
1346 ascii().addPos(pos);
1347 ascii().addNote(f.str().c_str());
1348
1349 input->seek(pos+1420, librevenge::RVNG_SEEK_SET);
1350
1351 //--- end of unknown zone
1352 pos=input->tell();
1353 f.str("");
1354 f << "Layout-" << id << "[D]:";
1355 auto N=static_cast<int>(input->readULong(2));
1356 f << "N=" << N << ",";
1357 if (!input->checkPosition(pos+2+2*N)) {
1358 MWAW_DEBUG_MSG(("BeagleWksDBParser::readLayouts: can find zone D\n"));
1359 f << "###";
1360 ascii().addPos(pos);
1361 ascii().addNote(f.str().c_str());
1362 return false;
1363 }
1364 if (N) {
1365 f << "lists=[";
1366 for (int i=0; i<N; ++i) f << input->readLong(2) << ",";
1367 f << "],";
1368 }
1369 ascii().addPos(pos);
1370 ascii().addNote(f.str().c_str());
1371
1372 pos=input->tell();
1373 f.str("");
1374 f << "Layout-" << id << "[fields]:";
1375 val=static_cast<int>(input->readULong(2));
1376 auto type=static_cast<int>(input->readULong(2));
1377 N=static_cast<int>(input->readULong(2));
1378 f << "N=" << N << ",";
1379 if (type!=0x5a || !input->checkPosition(pos+6+36*N)) {
1380 MWAW_DEBUG_MSG(("BeagleWksDBParser::readLayouts: can find field zone \n"));
1381 f << "###";
1382 ascii().addPos(pos);
1383 ascii().addNote(f.str().c_str());
1384 return false;
1385 }
1386 ascii().addPos(pos);
1387 ascii().addNote(f.str().c_str());
1388 BeagleWksDBParserInternal::Cell field;
1389 for (int i=0; i<N; i++) {
1390 pos=input->tell();
1391 f.str("");
1392 f << "Layout-" << id << "[field" << i << "]:";
1393 id=static_cast<int>(input->readLong(2));
1394 if (id!=i) f << "id[field]=" << val << ",";
1395 val=static_cast<int>(input->readLong(2));
1396 if (val!=0x4b) f << "f0=" << val << ",";
1397 for (int j=0; j<2; ++j) { // j=0: field name, j=1: field content
1398 std::string extra("");
1399 if (!readFormat(field, extra)) {
1400 f << "###";
1401 break;
1402 }
1403 f << "field" << j << "=[" << extra << "],";
1404 }
1405 for (int j=0; j<2; ++j) {
1406 int dim[4];
1407 for (auto &k : dim) k=static_cast<int>(input->readLong(2));
1408 f << "box" << j << "=" << dim[1] << "x" << dim[0] << "<->" << dim[3] << "x" << dim[2] << ",";
1409 }
1410 ascii().addPos(pos);
1411 ascii().addNote(f.str().c_str());
1412 input->seek(pos+36, librevenge::RVNG_SEEK_SET);
1413 }
1414
1415 pos=input->tell();
1416 f.str("");
1417 f << "Layout-" << id << "[list]:";
1418 dSz=static_cast<int>(input->readULong(2));
1419 type=static_cast<int>(input->readLong(2));
1420 N=static_cast<int>(input->readULong(2));
1421 endPos=pos+6+dSz;
1422 if (2*N>dSz || type!=0x75 || !input->checkPosition(endPos)) {
1423 MWAW_DEBUG_MSG(("BeagleWksDBParser::readLayouts: can read zone F\n"));
1424 f << "###";
1425 ascii().addPos(pos);
1426 ascii().addNote(f.str().c_str());
1427 return false;
1428 }
1429 f << "N=" << N << ",";
1430 if (val!=0x75) f << "f0=" << val << ",";
1431 f << "lists=[";
1432 for (int i=0; i<N; ++i) f << input->readLong(2) << ",";
1433 f << "],";
1434 ascii().addPos(pos);
1435 ascii().addNote(f.str().c_str());
1436 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1437 return true;
1438 }
1439
1440 ////////////////////////////////////////////////////////////
1441 // send data
1442 ////////////////////////////////////////////////////////////
1443 bool BeagleWksDBParser::sendPageFrames()
1444 {
1445 auto const &frameMap = m_structureManager->getIdFrameMap();
1446 for (auto const &it : frameMap)
1447 sendFrame(it.second);
1448 return true;
1449 }
1450
1451 bool BeagleWksDBParser::sendFrame(BeagleWksStructManager::Frame const &frame)
1452 {
1453 MWAWPosition fPos(MWAWVec2f(0,0), frame.m_dim, librevenge::RVNG_POINT);
1454
1455 fPos.setPagePos(frame.m_page > 0 ? frame.m_page : 1, frame.m_origin);
1456 fPos.setRelativePosition(MWAWPosition::Page);
1457 fPos.m_wrapping = frame.m_wrap==0 ? MWAWPosition::WNone : MWAWPosition::WDynamic;
1458
1459 auto style=MWAWGraphicStyle::emptyStyle();
1460 style.setBorders(frame.m_bordersSet, frame.m_border);
1461 return sendPicture(frame.m_pictId, fPos, true, style);
1462 }
1463
1464 // read/send picture (edtp resource)
1465 bool BeagleWksDBParser::sendPicture
1466 (int pId, MWAWPosition const &pictPos, bool readEDTP, MWAWGraphicStyle const &style)
1467 {
1468 MWAWSpreadsheetListenerPtr listener=getSpreadsheetListener();
1469 if (!listener) {
1470 MWAW_DEBUG_MSG(("BeagleWksDBParser::sendPicture: can not find the listener\n"));
1471 return false;
1472 }
1473 auto rsrcParser = getRSRCParser();
1474 if (!rsrcParser) {
1475 static bool first=true;
1476 if (first) {
1477 MWAW_DEBUG_MSG(("BeagleWksDBParser::sendPicture: need access to resource fork to retrieve picture content\n"));
1478 first=false;
1479 }
1480 return true;
1481 }
1482
1483 librevenge::RVNGBinaryData data;
1484 if (!m_structureManager->readPicture(pId, data, readEDTP))
1485 return false;
1486
1487 listener->insertPicture(pictPos, MWAWEmbeddedObject(data, "image/pict"), style);
1488 return true;
1489 }
1490
1491 bool BeagleWksDBParser::sendText(MWAWEntry const &entry, bool /*headerFooter*/)
1492 {
1493 MWAWSpreadsheetListenerPtr listener=getSpreadsheetListener();
1494 if (!listener) {
1495 MWAW_DEBUG_MSG(("BeagleWksDBParser::sendText: can not find the listener\n"));
1496 return false;
1497 }
1498 if (!entry.valid()) {
1499 MWAW_DEBUG_MSG(("BeagleWksDBParser::sendText: can not find the entry\n"));
1500 return false;
1501 }
1502
1503 MWAWInputStreamPtr &input= getInput();
1504 long endPos=entry.end();
1505 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1506 while (!input->isEnd()) {
1507 long pos=input->tell();
1508 if (pos>=endPos) break;
1509 auto c = static_cast<unsigned char>(input->readULong(1));
1510 switch (c) {
1511 case 0x9:
1512 listener->insertTab();
1513 break;
1514 case 0xd:
1515 listener->insertEOL();
1516 break;
1517 default:
1518 listener->insertCharacter(static_cast<unsigned char>(c));
1519 break;
1520 }
1521 }
1522 return true;
1523 }
1524
1525 ////////////////////////////////////////////////////////////
1526 ////////////////////////////////////////////////////////////
1527 bool BeagleWksDBParser::sendDatabase()
1528 {
1529 MWAWSpreadsheetListenerPtr listener=getSpreadsheetListener();
1530 if (!listener) {
1531 MWAW_DEBUG_MSG(("BeagleWksDBParser::sendDatabase: I can not find the listener\n"));
1532 return false;
1533 }
1534 MWAWInputStreamPtr input=getInput();
1535 auto const &database=m_state->m_database;
1536 auto const &fields = database.m_fields;
1537 size_t numFields=fields.size();
1538 auto const &records=database.m_records;
1539 size_t numRecords=records.size();
1540 // fixme: use first layout colWidth here
1541 listener->openSheet(std::vector<float>(1,76), librevenge::RVNG_POINT, std::vector<int>(1,static_cast<int>(numRecords)), "Sheet0");
1542 for (size_t r=0; r<numRecords; ++r) {
1543 std::vector<MWAWCellContent> const &row=records[r];
1544 listener->openSheetRow(12, librevenge::RVNG_POINT);
1545 for (size_t c=0; c<row.size(); ++c) {
1546 if (c>=numFields) break;
1547 auto field=fields[c];
1548 database.updateWithContent(field, MWAWVec2i(int(c),int(r)), row[c]);
1549 if (field.empty()) continue;
1550
1551 auto const &content=field.m_content;
1552 listener->openSheetCell(field, content);
1553 if (content.m_contentType==MWAWCellContent::C_TEXT && content.m_textEntry.valid()) {
1554 listener->setFont(field.getFont());
1555 input->seek(content.m_textEntry.begin(), librevenge::RVNG_SEEK_SET);
1556 while (!input->isEnd() && input->tell()<content.m_textEntry.end()) {
1557 auto ch=static_cast<unsigned char>(input->readULong(1));
1558 if (ch==0xd)
1559 listener->insertEOL();
1560 else if (ch<30) {
1561 MWAW_DEBUG_MSG(("BeagleWksDBParser::sendDatabase: find some odd character\n"));
1562 break;
1563 }
1564 else
1565 listener->insertCharacter(ch);
1566 }
1567 }
1568 else if (field.m_type==BeagleWksDBParserInternal::Cell::Picture && field.m_pictureId>=0) {
1569 std::string cellName("Sheet0.");
1570 cellName+=MWAWCell::getBasicCellName(field.position()+MWAWVec2i(1,1));
1571 MWAWPosition position(MWAWVec2f(0,0), MWAWVec2f(76,12), librevenge::RVNG_POINT); // changeme
1572 position.setAnchorToCell(librevenge::RVNGString(cellName.c_str()));
1573 sendPicture(field.m_pictureId, position, false);
1574 }
1575 listener->closeSheetCell();
1576 }
1577 listener->closeSheetRow();
1578 }
1579 listener->closeSheet();
1580 return true;
1581 }
1582
1583 ////////////////////////////////////////////////////////////
1584 //
1585 // Low level
1586 //
1587 ////////////////////////////////////////////////////////////
1588
1589 ////////////////////////////////////////////////////////////
1590 // read the header
1591 ////////////////////////////////////////////////////////////
1592 bool BeagleWksDBParser::checkHeader(MWAWHeader *header, bool strict)
1593 {
1594 *m_state = BeagleWksDBParserInternal::State();
1595 MWAWInputStreamPtr input = getInput();
1596 if (!input || !input->hasDataFork() || !input->checkPosition(66))
1597 return false;
1598
1599 libmwaw::DebugStream f;
1600 f << "FileHeader:";
1601
1602 input->seek(0, librevenge::RVNG_SEEK_SET);
1603 if (input->readLong(2)!=0x4257 || input->readLong(2)!=0x6b73 ||
1604 input->readLong(2)!=0x4257 || input->readLong(2)!=0x6462 ||
1605 input->readLong(2)!=0x4257 || input->readLong(2)!=0x6462) {
1606 return false;
1607 }
1608 for (int i=0; i < 9; ++i) { // f2=f6=1 other 0
1609 auto val=static_cast<int>(input->readLong(2));
1610 if (val) f << "f" << i << "=" << val << ",";
1611 }
1612 setVersion(1);
1613
1614 if (header)
1615 header->reset(MWAWDocument::MWAW_T_BEAGLEWORKS, 1, MWAWDocument::MWAW_K_DATABASE);
1616
1617 ascii().addPos(0);
1618 ascii().addNote(f.str().c_str());
1619
1620 long pos=input->tell();
1621 f.str("");
1622 f << "FileHeader-II:";
1623 m_state->m_databaseBegin=input->readLong(4);
1624 if (!input->checkPosition(m_state->m_databaseBegin)) {
1625 MWAW_DEBUG_MSG(("BeagleWksDBParser::checkHeader: can not read the database position\n"));
1626 return false;
1627 }
1628 f << "database[ptr]=" << std::hex << m_state->m_databaseBegin << std::dec << ",";
1629 for (int i=0; i < 11; ++i) { // f2=0x50c|58c|5ac f3=f5=9
1630 long val=input->readLong(2);
1631 if (val) f << "f" << i << "=" << std::hex << val << std::dec << ",";
1632 }
1633 MWAWEntry entry;
1634 entry.setBegin(input->readLong(4));
1635 entry.setLength(input->readLong(4));
1636 entry.setId(static_cast<int>(input->readLong(2))); // in fact nFonts
1637 entry.setType("FontNames");
1638 f << "fontNames[ptr]=" << std::hex << entry.begin() << "<->" << entry.end()
1639 << std::dec << ",nFonts=" << entry.id() << ",";
1640 if (entry.length() && (!entry.valid() || !input->checkPosition(entry.end()))) {
1641 MWAW_DEBUG_MSG(("BeagleWksDBParser::checkHeader: can not read the font names position\n"));
1642 f << "###";
1643 ascii().addPos(pos);
1644 ascii().addNote(f.str().c_str());
1645 return false;
1646 }
1647
1648 m_state->m_typeEntryMap.insert
1649 (std::multimap<std::string, MWAWEntry>::value_type(entry.type(),entry));
1650 ascii().addPos(pos);
1651 ascii().addNote(f.str().c_str());
1652 if (strict && !readPrintInfo())
1653 return false;
1654 ascii().addPos(66);
1655 ascii().addNote("_");
1656
1657 return true;
1658 }
1659
1660 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: