| 1 |
/* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */ |
| 2 |
|
| 3 |
/* libmwaw |
| 4 |
* Version: MPL 2.0 / LGPLv2+ |
| 5 |
* |
| 6 |
* The contents of this file are subject to the Mozilla Public License Version |
| 7 |
* 2.0 (the "License"); you may not use this file except in compliance with |
| 8 |
* the License or as specified alternatively below. You may obtain a copy of |
| 9 |
* the License at http://www.mozilla.org/MPL/ |
| 10 |
* |
| 11 |
* Software distributed under the License is distributed on an "AS IS" basis, |
| 12 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 |
* for the specific language governing rights and limitations under the |
| 14 |
* License. |
| 15 |
* |
| 16 |
* Major Contributor(s): |
| 17 |
* Copyright (C) 2002 William Lachance ([email protected]) |
| 18 |
* Copyright (C) 2002,2004 Marc Maurer ([email protected]) |
| 19 |
* Copyright (C) 2004-2006 Fridrich Strba ([email protected]) |
| 20 |
* Copyright (C) 2006, 2007 Andrew Ziem |
| 21 |
* Copyright (C) 2011, 2012 Alonso Laurent ([email protected]) |
| 22 |
* |
| 23 |
* |
| 24 |
* All Rights Reserved. |
| 25 |
* |
| 26 |
* For minor contributions see the git repository. |
| 27 |
* |
| 28 |
* Alternatively, the contents of this file may be used under the terms of |
| 29 |
* the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"), |
| 30 |
* in which case the provisions of the LGPLv2+ are applicable |
| 31 |
* instead of those above. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include <cmath> |
| 35 |
#include <iomanip> |
| 36 |
#include <iostream> |
| 37 |
#include <limits> |
| 38 |
#include <map> |
| 39 |
#include <set> |
| 40 |
#include <sstream> |
| 41 |
#include <stack> |
| 42 |
|
| 43 |
#include <librevenge/librevenge.h> |
| 44 |
|
| 45 |
#include "MWAWFont.hxx" |
| 46 |
#include "MWAWFontConverter.hxx" |
| 47 |
#include "MWAWListener.hxx" |
| 48 |
#include "MWAWParagraph.hxx" |
| 49 |
#include "MWAWPosition.hxx" |
| 50 |
#include "MWAWSubDocument.hxx" |
| 51 |
|
| 52 |
#include "RagTime5ClusterManager.hxx" |
| 53 |
#include "RagTime5Document.hxx" |
| 54 |
#include "RagTime5Spreadsheet.hxx" |
| 55 |
#include "RagTime5StructManager.hxx" |
| 56 |
#include "RagTime5StyleManager.hxx" |
| 57 |
|
| 58 |
#include "RagTime5Formula.hxx" |
| 59 |
|
| 60 |
#include "libmwaw_internal.hxx" |
| 61 |
|
| 62 |
/** Internal: the structures of a RagTime5Formula */ |
| 63 |
namespace RagTime5FormulaInternal |
| 64 |
{ |
| 65 |
//////////////////////////////////////// |
| 66 |
//! Internal: the state of a RagTime5Formula |
| 67 |
struct State { |
| 68 |
//! constructor |
| 69 |
State() |
| 70 |
: m_idFunctionMap() |
| 71 |
, m_idFunctionSet() |
| 72 |
, m_documentFunctionNames() |
| 73 |
, m_idFormulaMap() |
| 74 |
{ |
| 75 |
m_idFunctionMap= { |
| 76 |
// layout |
| 77 |
{0x14c2017, "Page"}, |
| 78 |
{0x14c2027, "PageIndex"}, |
| 79 |
{0x14c2037, "NoOfPages"}, |
| 80 |
{0x14c2047, "Container"}, |
| 81 |
{0x14c2057, "StartingPageNumber"}, |
| 82 |
{0x14c2067, "EndingPageNumber"}, |
| 83 |
// standart |
| 84 |
{0x1559817, "Abs"}, |
| 85 |
{0x1559827, "Sign"}, |
| 86 |
{0x1559837, "Sqrt"}, |
| 87 |
{0x1559847, "SumSqr"}, // Sqr |
| 88 |
{0x1559857, "Int"}, |
| 89 |
{0x1559867, "Round"}, |
| 90 |
{0x1559877, "Floor"}, |
| 91 |
{0x1559887, "Ceiling"}, |
| 92 |
{0x1559897, "Trunc"}, |
| 93 |
{0x15598a7, "Max"}, |
| 94 |
{0x15598b7, "Min"}, |
| 95 |
{0x15598c7, "IsNumber"}, |
| 96 |
{0x15598d7, "IsBlank"}, |
| 97 |
{0x15598e7, "Type"}, |
| 98 |
{0x15598f7, "ErrorType"}, |
| 99 |
{0x1559907, "IsErr"}, |
| 100 |
{0x1559917, "IsNA"}, |
| 101 |
{0x1559927, "Error"}, |
| 102 |
{0x1559937, "If"}, |
| 103 |
{0x1559947, "True"}, |
| 104 |
{0x1559957, "False"}, |
| 105 |
{0x1559967, "Pi"}, |
| 106 |
{0x1559977, "Pi180"}, |
| 107 |
{0x1559987, "Rand"}, |
| 108 |
{0x1559997, "And"}, |
| 109 |
{0x15599a7, "Or"}, |
| 110 |
{0x15599c7, "IsOdd"}, |
| 111 |
{0x15599d7, "IsEven"}, |
| 112 |
{0x15599e7, "Sin"}, |
| 113 |
{0x15599f7, "Cos"}, |
| 114 |
{0x1559a07, "Tan"}, |
| 115 |
{0x1559a17, "Mod"}, |
| 116 |
{0x1559a27, "SpecialIf"}, |
| 117 |
{0x1559a37, "NA"}, |
| 118 |
{0x1559a47, "Frac"}, |
| 119 |
{0x1559a87, "ASin"}, // ArcSin |
| 120 |
{0x1559a97, "ACos"}, // ArcCos |
| 121 |
{0x1559aa7, "ATan"}, // ArcTan |
| 122 |
{0x1559b07, "Degrees"}, |
| 123 |
{0x1559b17, "Radians"}, |
| 124 |
{0x1559b27, "Exp"}, |
| 125 |
{0x1559b37, "Ln"}, |
| 126 |
{0x1559b47, "Log2"}, |
| 127 |
{0x1559b57, "Log10"}, |
| 128 |
{0x1559b67, "Log"}, |
| 129 |
{0x1559b77, "Exp1"}, |
| 130 |
{0x1559b87, "Ln1"}, |
| 131 |
{0x1559bc7, "Sum"}, |
| 132 |
{0x1559bd7, "SumSqr"}, // SqrSum |
| 133 |
{0x1559be7, "SumProduct"}, |
| 134 |
{0x1559bf7, "SumXPY2"}, |
| 135 |
{0x1559c07, "SumXMY2"}, |
| 136 |
{0x1559c17, "SumX2PY2"}, |
| 137 |
{0x1559c27, "SumX2MY2"}, |
| 138 |
{0x1559c37, "Count"}, |
| 139 |
{0x1559c47, "Average"}, |
| 140 |
{0x1559c57, "StDev"}, |
| 141 |
{0x1559c67, "Var"}, |
| 142 |
{0x1559c77, "RegressionB"}, |
| 143 |
{0x1559c87, "RegressionM"}, |
| 144 |
{0x1559c97, "LogRegressionB"}, |
| 145 |
{0x1559ca7, "LogRegressionM"}, |
| 146 |
{0x1559d07, "Annuity"}, |
| 147 |
{0x1559d17, "Compound"}, |
| 148 |
{0x1559d27, "FV"}, |
| 149 |
{0x1559d37, "NPV"}, |
| 150 |
{0x1559d47, "Factorial"}, |
| 151 |
{0x1559d57, "Combinations"}, |
| 152 |
{0x1559d67, "Permutations"}, |
| 153 |
{0x1559dc7, "SetHour"}, |
| 154 |
{0x1559dd7, "SetMinute"}, |
| 155 |
{0x1559de7, "SetSecond"}, |
| 156 |
{0x1559df7, "AddSecond"}, |
| 157 |
{0x1559e07, "AddMinute"}, |
| 158 |
{0x1559e17, "AddHour"}, |
| 159 |
{0x1559e27, "AddDay"}, |
| 160 |
{0x1559e37, "Date"}, // ragtime setdate |
| 161 |
{0x1559e47, "AddMonth"}, |
| 162 |
{0x1559e57, "AddYear"}, |
| 163 |
{0x1559e67, "Second"}, |
| 164 |
{0x1559e77, "Month"}, |
| 165 |
{0x1559e87, "Hour"}, |
| 166 |
{0x1559e97, "DiffSecond"}, |
| 167 |
{0x1559ea7, "DiffMinute"}, |
| 168 |
{0x1559eb7, "DiffHour"}, |
| 169 |
{0x1559ec7, "DiffDay"}, |
| 170 |
{0x1559ed7, "DiffDays30"}, |
| 171 |
{0x1559ee7, "DiffMonth"}, |
| 172 |
{0x1559ef7, "DiffYear"}, |
| 173 |
{0x1559f07, "DayOfWeekISO"}, |
| 174 |
{0x1559f17, "DayOfWeekUS"}, |
| 175 |
{0x1559f27, "WeekOfYearISO"}, |
| 176 |
{0x1559f37, "WeekOfYearUS"}, |
| 177 |
{0x1559f47, "Now"}, |
| 178 |
{0x1559f57, "Today"}, |
| 179 |
{0x1559f67, "SetDay"}, |
| 180 |
{0x1559f77, "SetMonth"}, |
| 181 |
{0x1559f87, "SetYear"}, |
| 182 |
{0x1559f97, "DayOfYear"}, |
| 183 |
{0x1559fa7, "Second"}, // SecondOf |
| 184 |
{0x1559fb7, "Minute"}, // MinuteOf |
| 185 |
{0x1559fc7, "Hour"}, // HourOf |
| 186 |
{0x1559fd7, "Day"}, // DayOf |
| 187 |
{0x1559fe7, "Month"}, // ragtime monthof |
| 188 |
{0x1559ff7, "YearOf"}, // YearOf |
| 189 |
{0x155a017, "Length"}, |
| 190 |
{0x155a027, "Left"}, |
| 191 |
{0x155a037, "Right"}, |
| 192 |
{0x155a047, "Mid"}, |
| 193 |
{0x155a057, "Replace"}, |
| 194 |
{0x155a067, "Repeat"}, |
| 195 |
{0x155a077, "Concatenate"}, |
| 196 |
{0x155a087, "Concatenate"}, // Smart concatenate |
| 197 |
{0x155a097, "Exact"}, |
| 198 |
{0x155a0a7, "Code"}, |
| 199 |
{0x155a0b7, "Code"}, // WinCode |
| 200 |
{0x155a0c7, "UniCode"}, |
| 201 |
{0x155a0d7, "Char"}, |
| 202 |
{0x155a0e7, "Char"}, // WinCharx |
| 203 |
{0x155a0f7, "UniChar"}, |
| 204 |
{0x155a107, "Clean"}, |
| 205 |
{0x155a117, "Trim"}, |
| 206 |
{0x155a127, "Lower"}, |
| 207 |
{0x155a137, "Upper"}, |
| 208 |
{0x155a147, "Proper"}, |
| 209 |
{0x155a157, "Small"}, |
| 210 |
{0x155a167, "Large"}, |
| 211 |
{0x155a177, "Median"}, |
| 212 |
{0x155a187, "Percentile"}, |
| 213 |
{0x155a197, "Quartile"}, |
| 214 |
{0x155a1a7, "Choose"}, |
| 215 |
{0x155a1b7, "Find"}, |
| 216 |
{0x155a1c7, "Text"}, |
| 217 |
{0x155a1d7, "ValueFormat"}, |
| 218 |
{0x155a1e7, "Value"}, |
| 219 |
{0x155a1f7, "SetDocName"}, |
| 220 |
{0x155a207, "DocumentDate"}, |
| 221 |
{0x155a217, "DocumentName"}, |
| 222 |
{0x155a227, "Date"}, |
| 223 |
{0x155a237, "Number"}, |
| 224 |
{0x155a247, "TimeSpan"}, |
| 225 |
{0x155a257, "SystemCurrency"}, |
| 226 |
{0x155a287, "SetTime"}, |
| 227 |
{0x155a297, "SetTimeSpan"}, |
| 228 |
{0x155a2a7, "Developers"}, |
| 229 |
// spreadsheet |
| 230 |
{0x1663817, "Row"}, |
| 231 |
{0x1663827, "Column"}, |
| 232 |
{0x1663837, "Plane"}, |
| 233 |
{0x16638a7, "Search"}, |
| 234 |
{0x16638b7, "HSearch"}, |
| 235 |
{0x16638c7, "VSearch"}, |
| 236 |
{0x16638d7, "LookUp"}, |
| 237 |
{0x1663907, "Index"}, |
| 238 |
{0x1663917, "Selection"}, |
| 239 |
{0x1663947, "CurrentResult"}, |
| 240 |
{0x1663967, "CurrentIndex"}, |
| 241 |
{0x1663977, "CurrentCell"}, |
| 242 |
{0x1663987, "ColumnValue"}, |
| 243 |
{0x1663997, "RowValue"}, |
| 244 |
{0x16639e7, "SetCell"}, |
| 245 |
{0x16639f7, "MailMerge"}, |
| 246 |
{0x1663a07, "PrintCycle"}, |
| 247 |
{0x1663a17, "PrintStop"}, |
| 248 |
// fax |
| 249 |
{0x1be5027, "FaxAddress"}, |
| 250 |
{0x1be5037, "FaxAddressRange"}, |
| 251 |
// button |
| 252 |
{0x1d50817, "Button"}, |
| 253 |
// slide time |
| 254 |
{0x1e16827, "STStart"}, |
| 255 |
{0x1e16837, "STStop"}, |
| 256 |
{0x1e16847, "STNextPage"}, |
| 257 |
{0x1e16857, "STShownPage"}, |
| 258 |
{0x1e16867, "STRequestedPage"}, |
| 259 |
{0x1e16877, "STLayout"}, |
| 260 |
{0x1e16887, "STStartTime"}, |
| 261 |
{0x1e16897, "STLastChange"}, |
| 262 |
{0x1e168a7, "STUpdate"}, |
| 263 |
{0x1e168b7, "STSlideCount"}, |
| 264 |
{0x1e168c7, "STPreparePage"}, |
| 265 |
{0x1e168d7, "STPreparePage"}, |
| 266 |
{0x1e168e7, "STSetPreviousPage"}, |
| 267 |
// calendar |
| 268 |
{0x23aa067, "ClJulianDate"}, |
| 269 |
{0x23aa077, "ClModJulian"}, |
| 270 |
{0x23aa087, "ClNumberToDate"}, |
| 271 |
{0x23aa097, "ClDateToNumber"}, |
| 272 |
{0x23aa0d7, "ClAddWorkDaysUSA"}, |
| 273 |
// serial number |
| 274 |
{0x23af017, "SnGetSerNum"}, |
| 275 |
{0x23af027, "SnFillSerNum"}, |
| 276 |
// euro |
| 277 |
{0x23b4017, "Euro"}, |
| 278 |
{0x23b4027, "EuroRound"}, |
| 279 |
{0x23b4037, "EuroCoinRound"}, |
| 280 |
}; |
| 281 |
for (auto const &it : m_idFunctionMap) |
| 282 |
m_idFunctionSet.insert(it.first); |
| 283 |
} |
| 284 |
//! map id to function name |
| 285 |
std::map<unsigned long, char const *> m_idFunctionMap; |
| 286 |
//! the set of function id |
| 287 |
std::set<unsigned long> m_idFunctionSet; |
| 288 |
//! the document fonction names |
| 289 |
std::vector<std::string> m_documentFunctionNames; |
| 290 |
|
| 291 |
//! map data id to text zone |
| 292 |
std::map<int, std::shared_ptr<ClusterFormula> > m_idFormulaMap; |
| 293 |
}; |
| 294 |
|
| 295 |
//! Internal: the helper to read function name |
| 296 |
struct FunctionNameParser final : public RagTime5StructManager::FieldParser { |
| 297 |
//! constructor |
| 298 |
explicit FunctionNameParser(std::map<unsigned long, char const *> const &idFunctionMap, std::vector<std::string> &functionNames) |
| 299 |
: RagTime5StructManager::FieldParser("FunctionName") |
| 300 |
, m_idFunctionMap(idFunctionMap) |
| 301 |
, m_functionNames(functionNames) |
| 302 |
{ |
| 303 |
} |
| 304 |
//! destructor |
| 305 |
~FunctionNameParser() final; |
| 306 |
//! parse a field |
| 307 |
bool parseField(RagTime5StructManager::Field &field, RagTime5Zone &/*zone*/, int n, libmwaw::DebugStream &f) final |
| 308 |
{ |
| 309 |
bool ok=false; |
| 310 |
if (m_functionNames.size()<=size_t(n)) |
| 311 |
m_functionNames.resize(size_t(n+1)); |
| 312 |
if (field.m_type==RagTime5StructManager::Field::T_FieldList && |
| 313 |
field.m_fieldList.size()==1) { |
| 314 |
auto const &child=field.m_fieldList[0]; |
| 315 |
if (child.m_type==RagTime5StructManager::Field::T_FieldList && |
| 316 |
child.m_name=="func[name]" && child.m_fieldList.size()==2) { |
| 317 |
ok=true; |
| 318 |
|
| 319 |
auto const &it=m_idFunctionMap.find(child.m_fileType); |
| 320 |
if (it!=m_idFunctionMap.end()) { |
| 321 |
m_functionNames[size_t(n)]=it->second; |
| 322 |
f << it->second << ","; |
| 323 |
} |
| 324 |
for (auto const &c : child.m_fieldList) { |
| 325 |
if (c.m_type==RagTime5StructManager::Field::T_Unicode) |
| 326 |
f << c.m_string.cstr() << ","; |
| 327 |
else |
| 328 |
f << "[" << c << "]"; |
| 329 |
} |
| 330 |
f << child.m_extra; |
| 331 |
} |
| 332 |
} |
| 333 |
if (!ok) { |
| 334 |
MWAW_DEBUG_MSG(("RagTime5FormulaInternal::FunctionNameParser::parseField: find unexpected field\n")); |
| 335 |
f << "###" << field; |
| 336 |
} |
| 337 |
return true; |
| 338 |
} |
| 339 |
|
| 340 |
protected: |
| 341 |
//! map id to function name |
| 342 |
std::map<unsigned long, char const *> const &m_idFunctionMap; |
| 343 |
//! the fonction names |
| 344 |
std::vector<std::string> &m_functionNames; |
| 345 |
}; |
| 346 |
|
| 347 |
FunctionNameParser::~FunctionNameParser() |
| 348 |
{ |
| 349 |
} |
| 350 |
|
| 351 |
} |
| 352 |
|
| 353 |
//////////////////////////////////////////////////////////// |
| 354 |
// constructor/destructor, ... |
| 355 |
//////////////////////////////////////////////////////////// |
| 356 |
RagTime5Formula::RagTime5Formula(RagTime5Document &doc) |
| 357 |
: m_document(doc) |
| 358 |
, m_structManager(m_document.getStructManager()) |
| 359 |
, m_parserState(doc.getParserState()) |
| 360 |
, m_state(new RagTime5FormulaInternal::State) |
| 361 |
{ |
| 362 |
} |
| 363 |
|
| 364 |
RagTime5Formula::~RagTime5Formula() |
| 365 |
{ |
| 366 |
} |
| 367 |
|
| 368 |
int RagTime5Formula::version() const |
| 369 |
{ |
| 370 |
return m_parserState->m_version; |
| 371 |
} |
| 372 |
|
| 373 |
std::set<unsigned long> const &RagTime5Formula::getFunctionsId() const |
| 374 |
{ |
| 375 |
return m_state->m_idFunctionSet; |
| 376 |
} |
| 377 |
|
| 378 |
bool RagTime5Formula::readFunctionNames(RagTime5ClusterManager::Link const &link) |
| 379 |
{ |
| 380 |
if (link.empty()) |
| 381 |
return true; |
| 382 |
RagTime5FormulaInternal::FunctionNameParser defaultParser(m_state->m_idFunctionMap, m_state->m_documentFunctionNames); |
| 383 |
return m_document.readStructZone(link, defaultParser, 0); |
| 384 |
} |
| 385 |
|
| 386 |
bool RagTime5Formula::readFormulaClusters(RagTime5ClusterManager::Link const &link, int sheetId) |
| 387 |
{ |
| 388 |
if (link.m_ids.size()!=2) { |
| 389 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormulaClusters: call with bad ids\n")); |
| 390 |
return false; |
| 391 |
} |
| 392 |
for (size_t i=0; i<2; ++i) { // formuladef and formulapos |
| 393 |
if (!link.m_ids[i]) continue; |
| 394 |
auto data=m_document.getDataZone(link.m_ids[i]); |
| 395 |
if (!data || data->m_isParsed || data->getKindLastPart(data->m_kinds[1].empty())!="Cluster") { |
| 396 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormulaClusters: the child cluster id %d seems bad\n", link.m_ids[i])); |
| 397 |
continue; |
| 398 |
} |
| 399 |
int zoneType=0x20000+int(i); |
| 400 |
std::shared_ptr<RagTime5ClusterManager::Cluster> cluster; |
| 401 |
if (!m_document.getClusterManager()->readCluster(*data, cluster, zoneType) || !cluster) |
| 402 |
continue; |
| 403 |
m_document.checkClusterList(cluster->m_clusterIdsList); |
| 404 |
|
| 405 |
RagTime5ClusterManager::Link const &clustLink=cluster->m_dataLink; |
| 406 |
readFormulaZones(*cluster, clustLink, sheetId, i==0); |
| 407 |
|
| 408 |
for (auto const &lnk : cluster->m_linksList) |
| 409 |
m_document.readFixedSizeZone(lnk, "FormulaUnknown"); |
| 410 |
} |
| 411 |
return true; |
| 412 |
} |
| 413 |
|
| 414 |
//////////////////////////////////////////////////////////// |
| 415 |
// |
| 416 |
// Intermediate level |
| 417 |
// |
| 418 |
//////////////////////////////////////////////////////////// |
| 419 |
|
| 420 |
// |
| 421 |
// formula |
| 422 |
// |
| 423 |
bool RagTime5Formula::readFormulaZones(RagTime5ClusterManager::Cluster &/*cluster*/, RagTime5ClusterManager::Link const &link, |
| 424 |
int sheetId, bool isDefinition) |
| 425 |
{ |
| 426 |
if (link.m_ids.size()<2 || !link.m_ids[1]) |
| 427 |
return false; |
| 428 |
|
| 429 |
std::vector<long> decal; |
| 430 |
if (link.m_ids[0]) |
| 431 |
m_document.readPositions(link.m_ids[0], decal); |
| 432 |
if (decal.empty()) |
| 433 |
decal=link.m_longList; |
| 434 |
|
| 435 |
int const dataId=link.m_ids[1]; |
| 436 |
auto dataZone=m_document.getDataZone(dataId); |
| 437 |
auto N=int(decal.size()); |
| 438 |
|
| 439 |
if (!dataZone || !dataZone->m_entry.valid() || |
| 440 |
dataZone->getKindLastPart(dataZone->m_kinds[1].empty())!="ItemData" || N<=1) { |
| 441 |
if (N==1 && dataZone && !dataZone->m_entry.valid()) { |
| 442 |
// a zone with 0 zone is ok... |
| 443 |
dataZone->m_isParsed=true; |
| 444 |
return true; |
| 445 |
} |
| 446 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormulaZones: the data zone %d seems bad\n", dataId)); |
| 447 |
return false; |
| 448 |
} |
| 449 |
|
| 450 |
dataZone->m_isParsed=true; |
| 451 |
MWAWEntry entry=dataZone->m_entry; |
| 452 |
libmwaw::DebugFile &ascFile=dataZone->ascii(); |
| 453 |
libmwaw::DebugStream f; |
| 454 |
std::string name(isDefinition ? "FormulaDef" : "FormulaPos"); |
| 455 |
f << "Entries(" << name << ")[" << *dataZone << "]:"; |
| 456 |
ascFile.addPos(entry.end()); |
| 457 |
ascFile.addNote("_"); |
| 458 |
ascFile.addPos(entry.begin()); |
| 459 |
ascFile.addNote(f.str().c_str()); |
| 460 |
|
| 461 |
MWAWInputStreamPtr input=dataZone->getInput(); |
| 462 |
input->setReadInverted(!dataZone->m_hiLoEndian); |
| 463 |
long debPos=entry.begin(); |
| 464 |
long endPos=entry.end(); |
| 465 |
|
| 466 |
std::map<int, std::vector<MWAWCellContent::FormulaInstruction> > idToFormulaMap; |
| 467 |
for (int i=0; i<N-1; ++i) { |
| 468 |
long pos=decal[size_t(i)]; |
| 469 |
long nextPos=decal[size_t(i+1)]; |
| 470 |
if (nextPos==pos) continue; |
| 471 |
if (pos<0 || debPos+nextPos>endPos || pos>nextPos) { |
| 472 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormulaZones: can not read the data zone %d-%d seems bad\n", dataId, i)); |
| 473 |
if (debPos+pos<endPos) { |
| 474 |
f.str(""); |
| 475 |
f << name << "-" << i+1 << ":###"; |
| 476 |
ascFile.addPos(debPos+pos); |
| 477 |
ascFile.addNote(f.str().c_str()); |
| 478 |
} |
| 479 |
continue; |
| 480 |
} |
| 481 |
input->seek(debPos+pos, librevenge::RVNG_SEEK_SET); |
| 482 |
if (isDefinition) { |
| 483 |
std::vector<MWAWCellContent::FormulaInstruction> formula; |
| 484 |
if (readFormulaDefinition(*dataZone, debPos+nextPos, sheetId, i+1, formula)) |
| 485 |
idToFormulaMap[i+1]=formula; |
| 486 |
} |
| 487 |
else if (!readFormulaPosition(*dataZone, debPos+nextPos,i+1)) { |
| 488 |
f.str(""); |
| 489 |
f << name << "-" << i+1 << ":"; |
| 490 |
ascFile.addPos(debPos+pos); |
| 491 |
ascFile.addNote(f.str().c_str()); |
| 492 |
} |
| 493 |
} |
| 494 |
if (!idToFormulaMap.empty() && sheetId>=0) // sheetId==-1 correspond to the document |
| 495 |
m_document.getSpreadsheetParser()->storeFormula(sheetId, idToFormulaMap); |
| 496 |
input->setReadInverted(false); |
| 497 |
return true; |
| 498 |
} |
| 499 |
namespace RagTime5FormulaInternal |
| 500 |
{ |
| 501 |
struct Functions { |
| 502 |
char const *m_name; |
| 503 |
int m_arity; |
| 504 |
}; |
| 505 |
|
| 506 |
static Functions const s_listFunctions[] = { |
| 507 |
{ "+", 2}, {"-", 2}, {"*", 2}, {"/", 2}, |
| 508 |
{nullptr, -2}, {"^", 2}, {"+", 1}, {"-", 1}, |
| 509 |
{nullptr, -2}, {nullptr, -2}, {"=", 2}, {"!=", 2}, |
| 510 |
{">", 2}, {"<", 2}, {">=", 2}, {"<=", 2}, |
| 511 |
|
| 512 |
{nullptr, -2}, {nullptr, -2}, {nullptr, -2}, {nullptr, -2}, |
| 513 |
{"AND", 2}, {"OR", 2}, {"NOT", 1}, {nullptr, -2}, |
| 514 |
{nullptr, -2}, {nullptr, -2}, {nullptr, -2}, {nullptr, -2}, |
| 515 |
{nullptr, -2}, {"&", 2}/*concat + space*/, {"&", 2}, {"_", 1} /* optional*/, |
| 516 |
|
| 517 |
{";", 2} /*list of arg*/, {nullptr, -2}, {nullptr, -2}, {nullptr, -2}, |
| 518 |
{nullptr, -2}, {nullptr, -2}/*date*/, {nullptr, -2}, {nullptr, -2}, |
| 519 |
{nullptr, -2}, {nullptr, -2}/*double*/, {nullptr, -2}, {nullptr, -2}/*char*/, |
| 520 |
{nullptr, -2}/*short*/, {nullptr, -2}, {nullptr, -2}/*text*/, {nullptr, -2}, |
| 521 |
|
| 522 |
{nullptr, -2}, {nullptr, -2}, {";", 2}, {nullptr, -2}, |
| 523 |
{nullptr, -2}, {nullptr, -2}, {nullptr, -2}, {nullptr, -2}, |
| 524 |
{nullptr, -2}, {nullptr, -2}, {nullptr, -2}, {nullptr, -2}, |
| 525 |
{nullptr, -2}, {nullptr, -2}, {nullptr, -2}, {nullptr, -2}, |
| 526 |
}; |
| 527 |
|
| 528 |
static size_t const s_numFunctions=MWAW_N_ELEMENTS(s_listFunctions); |
| 529 |
|
| 530 |
static char const *s_listFunctions2[] = { |
| 531 |
"^", "*", "/", nullptr, "+", "-", "&", "&", /*concat+space*/ // 80-9c |
| 532 |
"=", "!=", ">", "<", ">=", "<=", "AND", "OR", // a0-bc |
| 533 |
"NOT" // c0 |
| 534 |
}; |
| 535 |
} |
| 536 |
|
| 537 |
bool RagTime5Formula::readFormula(MWAWInputStreamPtr &input, |
| 538 |
std::vector<MWAWCellContent::FormulaInstruction> &formula, |
| 539 |
long const(&limitPos)[5], std::vector<std::string> const &functions, std::vector<MWAWCellContent::FormulaInstruction> const &cells, |
| 540 |
libmwaw::DebugStream &f1) const |
| 541 |
{ |
| 542 |
formula.clear(); |
| 543 |
long pos=input->tell(); |
| 544 |
long const endFormula=limitPos[1]; |
| 545 |
long const endFormula2=limitPos[2]; |
| 546 |
if (pos>=endFormula || !input->checkPosition(endFormula) || !input->checkPosition(endFormula2)) { |
| 547 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormula: the zone seems too short\n")); |
| 548 |
return false; |
| 549 |
} |
| 550 |
size_t numFuncs=functions.size(); |
| 551 |
size_t numCells=cells.size(); |
| 552 |
struct StackType { |
| 553 |
enum Type { S_Constant, S_Function, S_Operator, S_Operator1 }; |
| 554 |
explicit StackType(Type type=S_Constant, std::string const &op="") |
| 555 |
: m_type(type) |
| 556 |
, m_operator(op) |
| 557 |
{ |
| 558 |
} |
| 559 |
Type m_type; |
| 560 |
std::string m_operator; |
| 561 |
}; |
| 562 |
std::vector<std::vector<MWAWCellContent::FormulaInstruction> > stack; |
| 563 |
std::vector<StackType> stackType; |
| 564 |
bool ok=false; |
| 565 |
bool firstOptionalParam=false; |
| 566 |
std::vector<bool> firstOptionalParamStack; |
| 567 |
libmwaw::DebugStream f; |
| 568 |
while (input->tell()<endFormula) { |
| 569 |
auto code=input->readULong(1); |
| 570 |
pos=input->tell(); |
| 571 |
MWAWCellContent::FormulaInstruction instr; |
| 572 |
bool noneInstr=false; |
| 573 |
int arity=0; |
| 574 |
switch (code) { |
| 575 |
case 0x25: { // fixme |
| 576 |
if (pos+8>endFormula) |
| 577 |
break; |
| 578 |
double res; |
| 579 |
bool isNan; |
| 580 |
if (input->readDouble8(res, isNan)) { |
| 581 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Double; |
| 582 |
instr.m_doubleValue=res; |
| 583 |
f << "DT=" << res << ","; |
| 584 |
ok=true; |
| 585 |
} |
| 586 |
break; |
| 587 |
} |
| 588 |
case 0x26: { // fixme |
| 589 |
if (pos+8>endFormula) |
| 590 |
break; |
| 591 |
double res; |
| 592 |
bool isNan; |
| 593 |
if (input->readDouble8(res, isNan)) { |
| 594 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Double; |
| 595 |
instr.m_doubleValue=res; |
| 596 |
f << "H=" << res << ","; |
| 597 |
ok=true; |
| 598 |
} |
| 599 |
break; |
| 600 |
} |
| 601 |
case 0x29: { |
| 602 |
if (pos+8>endFormula) |
| 603 |
break; |
| 604 |
double res; |
| 605 |
bool isNan; |
| 606 |
if (input->readDouble8(res, isNan)) { |
| 607 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Double; |
| 608 |
instr.m_doubleValue=res; |
| 609 |
f << res << ","; |
| 610 |
ok=true; |
| 611 |
} |
| 612 |
break; |
| 613 |
} |
| 614 |
case 0x2b: |
| 615 |
if (pos+1>endFormula) |
| 616 |
break; |
| 617 |
ok=true; |
| 618 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Long; |
| 619 |
instr.m_longValue=double(input->readULong(1)); |
| 620 |
f << instr.m_longValue << ","; |
| 621 |
break; |
| 622 |
case 0x2c: |
| 623 |
if (pos+2>endFormula) |
| 624 |
break; |
| 625 |
ok=true; |
| 626 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Long; |
| 627 |
instr.m_longValue=double(input->readULong(2)); |
| 628 |
f << instr.m_longValue << ","; |
| 629 |
break; |
| 630 |
case 0x2e: { // string |
| 631 |
if (pos+2>endFormula) |
| 632 |
break; |
| 633 |
int decal=int(input->readULong(2)); |
| 634 |
if (pos+decal<endFormula||pos+1+decal>endFormula2) |
| 635 |
break; |
| 636 |
input->seek(pos+decal,librevenge::RVNG_SEEK_SET); |
| 637 |
int len=int(input->readULong(1)); |
| 638 |
if (pos+1+decal+len>endFormula2) |
| 639 |
break; |
| 640 |
ok=true; |
| 641 |
std::string text; |
| 642 |
for (int c=0; c<len; ++c) text+=char(input->readULong(1)); |
| 643 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Text; |
| 644 |
instr.m_content=text; |
| 645 |
f << text << ","; |
| 646 |
input->seek(pos+2,librevenge::RVNG_SEEK_SET); |
| 647 |
break; |
| 648 |
} |
| 649 |
case 0x2f: { // unicode string |
| 650 |
if (pos+2>endFormula) |
| 651 |
break; |
| 652 |
int decal=int(input->readULong(2)); |
| 653 |
if (pos+decal<endFormula||pos+2+decal>endFormula2) |
| 654 |
break; |
| 655 |
input->seek(pos+decal,librevenge::RVNG_SEEK_SET); |
| 656 |
int len=int(input->readULong(2)); |
| 657 |
if (pos+decal+2+2*len>endFormula2) |
| 658 |
break; |
| 659 |
ok=true; |
| 660 |
librevenge::RVNGString text; |
| 661 |
for (int c=0; c<len; ++c) libmwaw::appendUnicode(uint32_t(input->readULong(2)), text); |
| 662 |
f << text.cstr() << ","; |
| 663 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Text; |
| 664 |
if (text.cstr()) |
| 665 |
instr.m_content=text.cstr(); |
| 666 |
input->seek(pos+2,librevenge::RVNG_SEEK_SET); |
| 667 |
break; |
| 668 |
} |
| 669 |
case 0x30: // reference id at end |
| 670 |
case 0x37: // reference to data, difference with 0x30? |
| 671 |
case 0x3a: { // reference id at end (+copy format) |
| 672 |
if (pos+1>endFormula) |
| 673 |
break; |
| 674 |
ok=true; |
| 675 |
auto id=input->readULong(1); |
| 676 |
if (id==0 || id>numCells) { |
| 677 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormula: sorry, find unexpected cell id\n")); |
| 678 |
f << "##C" << id << ","; |
| 679 |
} |
| 680 |
else { |
| 681 |
instr=cells[id-1]; |
| 682 |
f << cells[id-1] << ","; |
| 683 |
} |
| 684 |
break; |
| 685 |
} |
| 686 |
case 0x33: { // begins of parameters (+deplacement) |
| 687 |
if (pos+2>endFormula) |
| 688 |
break; |
| 689 |
noneInstr=ok=true; |
| 690 |
auto decal=input->readULong(2); // in pos+decal the end of }, ie 0x35 |
| 691 |
firstOptionalParamStack.push_back(firstOptionalParam); |
| 692 |
firstOptionalParam=true; |
| 693 |
f << "{" << std::hex << decal << std::dec << ","; |
| 694 |
break; |
| 695 |
} |
| 696 |
case 0x34: // function id at end |
| 697 |
case 0x35: { // function id + deplacement |
| 698 |
if (pos+(code==0x34 ? 2 : 4)>endFormula) |
| 699 |
break; |
| 700 |
arity=int(input->readULong(1)); |
| 701 |
auto id=input->readULong(1); |
| 702 |
if (code==0x35) |
| 703 |
f << "}"; |
| 704 |
if (id==0 || id>numFuncs) { |
| 705 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormula: sorry, find unexpected function\n")); |
| 706 |
f << "##F" << id; |
| 707 |
} |
| 708 |
else { |
| 709 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Function; |
| 710 |
instr.m_content=functions[id-1]; |
| 711 |
f << functions[id-1]; |
| 712 |
} |
| 713 |
if (arity) |
| 714 |
f << ":" << arity; |
| 715 |
if (code==0x34) { |
| 716 |
ok=true; |
| 717 |
f << ","; |
| 718 |
break; |
| 719 |
} |
| 720 |
int N=int(input->readLong(2)); |
| 721 |
if (pos+4+2*N>=endFormula) |
| 722 |
break; |
| 723 |
if (!firstOptionalParamStack.empty()) { |
| 724 |
firstOptionalParam=firstOptionalParamStack.back(); |
| 725 |
firstOptionalParamStack.pop_back(); |
| 726 |
} |
| 727 |
else |
| 728 |
firstOptionalParam=false; |
| 729 |
ok=true; |
| 730 |
if (N) |
| 731 |
++arity; |
| 732 |
// in pos+4+value: the beginning of each parameters |
| 733 |
f << "["; |
| 734 |
for (int i=0; i<N; ++i) |
| 735 |
f << input->readLong(2) << ":"; |
| 736 |
f << "],"; |
| 737 |
break; |
| 738 |
} |
| 739 |
case 0x39: // happens in Button("toto", True) for true + dec{beg:end} |
| 740 |
if (pos+4>endFormula) |
| 741 |
break; |
| 742 |
noneInstr=ok=true; |
| 743 |
f << "Action" << std::hex << input->readULong(2) << "-" << input->readULong(2) << std::dec << ","; |
| 744 |
break; |
| 745 |
default: |
| 746 |
if (code<RagTime5FormulaInternal::s_numFunctions && RagTime5FormulaInternal::s_listFunctions[code].m_name) { |
| 747 |
auto const &func=RagTime5FormulaInternal::s_listFunctions[code]; |
| 748 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator; |
| 749 |
instr.m_content=func.m_name; |
| 750 |
f << func.m_name << ","; |
| 751 |
arity=func.m_arity; |
| 752 |
ok=true; |
| 753 |
break; |
| 754 |
} |
| 755 |
break; |
| 756 |
} |
| 757 |
if (!ok) |
| 758 |
break; |
| 759 |
if (noneInstr) |
| 760 |
continue; |
| 761 |
if (instr.m_type!=MWAWCellContent::FormulaInstruction::F_Function && instr.m_type!=MWAWCellContent::FormulaInstruction::F_Operator) { |
| 762 |
stack.push_back(std::vector<MWAWCellContent::FormulaInstruction>(1,instr)); |
| 763 |
stackType.push_back(StackType()); |
| 764 |
continue; |
| 765 |
} |
| 766 |
size_t numElt = stack.size(); |
| 767 |
if (instr.m_type==MWAWCellContent::FormulaInstruction::F_Operator && instr.m_content==";") { |
| 768 |
if (firstOptionalParam) { |
| 769 |
firstOptionalParam=false; |
| 770 |
continue; |
| 771 |
} |
| 772 |
if (static_cast<int>(numElt)<arity && input->tell()>=endFormula) { |
| 773 |
arity=1; |
| 774 |
instr.m_content="="; |
| 775 |
} |
| 776 |
} |
| 777 |
if (static_cast<int>(numElt) < arity) { |
| 778 |
f << "###"; |
| 779 |
ok = false; |
| 780 |
continue; |
| 781 |
} |
| 782 |
std::vector<MWAWCellContent::FormulaInstruction> child; |
| 783 |
if ((instr.m_content[0] >= 'A' && instr.m_content[0] <= 'Z') || instr.m_type==MWAWCellContent::FormulaInstruction::F_Function) { |
| 784 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Function; |
| 785 |
child.push_back(instr); |
| 786 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator; |
| 787 |
instr.m_content="("; |
| 788 |
child.push_back(instr); |
| 789 |
for (int i = 0; i < arity; i++) { |
| 790 |
if (i) { |
| 791 |
instr.m_content=";"; |
| 792 |
child.push_back(instr); |
| 793 |
} |
| 794 |
auto const &node=stack[size_t(static_cast<int>(numElt)-arity+i)]; |
| 795 |
child.insert(child.end(), node.begin(), node.end()); |
| 796 |
} |
| 797 |
instr.m_content=")"; |
| 798 |
child.push_back(instr); |
| 799 |
|
| 800 |
size_t newSize=size_t(static_cast<int>(numElt)-arity+1); |
| 801 |
stack.resize(newSize); |
| 802 |
stack[newSize-1] = child; |
| 803 |
stackType.resize(newSize); |
| 804 |
stackType[newSize-1] = StackType(StackType::S_Function); |
| 805 |
continue; |
| 806 |
} |
| 807 |
if (arity==1) { |
| 808 |
if (instr.m_content=="_") continue; |
| 809 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator; |
| 810 |
bool needAddPara=stack[numElt-1].size()!=1 && stackType[numElt-1].m_type==StackType::S_Operator && input->tell()!=endFormula; |
| 811 |
stack[numElt-1].insert(stack[numElt-1].begin(), instr); |
| 812 |
stackType[numElt-1] = StackType(StackType::S_Operator1, instr.m_content); |
| 813 |
if (needAddPara) { |
| 814 |
instr.m_content="("; |
| 815 |
stack[numElt-1].insert(stack[numElt-1].begin()+1, instr); |
| 816 |
instr.m_content=")"; |
| 817 |
stack[numElt-1].push_back(instr); |
| 818 |
} |
| 819 |
continue; |
| 820 |
} |
| 821 |
if (arity==2) { |
| 822 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator; |
| 823 |
bool needAddPara=stack[numElt-2].size()!=1 && instr.m_content!=";" && stackType[numElt-2].m_type==StackType::S_Operator && stackType[numElt-2].m_operator!=instr.m_content; |
| 824 |
if (needAddPara) { |
| 825 |
MWAWCellContent::FormulaInstruction instr2; |
| 826 |
instr2.m_type=MWAWCellContent::FormulaInstruction::F_Operator; |
| 827 |
instr2.m_content="("; |
| 828 |
stack[numElt-2].insert(stack[numElt-2].begin(), instr2); |
| 829 |
instr2.m_content=")"; |
| 830 |
stack[numElt-2].push_back(instr2); |
| 831 |
} |
| 832 |
stack[numElt-2].push_back(instr); |
| 833 |
stackType[numElt-2] = StackType(StackType::S_Operator, instr.m_content); |
| 834 |
needAddPara=stack[numElt-1].size()!=1 && instr.m_content!=";" && stackType[numElt-1].m_type==StackType::S_Operator; |
| 835 |
if (needAddPara) { |
| 836 |
instr.m_content="("; |
| 837 |
stack[numElt-2].push_back(instr); |
| 838 |
} |
| 839 |
stack[numElt-2].insert(stack[numElt-2].end(), stack[numElt-1].begin(), stack[numElt-1].end()); |
| 840 |
if (needAddPara) { |
| 841 |
instr.m_content=")"; |
| 842 |
stack[numElt-2].push_back(instr); |
| 843 |
} |
| 844 |
stack.resize(numElt-1); |
| 845 |
stackType.resize(numElt-1); |
| 846 |
continue; |
| 847 |
} |
| 848 |
ok=false; |
| 849 |
f << "### unexpected arity[" << instr << "]"; |
| 850 |
break; |
| 851 |
} |
| 852 |
if (!ok || stack.size()!=1 || stack[0].size()<2 || stack[0][0].m_content!="=" || input->tell()!=endFormula) { |
| 853 |
f1 << f.str(); |
| 854 |
f1 << "###["; |
| 855 |
for (auto const &i : stack) { |
| 856 |
f1 << "["; |
| 857 |
for (auto const &j : i) |
| 858 |
f1 << j << ","; |
| 859 |
f1 << "]"; |
| 860 |
} |
| 861 |
f1 << "],"; |
| 862 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormula: sorry, can not read a formula\n")); |
| 863 |
return false; |
| 864 |
} |
| 865 |
|
| 866 |
f1 << "["; |
| 867 |
for (auto const &j : stack[0]) |
| 868 |
f1 << j << ","; |
| 869 |
f1 << "],"; |
| 870 |
formula.insert(formula.begin(),stack[0].begin()+1, stack[0].end()); |
| 871 |
return true; |
| 872 |
} |
| 873 |
|
| 874 |
bool RagTime5Formula::readFormula2(MWAWInputStreamPtr &input, long const(&limitPos)[5], |
| 875 |
std::vector<std::string> const &functions, std::vector<MWAWCellContent::FormulaInstruction> const &cells, |
| 876 |
libmwaw::DebugStream &f) const |
| 877 |
{ |
| 878 |
long pos=input->tell(); |
| 879 |
long const endFormula=limitPos[2]; |
| 880 |
if (pos>=endFormula) { |
| 881 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormula2: the zone seems too short\n")); |
| 882 |
return false; |
| 883 |
} |
| 884 |
f << "form2="; |
| 885 |
size_t numFuncs=functions.size(); |
| 886 |
size_t numCells=cells.size(); |
| 887 |
while (input->tell()<endFormula) { |
| 888 |
auto code=input->readULong(1); |
| 889 |
bool ok=false; |
| 890 |
pos=input->tell(); |
| 891 |
switch (code) { |
| 892 |
case 0: |
| 893 |
f << "1"; |
| 894 |
ok=true; |
| 895 |
break; |
| 896 |
case 1: |
| 897 |
if (pos+1>endFormula) |
| 898 |
break; |
| 899 |
f << input->readULong(1); |
| 900 |
ok=true; |
| 901 |
break; |
| 902 |
case 2: |
| 903 |
if (pos+2>endFormula) |
| 904 |
break; |
| 905 |
f << input->readULong(2); |
| 906 |
ok=true; |
| 907 |
break; |
| 908 |
case 5: |
| 909 |
case 6: // date |
| 910 |
case 7: { // time |
| 911 |
if (pos+8>endFormula) |
| 912 |
break; |
| 913 |
double res; |
| 914 |
bool isNan; |
| 915 |
if (input->readDouble8(res, isNan)) { |
| 916 |
if (code==6) |
| 917 |
f << "D[" << res << "]"; |
| 918 |
else if (code==7) |
| 919 |
f << "T[" << res << "]"; |
| 920 |
else |
| 921 |
f << res; |
| 922 |
ok=true; |
| 923 |
} |
| 924 |
break; |
| 925 |
} |
| 926 |
case 0xc: { |
| 927 |
if (pos+1>endFormula) |
| 928 |
break; |
| 929 |
int len=int(input->readULong(1)); |
| 930 |
if (pos+1+len>endFormula) |
| 931 |
break; |
| 932 |
std::string texte; |
| 933 |
for (int c=0; c<len; ++c) texte+=char(input->readULong(1)); |
| 934 |
f << texte; |
| 935 |
ok=true; |
| 936 |
break; |
| 937 |
} |
| 938 |
case 0x10: { // unicode text |
| 939 |
if (pos+2>endFormula) |
| 940 |
break; |
| 941 |
int len=int(input->readULong(2)); |
| 942 |
if (pos+2+2*len>endFormula) |
| 943 |
break; |
| 944 |
std::string texte; |
| 945 |
for (int c=0; c<len; ++c) texte+=char(input->readULong(2)); |
| 946 |
f << texte; |
| 947 |
ok=true; |
| 948 |
break; |
| 949 |
} |
| 950 |
case 0x20: { // cell id |
| 951 |
if (pos+1>endFormula) |
| 952 |
break; |
| 953 |
size_t id=size_t(input->readULong(1)); |
| 954 |
if (id==0 || id>numCells) { |
| 955 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormula2: sorry, find unexpected cells\n")); |
| 956 |
f << "##C" << id; |
| 957 |
} |
| 958 |
else |
| 959 |
f << cells[id-1]; |
| 960 |
ok=true; |
| 961 |
break; |
| 962 |
} |
| 963 |
case 0x24: { |
| 964 |
if (pos+1>endFormula) |
| 965 |
break; |
| 966 |
size_t id=size_t(input->readULong(1)); |
| 967 |
if (id==0 || id>numFuncs) { |
| 968 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormula2: sorry, find unexpected function\n")); |
| 969 |
f << "##F" << id; |
| 970 |
} |
| 971 |
else |
| 972 |
f << functions[id-1]; |
| 973 |
ok=true; |
| 974 |
break; |
| 975 |
} |
| 976 |
case 0x40: |
| 977 |
f << "("; |
| 978 |
ok=true; |
| 979 |
break; |
| 980 |
case 0x44: |
| 981 |
f << ")"; |
| 982 |
ok=true; |
| 983 |
break; |
| 984 |
case 0x49: |
| 985 |
f << ";"; |
| 986 |
ok=true; |
| 987 |
break; |
| 988 |
case 0x54: |
| 989 |
f << "%"; |
| 990 |
ok=true; |
| 991 |
break; |
| 992 |
default: |
| 993 |
if ((code%4) || (code<0x80||code>0xc0) || !RagTime5FormulaInternal::s_listFunctions2[code/4-0x20]) |
| 994 |
break; |
| 995 |
f << RagTime5FormulaInternal::s_listFunctions2[code/4-0x20]; |
| 996 |
ok=true; |
| 997 |
break; |
| 998 |
} |
| 999 |
if (!ok) { |
| 1000 |
f << "###"; |
| 1001 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormula2: sorry, unknown code=%lx\n", code)); |
| 1002 |
return false; |
| 1003 |
} |
| 1004 |
} |
| 1005 |
f << ","; |
| 1006 |
return true; |
| 1007 |
} |
| 1008 |
|
| 1009 |
bool RagTime5Formula::readFormulaDefinition(RagTime5Zone &zone, long endPos, int sheetId, int n, |
| 1010 |
std::vector<MWAWCellContent::FormulaInstruction> &formula) |
| 1011 |
{ |
| 1012 |
MWAWInputStreamPtr input=zone.getInput(); |
| 1013 |
long pos=input->tell(); |
| 1014 |
libmwaw::DebugFile &ascFile=zone.ascii(); |
| 1015 |
libmwaw::DebugStream f; |
| 1016 |
f << "FormulaDef-FD" << n << ":"; |
| 1017 |
if (pos+6>endPos) { |
| 1018 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormulaDefinition: the zone seems too short\n")); |
| 1019 |
f<<"###"; |
| 1020 |
ascFile.addPos(pos); |
| 1021 |
ascFile.addNote(f.str().c_str()); |
| 1022 |
return false; |
| 1023 |
} |
| 1024 |
long limitPos[5]= {6,0,0,0,int(endPos-pos)}; |
| 1025 |
for (int i=1; i<4; ++i) { |
| 1026 |
limitPos[i]=static_cast<long>(input->readULong(2)); |
| 1027 |
if (limitPos[i]==0) continue; |
| 1028 |
if (limitPos[i]&0x8000) { |
| 1029 |
f << "fl" << i << ","; |
| 1030 |
limitPos[i] &= 0x7FFF; |
| 1031 |
} |
| 1032 |
if (limitPos[i]<6 || pos+limitPos[i]>=endPos) { |
| 1033 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormulaDefinition: the %d pointer seems bad\n", i)); |
| 1034 |
f << "##limitPos[" << i << "]=" << limitPos[i] << ","; |
| 1035 |
limitPos[i]=0; |
| 1036 |
continue; |
| 1037 |
} |
| 1038 |
} |
| 1039 |
for (int i=3; i>=1; --i) { |
| 1040 |
if (!limitPos[i]) |
| 1041 |
limitPos[i]=limitPos[i+1]; |
| 1042 |
} |
| 1043 |
for (auto &d : limitPos) d += pos; |
| 1044 |
|
| 1045 |
// first retrieve the function names |
| 1046 |
std::vector<std::string> functions; |
| 1047 |
bool functionsOk=true; |
| 1048 |
if (((limitPos[4]-limitPos[3])%2) || limitPos[3]>limitPos[4]) { |
| 1049 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormulaDefinition: the function's name zone seems bad\n")); |
| 1050 |
f << "###function,"; |
| 1051 |
ascFile.addDelimiter(limitPos[3],'|'); |
| 1052 |
} |
| 1053 |
else if (limitPos[3]<limitPos[4]) { |
| 1054 |
input->seek(limitPos[3], librevenge::RVNG_SEEK_SET); |
| 1055 |
size_t N=size_t((limitPos[4]-limitPos[3])/2); |
| 1056 |
functions.resize(N); |
| 1057 |
size_t numDocFunc=m_state->m_documentFunctionNames.size(); |
| 1058 |
for (size_t i=0; i<N; ++i) { |
| 1059 |
size_t id=size_t(input->readULong(2)); |
| 1060 |
if (id>=numDocFunc) { |
| 1061 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormulaDefinition: the function's name zone seems bad\n")); |
| 1062 |
f << "###F" << id << ","; |
| 1063 |
functionsOk=true; |
| 1064 |
} |
| 1065 |
else { |
| 1066 |
functions[i]=m_state->m_documentFunctionNames[id]; |
| 1067 |
if (m_state->m_documentFunctionNames[id].empty()) |
| 1068 |
functionsOk=false; |
| 1069 |
} |
| 1070 |
} |
| 1071 |
ascFile.addDelimiter(limitPos[3],'|'); |
| 1072 |
} |
| 1073 |
// now retrieve the cells |
| 1074 |
std::vector<MWAWCellContent::FormulaInstruction> cells; |
| 1075 |
bool cellsOk=true; |
| 1076 |
if (((limitPos[3]-limitPos[2])%4) || limitPos[2]>limitPos[3]) { |
| 1077 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormulaDefinition: the cell's zone seems bad\n")); |
| 1078 |
f << "###cells,"; |
| 1079 |
ascFile.addDelimiter(limitPos[2],'|'); |
| 1080 |
} |
| 1081 |
else if (limitPos[2]<limitPos[3]) { |
| 1082 |
auto const &sheetManager=*m_document.getSpreadsheetParser(); |
| 1083 |
input->seek(limitPos[2], librevenge::RVNG_SEEK_SET); |
| 1084 |
long endDataPos=limitPos[3]; |
| 1085 |
f << "cells=["; |
| 1086 |
while (!input->isEnd()) { |
| 1087 |
long begDataPos=input->tell(); |
| 1088 |
if (begDataPos==endDataPos) break; |
| 1089 |
if (begDataPos+4>endDataPos) { |
| 1090 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormulaDefinition: problem with length for cells' zone\n")); |
| 1091 |
f << "###end,"; |
| 1092 |
break; |
| 1093 |
} |
| 1094 |
auto lVal=long(input->readULong(4)); |
| 1095 |
int type=int(lVal>>24); |
| 1096 |
lVal=(lVal&0xFFFFFF); |
| 1097 |
MWAWCellContent::FormulaInstruction instr; |
| 1098 |
std::stringstream s; |
| 1099 |
if (type==3) { |
| 1100 |
if (sheetManager.getFormulaRef(sheetId, int(lVal), instr)) |
| 1101 |
f << instr << ","; |
| 1102 |
else { |
| 1103 |
s << "##RP" << std::hex << lVal << std::dec; |
| 1104 |
instr.m_content=s.str(); |
| 1105 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Text; |
| 1106 |
f << "#" << instr.m_content << ","; |
| 1107 |
cellsOk=false; |
| 1108 |
} |
| 1109 |
cells.push_back(instr); |
| 1110 |
continue; |
| 1111 |
} |
| 1112 |
input->seek(begDataPos, librevenge::RVNG_SEEK_SET); |
| 1113 |
std::vector<int> listIds; |
| 1114 |
if (begDataPos+8>endDataPos || !m_structManager->readDataIdList(input, 1, listIds)) { |
| 1115 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormulaDefinition: can not read data for cells zone\n")); |
| 1116 |
f << "#type=" << std::hex << lVal << std::dec << ","; |
| 1117 |
break; |
| 1118 |
} |
| 1119 |
if (listIds[0]) // some cluster data |
| 1120 |
f << m_document.getClusterManager()->getClusterDebugName(listIds[0]); |
| 1121 |
lVal=long(input->readULong(4)); |
| 1122 |
type=int(lVal>>24); |
| 1123 |
lVal=(lVal&0xFFFFFF); |
| 1124 |
if (type==3) { |
| 1125 |
if (sheetManager.getFormulaRef(listIds[0], int(lVal), instr)) |
| 1126 |
f << instr << ","; |
| 1127 |
else { |
| 1128 |
s << "##RP" << std::hex << lVal << std::dec; |
| 1129 |
instr.m_content=s.str(); |
| 1130 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Text; |
| 1131 |
f << "##" << instr.m_content << ","; |
| 1132 |
cellsOk=false; |
| 1133 |
} |
| 1134 |
} |
| 1135 |
else if ((type&0xef)==0) { // 0 or 10 |
| 1136 |
static bool first=true; |
| 1137 |
if (first) { |
| 1138 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormulaDefinition: reference to button is not implemented\n")); |
| 1139 |
first=false; |
| 1140 |
} |
| 1141 |
if (lVal==0x2a01) |
| 1142 |
f << "Button,"; |
| 1143 |
else |
| 1144 |
f << "#Button=" << std::hex << lVal << std::dec << ","; |
| 1145 |
s << "#Button" << listIds[0]; |
| 1146 |
instr.m_content=s.str(); |
| 1147 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Text; |
| 1148 |
cellsOk=false; |
| 1149 |
} |
| 1150 |
else { |
| 1151 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormulaDefinition: unknown cells type\n")); |
| 1152 |
s << "Unknown" << std::hex << lVal << std::dec << "[" << type << "],"; |
| 1153 |
instr.m_content=s.str(); |
| 1154 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Text; |
| 1155 |
f << "###" << s.str() << ","; |
| 1156 |
cellsOk=false; |
| 1157 |
} |
| 1158 |
if (cellsOk && instr.m_type==MWAWCellContent::FormulaInstruction::F_CellList && |
| 1159 |
(instr.m_position[0][0]>instr.m_position[1][0] || instr.m_position[0][1]>instr.m_position[1][1])) { |
| 1160 |
cellsOk=false; |
| 1161 |
static bool first=true; |
| 1162 |
if (first) { |
| 1163 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormulaDefinition: find some invalid cells\n")); |
| 1164 |
first=false; |
| 1165 |
} |
| 1166 |
} |
| 1167 |
cells.push_back(instr); |
| 1168 |
} |
| 1169 |
f << "],"; |
| 1170 |
} |
| 1171 |
bool formulaOk=false; |
| 1172 |
for (int i=0; i<2; ++i) { |
| 1173 |
if (limitPos[i+1]==limitPos[i]) |
| 1174 |
continue; |
| 1175 |
if (limitPos[i+1]<limitPos[i]) { |
| 1176 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormulaDefinition: the %d pointer seems bad\n", i)); |
| 1177 |
f << "##decal" << i << ","; |
| 1178 |
continue; |
| 1179 |
} |
| 1180 |
switch (i) { |
| 1181 |
case 0: { |
| 1182 |
if (limitPos[i+1]-limitPos[i]<8) { |
| 1183 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormulaDefinition: the zone 0 size seems bad\n")); |
| 1184 |
f << "##decal2,"; |
| 1185 |
break; |
| 1186 |
} |
| 1187 |
input->seek(limitPos[i], librevenge::RVNG_SEEK_SET); |
| 1188 |
auto val=static_cast<int>(input->readLong(2)); // always 0? |
| 1189 |
if (val) f<< "#f0=" << val << ","; |
| 1190 |
auto lVal=input->readULong(4); |
| 1191 |
int type=int(lVal>>16); |
| 1192 |
int id=int(lVal&0xffff); |
| 1193 |
if ((type!=1 && type!=0x100) && (id==1||id==0x100)) { |
| 1194 |
static bool first=true; |
| 1195 |
if (first) { |
| 1196 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormulaDefinition: orderings seems bad\n")); |
| 1197 |
first=false; |
| 1198 |
} |
| 1199 |
lVal=((lVal&0xff)<<24)|((lVal&0xff00)<<8)|(lVal>>16); |
| 1200 |
type=int(lVal>>16); |
| 1201 |
id=int(lVal&0xffff); |
| 1202 |
} |
| 1203 |
f << "id=" << id << ","; |
| 1204 |
f << "type=" << type << ","; |
| 1205 |
if (type==1 && input->tell()+4<limitPos[i+1]) { |
| 1206 |
auto type2=input->readULong(4); |
| 1207 |
if (type2) f << "type2=" << printType(type2) << ","; |
| 1208 |
} |
| 1209 |
else if (type!=256) { |
| 1210 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormulaDefinition: unexpected type\n")); |
| 1211 |
f << "##type,"; |
| 1212 |
} |
| 1213 |
if (input->tell()!=limitPos[i+1]) { |
| 1214 |
f << "hasForm,"; |
| 1215 |
ascFile.addDelimiter(input->tell(),'|'); |
| 1216 |
if (!readFormula(input, formula, limitPos, functions, cells, f)) { |
| 1217 |
f << "###"; |
| 1218 |
ascFile.addDelimiter(input->tell(),'@'); |
| 1219 |
} |
| 1220 |
else |
| 1221 |
formulaOk=true; |
| 1222 |
} |
| 1223 |
ascFile.addDelimiter(limitPos[i+1],'|'); |
| 1224 |
break; |
| 1225 |
} |
| 1226 |
case 1: |
| 1227 |
input->seek(limitPos[i], librevenge::RVNG_SEEK_SET); |
| 1228 |
if (input->tell()!=limitPos[i+1]) { |
| 1229 |
ascFile.addDelimiter(input->tell(),'|'); |
| 1230 |
if (!readFormula2(input, limitPos, functions, cells, f)) { |
| 1231 |
f << "###"; |
| 1232 |
ascFile.addDelimiter(input->tell(),'@'); |
| 1233 |
} |
| 1234 |
} |
| 1235 |
ascFile.addDelimiter(limitPos[i+1],'|'); |
| 1236 |
break; |
| 1237 |
default: |
| 1238 |
break; |
| 1239 |
} |
| 1240 |
} |
| 1241 |
ascFile.addPos(pos); |
| 1242 |
ascFile.addNote(f.str().c_str()); |
| 1243 |
return functionsOk && cellsOk && formulaOk; |
| 1244 |
} |
| 1245 |
|
| 1246 |
bool RagTime5Formula::readFormulaPosition(RagTime5Zone &zone, long endPos, int n) |
| 1247 |
{ |
| 1248 |
MWAWInputStreamPtr input=zone.getInput(); |
| 1249 |
long pos=input->tell(); |
| 1250 |
libmwaw::DebugFile &ascFile=zone.ascii(); |
| 1251 |
libmwaw::DebugStream f; |
| 1252 |
f << "FormulaPos-" << n << ":"; |
| 1253 |
if ((endPos-pos)%8) { |
| 1254 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormulaPosition: the zone seems bad\n")); |
| 1255 |
f<<"###"; |
| 1256 |
ascFile.addPos(pos); |
| 1257 |
ascFile.addNote(f.str().c_str()); |
| 1258 |
return false; |
| 1259 |
} |
| 1260 |
auto N=int((endPos-pos)/8); |
| 1261 |
f << "cluster=["; |
| 1262 |
for (int i=0; i<N; ++i) { |
| 1263 |
long actPos=input->tell(); |
| 1264 |
std::vector<int> listIds; // the cluster which contains the definition |
| 1265 |
if (!m_structManager->readDataIdList(input, 1, listIds)) { |
| 1266 |
MWAW_DEBUG_MSG(("RagTime5Formula::readFormulaPosition: find unknown block type\n")); |
| 1267 |
f << "##type,"; |
| 1268 |
input->seek(actPos+8, librevenge::RVNG_SEEK_SET); |
| 1269 |
continue; |
| 1270 |
} |
| 1271 |
auto id=input->readULong(4); |
| 1272 |
if (listIds[0]==0) f << "_,"; |
| 1273 |
else if (id&0xc0000000) f << "data" << listIds[0] << "A-FD" << (id&0x3fffffff) << "[" << (id>>30) << ",]"; |
| 1274 |
else f << "data" << listIds[0] << "A-FD" << (id&0x3fffffff) << ","; |
| 1275 |
} |
| 1276 |
f << "],"; |
| 1277 |
ascFile.addPos(pos); |
| 1278 |
ascFile.addNote(f.str().c_str()); |
| 1279 |
return true; |
| 1280 |
} |
| 1281 |
|
| 1282 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |