Debian Codegraph @

src/lib/ClarisWksDbaseContent.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 <time.h>
35
36 #include <cmath>
37 #include <cstring>
38 #include <ctime>
39 #include <iomanip>
40 #include <iostream>
41 #include <set>
42 #include <sstream>
43 #include <limits>
44
45 #include <librevenge/librevenge.h>
46
47 #include "MWAWDebug.hxx"
48 #include "MWAWHeader.hxx"
49 #include "MWAWInputStream.hxx"
50 #include "MWAWListener.hxx"
51 #include "MWAWParagraph.hxx"
52 #include "MWAWParser.hxx"
53
54 #include "ClarisWksDocument.hxx"
55 #include "ClarisWksStyleManager.hxx"
56
57 #include "ClarisWksDbaseContent.hxx"
58
59 ClarisWksDbaseContent::ClarisWksDbaseContent(ClarisWksDocument &document, bool spreadsheet)
60 : m_version(0)
61 , m_isSpreadsheet(spreadsheet)
62 , m_document(document)
63 , m_parserState(document.m_parserState)
64 , m_idColumnMap()
65 , m_positionSet()
66 , m_dbFormatList()
67 {
68 if (!m_parserState || !m_parserState->m_header) {
69 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::ClarisWksDbaseContent: can not find the file header\n"));
70 return;
71 }
72 m_version = m_parserState->m_header->getMajorVersion();
73 }
74
75 ClarisWksDbaseContent::~ClarisWksDbaseContent()
76 {
77 }
78
79 void ClarisWksDbaseContent::setDatabaseFormats(std::vector<ClarisWksStyleManager::CellFormat> const &format)
80 {
81 if (m_isSpreadsheet) {
82 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::setDatabaseFormats: called with spreadsheet\n"));
83 return;
84 }
85 m_dbFormatList=format;
86 }
87
88 bool ClarisWksDbaseContent::getExtrema(MWAWVec2i &min, MWAWVec2i &max) const
89 {
90 if (m_idColumnMap.empty())
91 return false;
92 bool first=true;
93 for (auto const &it : m_idColumnMap) {
94 int col=it.first;
95 Column const &column=it.second;
96 if (column.m_idRecordMap.empty())
97 continue;
98 max[0]=col;
99 for (auto const &rIt : column.m_idRecordMap) {
100 int row=rIt.first;
101 if (first) {
102 min[0]=col;
103 min[1]=max[1]=row;
104 first=false;
105 }
106 else if (row < min[1])
107 min[1]=row;
108 else if (row > max[1])
109 max[1]=row;
110 }
111 }
112 return !first;
113 }
114
115 void ClarisWksDbaseContent::updateCellPositionsSet() const
116 {
117 if (!m_positionSet.empty() || m_idColumnMap.empty())
118 return;
119 for (auto const &it : m_idColumnMap) {
120 int col=it.first;
121 Column const &column=it.second;
122 for (auto const &rIt : column.m_idRecordMap) {
123 int row=rIt.first;
124 m_positionSet.insert(MWAWVec2i(col,row));
125 }
126 }
127 }
128
129 bool ClarisWksDbaseContent::getRecordList(std::vector<int> &list) const
130 {
131 list.resize(0);
132 if (m_idColumnMap.empty())
133 return false;
134 std::set<int> set;
135 for (auto const &it : m_idColumnMap) {
136 Column const &column=it.second;
137 for (auto const &rIt : column.m_idRecordMap)
138 set.insert(rIt.first);
139 }
140 if (set.empty())
141 return false;
142 list = std::vector<int>(set.begin(), set.end());
143
144 return true;
145 }
146
147 bool ClarisWksDbaseContent::getColumnList(int row, std::vector<int> &list) const
148 {
149 list.resize(0);
150 if (m_idColumnMap.empty())
151 return false;
152
153 updateCellPositionsSet();
154 auto rIt=m_positionSet.lower_bound(MWAWVec2i(-1,row));
155 while (rIt!=m_positionSet.end()) {
156 MWAWVec2i const &pos=*(rIt++);
157 if (pos[1]!=row)
158 break;
159 list.push_back(pos[0]);
160 }
161
162 return !list.empty();
163 }
164
165 bool ClarisWksDbaseContent::readContent()
166 {
167 if (!m_parserState) return false;
168 MWAWInputStreamPtr &input= m_parserState->m_input;
169 long pos = input->tell();
170 auto sz = long(input->readULong(4));
171 /** ARGHH: this zone is almost the only zone which count the header in sz ... */
172 long endPos = pos+sz;
173 std::string zoneName(m_isSpreadsheet ? "spread" : "dbase");
174 input->seek(endPos, librevenge::RVNG_SEEK_SET);
175 if (long(input->tell()) != endPos || sz < 6) {
176 input->seek(pos, librevenge::RVNG_SEEK_SET);
177 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readContent: file is too short\n"));
178 return false;
179 }
180
181 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
182 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
183 libmwaw::DebugStream f;
184 f << "Entries(DBHeader)[" << zoneName << "]:";
185 auto N = static_cast<int>(input->readULong(2));
186 f << "N=" << N << ",";
187 ascFile.addPos(pos);
188 ascFile.addNote(f.str().c_str());
189
190 input->pushLimit(endPos);
191 readColumnList();
192
193 if (input->tell() == endPos) {
194 input->popLimit();
195 return true;
196 }
197 /* can we have more than one sheet ? If so, going into the while
198 loop may be ok, we will not read the data...*/
199 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readContent: find extra data\n"));
200 bool ok=true;
201 while (input->tell() < endPos) {
202 pos = input->tell();
203 sz = long(input->readULong(4));
204 long zoneEnd=pos+4+sz;
205 if (zoneEnd > endPos || (sz && sz < 12)) {
206 input->seek(pos, librevenge::RVNG_SEEK_SET);
207 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readContent: find a odd content field\n"));
208 ok=false;
209 break;
210 }
211 if (!sz) {
212 ascFile.addPos(pos);
213 ascFile.addNote("Nop");
214 continue;
215 }
216 std::string name("");
217 for (int i = 0; i < 4; i++)
218 name+=char(input->readULong(1));
219 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readContent: find unexpected content field\n"));
220 f << "DBHeader[" << zoneName << "]:###" << name;
221 ascFile.addDelimiter(input->tell(),'|');
222 ascFile.addPos(pos);
223 ascFile.addNote(f.str().c_str());
224 input->seek(zoneEnd, librevenge::RVNG_SEEK_SET);
225 }
226 input->popLimit();
227 return ok;
228 }
229
230 bool ClarisWksDbaseContent::readColumnList()
231 {
232 if (!m_parserState) return false;
233 MWAWInputStreamPtr &input= m_parserState->m_input;
234 long pos=input->tell();
235 long sz=input->readLong(4);
236 std::string hName("");
237 for (int i=0; i < 4; ++i) hName+=char(input->readULong(1));
238 if (sz!=0x408 || hName!="CTAB" || !input->checkPosition(pos+4+sz)) {
239 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readCOLM: the entry seems bad\n"));
240 input->seek(pos, librevenge::RVNG_SEEK_SET);
241 return false;
242 }
243
244 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
245 libmwaw::DebugStream f;
246 if (m_isSpreadsheet)
247 f << "Entries(DBCTAB)[spread]:";
248 else
249 f << "Entries(DBCTAB)[dbase]:";
250 auto N=static_cast<int>(input->readLong(2));
251 if (N) f << "Ncols=" << N << ",";
252 auto val=static_cast<int>(input->readLong(2));
253 if (val) f << "Nrows=" << val << ",";
254 if (N<0 || N>255) {
255 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readColumnList: the entries number of elements seems bad\n"));
256 f << "####";
257 ascFile.addPos(pos);
258 ascFile.addNote(f.str().c_str());
259 return false;
260 }
261 f << "ptr=[";
262 long ptr;
263 std::vector<long> listIds;
264 for (int i=0; i <= N; i++) {
265 ptr=long(input->readULong(4));
266 listIds.push_back(ptr);
267 if (ptr)
268 f << std::hex << ptr << std::dec << ",";
269 else
270 f << "_,";
271 }
272 f << "],";
273 for (int i=N+1; i<256; i++) { // always 0
274 ptr=long(input->readULong(4));
275 if (!ptr) continue;
276 static bool first=true;
277 if (first) {
278 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readColumnList: find some extra values\n"));
279 first=false;
280 }
281 f << "#g" << i << "=" << ptr << ",";
282 }
283 ascFile.addPos(pos);
284 ascFile.addNote(f.str().c_str());
285 for (size_t c=0; c<listIds.size(); c++) {
286 if (!listIds[c]) continue;
287 pos=input->tell();
288 if (readColumn(int(c)))
289 continue;
290 input->seek(pos, librevenge::RVNG_SEEK_SET);
291 return false;
292 }
293 return true;
294 }
295
296 bool ClarisWksDbaseContent::readColumn(int c)
297 {
298 if (!m_parserState) return false;
299 MWAWInputStreamPtr &input= m_parserState->m_input;
300 long pos=input->tell();
301 long sz=input->readLong(4);
302 std::string hName("");
303 for (int i=0; i < 4; ++i) hName+=char(input->readULong(1));
304 int cPos[2];
305 for (int &i : cPos)
306 i=static_cast<int>(input->readLong(2));
307 if (sz!=8+4*(cPos[1]-cPos[0]+1) || hName!="COLM" || !input->checkPosition(pos+4+sz)) {
308 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readCOLM: the entry seems bad\n"));
309 input->seek(pos, librevenge::RVNG_SEEK_SET);
310 return false;
311 }
312 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
313 libmwaw::DebugStream f;
314 if (m_isSpreadsheet)
315 f << "Entries(DBCOLM)[spread]:";
316 else
317 f << "Entries(DBCOLM)[dbase]:";
318 f << "ptr[" << cPos[0] << "<=>" << cPos[1] << "]=[";
319 std::vector<long> listIds;
320 listIds.resize(size_t(cPos[0]),0);
321 for (int i=cPos[0]; i <= cPos[1]; i++) {
322 auto ptr=long(input->readULong(4));
323 listIds.push_back(ptr);
324 if (ptr)
325 f << std::hex << ptr << std::dec << ",";
326 else
327 f << "_,";
328 }
329 f << "],";
330 ascFile.addPos(pos);
331 ascFile.addNote(f.str().c_str());
332
333 // now read the chnk
334 ClarisWksDbaseContent::Column col;
335 bool ok=true;
336 for (size_t i=0; i < listIds.size(); ++i) {
337 pos=input->tell();
338 if (!listIds[i] || readRecordList(MWAWVec2i(c,64*int(i)), col))
339 continue;
340 input->seek(pos, librevenge::RVNG_SEEK_SET);
341 ok=false;
342 break;
343 }
344 if (!col.m_idRecordMap.empty())
345 m_idColumnMap[c]=col;
346 return ok;
347 }
348
349 bool ClarisWksDbaseContent::readRecordList(MWAWVec2i const &where, Column &col)
350 {
351 if (!m_parserState) return false;
352 MWAWInputStreamPtr &input= m_parserState->m_input;
353 long pos=input->tell();
354 long sz=input->readLong(4);
355 long endPos=pos+4+sz;
356 std::string hName("");
357 for (int i=0; i < 4; ++i) hName+=char(input->readULong(1));
358 auto N=static_cast<int>(input->readULong(2));
359 if (sz<6+134 || hName!="CHNK" || !input->checkPosition(pos+4+sz) || N>0x40) {
360 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordList: the entry seems bad\n"));
361 input->seek(pos, librevenge::RVNG_SEEK_SET);
362 return false;
363 }
364
365 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
366 libmwaw::DebugStream f;
367 std::string zoneName(m_isSpreadsheet ? "spread" : "dbase");
368 f << "Entries(DBCHNK)[" << zoneName << "]:N=" << N << ",";
369 auto type=static_cast<int>(input->readULong(2)); // often 400, 800...
370 f << "type=" << std::hex << type << std::dec << ",";
371 int dim[2]; // checkme
372 for (auto &d : dim) d=static_cast<int>(input->readLong(2));
373 f << "dim=" << dim[0] << "x" << dim[1] << ",";
374
375 f << "depl=[";
376 std::vector<long> ptrLists(64,0);
377 int find=0;
378 for (size_t i=0; i < 64; ++i) {
379 auto depl=long(input->readLong(2));
380 if (depl==0) {
381 f << "_,";
382 continue;
383 }
384 find++;
385 long fPos=pos+4+depl;
386 if (fPos > endPos) {
387 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordList: the %d ptr seems bad\n", static_cast<int>(i)));
388 f << "###";
389 ascFile.addPos(pos);
390 ascFile.addNote(f.str().c_str());
391 return false;
392 }
393 f << std::hex << depl << std::dec << ",";
394 ptrLists[i]=fPos;
395 }
396 f << "],";
397 if (find!=N) {
398 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordList: the number of find data seems bad\n"));
399 f << "###find=" << find << "!=" << N << ",";
400 }
401 ascFile.addPos(pos);
402 ascFile.addNote(f.str().c_str());
403
404 for (size_t i=0; i<64; ++i) {
405 if (!ptrLists[i]) continue;
406 Record record;
407 MWAWVec2i wh(where[0],where[1]+static_cast<int>(i));
408 if ((m_isSpreadsheet && readRecordSS(wh, ptrLists[i], record)) ||
409 (!m_isSpreadsheet && readRecordDB(wh, ptrLists[i], record))) {
410 col.m_idRecordMap[wh[1]]=record;
411 continue;
412 }
413
414 f.str("");
415 f << "DBCHNK[" << zoneName << wh << "]:#";
416 input->seek(ptrLists[i], librevenge::RVNG_SEEK_SET);
417 auto fType=static_cast<int>(input->readULong(1));
418 f << "type=" << std::hex << fType << std::dec << ",";
419 ascFile.addPos(ptrLists[i]);
420 ascFile.addNote(f.str().c_str());
421 col.m_idRecordMap[wh[1]]=record;
422 }
423
424 input->seek(endPos, librevenge::RVNG_SEEK_SET);
425 return true;
426 }
427
428 bool ClarisWksDbaseContent::readRecordSSV1(MWAWVec2i const &id, long pos, ClarisWksDbaseContent::Record &record)
429 {
430 record=ClarisWksDbaseContent::Record();
431 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
432 libmwaw::DebugStream f;
433 f << "DBCHNK[spread" << id << "]:";
434 MWAWInputStreamPtr &input= m_parserState->m_input;
435 input->seek(pos, librevenge::RVNG_SEEK_SET);
436 auto val=static_cast<int>(input->readULong(1));
437 int type=(val>>4);
438 int fileFormat=(val&0xF);
439 auto &format=record.m_format;
440 switch (fileFormat) {
441 case 0: // general
442 break;
443 case 1:
444 format.m_format=MWAWCell::F_NUMBER;
445 format.m_numberFormat=MWAWCell::F_NUMBER_CURRENCY;
446 break;
447 case 2:
448 format.m_format=MWAWCell::F_NUMBER;
449 format.m_numberFormat=MWAWCell::F_NUMBER_PERCENT;
450 break;
451 case 3:
452 format.m_format=MWAWCell::F_NUMBER;
453 format.m_numberFormat=MWAWCell::F_NUMBER_SCIENTIFIC;
454 break;
455 case 4:
456 format.m_format=MWAWCell::F_NUMBER;
457 format.m_numberFormat=MWAWCell::F_NUMBER_DECIMAL;
458 break;
459 case 5:
460 case 6:
461 case 7:
462 case 8:
463 case 9: {
464 static char const *wh[]= {"%m/%d/%y", "%B %d, %y", "%B %d, %Y", "%a, %b %d %y", "%A, %B %d %Y" };
465 format.m_format=MWAWCell::F_DATE;
466 format.m_DTFormat=wh[fileFormat-5];
467 break;
468 }
469 case 10:
470 case 11:
471 case 12:
472 case 13: {
473 static char const *wh[]= {"%I:%M %p", "%I:%M:%S %p", "%H:%M", "%H:%M:%S" };
474 format.m_format=MWAWCell::F_TIME;
475 format.m_DTFormat=wh[fileFormat-10];
476 break;
477 }
478 default: // unknown
479 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSSV1: find unknown format\n"));
480 f << "format=##" << fileFormat << ",";
481 break;
482 }
483
484 bool ok=true;
485 auto ord=static_cast<int>(input->readULong(1));
486 if (ord&8) {
487 f << "commas[thousand],";
488 ord &= 0xF7;
489 }
490 if (ord&4) {
491 f << "parenthese[negative],";
492 ord &= 0xFB;
493 }
494 if (ord&2) {
495 f << "lock,";
496 ord &= 0xFD;
497 }
498 if (ord&1) {
499 if (!input->checkPosition(pos+8)) {
500 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSSV1: can not read format\n"));
501 f << "###";
502 ok = false;
503 }
504 else {
505 MWAWFont &font = record.m_font;
506 font = MWAWFont();
507 auto fId= static_cast<int>(input->readULong(2));
508 if (fId!=0xFFFF)
509 font.setId(m_document.getStyleManager()->getFontId(static_cast<int>(fId)));
510 font.setSize(float(input->readULong(1)));
511 auto flag =static_cast<int>(input->readULong(1));
512 uint32_t flags=0;
513 if (flag&0x1) flags |= MWAWFont::boldBit;
514 if (flag&0x2) flags |= MWAWFont::italicBit;
515 if (flag&0x4) font.setUnderlineStyle(MWAWFont::Line::Simple);
516 if (flag&0x8) flags |= MWAWFont::embossBit;
517 if (flag&0x10) flags |= MWAWFont::shadowBit;
518 if (flag&0x20) font.setDeltaLetterSpacing(-1);
519 if (flag&0x40) font.setDeltaLetterSpacing(1);
520 if (flag&0x80) font.setStrikeOutStyle(MWAWFont::Line::Simple);
521 font.setFlags(flags);
522 auto colId = static_cast<int>(input->readULong(1));
523 if (colId!=1) {
524 MWAWColor col;
525 if (m_document.getStyleManager()->getColor(colId, col))
526 font.setColor(col);
527 else {
528 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSSV1: unknown color %d\n", colId));
529 }
530 }
531 f << "font=[" << font.getDebugString(m_parserState->m_fontConverter) << "],";
532 val=static_cast<int>(input->readULong(1));
533 record.m_borders = (val>>4);
534 if (record.m_borders) {
535 f << "border=";
536 if (record.m_borders&1) f << "L";
537 if (record.m_borders&2) f << "T";
538 if (record.m_borders&4) f << "R";
539 if (record.m_borders&8) f << "B";
540 f << ",";
541 }
542 val &=0xF;
543 switch ((val>>2)) {
544 case 1:
545 record.m_hAlign=MWAWCell::HALIGN_LEFT;
546 f << "left,";
547 break;
548 case 2:
549 record.m_hAlign=MWAWCell::HALIGN_CENTER;
550 f << "center,";
551 break;
552 case 3:
553 record.m_hAlign=MWAWCell::HALIGN_RIGHT;
554 f << "right,";
555 break;
556 default:
557 break;
558 }
559 val &=0x3;
560 if (val) f << "#unk=" << val << ",";
561 ascFile.addDelimiter(pos+2,'|');
562 input->seek(pos+8, librevenge::RVNG_SEEK_SET);
563 ascFile.addDelimiter(pos+8,'|');
564 ord &= 0xFE;
565 }
566 }
567 format.m_digits=(ord>>4);
568 ord &= 0xF;
569 if (ok && ord) {
570 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSSV1: find unexpected order\n"));
571 f << "###ord=" << std::hex << ord << std::dec;
572 ok = false;
573 }
574 auto &content=record.m_content;
575 if (ok && type==4) {
576 f << "formula,";
577 long actPos=input->tell();
578 auto formSz=static_cast<int>(input->readULong(1));
579 if (!input->checkPosition(actPos+2+formSz)) {
580 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSSV1: the formula seems bad\n"));
581 f << "###";
582 ok = false;
583 }
584 else {
585 ascFile.addDelimiter(input->tell(),'|');
586 std::vector<MWAWCellContent::FormulaInstruction> formula;
587 std::string error;
588 if (readFormula(id, actPos+1+formSz, formula, error)) {
589 content.m_contentType=MWAWCellContent::C_FORMULA;
590 content.m_formula=formula;
591 }
592 else {
593 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSSV1: can not read a formula\n"));
594 f << "###";
595 }
596 f << "form=";
597 for (auto const &fo : formula)
598 f << fo;
599 f << error << ",";
600 if ((formSz%2)==0) ++formSz;
601 input->seek(actPos+1+formSz, librevenge::RVNG_SEEK_SET);
602 ascFile.addDelimiter(input->tell(),'|');
603 val=static_cast<int>(input->readULong(1));
604 type=(val>>4);
605 if (val&0xF) f << "unkn0=" << (val&0xF) << ",";
606 val=static_cast<int>(input->readULong(1));
607 if (val) f << "unkn1=" << (val) << ",";
608 }
609 }
610 if (ok) {
611 long actPos=input->tell();
612 switch (type) {
613 case 0:
614 case 1:
615 if (type==0)
616 f << "int,";
617 else
618 f << "long,";
619 if (!input->checkPosition(actPos+2+2*type)) {
620 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSSV1: unexpected size for a int\n"));
621 f << "###";
622 ok = false;
623 break;
624 }
625 record.m_valueType=MWAWCellContent::C_NUMBER;
626 content.setValue(double(input->readLong(2+2*type)));
627 f << "val=" << content.m_value << ",";
628 break;
629 case 2: {
630 f << "float,";
631 if (!input->checkPosition(actPos+10)) {
632 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSSV1: unexpected size for a float\n"));
633 f << "###";
634 ok=false;
635 break;
636 }
637 record.m_valueType=MWAWCellContent::C_NUMBER;
638 double value;
639 if (input->readDouble10(value, record.m_hasNaNValue)) {
640 content.setValue(value);
641 f << "val=" << value << ",";
642 }
643 else {
644 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSSV1: can not read a float\n"));
645 f << "###,";
646 }
647 break;
648 }
649 case 3: {
650 f << "string,";
651 auto stringSize = static_cast<int>(input->readULong(1));
652 if (!input->checkPosition(actPos+1+stringSize)) {
653 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSSV1: the string(II) seems bad\n"));
654 f << "###";
655 ok=false;
656 break;
657 }
658 record.m_valueType=MWAWCellContent::C_TEXT;
659 content.m_textEntry.setBegin(input->tell());
660 content.m_textEntry.setLength(long(stringSize));
661 std::string data("");
662 for (int c=0; c < stringSize; ++c) data+=char(input->readULong(1));
663 f << "val=" << data << ",";
664 break;
665 }
666 // case 4: the formula is already read
667 case 5: // bool
668 if (input->checkPosition(actPos+1)) {
669 if (!fileFormat)
670 format.m_format=MWAWCell::F_BOOLEAN;
671 record.m_valueType=MWAWCellContent::C_NUMBER;
672 content.setValue(double(input->readLong(1)));
673 f << "val=" << content.m_value << ",";
674 break;
675 }
676 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSSV1: can not read a bool\n"));
677 f << "###bool,";
678 break;
679 case 6:
680 if (input->checkPosition(actPos+1)) {
681 record.m_valueType=MWAWCellContent::C_NUMBER;
682 content.setValue(std::numeric_limits<double>::quiet_NaN());
683 record.m_hasNaNValue = true;
684 f << "val=nan" << input->readLong(1) << ",";
685 break;
686 }
687 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSSV1: can not read a nanc[formula]\n"));
688 f << "###nan[res],";
689 break;
690 case 7: // empty
691 break;
692 case 8: // checkme: does such cell can have data?
693 f << "recovered,";
694 break;
695 default:
696 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSSV1: unexpected type\n"));
697 f << "###type=" << type << ",";
698 ok=false;
699 break;
700 }
701 }
702 if (content.m_contentType!=MWAWCellContent::C_FORMULA)
703 content.m_contentType=record.m_valueType;
704 if (format.m_format==MWAWCell::F_UNKNOWN && content.isValueSet()) {
705 format.m_format=MWAWCell::F_NUMBER;
706 format.m_numberFormat=MWAWCell::F_NUMBER_GENERIC;
707 }
708 f << format << ",";
709 ascFile.addPos(pos);
710 ascFile.addNote(f.str().c_str());
711 if (ok) {
712 ascFile.addPos(input->tell());
713 ascFile.addNote("_");
714 }
715 return ok;
716 }
717
718 bool ClarisWksDbaseContent::readRecordSS(MWAWVec2i const &id, long pos, ClarisWksDbaseContent::Record &record)
719 {
720 if (m_version <= 3)
721 return readRecordSSV1(id, pos, record);
722 record=ClarisWksDbaseContent::Record();
723 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
724 libmwaw::DebugStream f;
725 f << "DBCHNK[spread" << id << "]:";
726 MWAWInputStreamPtr &input= m_parserState->m_input;
727 input->seek(pos, librevenge::RVNG_SEEK_SET);
728 auto sz=long(input->readULong(2));
729 long endPos=pos+sz+2;
730 if (!input->checkPosition(endPos) || sz < 4) {
731 f << "###sz=" << sz;
732 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSS: the sz seems bad\n"));
733 ascFile.addPos(pos);
734 ascFile.addNote(f.str().c_str());
735 return true;
736 }
737 auto type=static_cast<int>(input->readULong(1));
738 // next some format ?
739 auto val= static_cast<int>(input->readULong(1)); // 0-46
740 if (val) f << "format=" << std::hex << val << std::dec << ",";
741 record.m_style=static_cast<int>(input->readLong(2));
742 if (record.m_style) f << "Style-" << record.m_style << ",";
743
744 int fileFormat=0;
745 ClarisWksStyleManager::Style style;
746 ClarisWksStyleManager::CellFormat format;
747 if (m_document.getStyleManager()->get(record.m_style, style)) {
748 if (m_document.getStyleManager()->get(style.m_cellFormatId, format)) {
749 f << format << ",";
750 fileFormat=format.m_fileFormat;
751 }
752 if (style.m_fontId>=0)
753 m_document.getStyleManager()->get(style.m_fontId, record.m_font);
754 MWAWGraphicStyle graphStyle;
755 if (style.m_graphicId>=0 && m_document.getStyleManager()->get(style.m_graphicId, graphStyle)) {
756 if (graphStyle.hasSurfaceColor())
757 record.m_backgroundColor=graphStyle.m_surfaceColor;
758 }
759 }
760 switch (fileFormat) {
761 case 0: // general
762 break;
763 case 1:
764 format.m_format=MWAWCell::F_NUMBER;
765 format.m_numberFormat=MWAWCell::F_NUMBER_CURRENCY;
766 break;
767 case 2:
768 format.m_format=MWAWCell::F_NUMBER;
769 format.m_numberFormat=MWAWCell::F_NUMBER_PERCENT;
770 break;
771 case 3:
772 format.m_format=MWAWCell::F_NUMBER;
773 format.m_numberFormat=MWAWCell::F_NUMBER_SCIENTIFIC;
774 break;
775 case 4:
776 format.m_format=MWAWCell::F_NUMBER;
777 format.m_numberFormat=MWAWCell::F_NUMBER_DECIMAL;
778 break;
779 case 5:
780 case 6:
781 case 7:
782 case 8:
783 case 9: {
784 static char const *wh[]= {"%m/%d/%y", "%B %d, %y", "%B %d, %Y", "%a, %b %d %y", "%A, %B %d %Y" };
785 format.m_format=MWAWCell::F_DATE;
786 format.m_DTFormat=wh[fileFormat-5];
787 break;
788 }
789 case 10: // unknown
790 case 11:
791 break;
792 case 12:
793 case 13:
794 case 14:
795 case 15: {
796 static char const *wh[]= {"%H:%M", "%H:%M:%S", "%I:%M %p", "%I:%M:%S %p" };
797 format.m_format=MWAWCell::F_TIME;
798 format.m_DTFormat=wh[fileFormat-12];
799 break;
800 }
801 default: // unknown
802 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSS: find unknown format\n"));
803 f << "format=##" << fileFormat << ",";
804 break;
805 }
806
807 auto &content=record.m_content;
808 bool ok=true;
809 if (type==4) {
810 f << "formula,";
811 if (sz<7) {
812 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSS: the formula seems bad\n"));
813 f << "##sz,";
814 ok = false;
815 }
816 else {
817 type=static_cast<int>(input->readULong(1));
818 val=static_cast<int>(input->readLong(1));
819 if (val) f << "unkn=" << val << ","; // 1: maybe an enum stored by value instead of by id
820 auto formSz=static_cast<int>(input->readULong(1));
821 long actPos=input->tell();
822 ascFile.addDelimiter(actPos,'|');
823
824 if (8+formSz > sz) {
825 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSS: the formula seems bad\n"));
826 f << "###";
827 ok=false;
828 }
829 else {
830 std::vector<MWAWCellContent::FormulaInstruction> formula;
831 std::string error;
832 if (readFormula(id, actPos+1+formSz, formula, error)) {
833 content.m_contentType=MWAWCellContent::C_FORMULA;
834 content.m_formula=formula;
835 }
836 else {
837 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSS: can not read a formule\n"));
838 f << "###";
839 }
840 f << "form=";
841 for (auto const &fo : formula)
842 f << fo;
843 f << error << ",";
844
845 /** checkme: there does not seem to be alignment, but another
846 variable before the result */
847 input->seek(actPos+formSz+1, librevenge::RVNG_SEEK_SET);
848 ascFile.addDelimiter(input->tell(),'|');
849 sz=4+int(endPos-input->tell());
850 }
851 }
852 }
853 if (ok) {
854 switch (type) {
855 case 0:
856 case 1:
857 if (type==0)
858 f << "int,";
859 else
860 f << "long,";
861 if (sz==4) {
862 // rare, broken file ? AppleWorks seems to display a 0, but show a different number in the formula
863 record.m_valueType=MWAWCellContent::C_NUMBER;
864 content.setValue(0);
865 f << "##val=" << 0 << ",";
866 break;
867 }
868 if (sz<6+2*type) {
869 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSS: unexpected size for a int\n"));
870 f << "###";
871 break;
872 }
873 record.m_valueType=MWAWCellContent::C_NUMBER;
874 content.setValue(double(input->readLong(2+2*type)));
875 f << "val=" << content.m_value << ",";
876 break;
877 case 2: {
878 f << "float,";
879 if (sz<0xe) {
880 if (sz>=0xc && input->checkPosition(input->tell()+10)) {
881 // rare, broken file ? but as we have the main 8 bytes, let continue
882 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSS: unexpected size for a float, try to read it\n"));
883 f << "#";
884 }
885 else {
886 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSS: unexpected size for a float\n"));
887 f << "###";
888 break;
889 }
890 }
891 record.m_valueType=MWAWCellContent::C_NUMBER;
892 double value;
893 if (input->readDouble10(value, record.m_hasNaNValue)) {
894 content.setValue(value);
895 f << value << ",";
896 }
897 else {
898 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSS: can not read a float\n"));
899 f << "###,";
900 }
901 break;
902 }
903 case 3: {
904 f << "string,";
905 if (sz<5) {
906 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSS: the string seems bad\n"));
907 f << "###";
908 break;
909 }
910 auto stringSize = static_cast<int>(input->readULong(1));
911 if (stringSize+5>sz) {
912 // rare, broken file ? let try to read the beginning of the file
913 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSS: the string(II) seems bad, try to read the beginning\n"));
914 f << "###";
915 if (sz==5)
916 break;
917 stringSize=int(sz)-5;
918 }
919 record.m_valueType=MWAWCellContent::C_TEXT;
920 content.m_textEntry.setBegin(input->tell());
921 content.m_textEntry.setLength(long(stringSize));
922 std::string data("");
923 for (int c=0; c < stringSize; ++c) data+=char(input->readULong(1));
924 f << data << ",";
925 break;
926 }
927 // 4: formula
928 case 5: // bool
929 if (sz>=4+1) {
930 if (format.m_format==MWAWCell::F_UNKNOWN)
931 format.m_format=MWAWCell::F_BOOLEAN;
932 record.m_valueType=MWAWCellContent::C_NUMBER;
933 content.setValue(double(input->readLong(1)));
934 f << "val=" << content.m_value << ",";
935 break;
936 }
937 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSS: can not read a bool\n"));
938 f << "###bool,";
939 break;
940 case 6:
941 if (sz>=4+1) {
942 record.m_valueType=MWAWCellContent::C_NUMBER;
943 content.setValue(std::numeric_limits<double>::quiet_NaN());
944 record.m_hasNaNValue = true;
945 f << "val=nan" << input->readLong(1) << ",";
946 break;
947 }
948 break;
949 case 7: // link/anchor/goto
950 if (sz<4) {
951 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSS: the mark size seems bad\n"));
952 f << "###mark";
953 break;
954 }
955 f << "mark,";
956 break;
957 case 8:
958 case 9:
959 f << "type" << type << ",";
960 if (sz<4) {
961 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordSS: the type%d seems bad\n", type));
962 f << "###";
963 break;
964 }
965 break;
966 default:
967 f << "#type=" << type << ",";
968 }
969 }
970 if (content.m_contentType!=MWAWCellContent::C_FORMULA)
971 content.m_contentType=record.m_valueType;
972 record.m_format=format;
973 record.m_hAlign=format.m_hAlign;
974 record.m_borders=format.m_borders;
975 ascFile.addPos(pos);
976 ascFile.addNote(f.str().c_str());
977 ascFile.addPos(endPos);
978 ascFile.addNote("_");
979 return true;
980 }
981
982 bool ClarisWksDbaseContent::readRecordDB(MWAWVec2i const &id, long pos, ClarisWksDbaseContent::Record &record)
983 {
984 record=ClarisWksDbaseContent::Record();
985 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
986 libmwaw::DebugStream f;
987 f << "DBCHNK[dbase" << id << "]:";
988 MWAWInputStreamPtr &input= m_parserState->m_input;
989 input->seek(pos, librevenge::RVNG_SEEK_SET);
990 long sz=0;
991 long endPos=-1;
992 if (m_version>3) {
993 sz=long(input->readULong(2));
994 endPos=pos+sz+2;
995 if (!input->checkPosition(endPos) || sz < 2) {
996 f << "###sz=" << sz;
997 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordDB: the sz seems bad\n"));
998 ascFile.addPos(pos);
999 ascFile.addNote(f.str().c_str());
1000 return true;
1001 }
1002 }
1003 auto val=static_cast<int>(input->readULong(2));
1004 int type=(val>>12);
1005 val = int(val&0xFFF);
1006 MWAWCellContent &content=record.m_content;
1007 switch (type) {
1008 case 0: {
1009 f << "string,";
1010 if ((m_version<=3&&!input->checkPosition(pos+2+val)) ||
1011 (m_version>3 && (val+2>sz || val+4<sz))) {
1012 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordDB: the string(II) seems bad\n"));
1013 f << "###";
1014 break;
1015 }
1016 content.m_contentType=MWAWCellContent::C_TEXT;
1017 content.m_textEntry.setBegin(input->tell());
1018 content.m_textEntry.setLength(long(val));
1019 std::string data("");
1020 for (int c=0; c < val; ++c) data+=char(input->readULong(1));
1021 f << data << ",";
1022 break;
1023 }
1024 case 2: {
1025 if ((m_version<=3&&!input->checkPosition(pos+2+val+2)) ||
1026 (m_version>3 && val+2>sz)) {
1027 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordDB: can not read a text and style field\n"));
1028 f << "##string2" << type << "[" << val << "],";
1029 break;
1030 }
1031 content.m_contentType=MWAWCellContent::C_TEXT;
1032 content.m_textEntry.setBegin(input->tell());
1033 content.m_textEntry.setLength(long(val));
1034 std::string data("");
1035 for (int c=0; c < val; ++c) data+=char(input->readULong(1));
1036 f << "string2," << data << ",";
1037 if (val&1) input->seek(1, librevenge::RVNG_SEEK_CUR);
1038 auto N=int(input->readULong(2));
1039 f << "N=" << N << ",";
1040 int fontSize = m_version<=3 ? 10 : m_version<=5 ? 12 : 18;
1041 if ((m_version<=3 && !input->checkPosition(input->tell()+fontSize*N)) ||
1042 (m_version>3 && input->tell()+fontSize*N>endPos)) {
1043 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordDB: can not read the text's style\n"));
1044 f << "##N";
1045 break;
1046 }
1047 for (int i=0; i<N; ++i) {
1048 MWAWFont font;
1049 int posChar;
1050 if (!m_document.getStyleManager()->readFontAndPos(i, posChar, font)) {
1051 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordDB: can not read the text's style\n"));
1052 f << "##font";
1053 break;
1054 }
1055 record.m_posToFontMap[posChar]=font;
1056 }
1057 break;
1058 }
1059 case 4:
1060 // find also some string here when val is not null so let test
1061 if (val && ((m_version>3 && val+2<=sz && val+4>=sz) || (m_version<=3 && input->checkPosition(pos+2+val)))) {
1062 content.m_contentType=MWAWCellContent::C_TEXT;
1063 content.m_textEntry.setBegin(input->tell());
1064 content.m_textEntry.setLength(long(val));
1065 std::string data("");
1066 for (int c=0; c < val; ++c) data+=char(input->readULong(1));
1067 f << "string4," << data << ",";
1068 break;
1069 }
1070 f << "intDB,";
1071 if ((m_version<=3&&!input->checkPosition(pos+2)) || (m_version>3 && sz!=2)) {
1072 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordDB: unexpected size for a int\n"));
1073 f << "###";
1074 break;
1075 }
1076 content.m_contentType=MWAWCellContent::C_NUMBER;
1077 content.setValue(double(input->readLong(1)));
1078 break;
1079 case 8:
1080 case 9: {
1081 if (val) f << "unkn=" << std::hex << val << std::dec << ",";
1082 f << "float" << type << ",";
1083 if ((m_version<=3&&!input->checkPosition(pos+12)) || (m_version>3 && sz!=12)) {
1084 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordDB: unexpected size for a float\n"));
1085 f << "###";
1086 break;
1087 }
1088 double value;
1089 if (input->readDouble10(value, record.m_hasNaNValue)) {
1090 content.m_contentType=MWAWCellContent::C_NUMBER;
1091 content.setValue(value);
1092 f << value << ",";
1093 }
1094 else {
1095 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readRecordDB: can not read a float\n"));
1096 f << "###,";
1097 }
1098 break;
1099 }
1100 default:
1101 if (val) f << "unkn=" << std::hex << val << std::dec << ",";
1102 f << "#type=" << type << ",";
1103 }
1104 ascFile.addPos(pos);
1105 ascFile.addNote(f.str().c_str());
1106 if (m_version>3) {
1107 ascFile.addPos(endPos);
1108 ascFile.addNote("_");
1109 }
1110 return true;
1111 }
1112
1113 bool ClarisWksDbaseContent::get(MWAWVec2i const &pos, ClarisWksDbaseContent::Record &record) const
1114 {
1115 auto it=m_idColumnMap.find(pos[0]);
1116 if (it==m_idColumnMap.end()) return false;
1117 Column const &col=it->second;
1118 auto rIt=col.m_idRecordMap.find(pos[1]);
1119 if (rIt==col.m_idRecordMap.end()) return false;
1120 record=rIt->second;
1121 if (m_isSpreadsheet) return true;
1122
1123 static bool first=true;
1124 if (pos[0]>=0&&pos[0]<int(m_dbFormatList.size())) {
1125 auto const &format=m_dbFormatList[size_t(pos[0])];
1126 record.m_format=format;
1127 record.m_fileFormat=format.m_fileFormat;
1128 record.m_hAlign=format.m_hAlign;
1129 }
1130 else if (first) {
1131 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::get: can not find format for field %d\n", pos[0]));
1132 first=false;
1133 }
1134 return true;
1135 }
1136
1137 bool ClarisWksDbaseContent::send(MWAWVec2i const &pos)
1138 {
1139 MWAWListenerPtr listener=m_parserState->getMainListener();
1140 if (!listener) {
1141 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::send: can not find the listener\n"));
1142 return false;
1143 }
1144 Record record;
1145 if (!get(pos, record)) return true;
1146 auto const &content=record.m_content;
1147 listener->setFont(record.m_font);
1148 auto contentType= content.m_contentType==MWAWCellContent::C_FORMULA ? record.m_valueType : content.m_contentType;
1149 MWAWParagraph para;
1150 para.m_justify =
1151 record.m_hAlign==MWAWCell::HALIGN_LEFT ? MWAWParagraph::JustificationLeft :
1152 record.m_hAlign==MWAWCell::HALIGN_CENTER ? MWAWParagraph::JustificationCenter :
1153 record.m_hAlign==MWAWCell::HALIGN_RIGHT ? MWAWParagraph::JustificationRight :
1154 contentType==MWAWCellContent::C_TEXT ? MWAWParagraph::JustificationLeft :
1155 MWAWParagraph::JustificationRight;
1156 listener->setParagraph(para);
1157 switch (contentType) {
1158 case MWAWCellContent::C_NUMBER:
1159 if (record.m_fileFormat)
1160 send(content.m_value, record.m_hasNaNValue, ClarisWksStyleManager::CellFormat(record.m_format));
1161 else {
1162 std::stringstream s;
1163 s << content.m_value;
1164 listener->insertUnicodeString(librevenge::RVNGString(s.str().c_str()));
1165 }
1166 break;
1167 case MWAWCellContent::C_TEXT:
1168 if (content.m_textEntry.valid()) {
1169 MWAWInputStreamPtr &input= m_parserState->m_input;
1170 long fPos = input->tell();
1171 input->seek(content.m_textEntry.begin(), librevenge::RVNG_SEEK_SET);
1172 long endPos = content.m_textEntry.end();
1173 while (!input->isEnd() && input->tell() < endPos) {
1174 auto c=static_cast<unsigned char>(input->readULong(1));
1175 if (c==0x9)
1176 listener->insertTab();
1177 else if (c==0xd || c==0xa)
1178 listener->insertEOL();
1179 else
1180 listener->insertCharacter(c, input, endPos);
1181 }
1182 input->seek(fPos,librevenge::RVNG_SEEK_SET);
1183 }
1184 break;
1185 case MWAWCellContent::C_FORMULA:
1186 case MWAWCellContent::C_NONE:
1187 case MWAWCellContent::C_UNKNOWN:
1188 #if !defined(__clang__)
1189 default:
1190 #endif
1191 break;
1192 }
1193 return true;
1194 }
1195
1196 void ClarisWksDbaseContent::send(double val, bool isNotANumber, ClarisWksStyleManager::CellFormat const &format)
1197 {
1198 MWAWListenerPtr listener=m_parserState->getMainListener();
1199 if (!listener)
1200 return;
1201 std::stringstream s;
1202 int const &type=format.m_fileFormat;
1203 // note: if val*0!=0, val is a NaN so better so simply print NaN
1204 if (type <= 0 || type >=16 || type==10 || type==11 || isNotANumber) {
1205 s << val;
1206 listener->insertUnicodeString(librevenge::RVNGString(s.str().c_str()));
1207 return;
1208 }
1209 std::string value("");
1210 // FIXME: must not be here, change the reference date from 1/1/1904 to 1/1/1900
1211 if (MWAWCellContent::double2String(format.m_format==MWAWCell::F_DATE ? val+1460 : val, format, value))
1212 s << value;
1213 else {
1214 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::send: can not convert the actual value\n"));
1215 s << val;
1216 }
1217 listener->insertUnicodeString(librevenge::RVNGString(s.str().c_str()));
1218 }
1219
1220 ////////////////////////////////////////////////////////////
1221 // formula
1222 ////////////////////////////////////////////////////////////
1223 bool ClarisWksDbaseContent::readCellInFormula(MWAWVec2i const &pos, MWAWCellContent::FormulaInstruction &instr)
1224 {
1225 MWAWInputStreamPtr input=m_parserState->m_input;
1226 instr=MWAWCellContent::FormulaInstruction();
1227 instr.m_type=MWAWCellContent::FormulaInstruction::F_Cell;
1228 bool absolute[2] = { true, true};
1229 int cPos[2]= {0,0};
1230 for (int i=0; i<2; ++i) {
1231 auto val = static_cast<int>(input->readULong(2));
1232 if (val & 0x8000) {
1233 absolute[1-i]=false;
1234 if (val&0x4000)
1235 cPos[1-i] = pos[1-i]-1+(val-0xFFFF);
1236 else
1237 cPos[1-i] = pos[1-i]-1+(val-0x7FFF);
1238 }
1239 else
1240 cPos[1-i]=val;
1241 }
1242 if (m_version==6) {
1243 // checkme: what is this number
1244 auto val=static_cast<int>(input->readLong(2));
1245 static bool first = true;
1246 if (val!=-1 && first) {
1247 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readCellInFormula: ARGHHH value after cell is %d\n", val));
1248 first=false;
1249 }
1250 }
1251
1252 if (cPos[0] < 0 || cPos[1] < 0) {
1253 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readCellInFormula: can not read cell position\n"));
1254 return false;
1255 }
1256 instr.m_position[0]=MWAWVec2i(cPos[0],cPos[1]);
1257 instr.m_positionRelative[0]=MWAWVec2b(!absolute[0],!absolute[1]);
1258 return true;
1259 }
1260
1261 bool ClarisWksDbaseContent::readString(long endPos, std::string &res)
1262 {
1263 res="";
1264 MWAWInputStreamPtr input=m_parserState->m_input;
1265 long pos=input->tell();
1266 auto stringSize=static_cast<int>(input->readULong(1));
1267 if (pos+1+stringSize>endPos || !input->checkPosition(pos+1+stringSize)) {
1268 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readString: can not read string size\n"));
1269 return false;
1270 }
1271 for (int i=0; i<stringSize; ++i)
1272 res += char(input->readULong(1));
1273 return true;
1274 }
1275
1276 bool ClarisWksDbaseContent::readNumber(long endPos, double &res, bool &isNan)
1277 {
1278 MWAWInputStreamPtr input=m_parserState->m_input;
1279 long pos=input->tell();
1280 if (pos+10>endPos) {
1281 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readNumber: can not read a number\n"));
1282 return false;
1283 }
1284 return input->readDouble10(res, isNan);
1285 }
1286
1287 namespace ClarisWksDbaseContentInternal
1288 {
1289 struct Operators {
1290 char const *m_name;
1291 int m_arity;
1292 };
1293
1294 static Operators const s_listOperators[] = {
1295 { "<", 2}, { ">", 2}, { "=", 2}, { "<=", 2},
1296 { ">=", 2}, { "<>", 2}, { "&", 2},{ "+", 2},
1297
1298 { "-", 2}, { "*", 2}, { "/", 2}, { "^", 2},
1299 { "%", 1}/*percent*/, { "-", 1}, { "(", 1}, { "", -2} /*UNKN*/,
1300 };
1301 }
1302
1303 bool ClarisWksDbaseContent::readFormula(MWAWVec2i const &cPos, long endPos, std::vector<MWAWCellContent::FormulaInstruction> &formula, std::string &error)
1304 {
1305 MWAWInputStreamPtr input=m_parserState->m_input;
1306 libmwaw::DebugStream f;
1307 bool ok=true;
1308 long pos=input->tell();
1309 if (input->isEnd() || pos >= endPos)
1310 return false;
1311 int arity=0, type=static_cast<int>(input->readULong(1));
1312 bool isFunction=false, isOperator=false;
1313 MWAWCellContent::FormulaInstruction instr;
1314 switch (type) {
1315 case 0x10:
1316 if (pos+1+2>endPos) {
1317 ok=false;
1318 break;
1319 }
1320 instr.m_type=MWAWCellContent::FormulaInstruction::F_Long;
1321 instr.m_longValue=double(input->readLong(2));
1322 break;
1323 case 0x11:
1324 if (pos+1+4>endPos) {
1325 ok=false;
1326 break;
1327 }
1328 instr.m_type=MWAWCellContent::FormulaInstruction::F_Long;
1329 instr.m_longValue=double(input->readLong(4));
1330 break;
1331 case 0x12: {
1332 double value;
1333 bool isNan;
1334 ok=readNumber(endPos, value, isNan);
1335 if (!ok) break;
1336 instr.m_type=MWAWCellContent::FormulaInstruction::F_Double;
1337 instr.m_doubleValue=value;
1338 break;
1339 }
1340 case 0x13: {
1341 ok=readCellInFormula(cPos,instr);
1342 break;
1343 }
1344 case 0x1b:
1345 if (pos+1+8 > endPos) {
1346 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readFormula: find instruction 0x1b, unknown size\n"));
1347 f << "##[code=1b,short],";
1348 ok = false;
1349 break;
1350 }
1351 /* found in some web files followed by a bad cell list */
1352 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readFormula: find instruction 0x1b\n"));
1353 f << "##[code=1b],";
1354 instr.m_type=MWAWCellContent::FormulaInstruction::F_CellList;
1355 input->seek(8, librevenge::RVNG_SEEK_SET);
1356 break;
1357 case 0x14: {
1358 if (pos+1+8 > endPos || !readCellInFormula(cPos, instr)) {
1359 f << "###list cell short";
1360 ok = false;
1361 break;
1362 }
1363 MWAWCellContent::FormulaInstruction instr2;
1364 if (!readCellInFormula(cPos, instr2)) {
1365 f << "###list cell short2";
1366 ok = false;
1367 break;
1368 }
1369 instr.m_type=MWAWCellContent::FormulaInstruction::F_CellList;
1370 instr.m_position[1]=instr2.m_position[0];
1371 instr.m_positionRelative[1]=instr2.m_positionRelative[0];
1372 break;
1373 }
1374 case 0x15: {
1375 std::string text;
1376 ok=readString(endPos, text);
1377 if (!ok) break;
1378 instr.m_type=MWAWCellContent::FormulaInstruction::F_Text;
1379 instr.m_content=text;
1380 break;
1381 }
1382 case 0x16: {
1383 if (pos+1+2>endPos) {
1384 ok=false;
1385 break;
1386 }
1387 isFunction=true;
1388 auto val=static_cast<int>(input->readULong(1));
1389 arity=static_cast<int>(input->readULong(1));
1390 std::string name("");
1391 if (val<0x70) {
1392 static char const *wh[] = {
1393 "Abs", "Acos", "Alert", "And", "Code", "Asin", "Atan", "Atan2",
1394 "Average", "Choose", "Char", "Concatenate", "Cos", "Count", "Date", "DateToText",
1395
1396 "DateValue", "Day", "DayName", "DayOfYear", "Degrees", "NA"/* error*/, "Exact", "Exp",
1397 "Frac", "FV", "HLookup", "Hour", "If", "Index", "Int", "IRR",
1398
1399 "IsBlank", "IsError", "IsNA", "IsNumber", "IsText", "Left", "Ln", "Log",
1400 "Log10", "LookUp", "Lower", "Match", "Max", "Mid", "Min", "Minute",
1401
1402 "MIRR", "Mod", "MonthName", "NA", "Not", "Now", "NPER", "NPV",
1403 "N" /*NumToText*/, "Or", "Pi", "PMT", "Product", "Proper", "PV", "Radians",
1404
1405 "Rand", "Rate", "Replace", "Right", "Round", "Second", "Sign", "Sqrt",
1406 "StDev", "Sum", "Tan", "Rept", "Value"/*TextToNum*/, "Time", "TimeToText", "TimeValue",
1407
1408 "", "Trim", "Type", "Upper", "Var", "VLookUp", "WeekDay", "WeekOfYear",
1409 "Year", "Find", "Column", "Row", "Fact", "Len", "Sin", "Month",
1410
1411 "Trunc", "Count2", "Macro", "Beep", "", "", "", "",
1412 "", "", "", "", "", "", "", "",
1413 };
1414 name=wh[val];
1415 }
1416 if (name.empty()) {
1417 f << "###";
1418 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::readFormula: find unknown function\n"));
1419 std::stringstream s;
1420 s << "Funct" << std::hex << val << std::dec;
1421 name=s.str();
1422 }
1423 instr.m_type=MWAWCellContent::FormulaInstruction::F_Function;
1424 instr.m_content=name;
1425 formula.push_back(instr);
1426 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
1427 instr.m_content="(";
1428 break;
1429 }
1430 case 0x18: // bool
1431 if (pos+1+1>endPos) {
1432 ok=false;
1433 break;
1434 }
1435 instr.m_type=MWAWCellContent::FormulaInstruction::F_Long;
1436 instr.m_longValue=static_cast<int>(input->readLong(1));
1437 break;
1438 case 0x19: // database, cell
1439 if (pos+1+1>endPos) {
1440 ok = false;
1441 break;
1442 }
1443 instr.m_type=MWAWCellContent::FormulaInstruction::F_Cell;
1444 instr.m_position[0]=MWAWVec2i(int(input->readULong(1)),0);
1445 instr.m_positionRelative[0]=MWAWVec2b(true, false);
1446 break;
1447 case 0x1f: // final equal sign
1448 return true;
1449 default: {
1450 std::string op("");
1451 if (type<0x10) {
1452 op=ClarisWksDbaseContentInternal::s_listOperators[type].m_name;
1453 arity=ClarisWksDbaseContentInternal::s_listOperators[type].m_arity;
1454 }
1455 if (!op.empty()&&arity>0) {
1456 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
1457 instr.m_content=op;
1458 isOperator=true;
1459 break;
1460 }
1461 f << "##type=" << std::hex << type << std::dec << ",";
1462 ok=false;
1463 break;
1464 }
1465 }
1466 error+=f.str();
1467 if (!ok) {
1468 input->seek(pos, librevenge::RVNG_SEEK_SET);
1469 return false;
1470 }
1471 if (isOperator && instr.m_content=="%") {
1472 // special case, percentage %2 => 2%
1473 if (!readFormula(cPos, endPos, formula, error))
1474 return false;
1475 formula.push_back(instr);
1476 arity=0;
1477 }
1478 else if (!isOperator || arity==1)
1479 formula.push_back(instr);
1480 if (isFunction && arity>1) {
1481 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
1482 instr.m_content=";";
1483 }
1484 for (int i=0; i < arity; ++i) {
1485 if (!readFormula(cPos, endPos, formula, error))
1486 return false;
1487 if (i+1==arity) break;
1488 formula.push_back(instr);
1489 }
1490 if (isFunction || instr.m_content=="(") {
1491 instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator;
1492 instr.m_content=")";
1493 formula.push_back(instr);
1494 }
1495
1496 return true;
1497 }
1498
1499 ////////////////////////////////////////////////////////////
1500 //
1501 ////////////////////////////////////////////////////////////
1502 void ClarisWksDbaseContent::Record::updateFormulaCells(MWAWVec2i const &removeDelta)
1503 {
1504 if (m_content.m_contentType!=MWAWCellContent::C_FORMULA)
1505 return;
1506 for (auto &instr : m_content.m_formula) {
1507 int numCell=instr.m_type==MWAWCellContent::FormulaInstruction::F_Cell ? 1 :
1508 instr.m_type==MWAWCellContent::FormulaInstruction::F_CellList ? 2 : 0;
1509 for (int c=0; c<numCell; ++c) {
1510 instr.m_position[c]-=removeDelta;
1511 if (instr.m_position[c][0]<0 || instr.m_position[c][1]<0) {
1512 static bool first=true;
1513 if (first) {
1514 MWAW_DEBUG_MSG(("ClarisWksDbaseContent::Record::updateFormulaCells: some cell's positions are bad, remove formula\n"));
1515 first=false;
1516 }
1517 // revert to the basic cell type
1518 m_content.m_contentType=m_valueType;
1519 return;
1520 }
1521 }
1522 }
1523 }
1524
1525 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: