Debian Codegraph @

src/lib/BeagleWksStructManager.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 <set>
38 #include <sstream>
39
40 #include <librevenge/librevenge.h>
41
42 #include "MWAWDebug.hxx"
43 #include "MWAWFontConverter.hxx"
44 #include "MWAWParser.hxx"
45 #include "MWAWRSRCParser.hxx"
46
47 #include "BeagleWksStructManager.hxx"
48
49 /** Internal: the structures of a BeagleWksStructManager */
50 namespace BeagleWksStructManagerInternal
51 {
52
53 ////////////////////////////////////////
54 //! Internal: the state of a BeagleWksStructManager
55 struct State {
56 //! constructor
57 State()
58 : m_fileIdFontIdList()
59 , m_header()
60 , m_footer()
61 , m_idFrameMap()
62 {
63 }
64 //! a list to get the correspondance between fileId and fontId
65 std::vector<int> m_fileIdFontIdList;
66 //! the header
67 MWAWEntry m_header;
68 //! the footer
69 MWAWEntry m_footer;
70 /** the map id to frame */
71 std::map<int, BeagleWksStructManager::Frame> m_idFrameMap;
72 };
73 }
74
75
76 ////////////////////////////////////////////////////////////
77 // constructor/destructor, ...
78 ////////////////////////////////////////////////////////////
79 BeagleWksStructManager::BeagleWksStructManager(MWAWParserStatePtr const &parserState)
80 : m_parserState(parserState)
81 , m_state(new BeagleWksStructManagerInternal::State())
82 {
83 }
84
85 BeagleWksStructManager::~BeagleWksStructManager()
86 {
87 }
88
89 int BeagleWksStructManager::getFontId(int fId) const
90 {
91 if (fId<0||fId>=int(m_state->m_fileIdFontIdList.size())) {
92 MWAW_DEBUG_MSG(("BeagleWksStructManager::getFontId can not find the final font id\n"));
93 return 3;
94 }
95
96 return m_state->m_fileIdFontIdList[size_t(fId)];
97 }
98
99 void BeagleWksStructManager::getHeaderFooterEntries(MWAWEntry &header, MWAWEntry &footer) const
100 {
101 header=m_state->m_header;
102 footer=m_state->m_footer;
103 }
104
105 std::map<int,BeagleWksStructManager::Frame> const &BeagleWksStructManager::getIdFrameMap() const
106 {
107 return m_state->m_idFrameMap;
108 }
109
110 bool BeagleWksStructManager::getFrame(int fId, Frame &frame) const
111 {
112 auto it=m_state->m_idFrameMap.find(fId);
113 if (it==m_state->m_idFrameMap.end()) {
114 MWAW_DEBUG_MSG(("BeagleWksStructManager::getFrame: can not find frame for id=%d\n",fId));
115 return false;
116 }
117 frame=it->second;
118 return true;
119 }
120
121 MWAWInputStreamPtr BeagleWksStructManager::getInput()
122 {
123 return m_parserState->m_input;
124 }
125
126 libmwaw::DebugFile &BeagleWksStructManager::ascii()
127 {
128 return m_parserState->m_asciiFile;
129 }
130
131 MWAWInputStreamPtr BeagleWksStructManager::rsrcInput()
132 {
133 return m_parserState->m_rsrcParser->getInput();
134 }
135
136 libmwaw::DebugFile &BeagleWksStructManager::rsrcAscii()
137 {
138 return m_parserState->m_rsrcParser->ascii();
139 }
140
141 ////////////////////////////////////////////////////////////
142 // the frame
143 ////////////////////////////////////////////////////////////
144 bool BeagleWksStructManager::readFrame(MWAWEntry const &entry)
145 {
146 if (entry.length()!=156*long(entry.id())) {
147 MWAW_DEBUG_MSG(("BeagleWksStructManager::readFrame: the entry seems bad\n"));
148 return false;
149 }
150 entry.setParsed(true);
151 MWAWInputStreamPtr input = getInput();
152 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
153 for (int i=0; i<entry.id(); ++i) {
154 Frame frame;
155 long pos=input->tell(), begPos=pos;
156 libmwaw::DebugStream f;
157 f << "Entries(Frame)[" << i << "]:";
158 auto type=static_cast<int>(input->readULong(2));
159 int val;
160 switch (type) {
161 case 0x8000: {
162 f << "picture,";
163 val=static_cast<int>(input->readLong(2)); // 1|8
164 if (val) f << "f0=" << val << ",";
165 ascii().addDelimiter(input->tell(),'|');
166 input->seek(pos+40, librevenge::RVNG_SEEK_SET);
167 ascii().addDelimiter(input->tell(),'|');
168 for (int j=0; j < 5; ++j) { // f1=5, f3=2, f5=e|13
169 val=static_cast<int>(input->readLong(2));
170 if (val) f << "f" << j+1 << "=" << val << ",";
171 }
172 double dim[4];
173 for (auto &j : dim) j=double(input->readLong(4))/65536.;
174 f << "dim?=" << dim[1] << "x" << dim[0] << "<->" << dim[3] << "x" << dim[2] << ",";
175 val =static_cast<int>(input->readLong(2));
176 if (val) f << "f6=" << val << ",";
177 break;
178 }
179 case 0xffff: {
180 f << "attachment,";
181 for (int j=0; j<2; ++j) { // f0=0, f1=4ef8
182 val=static_cast<int>(input->readLong(2));
183 if (val) f << "f" << j << "=" << val << ",";
184 }
185 auto fSz=static_cast<int>(input->readULong(1));
186 if (fSz>0 && fSz<32) {
187 std::string name("");
188 for (int c=0; c < fSz; c++)
189 name+=char(input->readLong(1));
190 f << name << ",";
191 }
192 else {
193 MWAW_DEBUG_MSG(("BeagleWksStructManager::readFrame: the size seems bad\n"));
194 f << "#fSz=" << fSz << ",";
195 }
196 input->seek(pos+44, librevenge::RVNG_SEEK_SET);
197 for (int j=0; j<6; ++j)
198 f << "dim" << j << "?=" << input->readLong(2) << "x"
199 << input->readLong(2) << ",";
200 break;
201 }
202 default:
203 MWAW_DEBUG_MSG(("BeagleWksStructManager::readFrame: unknown frame type\n"));
204 f << "type=" << std::hex << type << std::dec << ",";
205 break;
206 }
207 ascii().addPos(pos);
208 ascii().addNote(f.str().c_str());
209
210 pos=input->tell();
211 f.str("");
212 val=static_cast<int>(input->readULong(2)); // afc, 1df, 1dd, f34, a5a8
213 f << "f0=" << std::hex << val << std::dec << ",";
214 f << "PTR=" << std::hex << input->readULong(4) << std::dec << ",";
215 float orig[2];
216 for (auto &j : orig) j=float(input->readLong(4))/65536.f;
217 frame.m_origin=MWAWVec2f(orig[1],orig[0]);
218 f << "PTR1=" << std::hex << input->readULong(4) << std::dec << ",";
219
220 frame.m_page=static_cast<int>(input->readLong(2));
221 float dim[2];
222 for (auto &j : dim) j=float(input->readLong(2));
223 frame.m_dim=MWAWVec2f(dim[1],dim[0]);
224 f << "dim=" << dim[1] << "x" << dim[0] << ",";
225 for (int j=0; j<4; ++j) { // f1=0|05b1 other 0
226 val=static_cast<int>(input->readLong(2));
227 if (val) f << "f" << j+1 << "=" << std::hex << val << std::dec << ",";
228 }
229 frame.m_id=static_cast<int>(input->readLong(2));
230 for (int j=0; j<2; ++j) { // 0
231 val=static_cast<int>(input->readLong(2));
232 if (val) f << "g" << j << "=" << std::hex << val << std::dec << ",";
233 }
234 frame.m_extra=f.str();
235 f.str("");
236 f << "Frame-II[" << i << "]:" << frame;
237
238 ascii().addPos(pos);
239 ascii().addNote(f.str().c_str());
240
241 pos=input->tell();
242 f.str("");
243 f << "Frame-III[" << i << "]:";
244 val=int(input->readLong(2)); // 0|6|8|9
245 if (val) f << "f0=" << val << ",";
246 val=int(input->readULong(4)); // big number
247 f << "PTR=" << std::hex << val << std::dec << ",";
248 frame.m_border.m_width = double(input->readLong(2));
249 if (frame.m_border.m_width > 0)
250 f << "borderSize=" << frame.m_border.m_width << ",";
251 val=int(input->readLong(2)); // 0
252 if (val) f << "f1=" << val << ",";
253 val=int(input->readLong(4));
254 if (val) f << "offset=" << double(val)/65536. << ",";
255 val=int(input->readLong(2)); // 0
256 if (val) f << "f2=" << val << ",";
257 auto flags=static_cast<int>(input->readLong(2));
258 frame.m_wrap=(flags&3);
259 switch (frame.m_wrap) { // textaround
260 case 0: // none
261 f << "wrap=none,";
262 break;
263 case 1:
264 f << "wrap=rectangle,";
265 break;
266 case 2:
267 f << "wrap=irregular,";
268 break;
269 default:
270 f << "#wrap=3,";
271 break;
272 }
273 if (flags&0x8) {
274 frame.m_charAnchor = false;
275 f << "anchor=page,";
276 }
277 if (flags&0x10) {
278 f << "bord[all],";
279 frame.m_bordersSet=libmwaw::LeftBit|libmwaw::RightBit|
280 libmwaw::BottomBit|libmwaw::TopBit;
281 }
282 else if (flags&0x1E0) {
283 f << "bord[";
284 if (flags&0x20) {
285 f << "T";
286 frame.m_bordersSet |= libmwaw::TopBit;
287 }
288 if (flags&0x40) {
289 f << "L";
290 frame.m_bordersSet |= libmwaw::LeftBit;
291 }
292 if (flags&0x80) {
293 f << "B";
294 frame.m_bordersSet |= libmwaw::BottomBit;
295 }
296 if (flags&0x100) {
297 f << "R";
298 frame.m_bordersSet |= libmwaw::RightBit;
299 }
300 f << "],";
301 }
302 flags &= 0xFE04;
303 if (flags) f << "fl=" << std::hex << flags << std::dec << ",";
304 frame.m_pictId=static_cast<int>(input->readULong(2));
305 f << "pId=" << frame.m_pictId << ",";
306 ascii().addDelimiter(input->tell(),'|');
307 input->seek(18, librevenge::RVNG_SEEK_CUR);
308 ascii().addDelimiter(input->tell(),'|');
309 val=int(input->readLong(4));
310 if (val) f << "textAround[offsT/B]=" << double(val)/65536. << ",";
311 val=int(input->readLong(4));
312 if (val) f << "textAround[offsR/L]=" << double(val)/65536. << ",";
313 for (int j=0; j<2; ++j) { // g0,g1=0 or g0,g1=5c0077c (dim?)
314 val=static_cast<int>(input->readLong(2));
315 if (val) f << "g" << j << "=" << val << ",";
316 }
317 ascii().addPos(pos);
318 ascii().addNote(f.str().c_str());
319 if (m_state->m_idFrameMap.find(frame.m_id)!=m_state->m_idFrameMap.end()) {
320 MWAW_DEBUG_MSG(("BeagleWksStructManager::readFrame: frame %d already exists\n", frame.m_id));
321 }
322 else
323 m_state->m_idFrameMap[frame.m_id]=frame;
324 input->seek(begPos+156, librevenge::RVNG_SEEK_SET);
325 }
326 return true;
327 }
328
329 ////////////////////////////////////////////////////////////
330 // the fonts
331 ////////////////////////////////////////////////////////////
332 bool BeagleWksStructManager::readFontNames(MWAWEntry const &entry)
333 {
334 if (!entry.valid())
335 return (entry.length()==0&&entry.id()==0);
336
337 entry.setParsed(true);
338 MWAWInputStreamPtr input=getInput();
339 if (!input || !input->checkPosition(entry.end())) {
340 MWAW_DEBUG_MSG(("BeagleWksStructManager::readFontNames: bad input\n"));
341 return false;
342 }
343 long pos=entry.begin(), endPos=entry.end();
344 input->seek(pos, librevenge::RVNG_SEEK_SET);
345 libmwaw::DebugStream f;
346 m_state->m_fileIdFontIdList.resize(0);
347 for (int i=0; i < entry.id(); ++i) {
348 pos = input->tell();
349 f.str("");
350 f << "Entries(FontNames)[" << i << "]:";
351 auto fSz=static_cast<int>(input->readULong(1));
352 if (pos+1+fSz>endPos) {
353 MWAW_DEBUG_MSG(("BeagleWksStructManager::readFontNames: can not read font %d\n", i));
354 f << "###";
355 ascii().addPos(pos);
356 ascii().addNote(f.str().c_str());
357 input->seek(endPos, librevenge::RVNG_SEEK_SET);
358 return i>0;
359 }
360 std::string name("");
361 for (int c=0; c < fSz; ++c)
362 name+=char(input->readULong(1));
363 if (!name.empty())
364 m_state->m_fileIdFontIdList.push_back(m_parserState->m_fontConverter->getId(name));
365
366 f << "\"" << name << "\",";
367 ascii().addPos(pos);
368 ascii().addNote(f.str().c_str());
369 }
370 pos = input->tell();
371 if (pos!=endPos) {
372 MWAW_DEBUG_MSG(("BeagleWksStructManager::readFontNames: find extra data\n"));
373 ascii().addPos(pos);
374 ascii().addNote("FontNames:###");
375 input->seek(endPos, librevenge::RVNG_SEEK_SET);
376 }
377 ascii().addPos(endPos);
378 ascii().addNote("_");
379
380 return true;
381 }
382
383 ////////////////////////////////////////////////////////////
384 // the document info and preferences
385 ////////////////////////////////////////////////////////////
386 bool BeagleWksStructManager::readDocumentInfo()
387 {
388 MWAWInputStreamPtr input= getInput();
389 long pos=input->tell();
390 libmwaw::DebugStream f;
391 f << "Entries(DocInfo):";
392 long dSz=static_cast<int>(input->readULong(2));
393 long endPos=pos+dSz+4;
394 if (dSz<0x226 || !input->checkPosition(endPos)) {
395 MWAW_DEBUG_MSG(("BeagleWksStructManager::readDocumentInfo: can not find the database zone\n"));
396 input->seek(pos, librevenge::RVNG_SEEK_SET);
397 return false;
398 }
399 auto id=static_cast<int>(input->readLong(2));
400 if (id!=1) f << "id=" << id << ",";
401 std::string what("");
402 for (int i=0; i < 4; ++i) // dosu
403 what+=char(input->readLong(1));
404 f << what << ",";
405 int val;
406 for (int i=0; i < 3; ++i) { // 961/2101, 0, 0
407 val=static_cast<int>(input->readLong(2));
408 if ((i==0 && val!=0x961) || (i&&val))
409 f << "f" << i+2 << "=" << val << ",";
410 }
411 f << "ids=[";
412 for (int i=0; i < 2; ++i)
413 f << std::hex << long(input->readULong(4)) << std::dec << ",";
414 f << "],";
415 double margins[4];
416 f << "margins=[";
417 for (auto &margin : margins) {
418 margin=double(input->readLong(4))/72.;
419 f << margin << ",";
420 }
421 f << "],";
422 MWAWPageSpan &pageSpan=m_parserState->m_pageSpan;
423 if (margins[0]>=0&&margins[1]>=0&&margins[2]>=0&&margins[3]>=0&&
424 margins[0]+margins[1]<0.5*pageSpan.getFormLength() &&
425 margins[2]+margins[3]<0.5*pageSpan.getFormWidth()) {
426 pageSpan.setMarginTop(margins[0]);
427 pageSpan.setMarginBottom(margins[1]);
428 pageSpan.setMarginLeft(margins[3]);
429 pageSpan.setMarginRight(margins[2]);
430 }
431 else {
432 MWAW_DEBUG_MSG(("BeagleWksStructManager::readDocumentInfo: the page margins seem bad\n"));
433 f << "###";
434 }
435 auto numRemains=int(endPos-512-input->tell());
436 f << "fls=[";
437 for (int i=0; i < numRemains; ++i) { // [_,_,_,_,_,1,]
438 val = static_cast<int>(input->readLong(1));
439 if (val) f << val << ",";
440 else f << "_,";
441 }
442 f << "],";
443 ascii().addPos(pos);
444 ascii().addNote(f.str().c_str());
445
446 for (int st=0; st<2; ++st) {
447 pos=input->tell();
448 f.str("");
449 if (st==0)
450 f << "DocInfo[header]:";
451 else
452 f << "DocInfo[footer]:";
453 auto fSz = static_cast<int>(input->readULong(1));
454 MWAWEntry &entry=st==0 ? m_state->m_header : m_state->m_footer;
455 entry.setBegin(input->tell());
456 entry.setLength(fSz);
457 std::string name("");
458 for (int i=0; i<fSz; ++i)
459 name+=char(input->readULong(1));
460 f << name;
461 input->seek(pos+256, librevenge::RVNG_SEEK_SET);
462 ascii().addPos(pos);
463 ascii().addNote(f.str().c_str());
464 }
465
466 return true;
467 }
468
469 bool BeagleWksStructManager::readDocumentPreferences()
470 {
471 MWAWInputStreamPtr input= getInput();
472 long pos=input->tell();
473 libmwaw::DebugStream f;
474 f << "Entries(Preferences):";
475 auto dSz=long(input->readULong(2));
476 long endPos=pos+dSz+4;
477 if (dSz<0x2e || !input->checkPosition(endPos)) {
478 MWAW_DEBUG_MSG(("BeagleWksStructManager::readDocumentInfo: can not find the database zone\n"));
479 input->seek(pos, librevenge::RVNG_SEEK_SET);
480 return false;
481 }
482 auto id=static_cast<int>(input->readLong(2));
483 if (id!=0xa) f << "id=" << id << ",";
484 std::string what="";
485 for (int i=0; i < 4; ++i) // pref
486 what+=char(input->readLong(1));
487 f << what << ",";
488 for (int i=0; i < 3; ++i) { // always 0
489 auto val=static_cast<int>(input->readLong(2));
490 if (val) f << "f" << i+2 << "=" << val << ",";
491 }
492 f << "ids=[";
493 for (int i=0; i < 2; i++)
494 f << std::hex << long(input->readULong(4)) << std::dec << ",";
495 f << "],";
496 auto val=static_cast<int>(input->readULong(2)); // 0|22d8|4ead|e2c8
497 if (val)
498 f << "fl?=" << std::hex << val << std::dec << ",";
499 for (int i=0; i < 8; i++) {
500 static int const expectedValues[] = {1,4/*or 2*/,3,2,2,1,1,1 };
501 val=static_cast<int>(input->readLong(1));
502 if (val!=expectedValues[i])
503 f << "g" << i << "=" << val << ",";
504 }
505 for (int i=0; i < 8; ++i) { // 1,a|e, 0, 21, 3|4, 6|7|9, d|13, 3|5: related to font?
506 val=static_cast<int>(input->readLong(2));
507 if (val)
508 f << "h" << i << "=" << val << ",";
509 }
510 val=static_cast<int>(input->readULong(2)); //0|10|3e|50|c8|88|98
511 if (val)
512 f << "h8=" << std::hex << val << std::dec << ",";
513 input->seek(endPos, librevenge::RVNG_SEEK_SET);
514 ascii().addPos(pos);
515 ascii().addNote(f.str().c_str());
516
517 return true;
518 }
519
520 ////////////////////////////////////////////////////////////
521 // resource fork data
522 ////////////////////////////////////////////////////////////
523
524 // read the windows position blocks
525 bool BeagleWksStructManager::readwPos(MWAWEntry const &entry)
526 {
527 if (!entry.valid() || entry.length() != 8) {
528 MWAW_DEBUG_MSG(("BeagleWksStructManager::readwPos: the entry is bad\n"));
529 return false;
530 }
531
532 long pos = entry.begin();
533 MWAWInputStreamPtr input = rsrcInput();
534 libmwaw::DebugFile &ascFile = rsrcAscii();
535 libmwaw::DebugStream f;
536 entry.setParsed(true);
537
538 input->seek(pos, librevenge::RVNG_SEEK_SET);
539 f << "Entries(Windows):";
540 int dim[4];
541 for (int &i : dim) i=static_cast<int>(input->readLong(2));
542
543 f << "dim=" << dim[1] << "x" << dim[0] << "<->"
544 << dim[3] << "x" << dim[2] << ",";
545 ascFile.addPos(pos-4);
546 ascFile.addNote(f.str().c_str());
547 return true;
548 }
549
550 bool BeagleWksStructManager::readFontStyle(MWAWEntry const &entry)
551 {
552 if (!entry.valid() || entry.length() != 8) {
553 MWAW_DEBUG_MSG(("BeagleWksStructManager::readFontStyle: the entry is bad\n"));
554 return false;
555 }
556
557 long pos = entry.begin();
558 MWAWInputStreamPtr input = rsrcInput();
559 libmwaw::DebugFile &ascFile = rsrcAscii();
560 libmwaw::DebugStream f;
561 entry.setParsed(true);
562
563 input->seek(pos, librevenge::RVNG_SEEK_SET);
564 f << "Entries(FontStyle)[" << std::hex << entry.id() << std::dec << "]:";
565 auto fSz=static_cast<int>(input->readLong(2));
566 if (fSz) f << "fSz=" << fSz << ",";
567 auto fl=static_cast<int>(input->readLong(2));
568 if (fl) f << "flags=" << std::hex << fl << std::dec << ",";
569 auto id=static_cast<int>(input->readLong(2));
570 if (id) f << "fId=" << id << ",";
571 auto val=static_cast<int>(input->readLong(2));
572 if (val) f << "color?=" << val << ",";
573 ascFile.addPos(pos-4);
574 ascFile.addNote(f.str().c_str());
575 return true;
576 }
577
578 bool BeagleWksStructManager::readPicture(int pId, librevenge::RVNGBinaryData &pict, bool readEDTP)
579 {
580 auto rsrcParser = m_parserState->m_rsrcParser;
581 if (!rsrcParser) {
582 static bool first=true;
583 if (first) {
584 MWAW_DEBUG_MSG(("BeagleWksStructManager::readPicture: need access to resource fork to retrieve picture content\n"));
585 first=false;
586 }
587 return true;
588 }
589
590 auto const &entryMap = rsrcParser->getEntriesMap();
591 std::string const what(readEDTP ? "edtp" : "PICT");
592 auto it=entryMap.find(what);
593 MWAWEntry pictEntry;
594 while (it!=entryMap.end()) {
595 if (it->first!=what)
596 break;
597 MWAWEntry const &entry=it++->second;
598 if (entry.id()!=pId)
599 continue;
600 entry.setParsed(true);
601 pictEntry=entry;
602 break;
603 }
604 if (!pictEntry.valid()) {
605 MWAW_DEBUG_MSG(("BeagleWksStructManager::readPicture: can not find picture %d\n", pId));
606 return false;
607 }
608
609 MWAWInputStreamPtr input = rsrcInput();
610 input->seek(pictEntry.begin(), librevenge::RVNG_SEEK_SET);
611 pict.clear();
612 input->readDataBlock(pictEntry.length(), pict);
613
614 #ifdef DEBUG_WITH_FILES
615 libmwaw::DebugFile &ascFile = rsrcAscii();
616 libmwaw::DebugStream f;
617 static int volatile pictName = 0;
618 f << "PICT" << ++pictName << ".pct";
619 libmwaw::Debug::dumpFile(pict, f.str().c_str());
620 ascFile.addPos(pictEntry.begin()-4);
621 ascFile.addNote(f.str().c_str());
622 ascFile.skipZone(pictEntry.begin(),pictEntry.end()-1);
623 #endif
624
625 return true;
626 }
627
628 ////////////////////////////////////////////////////////////
629 // formula data
630 ////////////////////////////////////////////////////////////
631 bool BeagleWksStructManager::readCellInFormula(MWAWVec2i actPos, MWAWCellContent::FormulaInstruction &instr)
632 {
633 instr=MWAWCellContent::FormulaInstruction();
634 instr.m_type=MWAWCellContent::FormulaInstruction::F_Cell;
635 int pos[2];
636 bool absolute[2] = { true, true};
637 for (int dim = 0; dim < 2; dim++) {
638 int val = static_cast<int>(getInput()->readULong(2));
639 if ((val & 0xF000) == 0); // absolue value ?
640 else {
641 val &= 0x7FFF;
642 if (val & 0x4000) val = val - 0x8000;
643 val += actPos[dim];
644 absolute[dim] = false;
645 }
646 pos[dim] = val;
647 }
648
649 if (pos[0] < 0 || pos[1] < 0) {
650 std::stringstream f;
651 f << "###[" << pos[1] << "," << pos[0] << "]";
652 MWAW_DEBUG_MSG(("BeagleWksStructManager::readCell: can not read cell position\n"));
653 return false;
654 }
655 instr.m_position[0]=MWAWVec2i(pos[0],pos[1]);
656 instr.m_positionRelative[0]=MWAWVec2b(!absolute[0],!absolute[1]);
657 return true;
658 }
659
660 namespace BeagleWksStructManagerInternal
661 {
662 struct Functions {
663 char const *m_name;
664 int m_arity;
665 };
666
667 static Functions const s_listFunctions[] = {
668 { "", 0} /*SPEC: number*/, {"", 0}/*SPEC: cell*/, {"", 0}/*SPEC: cells*/, {"=", 1} /*=*/,
669 { "(", 1} /*SPEC: ()*/, {"", 0}/*SPEC: number*/, { "", -2} /*UNKN*/, {"", -2}/*UNKN*/,
670 { "", -2} /*UNKN*/, {"+", 1}, {"-", 1}, {"+", 2},
671 { "-", 2}, { "*", 2}, {"/", 2}, {"^", 2},
672
673 { "", -2} /*UNKN*/,{ "&", 2}, { "=", 2},{ "<>", 2},
674 { "<=", 2},{ ">=", 2}, { "<", 2},{ ">", 2},
675 { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
676 { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "Error", -1},{ "False", -1},
677
678 { "NA", -1},{ "Now", -1},{ "Rand", -1},{ "Pi", -1},
679 { "True", -1},{ "IsError", -1},{ "Abs", -1},{ "Exp", -1},
680 { "Int", -1},{ "LN", -1},{ "LOG10", -1},{ "Sign", -1},
681 { "Sqrt", -1},{ "Acos", -1},{ "Asin", -1},{ "Atan", -1},
682
683 { "Cos", -1},{ "Degrees", -1},{ "Radians", -1},{ "Sin", -1},
684 { "Tan", -1},{ "Day", -1},{ "Hour", -1},{ "Minute", -1},
685 { "Month", -1},{ "Second", -1},{ "Weekday", -1},{ "Year", -1},
686 { "IsBlank", -1},{ "IsNa", -1},{ "Not", -1},{ "Type", -1},
687
688 { "Text", -1},{ "Log", -1},{ "Mod", -1},{ "Round", -1},
689 { "Atan2", -1},{ "IRR", -1},{ "Lookup", -1},{ "Match", -1},
690 { "Date", -1},{ "Time", -1},{ "If", -1},{ "MIRR", -1},
691 { "HLookup", -1},{ "Index", -1},{ "VLookup", -1},{ "FV", -1},
692
693 { "NPER", -1},{ "PV", -1},{ "PMT", -1},{ "Rate", -1},
694 { "Count", -1},{ "Average", -1},{ "Max", -1},{ "Min", -1},
695 { "StDev", -1},{ "Sum", -1},{ "", -2}/*UNKN*/,{ "Var", -1},
696 { "And", -1},{ "Choose", -1},{ "Or", -1},{ "NPV", -1},
697
698 { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
699 { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
700 { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
701 { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
702
703 };
704 }
705
706 bool BeagleWksStructManager::readFormula(long endPos, MWAWVec2i const &position,
707 std::vector<MWAWCellContent::FormulaInstruction> &formula, std::string &error)
708 {
709 MWAWInputStreamPtr input=getInput();
710 formula.resize(0);
711 if (!input->checkPosition(endPos)) {
712 MWAW_DEBUG_MSG(("BeagleWksStructManager::readFormula: called with bad endPos\n"));
713 error="###endPos";
714 return false;
715 }
716
717 error = "";
718 std::stringstream f;
719 std::vector<std::vector<MWAWCellContent::FormulaInstruction> > stack;
720 bool ok = true;
721 bool const isSheet=m_parserState->m_kind==MWAWDocument::MWAW_K_SPREADSHEET;
722 while (long(input->tell()) != endPos) {
723 double val;
724 long pos = input->tell();
725 if (pos > endPos) return false;
726 auto wh = static_cast<int>(input->readULong(1));
727 int arity = 0;
728 bool isNan;
729 MWAWCellContent::FormulaInstruction instr;
730 f.str("");
731 switch (wh) {
732 case 0x0:
733 if (endPos-pos<11 || !input->readDouble10(val, isNan)) {
734 f << "###number";
735 error=f.str();
736 ok = false;
737 break;
738 }
739 instr.m_type=MWAWCellContent::FormulaInstruction::F_Double;
740 instr.m_doubleValue=val;
741 break;
742 case 0x1: // code in spreadsheet
743 case 0x1a: // code in database
744 if ((wh==1 && !isSheet) || (wh==0x1a && isSheet)) {
745 f << "###unexpected cell code";
746 ok=false;
747 break;
748 }
749 if (endPos-pos<5) {
750 f << "###cell short";
751 error=f.str();
752 ok = false;
753 break;
754 }
755 ok = readCellInFormula(position, instr);
756 break;
757 case 0x2: {
758 if (!isSheet) {
759 f << "###list cell in database";
760 ok=false;
761 break;
762 }
763 if (endPos-pos< 9 || !readCellInFormula(position, instr)) {
764 f << "###list cell short";
765 error=f.str();
766 ok = false;
767 break;
768 }
769 MWAWCellContent::FormulaInstruction instr2;
770 if (!readCellInFormula(position, instr2)) {
771 ok = false;
772 f << "###list cell short";
773 error=f.str();
774 break;
775 }
776 instr.m_type=MWAWCellContent::FormulaInstruction::F_CellList;
777 instr.m_position[1]=instr2.m_position[0];
778 instr.m_positionRelative[1]=instr2.m_positionRelative[0];
779 break;
780 }
781 case 0x5:
782 instr.m_type=MWAWCellContent::FormulaInstruction::F_Long;
783 instr.m_longValue=double(input->readLong(4));
784 break;
785 case 0x6: {
786 instr.m_type=MWAWCellContent::FormulaInstruction::F_Text;
787 auto fSz=static_cast<int>(input->readULong(1));
788 if (input->tell()+fSz > endPos) {
789 ok=false;
790 break;
791 }
792 for (int i=0; i<fSz; ++i) {
793 auto c = char(input->readULong(1));
794 if (c==0) {
795 ok = i+1==fSz;
796 break;
797 }
798 instr.m_content += c;
799 }
800 break;
801 }
802 default:
803 if (wh >= 0x70 || (wh < 0x20 && BeagleWksStructManagerInternal::s_listFunctions[wh].m_arity == -2)) {
804 f.str("");
805 f << "##Funct" << std::hex << wh << std::dec;
806 error=f.str();
807 ok = false;
808 break;
809 }
810 instr.m_type=MWAWCellContent::FormulaInstruction::F_Function;
811 if (BeagleWksStructManagerInternal::s_listFunctions[wh].m_arity == -2) {
812 std::stringstream s;
813 s << "Funct" << std::hex << wh << std::dec;
814 instr.m_content=s.str();
815 arity = static_cast<int>(input->readLong(1));
816 }
817 else {
818 instr.m_content=BeagleWksStructManagerInternal::s_listFunctions[wh].m_name;
819 ok=!instr.m_content.empty();
820 arity = BeagleWksStructManagerInternal::s_listFunctions[wh].m_arity;
821 if (arity == -1) arity = static_cast<int>(input->readLong(1));
822 }
823 break;
824 }
825
826 if (!ok) break;
827 std::vector<MWAWCellContent::FormulaInstruction> child;
828 if (instr.m_type!=MWAWCellContent::FormulaInstruction::F_Function) {
829 child.push_back(instr);
830 stack.push_back(child);
831 continue;
832 }
833 size_t numElt = stack.size();
834 if (static_cast<int>(numElt) < arity) {
835 f.str("");
836 f << instr.m_content << "[##" << arity << "]";
837 error=f.str();
838 ok = false;
839 break;
840 }
841 if ((instr.m_content[0] >= 'A' && instr.m_content[0] <= 'Z') || instr.m_content[0] == '(') {
842 if (instr.m_content[0] != '(')
843 child.push_back(instr);
844
845 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
846 instr.m_content="(";
847 child.push_back(instr);
848 for (int i = 0; i < arity; i++) {
849 if (i) {
850 instr.m_content=";";
851 child.push_back(instr);
852 }
853 auto const &node=stack[size_t(static_cast<int>(numElt)-arity+i)];
854 child.insert(child.end(), node.begin(), node.end());
855 }
856 instr.m_content=")";
857 child.push_back(instr);
858
859 stack.resize(size_t(static_cast<int>(numElt)-arity+1));
860 stack[size_t(static_cast<int>(numElt)-arity)] = child;
861 continue;
862 }
863 if (arity==1) {
864 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
865 stack[numElt-1].insert(stack[numElt-1].begin(), instr);
866 if (wh==0x3 && pos+2==endPos)
867 break;
868 continue;
869 }
870 if (arity==2) {
871 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
872 stack[numElt-2].push_back(instr);
873 stack[numElt-2].insert(stack[numElt-2].end(), stack[numElt-1].begin(), stack[numElt-1].end());
874 stack.resize(numElt-1);
875 continue;
876 }
877 ok=false;
878 error = "### unexpected arity";
879 break;
880 }
881
882 if (!ok) ;
883 else if (stack.size()==1 && stack[0].size()>1 && stack[0][0].m_content=="=") {
884 formula.insert(formula.begin(),stack[0].begin()+1,stack[0].end());
885 return true;
886 }
887 else
888 error = "###stack problem";
889
890 static bool first = true;
891 if (first) {
892 MWAW_DEBUG_MSG(("BeagleWksStructManager::readFormula: I can not read some formula\n"));
893 first = false;
894 }
895
896 f.str("");
897 for (auto const &i : stack) {
898 for (auto const &j : i)
899 f << j << ",";
900 }
901 f << error;
902 error = f.str();
903 return false;
904 }
905
906 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: