| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
| 2 | /* libwps |
| 3 | * Version: MPL 2.0 / LGPLv2.1+ |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | * |
| 9 | * Major Contributor(s): |
| 10 | * Copyright (C) 2006, 2007 Andrew Ziem |
| 11 | * Copyright (C) 2004 Marc Maurer ([email protected]) |
| 12 | * Copyright (C) 2004-2006 Fridrich Strba ([email protected]) |
| 13 | * |
| 14 | * For minor contributions see the git repository. |
| 15 | * |
| 16 | * Alternatively, the contents of this file may be used under the terms |
| 17 | * of the GNU Lesser General Public License Version 2.1 or later |
| 18 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 19 | * applicable instead of those above. |
| 20 | */ |
| 21 | |
| 22 | #include <map> |
| 23 | #include <sstream> |
| 24 | |
| 25 | #include "WPSStream.h" |
| 26 | |
| 27 | #include "QuattroFormula.h" |
| 28 | |
| 29 | /** namespace to regroup data used to read QuattroPro .wb1-3, .qpw formula */ |
| 30 | namespace QuattroFormulaInternal |
| 31 | { |
| 32 | struct Functions |
| 33 | { |
| 34 | char const *m_name; |
| 35 | int m_arity; |
| 36 | }; |
| 37 | |
| 38 | struct State |
| 39 | { |
| 40 | /** constructor */ |
| 41 | State(QuattroFormulaManager::CellReferenceFunction const &readCellReference, int version) |
| 42 | : m_readCellReferenceFunction(readCellReference) |
| 43 | , m_version(version) |
| 44 | , m_idFunctionsMap() |
| 45 | , m_idToDLLName1Map() |
| 46 | , m_actDLLName1Id(-1) |
| 47 | , m_idToDLLName2Map() |
| 48 | { |
| 49 | if (m_version>=2) |
| 50 | { |
| 51 | // in .qpw, H/VLookUp have four arguments |
| 52 | m_idFunctionsMap = |
| 53 | { |
| 54 | {0x55, {"VLookUp", 4}}, |
| 55 | {0x5a, {"HLookup", 4}} |
| 56 | }; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** function to call to read a cell reference*/ |
| 61 | QuattroFormulaManager::CellReferenceFunction m_readCellReferenceFunction; |
| 62 | /** the file version: 1: .wb1-3, 2: .qpw*/ |
| 63 | int m_version; |
| 64 | /** the function which differs from default */ |
| 65 | std::map<int, Functions> m_idFunctionsMap; |
| 66 | //! map id to DLL name 1 |
| 67 | std::map<int, librevenge::RVNGString> m_idToDLLName1Map; |
| 68 | //! the current id DLL name 1 |
| 69 | int m_actDLLName1Id; |
| 70 | //! map id to DLL name2 |
| 71 | std::map<Vec2i, librevenge::RVNGString> m_idToDLLName2Map; |
| 72 | }; |
| 73 | } |
| 74 | |
| 75 | // constructor |
| 76 | QuattroFormulaManager::QuattroFormulaManager(QuattroFormulaManager::CellReferenceFunction const &readCellReference, int version) |
| 77 | : m_state(new QuattroFormulaInternal::State(readCellReference, version)) |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | void QuattroFormulaManager::addDLLIdName(int id, librevenge::RVNGString const &name, bool func1) |
| 82 | { |
| 83 | if (name.empty()) |
| 84 | { |
| 85 | WPS_DEBUG_MSG(("QuattroFormulaManager::addDLLIdName: called with empty name for id=%d\n", id)); |
| 86 | return; |
| 87 | } |
| 88 | if (func1) |
| 89 | { |
| 90 | m_state->m_actDLLName1Id=id; |
| 91 | auto &map = m_state->m_idToDLLName1Map; |
| 92 | if (map.find(id) != map.end()) |
| 93 | { |
| 94 | WPS_DEBUG_MSG(("QuattroFormulaManager::addDLLIdName: called with dupplicated id=%d\n", id)); |
| 95 | } |
| 96 | else |
| 97 | map[id]=name; |
| 98 | return; |
| 99 | } |
| 100 | if (m_state->m_actDLLName1Id<0) |
| 101 | { |
| 102 | WPS_DEBUG_MSG(("QuattroFormulaManager::addDLLIdName: oops, unknown name1 id for %d\n", id)); |
| 103 | return; |
| 104 | } |
| 105 | auto &map = m_state->m_idToDLLName2Map; |
| 106 | Vec2i fId(m_state->m_actDLLName1Id, id); |
| 107 | if (map.find(fId) != map.end()) |
| 108 | { |
| 109 | WPS_DEBUG_MSG(("QuattroFormulaManager::addDLLIdName: called with dupplicated id=%d,%d\n", m_state->m_actDLLName1Id, id)); |
| 110 | } |
| 111 | else |
| 112 | map[fId]=name; |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | //------------------------------------------------------------ |
| 117 | // read a formula |
| 118 | //------------------------------------------------------------ |
| 119 | namespace QuattroFormulaInternal |
| 120 | { |
| 121 | static Functions const s_listFunctions[] = |
| 122 | { |
| 123 | // 0 |
| 124 | { "", 0} /*SPEC: double*/, {"", 0}/*SPEC: cell*/, {"", 0}/*SPEC: cells*/, {"=", 1} /*SPEC: end of formula*/, |
| 125 | { "(", 1} /* SPEC: () */, {"", 0}/*SPEC: int*/, { "", -2} /*SPEC: text*/, {"", -2} /*SPEC: default argument*/, |
| 126 | { "-", 1}, {"+", 2}, {"-", 2}, {"*", 2}, |
| 127 | { "/", 2}, { "^", 2}, {"=", 2}, {"<>", 2}, |
| 128 | |
| 129 | // 1 |
| 130 | { "<=", 2},{ ">=", 2},{ "<", 2},{ ">", 2}, |
| 131 | { "And", 2},{ "Or", 2}, { "Not", 1}, { "+", 1}, |
| 132 | { "&", 2}, { "", -2} /*halt*/, { "DLL", 0} /*DLL*/,{ "", -2} /*extended noop: 1b00011c020400020000000 means A*/, |
| 133 | { "", -2} /*extended op*/,{ "", -2} /*reserved*/,{ "", -2} /*reserved*/,{ "NA", 0} /*checkme*/, |
| 134 | |
| 135 | // 2 |
| 136 | { "NA", 0} /* Error*/,{ "Abs", 1},{ "Int", 1},{ "Sqrt", 1}, |
| 137 | { "Log10", 1},{ "Ln", 1},{ "Pi", 0},{ "Sin", 1}, |
| 138 | { "Cos", 1},{ "Tan", 1},{ "Atan2", 2},{ "Atan", 1}, |
| 139 | { "Asin", 1},{ "Acos", 1},{ "Exp", 1},{ "Mod", 2}, |
| 140 | |
| 141 | // 3 |
| 142 | { "Choose", -1},{ "IsNa", 1},{ "IsError", 1},{ "False", 0}, |
| 143 | { "True", 0},{ "Rand", 0},{ "Date", 3},{ "Now", 0}, |
| 144 | { "PMT", 3} /*BAD*/,{ "QPRO_PV", 3} /*BAD*/,{ "QPRO_FV", 3} /*BAD*/,{ "IF", 3}, |
| 145 | { "Day", 1},{ "Month", 1},{ "Year", 1},{ "Round", 2}, |
| 146 | |
| 147 | // 4 |
| 148 | { "Time", 3},{ "Hour", 1},{ "Minute", 1},{ "Second", 1}, |
| 149 | { "IsNumber", 1},{ "IsText", 1},{ "Len", 1},{ "Value", 1}, |
| 150 | { "Fixed", 2}, { "Mid", 3}, { "Char", 1},{ "Ascii", 1}, |
| 151 | { "Find", 3},{ "DateValue", 1} /*checkme*/,{ "TimeValue", 1} /*checkme*/,{ "CellPointer", 1} /*checkme*/, |
| 152 | |
| 153 | // 5 |
| 154 | { "Sum", -1},{ "Average", -1},{ "COUNT", -1},{ "Min", -1}, |
| 155 | { "Max", -1},{ "VLookUp", 3},{ "NPV", 2}, { "Var", -1}, |
| 156 | { "StDev", -1},{ "IRR", 2} /*BAD*/, { "HLookup", 3},{ "DSum", 3}, |
| 157 | { "DAverage", 3},{ "DCount", 3},{ "DMin", 3},{ "DMax", 3}, |
| 158 | |
| 159 | // 6 |
| 160 | { "DVar", 3},{ "DStd", 3},{ "Index", 3} /* index2d*/, { "Columns", 1}, |
| 161 | { "Rows", 1},{ "Rept", 2},{ "Upper", 1},{ "Lower", 1}, |
| 162 | { "Left", 2},{ "Right", 2},{ "Replace", 4}, { "Proper", 1}, |
| 163 | { "Cell", 2},{ "Trim", 1},{ "Clean", 1},{ "IsText", 1}, |
| 164 | |
| 165 | // 7 |
| 166 | { "IsNonText", 1},{ "Exact", 2},{ "QPRO_Call", -2} /*UNKN*/,{ "Indirect", 1}, |
| 167 | { "RRI", 3}, { "TERM", 3}, { "CTERM", 3}, { "SLN", 3}, |
| 168 | { "SYD", 4},{ "DDB", 4}, { "StDevP", -1}, { "VarP", -1}, |
| 169 | { "DBStdDevP", 3}, { "DBVarP", 3}, { "PV", 5}, { "PMT", 5}, |
| 170 | |
| 171 | // 8 |
| 172 | { "FV", 5}, { "Nper", 5}, { "Rate", 5}/*IRate*/, { "Ipmt", 6}, |
| 173 | { "Ppmt", 6}, { "SumProduct", 2}, { "QPRO_MemAvail", 0}, { "QPRO_MememsAvail", 0}, |
| 174 | { "QPRO_FileExist", 1}, { "QPRO_CurValue", 2}, { "Degrees", 1},{ "Radians", 1}, |
| 175 | { "QPRO_Hex", 1},{ "QPRO_Num", 1},{ "Today", 0},{ "NPV", 2}, |
| 176 | |
| 177 | // 9 |
| 178 | { "QPRO_CellIndex", 4}, { "QPRO_Version", 0}, { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, |
| 179 | { "QPRO_Dhol", 3} /* fixme name: DHOL ?*/, { "", -2} /*UNKN*/, { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, |
| 180 | { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, { "Sheet", 1}, { "", -2} /*UNKN*/, |
| 181 | { "", -2} /*UNKN*/,{ "Index", 4}, { "QPRO_CellIndex3d", -2} /*UNKN*/,{ "QPRO_property", 1}, |
| 182 | |
| 183 | // a |
| 184 | {"QPRO_DDE", 4}, {"QPRO_Command", 1}, {"QPRO_Gerlinie", 3} /* fixme: name GERLINIE? */ |
| 185 | }; |
| 186 | |
| 187 | } |
| 188 | |
| 189 | bool QuattroFormulaManager::readFormula(std::shared_ptr<WPSStream> const &stream, long endPos, |
| 190 | Vec2i const &position, int sheetId, |
| 191 | std::vector<WKSContentListener::FormulaInstruction> &formula, std::string &error) const |
| 192 | { |
| 193 | RVNGInputStreamPtr input = stream->m_input; |
| 194 | libwps::DebugFile &ascFile=stream->m_ascii; |
| 195 | formula.resize(0); |
| 196 | error = ""; |
| 197 | long pos = input->tell(); |
| 198 | if (endPos - pos < 4) return false; |
| 199 | auto sz = int(libwps::readU16(input)); // max 1024 |
| 200 | if (endPos-pos-4 != sz) return false; |
| 201 | |
| 202 | std::vector<QuattroFormulaInternal::CellReference> listCellsPos; |
| 203 | auto fieldPos= int(libwps::readU16(input)); // ref begin |
| 204 | if (fieldPos<0||fieldPos>sz) |
| 205 | { |
| 206 | WPS_DEBUG_MSG(("QuattroFormulaManager::readFormula: can not find the field header\n")); |
| 207 | error="###fieldPos"; |
| 208 | return false; |
| 209 | } |
| 210 | if (fieldPos!=sz) |
| 211 | { |
| 212 | input->seek(pos+4+fieldPos, librevenge::RVNG_SEEK_SET); |
| 213 | ascFile.addDelimiter(pos+4+fieldPos,'|'); |
| 214 | while (!input->isEnd()) |
| 215 | { |
| 216 | long actPos=input->tell(); |
| 217 | if (actPos+4>endPos) break; |
| 218 | QuattroFormulaInternal::CellReference cell; |
| 219 | if (!m_state->m_readCellReferenceFunction(stream, endPos, cell, position, sheetId) || input->tell()<actPos+2) |
| 220 | { |
| 221 | input->seek(actPos, librevenge::RVNG_SEEK_SET); |
| 222 | break; |
| 223 | } |
| 224 | if (cell.empty()) |
| 225 | { |
| 226 | WPS_DEBUG_MSG(("QuattroFormulaManager::readFormula: find some deleted cells\n")); |
| 227 | } |
| 228 | else |
| 229 | listCellsPos.push_back(cell); |
| 230 | } |
| 231 | if (input->tell() !=endPos) |
| 232 | { |
| 233 | ascFile.addDelimiter(input->tell(),'@'); |
| 234 | static bool first=true; |
| 235 | if (first) |
| 236 | { |
| 237 | WPS_DEBUG_MSG(("QuattroFormulaManager::readFormula: potential formula codes\n")); |
| 238 | first=false; |
| 239 | } |
| 240 | error="###codes,"; |
| 241 | } |
| 242 | input->seek(pos+4, librevenge::RVNG_SEEK_SET); |
| 243 | endPos=pos+4+fieldPos; |
| 244 | } |
| 245 | std::stringstream f; |
| 246 | std::vector<std::vector<WKSContentListener::FormulaInstruction> > stack; |
| 247 | bool ok = true; |
| 248 | size_t actCellId=0; |
| 249 | int numDefault=0; |
| 250 | while (long(input->tell()) != endPos) |
| 251 | { |
| 252 | double val; |
| 253 | bool isNaN; |
| 254 | pos = input->tell(); |
| 255 | if (pos > endPos) return false; |
| 256 | auto wh = int(libwps::readU8(input)); |
| 257 | int arity = 0; |
| 258 | WKSContentListener::FormulaInstruction instr; |
| 259 | bool noInstr=false; |
| 260 | switch (wh) |
| 261 | { |
| 262 | case 0x0: |
| 263 | if (endPos-pos<9 || !libwps::readDouble8(input, val, isNaN)) |
| 264 | { |
| 265 | f.str(""); |
| 266 | f << "###number"; |
| 267 | error=f.str(); |
| 268 | ok = false; |
| 269 | break; |
| 270 | } |
| 271 | instr.m_type=WKSContentListener::FormulaInstruction::F_Double; |
| 272 | instr.m_doubleValue=val; |
| 273 | break; |
| 274 | case 0x1: |
| 275 | if (actCellId>=listCellsPos.size()) |
| 276 | { |
| 277 | f.str(""); |
| 278 | f << "###unknCell" << actCellId; |
| 279 | error=f.str(); |
| 280 | ok = false; |
| 281 | break; |
| 282 | } |
| 283 | stack.push_back(listCellsPos[actCellId++].m_cells); |
| 284 | noInstr=true; |
| 285 | break; |
| 286 | case 0x2: |
| 287 | if (actCellId>=listCellsPos.size()) |
| 288 | { |
| 289 | f.str(""); |
| 290 | f << "###unknListCell" << actCellId; |
| 291 | error=f.str(); |
| 292 | ok = false; |
| 293 | break; |
| 294 | } |
| 295 | stack.push_back(listCellsPos[actCellId++].m_cells); |
| 296 | noInstr=true; |
| 297 | break; |
| 298 | case 0x5: |
| 299 | instr.m_type=WKSContentListener::FormulaInstruction::F_Long; |
| 300 | instr.m_longValue=long(libwps::read16(input)); |
| 301 | break; |
| 302 | case 0x6: |
| 303 | instr.m_type=WKSContentListener::FormulaInstruction::F_Text; |
| 304 | while (!input->isEnd()) |
| 305 | { |
| 306 | if (input->tell() >= endPos) |
| 307 | { |
| 308 | ok=false; |
| 309 | break; |
| 310 | } |
| 311 | auto c = char(libwps::readU8(input)); |
| 312 | if (c==0) break; |
| 313 | instr.m_content += c; |
| 314 | } |
| 315 | break; |
| 316 | case 0x7: // maybe default parameter |
| 317 | ++numDefault; |
| 318 | noInstr=true; |
| 319 | break; |
| 320 | case 0x1a: |
| 321 | { |
| 322 | if (input->tell()+4 >= endPos) |
| 323 | { |
| 324 | ok=false; |
| 325 | break; |
| 326 | } |
| 327 | static bool first=true; |
| 328 | if (first) |
| 329 | { |
| 330 | WPS_DEBUG_MSG(("QuattroFormulaManager::readFormula: this file contains some DLL functions, the result can be bad\n")); |
| 331 | first=false; |
| 332 | } |
| 333 | arity= int(libwps::read8(input)); |
| 334 | std::stringstream s; |
| 335 | s << "DLL"; |
| 336 | int ids[2]; |
| 337 | for (auto &id : ids) id=int(libwps::readU16(input)); |
| 338 | s << "_"; |
| 339 | auto it1 = m_state->m_idToDLLName1Map.find(ids[0]); |
| 340 | if (it1!= m_state->m_idToDLLName1Map.end()) |
| 341 | s << it1->second.cstr(); |
| 342 | else |
| 343 | { |
| 344 | WPS_DEBUG_MSG(("QuattroFormulaManager::readFormula: can not find DLL function0 name for id=%d\n", ids[0])); |
| 345 | s << "F" << ids[0]; |
| 346 | f << "##DLLFunc0=" << ids[0] << ","; |
| 347 | } |
| 348 | s << "_"; |
| 349 | auto it2 = m_state->m_idToDLLName2Map.find(Vec2i(ids[0],ids[1])); |
| 350 | if (it2!= m_state->m_idToDLLName2Map.end()) |
| 351 | s << it2->second.cstr(); |
| 352 | else |
| 353 | { |
| 354 | WPS_DEBUG_MSG(("QuattroFormulaManager::readFormula: can not find DLL function1 name for id=%d\n", ids[1])); |
| 355 | s << "F" << ids[1]; |
| 356 | f << "##DLLFunc1=" << ids[1] << ","; |
| 357 | } |
| 358 | instr.m_type=WKSContentListener::FormulaInstruction::F_Function; |
| 359 | instr.m_content=s.str(); |
| 360 | break; |
| 361 | } |
| 362 | default: |
| 363 | { |
| 364 | auto fIt=m_state->m_idFunctionsMap.find(wh); |
| 365 | if (fIt!=m_state->m_idFunctionsMap.end()) |
| 366 | { |
| 367 | instr.m_type=WKSContentListener::FormulaInstruction::F_Function; |
| 368 | instr.m_content=fIt->second.m_name; |
| 369 | arity = fIt->second.m_arity; |
| 370 | } |
| 371 | else if (unsigned(wh) >= WPS_N_ELEMENTS(QuattroFormulaInternal::s_listFunctions) || QuattroFormulaInternal::s_listFunctions[wh].m_arity == -2) |
| 372 | { |
| 373 | f.str(""); |
| 374 | f << "##Funct" << std::hex << wh; |
| 375 | error=f.str(); |
| 376 | ok = false; |
| 377 | break; |
| 378 | } |
| 379 | else |
| 380 | { |
| 381 | instr.m_type=WKSContentListener::FormulaInstruction::F_Function; |
| 382 | instr.m_content=QuattroFormulaInternal::s_listFunctions[wh].m_name; |
| 383 | arity = QuattroFormulaInternal::s_listFunctions[wh].m_arity; |
| 384 | } |
| 385 | ok=!instr.m_content.empty(); |
| 386 | if (arity == -1) arity = int(libwps::read8(input)); |
| 387 | break; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | if (!ok) break; |
| 392 | if (noInstr) continue; |
| 393 | std::vector<WKSContentListener::FormulaInstruction> child; |
| 394 | if (instr.m_type!=WKSContentListener::FormulaInstruction::F_Function) |
| 395 | { |
| 396 | child.push_back(instr); |
| 397 | stack.push_back(child); |
| 398 | continue; |
| 399 | } |
| 400 | size_t numElt = stack.size(); |
| 401 | arity-=numDefault; |
| 402 | numDefault=0; |
| 403 | if (arity<0 || int(numElt) < arity) |
| 404 | { |
| 405 | f.str(""); |
| 406 | f << instr.m_content << "[##" << arity << "]"; |
| 407 | error=f.str(); |
| 408 | ok = false; |
| 409 | break; |
| 410 | } |
| 411 | // |
| 412 | // first treat the special cases |
| 413 | // |
| 414 | if (arity==3 && instr.m_type==WKSContentListener::FormulaInstruction::F_Function && instr.m_content=="TERM") |
| 415 | { |
| 416 | // @TERM(pmt,pint,fv) -> NPER(pint,-pmt,pv=0,fv) |
| 417 | auto pmt=stack[size_t(int(numElt)-3)]; |
| 418 | auto pint=stack[size_t(int(numElt)-2)]; |
| 419 | auto fv=stack[size_t(int(numElt)-1)]; |
| 420 | |
| 421 | stack.resize(size_t(++numElt)); |
| 422 | // pint |
| 423 | stack[size_t(int(numElt)-4)]=pint; |
| 424 | //-pmt |
| 425 | auto &node=stack[size_t(int(numElt)-3)]; |
| 426 | instr.m_type=WKSContentListener::FormulaInstruction::F_Operator; |
| 427 | instr.m_content="-"; |
| 428 | node.resize(0); |
| 429 | node.push_back(instr); |
| 430 | instr.m_content="("; |
| 431 | node.push_back(instr); |
| 432 | node.insert(node.end(), pmt.begin(), pmt.end()); |
| 433 | instr.m_content=")"; |
| 434 | node.push_back(instr); |
| 435 | //pv=zero |
| 436 | instr.m_type=WKSContentListener::FormulaInstruction::F_Long; |
| 437 | instr.m_longValue=0; |
| 438 | stack[size_t(int(numElt)-2)].resize(0); |
| 439 | stack[size_t(int(numElt)-2)].push_back(instr); |
| 440 | //fv |
| 441 | stack[size_t(int(numElt)-1)]=fv; |
| 442 | arity=4; |
| 443 | instr.m_type=WKSContentListener::FormulaInstruction::F_Function; |
| 444 | instr.m_content="NPER"; |
| 445 | } |
| 446 | else if (arity==3 && instr.m_type==WKSContentListener::FormulaInstruction::F_Function && instr.m_content=="CTERM") |
| 447 | { |
| 448 | // @CTERM(pint,fv,pv) -> NPER(pint,pmt=0,-pv,fv) |
| 449 | auto pint=stack[size_t(int(numElt)-3)]; |
| 450 | auto fv=stack[size_t(int(numElt)-2)]; |
| 451 | auto pv=stack[size_t(int(numElt)-1)]; |
| 452 | stack.resize(size_t(++numElt)); |
| 453 | // pint |
| 454 | stack[size_t(int(numElt)-4)]=pint; |
| 455 | // pmt=0 |
| 456 | instr.m_type=WKSContentListener::FormulaInstruction::F_Long; |
| 457 | instr.m_longValue=0; |
| 458 | stack[size_t(int(numElt)-3)].resize(0); |
| 459 | stack[size_t(int(numElt)-3)].push_back(instr); |
| 460 | // -pv |
| 461 | auto &node=stack[size_t(int(numElt)-2)]; |
| 462 | instr.m_type=WKSContentListener::FormulaInstruction::F_Operator; |
| 463 | instr.m_content="-"; |
| 464 | node.resize(0); |
| 465 | node.push_back(instr); |
| 466 | instr.m_content="("; |
| 467 | node.push_back(instr); |
| 468 | node.insert(node.end(), pv.begin(), pv.end()); |
| 469 | instr.m_content=")"; |
| 470 | node.push_back(instr); |
| 471 | |
| 472 | //fv |
| 473 | stack[size_t(int(numElt)-1)]=fv; |
| 474 | arity=4; |
| 475 | instr.m_type=WKSContentListener::FormulaInstruction::F_Function; |
| 476 | instr.m_content="NPER"; |
| 477 | } |
| 478 | |
| 479 | if ((instr.m_content[0] >= 'A' && instr.m_content[0] <= 'Z') || instr.m_content[0] == '(') |
| 480 | { |
| 481 | if (instr.m_content[0] != '(') |
| 482 | child.push_back(instr); |
| 483 | |
| 484 | instr.m_type=WKSContentListener::FormulaInstruction::F_Operator; |
| 485 | instr.m_content="("; |
| 486 | child.push_back(instr); |
| 487 | for (int i = 0; i < arity; i++) |
| 488 | { |
| 489 | if (i) |
| 490 | { |
| 491 | instr.m_content=";"; |
| 492 | child.push_back(instr); |
| 493 | } |
| 494 | auto const &node=stack[size_t(int(numElt)-arity+i)]; |
| 495 | child.insert(child.end(), node.begin(), node.end()); |
| 496 | } |
| 497 | instr.m_content=")"; |
| 498 | child.push_back(instr); |
| 499 | |
| 500 | stack.resize(size_t(int(numElt)-arity+1)); |
| 501 | stack[size_t(int(numElt)-arity)] = child; |
| 502 | continue; |
| 503 | } |
| 504 | if (arity==1) |
| 505 | { |
| 506 | instr.m_type=WKSContentListener::FormulaInstruction::F_Operator; |
| 507 | stack[numElt-1].insert(stack[numElt-1].begin(), instr); |
| 508 | if (wh==3) |
| 509 | break; |
| 510 | continue; |
| 511 | } |
| 512 | if (arity==2) |
| 513 | { |
| 514 | instr.m_type=WKSContentListener::FormulaInstruction::F_Operator; |
| 515 | stack[numElt-2].push_back(instr); |
| 516 | stack[numElt-2].insert(stack[numElt-2].end(), stack[numElt-1].begin(), stack[numElt-1].end()); |
| 517 | stack.resize(numElt-1); |
| 518 | continue; |
| 519 | } |
| 520 | ok=false; |
| 521 | error = "### unexpected arity"; |
| 522 | break; |
| 523 | } |
| 524 | |
| 525 | if (!ok) ; |
| 526 | else if (stack.size()==1 && stack[0].size()>1 && stack[0][0].m_content=="=") |
| 527 | { |
| 528 | formula.insert(formula.begin(),stack[0].begin()+1,stack[0].end()); |
| 529 | if (input->tell()!=endPos) |
| 530 | { |
| 531 | // unsure, find some text here, maybe some note |
| 532 | static bool first=true; |
| 533 | if (first) |
| 534 | { |
| 535 | WPS_DEBUG_MSG(("QuattroFormulaManager::readFormula: find some extra data\n")); |
| 536 | first=false; |
| 537 | } |
| 538 | error="##extra data"; |
| 539 | ascFile.addDelimiter(input->tell(),'#'); |
| 540 | } |
| 541 | return true; |
| 542 | } |
| 543 | else |
| 544 | error = "###stack problem"; |
| 545 | |
| 546 | static bool first = true; |
| 547 | if (first) |
| 548 | { |
| 549 | WPS_DEBUG_MSG(("QuattroFormulaManager::readFormula: I can not read some formula\n")); |
| 550 | first = false; |
| 551 | } |
| 552 | |
| 553 | f.str(""); |
| 554 | for (auto const &i : stack) |
| 555 | { |
| 556 | for (auto const &j : i) |
| 557 | f << j << ","; |
| 558 | f << "@"; |
| 559 | } |
| 560 | f << error << "###"; |
| 561 | error = f.str(); |
| 562 | return false; |
| 563 | } |
| 564 | //////////////////////////////////////////////////////////// |
| 565 | // cell reference |
| 566 | //////////////////////////////////////////////////////////// |
| 567 | namespace QuattroFormulaInternal |
| 568 | { |
| 569 | std::ostream &operator<<(std::ostream &o, CellReference const &ref) |
| 570 | { |
| 571 | if (ref.m_cells.size()==1) |
| 572 | { |
| 573 | o << ref.m_cells[0]; |
| 574 | return o; |
| 575 | } |
| 576 | o << "["; |
| 577 | for (auto const &r: ref.m_cells) o << r; |
| 578 | o << "]"; |
| 579 | return o; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |
| 584 |