1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 #ifndef SC_DOCITER_HXX 25 #define SC_DOCITER_HXX 26 27 #include "address.hxx" 28 #include <tools/solar.h> 29 #include "global.hxx" 30 #include "scdllapi.h" 31 #include "queryparam.hxx" 32 33 #include <memory> 34 35 #include <set> 36 37 class ScDocument; 38 class ScBaseCell; 39 class ScPatternAttr; 40 class ScAttrArray; 41 class ScAttrIterator; 42 class ScRange; 43 44 class ScDocumentIterator // run through all non empty cells 45 { 46 private: 47 ScDocument* pDoc; 48 SCTAB nStartTab; 49 SCTAB nEndTab; 50 51 const ScPatternAttr* pDefPattern; 52 53 SCCOL nCol; 54 SCROW nRow; 55 SCTAB nTab; 56 ScBaseCell* pCell; 57 const ScPatternAttr* pPattern; 58 59 60 SCSIZE nColPos; 61 SCSIZE nAttrPos; 62 63 sal_Bool GetThis(); 64 sal_Bool GetThisCol(); 65 66 public: 67 ScDocumentIterator( ScDocument* pDocument, SCTAB nStartTable, SCTAB nEndTable ); 68 ~ScDocumentIterator(); 69 70 sal_Bool GetFirst(); 71 sal_Bool GetNext(); 72 73 ScBaseCell* GetCell(); 74 const ScPatternAttr* GetPattern(); 75 void GetPos( SCCOL& rCol, SCROW& rRow, SCTAB& rTab ); 76 }; 77 78 class ScValueIterator // run through all numbers in one area 79 { 80 private: 81 double fNextValue; 82 ScDocument* pDoc; 83 const ScAttrArray* pAttrArray; 84 sal_uLong nNumFormat; // for CalcAsShown 85 sal_uLong nNumFmtIndex; 86 SCCOL nStartCol; 87 SCROW nStartRow; 88 SCTAB nStartTab; 89 SCCOL nEndCol; 90 SCROW nEndRow; 91 SCTAB nEndTab; 92 SCCOL nCol; 93 SCROW nRow; 94 SCTAB nTab; 95 SCSIZE nColRow; 96 SCROW nNextRow; 97 SCROW nAttrEndRow; 98 short nNumFmtType; 99 sal_Bool bNumValid; 100 sal_Bool bSubTotal; 101 sal_Bool bNextValid; 102 sal_Bool bCalcAsShown; 103 sal_Bool bTextAsZero; 104 105 sal_Bool GetThis(double& rValue, sal_uInt16& rErr); 106 public: 107 //UNUSED2008-05 ScValueIterator(ScDocument* pDocument, 108 //UNUSED2008-05 SCCOL nSCol, SCROW nSRow, SCTAB nSTab, 109 //UNUSED2008-05 SCCOL nECol, SCROW nERow, SCTAB nETab, 110 //UNUSED2008-05 sal_Bool bSTotal = sal_False, sal_Bool bTextAsZero = sal_False); 111 112 ScValueIterator(ScDocument* pDocument, 113 const ScRange& rRange, sal_Bool bSTotal = sal_False, 114 sal_Bool bTextAsZero = sal_False ); 115 void GetCurNumFmtInfo( short& nType, sal_uLong& nIndex ); 116 /// Does NOT reset rValue if no value found! 117 sal_Bool GetFirst(double& rValue, sal_uInt16& rErr); 118 /// Does NOT reset rValue if no value found! GetNext(double & rValue,sal_uInt16 & rErr)119 sal_Bool GetNext(double& rValue, sal_uInt16& rErr) 120 { 121 return bNextValid ? ( bNextValid = sal_False, rValue = fNextValue, 122 rErr = 0, nRow = nNextRow, 123 ++nColRow, bNumValid = sal_False, sal_True ) 124 : ( ++nRow, GetThis(rValue, rErr) ); 125 } 126 }; 127 128 // ============================================================================ 129 130 class ScDBQueryDataIterator 131 { 132 public: 133 struct Value 134 { 135 ::rtl::OUString maString; 136 double mfValue; 137 sal_uInt16 mnError; 138 bool mbIsNumber; 139 140 Value(); 141 }; 142 143 private: 144 static SCROW GetRowByColEntryIndex(ScDocument& rDoc, SCTAB nTab, SCCOL nCol, SCSIZE nColRow); 145 static ScBaseCell* GetCellByColEntryIndex(ScDocument& rDoc, SCTAB nTab, SCCOL nCol, SCSIZE nColRow); 146 static ScAttrArray* GetAttrArrayByCol(ScDocument& rDoc, SCTAB nTab, SCCOL nCol); 147 static bool IsQueryValid(ScDocument& rDoc, const ScQueryParam& rParam, SCTAB nTab, SCROW nRow, ScBaseCell* pCell); 148 static SCSIZE SearchColEntryIndex(ScDocument& rDoc, SCTAB nTab, SCROW nRow, SCCOL nCol); 149 150 class DataAccess 151 { 152 public: 153 DataAccess(const ScDBQueryDataIterator* pParent); 154 virtual ~DataAccess() = 0; 155 virtual bool getCurrent(Value& rValue) = 0; 156 virtual bool getFirst(Value& rValue) = 0; 157 virtual bool getNext(Value& rValue) = 0; 158 virtual SCSIZE countRecords() = 0; 159 protected: 160 const ScDBQueryDataIterator* mpParent; 161 }; 162 163 class DataAccessInternal : public DataAccess 164 { 165 public: 166 DataAccessInternal(const ScDBQueryDataIterator* pParent, ScDBQueryParamInternal* pParam, ScDocument* pDoc); 167 virtual ~DataAccessInternal(); 168 virtual bool getCurrent(Value& rValue); 169 virtual bool getFirst(Value& rValue); 170 virtual bool getNext(Value& rValue); 171 virtual SCSIZE countRecords(); 172 173 private: 174 ScDBQueryParamInternal* mpParam; 175 ScDocument* mpDoc; 176 const ScAttrArray* pAttrArray; 177 sal_uLong nNumFormat; // for CalcAsShown 178 sal_uLong nNumFmtIndex; 179 SCCOL nCol; 180 SCROW nRow; 181 SCSIZE nColRow; 182 SCROW nAttrEndRow; 183 SCTAB nTab; 184 short nNumFmtType; 185 bool bCalcAsShown; 186 }; 187 188 class DataAccessMatrix : public DataAccess 189 { 190 public: 191 DataAccessMatrix(const ScDBQueryDataIterator* pParent, ScDBQueryParamMatrix* pParam); 192 virtual ~DataAccessMatrix(); 193 virtual bool getCurrent(Value& rValue); 194 virtual bool getFirst(Value& rValue); 195 virtual bool getNext(Value& rValue); 196 virtual SCSIZE countRecords(); 197 198 private: 199 bool isValidQuery(SCROW mnRow, const ScMatrix& rMat) const; 200 201 ScDBQueryParamMatrix* mpParam; 202 SCROW mnCurRow; 203 SCROW mnRows; 204 SCCOL mnCols; 205 }; 206 207 ::std::unique_ptr<ScDBQueryParamBase> mpParam; 208 ::std::unique_ptr<DataAccess> mpData; 209 210 public: 211 ScDBQueryDataIterator(ScDocument* pDocument, ScDBQueryParamBase* pParam); 212 /// Does NOT reset rValue if no value found! 213 bool GetFirst(Value& rValue); 214 /// Does NOT reset rValue if no value found! 215 bool GetNext(Value& rValue); 216 /// Number of records matching the query, whatever their cells contain. 217 SCSIZE CountRecords(); 218 }; 219 220 // ============================================================================ 221 222 class ScCellIterator // run through all cell in one Area 223 { // at SubTotal, skip hidden and 224 private: // SubTotal line 225 ScDocument* pDoc; 226 SCCOL nStartCol; 227 SCROW nStartRow; 228 SCTAB nStartTab; 229 SCCOL nEndCol; 230 SCROW nEndRow; 231 SCTAB nEndTab; 232 SCCOL nCol; 233 SCROW nRow; 234 SCTAB nTab; 235 SCSIZE nColRow; 236 sal_Bool bSubTotal; 237 238 ScBaseCell* GetThis(); 239 public: 240 ScCellIterator(ScDocument* pDocument, 241 SCCOL nSCol, SCROW nSRow, SCTAB nSTab, 242 SCCOL nECol, SCROW nERow, SCTAB nETab, 243 sal_Bool bSTotal = sal_False); 244 ScCellIterator(ScDocument* pDocument, 245 const ScRange& rRange, sal_Bool bSTotal = sal_False); 246 ScBaseCell* GetFirst(); 247 ScBaseCell* GetNext(); GetCol() const248 SCCOL GetCol() const { return nCol; } GetRow() const249 SCROW GetRow() const { return nRow; } GetTab() const250 SCTAB GetTab() const { return nTab; } GetPos() const251 ScAddress GetPos() const { return ScAddress( nCol, nRow, nTab ); } 252 }; 253 254 class ScQueryCellIterator // run through all not empty cells in an area 255 { 256 enum StopOnMismatchBits 257 { 258 nStopOnMismatchDisabled = 0x00, 259 nStopOnMismatchEnabled = 0x01, 260 nStopOnMismatchOccured = 0x02, 261 nStopOnMismatchExecuted = nStopOnMismatchEnabled | nStopOnMismatchOccured 262 }; 263 264 enum TestEqualConditionBits 265 { 266 nTestEqualConditionDisabled = 0x00, 267 nTestEqualConditionEnabled = 0x01, 268 nTestEqualConditionMatched = 0x02, 269 nTestEqualConditionFulfilled = nTestEqualConditionEnabled | nTestEqualConditionMatched 270 }; 271 272 private: 273 ScQueryParam aParam; 274 ScDocument* pDoc; 275 const ScAttrArray* pAttrArray; 276 sal_uLong nNumFormat; 277 SCTAB nTab; 278 SCCOL nCol; 279 SCROW nRow; 280 SCSIZE nColRow; 281 SCROW nAttrEndRow; 282 sal_uInt8 nStopOnMismatch; 283 sal_uInt8 nTestEqualCondition; 284 sal_Bool bAdvanceQuery; 285 sal_Bool bIgnoreMismatchOnLeadingStrings; 286 287 ScBaseCell* GetThis(); 288 289 /* Only works if no regular expression is involved, only 290 searches for rows in one column, and only the first 291 query entry is considered with simple conditions 292 SC_LESS_EQUAL (sorted ascending) or SC_GREATER_EQUAL 293 (sorted descending). Check these things before 294 invocation! Delivers a starting point, continue with 295 GetThis() and GetNext() afterwards. Introduced for 296 FindEqualOrSortedLastInRange() 297 */ 298 ScBaseCell* BinarySearch(); 299 300 public: 301 ScQueryCellIterator(ScDocument* pDocument, SCTAB nTable, 302 const ScQueryParam& aParam, sal_Bool bMod = sal_True); 303 // for bMod = sal_False has to be QueryParam be filled 304 // (bIsString) 305 ScBaseCell* GetFirst(); 306 ScBaseCell* GetNext(); GetCol()307 SCCOL GetCol() { return nCol; } GetRow()308 SCROW GetRow() { return nRow; } 309 310 // move Entry.nField by one, if row 311 // changes, for ScInterpreter ScHLookup() SetAdvanceQueryParamEntryField(sal_Bool bVal)312 void SetAdvanceQueryParamEntryField( sal_Bool bVal ) 313 { bAdvanceQuery = bVal; } 314 void AdvanceQueryParamEntryField(); 315 316 /** If set, iterator stops on first non-matching cell 317 content. May be used in SC_LESS_EQUAL queries where a 318 cell range is assumed to be sorted; stops on first 319 value being greater than the queried value and 320 GetFirst()/GetNext() return NULL. StoppedOnMismatch() 321 returns sal_True then. 322 However, the iterator's conditions are not set to end 323 all queries, GetCol() and GetRow() return values for 324 the non-matching cell, further GetNext() calls may be 325 executed. */ SetStopOnMismatch(sal_Bool bVal)326 void SetStopOnMismatch( sal_Bool bVal ) 327 { 328 nStopOnMismatch = sal::static_int_cast<sal_uInt8>(bVal ? nStopOnMismatchEnabled : 329 nStopOnMismatchDisabled); 330 } StoppedOnMismatch() const331 sal_Bool StoppedOnMismatch() const 332 { return nStopOnMismatch == nStopOnMismatchExecuted; } 333 334 /** If set, an additional test for SC_EQUAL condition is 335 executed in ScTable::ValidQuery() if SC_LESS_EQUAL or 336 SC_GREATER_EQUAL conditions are to be tested. May be 337 used where a cell range is assumed to be sorted to stop 338 if an equal match is found. */ SetTestEqualCondition(sal_Bool bVal)339 void SetTestEqualCondition( sal_Bool bVal ) 340 { 341 nTestEqualCondition = sal::static_int_cast<sal_uInt8>(bVal ? 342 nTestEqualConditionEnabled : 343 nTestEqualConditionDisabled); 344 } IsEqualConditionFulfilled() const345 sal_Bool IsEqualConditionFulfilled() const 346 { return nTestEqualCondition == nTestEqualConditionFulfilled; } 347 348 /** In a range assumed to be sorted find either the last of 349 a sequence of equal entries or the last being less than 350 (or greater than) the queried value. Used by the 351 interpreter for [HV]?LOOKUP() and MATCH(). Column and 352 row position of the found entry are returned, otherwise 353 invalid. 354 355 @param bSearchForEqualAfterMismatch 356 Continue searching for an equal entry even if the 357 last entry matching the range was found, in case 358 the data is not sorted. Is always done if regular 359 expressions are involved. 360 361 @param bIgnoreMismatchOnLeadingStrings 362 Normally strings are sorted behind numerical 363 values. If this parameter is sal_True, the search does 364 not stop when encountering a string and does not 365 assume that no values follow anymore. 366 If querying for a string a mismatch on the first 367 entry, e.g. column header, is ignored. 368 369 @ATTENTION! StopOnMismatch, TestEqualCondition and 370 the internal IgnoreMismatchOnLeadingStrings and query 371 params are in an undefined state upon return! The 372 iterator is not usable anymore except for obtaining the 373 number format! 374 */ 375 sal_Bool FindEqualOrSortedLastInRange( SCCOL& nFoundCol, 376 SCROW& nFoundRow, sal_Bool bSearchForEqualAfterMismatch = sal_False, 377 sal_Bool bIgnoreMismatchOnLeadingStrings = sal_True ); 378 }; 379 380 class ScDocAttrIterator // all Attribut-Areas 381 { 382 private: 383 ScDocument* pDoc; 384 SCTAB nTab; 385 SCCOL nEndCol; 386 SCROW nStartRow; 387 SCROW nEndRow; 388 SCCOL nCol; 389 ScAttrIterator* pColIter; 390 391 public: 392 ScDocAttrIterator(ScDocument* pDocument, SCTAB nTable, 393 SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2); 394 ~ScDocAttrIterator(); 395 396 const ScPatternAttr* GetNext( SCCOL& rCol, SCROW& rRow1, SCROW& rRow2 ); 397 }; 398 399 class ScAttrRectIterator // all Attribute-Areas, including Areas containing multiple rows 400 { 401 private: 402 ScDocument* pDoc; 403 SCTAB nTab; 404 SCCOL nEndCol; 405 SCROW nStartRow; 406 SCROW nEndRow; 407 SCCOL nIterStartCol; 408 SCCOL nIterEndCol; 409 ScAttrIterator* pColIter; 410 411 public: 412 ScAttrRectIterator(ScDocument* pDocument, SCTAB nTable, 413 SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2); 414 ~ScAttrRectIterator(); 415 416 void DataChanged(); 417 const ScPatternAttr* GetNext( SCCOL& rCol1, SCCOL& rCol2, SCROW& rRow1, SCROW& rRow2 ); 418 }; 419 420 class ScHorizontalCellIterator // run through all not empty cells, line by line 421 { 422 private: 423 ScDocument* pDoc; 424 SCTAB nTab; 425 SCCOL nStartCol; 426 SCCOL nEndCol; 427 SCROW nStartRow; 428 SCROW nEndRow; 429 SCROW* pNextRows; 430 SCSIZE* pNextIndices; 431 SCCOL nCol; 432 SCROW nRow; 433 sal_Bool bMore; 434 435 public: 436 ScHorizontalCellIterator(ScDocument* pDocument, SCTAB nTable, 437 SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2); 438 ~ScHorizontalCellIterator(); 439 440 ScBaseCell* GetNext( SCCOL& rCol, SCROW& rRow ); 441 sal_Bool ReturnNext( SCCOL& rCol, SCROW& rRow ); 442 /// Set a(nother) sheet and (re)init. 443 void SetTab( SCTAB nTab ); 444 445 private: 446 void Advance(); 447 }; 448 449 450 /** Row-wise value iterator. */ 451 class ScHorizontalValueIterator 452 { 453 private: 454 ScDocument *pDoc; 455 const ScAttrArray *pAttrArray; 456 ScHorizontalCellIterator *pCellIter; 457 sal_uLong nNumFormat; // for CalcAsShown 458 sal_uLong nNumFmtIndex; 459 SCTAB nEndTab; 460 SCCOL nCurCol; 461 SCROW nCurRow; 462 SCTAB nCurTab; 463 SCROW nAttrEndRow; 464 short nNumFmtType; 465 bool bNumValid; 466 bool bSubTotal; 467 bool bCalcAsShown; 468 bool bTextAsZero; 469 470 public: 471 472 ScHorizontalValueIterator( ScDocument* pDocument, 473 const ScRange& rRange, 474 bool bSTotal = false, 475 bool bTextAsZero = false ); 476 ~ScHorizontalValueIterator(); 477 void GetCurNumFmtInfo( short& nType, sal_uLong& nIndex ); 478 /// Does NOT reset rValue if no value found! 479 bool GetNext( double& rValue, sal_uInt16& rErr ); 480 }; 481 482 483 // 484 // returns all areas that do not have a Default-Formatting (horizontal) 485 // 486 487 class ScHorizontalAttrIterator 488 { 489 private: 490 ScDocument* pDoc; 491 SCTAB nTab; 492 SCCOL nStartCol; 493 SCROW nStartRow; 494 SCCOL nEndCol; 495 SCROW nEndRow; 496 497 SCROW* pNextEnd; 498 SCSIZE* pIndices; 499 const ScPatternAttr** ppPatterns; 500 SCCOL nCol; 501 SCROW nRow; 502 sal_Bool bRowEmpty; 503 504 public: 505 ScHorizontalAttrIterator( ScDocument* pDocument, SCTAB nTable, 506 SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2 ); 507 ~ScHorizontalAttrIterator(); 508 509 const ScPatternAttr* GetNext( SCCOL& rCol1, SCCOL& rCol2, SCROW& rRow ); 510 }; 511 512 // 513 // returns all non empty cells and areas with formatting (horizontal) 514 // 515 516 class SC_DLLPUBLIC ScUsedAreaIterator 517 { 518 private: 519 ScHorizontalCellIterator aCellIter; 520 ScHorizontalAttrIterator aAttrIter; 521 522 SCCOL nNextCol; 523 SCROW nNextRow; 524 525 SCCOL nCellCol; 526 SCROW nCellRow; 527 const ScBaseCell* pCell; 528 SCCOL nAttrCol1; 529 SCCOL nAttrCol2; 530 SCROW nAttrRow; 531 const ScPatternAttr* pPattern; 532 533 SCCOL nFoundStartCol; // results after GetNext 534 SCCOL nFoundEndCol; 535 SCROW nFoundRow; 536 const ScPatternAttr* pFoundPattern; 537 const ScBaseCell* pFoundCell; 538 539 public: 540 ScUsedAreaIterator( ScDocument* pDocument, SCTAB nTable, 541 SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2 ); 542 ~ScUsedAreaIterator(); 543 544 sal_Bool GetNext(); 545 GetStartCol() const546 SCCOL GetStartCol() const { return nFoundStartCol; } GetEndCol() const547 SCCOL GetEndCol() const { return nFoundEndCol; } GetRow() const548 SCROW GetRow() const { return nFoundRow; } GetPattern() const549 const ScPatternAttr* GetPattern() const { return pFoundPattern; } GetCell() const550 const ScBaseCell* GetCell() const { return pFoundCell; } 551 }; 552 553 // ============================================================================ 554 555 class ScRowBreakIterator 556 { 557 public: 558 static SCROW NOT_FOUND; 559 560 explicit ScRowBreakIterator(::std::set<SCROW>& rBreaks); 561 SCROW first(); 562 SCROW next(); 563 564 private: 565 ::std::set<SCROW>& mrBreaks; 566 ::std::set<SCROW>::const_iterator maItr; 567 ::std::set<SCROW>::const_iterator maEnd; 568 }; 569 570 #endif 571