| 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 <cstring> |
| 36 |
#include <iomanip> |
| 37 |
#include <iostream> |
| 38 |
#include <limits> |
| 39 |
#include <map> |
| 40 |
#include <set> |
| 41 |
#include <sstream> |
| 42 |
|
| 43 |
#include <librevenge/librevenge.h> |
| 44 |
|
| 45 |
#include "MWAWCell.hxx" |
| 46 |
#include "MWAWFont.hxx" |
| 47 |
#include "MWAWFontConverter.hxx" |
| 48 |
#include "MWAWHeader.hxx" |
| 49 |
#include "MWAWParagraph.hxx" |
| 50 |
#include "MWAWPosition.hxx" |
| 51 |
#include "MWAWRSRCParser.hxx" |
| 52 |
#include "MWAWSpreadsheetListener.hxx" |
| 53 |
#include "MWAWSubDocument.hxx" |
| 54 |
|
| 55 |
#include "JazzSSParser.hxx" |
| 56 |
|
| 57 |
/** Internal: the structures of a JazzSSParser */ |
| 58 |
namespace JazzSSParserInternal |
| 59 |
{ |
| 60 |
//! Internal: the cell of a JazzSSParser |
| 61 |
struct Cell final : public MWAWCell { |
| 62 |
//! constructor |
| 63 |
explicit Cell() |
| 64 |
: MWAWCell() |
| 65 |
, m_content() |
| 66 |
{ |
| 67 |
} |
| 68 |
Cell(Cell const &)=default; |
| 69 |
//! destructor |
| 70 |
~Cell() final; |
| 71 |
//! the cell content |
| 72 |
MWAWCellContent m_content; |
| 73 |
}; |
| 74 |
|
| 75 |
Cell::~Cell() |
| 76 |
{ |
| 77 |
} |
| 78 |
//////////////////////////////////////// |
| 79 |
//! Internal: the state of a JazzSSParser |
| 80 |
struct State { |
| 81 |
//! constructor |
| 82 |
State() : |
| 83 |
m_isDatabase(false), |
| 84 |
m_dimensions(-1,-1), |
| 85 |
m_font(), |
| 86 |
m_widths(), |
| 87 |
m_posToCells() |
| 88 |
{ |
| 89 |
} |
| 90 |
//! true if the file is a database file |
| 91 |
bool m_isDatabase; |
| 92 |
//! the sheet dimensions |
| 93 |
MWAWVec2i m_dimensions; |
| 94 |
//! the cell default font |
| 95 |
MWAWFont m_font; |
| 96 |
//! the columns width |
| 97 |
std::vector<float> m_widths; |
| 98 |
//! map of cells sorted by columns |
| 99 |
std::map<MWAWVec2i, Cell> m_posToCells; |
| 100 |
}; |
| 101 |
|
| 102 |
//////////////////////////////////////// |
| 103 |
//! Internal: the subdocument of a JazzSSParserInternal |
| 104 |
class SubDocument final : public MWAWSubDocument |
| 105 |
{ |
| 106 |
public: |
| 107 |
SubDocument(JazzSSParser &parser, MWAWInputStreamPtr const &input, MWAWEntry const &entry) |
| 108 |
: MWAWSubDocument(&parser, input, entry) |
| 109 |
, m_multiParser(parser) |
| 110 |
{ |
| 111 |
} |
| 112 |
|
| 113 |
//! destructor |
| 114 |
~SubDocument() final {} |
| 115 |
|
| 116 |
//! operator!= |
| 117 |
bool operator!=(MWAWSubDocument const &doc) const final; |
| 118 |
|
| 119 |
//! the parser function |
| 120 |
void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final; |
| 121 |
|
| 122 |
protected: |
| 123 |
/** the main parser */ |
| 124 |
JazzSSParser &m_multiParser; |
| 125 |
}; |
| 126 |
|
| 127 |
void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/) |
| 128 |
{ |
| 129 |
if (!listener.get()) { |
| 130 |
MWAW_DEBUG_MSG(("JazzSSParser::SubDocument::parse: no listener\n")); |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
long pos = m_input->tell(); |
| 135 |
//m_multiParser.sendText(m_zone); |
| 136 |
m_input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 137 |
} |
| 138 |
|
| 139 |
bool SubDocument::operator!=(MWAWSubDocument const &doc) const |
| 140 |
{ |
| 141 |
if (MWAWSubDocument::operator!=(doc)) return true; |
| 142 |
auto const *sDoc = dynamic_cast<SubDocument const *>(&doc); |
| 143 |
if (!sDoc) return true; |
| 144 |
if (&m_multiParser != &sDoc->m_multiParser) return true; |
| 145 |
return false; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
//////////////////////////////////////////////////////////// |
| 150 |
// constructor/destructor, ... |
| 151 |
//////////////////////////////////////////////////////////// |
| 152 |
JazzSSParser::JazzSSParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header) |
| 153 |
: MWAWSpreadsheetParser(input, rsrcParser, header) |
| 154 |
, m_state(new JazzSSParserInternal::State) |
| 155 |
{ |
| 156 |
setAsciiName("main-1"); |
| 157 |
// reduce the margin (in case, the page is not defined) |
| 158 |
getPageSpan().setMargins(0.1); |
| 159 |
} |
| 160 |
|
| 161 |
JazzSSParser::~JazzSSParser() |
| 162 |
{ |
| 163 |
} |
| 164 |
|
| 165 |
//////////////////////////////////////////////////////////// |
| 166 |
// the parser |
| 167 |
//////////////////////////////////////////////////////////// |
| 168 |
void JazzSSParser::parse(librevenge::RVNGSpreadsheetInterface *docInterface) |
| 169 |
{ |
| 170 |
if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException()); |
| 171 |
bool ok = true; |
| 172 |
try { |
| 173 |
// create the asciiFile |
| 174 |
ascii().setStream(getInput()); |
| 175 |
ascii().open(asciiName()); |
| 176 |
checkHeader(nullptr); |
| 177 |
ok = createZones(); |
| 178 |
if (ok) { |
| 179 |
createDocument(docInterface); |
| 180 |
sendSpreadsheet(); |
| 181 |
} |
| 182 |
} |
| 183 |
catch (...) { |
| 184 |
MWAW_DEBUG_MSG(("JazzSSParser::parse: exception catched when parsing\n")); |
| 185 |
ok = false; |
| 186 |
} |
| 187 |
|
| 188 |
ascii().reset(); |
| 189 |
resetSpreadsheetListener(); |
| 190 |
if (!ok) throw(libmwaw::ParseException()); |
| 191 |
} |
| 192 |
|
| 193 |
//////////////////////////////////////////////////////////// |
| 194 |
// create the document |
| 195 |
//////////////////////////////////////////////////////////// |
| 196 |
void JazzSSParser::createDocument(librevenge::RVNGSpreadsheetInterface *documentInterface) |
| 197 |
{ |
| 198 |
if (!documentInterface) return; |
| 199 |
if (getSpreadsheetListener()) { |
| 200 |
MWAW_DEBUG_MSG(("JazzSSParser::createDocument: listener already exist\n")); |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
// create the page list |
| 205 |
MWAWPageSpan ps(getPageSpan()); |
| 206 |
ps.setPageSpan(1); |
| 207 |
std::vector<MWAWPageSpan> pageList(1,ps); |
| 208 |
// |
| 209 |
MWAWSpreadsheetListenerPtr listen(new MWAWSpreadsheetListener(*getParserState(), pageList, documentInterface)); |
| 210 |
setSpreadsheetListener(listen); |
| 211 |
listen->startDocument(); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
//////////////////////////////////////////////////////////// |
| 216 |
// |
| 217 |
// Intermediate level |
| 218 |
// |
| 219 |
//////////////////////////////////////////////////////////// |
| 220 |
bool JazzSSParser::createZones() |
| 221 |
{ |
| 222 |
#ifdef DEBUG_WITH_FILES |
| 223 |
MWAWRSRCParserPtr rsrcParser = getRSRCParser(); |
| 224 |
// normally contains a string:256 JAZZ 01.000,1 |
| 225 |
if (rsrcParser) |
| 226 |
rsrcParser->getEntriesMap(); |
| 227 |
#endif |
| 228 |
MWAWInputStreamPtr input = getInput(); |
| 229 |
input->seek(6, librevenge::RVNG_SEEK_SET); |
| 230 |
libmwaw::DebugStream f; |
| 231 |
while (!input->isEnd()) { |
| 232 |
long pos=input->tell(); |
| 233 |
f.str(""); |
| 234 |
if (!input->checkPosition(pos+4)) |
| 235 |
break; |
| 236 |
int id=int(input->readULong(2)); |
| 237 |
f << "Entries(Zone" << id << "A):"; |
| 238 |
int len=int(input->readULong(2)); |
| 239 |
long endPos=pos+4+len; |
| 240 |
if (!input->checkPosition(endPos)) { |
| 241 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 242 |
break; |
| 243 |
} |
| 244 |
bool isParsed = false, done=false; |
| 245 |
switch (id) { |
| 246 |
// 0 version (already done) |
| 247 |
case 1: |
| 248 |
f.str(""); |
| 249 |
f << "Entries(End):"; |
| 250 |
if (len!=0) |
| 251 |
f << "###"; |
| 252 |
else |
| 253 |
done=true; |
| 254 |
break; |
| 255 |
case 6: |
| 256 |
isParsed = readSheetSize(endPos); |
| 257 |
break; |
| 258 |
case 0xc: // blank cell |
| 259 |
case 0xd: // integer cell |
| 260 |
case 0xe: // floating cell |
| 261 |
case 0xf: // label cell |
| 262 |
case 0x10: // formula cell |
| 263 |
isParsed = readCell(id, endPos); |
| 264 |
break; |
| 265 |
case 0x11: |
| 266 |
isParsed = readZone11(endPos); |
| 267 |
break; |
| 268 |
case 0x12: |
| 269 |
case 0x13: |
| 270 |
if (len%2) { |
| 271 |
MWAW_DEBUG_MSG(("JazzSSParser::createZones: unexpected size for page break\n")); |
| 272 |
f << "###"; |
| 273 |
break; |
| 274 |
} |
| 275 |
f.str(""); |
| 276 |
f << "Entries(" << (id==0x12 ? "ColBreak" : "RowBreak") << "):"; |
| 277 |
f << "br=["; |
| 278 |
for (int i=0; i<len/2; ++i) f << input->readLong(2) << ","; |
| 279 |
f << "],"; |
| 280 |
break; |
| 281 |
case 0x15: |
| 282 |
if (len!=0x126 && len!=0x16c) { |
| 283 |
MWAW_DEBUG_MSG(("JazzSSParser::createZones: unexpected size for document\n")); |
| 284 |
f << "###"; |
| 285 |
break; |
| 286 |
} |
| 287 |
isParsed=readDocument(endPos); |
| 288 |
break; |
| 289 |
default: |
| 290 |
break; |
| 291 |
} |
| 292 |
if (len && input->tell()!=endPos) |
| 293 |
ascii().addDelimiter(input->tell(),'|'); |
| 294 |
input->seek(endPos, librevenge::RVNG_SEEK_SET); |
| 295 |
if (!isParsed) { |
| 296 |
ascii().addPos(pos); |
| 297 |
ascii().addNote(f.str().c_str()); |
| 298 |
} |
| 299 |
if (done) |
| 300 |
break; |
| 301 |
} |
| 302 |
if (!input->isEnd()) { |
| 303 |
MWAW_DEBUG_MSG(("JazzSSParser::createZones: find extra data\n")); |
| 304 |
ascii().addPos(input->tell()); |
| 305 |
ascii().addNote("Entries(BAD):###"); |
| 306 |
} |
| 307 |
return !m_state->m_widths.empty() && !m_state->m_posToCells.empty(); |
| 308 |
} |
| 309 |
|
| 310 |
//////////////////////////////////////////////////////////// |
| 311 |
// |
| 312 |
// Low level |
| 313 |
// |
| 314 |
//////////////////////////////////////////////////////////// |
| 315 |
|
| 316 |
//////////////////////////////////////////////////////////// |
| 317 |
// spreadsheet |
| 318 |
//////////////////////////////////////////////////////////// |
| 319 |
bool JazzSSParser::readZone11(long endPos) |
| 320 |
{ |
| 321 |
auto input=getInput(); |
| 322 |
long pos=input->tell(); |
| 323 |
long len=endPos-pos; |
| 324 |
if (len<28) { |
| 325 |
MWAW_DEBUG_MSG(("JazzSSParser::createZones: unexpected size for name cells\n")); |
| 326 |
return false; |
| 327 |
} |
| 328 |
|
| 329 |
libmwaw::DebugStream f; |
| 330 |
f.str(""); |
| 331 |
f << "Entries(Zone11):"; |
| 332 |
std::string name; |
| 333 |
for (int i=0; i<16; ++i) { |
| 334 |
char c=char(input->readULong(1)); |
| 335 |
if (c==0) |
| 336 |
break; |
| 337 |
name+=c; |
| 338 |
} |
| 339 |
f << name << ","; |
| 340 |
input->seek(pos+4+16, librevenge::RVNG_SEEK_SET); |
| 341 |
int val=int(input->readLong(2)); |
| 342 |
switch (val) { |
| 343 |
case 0: |
| 344 |
f << "name,"; |
| 345 |
break; |
| 346 |
case 7: |
| 347 |
f << "sort,"; |
| 348 |
break; |
| 349 |
case 8: |
| 350 |
f << "distribution,"; |
| 351 |
break; |
| 352 |
case 9: |
| 353 |
f << "table,"; |
| 354 |
break; |
| 355 |
default: |
| 356 |
f << "f0=" << val << ","; |
| 357 |
break; |
| 358 |
} |
| 359 |
int dim[4]; |
| 360 |
for (auto &d : dim) d=int(input->readULong(2)); |
| 361 |
f << "range=" << MWAWBox2i(MWAWVec2i(dim[0],dim[1]),MWAWVec2i(dim[2],dim[3])) << ","; |
| 362 |
if (len==28) { |
| 363 |
val=int(input->readLong(2)); |
| 364 |
if (val!=4) f << "f1=" << val << ","; |
| 365 |
} |
| 366 |
ascii().addPos(pos-4); |
| 367 |
ascii().addNote(f.str().c_str()); |
| 368 |
return true; |
| 369 |
} |
| 370 |
|
| 371 |
bool JazzSSParser::readDocument(long endPos) |
| 372 |
{ |
| 373 |
auto input=getInput(); |
| 374 |
long pos = input->tell(); |
| 375 |
if (endPos-pos != 0x126 && endPos-pos != 0x16c) { |
| 376 |
MWAW_DEBUG_MSG(("JazzSSParser::readDocument: block is too short\n")); |
| 377 |
return false; |
| 378 |
} |
| 379 |
m_state->m_isDatabase=endPos-pos==0x16c; |
| 380 |
libmwaw::DebugStream f; |
| 381 |
f << "Entries(Document):"; |
| 382 |
int val; |
| 383 |
if (!m_state->m_isDatabase) { |
| 384 |
for (int i=0; i<4; ++i) { |
| 385 |
val=int(input->readULong(2)); |
| 386 |
int const expected[]= {0,0x100,0,0x7150}; |
| 387 |
if (val!=expected[i]) |
| 388 |
f << "f" << i << "=" << val << ","; |
| 389 |
} |
| 390 |
int dim[2], dim2[2]; |
| 391 |
for (auto &d : dim) d=int(input->readULong(2)); |
| 392 |
for (auto &d : dim2) d=int(input->readULong(2)); |
| 393 |
if (dim[0]!=dim2[0] || dim[1]!=dim2[1]) |
| 394 |
f << "select=" << MWAWVec2i(dim[0],dim[1]) << "<->" << MWAWVec2i(dim2[0],dim2[1]) << ","; |
| 395 |
else |
| 396 |
f << "select=" << MWAWVec2i(dim[0],dim[1]) << ","; |
| 397 |
for (auto &d : dim2) d=int(input->readULong(2)); |
| 398 |
if (dim[0]!=dim2[0] || dim[1]!=dim2[1]) |
| 399 |
f << "pos=" << MWAWVec2i(dim2[0],dim2[1]) << ","; |
| 400 |
for (int i=0; i<2; ++i) { |
| 401 |
val=int(input->readULong(2)); |
| 402 |
int const expected[]= {0,5}; |
| 403 |
if (val!=expected[i]) |
| 404 |
f << "f" << i+4 << "=" << val << ","; |
| 405 |
} |
| 406 |
} |
| 407 |
int defWidth=int(input->readULong(1)); // width in number of char |
| 408 |
if (defWidth!=7) f << "w[def]=" << defWidth << ","; |
| 409 |
ascii().addPos(pos-4); |
| 410 |
ascii().addNote(f.str().c_str()); |
| 411 |
|
| 412 |
pos=input->tell(); |
| 413 |
f.str(""); |
| 414 |
f << "Document-width:"; |
| 415 |
std::vector<int> colWidths; |
| 416 |
colWidths.resize(255); |
| 417 |
for (size_t i=0; i<256; ++i) { // w0=0, w1 correspond to the width of column A, ... |
| 418 |
val=int(input->readULong(1)); |
| 419 |
if (i) |
| 420 |
colWidths[i-1]=val ? val : defWidth; |
| 421 |
if (val) f << "w" << i << "=" << val << ","; |
| 422 |
} |
| 423 |
input->seek(1, librevenge::RVNG_SEEK_CUR); |
| 424 |
ascii().addPos(pos); |
| 425 |
ascii().addNote(f.str().c_str()); |
| 426 |
|
| 427 |
pos=input->tell(); |
| 428 |
f.str(""); |
| 429 |
f << "Document-A:"; |
| 430 |
for (int i=0; i<2; ++i) { |
| 431 |
val=int(input->readLong(1)); |
| 432 |
if (val==-1) continue; |
| 433 |
char const *wh[]= {"border","grid"}; |
| 434 |
if (val==0) f << "hide[" << wh[i] << "],"; |
| 435 |
else f << "#show[" << wh[i] << "]=" << val << ","; |
| 436 |
} |
| 437 |
MWAWFont &font=m_state->m_font; |
| 438 |
font.setId(int(input->readULong(2))); |
| 439 |
float fSz=float(input->readULong(2)); |
| 440 |
font.setSize(fSz); |
| 441 |
val=int(input->readULong(1)); |
| 442 |
unsigned flags=0; |
| 443 |
if (val&0x1) flags |= MWAWFont::boldBit; |
| 444 |
if (val&0x2) flags |= MWAWFont::italicBit; |
| 445 |
if (val&0x4) font.setUnderlineStyle(MWAWFont::Line::Simple); |
| 446 |
if (val&0x8) flags |= MWAWFont::embossBit; |
| 447 |
if (val&0x10) flags |= MWAWFont::shadowBit; |
| 448 |
val &= 0xe0; |
| 449 |
font.setFlags(flags); |
| 450 |
f << "font=["; |
| 451 |
f << font.getDebugString(getFontConverter()); |
| 452 |
if (val) |
| 453 |
f << "fl=" << std::hex << val << std::dec << ","; |
| 454 |
f << "],"; |
| 455 |
val=int(input->readULong(1)); |
| 456 |
if (val&0x8) |
| 457 |
f << "show[formula],"; |
| 458 |
val&=0xf7; |
| 459 |
if (val) f << "fl=" << std::hex << val << std::dec << ","; |
| 460 |
for (int i=0; i<2; ++i) { |
| 461 |
val=int(input->readULong(2)); |
| 462 |
if (val) f << "g" << i+1 << "=" << val << ","; |
| 463 |
} |
| 464 |
// time to update the columns width |
| 465 |
m_state->m_widths.clear(); |
| 466 |
m_state->m_widths.reserve(255); |
| 467 |
for (auto const &w : colWidths) m_state->m_widths.push_back(float(w)*fSz); |
| 468 |
|
| 469 |
if (input->tell()!=endPos) |
| 470 |
ascii().addDelimiter(input->tell(), '|'); |
| 471 |
ascii().addPos(pos); |
| 472 |
ascii().addNote(f.str().c_str()); |
| 473 |
|
| 474 |
return true; |
| 475 |
} |
| 476 |
|
| 477 |
bool JazzSSParser::readCell(int id, long endPos) |
| 478 |
{ |
| 479 |
auto input=getInput(); |
| 480 |
long pos = input->tell(); |
| 481 |
if (endPos-pos < 6) { |
| 482 |
MWAW_DEBUG_MSG(("JazzSSParser::readCell: block is too short\n")); |
| 483 |
return false; |
| 484 |
} |
| 485 |
JazzSSParserInternal::Cell cell; |
| 486 |
MWAWCell::Format format; |
| 487 |
MWAWCellContent &content=cell.m_content; |
| 488 |
libmwaw::DebugStream f; |
| 489 |
f << "Entries(Cell):"; |
| 490 |
int val=int(input->readULong(2)); |
| 491 |
if (val&0x8000) |
| 492 |
f << "locked,"; |
| 493 |
int type=(val>>12)&7; |
| 494 |
int digits=(val>>8)&0xf; |
| 495 |
format.m_digits=digits; |
| 496 |
switch (type) { |
| 497 |
case 0: |
| 498 |
f << "fixed,"; |
| 499 |
format.m_format=MWAWCell::F_NUMBER; |
| 500 |
format.m_numberFormat=MWAWCell::F_NUMBER_DECIMAL; |
| 501 |
break; |
| 502 |
case 1: |
| 503 |
f << "scientific,"; |
| 504 |
format.m_format=MWAWCell::F_NUMBER; |
| 505 |
format.m_numberFormat=MWAWCell::F_NUMBER_SCIENTIFIC; |
| 506 |
break; |
| 507 |
case 2: |
| 508 |
f << "currency,"; |
| 509 |
format.m_format=MWAWCell::F_NUMBER; |
| 510 |
format.m_numberFormat=MWAWCell::F_NUMBER_CURRENCY; |
| 511 |
break; |
| 512 |
case 3: |
| 513 |
f << "percent,"; |
| 514 |
format.m_format=MWAWCell::F_NUMBER; |
| 515 |
format.m_numberFormat=MWAWCell::F_NUMBER_PERCENT; |
| 516 |
break; |
| 517 |
case 4: |
| 518 |
f << "thousand,"; |
| 519 |
format.m_format=MWAWCell::F_NUMBER; |
| 520 |
format.m_thousandHasSeparator=true; |
| 521 |
break; |
| 522 |
case 5: |
| 523 |
f << "text,"; |
| 524 |
format.m_format=MWAWCell::F_TEXT; |
| 525 |
switch (digits&3) { |
| 526 |
case 0: |
| 527 |
cell.setHAlignment(MWAWCell::HALIGN_LEFT); |
| 528 |
f << "left,"; |
| 529 |
break; |
| 530 |
case 1: |
| 531 |
cell.setHAlignment(MWAWCell::HALIGN_RIGHT); |
| 532 |
f << "right,"; |
| 533 |
break; |
| 534 |
case 2: |
| 535 |
cell.setHAlignment(MWAWCell::HALIGN_CENTER); |
| 536 |
f << "center,"; |
| 537 |
break; |
| 538 |
default: |
| 539 |
case 3: { |
| 540 |
static bool first=true; |
| 541 |
if (first) { |
| 542 |
first=false; |
| 543 |
MWAW_DEBUG_MSG(("JazzSSParser::readCell: repeated text is ignored\n")); |
| 544 |
} |
| 545 |
f << "#repeat,"; |
| 546 |
break; |
| 547 |
} |
| 548 |
} |
| 549 |
if (digits&4) |
| 550 |
f << "extend[cell],"; |
| 551 |
if (digits&8) { |
| 552 |
MWAW_DEBUG_MSG(("JazzSSParser::readCell: unknown text align8\n")); |
| 553 |
f << "##align8,"; |
| 554 |
} |
| 555 |
digits=2; |
| 556 |
break; |
| 557 |
case 7: |
| 558 |
switch (digits) { |
| 559 |
case 0: // a bar 0 empty, 10? full |
| 560 |
f << "bar,"; |
| 561 |
format.m_format=MWAWCell::F_NUMBER; |
| 562 |
break; |
| 563 |
case 1: |
| 564 |
f << "number[general],"; |
| 565 |
format.m_format=MWAWCell::F_NUMBER; |
| 566 |
break; |
| 567 |
case 2: |
| 568 |
case 3: |
| 569 |
case 4: |
| 570 |
case 5: { |
| 571 |
f << "date,"; |
| 572 |
format.m_format=MWAWCell::F_DATE; |
| 573 |
char const *wh[]= {"%d-%b-%y", "%d-%b", "%b-%y", "%m/%d/%y"}; |
| 574 |
format.m_DTFormat=wh[digits-2]; |
| 575 |
f << format.m_DTFormat << ","; |
| 576 |
break; |
| 577 |
} |
| 578 |
case 7: |
| 579 |
case 8: |
| 580 |
case 9: |
| 581 |
case 10: { |
| 582 |
f << "time,"; |
| 583 |
format.m_format=MWAWCell::F_TIME; |
| 584 |
char const *wh[]= {"%I:%M:%S %p", "%I:%M %p", "%H:%M:%S", "%H:%M"}; |
| 585 |
format.m_DTFormat=wh[digits-7]; |
| 586 |
f << format.m_DTFormat << ","; |
| 587 |
break; |
| 588 |
} |
| 589 |
case 11: |
| 590 |
f << "text[formula],"; |
| 591 |
break; |
| 592 |
case 15: |
| 593 |
f << "general,"; |
| 594 |
break; |
| 595 |
default: |
| 596 |
MWAW_DEBUG_MSG(("JazzSSParser::readCell: unknown format=7\n")); |
| 597 |
f << "##type1=" << digits << ","; |
| 598 |
break; |
| 599 |
} |
| 600 |
break; |
| 601 |
default: |
| 602 |
MWAW_DEBUG_MSG(("JazzSSParser::readCell: unknown format=6\n")); |
| 603 |
f << "##type=" << type << ","; |
| 604 |
break; |
| 605 |
} |
| 606 |
if (type!=7 && digits!=2) |
| 607 |
f << "digits=" << digits << ","; |
| 608 |
|
| 609 |
if (val&1) |
| 610 |
f << "formula[text],"; |
| 611 |
if (val&2) |
| 612 |
f << "check[entry],"; |
| 613 |
MWAWFont font=m_state->m_font; |
| 614 |
if (val&4) { |
| 615 |
font.setFlags(font.flags()|MWAWFont::hiddenBit); |
| 616 |
f << "hide,"; |
| 617 |
} |
| 618 |
int iFormat=val&0xf9; |
| 619 |
if (iFormat!=0x80) |
| 620 |
f << "format=" << std::hex << iFormat << std::dec << ","; |
| 621 |
int cellPos[2]; |
| 622 |
for (auto &p : cellPos) p=int(input->readULong(2)); |
| 623 |
f << "C" << cellPos[0] << "R" << cellPos[1] << ","; |
| 624 |
MWAWVec2i const cPos(cellPos[0],cellPos[1]); |
| 625 |
cell.setPosition(cPos); |
| 626 |
cell.setFont(font); |
| 627 |
cell.setFormat(format); |
| 628 |
|
| 629 |
bool ok=false; |
| 630 |
long dataSz=endPos-input->tell(); |
| 631 |
switch (id) { |
| 632 |
case 12: // empty |
| 633 |
f << "empty,"; |
| 634 |
ok=dataSz==0; |
| 635 |
content.m_contentType=MWAWCellContent::C_NONE; |
| 636 |
break; |
| 637 |
case 13: { |
| 638 |
f << "int,"; |
| 639 |
if (dataSz!=2) |
| 640 |
break; |
| 641 |
val=int(input->readLong(2)); |
| 642 |
f << "val=" << val << ","; |
| 643 |
content.m_contentType=content.C_NUMBER; |
| 644 |
content.setValue(double(val)); |
| 645 |
ok=true; |
| 646 |
break; |
| 647 |
} |
| 648 |
case 14: { |
| 649 |
if (dataSz!=10) |
| 650 |
break; |
| 651 |
f << "double,"; |
| 652 |
ok=true; |
| 653 |
double res; |
| 654 |
bool isNan; |
| 655 |
content.m_contentType=content.C_NUMBER; |
| 656 |
if (!input->readDouble10(res, isNan)) { |
| 657 |
MWAW_DEBUG_MSG(("JazzSSParser::readCell: can not read a double\n")); |
| 658 |
f << "###nan,"; |
| 659 |
} |
| 660 |
else { |
| 661 |
content.setValue(res); |
| 662 |
f << "val=" << res << ","; |
| 663 |
} |
| 664 |
break; |
| 665 |
} |
| 666 |
case 15: { |
| 667 |
if (dataSz<1) |
| 668 |
break; |
| 669 |
int sz=int(input->readULong(1)); |
| 670 |
if (sz+1>dataSz) |
| 671 |
break; |
| 672 |
f << "text,"; |
| 673 |
ok=true; |
| 674 |
content.m_contentType=content.C_TEXT; |
| 675 |
content.m_textEntry.setBegin(input->tell()); |
| 676 |
content.m_textEntry.setLength(sz); |
| 677 |
std::string text; |
| 678 |
for (int i=0; i<sz; ++i) text+=char(input->readULong(1)); |
| 679 |
f << text; |
| 680 |
break; |
| 681 |
} |
| 682 |
case 16: { |
| 683 |
f << "formula,"; |
| 684 |
if (iFormat&1) { |
| 685 |
int sz=int(input->readULong(1)); |
| 686 |
if (1+sz+2>dataSz) |
| 687 |
break; |
| 688 |
content.m_contentType=content.C_TEXT; |
| 689 |
content.m_textEntry.setBegin(input->tell()); |
| 690 |
content.m_textEntry.setLength(sz); |
| 691 |
std::string res; |
| 692 |
for (int i=0; i<sz; ++i) res+=char(input->readULong(1)); |
| 693 |
f << res << ","; |
| 694 |
} |
| 695 |
else { |
| 696 |
if (dataSz<12) |
| 697 |
break; |
| 698 |
content.m_contentType=content.C_NUMBER; |
| 699 |
double res; |
| 700 |
bool isNan; |
| 701 |
if (!input->readDouble10(res, isNan)) { |
| 702 |
// can be ok if the result is nan |
| 703 |
static bool first=true; |
| 704 |
if (first) { |
| 705 |
first=false; |
| 706 |
MWAW_DEBUG_MSG(("JazzSSParser::readCell: can not read some double\n")); |
| 707 |
} |
| 708 |
f << "#nan,"; |
| 709 |
} |
| 710 |
else { |
| 711 |
content.setValue(double(res)); |
| 712 |
f << "val=" << res << ","; |
| 713 |
} |
| 714 |
input->seek(pos+16, librevenge::RVNG_SEEK_SET); |
| 715 |
} |
| 716 |
ok=true; |
| 717 |
std::string error; |
| 718 |
if (!readFormula(endPos, cPos, content.m_formula, error)) |
| 719 |
f << "###"; |
| 720 |
else |
| 721 |
content.m_contentType=content.C_FORMULA; |
| 722 |
for (auto const &instr : content.m_formula) f << instr << ","; |
| 723 |
if (!error.empty()) f << error; |
| 724 |
break; |
| 725 |
} |
| 726 |
default: |
| 727 |
break; |
| 728 |
} |
| 729 |
if (!ok) { |
| 730 |
f << "###"; |
| 731 |
} |
| 732 |
if (m_state->m_posToCells.find(cPos)!=m_state->m_posToCells.end()) { |
| 733 |
MWAW_DEBUG_MSG(("JazzSSParser::readCell: find a dupplicated cell\n")); |
| 734 |
f << "##dupplicated"; |
| 735 |
} |
| 736 |
else if (cPos[0]>=0x100 || cPos[1]>=0x2000) { |
| 737 |
MWAW_DEBUG_MSG(("JazzSSParser::readCell: the cell position seems bad\n")); |
| 738 |
f << "##dupplicated"; |
| 739 |
} |
| 740 |
else |
| 741 |
m_state->m_posToCells[cPos]=cell; |
| 742 |
if (input->tell()!=endPos) |
| 743 |
ascii().addDelimiter(input->tell(), '|'); |
| 744 |
ascii().addPos(pos-4); |
| 745 |
ascii().addNote(f.str().c_str()); |
| 746 |
|
| 747 |
return true; |
| 748 |
} |
| 749 |
|
| 750 |
bool JazzSSParser::readSheetSize(long endPos) |
| 751 |
{ |
| 752 |
auto input=getInput(); |
| 753 |
long pos = input->tell(); |
| 754 |
if (endPos-pos < 6) { |
| 755 |
MWAW_DEBUG_MSG(("JazzSSParser::readSheetSize: block is too short\n")); |
| 756 |
return false; |
| 757 |
} |
| 758 |
libmwaw::DebugStream f; |
| 759 |
f << "Entries(SheetSize):"; |
| 760 |
input->seek(2, librevenge::RVNG_SEEK_CUR); // clearly junk |
| 761 |
int dims[2]; |
| 762 |
for (auto &d : dims) d=int(input->readLong(2)); |
| 763 |
f << "dims=" << MWAWVec2i(dims[0], dims[1]) << ","; |
| 764 |
ascii().addPos(pos-4); |
| 765 |
ascii().addNote(f.str().c_str()); |
| 766 |
|
| 767 |
// empty spreadsheet |
| 768 |
if (dims[0]==-1 && dims[1]==-1) return true; |
| 769 |
if (dims[0] < 0 || dims[1] < 0) return false; |
| 770 |
|
| 771 |
m_state->m_dimensions=MWAWVec2i(dims[0], dims[1]); |
| 772 |
return true; |
| 773 |
} |
| 774 |
|
| 775 |
//////////////////////////////////////////////////////////// |
| 776 |
// formula |
| 777 |
//////////////////////////////////////////////////////////// |
| 778 |
namespace JazzSSParserInternal |
| 779 |
{ |
| 780 |
struct Functions { |
| 781 |
char const *m_name; |
| 782 |
int m_arity; |
| 783 |
}; |
| 784 |
|
| 785 |
static Functions const s_listFunctions[] = { |
| 786 |
{ "", 0} /*SPEC: number*/, {"", 0}/*SPEC: cell*/, {"", 0}/*SPEC: cells*/, {"=", 1} /*SPEC: end of formula*/, |
| 787 |
{ "(", 1} /* SPEC: () */, {"", 0}/*SPEC: number*/, { "", -2} /*SPEC: text*/, {"", -2}/*unused*/, |
| 788 |
{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, { "", -2} /*UNKN*/, |
| 789 |
{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, |
| 790 |
|
| 791 |
// 10-1f |
| 792 |
{ "-", 1}, { "+", 2}, { "-", 2}, { "*", 2}, |
| 793 |
{ "/", 2}, { "^", 2}, { "=", 2}, { "<>", 2}, |
| 794 |
{ "<=", 2}, { ">=", 2}, { "<", 2}, { ">", 2}, |
| 795 |
{ "And", 2}, { "Or", 2}, { "Not", 1}, { "+", 1}, |
| 796 |
|
| 797 |
// 20-2f |
| 798 |
{ "&", 2}, { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, { "", -2} /*UNKN*/, |
| 799 |
{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, |
| 800 |
{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, |
| 801 |
{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, |
| 802 |
|
| 803 |
// 30-3f |
| 804 |
{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, { "Repeat", 2}, |
| 805 |
{ "Replace", 4}, { "Left", 2}, { "Right", 2}, { "Lower", 1}, |
| 806 |
{ "Upper", 1}, { "Proper", 1}, { "Clean", 1}, { "", -2} /*UNKN*/, |
| 807 |
{ "Trim", 1}, { "Exact", 2}, { "CellPointer", 1}, { "IsBlank", 1}, |
| 808 |
|
| 809 |
// 40-4f |
| 810 |
{ "NA", 0}, { "Err", 0}, { "Abs", 1}, { "Int", 1}, |
| 811 |
{ "Sqrt", 1}, { "Log", 1}, { "Ln", 1}, { "Pi", 0}, |
| 812 |
{ "Sin", 1}, { "Cos", 1}, { "Tan", 1}, { "Atan2", 2}, |
| 813 |
{ "Atan", 1}, { "Asin", 1}, { "Acos", 1}, { "Exp", 1}, |
| 814 |
|
| 815 |
// 50-5f |
| 816 |
{ "Mod", 2}, { "Choose", -1}, { "IsNa", 1}, { "IsErr", 1}, |
| 817 |
{ "False", 0}, { "True", 0}, { "Rand", 0}, { "Date", 3}, |
| 818 |
{ "Now", 0}, { "PMT", 3}, { "PV", 3}, { "FV", 3}, |
| 819 |
{ "If", 3}, { "Day", 1}, { "Month", 1}, { "Year", 1}, |
| 820 |
|
| 821 |
// 60-6f |
| 822 |
{ "Round", 2}, { "Time", 3}, { "Hour", 1}, { "Minute", 1}, |
| 823 |
{ "Second", 1}, { "IsNumber", 1}, { "IsString", 1}, { "Length", 1}, |
| 824 |
{ "Value", 1}, { "Fixed", 2}, { "SubStr", 3}, { "Char", 1}, |
| 825 |
{ "Code", 1}, { "Find", 3}, { "DateValue", 1}, { "", -2} /*UNKN*/, |
| 826 |
|
| 827 |
// 70-7f |
| 828 |
{ "", -2} /*UNKN*/,{ "IsRef", 1}, { "CpySgn", 2}, { "Scale", 2}, |
| 829 |
{ "Ln1", 1}, { "Exp2", 1}, { "Exp1", 1}, { "", -2} /*UNKN*/, |
| 830 |
{ "", -2} /*UNKN*/, { "TimeValue", 1}, { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, |
| 831 |
{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, |
| 832 |
|
| 833 |
// 80-8f |
| 834 |
{ "Sum", -1}, { "Avg", -1}, { "Count", -1}, { "Min", -1}, |
| 835 |
{ "Max", -1}, { "VLookUp", 3}, { "NPV", 2}, { "Var", -1}, |
| 836 |
{ "Std", -1}, { "IRR", 2}, { "HLookUp", 3}, { "DSum", 3}, |
| 837 |
{ "DAvg", 3}, { "DCount", 3}, { "DMin", 3}, { "DMax", 3}, |
| 838 |
|
| 839 |
// 90-9f |
| 840 |
{ "DVar", 3}, { "DStd", 3}, { "Index", 3}, { "Cols", 1}, |
| 841 |
{ "Rows", 1}, { "N", 1}, { "S", 1}, { "", -2} /*UNKN*/, |
| 842 |
{ "Cell", 2}, { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, |
| 843 |
{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2}, |
| 844 |
|
| 845 |
// a0-af |
| 846 |
{ "FCount", 1}, { "FSum", 1}, { "FAVG", 1}, { "FMin", 1}, |
| 847 |
{ "FMax", 1}, { "FStd", 1}, { "FVar", 1}, { "FPage", 0}, |
| 848 |
{ "FPrev", 0}, { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2}, |
| 849 |
{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2}, |
| 850 |
}; |
| 851 |
} |
| 852 |
|
| 853 |
bool JazzSSParser::readCell(MWAWVec2i actPos, MWAWCellContent::FormulaInstruction &instr) |
| 854 |
{ |
| 855 |
instr=MWAWCellContent::FormulaInstruction(); |
| 856 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Cell; |
| 857 |
int pos[2]; |
| 858 |
bool absolute[2] = { true, true}; |
| 859 |
auto input=getInput(); |
| 860 |
for (int dim = 0; dim < 2; dim++) { |
| 861 |
auto val = int(input->readULong(2)); |
| 862 |
if ((val & 0x8000) == 0); // absolue value ? |
| 863 |
else { |
| 864 |
// relative |
| 865 |
int const maxVal=0x2000; |
| 866 |
val &= (2*maxVal-1); |
| 867 |
if (val & maxVal) val = val - 2*maxVal; |
| 868 |
if (val+actPos[dim]>=maxVal) val-=maxVal; |
| 869 |
val += actPos[dim]; |
| 870 |
absolute[dim] = false; |
| 871 |
} |
| 872 |
pos[dim] = val; |
| 873 |
} |
| 874 |
if (pos[0] < 0 || pos[1] < 0) { |
| 875 |
MWAW_DEBUG_MSG(("JazzSSParser::readCell: can not read cell position\n")); |
| 876 |
return false; |
| 877 |
} |
| 878 |
if (pos[0]>=0x100) pos[0]%=0x100; |
| 879 |
instr.m_position[0]=MWAWVec2i(pos[0],pos[1]); |
| 880 |
instr.m_positionRelative[0]=MWAWVec2b(!absolute[0],!absolute[1]); |
| 881 |
return true; |
| 882 |
} |
| 883 |
|
| 884 |
bool JazzSSParser::readFormula(long endPos, MWAWVec2i const &position, |
| 885 |
std::vector<MWAWCellContent::FormulaInstruction> &formula, std::string &error) |
| 886 |
{ |
| 887 |
formula.resize(0); |
| 888 |
error = ""; |
| 889 |
auto input=getInput(); |
| 890 |
long pos = input->tell(); |
| 891 |
if (endPos - pos < 2) return false; |
| 892 |
auto sz = int(input->readULong(2)); |
| 893 |
if (endPos-pos-2 != sz || !input->checkPosition(endPos)) return false; |
| 894 |
|
| 895 |
std::stringstream f; |
| 896 |
std::vector<std::vector<MWAWCellContent::FormulaInstruction> > stack; |
| 897 |
bool ok = true; |
| 898 |
while (long(input->tell()) != endPos) { |
| 899 |
pos = input->tell(); |
| 900 |
if (pos > endPos) return false; |
| 901 |
auto wh = int(input->readULong(1)); |
| 902 |
int arity = 0; |
| 903 |
MWAWCellContent::FormulaInstruction instr; |
| 904 |
switch (wh) { |
| 905 |
case 0x0: { |
| 906 |
double val; |
| 907 |
bool isNaN; |
| 908 |
if (endPos-pos<10 || !input->readDouble10(val, isNaN)) { |
| 909 |
f.str(""); |
| 910 |
f << "###number"; |
| 911 |
error=f.str(); |
| 912 |
ok = false; |
| 913 |
break; |
| 914 |
} |
| 915 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Double; |
| 916 |
instr.m_doubleValue=val; |
| 917 |
break; |
| 918 |
} |
| 919 |
case 0x1: { |
| 920 |
if (endPos-pos<7) { |
| 921 |
f.str(""); |
| 922 |
f << "###cell short"; |
| 923 |
error=f.str(); |
| 924 |
ok = false; |
| 925 |
break; |
| 926 |
} |
| 927 |
ok = readCell(position, instr); |
| 928 |
int val=int(input->readULong(2)); |
| 929 |
if (val) { |
| 930 |
MWAW_DEBUG_MSG(("JazzSSParser::readFormula: oops find a sheet val=%d\n", val)); |
| 931 |
f << "##sheet=" << val << ","; |
| 932 |
} |
| 933 |
break; |
| 934 |
} |
| 935 |
case 0x2: { |
| 936 |
if (endPos-pos< 1+10 || !readCell(position, instr)) { |
| 937 |
f.str(""); |
| 938 |
f << "###list cell short"; |
| 939 |
error=f.str(); |
| 940 |
ok = false; |
| 941 |
break; |
| 942 |
} |
| 943 |
MWAWCellContent::FormulaInstruction instr2; |
| 944 |
if (!readCell(position, instr2)) { |
| 945 |
ok = false; |
| 946 |
f.str(""); |
| 947 |
f << "###list cell short(2)"; |
| 948 |
error=f.str(); |
| 949 |
break; |
| 950 |
} |
| 951 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_CellList; |
| 952 |
instr.m_position[1]=instr2.m_position[0]; |
| 953 |
instr.m_positionRelative[1]=instr2.m_positionRelative[0]; |
| 954 |
int val=int(input->readULong(2)); |
| 955 |
if (val) { |
| 956 |
MWAW_DEBUG_MSG(("JazzSSParser::readFormula: oops find a sheet val=%d\n", val)); |
| 957 |
f << "##sheet=" << val << ","; |
| 958 |
} |
| 959 |
break; |
| 960 |
} |
| 961 |
case 0x5: |
| 962 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Long; |
| 963 |
instr.m_longValue=double(input->readLong(2)); |
| 964 |
break; |
| 965 |
case 0x6: { |
| 966 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Text; |
| 967 |
int sSz=int(input->readULong(1)); |
| 968 |
if (input->tell()+sSz > endPos) { |
| 969 |
ok=false; |
| 970 |
break; |
| 971 |
} |
| 972 |
for (int i=0; i<sSz; ++i) { |
| 973 |
auto c = char(input->readULong(1)); |
| 974 |
if (c==0) break; |
| 975 |
instr.m_content += c; |
| 976 |
} |
| 977 |
break; |
| 978 |
} |
| 979 |
default: |
| 980 |
if (wh >= 0xb0 || JazzSSParserInternal::s_listFunctions[wh].m_arity == -2) { |
| 981 |
f.str(""); |
| 982 |
f << "##Funct" << std::hex << wh; |
| 983 |
error=f.str(); |
| 984 |
ok = false; |
| 985 |
break; |
| 986 |
} |
| 987 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Function; |
| 988 |
instr.m_content=JazzSSParserInternal::s_listFunctions[wh].m_name; |
| 989 |
ok=!instr.m_content.empty(); |
| 990 |
arity = JazzSSParserInternal::s_listFunctions[wh].m_arity; |
| 991 |
if (arity == -1) arity = int(input->readLong(1)); |
| 992 |
break; |
| 993 |
} |
| 994 |
|
| 995 |
if (!ok) break; |
| 996 |
std::vector<MWAWCellContent::FormulaInstruction> child; |
| 997 |
if (instr.m_type!=MWAWCellContent::FormulaInstruction::F_Function) { |
| 998 |
child.push_back(instr); |
| 999 |
stack.push_back(child); |
| 1000 |
continue; |
| 1001 |
} |
| 1002 |
size_t numElt = stack.size(); |
| 1003 |
if (int(numElt) < arity) { |
| 1004 |
f.str(""); |
| 1005 |
f << instr.m_content << "[##" << arity << "]"; |
| 1006 |
error=f.str(); |
| 1007 |
ok = false; |
| 1008 |
break; |
| 1009 |
} |
| 1010 |
if ((instr.m_content[0] >= 'A' && instr.m_content[0] <= 'Z') || instr.m_content[0] == '(') { |
| 1011 |
if (instr.m_content[0] != '(') |
| 1012 |
child.push_back(instr); |
| 1013 |
|
| 1014 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator; |
| 1015 |
instr.m_content="("; |
| 1016 |
child.push_back(instr); |
| 1017 |
for (int i = 0; i < arity; i++) { |
| 1018 |
if (i) { |
| 1019 |
instr.m_content=";"; |
| 1020 |
child.push_back(instr); |
| 1021 |
} |
| 1022 |
auto const &node=stack[size_t(int(numElt)-arity+i)]; |
| 1023 |
child.insert(child.end(), node.begin(), node.end()); |
| 1024 |
} |
| 1025 |
instr.m_content=")"; |
| 1026 |
child.push_back(instr); |
| 1027 |
|
| 1028 |
stack.resize(size_t(int(numElt)-arity+1)); |
| 1029 |
stack[size_t(int(numElt)-arity)] = child; |
| 1030 |
continue; |
| 1031 |
} |
| 1032 |
if (arity==1) { |
| 1033 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator; |
| 1034 |
stack[numElt-1].insert(stack[numElt-1].begin(), instr); |
| 1035 |
if (wh==3) |
| 1036 |
break; |
| 1037 |
continue; |
| 1038 |
} |
| 1039 |
if (arity==2) { |
| 1040 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator; |
| 1041 |
stack[numElt-2].push_back(instr); |
| 1042 |
stack[numElt-2].insert(stack[numElt-2].end(), stack[numElt-1].begin(), stack[numElt-1].end()); |
| 1043 |
stack.resize(numElt-1); |
| 1044 |
continue; |
| 1045 |
} |
| 1046 |
ok=false; |
| 1047 |
error = "### unexpected arity"; |
| 1048 |
break; |
| 1049 |
} |
| 1050 |
|
| 1051 |
if (!ok) ; |
| 1052 |
else if (stack.size()==1 && stack[0].size()>1 && stack[0][0].m_content=="=") { |
| 1053 |
formula.insert(formula.begin(),stack[0].begin()+1,stack[0].end()); |
| 1054 |
if (input->tell()!=endPos) { |
| 1055 |
MWAW_DEBUG_MSG(("JazzSSParser::readFormula: find some extra data\n")); |
| 1056 |
error="##extra data"; |
| 1057 |
ascii().addDelimiter(input->tell(),'#'); |
| 1058 |
} |
| 1059 |
return true; |
| 1060 |
} |
| 1061 |
else |
| 1062 |
error = "###stack problem"; |
| 1063 |
|
| 1064 |
static bool first = true; |
| 1065 |
if (first) { |
| 1066 |
MWAW_DEBUG_MSG(("JazzSSParser::readFormula: I can not read some formula\n")); |
| 1067 |
first = false; |
| 1068 |
} |
| 1069 |
|
| 1070 |
f.str(""); |
| 1071 |
for (auto const &i : stack) { |
| 1072 |
for (auto const &j : i) |
| 1073 |
f << j << ","; |
| 1074 |
} |
| 1075 |
f << error << "###"; |
| 1076 |
error = f.str(); |
| 1077 |
return false; |
| 1078 |
} |
| 1079 |
|
| 1080 |
//////////////////////////////////////////////////////////// |
| 1081 |
// read the header |
| 1082 |
//////////////////////////////////////////////////////////// |
| 1083 |
bool JazzSSParser::checkHeader(MWAWHeader *header, bool strict) |
| 1084 |
{ |
| 1085 |
*m_state = JazzSSParserInternal::State(); |
| 1086 |
|
| 1087 |
MWAWInputStreamPtr input = getInput(); |
| 1088 |
if (!input || !input->hasDataFork()) |
| 1089 |
return false; |
| 1090 |
|
| 1091 |
if (!input->checkPosition(46+0x100)) { |
| 1092 |
MWAW_DEBUG_MSG(("JazzSSParser::checkHeader: file is too short\n")); |
| 1093 |
return false; |
| 1094 |
} |
| 1095 |
input->seek(0,librevenge::RVNG_SEEK_SET); |
| 1096 |
if (input->readULong(2)!=0 || input->readULong(2)!=2) |
| 1097 |
return false; |
| 1098 |
libmwaw::DebugStream f; |
| 1099 |
f << "FileHeader:"; |
| 1100 |
int val=int(input->readLong(2)); |
| 1101 |
if (val!=0xb) |
| 1102 |
f << "vers=" << val << ","; |
| 1103 |
ascii().addPos(0); |
| 1104 |
ascii().addNote(f.str().c_str()); |
| 1105 |
if (strict) { |
| 1106 |
bool ok=false; |
| 1107 |
for (int i=0; i<20; ++i) { |
| 1108 |
long pos=input->tell(); |
| 1109 |
if (!input->checkPosition(pos+4)) { |
| 1110 |
MWAW_DEBUG_MSG(("JazzSSParser::checkHeader: file is too short\n")); |
| 1111 |
return false; |
| 1112 |
} |
| 1113 |
int id=int(input->readULong(2)); |
| 1114 |
int len=int(input->readULong(2)); |
| 1115 |
if (!input->checkPosition(pos+4+len)) |
| 1116 |
return false; |
| 1117 |
if ((id==6 && len==6) || (id==0x11 && (len==0x1c||len==0x1e)) || (id==0x15 && (len==0x126||len==0x16c))) { |
| 1118 |
ok=true; |
| 1119 |
break; |
| 1120 |
} |
| 1121 |
input->seek(pos+4+len, librevenge::RVNG_SEEK_SET); |
| 1122 |
} |
| 1123 |
if (!ok) { |
| 1124 |
MWAW_DEBUG_MSG(("JazzSSParser::checkHeader: can not find any expected zone\n")); |
| 1125 |
return false; |
| 1126 |
} |
| 1127 |
} |
| 1128 |
if (header) |
| 1129 |
header->reset(MWAWDocument::MWAW_T_JAZZLOTUS, 1, MWAWDocument::MWAW_K_SPREADSHEET); |
| 1130 |
return true; |
| 1131 |
} |
| 1132 |
|
| 1133 |
//////////////////////////////////////////////////////////// |
| 1134 |
// send spreadsheet |
| 1135 |
//////////////////////////////////////////////////////////// |
| 1136 |
bool JazzSSParser::sendSpreadsheet() |
| 1137 |
{ |
| 1138 |
MWAWSpreadsheetListenerPtr listener=getSpreadsheetListener(); |
| 1139 |
MWAWInputStreamPtr &input= getInput(); |
| 1140 |
if (!listener) { |
| 1141 |
MWAW_DEBUG_MSG(("JazzSSParser::sendSpreadsheet: I can not find the listener\n")); |
| 1142 |
return false; |
| 1143 |
} |
| 1144 |
listener->openSheet(m_state->m_widths, librevenge::RVNG_POINT); |
| 1145 |
|
| 1146 |
int prevRow = -1; |
| 1147 |
for (auto const &it : m_state->m_posToCells) { |
| 1148 |
auto const &cell=it.second; |
| 1149 |
if (cell.position()[1]>prevRow) { |
| 1150 |
if (prevRow != -1) listener->closeSheetRow(); |
| 1151 |
int numRepeat=cell.position()[1]-1-prevRow; |
| 1152 |
if (numRepeat) { |
| 1153 |
listener->openSheetRow(0, librevenge::RVNG_POINT, numRepeat); |
| 1154 |
listener->closeSheetRow(); |
| 1155 |
} |
| 1156 |
listener->openSheetRow(0, librevenge::RVNG_POINT); |
| 1157 |
prevRow=cell.position()[1]; |
| 1158 |
} |
| 1159 |
listener->openSheetCell(cell, cell.m_content); |
| 1160 |
if (cell.m_content.m_textEntry.valid()) { |
| 1161 |
listener->setFont(cell.isFontSet() ? cell.getFont() : m_state->m_font); |
| 1162 |
input->seek(cell.m_content.m_textEntry.begin(), librevenge::RVNG_SEEK_SET); |
| 1163 |
while (!input->isEnd() && input->tell()<cell.m_content.m_textEntry.end()) { |
| 1164 |
auto c=static_cast<unsigned char>(input->readULong(1)); |
| 1165 |
if (c==0xd) |
| 1166 |
listener->insertEOL(); |
| 1167 |
else |
| 1168 |
listener->insertCharacter(c); |
| 1169 |
} |
| 1170 |
} |
| 1171 |
listener->closeSheetCell(); |
| 1172 |
} |
| 1173 |
if (prevRow!=-1) listener->closeSheetRow(); |
| 1174 |
listener->closeSheet(); |
| 1175 |
return true; |
| 1176 |
} |
| 1177 |
|
| 1178 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |