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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26
27 // INCLUDE ---------------------------------------------------------------
28
29 #include "scitems.hxx"
30 #include <editeng/langitem.hxx>
31 #include <svx/algitem.hxx>
32 #include <unotools/textsearch.hxx>
33 #include <svl/zforlist.hxx>
34 #include <svl/zformat.hxx>
35 #include <tools/urlobj.hxx>
36 #include <unotools/charclass.hxx>
37 #include <sfx2/docfile.hxx>
38 #include <sfx2/printer.hxx>
39 #include <unotools/collatorwrapper.hxx>
40 #include <unotools/transliterationwrapper.hxx>
41 #include <rtl/ustring.hxx>
42 #include <rtl/logfile.hxx>
43 #include <rtl/random.h>
44 #include <unicode/uchar.h>
45
46 #include "interpre.hxx"
47 #include "patattr.hxx"
48 #include "global.hxx"
49 #include "document.hxx"
50 #include "dociter.hxx"
51 #include "cell.hxx"
52 #include "scmatrix.hxx"
53 #include "docoptio.hxx"
54 #include "globstr.hrc"
55 #include "attrib.hxx"
56 #include "jumpmatrix.hxx"
57
58 #ifndef _COMPHELPER_PROCESSFACTORY_HXX_
59 #include <comphelper/processfactory.hxx>
60 #endif
61
62 #include <stdlib.h>
63 #include <string.h>
64 #include <math.h>
65 #include <vector>
66 #include <memory>
67 #include "cellkeytranslator.hxx"
68 #include "lookupcache.hxx"
69 #include "rangenam.hxx"
70 #include "compiler.hxx"
71 #include "externalrefmgr.hxx"
72 #include "doubleref.hxx"
73 #include "queryparam.hxx"
74
75 #include <cmath>
76
77 #define SC_DOUBLE_MAXVALUE 1.7e307
78
79 IMPL_FIXEDMEMPOOL_NEWDEL( ScTokenStack, 8, 4 )
80 IMPL_FIXEDMEMPOOL_NEWDEL( ScInterpreter, 32, 16 )
81
82 ScTokenStack* ScInterpreter::pGlobalStack = NULL;
83 sal_Bool ScInterpreter::bGlobalStackInUse = sal_False;
84
85 using namespace formula;
86 using ::std::unique_ptr;
87
88 //-----------------------------------------------------------------------------
89 // Funktionen
90 //-----------------------------------------------------------------------------
91
92
ScIfJump()93 void ScInterpreter::ScIfJump()
94 {
95 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScIfJump" );
96 const short* pJump = pCur->GetJump();
97 short nJumpCount = pJump[ 0 ];
98 MatrixDoubleRefToMatrix();
99 switch ( GetStackType() )
100 {
101 case svMatrix:
102 {
103 ScMatrixRef pMat = PopMatrix();
104 if ( !pMat )
105 PushIllegalParameter();
106 else
107 {
108 FormulaTokenRef xNew;
109 ScTokenMatrixMap::const_iterator aMapIter;
110 // DoubleError handled by JumpMatrix
111 pMat->SetErrorInterpreter( NULL);
112 SCSIZE nCols, nRows;
113 pMat->GetDimensions( nCols, nRows );
114 if ( nCols == 0 || nRows == 0 )
115 PushIllegalArgument();
116 else if (pTokenMatrixMap && ((aMapIter = pTokenMatrixMap->find(
117 pCur)) != pTokenMatrixMap->end()))
118 xNew = (*aMapIter).second;
119 else
120 {
121 ScJumpMatrix* pJumpMat = new ScJumpMatrix( nCols, nRows );
122 for ( SCSIZE nC=0; nC < nCols; ++nC )
123 {
124 for ( SCSIZE nR=0; nR < nRows; ++nR )
125 {
126 double fVal;
127 bool bTrue;
128 ScMatValType nType = 0;
129 const ScMatrixValue* pMatVal = pMat->Get( nC, nR,
130 nType);
131 bool bIsValue = ScMatrix::IsValueType( nType);
132 if ( bIsValue )
133 {
134 fVal = pMatVal->fVal;
135 bIsValue = ::rtl::math::isFinite( fVal );
136 bTrue = bIsValue && (fVal != 0.0);
137 if ( bTrue )
138 fVal = 1.0;
139 }
140 else
141 {
142 // Treat empty and empty path as 0, but string
143 // as error.
144 bIsValue = !ScMatrix::IsRealStringType( nType);
145 bTrue = false;
146 fVal = (bIsValue ? 0.0 : CreateDoubleError( errNoValue));
147 }
148 if ( bTrue )
149 { // TRUE
150 if( nJumpCount >= 2 )
151 { // THEN path
152 pJumpMat->SetJump( nC, nR, fVal,
153 pJump[ 1 ],
154 pJump[ nJumpCount ]);
155 }
156 else
157 { // no parameter given for THEN
158 pJumpMat->SetJump( nC, nR, fVal,
159 pJump[ nJumpCount ],
160 pJump[ nJumpCount ]);
161 }
162 }
163 else
164 { // FALSE
165 if( nJumpCount == 3 && bIsValue )
166 { // ELSE path
167 pJumpMat->SetJump( nC, nR, fVal,
168 pJump[ 2 ],
169 pJump[ nJumpCount ]);
170 }
171 else
172 { // no parameter given for ELSE,
173 // or DoubleError
174 pJumpMat->SetJump( nC, nR, fVal,
175 pJump[ nJumpCount ],
176 pJump[ nJumpCount ]);
177 }
178 }
179 }
180 }
181 xNew = new ScJumpMatrixToken( pJumpMat );
182 GetTokenMatrixMap().insert( ScTokenMatrixMap::value_type(pCur, xNew));
183 }
184 PushTempToken( xNew);
185 // set endpoint of path for main code line
186 aCode.Jump( pJump[ nJumpCount ], pJump[ nJumpCount ] );
187 }
188 }
189 break;
190 default:
191 {
192 if ( GetBool() )
193 { // TRUE
194 if( nJumpCount >= 2 )
195 { // THEN path
196 aCode.Jump( pJump[ 1 ], pJump[ nJumpCount ] );
197 }
198 else
199 { // no parameter given for THEN
200 nFuncFmtType = NUMBERFORMAT_LOGICAL;
201 PushInt(1);
202 aCode.Jump( pJump[ nJumpCount ], pJump[ nJumpCount ] );
203 }
204 }
205 else
206 { // FALSE
207 if( nJumpCount == 3 )
208 { // ELSE path
209 aCode.Jump( pJump[ 2 ], pJump[ nJumpCount ] );
210 }
211 else
212 { // no parameter given for ELSE
213 nFuncFmtType = NUMBERFORMAT_LOGICAL;
214 PushInt(0);
215 aCode.Jump( pJump[ nJumpCount ], pJump[ nJumpCount ] );
216 }
217 }
218 }
219 }
220 }
221
222
ScChoseJump()223 void ScInterpreter::ScChoseJump()
224 {
225 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScChoseJump" );
226 // We have to set a jump, if there was none chosen because of an error set
227 // it to endpoint.
228 bool bHaveJump = false;
229 const short* pJump = pCur->GetJump();
230 short nJumpCount = pJump[ 0 ];
231 MatrixDoubleRefToMatrix();
232 switch ( GetStackType() )
233 {
234 case svMatrix:
235 {
236 ScMatrixRef pMat = PopMatrix();
237 if ( !pMat )
238 PushIllegalParameter();
239 else
240 {
241 FormulaTokenRef xNew;
242 ScTokenMatrixMap::const_iterator aMapIter;
243 // DoubleError handled by JumpMatrix
244 pMat->SetErrorInterpreter( NULL);
245 SCSIZE nCols, nRows;
246 pMat->GetDimensions( nCols, nRows );
247 if ( nCols == 0 || nRows == 0 )
248 PushIllegalParameter();
249 else if (pTokenMatrixMap && ((aMapIter = pTokenMatrixMap->find(
250 pCur)) != pTokenMatrixMap->end()))
251 xNew = (*aMapIter).second;
252 else
253 {
254 ScJumpMatrix* pJumpMat = new ScJumpMatrix( nCols, nRows );
255 for ( SCSIZE nC=0; nC < nCols; ++nC )
256 {
257 for ( SCSIZE nR=0; nR < nRows; ++nR )
258 {
259 double fVal;
260 ScMatValType nType;
261 const ScMatrixValue* pMatVal = pMat->Get( nC, nR,
262 nType);
263 bool bIsValue = ScMatrix::IsValueType( nType);
264 if ( bIsValue )
265 {
266 fVal = pMatVal->fVal;
267 bIsValue = ::rtl::math::isFinite( fVal );
268 if ( bIsValue )
269 {
270 fVal = ::rtl::math::approxFloor( fVal);
271 if ( (fVal < 1) || (fVal >= nJumpCount))
272 {
273 bIsValue = sal_False;
274 fVal = CreateDoubleError(
275 errIllegalArgument);
276 }
277 }
278 }
279 else
280 {
281 fVal = CreateDoubleError( errNoValue);
282 }
283 if ( bIsValue )
284 {
285 pJumpMat->SetJump( nC, nR, fVal,
286 pJump[ (short)fVal ],
287 pJump[ nJumpCount ]);
288 }
289 else
290 {
291 pJumpMat->SetJump( nC, nR, fVal,
292 pJump[ nJumpCount ],
293 pJump[ nJumpCount ]);
294 }
295 }
296 }
297 xNew = new ScJumpMatrixToken( pJumpMat );
298 GetTokenMatrixMap().insert( ScTokenMatrixMap::value_type(
299 pCur, xNew));
300 }
301 PushTempToken( xNew);
302 // set endpoint of path for main code line
303 aCode.Jump( pJump[ nJumpCount ], pJump[ nJumpCount ] );
304 bHaveJump = true;
305 }
306 }
307 break;
308 default:
309 {
310 double nJumpIndex = ::rtl::math::approxFloor( GetDouble() );
311 if (!nGlobalError && (nJumpIndex >= 1) && (nJumpIndex < nJumpCount))
312 {
313 aCode.Jump( pJump[ (short) nJumpIndex ], pJump[ nJumpCount ] );
314 bHaveJump = true;
315 }
316 else
317 PushIllegalArgument();
318 }
319 }
320 if (!bHaveJump)
321 aCode.Jump( pJump[ nJumpCount ], pJump[ nJumpCount ] );
322 }
323
lcl_IsIfnaHit(sal_uInt16 nErr,bool bAnyError)324 static bool lcl_IsIfnaHit( sal_uInt16 nErr, bool bAnyError )
325 {
326 return bAnyError ? (nErr != 0) : (nErr == NOTAVAILABLE);
327 }
328
ScIfna()329 void ScInterpreter::ScIfna()
330 {
331 ScIfnaOrError( false );
332 }
333
ScIfError()334 void ScInterpreter::ScIfError()
335 {
336 ScIfnaOrError( true );
337 }
338
ScIfnaOrError(bool bAnyError)339 void ScInterpreter::ScIfnaOrError( bool bAnyError )
340 {
341 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScIfnaOrError" );
342 const short* pJump = pCur->GetJump();
343 bool bIsNA = false;
344 MatrixDoubleRefToMatrix();
345 // the value path keeps the format of the value
346 nFuncFmtType = nCurFmtType;
347 nFuncFmtIndex = nCurFmtIndex;
348
349 switch ( GetStackType() )
350 {
351 case svSingleRef :
352 case svDoubleRef :
353 {
354 ScAddress aAdr;
355 PopDoubleRefOrSingleRef( aAdr );
356 if ( lcl_IsIfnaHit( nGlobalError, bAnyError ) )
357 bIsNA = true;
358 else if ( nGlobalError )
359 PushError( nGlobalError );
360 else if ( lcl_IsIfnaHit( GetCellErrCode( GetCell( aAdr ) ), bAnyError ) )
361 bIsNA = true;
362 else
363 PushCellResultToken( false, aAdr, &nFuncFmtType, &nFuncFmtIndex );
364 }
365 break;
366 case svRefList :
367 // No per-element #N/A substitution, the value passes through.
368 break;
369 case svMatrix :
370 {
371 ScMatrixRef pMat = PopMatrix();
372 SCSIZE nCols = 0, nRows = 0;
373 if ( pMat )
374 pMat->GetDimensions( nCols, nRows );
375 if ( !nCols || !nRows )
376 {
377 PushIllegalParameter();
378 aCode.Jump( pJump[ 2 ], pJump[ 2 ] );
379 return;
380 }
381 FormulaTokenRef xNew;
382 ScTokenMatrixMap::const_iterator aMapIter;
383 if ( pTokenMatrixMap && ((aMapIter = pTokenMatrixMap->find( pCur )) != pTokenMatrixMap->end()) )
384 xNew = (*aMapIter).second;
385 else
386 {
387 bool bAnyNA = false;
388 for ( SCSIZE nC = 0; nC < nCols && !bAnyNA; ++nC )
389 for ( SCSIZE nR = 0; nR < nRows && !bAnyNA; ++nR )
390 bAnyNA = pMat->IsValue( nC, nR ) && lcl_IsIfnaHit( pMat->GetError( nC, nR ), bAnyError );
391 if ( !bAnyNA )
392 {
393 // nothing to substitute, the fallback is not evaluated
394 PushMatrix( pMat );
395 aCode.Jump( pJump[ 2 ], pJump[ 2 ] );
396 return;
397 }
398 // DoubleError handled by JumpMatrix
399 pMat->SetErrorInterpreter( NULL );
400 ScJumpMatrix* pJumpMat = new ScJumpMatrix( nCols, nRows );
401 pJumpMat->SetKeepNoPath();
402 ScMatrix* pRes = pJumpMat->GetResultMatrix();
403 for ( SCSIZE nC = 0; nC < nCols; ++nC )
404 {
405 for ( SCSIZE nR = 0; nR < nRows; ++nR )
406 {
407 if ( pMat->IsValue( nC, nR ) && lcl_IsIfnaHit( pMat->GetError( nC, nR ), bAnyError ) )
408 pJumpMat->SetJump( nC, nR, 1.0, pJump[ 1 ], pJump[ 2 ] );
409 else
410 {
411 ScMatValType nType = 0;
412 const ScMatrixValue* pMatVal = pMat->Get( nC, nR, nType );
413 if ( ScMatrix::IsValueType( nType ) )
414 pRes->PutDouble( pMatVal->fVal, nC, nR );
415 else if ( ScMatrix::IsRealStringType( nType ) )
416 pRes->PutString( pMat->GetString( nC, nR ), nC, nR );
417 else
418 pRes->PutEmpty( nC, nR );
419 pJumpMat->SetJump( nC, nR, 1.0, pJump[ 2 ], pJump[ 2 ] );
420 }
421 }
422 }
423 xNew = new ScJumpMatrixToken( pJumpMat );
424 GetTokenMatrixMap().insert( ScTokenMatrixMap::value_type( pCur, xNew ) );
425 }
426 PushTempToken( xNew );
427 // set endpoint of path for main code line
428 aCode.Jump( pJump[ 2 ], pJump[ 2 ] );
429 return;
430 }
431 default:
432 if ( lcl_IsIfnaHit( nGlobalError, bAnyError ) || (GetStackType() == svError &&
433 lcl_IsIfnaHit( pStack[ sp - 1 ]->GetError(), bAnyError )) )
434 {
435 Pop();
436 bIsNA = true;
437 }
438 }
439
440 if ( bIsNA )
441 {
442 nGlobalError = 0;
443 nFuncFmtType = NUMBERFORMAT_NUMBER;
444 nFuncFmtIndex = 0;
445 aCode.Jump( pJump[ 1 ], pJump[ 2 ] );
446 }
447 else
448 aCode.Jump( pJump[ 2 ], pJump[ 2 ] );
449 }
450
lcl_AdjustJumpMatrix(ScJumpMatrix * pJumpM,ScMatrixRef & pResMat,SCSIZE nParmCols,SCSIZE nParmRows)451 void lcl_AdjustJumpMatrix( ScJumpMatrix* pJumpM, ScMatrixRef& pResMat, SCSIZE nParmCols, SCSIZE nParmRows )
452 {
453 SCSIZE nJumpCols, nJumpRows;
454 SCSIZE nResCols, nResRows;
455 SCSIZE nAdjustCols, nAdjustRows;
456 pJumpM->GetDimensions( nJumpCols, nJumpRows );
457 pJumpM->GetResMatDimensions( nResCols, nResRows );
458 if (( nJumpCols == 1 && nParmCols > nResCols ) ||
459 ( nJumpRows == 1 && nParmRows > nResRows ))
460 {
461 if ( nJumpCols == 1 && nJumpRows == 1 )
462 {
463 nAdjustCols = nParmCols > nResCols ? nParmCols : nResCols;
464 nAdjustRows = nParmRows > nResRows ? nParmRows : nResRows;
465 }
466 else if ( nJumpCols == 1 )
467 {
468 nAdjustCols = nParmCols;
469 nAdjustRows = nResRows;
470 }
471 else
472 {
473 nAdjustCols = nResCols;
474 nAdjustRows = nParmRows;
475 }
476 pJumpM->SetNewResMat( nAdjustCols, nAdjustRows );
477 pResMat = pJumpM->GetResultMatrix();
478 }
479 }
480
JumpMatrix(short nStackLevel)481 bool ScInterpreter::JumpMatrix( short nStackLevel )
482 {
483 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::JumpMatrix" );
484 pJumpMatrix = static_cast<ScToken*>(pStack[sp-nStackLevel])->GetJumpMatrix();
485 ScMatrixRef pResMat = pJumpMatrix->GetResultMatrix();
486 SCSIZE nC, nR;
487 if ( nStackLevel == 2 )
488 {
489 if ( aCode.HasStacked() )
490 aCode.Pop(); // pop what Jump() pushed
491 else
492 {
493 DBG_ERRORFILE( "ScInterpreter::JumpMatrix: pop goes the weasel" );
494 }
495
496 if ( !pResMat )
497 {
498 Pop();
499 SetError( errUnknownStackVariable );
500 }
501 else
502 {
503 pJumpMatrix->GetPos( nC, nR );
504 switch ( GetStackType() )
505 {
506 case svDouble:
507 {
508 double fVal = GetDouble();
509 if ( nGlobalError )
510 {
511 fVal = CreateDoubleError( nGlobalError );
512 nGlobalError = 0;
513 }
514 pResMat->PutDouble( fVal, nC, nR );
515 }
516 break;
517 case svString:
518 {
519 const String& rStr = GetString();
520 if ( nGlobalError )
521 {
522 pResMat->PutDouble( CreateDoubleError( nGlobalError),
523 nC, nR);
524 nGlobalError = 0;
525 }
526 else
527 pResMat->PutString( rStr, nC, nR );
528 }
529 break;
530 case svSingleRef:
531 {
532 ScAddress aAdr;
533 PopSingleRef( aAdr );
534 if ( nGlobalError )
535 {
536 pResMat->PutDouble( CreateDoubleError( nGlobalError),
537 nC, nR);
538 nGlobalError = 0;
539 }
540 else
541 {
542 ScBaseCell* pCell = GetCell( aAdr );
543 if (HasCellEmptyData( pCell))
544 pResMat->PutEmpty( nC, nR );
545 else if (HasCellValueData( pCell))
546 {
547 double fVal = GetCellValue( aAdr, pCell);
548 if ( nGlobalError )
549 {
550 fVal = CreateDoubleError(
551 nGlobalError);
552 nGlobalError = 0;
553 }
554 pResMat->PutDouble( fVal, nC, nR );
555 }
556 else
557 {
558 String aStr;
559 GetCellString( aStr, pCell );
560 if ( nGlobalError )
561 {
562 pResMat->PutDouble( CreateDoubleError(
563 nGlobalError), nC, nR);
564 nGlobalError = 0;
565 }
566 else
567 pResMat->PutString( aStr, nC, nR);
568 }
569 }
570 }
571 break;
572 case svDoubleRef:
573 { // upper left plus offset within matrix
574 double fVal;
575 ScRange aRange;
576 PopDoubleRef( aRange );
577 if ( nGlobalError )
578 {
579 fVal = CreateDoubleError( nGlobalError );
580 nGlobalError = 0;
581 pResMat->PutDouble( fVal, nC, nR );
582 }
583 else
584 {
585 // Do not modify the original range because we use it
586 // to adjust the size of the result matrix if necessary.
587 ScAddress aAdr( aRange.aStart);
588 sal_uLong nCol = (sal_uLong)aAdr.Col() + nC;
589 sal_uLong nRow = (sal_uLong)aAdr.Row() + nR;
590 if ((nCol > static_cast<sal_uLong>(aRange.aEnd.Col()) &&
591 aRange.aEnd.Col() != aRange.aStart.Col())
592 || (nRow > static_cast<sal_uLong>(aRange.aEnd.Row()) &&
593 aRange.aEnd.Row() != aRange.aStart.Row()))
594 {
595 fVal = CreateDoubleError( NOTAVAILABLE );
596 pResMat->PutDouble( fVal, nC, nR );
597 }
598 else
599 {
600 // Replicate column and/or row of a vector if it is
601 // one. Note that this could be a range reference
602 // that in fact consists of only one cell, e.g. A1:A1
603 if (aRange.aEnd.Col() == aRange.aStart.Col())
604 nCol = aRange.aStart.Col();
605 if (aRange.aEnd.Row() == aRange.aStart.Row())
606 nRow = aRange.aStart.Row();
607 aAdr.SetCol( static_cast<SCCOL>(nCol) );
608 aAdr.SetRow( static_cast<SCROW>(nRow) );
609 ScBaseCell* pCell = GetCell( aAdr );
610 if (HasCellEmptyData( pCell))
611 pResMat->PutEmpty( nC, nR );
612 else if (HasCellValueData( pCell))
613 {
614 double fCellVal = GetCellValue( aAdr, pCell);
615 if ( nGlobalError )
616 {
617 fCellVal = CreateDoubleError(
618 nGlobalError);
619 nGlobalError = 0;
620 }
621 pResMat->PutDouble( fCellVal, nC, nR );
622 }
623 else
624 {
625 String aStr;
626 GetCellString( aStr, pCell );
627 if ( nGlobalError )
628 {
629 pResMat->PutDouble( CreateDoubleError(
630 nGlobalError), nC, nR);
631 nGlobalError = 0;
632 }
633 else
634 pResMat->PutString( aStr, nC, nR );
635 }
636 }
637 SCSIZE nParmCols = aRange.aEnd.Col() - aRange.aStart.Col() + 1;
638 SCSIZE nParmRows = aRange.aEnd.Row() - aRange.aStart.Row() + 1;
639 lcl_AdjustJumpMatrix( pJumpMatrix, pResMat, nParmCols, nParmRows );
640 }
641 }
642 break;
643 case svMatrix:
644 { // match matrix offsets
645 double fVal;
646 ScMatrixRef pMat = PopMatrix();
647 if ( nGlobalError )
648 {
649 fVal = CreateDoubleError( nGlobalError );
650 nGlobalError = 0;
651 pResMat->PutDouble( fVal, nC, nR );
652 }
653 else if ( !pMat )
654 {
655 fVal = CreateDoubleError( errUnknownVariable );
656 pResMat->PutDouble( fVal, nC, nR );
657 }
658 else
659 {
660 SCSIZE nCols, nRows;
661 pMat->GetDimensions( nCols, nRows );
662 if ((nCols <= nC && nCols != 1) ||
663 (nRows <= nR && nRows != 1))
664 {
665 fVal = CreateDoubleError( NOTAVAILABLE );
666 pResMat->PutDouble( fVal, nC, nR );
667 }
668 else
669 {
670 if ( pMat->IsValue( nC, nR ) )
671 {
672 fVal = pMat->GetDouble( nC, nR );
673 pResMat->PutDouble( fVal, nC, nR );
674 }
675 else if ( pMat->IsEmpty( nC, nR ) )
676 pResMat->PutEmpty( nC, nR );
677 else
678 {
679 const String& rStr = pMat->GetString( nC, nR );
680 pResMat->PutString( rStr, nC, nR );
681 }
682 }
683 lcl_AdjustJumpMatrix( pJumpMatrix, pResMat, nCols, nRows );
684 }
685 }
686 break;
687 case svError:
688 {
689 PopError();
690 double fVal = CreateDoubleError( nGlobalError);
691 nGlobalError = 0;
692 pResMat->PutDouble( fVal, nC, nR );
693 }
694 break;
695 default:
696 {
697 Pop();
698 double fVal = CreateDoubleError( errIllegalArgument);
699 pResMat->PutDouble( fVal, nC, nR );
700 }
701 }
702 }
703 }
704 bool bCont = pJumpMatrix->Next( nC, nR );
705 if ( bCont )
706 {
707 double fBool;
708 short nStart, nNext, nStop;
709 pJumpMatrix->GetJump( nC, nR, fBool, nStart, nNext, nStop );
710 while ( bCont && nStart == nNext )
711 { // push all results that have no jump path
712 if ( pResMat )
713 {
714 // a sal_False without path results in an empty path value
715 if ( pJumpMatrix->IsKeepNoPath() )
716 ; // result already in place
717 else if ( fBool == 0.0 )
718 pResMat->PutEmptyPath( nC, nR );
719 else
720 pResMat->PutDouble( fBool, nC, nR );
721 }
722 bCont = pJumpMatrix->Next( nC, nR );
723 if ( bCont )
724 pJumpMatrix->GetJump( nC, nR, fBool, nStart, nNext, nStop );
725 }
726 if ( bCont && nStart != nNext )
727 {
728 const ScTokenVec* pParams = pJumpMatrix->GetJumpParameters();
729 if ( pParams )
730 {
731 for ( ScTokenVec::const_iterator i = pParams->begin();
732 i != pParams->end(); ++i )
733 {
734 // This is not the current state of the interpreter, so
735 // push without error, and elements' errors are coded into
736 // double.
737 PushWithoutError( *(*i));
738 }
739 }
740 aCode.Jump( nStart, nNext, nStop );
741 }
742 }
743 if ( !bCont )
744 { // we're done with it, throw away jump matrix, keep result
745 pJumpMatrix = NULL;
746 Pop();
747 PushMatrix( pResMat );
748 // Remove jump matrix from map and remember result matrix in case it
749 // could be reused in another path of the same condition.
750 if (pTokenMatrixMap)
751 {
752 pTokenMatrixMap->erase( pCur);
753 pTokenMatrixMap->insert( ScTokenMatrixMap::value_type( pCur,
754 pStack[sp-1]));
755 }
756 return true;
757 }
758 return false;
759 }
760
761
ScCompareOptions(ScDocument * pDoc,const ScQueryEntry & rEntry,bool bReg)762 ScCompareOptions::ScCompareOptions( ScDocument* pDoc, const ScQueryEntry& rEntry, bool bReg ) :
763 aQueryEntry(rEntry),
764 bRegEx(bReg),
765 bMatchWholeCell(pDoc->GetDocOptions().IsMatchWholeCell()),
766 bIgnoreCase(true)
767 {
768 bRegEx = (bRegEx && (aQueryEntry.eOp == SC_EQUAL || aQueryEntry.eOp == SC_NOT_EQUAL));
769 // Interpreter functions usually are case insensitive, except the simple
770 // comparison operators, for which these options aren't used. Override in
771 // struct if needed.
772 }
773
774
CompareFunc(const ScCompare & rComp,ScCompareOptions * pOptions)775 double ScInterpreter::CompareFunc( const ScCompare& rComp, ScCompareOptions* pOptions )
776 {
777 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::CompareFunc" );
778 // Keep DoubleError if encountered
779 // #i40539# if bEmpty is set, bVal/nVal are uninitialized
780 if ( !rComp.bEmpty[0] && rComp.bVal[0] && !::rtl::math::isFinite( rComp.nVal[0]))
781 return rComp.nVal[0];
782 if ( !rComp.bEmpty[1] && rComp.bVal[1] && !::rtl::math::isFinite( rComp.nVal[1]))
783 return rComp.nVal[1];
784
785 size_t nStringQuery = 0; // 0:=no, 1:=0, 2:=1
786 double fRes = 0;
787 if ( rComp.bEmpty[ 0 ] )
788 {
789 if ( rComp.bEmpty[ 1 ] )
790 ; // empty cell == empty cell, fRes 0
791 else if( rComp.bVal[ 1 ] )
792 {
793 if ( !::rtl::math::approxEqual( rComp.nVal[ 1 ], 0.0 ) )
794 {
795 if ( rComp.nVal[ 1 ] < 0.0 )
796 fRes = 1; // empty cell > -x
797 else
798 fRes = -1; // empty cell < x
799 }
800 // else: empty cell == 0.0
801 }
802 else
803 {
804 if ( rComp.pVal[ 1 ]->Len() )
805 fRes = -1; // empty cell < "..."
806 // else: empty cell == ""
807 }
808 }
809 else if ( rComp.bEmpty[ 1 ] )
810 {
811 if( rComp.bVal[ 0 ] )
812 {
813 if ( !::rtl::math::approxEqual( rComp.nVal[ 0 ], 0.0 ) )
814 {
815 if ( rComp.nVal[ 0 ] < 0.0 )
816 fRes = -1; // -x < empty cell
817 else
818 fRes = 1; // x > empty cell
819 }
820 // else: empty cell == 0.0
821 }
822 else
823 {
824 if ( rComp.pVal[ 0 ]->Len() )
825 fRes = 1; // "..." > empty cell
826 // else: "" == empty cell
827 }
828 }
829 else if( rComp.bVal[ 0 ] )
830 {
831 if( rComp.bVal[ 1 ] )
832 {
833 if ( !::rtl::math::approxEqual( rComp.nVal[ 0 ], rComp.nVal[ 1 ] ) )
834 {
835 if( rComp.nVal[ 0 ] - rComp.nVal[ 1 ] < 0 )
836 fRes = -1;
837 else
838 fRes = 1;
839 }
840 }
841 else
842 {
843 fRes = -1; // number is less than string
844 nStringQuery = 2; // 1+1
845 }
846 }
847 else if( rComp.bVal[ 1 ] )
848 {
849 fRes = 1; // string is greater than number
850 nStringQuery = 1; // 0+1
851 }
852 else
853 {
854 // Both strings.
855 if (pOptions)
856 {
857 // All similar to ScTable::ValidQuery(), *rComp.pVal[1] actually
858 // is/must be identical to *rEntry.pStr, which is essential for
859 // regex to work through GetSearchTextPtr().
860 ScQueryEntry& rEntry = pOptions->aQueryEntry;
861 DBG_ASSERT( *rComp.pVal[1] == *rEntry.pStr, "ScInterpreter::CompareFunc: broken options");
862 if (pOptions->bRegEx)
863 {
864 xub_StrLen nStart = 0;
865 xub_StrLen nStop = rComp.pVal[0]->Len();
866 bool bMatch = rEntry.GetSearchTextPtr(
867 !pOptions->bIgnoreCase)->SearchFrwrd( *rComp.pVal[0],
868 &nStart, &nStop);
869 if (bMatch && pOptions->bMatchWholeCell && (nStart != 0 || nStop != rComp.pVal[0]->Len()))
870 bMatch = false; // RegEx must match entire string.
871 fRes = (bMatch ? 0 : 1);
872 }
873 else if (rEntry.eOp == SC_EQUAL || rEntry.eOp == SC_NOT_EQUAL)
874 {
875 ::utl::TransliterationWrapper* pTransliteration =
876 (pOptions->bIgnoreCase ? ScGlobal::GetpTransliteration() :
877 ScGlobal::GetCaseTransliteration());
878 bool bMatch;
879 if (pOptions->bMatchWholeCell)
880 bMatch = pTransliteration->isEqual( *rComp.pVal[0], *rComp.pVal[1]);
881 else
882 {
883 String aCell( pTransliteration->transliterate(
884 *rComp.pVal[0], ScGlobal::eLnge, 0,
885 rComp.pVal[0]->Len(), NULL));
886 String aQuer( pTransliteration->transliterate(
887 *rComp.pVal[1], ScGlobal::eLnge, 0,
888 rComp.pVal[1]->Len(), NULL));
889 bMatch = (aCell.Search( aQuer ) != STRING_NOTFOUND);
890 }
891 fRes = (bMatch ? 0 : 1);
892 }
893 else if (pOptions->bIgnoreCase)
894 fRes = (double) ScGlobal::GetCollator()->compareString(
895 *rComp.pVal[ 0 ], *rComp.pVal[ 1 ] );
896 else
897 fRes = (double) ScGlobal::GetCaseCollator()->compareString(
898 *rComp.pVal[ 0 ], *rComp.pVal[ 1 ] );
899 }
900 else if (pDok->GetDocOptions().IsIgnoreCase())
901 fRes = (double) ScGlobal::GetCollator()->compareString(
902 *rComp.pVal[ 0 ], *rComp.pVal[ 1 ] );
903 else
904 fRes = (double) ScGlobal::GetCaseCollator()->compareString(
905 *rComp.pVal[ 0 ], *rComp.pVal[ 1 ] );
906 }
907 if (nStringQuery && pOptions)
908 {
909 const ScQueryEntry& rEntry = pOptions->aQueryEntry;
910 if (!rEntry.bQueryByString && rEntry.pStr->Len() &&
911 (rEntry.eOp == SC_EQUAL || rEntry.eOp == SC_NOT_EQUAL))
912 {
913 // As in ScTable::ValidQuery() match a numeric string for a
914 // number query that originated from a string, e.g. in SUMIF
915 // and COUNTIF. Transliteration is not needed here.
916 bool bEqual = rComp.pVal[nStringQuery-1]->Equals( *rEntry.pStr);
917 // match => fRes=0, else fRes=1
918 fRes = (rEntry.eOp == SC_NOT_EQUAL) ? bEqual : !bEqual;
919 }
920 }
921 return fRes;
922 }
923
924
Compare()925 double ScInterpreter::Compare()
926 {
927 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::Compare" );
928 String aVal1, aVal2;
929 ScCompare aComp( &aVal1, &aVal2 );
930 for( short i = 1; i >= 0; i-- )
931 {
932 switch ( GetRawStackType() )
933 {
934 case svEmptyCell:
935 Pop();
936 aComp.bEmpty[ i ] = sal_True;
937 break;
938 case svMissing:
939 case svDouble:
940 aComp.nVal[ i ] = GetDouble();
941 aComp.bVal[ i ] = sal_True;
942 break;
943 case svString:
944 *aComp.pVal[ i ] = GetString();
945 aComp.bVal[ i ] = sal_False;
946 break;
947 case svDoubleRef :
948 case svSingleRef :
949 {
950 ScAddress aAdr;
951 if ( !PopDoubleRefOrSingleRef( aAdr ) )
952 break;
953 ScBaseCell* pCell = GetCell( aAdr );
954 if (HasCellEmptyData( pCell))
955 aComp.bEmpty[ i ] = sal_True;
956 else if (HasCellStringData( pCell))
957 {
958 GetCellString( *aComp.pVal[ i ], pCell);
959 aComp.bVal[ i ] = sal_False;
960 }
961 else
962 {
963 aComp.nVal[ i ] = GetCellValue( aAdr, pCell );
964 aComp.bVal[ i ] = sal_True;
965 }
966 }
967 break;
968 default:
969 SetError( errIllegalParameter);
970 break;
971 }
972 }
973 if( nGlobalError )
974 return 0;
975 nCurFmtType = nFuncFmtType = NUMBERFORMAT_LOGICAL;
976 return CompareFunc( aComp );
977 }
978
979
CompareMat(ScCompareOptions * pOptions)980 ScMatrixRef ScInterpreter::CompareMat( ScCompareOptions* pOptions )
981 {
982 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::CompareMat" );
983 String aVal1, aVal2;
984 ScCompare aComp( &aVal1, &aVal2 );
985 ScMatrixRef pMat[2];
986 ScAddress aAdr;
987 for( short i = 1; i >= 0; i-- )
988 {
989 switch (GetRawStackType())
990 {
991 case svEmptyCell:
992 Pop();
993 aComp.bEmpty[ i ] = sal_True;
994 break;
995 case svMissing:
996 case svDouble:
997 aComp.nVal[ i ] = GetDouble();
998 aComp.bVal[ i ] = sal_True;
999 break;
1000 case svString:
1001 *aComp.pVal[ i ] = GetString();
1002 aComp.bVal[ i ] = sal_False;
1003 break;
1004 case svSingleRef:
1005 {
1006 PopSingleRef( aAdr );
1007 ScBaseCell* pCell = GetCell( aAdr );
1008 if (HasCellEmptyData( pCell))
1009 aComp.bEmpty[ i ] = sal_True;
1010 else if (HasCellStringData( pCell))
1011 {
1012 GetCellString( *aComp.pVal[ i ], pCell);
1013 aComp.bVal[ i ] = sal_False;
1014 }
1015 else
1016 {
1017 aComp.nVal[ i ] = GetCellValue( aAdr, pCell );
1018 aComp.bVal[ i ] = sal_True;
1019 }
1020 }
1021 break;
1022 case svDoubleRef:
1023 case svMatrix:
1024 pMat[ i ] = GetMatrix();
1025 if ( !pMat[ i ] )
1026 SetError( errIllegalParameter);
1027 else
1028 pMat[i]->SetErrorInterpreter( NULL);
1029 // errors are transported as DoubleError inside matrix
1030 break;
1031 default:
1032 SetError( errIllegalParameter);
1033 break;
1034 }
1035 }
1036 ScMatrixRef pResMat = NULL;
1037 if( !nGlobalError )
1038 {
1039 if ( pMat[0] && pMat[1] )
1040 {
1041 SCSIZE nC0, nC1;
1042 SCSIZE nR0, nR1;
1043 pMat[0]->GetDimensions( nC0, nR0 );
1044 pMat[1]->GetDimensions( nC1, nR1 );
1045 SCSIZE nC = Max( nC0, nC1 );
1046 SCSIZE nR = Max( nR0, nR1 );
1047 pResMat = GetNewMat( nC, nR);
1048 if ( !pResMat )
1049 return NULL;
1050 for ( SCSIZE j=0; j<nC; j++ )
1051 {
1052 for ( SCSIZE k=0; k<nR; k++ )
1053 {
1054 SCSIZE nCol = j, nRow = k;
1055 if ( pMat[0]->ValidColRowOrReplicated( nCol, nRow ) &&
1056 pMat[1]->ValidColRowOrReplicated( nCol, nRow ))
1057 {
1058 for ( short i=1; i>=0; i-- )
1059 {
1060 if ( pMat[i]->IsString(j,k) )
1061 {
1062 aComp.bVal[i] = sal_False;
1063 *aComp.pVal[i] = pMat[i]->GetString(j,k);
1064 aComp.bEmpty[i] = pMat[i]->IsEmpty(j,k);
1065 }
1066 else
1067 {
1068 aComp.bVal[i] = sal_True;
1069 aComp.nVal[i] = pMat[i]->GetDouble(j,k);
1070 aComp.bEmpty[i] = sal_False;
1071 }
1072 }
1073 pResMat->PutDouble( CompareFunc( aComp, pOptions ), j,k );
1074 }
1075 else
1076 pResMat->PutString( ScGlobal::GetRscString(STR_NO_VALUE), j,k );
1077 }
1078 }
1079 }
1080 else if ( pMat[0] || pMat[1] )
1081 {
1082 short i = ( pMat[0] ? 0 : 1);
1083 SCSIZE nC, nR;
1084 pMat[i]->GetDimensions( nC, nR );
1085 pResMat = GetNewMat( nC, nR);
1086 if ( !pResMat )
1087 return NULL;
1088 SCSIZE n = nC * nR;
1089 for ( SCSIZE j=0; j<n; j++ )
1090 {
1091 if ( pMat[i]->IsValue(j) )
1092 {
1093 aComp.bVal[i] = sal_True;
1094 aComp.nVal[i] = pMat[i]->GetDouble(j);
1095 aComp.bEmpty[i] = sal_False;
1096 }
1097 else
1098 {
1099 aComp.bVal[i] = sal_False;
1100 *aComp.pVal[i] = pMat[i]->GetString(j);
1101 aComp.bEmpty[i] = pMat[i]->IsEmpty(j);
1102 }
1103 pResMat->PutDouble( CompareFunc( aComp, pOptions ), j );
1104 }
1105 }
1106 }
1107 nCurFmtType = nFuncFmtType = NUMBERFORMAT_LOGICAL;
1108 return pResMat;
1109 }
1110
1111
QueryMat(ScMatrix * pMat,ScCompareOptions & rOptions)1112 ScMatrixRef ScInterpreter::QueryMat( ScMatrix* pMat, ScCompareOptions& rOptions )
1113 {
1114 short nSaveCurFmtType = nCurFmtType;
1115 short nSaveFuncFmtType = nFuncFmtType;
1116 PushMatrix( pMat);
1117 if (rOptions.aQueryEntry.bQueryByString)
1118 PushString( *rOptions.aQueryEntry.pStr);
1119 else
1120 PushDouble( rOptions.aQueryEntry.nVal);
1121 ScMatrixRef pResultMatrix = CompareMat( &rOptions);
1122 nCurFmtType = nSaveCurFmtType;
1123 nFuncFmtType = nSaveFuncFmtType;
1124 if (nGlobalError || !pResultMatrix)
1125 {
1126 SetError( errIllegalParameter);
1127 return pResultMatrix;
1128 }
1129
1130 switch (rOptions.aQueryEntry.eOp)
1131 {
1132 case SC_EQUAL:
1133 pResultMatrix->CompareEqual();
1134 break;
1135 case SC_LESS:
1136 pResultMatrix->CompareLess();
1137 break;
1138 case SC_GREATER:
1139 pResultMatrix->CompareGreater();
1140 break;
1141 case SC_LESS_EQUAL:
1142 pResultMatrix->CompareLessEqual();
1143 break;
1144 case SC_GREATER_EQUAL:
1145 pResultMatrix->CompareGreaterEqual();
1146 break;
1147 case SC_NOT_EQUAL:
1148 pResultMatrix->CompareNotEqual();
1149 break;
1150 default:
1151 SetError( errIllegalArgument);
1152 DBG_ERROR1( "ScInterpreter::QueryMat: unhandled comparison operator: %d", (int)rOptions.aQueryEntry.eOp);
1153 }
1154 return pResultMatrix;
1155 }
1156
1157
ScEqual()1158 void ScInterpreter::ScEqual()
1159 {
1160 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScEqual" );
1161 if ( GetStackType(1) == svMatrix || GetStackType(2) == svMatrix )
1162 {
1163 ScMatrixRef pMat = CompareMat();
1164 if ( !pMat )
1165 PushIllegalParameter();
1166 else
1167 {
1168 pMat->CompareEqual();
1169 PushMatrix( pMat );
1170 }
1171 }
1172 else
1173 PushInt( Compare() == 0 );
1174 }
1175
1176
ScNotEqual()1177 void ScInterpreter::ScNotEqual()
1178 {
1179 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScNotEqual" );
1180 if ( GetStackType(1) == svMatrix || GetStackType(2) == svMatrix )
1181 {
1182 ScMatrixRef pMat = CompareMat();
1183 if ( !pMat )
1184 PushIllegalParameter();
1185 else
1186 {
1187 pMat->CompareNotEqual();
1188 PushMatrix( pMat );
1189 }
1190 }
1191 else
1192 PushInt( Compare() != 0 );
1193 }
1194
1195
ScLess()1196 void ScInterpreter::ScLess()
1197 {
1198 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScLess" );
1199 if ( GetStackType(1) == svMatrix || GetStackType(2) == svMatrix )
1200 {
1201 ScMatrixRef pMat = CompareMat();
1202 if ( !pMat )
1203 PushIllegalParameter();
1204 else
1205 {
1206 pMat->CompareLess();
1207 PushMatrix( pMat );
1208 }
1209 }
1210 else
1211 PushInt( Compare() < 0 );
1212 }
1213
1214
ScGreater()1215 void ScInterpreter::ScGreater()
1216 {
1217 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScGreater" );
1218 if ( GetStackType(1) == svMatrix || GetStackType(2) == svMatrix )
1219 {
1220 ScMatrixRef pMat = CompareMat();
1221 if ( !pMat )
1222 PushIllegalParameter();
1223 else
1224 {
1225 pMat->CompareGreater();
1226 PushMatrix( pMat );
1227 }
1228 }
1229 else
1230 PushInt( Compare() > 0 );
1231 }
1232
1233
ScLessEqual()1234 void ScInterpreter::ScLessEqual()
1235 {
1236 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScLessEqual" );
1237 if ( GetStackType(1) == svMatrix || GetStackType(2) == svMatrix )
1238 {
1239 ScMatrixRef pMat = CompareMat();
1240 if ( !pMat )
1241 PushIllegalParameter();
1242 else
1243 {
1244 pMat->CompareLessEqual();
1245 PushMatrix( pMat );
1246 }
1247 }
1248 else
1249 PushInt( Compare() <= 0 );
1250 }
1251
1252
ScGreaterEqual()1253 void ScInterpreter::ScGreaterEqual()
1254 {
1255 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScGreaterEqual" );
1256 if ( GetStackType(1) == svMatrix || GetStackType(2) == svMatrix )
1257 {
1258 ScMatrixRef pMat = CompareMat();
1259 if ( !pMat )
1260 PushIllegalParameter();
1261 else
1262 {
1263 pMat->CompareGreaterEqual();
1264 PushMatrix( pMat );
1265 }
1266 }
1267 else
1268 PushInt( Compare() >= 0 );
1269 }
1270
ScBitAnd()1271 void ScInterpreter::ScBitAnd() {
1272 ScBitArithmeticOps(bitOperations::BITAND);
1273 }
1274
ScBitOr()1275 void ScInterpreter::ScBitOr() {
1276 ScBitArithmeticOps(bitOperations::BITOR);
1277 }
1278
ScBitXor()1279 void ScInterpreter::ScBitXor() {
1280 ScBitArithmeticOps(bitOperations::BITXOR);
1281 }
1282
1283 /* Helper function that calculates the result in bitwise arithmetic operations helping avoid code repetition */
doOperation(sal_uInt64 val,ScInterpreter::bitOperations::bitArithmetic bitOp,sal_uInt64 & res,sal_Bool & first)1284 static void doOperation( sal_uInt64 val, ScInterpreter::bitOperations::bitArithmetic bitOp, sal_uInt64 &res, sal_Bool &first )
1285 {
1286 if ( first )
1287 {
1288 res = val;
1289 first = sal_False;
1290 }
1291 else
1292 {
1293 if (bitOp == ScInterpreter::bitOperations::BITAND)
1294 res = res & val;
1295 else if (bitOp == ScInterpreter::bitOperations::BITOR)
1296 res = res | val;
1297 else if (bitOp == ScInterpreter::bitOperations::BITXOR)
1298 res = res ^ val;
1299 }
1300 }
1301
ScBitArithmeticOps(bitOperations::bitArithmetic bitOp)1302 void ScInterpreter::ScBitArithmeticOps(bitOperations::bitArithmetic bitOp)
1303 {
1304 nFuncFmtType = NUMBERFORMAT_NUMBER;
1305 short nParamCount = GetByte();
1306 static const sal_uInt64 max_val = SAL_CONST_UINT64( 281474976710655 );
1307 static const int NUMBER_OF_ARGUMENTS = 2;
1308
1309 if ( MustHaveParamCount( nParamCount, NUMBER_OF_ARGUMENTS ) )
1310 {
1311 double *arguments = new double[NUMBER_OF_ARGUMENTS];
1312 sal_Bool bValid = sal_True;
1313
1314 for (int i=0; i<NUMBER_OF_ARGUMENTS; i++)
1315 {
1316 arguments[i] = ::rtl::math::approxFloor( GetDouble() );
1317 if ( !::rtl::math::isFinite( arguments[i] )
1318 || arguments[i] < 0
1319 || arguments[i] > max_val )
1320 {
1321 bValid = sal_False;
1322 }
1323 }
1324
1325 if ( bValid )
1326 {
1327 sal_uInt64 res = 0;
1328 sal_Bool first = sal_True;
1329
1330 for (int i=0; i<NUMBER_OF_ARGUMENTS; i++)
1331 {
1332 doOperation( ( sal_uInt64 )arguments[i], bitOp, res, first );
1333 }
1334
1335 delete[] arguments;
1336 PushDouble( (double) res );
1337 }
1338 else
1339 {
1340 delete[] arguments;
1341 PushIllegalArgument();
1342 }
1343 }
1344 }
1345
ScBitRShift()1346 void ScInterpreter::ScBitRShift() {
1347 ScBitShiftOps(bitOperations::BITRSHIFT);
1348 }
1349
ScBitLShift()1350 void ScInterpreter::ScBitLShift() {
1351 ScBitShiftOps(bitOperations::BITLSHIFT);
1352 }
1353
ScBitShiftOps(bitOperations::bitShift bitOp)1354 void ScInterpreter::ScBitShiftOps(bitOperations::bitShift bitOp) {
1355 if ( MustHaveParamCount( GetByte(), 2 ) )
1356 {
1357 double n = ::rtl::math::approxFloor( GetDouble() );
1358 double val = ::rtl::math::approxFloor( GetDouble() );
1359
1360 static const sal_uInt64 max_val = SAL_CONST_UINT64( 281474976710655 );
1361
1362 if ( !::rtl::math::isFinite( n )
1363 || !::rtl::math::isFinite( val )
1364 || val < 0.0
1365 || static_cast<double>( max_val ) < val )
1366 {
1367 PushIllegalArgument();
1368 }
1369 else
1370 {
1371 double result;
1372 const sal_uInt64 uVal = static_cast<sal_uInt64>( val );
1373 const sal_uInt64 nShift = ( n >= 64.0 || n <= -64.0 )
1374 ? SAL_CONST_UINT64( 64 )
1375 : ( n < 0.0 ? static_cast<sal_uInt64>( -n )
1376 : static_cast<sal_uInt64>( n ) );
1377 if ( nShift == 0 )
1378 {
1379 result = static_cast<double>( uVal );
1380 }
1381 else if ( nShift >= 64 )
1382 {
1383 result = 0.0;
1384 }
1385 else if ( n < 0.0 )
1386 {
1387 if ( bitOp == bitOperations::BITLSHIFT )
1388 result = static_cast<double>( uVal >> nShift );
1389 else
1390 result = static_cast<double>( uVal << nShift );
1391 }
1392 else
1393 {
1394 if ( bitOp == bitOperations::BITLSHIFT )
1395 result = static_cast<double>( uVal << nShift );
1396 else
1397 result = static_cast<double>( uVal >> nShift );
1398 }
1399 PushDouble( result );
1400 }
1401 }
1402 }
1403
ScAnd()1404 void ScInterpreter::ScAnd()
1405 {
1406 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScAnd" );
1407 nFuncFmtType = NUMBERFORMAT_LOGICAL;
1408 short nParamCount = GetByte();
1409 if ( MustHaveParamCountMin( nParamCount, 1 ) )
1410 {
1411 sal_Bool bHaveValue = sal_False;
1412 short nRes = sal_True;
1413 size_t nRefInList = 0;
1414 while( nParamCount-- > 0)
1415 {
1416 if ( !nGlobalError )
1417 {
1418 switch ( GetStackType() )
1419 {
1420 case svDouble :
1421 bHaveValue = sal_True;
1422 nRes &= ( PopDouble() != 0.0 );
1423 break;
1424 case svString :
1425 Pop();
1426 SetError( errNoValue );
1427 break;
1428 case svSingleRef :
1429 {
1430 ScAddress aAdr;
1431 PopSingleRef( aAdr );
1432 if ( !nGlobalError )
1433 {
1434 ScBaseCell* pCell = GetCell( aAdr );
1435 if ( HasCellValueData( pCell ) )
1436 {
1437 bHaveValue = sal_True;
1438 nRes &= ( GetCellValue( aAdr, pCell ) != 0.0 );
1439 }
1440 // else: Xcl setzt hier keinen Fehler
1441 }
1442 }
1443 break;
1444 case svDoubleRef:
1445 case svRefList:
1446 {
1447 ScRange aRange;
1448 PopDoubleRef( aRange, nParamCount, nRefInList);
1449 if ( !nGlobalError )
1450 {
1451 double fVal;
1452 sal_uInt16 nErr = 0;
1453 ScValueIterator aValIter( pDok, aRange );
1454 if ( aValIter.GetFirst( fVal, nErr ) )
1455 {
1456 bHaveValue = sal_True;
1457 do
1458 {
1459 nRes &= ( fVal != 0.0 );
1460 } while ( (nErr == 0) &&
1461 aValIter.GetNext( fVal, nErr ) );
1462 }
1463 SetError( nErr );
1464 }
1465 }
1466 break;
1467 case svMatrix:
1468 {
1469 ScMatrixRef pMat = GetMatrix();
1470 if ( pMat )
1471 {
1472 bHaveValue = sal_True;
1473 double fVal = pMat->And();
1474 sal_uInt16 nErr = GetDoubleErrorValue( fVal );
1475 if ( nErr )
1476 {
1477 SetError( nErr );
1478 nRes = sal_False;
1479 }
1480 else
1481 nRes &= (fVal != 0.0);
1482 }
1483 // else: GetMatrix did set errIllegalParameter
1484 }
1485 break;
1486 default:
1487 Pop();
1488 SetError( errIllegalParameter);
1489 }
1490 }
1491 else
1492 Pop();
1493 }
1494 if ( bHaveValue )
1495 PushInt( nRes );
1496 else
1497 PushNoValue();
1498 }
1499 }
1500
1501
ScOr()1502 void ScInterpreter::ScOr()
1503 {
1504 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScOr" );
1505 nFuncFmtType = NUMBERFORMAT_LOGICAL;
1506 short nParamCount = GetByte();
1507 if ( MustHaveParamCountMin( nParamCount, 1 ) )
1508 {
1509 sal_Bool bHaveValue = sal_False;
1510 short nRes = sal_False;
1511 size_t nRefInList = 0;
1512 while( nParamCount-- > 0)
1513 {
1514 if ( !nGlobalError )
1515 {
1516 switch ( GetStackType() )
1517 {
1518 case svDouble :
1519 bHaveValue = sal_True;
1520 nRes |= ( PopDouble() != 0.0 );
1521 break;
1522 case svString :
1523 Pop();
1524 SetError( errNoValue );
1525 break;
1526 case svSingleRef :
1527 {
1528 ScAddress aAdr;
1529 PopSingleRef( aAdr );
1530 if ( !nGlobalError )
1531 {
1532 ScBaseCell* pCell = GetCell( aAdr );
1533 if ( HasCellValueData( pCell ) )
1534 {
1535 bHaveValue = sal_True;
1536 nRes |= ( GetCellValue( aAdr, pCell ) != 0.0 );
1537 }
1538 // else: Xcl setzt hier keinen Fehler
1539 }
1540 }
1541 break;
1542 case svDoubleRef:
1543 case svRefList:
1544 {
1545 ScRange aRange;
1546 PopDoubleRef( aRange, nParamCount, nRefInList);
1547 if ( !nGlobalError )
1548 {
1549 double fVal;
1550 sal_uInt16 nErr = 0;
1551 ScValueIterator aValIter( pDok, aRange );
1552 if ( aValIter.GetFirst( fVal, nErr ) )
1553 {
1554 bHaveValue = sal_True;
1555 do
1556 {
1557 nRes |= ( fVal != 0.0 );
1558 } while ( (nErr == 0) &&
1559 aValIter.GetNext( fVal, nErr ) );
1560 }
1561 SetError( nErr );
1562 }
1563 }
1564 break;
1565 case svMatrix:
1566 {
1567 bHaveValue = sal_True;
1568 ScMatrixRef pMat = GetMatrix();
1569 if ( pMat )
1570 {
1571 bHaveValue = sal_True;
1572 double fVal = pMat->Or();
1573 sal_uInt16 nErr = GetDoubleErrorValue( fVal );
1574 if ( nErr )
1575 {
1576 SetError( nErr );
1577 nRes = sal_False;
1578 }
1579 else
1580 nRes |= (fVal != 0.0);
1581 }
1582 // else: GetMatrix did set errIllegalParameter
1583 }
1584 break;
1585 default:
1586 Pop();
1587 SetError( errIllegalParameter);
1588 }
1589 }
1590 else
1591 Pop();
1592 }
1593 if ( bHaveValue )
1594 PushInt( nRes );
1595 else
1596 PushNoValue();
1597 }
1598 }
1599
ScXor()1600 void ScInterpreter::ScXor()
1601 {
1602 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "makkica", "ScInterpreter::ScXor" );
1603 nFuncFmtType = NUMBERFORMAT_LOGICAL;
1604 short nParamCount = GetByte();
1605 if ( MustHaveParamCountMin( nParamCount, 1 ) )
1606 {
1607 bool bHaveValue = false;
1608 short nRes = 0;
1609 size_t nRefInList = 0;
1610 while( nParamCount-- > 0)
1611 {
1612 if ( !nGlobalError )
1613 {
1614 switch ( GetStackType() )
1615 {
1616 case svDouble :
1617 bHaveValue = true;
1618 nRes ^= ( PopDouble() != 0.0 );
1619 break;
1620 case svString :
1621 Pop();
1622 SetError( errNoValue );
1623 break;
1624 case svSingleRef :
1625 {
1626 ScAddress aAdr;
1627 PopSingleRef( aAdr );
1628 if ( !nGlobalError )
1629 {
1630 ScBaseCell* pCell = GetCell( aAdr );
1631 if ( HasCellValueData( pCell ) )
1632 {
1633 bHaveValue = true;
1634 nRes ^= ( GetCellValue( aAdr, pCell ) != 0.0 );
1635 }
1636 /* TODO: set error? Excel doesn't have XOR, but
1637 * doesn't set an error in this case for AND and
1638 * OR. */
1639 }
1640 }
1641 break;
1642 case svDoubleRef:
1643 case svRefList:
1644 {
1645 ScRange aRange;
1646 PopDoubleRef( aRange, nParamCount, nRefInList);
1647 if ( !nGlobalError )
1648 {
1649 double fVal;
1650 sal_uInt16 nErr = 0;
1651 ScValueIterator aValIter( pDok, aRange );
1652 if ( aValIter.GetFirst( fVal, nErr ) )
1653 {
1654 bHaveValue = true;
1655 do
1656 {
1657 nRes ^= ( fVal != 0.0 );
1658 } while ( (nErr == 0) &&
1659 aValIter.GetNext( fVal, nErr ) );
1660 }
1661 SetError( nErr );
1662 }
1663 }
1664 break;
1665 case svMatrix:
1666 {
1667 bHaveValue = true;
1668 ScMatrixRef pMat = GetMatrix();
1669 if ( pMat )
1670 {
1671 bHaveValue = true;
1672 double fVal = pMat->Xor();
1673 sal_uInt16 nErr = GetDoubleErrorValue( fVal );
1674 if ( nErr )
1675 {
1676 SetError( nErr );
1677 nRes = 0;
1678 }
1679 else
1680 nRes ^= (fVal != 0.0);
1681 }
1682 // else: GetMatrix did set errIllegalParameter
1683 }
1684 break;
1685 default:
1686 Pop();
1687 SetError( errIllegalParameter);
1688 }
1689 }
1690 else
1691 Pop();
1692 }
1693 if ( bHaveValue )
1694 PushInt( nRes );
1695 else
1696 PushNoValue();
1697 }
1698 }
1699
1700
ScNeg()1701 void ScInterpreter::ScNeg()
1702 {
1703 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScNeg" );
1704 // Simple negation doesn't change current format type to number, keep
1705 // current type.
1706 nFuncFmtType = nCurFmtType;
1707 switch ( GetStackType() )
1708 {
1709 case svMatrix :
1710 {
1711 ScMatrixRef pMat = GetMatrix();
1712 if ( !pMat )
1713 PushIllegalParameter();
1714 else
1715 {
1716 SCSIZE nC, nR;
1717 pMat->GetDimensions( nC, nR );
1718 ScMatrixRef pResMat = GetNewMat( nC, nR);
1719 if ( !pResMat )
1720 PushIllegalArgument();
1721 else
1722 {
1723 SCSIZE nCount = nC * nR;
1724 for ( SCSIZE j=0; j<nCount; ++j )
1725 {
1726 if ( pMat->IsValueOrEmpty(j) )
1727 pResMat->PutDouble( -pMat->GetDouble(j), j );
1728 else
1729 pResMat->PutString(
1730 ScGlobal::GetRscString( STR_NO_VALUE ), j );
1731 }
1732 PushMatrix( pResMat );
1733 }
1734 }
1735 }
1736 break;
1737 default:
1738 PushDouble( -GetDouble() );
1739 }
1740 }
1741
1742
ScPercentSign()1743 void ScInterpreter::ScPercentSign()
1744 {
1745 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScPercentSign" );
1746 nFuncFmtType = NUMBERFORMAT_PERCENT;
1747 const FormulaToken* pSaveCur = pCur;
1748 sal_uInt8 nSavePar = cPar;
1749 PushInt( 100 );
1750 cPar = 2;
1751 FormulaByteToken aDivOp( ocDiv, cPar );
1752 pCur = &aDivOp;
1753 ScDiv();
1754 pCur = pSaveCur;
1755 cPar = nSavePar;
1756 }
1757
1758
ScNot()1759 void ScInterpreter::ScNot()
1760 {
1761 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScNot" );
1762 nFuncFmtType = NUMBERFORMAT_LOGICAL;
1763 switch ( GetStackType() )
1764 {
1765 case svMatrix :
1766 {
1767 ScMatrixRef pMat = GetMatrix();
1768 if ( !pMat )
1769 PushIllegalParameter();
1770 else
1771 {
1772 SCSIZE nC, nR;
1773 pMat->GetDimensions( nC, nR );
1774 ScMatrixRef pResMat = GetNewMat( nC, nR);
1775 if ( !pResMat )
1776 PushIllegalArgument();
1777 else
1778 {
1779 SCSIZE nCount = nC * nR;
1780 for ( SCSIZE j=0; j<nCount; ++j )
1781 {
1782 if ( pMat->IsValueOrEmpty(j) )
1783 pResMat->PutDouble( (pMat->GetDouble(j) == 0.0), j );
1784 else
1785 pResMat->PutString(
1786 ScGlobal::GetRscString( STR_NO_VALUE ), j );
1787 }
1788 PushMatrix( pResMat );
1789 }
1790 }
1791 }
1792 break;
1793 default:
1794 PushInt( GetDouble() == 0.0 );
1795 }
1796 }
1797
1798
ScPi()1799 void ScInterpreter::ScPi()
1800 {
1801 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScPi" );
1802 PushDouble(F_PI);
1803 }
1804
1805 #define SCRANDOMQ_SIZE 4194304
1806 static sal_uInt32 nScRandomQ[SCRANDOMQ_SIZE], nScCarry = 0;
1807
1808 // Multiply with Carry
1809 sal_uInt32
b32MWC(void)1810 b32MWC(void)
1811 {
1812 sal_uInt32 t, x;
1813 static int j = (SCRANDOMQ_SIZE - 1);
1814
1815 j = (j + 1) & (SCRANDOMQ_SIZE - 1);
1816 x = nScRandomQ[j];
1817 t = (x << 28) + nScCarry;
1818 nScCarry = (x >> 4) - (t < x);
1819 return (nScRandomQ[j] = t - x);
1820 }
1821
1822 // Congruential
1823 #define CNG ( ScCNG = 69069 * ScCNG + 13579 )
1824 // Xorshift
1825 #define XS ( ScXS ^= (ScXS << 13), ScXS ^= (ScXS >> 17), ScXS ^= (ScXS << 5) )
1826
1827 #define KISS (b32MWC() + CNG + XS)
1828
ScRandom()1829 void ScInterpreter::ScRandom()
1830 {
1831 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "pfg", "ScInterpreter::ScRandom" );
1832
1833 static sal_Bool SqSeeded = sal_False;
1834 static sal_uInt32 ScCNG, ScXS = 362436069;
1835
1836 // Seeding for the PRNG
1837 if (SqSeeded == sal_False) {
1838 rtlRandomPool aPool = rtl_random_createPool();
1839 rtl_random_getBytes(aPool, &ScCNG, sizeof(ScCNG));
1840 rtl_random_getBytes(aPool, &nScRandomQ,
1841 sizeof(nScRandomQ[0]) * SCRANDOMQ_SIZE);
1842 rtl_random_destroyPool(aPool);
1843 SqSeeded = sal_True;
1844 }
1845 PushDouble(static_cast<double>(KISS) / SAL_MAX_UINT32);
1846 }
1847
ScTrue()1848 void ScInterpreter::ScTrue()
1849 {
1850 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScTrue" );
1851 nFuncFmtType = NUMBERFORMAT_LOGICAL;
1852 PushInt(1);
1853 }
1854
1855
ScFalse()1856 void ScInterpreter::ScFalse()
1857 {
1858 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScFalse" );
1859 nFuncFmtType = NUMBERFORMAT_LOGICAL;
1860 PushInt(0);
1861 }
1862
1863
ScDeg()1864 void ScInterpreter::ScDeg()
1865 {
1866 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScDeg" );
1867 PushDouble((GetDouble() / F_PI) * 180.0);
1868 }
1869
1870
ScRad()1871 void ScInterpreter::ScRad()
1872 {
1873 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScRad" );
1874 PushDouble(GetDouble() * (F_PI / 180));
1875 }
1876
1877
ScSin()1878 void ScInterpreter::ScSin()
1879 {
1880 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScSin" );
1881 PushDouble(::rtl::math::sin(GetDouble()));
1882 }
1883
1884
ScCos()1885 void ScInterpreter::ScCos()
1886 {
1887 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScCos" );
1888 PushDouble(::rtl::math::cos(GetDouble()));
1889 }
1890
1891
ScTan()1892 void ScInterpreter::ScTan()
1893 {
1894 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScTan" );
1895 PushDouble(::rtl::math::tan(GetDouble()));
1896 }
1897
1898
ScCot()1899 void ScInterpreter::ScCot()
1900 {
1901 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScCot" );
1902 PushDouble(1.0 / ::rtl::math::tan(GetDouble()));
1903 }
1904
1905
ScArcSin()1906 void ScInterpreter::ScArcSin()
1907 {
1908 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScArcSin" );
1909 PushDouble(asin(GetDouble()));
1910 }
1911
1912
ScArcCos()1913 void ScInterpreter::ScArcCos()
1914 {
1915 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScArcCos" );
1916 PushDouble(acos(GetDouble()));
1917 }
1918
1919
ScArcTan()1920 void ScInterpreter::ScArcTan()
1921 {
1922 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScArcTan" );
1923 PushDouble(atan(GetDouble()));
1924 }
1925
1926
ScArcCot()1927 void ScInterpreter::ScArcCot()
1928 {
1929 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScArcCot" );
1930 PushDouble((F_PI2) - atan(GetDouble()));
1931 }
1932
1933
ScSinHyp()1934 void ScInterpreter::ScSinHyp()
1935 {
1936 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScSinHyp" );
1937 PushDouble(sinh(GetDouble()));
1938 }
1939
1940
ScCosHyp()1941 void ScInterpreter::ScCosHyp()
1942 {
1943 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScCosHyp" );
1944 PushDouble(cosh(GetDouble()));
1945 }
1946
1947
ScTanHyp()1948 void ScInterpreter::ScTanHyp()
1949 {
1950 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScTanHyp" );
1951 PushDouble(tanh(GetDouble()));
1952 }
1953
1954
ScCotHyp()1955 void ScInterpreter::ScCotHyp()
1956 {
1957 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScCotHyp" );
1958 PushDouble(1.0 / tanh(GetDouble()));
1959 }
1960
1961
ScArcSinHyp()1962 void ScInterpreter::ScArcSinHyp()
1963 {
1964 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScArcSinHyp" );
1965 PushDouble( std::asinh( GetDouble()));
1966 }
1967
ScArcCosHyp()1968 void ScInterpreter::ScArcCosHyp()
1969 {
1970 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScArcCosHyp" );
1971 double fVal = GetDouble();
1972 if (fVal < 1.0)
1973 PushIllegalArgument();
1974 else
1975 PushDouble( std::acosh( fVal));
1976 }
1977
ScArcTanHyp()1978 void ScInterpreter::ScArcTanHyp()
1979 {
1980 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScArcTanHyp" );
1981 double fVal = GetDouble();
1982 if (fabs(fVal) >= 1.0)
1983 PushIllegalArgument();
1984 else
1985 PushDouble( std::atanh( fVal));
1986 }
1987
1988
ScArcCotHyp()1989 void ScInterpreter::ScArcCotHyp()
1990 {
1991 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScArcCotHyp" );
1992 double nVal = GetDouble();
1993 if (fabs(nVal) <= 1.0)
1994 PushIllegalArgument();
1995 else
1996 PushDouble(0.5 * log((nVal + 1.0) / (nVal - 1.0)));
1997 }
1998
ScCosecant()1999 void ScInterpreter::ScCosecant()
2000 {
2001 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "regina", "ScInterpreter::ScCosecant" );
2002 PushDouble(1.0 / ::rtl::math::sin(GetDouble()));
2003 }
2004
ScSecant()2005 void ScInterpreter::ScSecant()
2006 {
2007 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "regina", "ScInterpreter::ScSecant" );
2008 PushDouble(1.0 / ::rtl::math::cos(GetDouble()));
2009 }
2010
ScCosecantHyp()2011 void ScInterpreter::ScCosecantHyp()
2012 {
2013 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "regina", "ScInterpreter::ScCosecantHyp" );
2014 PushDouble(1.0 / sinh(GetDouble()));
2015 }
2016
ScSecantHyp()2017 void ScInterpreter::ScSecantHyp()
2018 {
2019 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "regina", "ScInterpreter::ScSecantHyp" );
2020 PushDouble(1.0 / cosh(GetDouble()));
2021 }
2022
2023
ScExp()2024 void ScInterpreter::ScExp()
2025 {
2026 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScExp" );
2027 PushDouble(exp(GetDouble()));
2028 }
2029
2030
ScSqrt()2031 void ScInterpreter::ScSqrt()
2032 {
2033 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScSqrt" );
2034 double fVal = GetDouble();
2035 if (fVal >= 0.0)
2036 PushDouble(sqrt(fVal));
2037 else
2038 PushIllegalArgument();
2039 }
2040
2041
ScIsEmpty()2042 void ScInterpreter::ScIsEmpty()
2043 {
2044 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScIsEmpty" );
2045 short nRes = 0;
2046 nFuncFmtType = NUMBERFORMAT_LOGICAL;
2047 switch ( GetRawStackType() )
2048 {
2049 case svEmptyCell:
2050 {
2051 FormulaTokenRef p = PopToken();
2052 if (!static_cast<const ScEmptyCellToken*>(p.get())->IsInherited())
2053 nRes = 1;
2054 }
2055 break;
2056 case svDoubleRef :
2057 case svSingleRef :
2058 {
2059 ScAddress aAdr;
2060 if ( !PopDoubleRefOrSingleRef( aAdr ) )
2061 break;
2062 // NOTE: this could test also on inherited emptiness, but then the
2063 // cell tested wouldn't be empty. Must correspond with
2064 // ScCountEmptyCells().
2065 // if (HasCellEmptyData( GetCell( aAdr)))
2066 CellType eCellType = GetCellType( GetCell( aAdr ) );
2067 if((eCellType == CELLTYPE_NONE) || (eCellType == CELLTYPE_NOTE))
2068 nRes = 1;
2069 }
2070 break;
2071 case svMatrix:
2072 {
2073 ScMatrixRef pMat = PopMatrix();
2074 if ( !pMat )
2075 ; // nothing
2076 else if ( !pJumpMatrix )
2077 nRes = pMat->IsEmpty( 0 );
2078 else
2079 {
2080 SCSIZE nCols, nRows, nC, nR;
2081 pMat->GetDimensions( nCols, nRows);
2082 pJumpMatrix->GetPos( nC, nR);
2083 if ( nC < nCols && nR < nRows )
2084 nRes = pMat->IsEmpty( nC, nR);
2085 // else: sal_False, not empty (which is what Xcl does)
2086 }
2087 }
2088 break;
2089 default:
2090 Pop();
2091 }
2092 nGlobalError = 0;
2093 PushInt( nRes );
2094 }
2095
2096
IsString()2097 short ScInterpreter::IsString()
2098 {
2099 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::IsString" );
2100 nFuncFmtType = NUMBERFORMAT_LOGICAL;
2101 short nRes = 0;
2102 switch ( GetRawStackType() )
2103 {
2104 case svString:
2105 Pop();
2106 nRes = 1;
2107 break;
2108 case svDoubleRef :
2109 case svSingleRef :
2110 {
2111 ScAddress aAdr;
2112 if ( !PopDoubleRefOrSingleRef( aAdr ) )
2113 break;
2114 ScBaseCell* pCell = GetCell( aAdr );
2115 if (GetCellErrCode( pCell ) == 0)
2116 {
2117 switch ( GetCellType( pCell ) )
2118 {
2119 case CELLTYPE_STRING :
2120 case CELLTYPE_EDIT :
2121 nRes = 1;
2122 break;
2123 case CELLTYPE_FORMULA :
2124 nRes = !((ScFormulaCell*)pCell)->IsValue() &&
2125 !((ScFormulaCell*)pCell)->IsEmpty();
2126 break;
2127 default:
2128 ; // nothing
2129 }
2130 }
2131 }
2132 break;
2133 case svMatrix:
2134 {
2135 ScMatrixRef pMat = PopMatrix();
2136 if ( !pMat )
2137 ; // nothing
2138 else if ( !pJumpMatrix )
2139 nRes = pMat->IsString(0) && !pMat->IsEmpty(0);
2140 else
2141 {
2142 SCSIZE nCols, nRows, nC, nR;
2143 pMat->GetDimensions( nCols, nRows);
2144 pJumpMatrix->GetPos( nC, nR);
2145 if ( nC < nCols && nR < nRows )
2146 nRes = pMat->IsString( nC, nR) && !pMat->IsEmpty( nC, nR);
2147 }
2148 }
2149 break;
2150 default:
2151 Pop();
2152 }
2153 nGlobalError = 0;
2154 return nRes;
2155 }
2156
2157
ScIsString()2158 void ScInterpreter::ScIsString()
2159 {
2160 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScIsString" );
2161 PushInt( IsString() );
2162 }
2163
2164
ScIsNonString()2165 void ScInterpreter::ScIsNonString()
2166 {
2167 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScIsNonString" );
2168 PushInt( !IsString() );
2169 }
2170
2171
ScIsLogical()2172 void ScInterpreter::ScIsLogical()
2173 {
2174 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScIsLogical" );
2175 short nRes = 0;
2176 switch ( GetStackType() )
2177 {
2178 case svDoubleRef :
2179 case svSingleRef :
2180 {
2181 ScAddress aAdr;
2182 if ( !PopDoubleRefOrSingleRef( aAdr ) )
2183 break;
2184 ScBaseCell* pCell = GetCell( aAdr );
2185 if (GetCellErrCode( pCell ) == 0)
2186 {
2187 if (HasCellValueData(pCell))
2188 {
2189 sal_uLong nFormat = GetCellNumberFormat( aAdr, pCell );
2190 nRes = ( pFormatter->GetType(nFormat)
2191 == NUMBERFORMAT_LOGICAL);
2192 }
2193 }
2194 }
2195 break;
2196 case svMatrix:
2197 // TODO: we don't have type information for arrays except
2198 // numerical/string.
2199 // Fall thru
2200 default:
2201 PopError();
2202 if ( !nGlobalError )
2203 nRes = ( nCurFmtType == NUMBERFORMAT_LOGICAL );
2204 }
2205 nCurFmtType = nFuncFmtType = NUMBERFORMAT_LOGICAL;
2206 nGlobalError = 0;
2207 PushInt( nRes );
2208 }
2209
2210
ScType()2211 void ScInterpreter::ScType()
2212 {
2213 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScType" );
2214 short nType = 0;
2215 switch ( GetStackType() )
2216 {
2217 case svDoubleRef :
2218 case svSingleRef :
2219 {
2220 ScAddress aAdr;
2221 if ( !PopDoubleRefOrSingleRef( aAdr ) )
2222 break;
2223 ScBaseCell* pCell = GetCell( aAdr );
2224 if (GetCellErrCode( pCell ) == 0)
2225 {
2226 switch ( GetCellType( pCell ) )
2227 {
2228 // NOTE: this is Xcl nonsense!
2229 case CELLTYPE_NOTE :
2230 nType = 1; // empty cell is value (0)
2231 break;
2232 case CELLTYPE_STRING :
2233 case CELLTYPE_EDIT :
2234 nType = 2;
2235 break;
2236 case CELLTYPE_VALUE :
2237 {
2238 sal_uLong nFormat = GetCellNumberFormat( aAdr, pCell );
2239 if (pFormatter->GetType(nFormat)
2240 == NUMBERFORMAT_LOGICAL)
2241 nType = 4;
2242 else
2243 nType = 1;
2244 }
2245 break;
2246 case CELLTYPE_FORMULA :
2247 nType = 8;
2248 break;
2249 default:
2250 PushIllegalArgument();
2251 }
2252 }
2253 else
2254 nType = 16;
2255 }
2256 break;
2257 case svString:
2258 PopError();
2259 if ( nGlobalError )
2260 {
2261 nType = 16;
2262 nGlobalError = 0;
2263 }
2264 else
2265 nType = 2;
2266 break;
2267 case svMatrix:
2268 PopMatrix();
2269 if ( nGlobalError )
2270 {
2271 nType = 16;
2272 nGlobalError = 0;
2273 }
2274 else
2275 nType = 64;
2276 // we could return the type of one element if in JumpMatrix or
2277 // ForceArray mode, but Xcl doesn't ...
2278 break;
2279 default:
2280 PopError();
2281 if ( nGlobalError )
2282 {
2283 nType = 16;
2284 nGlobalError = 0;
2285 }
2286 else
2287 nType = 1;
2288 }
2289 PushInt( nType );
2290 }
2291
2292
lcl_FormatHasNegColor(const SvNumberformat * pFormat)2293 inline sal_Bool lcl_FormatHasNegColor( const SvNumberformat* pFormat )
2294 {
2295 return pFormat && pFormat->GetColor( 1 );
2296 }
2297
2298
lcl_FormatHasOpenPar(const SvNumberformat * pFormat)2299 inline sal_Bool lcl_FormatHasOpenPar( const SvNumberformat* pFormat )
2300 {
2301 return pFormat && (pFormat->GetFormatstring().Search( '(' ) != STRING_NOTFOUND);
2302 }
2303
2304
ScCell()2305 void ScInterpreter::ScCell()
2306 { // ATTRIBUTE ; [REF]
2307 sal_uInt8 nParamCount = GetByte();
2308 if( MustHaveParamCount( nParamCount, 1, 2 ) )
2309 {
2310 ScAddress aCellPos( aPos );
2311 sal_Bool bError = sal_False;
2312 if( nParamCount == 2 )
2313 bError = !PopDoubleRefOrSingleRef( aCellPos );
2314 String aInfoType( GetString() );
2315 if( bError || nGlobalError )
2316 PushIllegalParameter();
2317 else
2318 {
2319 String aFuncResult;
2320 ScBaseCell* pCell = GetCell( aCellPos );
2321
2322 ScCellKeywordTranslator::transKeyword(aInfoType, ScGlobal::GetLocale(), ocCell);
2323
2324 // *** ADDRESS INFO ***
2325 if( aInfoType.EqualsAscii( "COL" ) )
2326 { // column number (1-based)
2327 PushInt( aCellPos.Col() + 1 );
2328 }
2329 else if( aInfoType.EqualsAscii( "ROW" ) )
2330 { // row number (1-based)
2331 PushInt( aCellPos.Row() + 1 );
2332 }
2333 else if( aInfoType.EqualsAscii( "SHEET" ) )
2334 { // table number (1-based)
2335 PushInt( aCellPos.Tab() + 1 );
2336 }
2337 else if( aInfoType.EqualsAscii( "ADDRESS" ) )
2338 { // address formatted as [['FILENAME'#]$TABLE.]$COL$ROW
2339 sal_uInt16 nFlags = (aCellPos.Tab() == aPos.Tab()) ? (SCA_ABS) : (SCA_ABS_3D);
2340 aCellPos.Format( aFuncResult, nFlags, pDok, pDok->GetAddressConvention() );
2341 PushString( aFuncResult );
2342 }
2343 else if( aInfoType.EqualsAscii( "FILENAME" ) )
2344 { // file name and table name: 'FILENAME'#$TABLE
2345 SCTAB nTab = aCellPos.Tab();
2346 if( nTab < pDok->GetTableCount() )
2347 {
2348 if( pDok->GetLinkMode( nTab ) == SC_LINK_VALUE )
2349 pDok->GetName( nTab, aFuncResult );
2350 else
2351 {
2352 SfxObjectShell* pShell = pDok->GetDocumentShell();
2353 if( pShell && pShell->GetMedium() )
2354 {
2355 aFuncResult = (sal_Unicode) '\'';
2356 const INetURLObject& rURLObj = pShell->GetMedium()->GetURLObject();
2357 aFuncResult += String( rURLObj.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS ) );
2358 aFuncResult.AppendAscii( "'#$" );
2359 String aTabName;
2360 pDok->GetName( nTab, aTabName );
2361 aFuncResult += aTabName;
2362 }
2363 }
2364 }
2365 PushString( aFuncResult );
2366 }
2367 else if( aInfoType.EqualsAscii( "COORD" ) )
2368 { // address, lotus 1-2-3 formatted: $TABLE:$COL$ROW
2369 // Yes, passing tab as col is intentional!
2370 ScAddress( static_cast<SCCOL>(aCellPos.Tab()), 0, 0 ).Format(
2371 aFuncResult, (SCA_COL_ABSOLUTE|SCA_VALID_COL), NULL, pDok->GetAddressConvention() );
2372 aFuncResult += ':';
2373 String aCellStr;
2374 aCellPos.Format( aCellStr, (SCA_COL_ABSOLUTE|SCA_VALID_COL|SCA_ROW_ABSOLUTE|SCA_VALID_ROW),
2375 NULL, pDok->GetAddressConvention() );
2376 aFuncResult += aCellStr;
2377 PushString( aFuncResult );
2378 }
2379
2380 // *** CELL PROPERTIES ***
2381 else if( aInfoType.EqualsAscii( "CONTENTS" ) )
2382 { // contents of the cell, no formatting
2383 if( pCell && pCell->HasStringData() )
2384 {
2385 GetCellString( aFuncResult, pCell );
2386 PushString( aFuncResult );
2387 }
2388 else
2389 PushDouble( GetCellValue( aCellPos, pCell ) );
2390 }
2391 else if( aInfoType.EqualsAscii( "TYPE" ) )
2392 { // b = blank; l = string (label); v = otherwise (value)
2393 if( HasCellStringData( pCell ) )
2394 aFuncResult = 'l';
2395 else
2396 aFuncResult = HasCellValueData( pCell ) ? 'v' : 'b';
2397 PushString( aFuncResult );
2398 }
2399 else if( aInfoType.EqualsAscii( "WIDTH" ) )
2400 { // column width (rounded off as count of zero characters in standard font and size)
2401 Printer* pPrinter = pDok->GetPrinter();
2402 MapMode aOldMode( pPrinter->GetMapMode() );
2403 Font aOldFont( pPrinter->GetFont() );
2404 Font aDefFont;
2405
2406 pPrinter->SetMapMode( MAP_TWIP );
2407 // font color doesn't matter here
2408 pDok->GetDefPattern()->GetFont( aDefFont, SC_AUTOCOL_BLACK, pPrinter );
2409 pPrinter->SetFont( aDefFont );
2410 long nZeroWidth = pPrinter->GetTextWidth( String( '0' ) );
2411 pPrinter->SetFont( aOldFont );
2412 pPrinter->SetMapMode( aOldMode );
2413 int nZeroCount = (int)(pDok->GetColWidth( aCellPos.Col(), aCellPos.Tab() ) / nZeroWidth);
2414 PushInt( nZeroCount );
2415 }
2416 else if( aInfoType.EqualsAscii( "PREFIX" ) )
2417 { // ' = left; " = right; ^ = centered
2418 if( HasCellStringData( pCell ) )
2419 {
2420 const SvxHorJustifyItem* pJustAttr = (const SvxHorJustifyItem*)
2421 pDok->GetAttr( aCellPos.Col(), aCellPos.Row(), aCellPos.Tab(), ATTR_HOR_JUSTIFY );
2422 switch( pJustAttr->GetValue() )
2423 {
2424 case SVX_HOR_JUSTIFY_STANDARD:
2425 case SVX_HOR_JUSTIFY_LEFT:
2426 case SVX_HOR_JUSTIFY_BLOCK: aFuncResult = '\''; break;
2427 case SVX_HOR_JUSTIFY_CENTER: aFuncResult = '^'; break;
2428 case SVX_HOR_JUSTIFY_RIGHT: aFuncResult = '"'; break;
2429 case SVX_HOR_JUSTIFY_REPEAT: aFuncResult = '\\'; break;
2430 }
2431 }
2432 PushString( aFuncResult );
2433 }
2434 else if( aInfoType.EqualsAscii( "PROTECT" ) )
2435 { // 1 = cell locked
2436 const ScProtectionAttr* pProtAttr = (const ScProtectionAttr*)
2437 pDok->GetAttr( aCellPos.Col(), aCellPos.Row(), aCellPos.Tab(), ATTR_PROTECTION );
2438 PushInt( pProtAttr->GetProtection() ? 1 : 0 );
2439 }
2440
2441 // *** FORMATTING ***
2442 else if( aInfoType.EqualsAscii( "FORMAT" ) )
2443 { // specific format code for standard formats
2444 sal_uLong nFormat = pDok->GetNumberFormat( aCellPos );
2445 sal_Bool bAppendPrec = sal_True;
2446 sal_uInt16 nPrec, nLeading;
2447 sal_Bool bThousand, bIsRed;
2448 pFormatter->GetFormatSpecialInfo( nFormat, bThousand, bIsRed, nPrec, nLeading );
2449
2450 switch( pFormatter->GetType( nFormat ) )
2451 {
2452 case NUMBERFORMAT_NUMBER: aFuncResult = (bThousand ? ',' : 'F'); break;
2453 case NUMBERFORMAT_CURRENCY: aFuncResult = 'C'; break;
2454 case NUMBERFORMAT_SCIENTIFIC: aFuncResult = 'S'; break;
2455 case NUMBERFORMAT_PERCENT: aFuncResult = 'P'; break;
2456 default:
2457 {
2458 bAppendPrec = sal_False;
2459 switch( pFormatter->GetIndexTableOffset( nFormat ) )
2460 {
2461 case NF_DATE_SYSTEM_SHORT:
2462 case NF_DATE_SYS_DMMMYY:
2463 case NF_DATE_SYS_DDMMYY:
2464 case NF_DATE_SYS_DDMMYYYY:
2465 case NF_DATE_SYS_DMMMYYYY:
2466 case NF_DATE_DIN_DMMMYYYY:
2467 case NF_DATE_SYS_DMMMMYYYY:
2468 case NF_DATE_DIN_DMMMMYYYY: aFuncResult.AssignAscii( RTL_CONSTASCII_STRINGPARAM( "D1" ) ); break;
2469 case NF_DATE_SYS_DDMMM: aFuncResult.AssignAscii( RTL_CONSTASCII_STRINGPARAM( "D2" ) ); break;
2470 case NF_DATE_SYS_MMYY: aFuncResult.AssignAscii( RTL_CONSTASCII_STRINGPARAM( "D3" ) ); break;
2471 case NF_DATETIME_SYSTEM_SHORT_HHMM:
2472 case NF_DATETIME_SYS_DDMMYYYY_HHMMSS:
2473 aFuncResult.AssignAscii( RTL_CONSTASCII_STRINGPARAM( "D4" ) ); break;
2474 case NF_DATE_DIN_MMDD: aFuncResult.AssignAscii( RTL_CONSTASCII_STRINGPARAM( "D5" ) ); break;
2475 case NF_TIME_HHMMSSAMPM: aFuncResult.AssignAscii( RTL_CONSTASCII_STRINGPARAM( "D6" ) ); break;
2476 case NF_TIME_HHMMAMPM: aFuncResult.AssignAscii( RTL_CONSTASCII_STRINGPARAM( "D7" ) ); break;
2477 case NF_TIME_HHMMSS: aFuncResult.AssignAscii( RTL_CONSTASCII_STRINGPARAM( "D8" ) ); break;
2478 case NF_TIME_HHMM: aFuncResult.AssignAscii( RTL_CONSTASCII_STRINGPARAM( "D9" ) ); break;
2479 default: aFuncResult = 'G';
2480 }
2481 }
2482 }
2483 if( bAppendPrec )
2484 aFuncResult += String::CreateFromInt32( nPrec );
2485 const SvNumberformat* pFormat = pFormatter->GetEntry( nFormat );
2486 if( lcl_FormatHasNegColor( pFormat ) )
2487 aFuncResult += '-';
2488 if( lcl_FormatHasOpenPar( pFormat ) )
2489 aFuncResult.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "()" ) );
2490 PushString( aFuncResult );
2491 }
2492 else if( aInfoType.EqualsAscii( "COLOR" ) )
2493 { // 1 = negative values are colored, otherwise 0
2494 const SvNumberformat* pFormat = pFormatter->GetEntry( pDok->GetNumberFormat( aCellPos ) );
2495 PushInt( lcl_FormatHasNegColor( pFormat ) ? 1 : 0 );
2496 }
2497 else if( aInfoType.EqualsAscii( "PARENTHESES" ) )
2498 { // 1 = format string contains a '(' character, otherwise 0
2499 const SvNumberformat* pFormat = pFormatter->GetEntry( pDok->GetNumberFormat( aCellPos ) );
2500 PushInt( lcl_FormatHasOpenPar( pFormat ) ? 1 : 0 );
2501 }
2502 else
2503 PushIllegalArgument();
2504 }
2505 }
2506 }
2507
2508
ScIsRef()2509 void ScInterpreter::ScIsRef()
2510 {
2511 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScCell" );
2512 nFuncFmtType = NUMBERFORMAT_LOGICAL;
2513 short nRes = 0;
2514 switch ( GetStackType() )
2515 {
2516 case svSingleRef :
2517 {
2518 ScAddress aAdr;
2519 PopSingleRef( aAdr );
2520 if ( !nGlobalError )
2521 nRes = 1;
2522 }
2523 break;
2524 case svDoubleRef :
2525 {
2526 ScRange aRange;
2527 PopDoubleRef( aRange );
2528 if ( !nGlobalError )
2529 nRes = 1;
2530 }
2531 break;
2532 case svRefList :
2533 {
2534 FormulaTokenRef x = PopToken();
2535 if ( !nGlobalError )
2536 nRes = !static_cast<ScToken*>(x.get())->GetRefList()->empty();
2537 }
2538 break;
2539 default:
2540 Pop();
2541 }
2542 nGlobalError = 0;
2543 PushInt( nRes );
2544 }
2545
2546
ScIsValue()2547 void ScInterpreter::ScIsValue()
2548 {
2549 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScIsValue" );
2550 nFuncFmtType = NUMBERFORMAT_LOGICAL;
2551 short nRes = 0;
2552 switch ( GetRawStackType() )
2553 {
2554 case svDouble:
2555 Pop();
2556 nRes = 1;
2557 break;
2558 case svDoubleRef :
2559 case svSingleRef :
2560 {
2561 ScAddress aAdr;
2562 if ( !PopDoubleRefOrSingleRef( aAdr ) )
2563 break;
2564 ScBaseCell* pCell = GetCell( aAdr );
2565 if (GetCellErrCode( pCell ) == 0)
2566 {
2567 switch ( GetCellType( pCell ) )
2568 {
2569 case CELLTYPE_VALUE :
2570 nRes = 1;
2571 break;
2572 case CELLTYPE_FORMULA :
2573 nRes = ((ScFormulaCell*)pCell)->IsValue() &&
2574 !((ScFormulaCell*)pCell)->IsEmpty();
2575 break;
2576 default:
2577 ; // nothing
2578 }
2579 }
2580 }
2581 break;
2582 case svMatrix:
2583 {
2584 ScMatrixRef pMat = PopMatrix();
2585 if ( !pMat )
2586 ; // nothing
2587 else if ( !pJumpMatrix )
2588 {
2589 if (pMat->GetErrorIfNotString( 0 ) == 0)
2590 nRes = pMat->IsValue( 0 );
2591 }
2592 else
2593 {
2594 SCSIZE nCols, nRows, nC, nR;
2595 pMat->GetDimensions( nCols, nRows);
2596 pJumpMatrix->GetPos( nC, nR);
2597 if ( nC < nCols && nR < nRows )
2598 if (pMat->GetErrorIfNotString( nC, nR) == 0)
2599 nRes = pMat->IsValue( nC, nR);
2600 }
2601 }
2602 break;
2603 default:
2604 Pop();
2605 }
2606 nGlobalError = 0;
2607 PushInt( nRes );
2608 }
2609
2610
ScIsFormula()2611 void ScInterpreter::ScIsFormula()
2612 {
2613 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScIsFormula" );
2614 nFuncFmtType = NUMBERFORMAT_LOGICAL;
2615 short nRes = 0;
2616 switch ( GetStackType() )
2617 {
2618 case svDoubleRef :
2619 case svSingleRef :
2620 {
2621 ScAddress aAdr;
2622 if ( !PopDoubleRefOrSingleRef( aAdr ) )
2623 break;
2624 nRes = (GetCellType( GetCell( aAdr ) ) == CELLTYPE_FORMULA);
2625 }
2626 break;
2627 default:
2628 Pop();
2629 }
2630 nGlobalError = 0;
2631 PushInt( nRes );
2632 }
2633
2634
ScFormula()2635 void ScInterpreter::ScFormula()
2636 {
2637 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScFormula" );
2638 String aFormula;
2639 switch ( GetStackType() )
2640 {
2641 case svDoubleRef :
2642 case svSingleRef :
2643 {
2644 ScAddress aAdr;
2645 if ( !PopDoubleRefOrSingleRef( aAdr ) )
2646 break;
2647 ScBaseCell* pCell = GetCell( aAdr );
2648 switch ( GetCellType( pCell ) )
2649 {
2650 case CELLTYPE_FORMULA :
2651 ((ScFormulaCell*)pCell)->GetFormula( aFormula );
2652 break;
2653 default:
2654 SetError( NOTAVAILABLE );
2655 }
2656 }
2657 break;
2658 default:
2659 Pop();
2660 SetError( NOTAVAILABLE );
2661 }
2662 PushString( aFormula );
2663 }
2664
2665
2666
ScIsNV()2667 void ScInterpreter::ScIsNV()
2668 {
2669 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScIsNV" );
2670 nFuncFmtType = NUMBERFORMAT_LOGICAL;
2671 short nRes = 0;
2672 switch ( GetStackType() )
2673 {
2674 case svDoubleRef :
2675 case svSingleRef :
2676 {
2677 ScAddress aAdr;
2678 PopDoubleRefOrSingleRef( aAdr );
2679 if ( nGlobalError == NOTAVAILABLE )
2680 nRes = 1;
2681 else
2682 {
2683 ScBaseCell* pCell = GetCell( aAdr );
2684 sal_uInt16 nErr = GetCellErrCode( pCell );
2685 nRes = (nErr == NOTAVAILABLE);
2686 }
2687 }
2688 break;
2689 case svMatrix:
2690 {
2691 ScMatrixRef pMat = PopMatrix();
2692 if ( !pMat )
2693 ; // nothing
2694 else if ( !pJumpMatrix )
2695 nRes = (pMat->GetErrorIfNotString( 0 ) == NOTAVAILABLE);
2696 else
2697 {
2698 SCSIZE nCols, nRows, nC, nR;
2699 pMat->GetDimensions( nCols, nRows);
2700 pJumpMatrix->GetPos( nC, nR);
2701 if ( nC < nCols && nR < nRows )
2702 nRes = (pMat->GetErrorIfNotString( nC, nR) == NOTAVAILABLE);
2703 }
2704 }
2705 break;
2706 default:
2707 PopError();
2708 if ( nGlobalError == NOTAVAILABLE )
2709 nRes = 1;
2710 }
2711 nGlobalError = 0;
2712 PushInt( nRes );
2713 }
2714
2715
ScIsErr()2716 void ScInterpreter::ScIsErr()
2717 {
2718 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScIsErr" );
2719 nFuncFmtType = NUMBERFORMAT_LOGICAL;
2720 short nRes = 0;
2721 switch ( GetStackType() )
2722 {
2723 case svDoubleRef :
2724 case svSingleRef :
2725 {
2726 ScAddress aAdr;
2727 PopDoubleRefOrSingleRef( aAdr );
2728 if ( nGlobalError && nGlobalError != NOTAVAILABLE )
2729 nRes = 1;
2730 else
2731 {
2732 ScBaseCell* pCell = GetCell( aAdr );
2733 sal_uInt16 nErr = GetCellErrCode( pCell );
2734 nRes = (nErr && nErr != NOTAVAILABLE);
2735 }
2736 }
2737 break;
2738 case svMatrix:
2739 {
2740 ScMatrixRef pMat = PopMatrix();
2741 if ( nGlobalError || !pMat )
2742 nRes = ((nGlobalError && nGlobalError != NOTAVAILABLE) || !pMat);
2743 else if ( !pJumpMatrix )
2744 {
2745 sal_uInt16 nErr = pMat->GetErrorIfNotString( 0 );
2746 nRes = (nErr && nErr != NOTAVAILABLE);
2747 }
2748 else
2749 {
2750 SCSIZE nCols, nRows, nC, nR;
2751 pMat->GetDimensions( nCols, nRows);
2752 pJumpMatrix->GetPos( nC, nR);
2753 if ( nC < nCols && nR < nRows )
2754 {
2755 sal_uInt16 nErr = pMat->GetErrorIfNotString( nC, nR);
2756 nRes = (nErr && nErr != NOTAVAILABLE);
2757 }
2758 }
2759 }
2760 break;
2761 default:
2762 PopError();
2763 if ( nGlobalError && nGlobalError != NOTAVAILABLE )
2764 nRes = 1;
2765 }
2766 nGlobalError = 0;
2767 PushInt( nRes );
2768 }
2769
2770
ScIsError()2771 void ScInterpreter::ScIsError()
2772 {
2773 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScIsError" );
2774 nFuncFmtType = NUMBERFORMAT_LOGICAL;
2775 short nRes = 0;
2776 switch ( GetStackType() )
2777 {
2778 case svDoubleRef :
2779 case svSingleRef :
2780 {
2781 ScAddress aAdr;
2782 if ( !PopDoubleRefOrSingleRef( aAdr ) )
2783 {
2784 nRes = 1;
2785 break;
2786 }
2787 if ( nGlobalError )
2788 nRes = 1;
2789 else
2790 {
2791 ScBaseCell* pCell = GetCell( aAdr );
2792 nRes = (GetCellErrCode( pCell ) != 0);
2793 }
2794 }
2795 break;
2796 case svMatrix:
2797 {
2798 ScMatrixRef pMat = PopMatrix();
2799 if ( nGlobalError || !pMat )
2800 nRes = 1;
2801 else if ( !pJumpMatrix )
2802 nRes = (pMat->GetErrorIfNotString( 0 ) != 0);
2803 else
2804 {
2805 SCSIZE nCols, nRows, nC, nR;
2806 pMat->GetDimensions( nCols, nRows);
2807 pJumpMatrix->GetPos( nC, nR);
2808 if ( nC < nCols && nR < nRows )
2809 nRes = (pMat->GetErrorIfNotString( nC, nR) != 0);
2810 }
2811 }
2812 break;
2813 default:
2814 PopError();
2815 if ( nGlobalError )
2816 nRes = 1;
2817 }
2818 nGlobalError = 0;
2819 PushInt( nRes );
2820 }
2821
2822
IsEven()2823 short ScInterpreter::IsEven()
2824 {
2825 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::IsEven" );
2826 nFuncFmtType = NUMBERFORMAT_LOGICAL;
2827 short nRes = 0;
2828 double fVal = 0.0;
2829 switch ( GetStackType() )
2830 {
2831 case svDoubleRef :
2832 case svSingleRef :
2833 {
2834 ScAddress aAdr;
2835 if ( !PopDoubleRefOrSingleRef( aAdr ) )
2836 break;
2837 ScBaseCell* pCell = GetCell( aAdr );
2838 sal_uInt16 nErr = GetCellErrCode( pCell );
2839 if (nErr != 0)
2840 SetError(nErr);
2841 else
2842 {
2843 switch ( GetCellType( pCell ) )
2844 {
2845 case CELLTYPE_VALUE :
2846 fVal = GetCellValue( aAdr, pCell );
2847 nRes = 1;
2848 break;
2849 case CELLTYPE_FORMULA :
2850 if( ((ScFormulaCell*)pCell)->IsValue() )
2851 {
2852 fVal = GetCellValue( aAdr, pCell );
2853 nRes = 1;
2854 }
2855 break;
2856 default:
2857 ; // nothing
2858 }
2859 }
2860 }
2861 break;
2862 case svDouble:
2863 {
2864 fVal = PopDouble();
2865 nRes = 1;
2866 }
2867 break;
2868 case svMatrix:
2869 {
2870 ScMatrixRef pMat = PopMatrix();
2871 if ( !pMat )
2872 ; // nothing
2873 else if ( !pJumpMatrix )
2874 {
2875 nRes = pMat->IsValue( 0 );
2876 if ( nRes )
2877 fVal = pMat->GetDouble( 0 );
2878 }
2879 else
2880 {
2881 SCSIZE nCols, nRows, nC, nR;
2882 pMat->GetDimensions( nCols, nRows);
2883 pJumpMatrix->GetPos( nC, nR);
2884 if ( nC < nCols && nR < nRows )
2885 {
2886 nRes = pMat->IsValue( nC, nR);
2887 if ( nRes )
2888 fVal = pMat->GetDouble( nC, nR);
2889 }
2890 else
2891 SetError( errNoValue);
2892 }
2893 }
2894 break;
2895 default:
2896 ; // nothing
2897 }
2898 if ( !nRes )
2899 SetError( errIllegalParameter);
2900 else
2901 nRes = ( fmod( ::rtl::math::approxFloor( fabs( fVal ) ), 2.0 ) < 0.5 );
2902 return nRes;
2903 }
2904
2905
ScIsEven()2906 void ScInterpreter::ScIsEven()
2907 {
2908 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScIsEven" );
2909 PushInt( IsEven() );
2910 }
2911
2912
ScIsOdd()2913 void ScInterpreter::ScIsOdd()
2914 {
2915 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScIsOdd" );
2916 PushInt( !IsEven() );
2917 }
2918
2919
ScN()2920 void ScInterpreter::ScN()
2921 {
2922 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScN" );
2923 sal_uInt16 nErr = nGlobalError;
2924 nGlobalError = 0;
2925 // Temporarily override the ConvertStringToValue() error for
2926 // GetCellValue() / GetCellValueOrZero()
2927 sal_uInt16 nSErr = mnStringNoValueError;
2928 mnStringNoValueError = errCellNoValue;
2929 double fVal = GetDouble();
2930 mnStringNoValueError = nSErr;
2931 if ( nGlobalError == NOTAVAILABLE || nGlobalError == errCellNoValue )
2932 nGlobalError = 0; // N(#NA) and N("text") are ok
2933 if ( !nGlobalError && nErr != NOTAVAILABLE )
2934 nGlobalError = nErr;
2935 PushDouble( fVal );
2936 }
2937
2938
ScTrim()2939 void ScInterpreter::ScTrim()
2940 { // trimmt nicht nur sondern schnibbelt auch doppelte raus!
2941 String aVal( GetString() );
2942 aVal.EraseLeadingChars();
2943 aVal.EraseTrailingChars();
2944 String aStr;
2945 const sal_Unicode* p = aVal.GetBuffer();
2946 const sal_Unicode* const pEnd = p + aVal.Len();
2947 while ( p < pEnd )
2948 {
2949 if ( *p != ' ' || p[-1] != ' ' ) // erster kann kein ' ' sein, -1 ist also ok
2950 aStr += *p;
2951 p++;
2952 }
2953 PushString( aStr );
2954 }
2955
2956
ScUpper()2957 void ScInterpreter::ScUpper()
2958 {
2959 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScTrim" );
2960 String aString = GetString();
2961 ScGlobal::pCharClass->toUpper(aString);
2962 PushString(aString);
2963 }
2964
2965
ScPropper()2966 void ScInterpreter::ScPropper()
2967 {
2968 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScPropper" );
2969 // TODO: what to do with I18N-CJK ?!?
2970 String aStr( GetString() );
2971 const xub_StrLen nLen = aStr.Len();
2972 // #i82487# don't try to write to empty string's BufferAccess
2973 // (would crash now that the empty string is const)
2974 if ( nLen > 0 )
2975 {
2976 String aUpr( ScGlobal::pCharClass->upper( aStr ) );
2977 String aLwr( ScGlobal::pCharClass->lower( aStr ) );
2978 sal_Unicode* pStr = aStr.GetBufferAccess();
2979 const sal_Unicode* pUpr = aUpr.GetBuffer();
2980 const sal_Unicode* pLwr = aLwr.GetBuffer();
2981 *pStr = *pUpr;
2982 String aTmpStr( 'x' );
2983 xub_StrLen nPos = 1;
2984 while( nPos < nLen )
2985 {
2986 aTmpStr.SetChar( 0, pStr[nPos-1] );
2987 if ( !ScGlobal::pCharClass->isLetter( aTmpStr, 0 ) )
2988 pStr[nPos] = pUpr[nPos];
2989 else
2990 pStr[nPos] = pLwr[nPos];
2991 nPos++;
2992 }
2993 aStr.ReleaseBufferAccess( nLen );
2994 }
2995 PushString( aStr );
2996 }
2997
2998
ScLower()2999 void ScInterpreter::ScLower()
3000 {
3001 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScLower" );
3002 String aString( GetString() );
3003 ScGlobal::pCharClass->toLower(aString);
3004 PushString(aString);
3005 }
3006
3007
ScLen()3008 void ScInterpreter::ScLen()
3009 {
3010 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScLen" );
3011 String aStr( GetString() );
3012 PushDouble( aStr.Len() );
3013 }
3014
3015
ScT()3016 void ScInterpreter::ScT()
3017 {
3018 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScT" );
3019 switch ( GetStackType() )
3020 {
3021 case svDoubleRef :
3022 case svSingleRef :
3023 {
3024 ScAddress aAdr;
3025 if ( !PopDoubleRefOrSingleRef( aAdr ) )
3026 {
3027 PushInt(0);
3028 return ;
3029 }
3030 sal_Bool bValue = sal_False;
3031 ScBaseCell* pCell = GetCell( aAdr );
3032 if ( GetCellErrCode( pCell ) == 0 )
3033 {
3034 switch ( GetCellType( pCell ) )
3035 {
3036 case CELLTYPE_VALUE :
3037 bValue = sal_True;
3038 break;
3039 case CELLTYPE_FORMULA :
3040 bValue = ((ScFormulaCell*)pCell)->IsValue();
3041 break;
3042 default:
3043 ; // nothing
3044 }
3045 }
3046 if ( bValue )
3047 PushString( EMPTY_STRING );
3048 else
3049 {
3050 // wie GetString()
3051 GetCellString( aTempStr, pCell );
3052 PushString( aTempStr );
3053 }
3054 }
3055 break;
3056 case svDouble :
3057 {
3058 PopError();
3059 PushString( EMPTY_STRING );
3060 }
3061 break;
3062 case svString :
3063 ; // leave on stack
3064 break;
3065 default :
3066 PushError( errUnknownOpCode);
3067 }
3068 }
3069
3070
3071 // Lexical form of xsd:float without INF and NaN.
lcl_IsPlainFloat(const String & rStr)3072 static bool lcl_IsPlainFloat( const String& rStr )
3073 {
3074 xub_StrLen n = rStr.Len(), i = 0;
3075 if ( i < n && (rStr.GetChar(i) == '+' || rStr.GetChar(i) == '-') )
3076 ++i;
3077 xub_StrLen nDigits = 0;
3078 while ( i < n && rStr.GetChar(i) >= '0' && rStr.GetChar(i) <= '9' )
3079 ++i, ++nDigits;
3080 if ( i < n && rStr.GetChar(i) == '.' )
3081 {
3082 ++i;
3083 while ( i < n && rStr.GetChar(i) >= '0' && rStr.GetChar(i) <= '9' )
3084 ++i, ++nDigits;
3085 }
3086 if ( !nDigits )
3087 return false;
3088 if ( i < n && (rStr.GetChar(i) == 'e' || rStr.GetChar(i) == 'E') )
3089 {
3090 ++i;
3091 if ( i < n && (rStr.GetChar(i) == '+' || rStr.GetChar(i) == '-') )
3092 ++i;
3093 xub_StrLen nExp = 0;
3094 while ( i < n && rStr.GetChar(i) >= '0' && rStr.GetChar(i) <= '9' )
3095 ++i, ++nExp;
3096 if ( !nExp )
3097 return false;
3098 }
3099 return i == n;
3100 }
3101
ScNumberValue()3102 void ScInterpreter::ScNumberValue()
3103 {
3104 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScNumberValue" );
3105 sal_uInt8 nParamCount = GetByte();
3106 if ( !MustHaveParamCount( nParamCount, 1, 3 ) )
3107 return;
3108
3109 String aGroupSep, aDecSep;
3110 if ( nParamCount == 3 )
3111 aGroupSep = GetString();
3112 if ( nParamCount >= 2 )
3113 aDecSep = GetString();
3114 else
3115 aDecSep = pFormatter->GetNumDecimalSep();
3116 if ( nParamCount < 3 )
3117 {
3118 aGroupSep = pFormatter->GetNumThousandSep();
3119 // The locale's group separator must not cancel an explicit decimal separator.
3120 if ( aDecSep.Len() == 1 && aGroupSep.Search( aDecSep.GetChar(0) ) != STRING_NOTFOUND )
3121 aGroupSep.Erase();
3122 }
3123 String aText = GetString();
3124 if ( nGlobalError )
3125 {
3126 PushError( nGlobalError );
3127 return;
3128 }
3129 if ( aDecSep.Len() != 1 || aGroupSep.Search( aDecSep.GetChar(0) ) != STRING_NOTFOUND )
3130 {
3131 PushIllegalArgument();
3132 return;
3133 }
3134
3135 // ODF 1.2 6.13.28: group separators before the decimal separator go, the
3136 // first decimal separator becomes '.', whitespace goes, then trailing '%'.
3137 sal_Unicode cDecSep = aDecSep.GetChar(0);
3138 String aNum;
3139 bool bDecimal = false;
3140 for ( xub_StrLen i = 0; i < aText.Len(); ++i )
3141 {
3142 sal_Unicode c = aText.GetChar(i);
3143 if ( !bDecimal && c == cDecSep )
3144 {
3145 bDecimal = true;
3146 aNum.Append( sal_Unicode('.') );
3147 }
3148 else if ( !bDecimal && aGroupSep.Search( c ) != STRING_NOTFOUND )
3149 ;
3150 else if ( c != ' ' && c != '\t' && c != '\n' && c != '\r' )
3151 aNum.Append( c );
3152 }
3153 int nPercent = 0;
3154 while ( aNum.Len() && aNum.GetChar( aNum.Len() - 1 ) == '%' )
3155 {
3156 aNum.Erase( aNum.Len() - 1 );
3157 ++nPercent;
3158 }
3159 if ( !lcl_IsPlainFloat( aNum ) )
3160 {
3161 PushIllegalArgument();
3162 return;
3163 }
3164 rtl_math_ConversionStatus eStatus;
3165 double fVal = ::rtl::math::stringToDouble( aNum, '.', 0, &eStatus, NULL );
3166 if ( eStatus != rtl_math_ConversionStatus_Ok )
3167 {
3168 PushIllegalArgument();
3169 return;
3170 }
3171 while ( nPercent-- > 0 )
3172 fVal /= 100.0;
3173 PushDouble( fVal );
3174 }
3175
ScValue()3176 void ScInterpreter::ScValue()
3177 {
3178 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScValue" );
3179 String aInputString;
3180 double fVal;
3181
3182 switch ( GetRawStackType() )
3183 {
3184 case svMissing:
3185 case svEmptyCell:
3186 Pop();
3187 PushInt(0);
3188 return;
3189 case svDouble:
3190 return; // leave on stack
3191 //break;
3192
3193 case svSingleRef:
3194 case svDoubleRef:
3195 {
3196 ScAddress aAdr;
3197 if ( !PopDoubleRefOrSingleRef( aAdr ) )
3198 {
3199 PushInt(0);
3200 return;
3201 }
3202 ScBaseCell* pCell = GetCell( aAdr );
3203 if ( pCell && pCell->HasStringData() )
3204 GetCellString( aInputString, pCell );
3205 else if ( pCell && pCell->HasValueData() )
3206 {
3207 PushDouble( GetCellValue(aAdr, pCell) );
3208 return;
3209 }
3210 else
3211 {
3212 PushDouble(0.0);
3213 return;
3214 }
3215 }
3216 break;
3217 case svMatrix:
3218 {
3219 ScMatValType nType = GetDoubleOrStringFromMatrix( fVal,
3220 aInputString);
3221 switch (nType)
3222 {
3223 case SC_MATVAL_EMPTY:
3224 fVal = 0.0;
3225 // fallthru
3226 case SC_MATVAL_VALUE:
3227 case SC_MATVAL_BOOLEAN:
3228 PushDouble( fVal);
3229 return;
3230 //break;
3231 case SC_MATVAL_STRING:
3232 // evaluated below
3233 break;
3234 default:
3235 PushIllegalArgument();
3236 }
3237 }
3238 break;
3239 default:
3240 aInputString = GetString();
3241 break;
3242 }
3243
3244 sal_uInt32 nFIndex = 0; // 0 for default locale
3245 if (pFormatter->IsNumberFormat(aInputString, nFIndex, fVal))
3246 PushDouble(fVal);
3247 else
3248 PushIllegalArgument();
3249 }
3250
3251
3252 // TODO: this should be a proper unicode string method
lcl_ScInterpreter_IsPrintable(sal_Unicode c)3253 inline sal_Bool lcl_ScInterpreter_IsPrintable( sal_Unicode c )
3254 {
3255 return 0x20 <= c && c != 0x7f;
3256 }
3257
ScClean()3258 void ScInterpreter::ScClean()
3259 {
3260 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScClean" );
3261 String aStr( GetString() );
3262 for ( xub_StrLen i = 0; i < aStr.Len(); i++ )
3263 {
3264 if ( !lcl_ScInterpreter_IsPrintable( aStr.GetChar( i ) ) )
3265 aStr.Erase(i,1);
3266 }
3267 PushString(aStr);
3268 }
3269
3270
ScCode()3271 void ScInterpreter::ScCode()
3272 {
3273 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScCode" );
3274 // TODO: make it full range unicode?
3275 const String& rStr = GetString();
3276 PushInt( (sal_uChar) ByteString::ConvertFromUnicode( rStr.GetChar(0), gsl_getSystemTextEncoding() ) );
3277 }
3278
3279
ScChar()3280 void ScInterpreter::ScChar()
3281 {
3282 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScChar" );
3283 // TODO: make it full range unicode?
3284 double fVal = GetDouble();
3285 if (fVal < 0.0 || fVal >= 256.0)
3286 PushIllegalArgument();
3287 else
3288 {
3289 String aStr( '0' );
3290 aStr.SetChar( 0, ByteString::ConvertToUnicode( (sal_Char) fVal, gsl_getSystemTextEncoding() ) );
3291 PushString( aStr );
3292 }
3293 }
3294
3295
3296 /* #i70213# fullwidth/halfwidth conversion provided by
3297 * Takashi Nakamoto <bluedwarf@ooo>
3298 * erAck: added Excel compatibility conversions as seen in issue's test case. */
3299
lcl_convertIntoHalfWidth(const::rtl::OUString & rStr)3300 static ::rtl::OUString lcl_convertIntoHalfWidth( const ::rtl::OUString & rStr )
3301 {
3302 static bool bFirstASCCall = true;
3303 static utl::TransliterationWrapper aTrans( ::comphelper::getProcessServiceFactory(), 0 );
3304
3305 if( bFirstASCCall )
3306 {
3307 aTrans.loadModuleByImplName( ::rtl::OUString::createFromAscii( "FULLWIDTH_HALFWIDTH_LIKE_ASC" ), LANGUAGE_SYSTEM );
3308 bFirstASCCall = false;
3309 }
3310
3311 return aTrans.transliterate( rStr, 0, sal_uInt16( rStr.getLength() ), NULL );
3312 }
3313
3314
lcl_convertIntoFullWidth(const::rtl::OUString & rStr)3315 static ::rtl::OUString lcl_convertIntoFullWidth( const ::rtl::OUString & rStr )
3316 {
3317 static bool bFirstJISCall = true;
3318 static utl::TransliterationWrapper aTrans( ::comphelper::getProcessServiceFactory(), 0 );
3319
3320 if( bFirstJISCall )
3321 {
3322 aTrans.loadModuleByImplName( ::rtl::OUString::createFromAscii( "HALFWIDTH_FULLWIDTH_LIKE_JIS" ), LANGUAGE_SYSTEM );
3323 bFirstJISCall = false;
3324 }
3325
3326 return aTrans.transliterate( rStr, 0, sal_uInt16( rStr.getLength() ), NULL );
3327 }
3328
3329
3330 /* ODFF:
3331 * Summary: Converts half-width to full-width ASCII and katakana characters.
3332 * Semantics: Conversion is done for half-width ASCII and katakana characters,
3333 * other characters are simply copied from T to the result. This is the
3334 * complementary function to ASC.
3335 * For references regarding halfwidth and fullwidth characters see
3336 * http://www.unicode.org/reports/tr11/
3337 * http://www.unicode.org/charts/charindex2.html#H
3338 * http://www.unicode.org/charts/charindex2.html#F
3339 */
ScJis()3340 void ScInterpreter::ScJis()
3341 {
3342 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScJis" );
3343 if (MustHaveParamCount( GetByte(), 1))
3344 PushString( lcl_convertIntoFullWidth( GetString()));
3345 }
3346
3347
3348 /* ODFF:
3349 * Summary: Converts full-width to half-width ASCII and katakana characters.
3350 * Semantics: Conversion is done for full-width ASCII and katakana characters,
3351 * other characters are simply copied from T to the result. This is the
3352 * complementary function to JIS.
3353 */
ScAsc()3354 void ScInterpreter::ScAsc()
3355 {
3356 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScAsc" );
3357 if (MustHaveParamCount( GetByte(), 1))
3358 PushString( lcl_convertIntoHalfWidth( GetString()));
3359 }
3360
ScUnicode()3361 void ScInterpreter::ScUnicode()
3362 {
3363 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScUnicode" );
3364 if ( MustHaveParamCount( GetByte(), 1 ) )
3365 {
3366 const rtl::OUString& rStr = GetString();
3367 if (rStr.getLength() <= 0)
3368 PushIllegalParameter();
3369 else
3370 {
3371 sal_Int32 i = 0;
3372 PushDouble( rStr.iterateCodePoints(&i) );
3373 }
3374 }
3375 }
3376
ScUnichar()3377 void ScInterpreter::ScUnichar()
3378 {
3379 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScUnichar" );
3380 if ( MustHaveParamCount( GetByte(), 1 ) )
3381 {
3382 double dVal = ::rtl::math::approxFloor( GetDouble() );
3383 if ((dVal < 0x000000) || (dVal > 0x10FFFF))
3384 PushIllegalArgument();
3385 else
3386 {
3387 sal_uInt32 nCodePoint = static_cast<sal_uInt32>( dVal );
3388 rtl::OUString aStr( &nCodePoint, 1 );
3389 PushString( aStr );
3390 }
3391 }
3392 }
3393
3394
ScMin(sal_Bool bTextAsZero)3395 void ScInterpreter::ScMin( sal_Bool bTextAsZero )
3396 {
3397 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScMin" );
3398 short nParamCount = GetByte();
3399 if (!MustHaveParamCountMin( nParamCount, 1))
3400 return;
3401 double nMin = ::std::numeric_limits<double>::max();
3402 double nVal = 0.0;
3403 ScAddress aAdr;
3404 ScRange aRange;
3405 size_t nRefInList = 0;
3406 while (nParamCount-- > 0)
3407 {
3408 switch (GetStackType())
3409 {
3410 case svDouble :
3411 {
3412 nVal = GetDouble();
3413 if (nMin > nVal) nMin = nVal;
3414 nFuncFmtType = NUMBERFORMAT_NUMBER;
3415 }
3416 break;
3417 case svSingleRef :
3418 {
3419 PopSingleRef( aAdr );
3420 ScBaseCell* pCell = GetCell( aAdr );
3421 if (HasCellValueData(pCell))
3422 {
3423 nVal = GetCellValue( aAdr, pCell );
3424 CurFmtToFuncFmt();
3425 if (nMin > nVal) nMin = nVal;
3426 }
3427 else if ( bTextAsZero && HasCellStringData( pCell ) )
3428 {
3429 if ( nMin > 0.0 )
3430 nMin = 0.0;
3431 }
3432 }
3433 break;
3434 case svDoubleRef :
3435 case svRefList :
3436 {
3437 sal_uInt16 nErr = 0;
3438 PopDoubleRef( aRange, nParamCount, nRefInList);
3439 ScValueIterator aValIter( pDok, aRange, glSubTotal, bTextAsZero );
3440 if (aValIter.GetFirst(nVal, nErr))
3441 {
3442 if (nMin > nVal)
3443 nMin = nVal;
3444 aValIter.GetCurNumFmtInfo( nFuncFmtType, nFuncFmtIndex );
3445 while ((nErr == 0) && aValIter.GetNext(nVal, nErr))
3446 {
3447 if (nMin > nVal)
3448 nMin = nVal;
3449 }
3450 SetError(nErr);
3451 }
3452 }
3453 break;
3454 case svMatrix :
3455 {
3456 ScMatrixRef pMat = PopMatrix();
3457 if (pMat)
3458 {
3459 SCSIZE nC, nR;
3460 nFuncFmtType = NUMBERFORMAT_NUMBER;
3461 pMat->GetDimensions(nC, nR);
3462 if (pMat->IsNumeric())
3463 {
3464 for (SCSIZE nMatCol = 0; nMatCol < nC; nMatCol++)
3465 for (SCSIZE nMatRow = 0; nMatRow < nR; nMatRow++)
3466 {
3467 nVal = pMat->GetDouble(nMatCol,nMatRow);
3468 if (nMin > nVal) nMin = nVal;
3469 }
3470 }
3471 else
3472 {
3473 for (SCSIZE nMatCol = 0; nMatCol < nC; nMatCol++)
3474 {
3475 for (SCSIZE nMatRow = 0; nMatRow < nR; nMatRow++)
3476 {
3477 if (!pMat->IsString(nMatCol,nMatRow))
3478 {
3479 nVal = pMat->GetDouble(nMatCol,nMatRow);
3480 if (nMin > nVal) nMin = nVal;
3481 }
3482 else if ( bTextAsZero )
3483 {
3484 if ( nMin > 0.0 )
3485 nMin = 0.0;
3486 }
3487 }
3488 }
3489 }
3490 }
3491 }
3492 break;
3493 case svString :
3494 {
3495 Pop();
3496 if ( bTextAsZero )
3497 {
3498 if ( nMin > 0.0 )
3499 nMin = 0.0;
3500 }
3501 else
3502 SetError(errIllegalParameter);
3503 }
3504 break;
3505 default :
3506 Pop();
3507 SetError(errIllegalParameter);
3508 }
3509 }
3510 if ( nVal < nMin )
3511 PushDouble(0.0);
3512 else
3513 PushDouble(nMin);
3514 }
3515
ScMax(sal_Bool bTextAsZero)3516 void ScInterpreter::ScMax( sal_Bool bTextAsZero )
3517 {
3518 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScMax" );
3519 short nParamCount = GetByte();
3520 if (!MustHaveParamCountMin( nParamCount, 1))
3521 return;
3522 double nMax = -(::std::numeric_limits<double>::max());
3523 double nVal = 0.0;
3524 ScAddress aAdr;
3525 ScRange aRange;
3526 size_t nRefInList = 0;
3527 while (nParamCount-- > 0)
3528 {
3529 switch (GetStackType())
3530 {
3531 case svDouble :
3532 {
3533 nVal = GetDouble();
3534 if (nMax < nVal) nMax = nVal;
3535 nFuncFmtType = NUMBERFORMAT_NUMBER;
3536 }
3537 break;
3538 case svSingleRef :
3539 {
3540 PopSingleRef( aAdr );
3541 ScBaseCell* pCell = GetCell( aAdr );
3542 if (HasCellValueData(pCell))
3543 {
3544 nVal = GetCellValue( aAdr, pCell );
3545 CurFmtToFuncFmt();
3546 if (nMax < nVal) nMax = nVal;
3547 }
3548 else if ( bTextAsZero && HasCellStringData( pCell ) )
3549 {
3550 if ( nMax < 0.0 )
3551 nMax = 0.0;
3552 }
3553 }
3554 break;
3555 case svDoubleRef :
3556 case svRefList :
3557 {
3558 sal_uInt16 nErr = 0;
3559 PopDoubleRef( aRange, nParamCount, nRefInList);
3560 ScValueIterator aValIter( pDok, aRange, glSubTotal, bTextAsZero );
3561 if (aValIter.GetFirst(nVal, nErr))
3562 {
3563 if (nMax < nVal)
3564 nMax = nVal;
3565 aValIter.GetCurNumFmtInfo( nFuncFmtType, nFuncFmtIndex );
3566 while ((nErr == 0) && aValIter.GetNext(nVal, nErr))
3567 {
3568 if (nMax < nVal)
3569 nMax = nVal;
3570 }
3571 SetError(nErr);
3572 }
3573 }
3574 break;
3575 case svMatrix :
3576 {
3577 ScMatrixRef pMat = PopMatrix();
3578 if (pMat)
3579 {
3580 nFuncFmtType = NUMBERFORMAT_NUMBER;
3581 SCSIZE nC, nR;
3582 pMat->GetDimensions(nC, nR);
3583 if (pMat->IsNumeric())
3584 {
3585 for (SCSIZE nMatCol = 0; nMatCol < nC; nMatCol++)
3586 for (SCSIZE nMatRow = 0; nMatRow < nR; nMatRow++)
3587 {
3588 nVal = pMat->GetDouble(nMatCol,nMatRow);
3589 if (nMax < nVal) nMax = nVal;
3590 }
3591 }
3592 else
3593 {
3594 for (SCSIZE nMatCol = 0; nMatCol < nC; nMatCol++)
3595 {
3596 for (SCSIZE nMatRow = 0; nMatRow < nR; nMatRow++)
3597 {
3598 if (!pMat->IsString(nMatCol,nMatRow))
3599 {
3600 nVal = pMat->GetDouble(nMatCol,nMatRow);
3601 if (nMax < nVal) nMax = nVal;
3602 }
3603 else if ( bTextAsZero )
3604 {
3605 if ( nMax < 0.0 )
3606 nMax = 0.0;
3607 }
3608 }
3609 }
3610 }
3611 }
3612 }
3613 break;
3614 case svString :
3615 {
3616 Pop();
3617 if ( bTextAsZero )
3618 {
3619 if ( nMax < 0.0 )
3620 nMax = 0.0;
3621 }
3622 else
3623 SetError(errIllegalParameter);
3624 }
3625 break;
3626 default :
3627 Pop();
3628 SetError(errIllegalParameter);
3629 }
3630 }
3631 if ( nVal > nMax )
3632 PushDouble(0.0);
3633 else
3634 PushDouble(nMax);
3635 }
3636
IterateParameters(ScIterFunc eFunc,sal_Bool bTextAsZero)3637 double ScInterpreter::IterateParameters( ScIterFunc eFunc, sal_Bool bTextAsZero )
3638 {
3639 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::IterateParameters" );
3640 short nParamCount = GetByte();
3641 double fRes = ( eFunc == ifPRODUCT ) ? 1.0 : 0.0;
3642 double fVal = 0.0;
3643 double fMem = 0.0;
3644 sal_Bool bNull = sal_True;
3645 sal_uLong nCount = 0;
3646 ScAddress aAdr;
3647 ScRange aRange;
3648 size_t nRefInList = 0;
3649 if ( nGlobalError && ( eFunc == ifCOUNT2 || eFunc == ifCOUNT ) )
3650 nGlobalError = 0;
3651 while (nParamCount-- > 0)
3652 {
3653 switch (GetStackType())
3654 {
3655
3656 case svString:
3657 {
3658 if( eFunc == ifCOUNT )
3659 {
3660 String aStr( PopString() );
3661 sal_uInt32 nFIndex = 0; // damit default Land/Spr.
3662 if ( bTextAsZero || pFormatter->IsNumberFormat(aStr, nFIndex, fVal))
3663 nCount++;
3664 }
3665 else
3666 {
3667 switch ( eFunc )
3668 {
3669 case ifAVERAGE:
3670 case ifSUM:
3671 case ifSUMSQ:
3672 case ifPRODUCT:
3673 {
3674 if ( bTextAsZero )
3675 {
3676 Pop();
3677 nCount++;
3678 if ( eFunc == ifPRODUCT )
3679 fRes = 0.0;
3680 }
3681 else
3682 {
3683 while (nParamCount-- > 0)
3684 Pop();
3685 SetError( errNoValue );
3686 }
3687 }
3688 break;
3689 default:
3690 Pop();
3691 nCount++;
3692 }
3693 }
3694 }
3695 break;
3696 case svDouble :
3697 fVal = GetDouble();
3698 nCount++;
3699 switch( eFunc )
3700 {
3701 case ifAVERAGE:
3702 case ifSUM:
3703 if ( bNull && fVal != 0.0 )
3704 {
3705 bNull = sal_False;
3706 fMem = fVal;
3707 }
3708 else
3709 fRes += fVal;
3710 break;
3711 case ifSUMSQ: fRes += fVal * fVal; break;
3712 case ifPRODUCT: fRes *= fVal; break;
3713 default: ; // nothing
3714 }
3715 nFuncFmtType = NUMBERFORMAT_NUMBER;
3716 break;
3717 case svSingleRef :
3718 {
3719 PopSingleRef( aAdr );
3720 if ( nGlobalError && ( eFunc == ifCOUNT2 || eFunc == ifCOUNT ) )
3721 {
3722 nGlobalError = 0;
3723 if ( eFunc == ifCOUNT2 )
3724 ++nCount;
3725 break;
3726 }
3727 ScBaseCell* pCell = GetCell( aAdr );
3728 if ( pCell )
3729 {
3730 if( eFunc == ifCOUNT2 )
3731 {
3732 CellType eCellType = pCell->GetCellType();
3733 if (eCellType != CELLTYPE_NONE && eCellType != CELLTYPE_NOTE)
3734 nCount++;
3735 if ( nGlobalError )
3736 nGlobalError = 0;
3737 }
3738 else if ( pCell->HasValueData() )
3739 {
3740 nCount++;
3741 fVal = GetCellValue( aAdr, pCell );
3742 CurFmtToFuncFmt();
3743 switch( eFunc )
3744 {
3745 case ifAVERAGE:
3746 case ifSUM:
3747 if ( bNull && fVal != 0.0 )
3748 {
3749 bNull = sal_False;
3750 fMem = fVal;
3751 }
3752 else
3753 fRes += fVal;
3754 break;
3755 case ifSUMSQ: fRes += fVal * fVal; break;
3756 case ifPRODUCT: fRes *= fVal; break;
3757 case ifCOUNT:
3758 if ( nGlobalError )
3759 {
3760 nGlobalError = 0;
3761 nCount--;
3762 }
3763 break;
3764 default: ; // nothing
3765 }
3766 }
3767 else if ( bTextAsZero && pCell->HasStringData() )
3768 {
3769 nCount++;
3770 if ( eFunc == ifPRODUCT )
3771 fRes = 0.0;
3772 }
3773 }
3774 }
3775 break;
3776 case svDoubleRef :
3777 case svRefList :
3778 {
3779 sal_uInt16 nErr = 0;
3780 PopDoubleRef( aRange, nParamCount, nRefInList);
3781 if ( nGlobalError && ( eFunc == ifCOUNT2 || eFunc == ifCOUNT ) )
3782 {
3783 nGlobalError = 0;
3784 if ( eFunc == ifCOUNT2 )
3785 ++nCount;
3786 break;
3787 }
3788 if( eFunc == ifCOUNT2 )
3789 {
3790 ScBaseCell* pCell;
3791 ScCellIterator aIter( pDok, aRange, glSubTotal );
3792 if ( (pCell = aIter.GetFirst()) != NULL )
3793 {
3794 do
3795 {
3796 CellType eType = pCell->GetCellType();
3797 if( eType != CELLTYPE_NONE && eType != CELLTYPE_NOTE )
3798 nCount++;
3799 }
3800 while ( (pCell = aIter.GetNext()) != NULL );
3801 }
3802 if ( nGlobalError )
3803 nGlobalError = 0;
3804 }
3805 else
3806 {
3807 ScValueIterator aValIter( pDok, aRange, glSubTotal, bTextAsZero );
3808 if (aValIter.GetFirst(fVal, nErr))
3809 {
3810 // Schleife aus Performance-Gruenden nach innen verlegt:
3811 aValIter.GetCurNumFmtInfo( nFuncFmtType, nFuncFmtIndex );
3812 switch( eFunc )
3813 {
3814 case ifAVERAGE:
3815 case ifSUM:
3816 do
3817 {
3818 SetError(nErr);
3819 if ( bNull && fVal != 0.0 )
3820 {
3821 bNull = sal_False;
3822 fMem = fVal;
3823 }
3824 else
3825 fRes += fVal;
3826 nCount++;
3827 }
3828 while (aValIter.GetNext(fVal, nErr));
3829 break;
3830 case ifSUMSQ:
3831 do
3832 {
3833 SetError(nErr);
3834 fRes += fVal * fVal;
3835 nCount++;
3836 }
3837 while (aValIter.GetNext(fVal, nErr));
3838 break;
3839 case ifPRODUCT:
3840 do
3841 {
3842 SetError(nErr);
3843 fRes *= fVal;
3844 nCount++;
3845 }
3846 while (aValIter.GetNext(fVal, nErr));
3847 break;
3848 case ifCOUNT:
3849 do
3850 {
3851 if ( !nErr )
3852 nCount++;
3853 }
3854 while (aValIter.GetNext(fVal, nErr));
3855 break;
3856 default: ; // nothing
3857 }
3858 SetError( nErr );
3859 }
3860 }
3861 }
3862 break;
3863 case svMatrix :
3864 {
3865 ScMatrixRef pMat = PopMatrix();
3866 if (pMat)
3867 {
3868 SCSIZE nC, nR;
3869 nFuncFmtType = NUMBERFORMAT_NUMBER;
3870 pMat->GetDimensions(nC, nR);
3871 if( eFunc == ifCOUNT2 )
3872 nCount += (sal_uLong) nC * nR;
3873 else
3874 {
3875 for (SCSIZE nMatCol = 0; nMatCol < nC; nMatCol++)
3876 {
3877 for (SCSIZE nMatRow = 0; nMatRow < nR; nMatRow++)
3878 {
3879 if (!pMat->IsString(nMatCol,nMatRow))
3880 {
3881 nCount++;
3882 fVal = pMat->GetDouble(nMatCol,nMatRow);
3883 switch( eFunc )
3884 {
3885 case ifAVERAGE:
3886 case ifSUM:
3887 if ( bNull && fVal != 0.0 )
3888 {
3889 bNull = sal_False;
3890 fMem = fVal;
3891 }
3892 else
3893 fRes += fVal;
3894 break;
3895 case ifSUMSQ: fRes += fVal * fVal; break;
3896 case ifPRODUCT: fRes *= fVal; break;
3897 default: ; // nothing
3898 }
3899 }
3900 else if ( bTextAsZero )
3901 {
3902 nCount++;
3903 if ( eFunc == ifPRODUCT )
3904 fRes = 0.0;
3905 }
3906 }
3907 }
3908 }
3909 }
3910 }
3911 break;
3912 case svError:
3913 {
3914 Pop();
3915 if ( eFunc == ifCOUNT )
3916 {
3917 nGlobalError = 0;
3918 }
3919 else if ( eFunc == ifCOUNT2 )
3920 {
3921 nCount++;
3922 nGlobalError = 0;
3923 }
3924 }
3925 break;
3926 default :
3927 while (nParamCount-- > 0)
3928 PopError();
3929 SetError(errIllegalParameter);
3930 }
3931 }
3932 switch( eFunc )
3933 {
3934 case ifSUM: fRes = ::rtl::math::approxAdd( fRes, fMem ); break;
3935 case ifAVERAGE: fRes = div(::rtl::math::approxAdd( fRes, fMem ), nCount); break;
3936 case ifCOUNT2:
3937 case ifCOUNT: fRes = nCount; break;
3938 case ifPRODUCT: if ( !nCount ) fRes = 0.0; break;
3939 default: ; // nothing
3940 }
3941 // Bei Summen etc. macht ein sal_Bool-Ergebnis keinen Sinn
3942 // und Anzahl ist immer Number (#38345#)
3943 if( eFunc == ifCOUNT || nFuncFmtType == NUMBERFORMAT_LOGICAL )
3944 nFuncFmtType = NUMBERFORMAT_NUMBER;
3945 return fRes;
3946 }
3947
3948
ScSumSQ()3949 void ScInterpreter::ScSumSQ()
3950 {
3951 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScSumSQ" );
3952 PushDouble( IterateParameters( ifSUMSQ ) );
3953 }
3954
3955
ScSum()3956 void ScInterpreter::ScSum()
3957 {
3958 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScSum" );
3959 PushDouble( IterateParameters( ifSUM ) );
3960 }
3961
3962
ScProduct()3963 void ScInterpreter::ScProduct()
3964 {
3965 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScProduct" );
3966 PushDouble( IterateParameters( ifPRODUCT ) );
3967 }
3968
3969
ScAverage(sal_Bool bTextAsZero)3970 void ScInterpreter::ScAverage( sal_Bool bTextAsZero )
3971 {
3972 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScAverage" );
3973 PushDouble( IterateParameters( ifAVERAGE, bTextAsZero ) );
3974 }
3975
3976
ScCount()3977 void ScInterpreter::ScCount()
3978 {
3979 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScCount" );
3980 PushDouble( IterateParameters( ifCOUNT ) );
3981 }
3982
3983
ScCount2()3984 void ScInterpreter::ScCount2()
3985 {
3986 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScCount2" );
3987 PushDouble( IterateParameters( ifCOUNT2 ) );
3988 }
3989
3990
GetStVarParams(double & rVal,double & rValCount,sal_Bool bTextAsZero)3991 void ScInterpreter::GetStVarParams( double& rVal, double& rValCount,
3992 sal_Bool bTextAsZero )
3993 {
3994 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::GetStVarParams" );
3995 short nParamCount = GetByte();
3996
3997 std::vector<double> values;
3998 double fSum = 0.0;
3999 double vSum = 0.0;
4000 double vMean = 0.0;
4001 double fVal = 0.0;
4002 rValCount = 0.0;
4003 ScAddress aAdr;
4004 ScRange aRange;
4005 size_t nRefInList = 0;
4006 while (nParamCount-- > 0)
4007 {
4008 switch (GetStackType())
4009 {
4010 case svDouble :
4011 {
4012 fVal = GetDouble();
4013 values.push_back(fVal);
4014 fSum += fVal;
4015 rValCount++;
4016 }
4017 break;
4018 case svSingleRef :
4019 {
4020 PopSingleRef( aAdr );
4021 ScBaseCell* pCell = GetCell( aAdr );
4022 if (HasCellValueData(pCell))
4023 {
4024 fVal = GetCellValue( aAdr, pCell );
4025 values.push_back(fVal);
4026 fSum += fVal;
4027 rValCount++;
4028 }
4029 else if ( bTextAsZero && HasCellStringData( pCell ) )
4030 {
4031 values.push_back(0.0);
4032 rValCount++;
4033 }
4034 }
4035 break;
4036 case svDoubleRef :
4037 case svRefList :
4038 {
4039 sal_uInt16 nErr = 0;
4040 PopDoubleRef( aRange, nParamCount, nRefInList);
4041 ScValueIterator aValIter( pDok, aRange, glSubTotal, bTextAsZero );
4042 if (aValIter.GetFirst(fVal, nErr))
4043 {
4044 do
4045 {
4046 values.push_back(fVal);
4047 fSum += fVal;
4048 rValCount++;
4049 }
4050 while ((nErr == 0) && aValIter.GetNext(fVal, nErr));
4051 }
4052 }
4053 break;
4054 case svMatrix :
4055 {
4056 ScMatrixRef pMat = PopMatrix();
4057 if (pMat)
4058 {
4059 SCSIZE nC, nR;
4060 pMat->GetDimensions(nC, nR);
4061 for (SCSIZE nMatCol = 0; nMatCol < nC; nMatCol++)
4062 {
4063 for (SCSIZE nMatRow = 0; nMatRow < nR; nMatRow++)
4064 {
4065 if (!pMat->IsString(nMatCol,nMatRow))
4066 {
4067 fVal= pMat->GetDouble(nMatCol,nMatRow);
4068 values.push_back(fVal);
4069 fSum += fVal;
4070 rValCount++;
4071 }
4072 else if ( bTextAsZero )
4073 {
4074 values.push_back(0.0);
4075 rValCount++;
4076 }
4077 }
4078 }
4079 }
4080 }
4081 break;
4082 case svString :
4083 {
4084 Pop();
4085 if ( bTextAsZero )
4086 {
4087 values.push_back(0.0);
4088 rValCount++;
4089 }
4090 else
4091 SetError(errIllegalParameter);
4092 }
4093 break;
4094 default :
4095 Pop();
4096 SetError(errIllegalParameter);
4097 }
4098 }
4099
4100 ::std::vector<double>::size_type n = values.size();
4101 vMean = fSum / n;
4102 for (::std::vector<double>::size_type i = 0; i < n; i++)
4103 vSum += ::rtl::math::approxSub( values[i], vMean) * ::rtl::math::approxSub( values[i], vMean);
4104 rVal = vSum;
4105 }
4106
4107
ScVar(sal_Bool bTextAsZero)4108 void ScInterpreter::ScVar( sal_Bool bTextAsZero )
4109 {
4110 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScVar" );
4111 double nVal;
4112 double nValCount;
4113 GetStVarParams( nVal, nValCount, bTextAsZero );
4114
4115 if (nValCount <= 1.0)
4116 PushError( errDivisionByZero );
4117 else
4118 PushDouble( nVal / (nValCount - 1.0));
4119 }
4120
4121
ScVarP(sal_Bool bTextAsZero)4122 void ScInterpreter::ScVarP( sal_Bool bTextAsZero )
4123 {
4124 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScVarP" );
4125 double nVal;
4126 double nValCount;
4127 GetStVarParams( nVal, nValCount, bTextAsZero );
4128
4129 PushDouble( div( nVal, nValCount));
4130 }
4131
4132
ScStDev(sal_Bool bTextAsZero)4133 void ScInterpreter::ScStDev( sal_Bool bTextAsZero )
4134 {
4135 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScStDev" );
4136 double nVal;
4137 double nValCount;
4138 GetStVarParams( nVal, nValCount, bTextAsZero );
4139 if (nValCount <= 1.0)
4140 PushError( errDivisionByZero );
4141 else
4142 PushDouble( sqrt( nVal / (nValCount - 1.0)));
4143 }
4144
4145
ScStDevP(sal_Bool bTextAsZero)4146 void ScInterpreter::ScStDevP( sal_Bool bTextAsZero )
4147 {
4148 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScStDevP" );
4149 double nVal;
4150 double nValCount;
4151 GetStVarParams( nVal, nValCount, bTextAsZero );
4152 if (nValCount == 0.0)
4153 PushError( errDivisionByZero );
4154 else
4155 PushDouble( sqrt( nVal / nValCount));
4156
4157 /* this was: PushDouble( sqrt( div( nVal, nValCount)));
4158 *
4159 * Besides that the special NAN gets lost in the call through sqrt(),
4160 * unxlngi6.pro then looped back and forth somewhere between div() and
4161 * ::rtl::math::setNan(). Tests showed that
4162 *
4163 * sqrt( div( 1, 0));
4164 *
4165 * produced a loop, but
4166 *
4167 * double f1 = div( 1, 0);
4168 * sqrt( f1 );
4169 *
4170 * was fine. There seems to be some compiler optimization problem. It does
4171 * not occur when compiled with debug=t.
4172 */
4173 }
4174
4175
ScColumns()4176 void ScInterpreter::ScColumns()
4177 {
4178 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScColumns" );
4179 sal_uInt8 nParamCount = GetByte();
4180 sal_uLong nVal = 0;
4181 SCCOL nCol1;
4182 SCROW nRow1;
4183 SCTAB nTab1;
4184 SCCOL nCol2;
4185 SCROW nRow2;
4186 SCTAB nTab2;
4187 while (nParamCount-- > 0)
4188 {
4189 switch ( GetStackType() )
4190 {
4191 case svSingleRef:
4192 PopError();
4193 nVal++;
4194 break;
4195 case svDoubleRef:
4196 PopDoubleRef(nCol1, nRow1, nTab1, nCol2, nRow2, nTab2);
4197 nVal += static_cast<sal_uLong>(nTab2 - nTab1 + 1) *
4198 static_cast<sal_uLong>(nCol2 - nCol1 + 1);
4199 break;
4200 case svMatrix:
4201 {
4202 ScMatrixRef pMat = PopMatrix();
4203 if (pMat)
4204 {
4205 SCSIZE nC, nR;
4206 pMat->GetDimensions(nC, nR);
4207 nVal += nC;
4208 }
4209 }
4210 break;
4211 default:
4212 PopError();
4213 SetError(errIllegalParameter);
4214 }
4215 }
4216 PushDouble((double)nVal);
4217 }
4218
4219
ScRows()4220 void ScInterpreter::ScRows()
4221 {
4222 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScRows" );
4223 sal_uInt8 nParamCount = GetByte();
4224 sal_uLong nVal = 0;
4225 SCCOL nCol1;
4226 SCROW nRow1;
4227 SCTAB nTab1;
4228 SCCOL nCol2;
4229 SCROW nRow2;
4230 SCTAB nTab2;
4231 while (nParamCount-- > 0)
4232 {
4233 switch ( GetStackType() )
4234 {
4235 case svSingleRef:
4236 PopError();
4237 nVal++;
4238 break;
4239 case svDoubleRef:
4240 PopDoubleRef(nCol1, nRow1, nTab1, nCol2, nRow2, nTab2);
4241 nVal += static_cast<sal_uLong>(nTab2 - nTab1 + 1) *
4242 static_cast<sal_uLong>(nRow2 - nRow1 + 1);
4243 break;
4244 case svMatrix:
4245 {
4246 ScMatrixRef pMat = PopMatrix();
4247 if (pMat)
4248 {
4249 SCSIZE nC, nR;
4250 pMat->GetDimensions(nC, nR);
4251 nVal += nR;
4252 }
4253 }
4254 break;
4255 default:
4256 PopError();
4257 SetError(errIllegalParameter);
4258 }
4259 }
4260 PushDouble((double)nVal);
4261 }
4262
ScTables()4263 void ScInterpreter::ScTables()
4264 {
4265 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScTables" );
4266 sal_uInt8 nParamCount = GetByte();
4267 sal_uLong nVal;
4268 if ( nParamCount == 0 )
4269 nVal = pDok->GetTableCount();
4270 else
4271 {
4272 nVal = 0;
4273 SCCOL nCol1;
4274 SCROW nRow1;
4275 SCTAB nTab1;
4276 SCCOL nCol2;
4277 SCROW nRow2;
4278 SCTAB nTab2;
4279 while (nParamCount-- > 0)
4280 {
4281 switch ( GetStackType() )
4282 {
4283 case svSingleRef:
4284 PopError();
4285 nVal++;
4286 break;
4287 case svDoubleRef:
4288 PopDoubleRef(nCol1, nRow1, nTab1, nCol2, nRow2, nTab2);
4289 nVal += static_cast<sal_uLong>(nTab2 - nTab1 + 1);
4290 break;
4291 case svMatrix:
4292 PopError();
4293 nVal++;
4294 break;
4295 default:
4296 PopError();
4297 SetError( errIllegalParameter );
4298 }
4299 }
4300 }
4301 PushDouble( (double) nVal );
4302 }
4303
4304
ScColumn()4305 void ScInterpreter::ScColumn()
4306 {
4307 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScColumn" );
4308 sal_uInt8 nParamCount = GetByte();
4309 if ( MustHaveParamCount( nParamCount, 0, 1 ) )
4310 {
4311 double nVal = 0;
4312 if (nParamCount == 0)
4313 {
4314 nVal = aPos.Col() + 1;
4315 if (bMatrixFormula)
4316 {
4317 SCCOL nCols;
4318 SCROW nRows;
4319 pMyFormulaCell->GetMatColsRows( nCols, nRows);
4320 ScMatrixRef pResMat = GetNewMat( static_cast<SCSIZE>(nCols), 1);
4321 if (pResMat)
4322 {
4323 for (SCCOL i=0; i < nCols; ++i)
4324 pResMat->PutDouble( nVal + i, static_cast<SCSIZE>(i), 0);
4325 PushMatrix( pResMat);
4326 return;
4327 }
4328 }
4329 }
4330 else
4331 {
4332 switch ( GetStackType() )
4333 {
4334 case svSingleRef :
4335 {
4336 SCCOL nCol1;
4337 SCROW nRow1;
4338 SCTAB nTab1;
4339 PopSingleRef( nCol1, nRow1, nTab1 );
4340 nVal = (double) (nCol1 + 1);
4341 }
4342 break;
4343 case svDoubleRef :
4344 {
4345 SCCOL nCol1;
4346 SCROW nRow1;
4347 SCTAB nTab1;
4348 SCCOL nCol2;
4349 SCROW nRow2;
4350 SCTAB nTab2;
4351 PopDoubleRef( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2 );
4352 if (nCol2 > nCol1)
4353 {
4354 ScMatrixRef pResMat = GetNewMat(
4355 static_cast<SCSIZE>(nCol2-nCol1+1), 1);
4356 if (pResMat)
4357 {
4358 for (SCCOL i = nCol1; i <= nCol2; i++)
4359 pResMat->PutDouble((double)(i+1),
4360 static_cast<SCSIZE>(i-nCol1), 0);
4361 PushMatrix(pResMat);
4362 return;
4363 }
4364 else
4365 nVal = 0.0;
4366 }
4367 else
4368 nVal = (double) (nCol1 + 1);
4369 }
4370 break;
4371 default:
4372 SetError( errIllegalParameter );
4373 nVal = 0.0;
4374 }
4375 }
4376 PushDouble( nVal );
4377 }
4378 }
4379
4380
ScRow()4381 void ScInterpreter::ScRow()
4382 {
4383 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScRow" );
4384 sal_uInt8 nParamCount = GetByte();
4385 if ( MustHaveParamCount( nParamCount, 0, 1 ) )
4386 {
4387 double nVal = 0;
4388 if (nParamCount == 0)
4389 {
4390 nVal = aPos.Row() + 1;
4391 if (bMatrixFormula)
4392 {
4393 SCCOL nCols;
4394 SCROW nRows;
4395 pMyFormulaCell->GetMatColsRows( nCols, nRows);
4396 ScMatrixRef pResMat = GetNewMat( 1, static_cast<SCSIZE>(nRows));
4397 if (pResMat)
4398 {
4399 for (SCROW i=0; i < nRows; i++)
4400 pResMat->PutDouble( nVal + i, 0, static_cast<SCSIZE>(i));
4401 PushMatrix( pResMat);
4402 return;
4403 }
4404 }
4405 }
4406 else
4407 {
4408 switch ( GetStackType() )
4409 {
4410 case svSingleRef :
4411 {
4412 SCCOL nCol1;
4413 SCROW nRow1;
4414 SCTAB nTab1;
4415 PopSingleRef( nCol1, nRow1, nTab1 );
4416 nVal = (double) (nRow1 + 1);
4417 }
4418 break;
4419 case svDoubleRef :
4420 {
4421 SCCOL nCol1;
4422 SCROW nRow1;
4423 SCTAB nTab1;
4424 SCCOL nCol2;
4425 SCROW nRow2;
4426 SCTAB nTab2;
4427 PopDoubleRef( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2 );
4428 if (nRow2 > nRow1)
4429 {
4430 ScMatrixRef pResMat = GetNewMat( 1,
4431 static_cast<SCSIZE>(nRow2-nRow1+1));
4432 if (pResMat)
4433 {
4434 for (SCROW i = nRow1; i <= nRow2; i++)
4435 pResMat->PutDouble((double)(i+1), 0,
4436 static_cast<SCSIZE>(i-nRow1));
4437 PushMatrix(pResMat);
4438 return;
4439 }
4440 else
4441 nVal = 0.0;
4442 }
4443 else
4444 nVal = (double) (nRow1 + 1);
4445 }
4446 break;
4447 default:
4448 SetError( errIllegalParameter );
4449 nVal = 0.0;
4450 }
4451 }
4452 PushDouble( nVal );
4453 }
4454 }
4455
ScTable()4456 void ScInterpreter::ScTable()
4457 {
4458 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScTable" );
4459 sal_uInt8 nParamCount = GetByte();
4460 if ( MustHaveParamCount( nParamCount, 0, 1 ) )
4461 {
4462 SCTAB nVal = 0;
4463 if ( nParamCount == 0 )
4464 nVal = aPos.Tab() + 1;
4465 else
4466 {
4467 switch ( GetStackType() )
4468 {
4469 case svString :
4470 {
4471 String aStr( PopString() );
4472 if ( pDok->GetTable( aStr, nVal ) )
4473 ++nVal;
4474 else
4475 SetError( errIllegalArgument );
4476 }
4477 break;
4478 case svSingleRef :
4479 {
4480 SCCOL nCol1;
4481 SCROW nRow1;
4482 SCTAB nTab1;
4483 PopSingleRef( nCol1, nRow1, nTab1 );
4484 nVal = nTab1 + 1;
4485 }
4486 break;
4487 case svDoubleRef :
4488 {
4489 SCCOL nCol1;
4490 SCROW nRow1;
4491 SCTAB nTab1;
4492 SCCOL nCol2;
4493 SCROW nRow2;
4494 SCTAB nTab2;
4495 PopDoubleRef( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2 );
4496 nVal = nTab1 + 1;
4497 }
4498 break;
4499 default:
4500 SetError( errIllegalParameter );
4501 }
4502 if ( nGlobalError )
4503 nVal = 0;
4504 }
4505 PushDouble( (double) nVal );
4506 }
4507 }
4508
4509 /** returns -1 when the matrix value is smaller than the query value, 0 when
4510 they are equal, and 1 when the matrix value is larger than the query
4511 value. */
lcl_CompareMatrix2Query(SCSIZE i,const ScMatrix & rMat,const ScQueryEntry & rEntry)4512 static sal_Int32 lcl_CompareMatrix2Query( SCSIZE i, const ScMatrix& rMat,
4513 const ScQueryEntry& rEntry)
4514 {
4515 if (rMat.IsEmpty(i))
4516 {
4517 /* TODO: in case we introduced query for real empty this would have to
4518 * be changed! */
4519 return -1; // empty always less than anything else
4520 }
4521
4522 /* FIXME: what is an empty path (result of IF(false;true_path) in
4523 * comparisons? */
4524
4525 if (rMat.IsValue(i))
4526 {
4527 if (rEntry.bQueryByString)
4528 return -1; // numeric always less than string
4529
4530 const double nVal1 = rMat.GetDouble(i);
4531 const double nVal2 = rEntry.nVal;
4532 if (nVal1 == nVal2)
4533 return 0;
4534
4535 return nVal1 < nVal2 ? -1 : 1;
4536 }
4537
4538 if (!rEntry.bQueryByString)
4539 return 1; // string always greater than numeric
4540
4541 if (!rEntry.pStr)
4542 // this should not happen!
4543 return 1;
4544
4545 const String& rStr1 = rMat.GetString(i);
4546 const String& rStr2 = *rEntry.pStr;
4547
4548 return ScGlobal::GetCollator()->compareString( rStr1, rStr2); // case-insensitive
4549 }
4550
4551 /** returns the last item with the identical value as the original item
4552 value. */
lcl_GetLastMatch(SCSIZE & rIndex,const ScMatrix & rMat,SCSIZE nMatCount,bool bReverse)4553 static void lcl_GetLastMatch( SCSIZE& rIndex, const ScMatrix& rMat,
4554 SCSIZE nMatCount, bool bReverse)
4555 {
4556 if (rMat.IsValue(rIndex))
4557 {
4558 double nVal = rMat.GetDouble(rIndex);
4559 if (bReverse)
4560 while (rIndex > 0 && rMat.IsValue(rIndex-1) &&
4561 nVal == rMat.GetDouble(rIndex-1))
4562 --rIndex;
4563 else
4564 while (rIndex < nMatCount-1 && rMat.IsValue(rIndex+1) &&
4565 nVal == rMat.GetDouble(rIndex+1))
4566 ++rIndex;
4567 }
4568 //! Order of IsEmptyPath, IsEmpty, IsString is significant!
4569 else if (rMat.IsEmptyPath(rIndex))
4570 {
4571 if (bReverse)
4572 while (rIndex > 0 && rMat.IsEmptyPath(rIndex-1))
4573 --rIndex;
4574 else
4575 while (rIndex < nMatCount-1 && rMat.IsEmptyPath(rIndex+1))
4576 ++rIndex;
4577 }
4578 else if (rMat.IsEmpty(rIndex))
4579 {
4580 if (bReverse)
4581 while (rIndex > 0 && rMat.IsEmpty(rIndex-1))
4582 --rIndex;
4583 else
4584 while (rIndex < nMatCount-1 && rMat.IsEmpty(rIndex+1))
4585 ++rIndex;
4586 }
4587 else if (rMat.IsString(rIndex))
4588 {
4589 String aStr( rMat.GetString(rIndex));
4590 if (bReverse)
4591 while (rIndex > 0 && rMat.IsString(rIndex-1) &&
4592 aStr == rMat.GetString(rIndex-1))
4593 --rIndex;
4594 else
4595 while (rIndex < nMatCount-1 && rMat.IsString(rIndex+1) &&
4596 aStr == rMat.GetString(rIndex+1))
4597 ++rIndex;
4598 }
4599 else
4600 {
4601 DBG_ERRORFILE("lcl_GetLastMatch: unhandled matrix type");
4602 }
4603 }
4604
ScMatch()4605 void ScInterpreter::ScMatch()
4606 {
4607 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScMatch" );
4608 ScMatrixRef pMatSrc = NULL;
4609
4610 sal_uInt8 nParamCount = GetByte();
4611 if ( MustHaveParamCount( nParamCount, 2, 3 ) )
4612 {
4613 double fTyp;
4614 if (nParamCount == 3)
4615 fTyp = GetDouble();
4616 else
4617 fTyp = 1.0;
4618 SCCOL nCol1 = 0;
4619 SCROW nRow1 = 0;
4620 SCTAB nTab1 = 0;
4621 SCCOL nCol2 = 0;
4622 SCROW nRow2 = 0;
4623 SCTAB nTab2 = 0;
4624 if (GetStackType() == svDoubleRef)
4625 {
4626 PopDoubleRef(nCol1, nRow1, nTab1, nCol2, nRow2, nTab2);
4627 if (nTab1 != nTab2 || (nCol1 != nCol2 && nRow1 != nRow2))
4628 {
4629 PushIllegalParameter();
4630 return;
4631 }
4632 }
4633 else if (GetStackType() == svMatrix)
4634 {
4635 pMatSrc = PopMatrix();
4636 if (!pMatSrc)
4637 {
4638 PushIllegalParameter();
4639 return;
4640 }
4641 }
4642 else
4643 {
4644 PushIllegalParameter();
4645 return;
4646 }
4647 if (nGlobalError == 0)
4648 {
4649 double fVal;
4650 String sStr;
4651 ScQueryParam rParam;
4652 rParam.nCol1 = nCol1;
4653 rParam.nRow1 = nRow1;
4654 rParam.nCol2 = nCol2;
4655 rParam.nTab = nTab1;
4656 rParam.bMixedComparison = sal_True;
4657
4658 ScQueryEntry& rEntry = rParam.GetEntry(0);
4659 rEntry.bDoQuery = sal_True;
4660 if (fTyp < 0.0)
4661 rEntry.eOp = SC_GREATER_EQUAL;
4662 else if (fTyp > 0.0)
4663 rEntry.eOp = SC_LESS_EQUAL;
4664 switch ( GetStackType() )
4665 {
4666 case svDouble:
4667 {
4668 fVal = GetDouble();
4669 rEntry.bQueryByString = sal_False;
4670 rEntry.nVal = fVal;
4671 }
4672 break;
4673 case svString:
4674 {
4675 sStr = GetString();
4676 rEntry.bQueryByString = sal_True;
4677 *rEntry.pStr = sStr;
4678 }
4679 break;
4680 case svDoubleRef :
4681 case svSingleRef :
4682 {
4683 ScAddress aAdr;
4684 if ( !PopDoubleRefOrSingleRef( aAdr ) )
4685 {
4686 PushInt(0);
4687 return ;
4688 }
4689 ScBaseCell* pCell = GetCell( aAdr );
4690 if (HasCellValueData(pCell))
4691 {
4692 fVal = GetCellValue( aAdr, pCell );
4693 rEntry.bQueryByString = sal_False;
4694 rEntry.nVal = fVal;
4695 }
4696 else
4697 {
4698 GetCellString(sStr, pCell);
4699 rEntry.bQueryByString = sal_True;
4700 *rEntry.pStr = sStr;
4701 }
4702 }
4703 break;
4704 case svMatrix :
4705 {
4706 ScMatValType nType = GetDoubleOrStringFromMatrix(
4707 rEntry.nVal, *rEntry.pStr);
4708 rEntry.bQueryByString = ScMatrix::IsNonValueType( nType);
4709 }
4710 break;
4711 default:
4712 {
4713 PushIllegalParameter();
4714 return;
4715 }
4716 }
4717 if ( rEntry.bQueryByString )
4718 rParam.bRegExp = MayBeRegExp( *rEntry.pStr, pDok );
4719
4720 if (pMatSrc) // The source data is matrix array.
4721 {
4722 SCSIZE nC, nR;
4723 pMatSrc->GetDimensions( nC, nR);
4724 if (nC > 1 && nR > 1)
4725 {
4726 // The source matrix must be a vector.
4727 PushIllegalParameter();
4728 return;
4729 }
4730 SCSIZE nMatCount = (nC == 1) ? nR : nC;
4731
4732 // simple serial search for equality mode (source data doesn't
4733 // need to be sorted).
4734
4735 if (rEntry.eOp == SC_EQUAL)
4736 {
4737 for (SCSIZE i = 0; i < nMatCount; ++i)
4738 {
4739 if (lcl_CompareMatrix2Query( i, *pMatSrc, rEntry) == 0)
4740 {
4741 PushDouble(i+1); // found !
4742 return;
4743 }
4744 }
4745 PushNA(); // not found
4746 return;
4747 }
4748
4749 // binary search for non-equality mode (the source data is
4750 // assumed to be sorted).
4751
4752 bool bAscOrder = (rEntry.eOp == SC_LESS_EQUAL);
4753 SCSIZE nFirst = 0, nLast = nMatCount-1, nHitIndex = 0;
4754 for (SCSIZE nLen = nLast-nFirst; nLen > 0; nLen = nLast-nFirst)
4755 {
4756 SCSIZE nMid = nFirst + nLen/2;
4757 sal_Int32 nCmp = lcl_CompareMatrix2Query( nMid, *pMatSrc, rEntry);
4758 if (nCmp == 0)
4759 {
4760 // exact match. find the last item with the same value.
4761 lcl_GetLastMatch( nMid, *pMatSrc, nMatCount, !bAscOrder);
4762 PushDouble( nMid+1);
4763 return;
4764 }
4765
4766 if (nLen == 1) // first and last items are next to each other.
4767 {
4768 if (nCmp < 0)
4769 nHitIndex = bAscOrder ? nLast : nFirst;
4770 else
4771 nHitIndex = bAscOrder ? nFirst : nLast;
4772 break;
4773 }
4774
4775 if (nCmp < 0)
4776 {
4777 if (bAscOrder)
4778 nFirst = nMid;
4779 else
4780 nLast = nMid;
4781 }
4782 else
4783 {
4784 if (bAscOrder)
4785 nLast = nMid;
4786 else
4787 nFirst = nMid;
4788 }
4789 }
4790
4791 if (nHitIndex == nMatCount-1) // last item
4792 {
4793 sal_Int32 nCmp = lcl_CompareMatrix2Query( nHitIndex, *pMatSrc, rEntry);
4794 if ((bAscOrder && nCmp <= 0) || (!bAscOrder && nCmp >= 0))
4795 {
4796 // either the last item is an exact match or the real
4797 // hit is beyond the last item.
4798 PushDouble( nHitIndex+1);
4799 return;
4800 }
4801 }
4802
4803 if (nHitIndex > 0) // valid hit must be 2nd item or higher
4804 {
4805 PushDouble( nHitIndex); // non-exact match
4806 return;
4807 }
4808
4809 PushNA();
4810 return;
4811 }
4812
4813 SCCOLROW nDelta = 0;
4814 if (nCol1 == nCol2)
4815 { // search row in column
4816 rParam.nRow2 = nRow2;
4817 rEntry.nField = nCol1;
4818 ScAddress aResultPos( nCol1, nRow1, nTab1);
4819 if (!LookupQueryWithCache( aResultPos, rParam))
4820 {
4821 PushNA();
4822 return;
4823 }
4824 nDelta = aResultPos.Row() - nRow1;
4825 }
4826 else
4827 { // search column in row
4828 SCCOL nC;
4829 rParam.bByRow = sal_False;
4830 rParam.nRow2 = nRow1;
4831 rEntry.nField = nCol1;
4832 ScQueryCellIterator aCellIter(pDok, nTab1, rParam, sal_False);
4833 // Advance Entry.nField in Iterator if column changed
4834 aCellIter.SetAdvanceQueryParamEntryField( sal_True );
4835 if (fTyp == 0.0)
4836 { // EQUAL
4837 if ( aCellIter.GetFirst() )
4838 nC = aCellIter.GetCol();
4839 else
4840 {
4841 PushNA();
4842 return;
4843 }
4844 }
4845 else
4846 { // <= or >=
4847 SCROW nR;
4848 if ( !aCellIter.FindEqualOrSortedLastInRange( nC, nR ) )
4849 {
4850 PushNA();
4851 return;
4852 }
4853 }
4854 nDelta = nC - nCol1;
4855 }
4856 PushDouble((double) (nDelta + 1));
4857 }
4858 else
4859 PushIllegalParameter();
4860 }
4861 }
4862
4863
4864 // A formula cell returning the empty string counts as blank, as in other spreadsheets.
lcl_IsCountBlankEmpty(ScBaseCell * pCell)4865 static bool lcl_IsCountBlankEmpty( ScBaseCell* pCell )
4866 {
4867 CellType eCellType = pCell ? pCell->GetCellType() : CELLTYPE_NONE;
4868 if (eCellType == CELLTYPE_NONE || eCellType == CELLTYPE_NOTE)
4869 return true;
4870 if (eCellType != CELLTYPE_FORMULA)
4871 return false;
4872 ScFormulaCell* pFCell = static_cast<ScFormulaCell*>(pCell);
4873 if (pFCell->GetErrCode() || pFCell->IsValue())
4874 return false;
4875 String aStr;
4876 pFCell->GetString( aStr );
4877 return aStr.Len() == 0;
4878 }
4879
ScCountEmptyCells()4880 void ScInterpreter::ScCountEmptyCells()
4881 {
4882 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScCountEmptyCells" );
4883 if ( MustHaveParamCount( GetByte(), 1 ) )
4884 {
4885 sal_uLong nMaxCount = 0, nCount = 0;
4886 switch (GetStackType())
4887 {
4888 case svSingleRef :
4889 {
4890 nMaxCount = 1;
4891 ScAddress aAdr;
4892 PopSingleRef( aAdr );
4893 if (!lcl_IsCountBlankEmpty( GetCell( aAdr ) ))
4894 nCount = 1;
4895 }
4896 break;
4897 case svDoubleRef :
4898 case svRefList :
4899 {
4900 ScRange aRange;
4901 short nParam = 1;
4902 size_t nRefInList = 0;
4903 while (nParam-- > 0)
4904 {
4905 PopDoubleRef( aRange, nParam, nRefInList);
4906 nMaxCount +=
4907 static_cast<sal_uLong>(aRange.aEnd.Row() - aRange.aStart.Row() + 1) *
4908 static_cast<sal_uLong>(aRange.aEnd.Col() - aRange.aStart.Col() + 1) *
4909 static_cast<sal_uLong>(aRange.aEnd.Tab() - aRange.aStart.Tab() + 1);
4910 ScBaseCell* pCell;
4911 ScCellIterator aDocIter( pDok, aRange, glSubTotal);
4912 if ( (pCell = aDocIter.GetFirst()) != NULL )
4913 {
4914 do
4915 {
4916 if (!lcl_IsCountBlankEmpty( pCell ))
4917 nCount++;
4918 } while ( (pCell = aDocIter.GetNext()) != NULL );
4919 }
4920 }
4921 }
4922 break;
4923 default : SetError(errIllegalParameter); break;
4924 }
4925 PushDouble(nMaxCount - nCount);
4926 }
4927 }
4928
4929
IterateParametersIf(ScIterFuncIf eFunc)4930 double ScInterpreter::IterateParametersIf( ScIterFuncIf eFunc )
4931 {
4932 sal_uInt8 nParamCount = GetByte();
4933 if ( MustHaveParamCount( nParamCount, 2, 3 ) )
4934 {
4935 SCCOL nCol3 = 0;
4936 SCROW nRow3 = 0;
4937 SCTAB nTab3 = 0;
4938
4939 ScMatrixRef pSumExtraMatrix;
4940 bool bSumExtraRange = (nParamCount == 3);
4941 if (bSumExtraRange)
4942 {
4943 // Save only the upperleft cell in case of cell range. The geometry
4944 // of the 3rd parameter is taken from the 1st parameter.
4945
4946 switch ( GetStackType() )
4947 {
4948 case svDoubleRef :
4949 {
4950 SCCOL nColJunk = 0;
4951 SCROW nRowJunk = 0;
4952 SCTAB nTabJunk = 0;
4953 PopDoubleRef( nCol3, nRow3, nTab3, nColJunk, nRowJunk, nTabJunk );
4954 if ( nTabJunk != nTab3 )
4955 {
4956 SetError( errIllegalParameter);
4957 }
4958 }
4959 break;
4960 case svSingleRef :
4961 PopSingleRef( nCol3, nRow3, nTab3 );
4962 break;
4963 case svMatrix:
4964 pSumExtraMatrix = PopMatrix();
4965 //! nCol3, nRow3, nTab3 remain 0
4966 break;
4967 default:
4968 SetError( errIllegalParameter);
4969 }
4970 }
4971 String rString;
4972 double fVal = 0.0;
4973 bool bIsString = true;
4974 switch ( GetStackType() )
4975 {
4976 case svDoubleRef :
4977 case svSingleRef :
4978 {
4979 ScAddress aAdr;
4980 if ( !PopDoubleRefOrSingleRef( aAdr ) )
4981 return 0;
4982
4983 ScBaseCell* pCell = GetCell( aAdr );
4984 switch ( GetCellType( pCell ) )
4985 {
4986 case CELLTYPE_VALUE :
4987 fVal = GetCellValue( aAdr, pCell );
4988 bIsString = false;
4989 break;
4990 case CELLTYPE_FORMULA :
4991 if( ((ScFormulaCell*)pCell)->IsValue() )
4992 {
4993 fVal = GetCellValue( aAdr, pCell );
4994 bIsString = false;
4995 }
4996 else
4997 GetCellString(rString, pCell);
4998 break;
4999 case CELLTYPE_STRING :
5000 case CELLTYPE_EDIT :
5001 GetCellString(rString, pCell);
5002 break;
5003 default:
5004 fVal = 0.0;
5005 bIsString = false;
5006 }
5007 }
5008 break;
5009 case svString:
5010 rString = GetString();
5011 break;
5012 case svMatrix :
5013 {
5014 ScMatValType nType = GetDoubleOrStringFromMatrix( fVal, rString);
5015 bIsString = ScMatrix::IsNonValueType( nType);
5016 }
5017 break;
5018 default:
5019 {
5020 fVal = GetDouble();
5021 bIsString = false;
5022 }
5023 }
5024
5025 double fSum = 0.0;
5026 double fMem = 0.0;
5027 double fRes = 0.0;
5028 double fCount = 0.0;
5029 bool bNull = true;
5030 short nParam = 1;
5031 size_t nRefInList = 0;
5032 while (nParam-- > 0)
5033 {
5034 SCCOL nCol1 = 0;
5035 SCROW nRow1 = 0;
5036 SCTAB nTab1 = 0;
5037 SCCOL nCol2 = 0;
5038 SCROW nRow2 = 0;
5039 SCTAB nTab2 = 0;
5040 ScMatrixRef pQueryMatrix;
5041 switch ( GetStackType() )
5042 {
5043 case svRefList :
5044 if (bSumExtraRange)
5045 {
5046 SetError( errIllegalParameter);
5047 }
5048 else
5049 {
5050 ScRange aRange;
5051 PopDoubleRef( aRange, nParam, nRefInList);
5052 aRange.GetVars( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2);
5053 }
5054 break;
5055 case svDoubleRef :
5056 PopDoubleRef( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2 );
5057 break;
5058 case svSingleRef :
5059 PopSingleRef( nCol1, nRow1, nTab1 );
5060 nCol2 = nCol1;
5061 nRow2 = nRow1;
5062 nTab2 = nTab1;
5063 break;
5064 case svMatrix:
5065 {
5066 pQueryMatrix = PopMatrix();
5067 if (!pQueryMatrix)
5068 {
5069 SetError( errIllegalParameter);
5070 }
5071 nCol1 = 0;
5072 nRow1 = 0;
5073 nTab1 = 0;
5074 SCSIZE nC, nR;
5075 pQueryMatrix->GetDimensions( nC, nR);
5076 nCol2 = static_cast<SCCOL>(nC - 1);
5077 nRow2 = static_cast<SCROW>(nR - 1);
5078 nTab2 = 0;
5079 }
5080 break;
5081 default:
5082 SetError( errIllegalParameter);
5083 }
5084 if ( nTab1 != nTab2 )
5085 {
5086 SetError( errIllegalParameter);
5087 }
5088
5089 if (bSumExtraRange)
5090 {
5091 // Take the range geometry of the 1st parameter and apply it to
5092 // the 3rd. If parts of the resulting range would point outside
5093 // the sheet, don't complain but silently ignore and simply cut
5094 // them away, this is what Xcl does :-/
5095
5096 // For the cut-away part we also don't need to determine the
5097 // criteria match, so shrink the source range accordingly,
5098 // instead of the result range.
5099 SCCOL nColDelta = nCol2 - nCol1;
5100 SCROW nRowDelta = nRow2 - nRow1;
5101 SCCOL nMaxCol;
5102 SCROW nMaxRow;
5103 if (pSumExtraMatrix)
5104 {
5105 SCSIZE nC, nR;
5106 pSumExtraMatrix->GetDimensions( nC, nR);
5107 nMaxCol = static_cast<SCCOL>(nC - 1);
5108 nMaxRow = static_cast<SCROW>(nR - 1);
5109 }
5110 else
5111 {
5112 nMaxCol = MAXCOL;
5113 nMaxRow = MAXROW;
5114 }
5115 if (nCol3 + nColDelta > nMaxCol)
5116 {
5117 SCCOL nNewDelta = nMaxCol - nCol3;
5118 nCol2 = nCol1 + nNewDelta;
5119 }
5120
5121 if (nRow3 + nRowDelta > nMaxRow)
5122 {
5123 SCROW nNewDelta = nMaxRow - nRow3;
5124 nRow2 = nRow1 + nNewDelta;
5125 }
5126 }
5127 else
5128 {
5129 nCol3 = nCol1;
5130 nRow3 = nRow1;
5131 nTab3 = nTab1;
5132 }
5133
5134 if (nGlobalError == 0)
5135 {
5136 ScQueryParam rParam;
5137 rParam.nRow1 = nRow1;
5138 rParam.nRow2 = nRow2;
5139
5140 ScQueryEntry& rEntry = rParam.GetEntry(0);
5141 rEntry.bDoQuery = true;
5142 if (!bIsString)
5143 {
5144 rEntry.bQueryByString = false;
5145 rEntry.nVal = fVal;
5146 rEntry.eOp = SC_EQUAL;
5147 }
5148 else
5149 {
5150 rParam.FillInExcelSyntax(rString, 0);
5151 sal_uInt32 nIndex = 0;
5152 rEntry.bQueryByString =
5153 !(pFormatter->IsNumberFormat(
5154 *rEntry.pStr, nIndex, rEntry.nVal));
5155 if ( rEntry.bQueryByString )
5156 rParam.bRegExp = MayBeRegExp( *rEntry.pStr, pDok );
5157 }
5158 ScAddress aAdr;
5159 aAdr.SetTab( nTab3 );
5160 rParam.nCol1 = nCol1;
5161 rParam.nCol2 = nCol2;
5162 rEntry.nField = nCol1;
5163 SCsCOL nColDiff = nCol3 - nCol1;
5164 SCsROW nRowDiff = nRow3 - nRow1;
5165 if (pQueryMatrix)
5166 {
5167 // Never case-sensitive.
5168 ScCompareOptions aOptions( pDok, rEntry, rParam.bRegExp);
5169 ScMatrixRef pResultMatrix = QueryMat( pQueryMatrix, aOptions);
5170 if (nGlobalError || !pResultMatrix)
5171 {
5172 SetError( errIllegalParameter);
5173 }
5174
5175 if (pSumExtraMatrix)
5176 {
5177 for (SCCOL nCol = nCol1; nCol <= nCol2; ++nCol)
5178 {
5179 for (SCROW nRow = nRow1; nRow <= nRow2; ++nRow)
5180 {
5181 if (pResultMatrix->IsValue( nCol, nRow) &&
5182 pResultMatrix->GetDouble( nCol, nRow))
5183 {
5184 SCSIZE nC = nCol + nColDiff;
5185 SCSIZE nR = nRow + nRowDiff;
5186 if (pSumExtraMatrix->IsValue( nC, nR))
5187 {
5188 fVal = pSumExtraMatrix->GetDouble( nC, nR);
5189 ++fCount;
5190 if ( bNull && fVal != 0.0 )
5191 {
5192 bNull = false;
5193 fMem = fVal;
5194 }
5195 else
5196 fSum += fVal;
5197 }
5198 }
5199 }
5200 }
5201 }
5202 else
5203 {
5204 for (SCCOL nCol = nCol1; nCol <= nCol2; ++nCol)
5205 {
5206 for (SCROW nRow = nRow1; nRow <= nRow2; ++nRow)
5207 {
5208 if (pResultMatrix->GetDouble( nCol, nRow))
5209 {
5210 aAdr.SetCol( nCol + nColDiff);
5211 aAdr.SetRow( nRow + nRowDiff);
5212 ScBaseCell* pCell = GetCell( aAdr );
5213 if ( HasCellValueData(pCell) )
5214 {
5215 fVal = GetCellValue( aAdr, pCell );
5216 ++fCount;
5217 if ( bNull && fVal != 0.0 )
5218 {
5219 bNull = false;
5220 fMem = fVal;
5221 }
5222 else
5223 fSum += fVal;
5224 }
5225 }
5226 }
5227 }
5228 }
5229 }
5230 else
5231 {
5232 ScQueryCellIterator aCellIter(pDok, nTab1, rParam, false);
5233 // Increment Entry.nField in iterator when switching to next column.
5234 aCellIter.SetAdvanceQueryParamEntryField( true );
5235 if ( aCellIter.GetFirst() )
5236 {
5237 if (pSumExtraMatrix)
5238 {
5239 do
5240 {
5241 SCSIZE nC = aCellIter.GetCol() + nColDiff;
5242 SCSIZE nR = aCellIter.GetRow() + nRowDiff;
5243 if (pSumExtraMatrix->IsValue( nC, nR))
5244 {
5245 fVal = pSumExtraMatrix->GetDouble( nC, nR);
5246 ++fCount;
5247 if ( bNull && fVal != 0.0 )
5248 {
5249 bNull = false;
5250 fMem = fVal;
5251 }
5252 else
5253 fSum += fVal;
5254 }
5255 } while ( aCellIter.GetNext() );
5256 }
5257 else
5258 {
5259 do
5260 {
5261 aAdr.SetCol( aCellIter.GetCol() + nColDiff);
5262 aAdr.SetRow( aCellIter.GetRow() + nRowDiff);
5263 ScBaseCell* pCell = GetCell( aAdr );
5264 if ( HasCellValueData(pCell) )
5265 {
5266 fVal = GetCellValue( aAdr, pCell );
5267 ++fCount;
5268 if ( bNull && fVal != 0.0 )
5269 {
5270 bNull = false;
5271 fMem = fVal;
5272 }
5273 else
5274 fSum += fVal;
5275 }
5276 } while ( aCellIter.GetNext() );
5277 }
5278 }
5279 }
5280 }
5281 else
5282 {
5283 SetError( errIllegalParameter);
5284 }
5285 }
5286
5287 switch( eFunc )
5288 {
5289 case ifSUMIF: fRes = ::rtl::math::approxAdd( fSum, fMem ); break;
5290 case ifAVERAGEIF: fRes = div( ::rtl::math::approxAdd( fSum, fMem ), fCount); break;
5291 }
5292 return fRes;
5293 }
5294 return 0;
5295 }
5296
ScSumIf()5297 void ScInterpreter::ScSumIf()
5298 {
5299 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScSumIf" );
5300 PushDouble( IterateParametersIf( ifSUMIF));
5301 }
5302
ScAverageIf()5303 void ScInterpreter::ScAverageIf()
5304 {
5305 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "makkica", "ScInterpreter::ScAverageIf" );
5306 PushDouble( IterateParametersIf( ifAVERAGEIF));
5307 }
5308
ScCountIf()5309 void ScInterpreter::ScCountIf()
5310 {
5311 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScCountIf" );
5312 if ( MustHaveParamCount( GetByte(), 2 ) )
5313 {
5314 String rString;
5315 double fVal = 0.0;
5316 sal_Bool bIsString = sal_True;
5317 switch ( GetStackType() )
5318 {
5319 case svDoubleRef :
5320 case svSingleRef :
5321 {
5322 ScAddress aAdr;
5323 if ( !PopDoubleRefOrSingleRef( aAdr ) )
5324 {
5325 PushInt(0);
5326 return ;
5327 }
5328 ScBaseCell* pCell = GetCell( aAdr );
5329 switch ( GetCellType( pCell ) )
5330 {
5331 case CELLTYPE_VALUE :
5332 fVal = GetCellValue( aAdr, pCell );
5333 bIsString = sal_False;
5334 break;
5335 case CELLTYPE_FORMULA :
5336 if( ((ScFormulaCell*)pCell)->IsValue() )
5337 {
5338 fVal = GetCellValue( aAdr, pCell );
5339 bIsString = sal_False;
5340 }
5341 else
5342 GetCellString(rString, pCell);
5343 break;
5344 case CELLTYPE_STRING :
5345 case CELLTYPE_EDIT :
5346 GetCellString(rString, pCell);
5347 break;
5348 default:
5349 fVal = 0.0;
5350 bIsString = sal_False;
5351 }
5352 }
5353 break;
5354 case svMatrix :
5355 {
5356 ScMatValType nType = GetDoubleOrStringFromMatrix( fVal, rString);
5357 bIsString = ScMatrix::IsNonValueType( nType);
5358 }
5359 break;
5360 case svString:
5361 rString = GetString();
5362 break;
5363 default:
5364 {
5365 fVal = GetDouble();
5366 bIsString = sal_False;
5367 }
5368 }
5369 double fCount = 0.0;
5370 short nParam = 1;
5371 size_t nRefInList = 0;
5372 while (nParam-- > 0)
5373 {
5374 SCCOL nCol1;
5375 SCROW nRow1;
5376 SCTAB nTab1;
5377 SCCOL nCol2;
5378 SCROW nRow2;
5379 SCTAB nTab2;
5380 ScMatrixRef pQueryMatrix;
5381 switch ( GetStackType() )
5382 {
5383 case svDoubleRef :
5384 case svRefList :
5385 {
5386 ScRange aRange;
5387 PopDoubleRef( aRange, nParam, nRefInList);
5388 aRange.GetVars( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2);
5389 }
5390 break;
5391 case svSingleRef :
5392 PopSingleRef( nCol1, nRow1, nTab1 );
5393 nCol2 = nCol1;
5394 nRow2 = nRow1;
5395 nTab2 = nTab1;
5396 break;
5397 case svMatrix:
5398 {
5399 pQueryMatrix = PopMatrix();
5400 if (!pQueryMatrix)
5401 {
5402 PushIllegalParameter();
5403 return;
5404 }
5405 nCol1 = 0;
5406 nRow1 = 0;
5407 nTab1 = 0;
5408 SCSIZE nC, nR;
5409 pQueryMatrix->GetDimensions( nC, nR);
5410 nCol2 = static_cast<SCCOL>(nC - 1);
5411 nRow2 = static_cast<SCROW>(nR - 1);
5412 nTab2 = 0;
5413 }
5414 break;
5415 default:
5416 PushIllegalParameter();
5417 return ;
5418 }
5419 if ( nTab1 != nTab2 )
5420 {
5421 PushIllegalParameter();
5422 return;
5423 }
5424 if (nCol1 > nCol2)
5425 {
5426 PushIllegalParameter();
5427 return;
5428 }
5429 if (nGlobalError == 0)
5430 {
5431 ScQueryParam rParam;
5432 rParam.nRow1 = nRow1;
5433 rParam.nRow2 = nRow2;
5434
5435 ScQueryEntry& rEntry = rParam.GetEntry(0);
5436 rEntry.bDoQuery = sal_True;
5437 if (!bIsString)
5438 {
5439 rEntry.bQueryByString = sal_False;
5440 rEntry.nVal = fVal;
5441 rEntry.eOp = SC_EQUAL;
5442 }
5443 else
5444 {
5445 rParam.FillInExcelSyntax(rString, 0);
5446 sal_uInt32 nIndex = 0;
5447 rEntry.bQueryByString =
5448 !(pFormatter->IsNumberFormat(
5449 *rEntry.pStr, nIndex, rEntry.nVal));
5450 if ( rEntry.bQueryByString )
5451 rParam.bRegExp = MayBeRegExp( *rEntry.pStr, pDok );
5452 }
5453 rParam.nCol1 = nCol1;
5454 rParam.nCol2 = nCol2;
5455 rEntry.nField = nCol1;
5456 if (pQueryMatrix)
5457 {
5458 // Never case-sensitive.
5459 ScCompareOptions aOptions( pDok, rEntry, rParam.bRegExp);
5460 ScMatrixRef pResultMatrix = QueryMat( pQueryMatrix, aOptions);
5461 if (nGlobalError || !pResultMatrix)
5462 {
5463 PushIllegalParameter();
5464 return;
5465 }
5466
5467 SCSIZE nSize = pResultMatrix->GetElementCount();
5468 for (SCSIZE nIndex = 0; nIndex < nSize; ++nIndex)
5469 {
5470 if (pResultMatrix->IsValue( nIndex) &&
5471 pResultMatrix->GetDouble( nIndex))
5472 ++fCount;
5473 }
5474 }
5475 else
5476 {
5477 ScQueryCellIterator aCellIter(pDok, nTab1, rParam, sal_False);
5478 // Entry.nField im Iterator bei Spaltenwechsel weiterschalten
5479 aCellIter.SetAdvanceQueryParamEntryField( sal_True );
5480 if ( aCellIter.GetFirst() )
5481 {
5482 do
5483 {
5484 fCount++;
5485 } while ( aCellIter.GetNext() );
5486 }
5487 }
5488 }
5489 else
5490 {
5491 PushIllegalParameter();
5492 return;
5493 }
5494 }
5495 PushDouble(fCount);
5496 }
5497 }
5498
IterateParametersIfs(ScIterFuncIfs eFunc)5499 double ScInterpreter::IterateParametersIfs( ScIterFuncIfs eFunc )
5500 {
5501 sal_uInt8 nParamCount = GetByte();
5502 sal_uInt8 nQueryCount = nParamCount / 2;
5503
5504 bool bCheck;
5505 if ( eFunc == ifCOUNTIFS )
5506 bCheck = (nParamCount >= 2) && (nParamCount % 2 == 0);
5507 else
5508 bCheck = (nParamCount >= 3) && (nParamCount % 2 == 1);
5509
5510 if ( !bCheck )
5511 {
5512 SetError( errParameterExpected);
5513 }
5514 else
5515 {
5516 ScMatrixRef pResMat;
5517 double fVal = 0.0;
5518 double fSum = 0.0;
5519 double fMem = 0.0;
5520 double fRes = 0.0;
5521 double fCount = 0.0;
5522 short nParam = 1;
5523 size_t nRefInList = 0;
5524 SCCOL nDimensionCols = 0;
5525 SCROW nDimensionRows = 0;
5526
5527 while (nParamCount > 1 && !nGlobalError)
5528 {
5529 // take criteria
5530 String rString;
5531 fVal = 0.0;
5532 bool bIsString = true;
5533 switch ( GetStackType() )
5534 {
5535 case svDoubleRef :
5536 case svSingleRef :
5537 {
5538 ScAddress aAdr;
5539 if ( !PopDoubleRefOrSingleRef( aAdr ) )
5540 return 0;
5541
5542 ScBaseCell* pCell = GetCell( aAdr );
5543 switch ( GetCellType( pCell ) )
5544 {
5545 case CELLTYPE_VALUE :
5546 fVal = GetCellValue( aAdr, pCell );
5547 bIsString = false;
5548 break;
5549 case CELLTYPE_FORMULA :
5550 if( ((ScFormulaCell*)pCell)->IsValue() )
5551 {
5552 fVal = GetCellValue( aAdr, pCell );
5553 bIsString = false;
5554 }
5555 else
5556 GetCellString(rString, pCell);
5557 break;
5558 case CELLTYPE_STRING :
5559 case CELLTYPE_EDIT :
5560 GetCellString(rString, pCell);
5561 break;
5562 default:
5563 fVal = 0.0;
5564 bIsString = false;
5565 }
5566 }
5567 break;
5568 case svString:
5569 rString = GetString();
5570 break;
5571 case svMatrix :
5572 {
5573 ScMatValType nType = GetDoubleOrStringFromMatrix( fVal, rString);
5574 bIsString = ScMatrix::IsNonValueType( nType);
5575 }
5576 break;
5577 default:
5578 {
5579 fVal = GetDouble();
5580 bIsString = false;
5581 }
5582 }
5583
5584 if (nGlobalError)
5585 continue; // and bail out, no need to evaluate other arguments
5586
5587 // take range
5588 nParam = 1;
5589 nRefInList = 0;
5590 SCCOL nCol1 = 0;
5591 SCROW nRow1 = 0;
5592 SCTAB nTab1 = 0;
5593 SCCOL nCol2 = 0;
5594 SCROW nRow2 = 0;
5595 SCTAB nTab2 = 0;
5596 ScMatrixRef pQueryMatrix;
5597 switch ( GetStackType() )
5598 {
5599 case svRefList :
5600 {
5601 ScRange aRange;
5602 PopDoubleRef( aRange, nParam, nRefInList);
5603 aRange.GetVars( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2);
5604 }
5605 break;
5606 case svDoubleRef :
5607 PopDoubleRef( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2 );
5608 break;
5609 case svSingleRef :
5610 PopSingleRef( nCol1, nRow1, nTab1 );
5611 nCol2 = nCol1;
5612 nRow2 = nRow1;
5613 nTab2 = nTab1;
5614 break;
5615 case svMatrix:
5616 {
5617 pQueryMatrix = PopMatrix();
5618 if (!pQueryMatrix)
5619 {
5620 SetError( errIllegalParameter);
5621 }
5622 nCol1 = 0;
5623 nRow1 = 0;
5624 nTab1 = 0;
5625 SCSIZE nC, nR;
5626 pQueryMatrix->GetDimensions( nC, nR);
5627 nCol2 = static_cast<SCCOL>(nC - 1);
5628 nRow2 = static_cast<SCROW>(nR - 1);
5629 nTab2 = 0;
5630 }
5631 break;
5632 default:
5633 SetError( errIllegalParameter);
5634 }
5635 if ( nTab1 != nTab2 )
5636 SetError( errIllegalArgument);
5637
5638 // All reference ranges must be of same dimension and size.
5639 if (!nDimensionCols)
5640 nDimensionCols = nCol2 - nCol1 + 1;
5641 if (!nDimensionRows)
5642 nDimensionRows = nRow2 - nRow1 + 1;
5643 if ((nDimensionCols != (nCol2 - nCol1 + 1)) || (nDimensionRows != (nRow2 - nRow1 + 1)))
5644 SetError ( errIllegalArgument);
5645
5646 // recalculate matrix values
5647 if (nGlobalError == 0)
5648 {
5649 // initialize temporary result matrix
5650 if (!pResMat)
5651 {
5652 SCSIZE nResC, nResR;
5653 nResC = nCol2 - nCol1 + 1;
5654 nResR = nRow2 - nRow1 + 1;
5655 pResMat = GetNewMat(nResC, nResR);
5656 if (!pResMat)
5657 SetError( errIllegalParameter);
5658 else
5659 pResMat->FillDouble( 0.0, 0, 0, nResC-1, nResR-1);
5660 }
5661
5662 ScQueryParam rParam;
5663 rParam.nRow1 = nRow1;
5664 rParam.nRow2 = nRow2;
5665
5666 ScQueryEntry& rEntry = rParam.GetEntry(0);
5667 rEntry.bDoQuery = true;
5668 if (!bIsString)
5669 {
5670 rEntry.bQueryByString = false;
5671 rEntry.nVal = fVal;
5672 rEntry.eOp = SC_EQUAL;
5673 }
5674 else
5675 {
5676 rParam.FillInExcelSyntax(rString, 0);
5677 sal_uInt32 nIndex = 0;
5678 rEntry.bQueryByString =
5679 !(pFormatter->IsNumberFormat(
5680 *rEntry.pStr, nIndex, rEntry.nVal));
5681 if ( rEntry.bQueryByString )
5682 rParam.bRegExp = MayBeRegExp( *rEntry.pStr, pDok );
5683 }
5684 ScAddress aAdr;
5685 aAdr.SetTab( nTab1 );
5686 rParam.nCol1 = nCol1;
5687 rParam.nCol2 = nCol2;
5688 rEntry.nField = nCol1;
5689 SCsCOL nColDiff = -nCol1;
5690 SCsROW nRowDiff = -nRow1;
5691 if (pQueryMatrix)
5692 {
5693 // Never case-sensitive.
5694 ScCompareOptions aOptions( pDok, rEntry, rParam.bRegExp);
5695 ScMatrixRef pResultMatrix = QueryMat( pQueryMatrix, aOptions);
5696 if (nGlobalError || !pResultMatrix)
5697 {
5698 SetError( errIllegalParameter);
5699 }
5700
5701 for (SCCOL nCol = nCol1; nCol <= nCol2; ++nCol)
5702 {
5703 for (SCROW nRow = nRow1; nRow <= nRow2; ++nRow)
5704 {
5705 if (pResultMatrix->IsValue( nCol, nRow) &&
5706 pResultMatrix->GetDouble( nCol, nRow))
5707 {
5708 SCSIZE nC = nCol + nColDiff;
5709 SCSIZE nR = nRow + nRowDiff;
5710 pResMat->PutDouble(pResMat->GetDouble(nC, nR)+1.0, nC, nR);
5711 }
5712 }
5713 }
5714 }
5715 else
5716 {
5717 ScQueryCellIterator aCellIter(pDok, nTab1, rParam, false);
5718 // Increment Entry.nField in iterator when switching to next column.
5719 aCellIter.SetAdvanceQueryParamEntryField( true );
5720 if ( aCellIter.GetFirst() )
5721 {
5722 do
5723 {
5724 SCSIZE nC = aCellIter.GetCol() + nColDiff;
5725 SCSIZE nR = aCellIter.GetRow() + nRowDiff;
5726 pResMat->PutDouble(pResMat->GetDouble(nC, nR)+1.0, nC, nR);
5727 } while ( aCellIter.GetNext() );
5728 }
5729 }
5730 }
5731 nParamCount -= 2;
5732 }
5733
5734 if (nGlobalError)
5735 return 0; // bail out
5736
5737 // main range - only for AVERAGEIFS and SUMIFS
5738 if (nParamCount == 1)
5739 {
5740 nParam = 1;
5741 nRefInList = 0;
5742 bool bNull = true;
5743 SCCOL nMainCol1 = 0;
5744 SCROW nMainRow1 = 0;
5745 SCTAB nMainTab1 = 0;
5746 SCCOL nMainCol2 = 0;
5747 SCROW nMainRow2 = 0;
5748 SCTAB nMainTab2 = 0;
5749 ScMatrixRef pMainMatrix;
5750 switch ( GetStackType() )
5751 {
5752 case svRefList :
5753 {
5754 ScRange aRange;
5755 PopDoubleRef( aRange, nParam, nRefInList);
5756 aRange.GetVars( nMainCol1, nMainRow1, nMainTab1, nMainCol2, nMainRow2, nMainTab2);
5757 }
5758 break;
5759 case svDoubleRef :
5760 PopDoubleRef( nMainCol1, nMainRow1, nMainTab1, nMainCol2, nMainRow2, nMainTab2 );
5761 break;
5762 case svSingleRef :
5763 PopSingleRef( nMainCol1, nMainRow1, nMainTab1 );
5764 nMainCol2 = nMainCol1;
5765 nMainRow2 = nMainRow1;
5766 nMainTab2 = nMainTab1;
5767 break;
5768 case svMatrix:
5769 {
5770 pMainMatrix = PopMatrix();
5771 if (!pMainMatrix)
5772 {
5773 SetError( errIllegalParameter);
5774 }
5775 nMainCol1 = 0;
5776 nMainRow1 = 0;
5777 nMainTab1 = 0;
5778 SCSIZE nC, nR;
5779 pMainMatrix->GetDimensions( nC, nR);
5780 nMainCol2 = static_cast<SCCOL>(nC - 1);
5781 nMainRow2 = static_cast<SCROW>(nR - 1);
5782 nMainTab2 = 0;
5783 }
5784 break;
5785 default:
5786 SetError( errIllegalParameter);
5787 }
5788 if ( nMainTab1 != nMainTab2 )
5789 SetError( errIllegalArgument);
5790
5791 // All reference ranges must be of same dimension and size.
5792 if ((nDimensionCols != (nMainCol2 - nMainCol1 + 1)) || (nDimensionRows != (nMainRow2 - nMainRow1 + 1)))
5793 SetError ( errIllegalArgument);
5794
5795 if (nGlobalError)
5796 return 0; // bail out
5797
5798 // end-result calculation
5799 ScAddress aAdr;
5800 aAdr.SetTab( nMainTab1 );
5801 if (pMainMatrix)
5802 {
5803 SCSIZE nC, nR;
5804 pResMat->GetDimensions(nC, nR);
5805 for (SCSIZE nCol = 0; nCol < nC; ++nCol)
5806 {
5807 for (SCSIZE nRow = 0; nRow < nR; ++nRow)
5808 {
5809 if (pResMat->GetDouble( nCol, nRow) == nQueryCount)
5810 {
5811 if (pMainMatrix->IsValue( nCol, nRow))
5812 {
5813 fVal = pMainMatrix->GetDouble( nCol, nRow);
5814 ++fCount;
5815 if ( bNull && fVal != 0.0 )
5816 {
5817 bNull = false;
5818 fMem = fVal;
5819 }
5820 else
5821 fSum += fVal;
5822 }
5823 }
5824 }
5825 }
5826 }
5827 else
5828 {
5829 SCSIZE nC, nR;
5830 pResMat->GetDimensions(nC, nR);
5831 for (SCSIZE nCol = 0; nCol < nC; ++nCol)
5832 {
5833 for (SCSIZE nRow = 0; nRow < nR; ++nRow)
5834 {
5835 if (pResMat->GetDouble( nCol, nRow) == nQueryCount)
5836 {
5837 aAdr.SetCol( static_cast<SCCOL>(nCol) + nMainCol1);
5838 aAdr.SetRow( static_cast<SCROW>(nRow) + nMainRow1);
5839 ScBaseCell* pCell = GetCell( aAdr );
5840 if ( HasCellValueData(pCell) )
5841 {
5842 fVal = GetCellValue( aAdr, pCell );
5843 ++fCount;
5844 if ( bNull && fVal != 0.0 )
5845 {
5846 bNull = false;
5847 fMem = fVal;
5848 }
5849 else
5850 fSum += fVal;
5851 }
5852 }
5853 }
5854 }
5855 }
5856 }
5857 else
5858 {
5859 SCSIZE nC, nR;
5860 pResMat->GetDimensions(nC, nR);
5861 for (SCSIZE nCol = 0; nCol < nC; ++nCol)
5862 {
5863 for (SCSIZE nRow = 0; nRow < nR; ++nRow)
5864 if (pResMat->GetDouble( nCol, nRow) == nQueryCount)
5865 ++fCount;
5866 }
5867 }
5868
5869 switch( eFunc )
5870 {
5871 case ifSUMIFS: fRes = ::rtl::math::approxAdd( fSum, fMem ); break;
5872 case ifAVERAGEIFS: fRes = div( ::rtl::math::approxAdd( fSum, fMem ), fCount); break;
5873 case ifCOUNTIFS: fRes = fCount; break;
5874 default: ; // nothing
5875 }
5876 return fRes;
5877 }
5878 return 0;
5879 }
5880
ScSumIfs()5881 void ScInterpreter::ScSumIfs()
5882 {
5883 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "makkica", "ScInterpreter::ScSumIfs" );
5884 PushDouble( IterateParametersIfs( ifSUMIFS));
5885 }
5886
ScAverageIfs()5887 void ScInterpreter::ScAverageIfs()
5888 {
5889 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "makkica", "ScInterpreter::ScAverageIfs" );
5890 PushDouble( IterateParametersIfs( ifAVERAGEIFS));
5891 }
5892
ScCountIfs()5893 void ScInterpreter::ScCountIfs()
5894 {
5895 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "makkica", "ScInterpreter::ScCountIfs" );
5896 PushDouble( IterateParametersIfs( ifCOUNTIFS));
5897 }
5898
ScLookup()5899 void ScInterpreter::ScLookup()
5900 {
5901 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScLookup" );
5902 sal_uInt8 nParamCount = GetByte();
5903 if ( !MustHaveParamCount( nParamCount, 2, 3 ) )
5904 return ;
5905
5906 ScMatrixRef pDataMat = NULL, pResMat = NULL;
5907 SCCOL nCol1 = 0, nCol2 = 0, nResCol1 = 0, nResCol2 = 0;
5908 SCROW nRow1 = 0, nRow2 = 0, nResRow1 = 0, nResRow2 = 0;
5909 SCTAB nTab1 = 0, nResTab = 0;
5910 SCSIZE nLenMajor = 0; // length of major direction
5911 bool bVertical = true; // whether to lookup vertically or horizontally
5912
5913 // The third parameter, result array, for double, string and single reference.
5914 double fResVal = 0.0;
5915 String aResStr;
5916 ScAddress aResAdr;
5917 StackVar eResArrayType = svUnknown;
5918
5919 if (nParamCount == 3)
5920 {
5921 eResArrayType = GetStackType();
5922 switch (eResArrayType)
5923 {
5924 case svDoubleRef:
5925 {
5926 SCTAB nTabJunk;
5927 PopDoubleRef(nResCol1, nResRow1, nResTab,
5928 nResCol2, nResRow2, nTabJunk);
5929 if (nResTab != nTabJunk ||
5930 ((nResRow2 - nResRow1) > 0 && (nResCol2 - nResCol1) > 0))
5931 {
5932 // The result array must be a vector.
5933 PushIllegalParameter();
5934 return;
5935 }
5936 }
5937 break;
5938 case svMatrix:
5939 {
5940 pResMat = PopMatrix();
5941 if (!pResMat)
5942 {
5943 PushIllegalParameter();
5944 return;
5945 }
5946 SCSIZE nC, nR;
5947 pResMat->GetDimensions(nC, nR);
5948 if (nC != 1 && nR != 1)
5949 {
5950 // Result matrix must be a vector.
5951 PushIllegalParameter();
5952 return;
5953 }
5954 }
5955 break;
5956 case svDouble:
5957 fResVal = GetDouble();
5958 break;
5959 case svString:
5960 aResStr = GetString();
5961 break;
5962 case svSingleRef:
5963 PopSingleRef( aResAdr );
5964 break;
5965 default:
5966 PushIllegalParameter();
5967 return;
5968 }
5969 }
5970
5971 // For double, string and single reference.
5972 double fDataVal = 0.0;
5973 String aDataStr;
5974 ScAddress aDataAdr;
5975 bool bValueData = false;
5976
5977 // Get the data-result range and also determine whether this is vertical
5978 // lookup or horizontal lookup.
5979
5980 StackVar eDataArrayType = GetStackType();
5981 switch (eDataArrayType)
5982 {
5983 case svDoubleRef:
5984 {
5985 SCTAB nTabJunk;
5986 PopDoubleRef(nCol1, nRow1, nTab1, nCol2, nRow2, nTabJunk);
5987 if (nTab1 != nTabJunk)
5988 {
5989 PushIllegalParameter();
5990 return;
5991 }
5992 bVertical = (nRow2 - nRow1) >= (nCol2 - nCol1);
5993 nLenMajor = bVertical ? nRow2 - nRow1 + 1 : nCol2 - nCol1 + 1;
5994 }
5995 break;
5996 case svMatrix:
5997 {
5998 pDataMat = PopMatrix();
5999 if (!pDataMat)
6000 {
6001 PushIllegalParameter();
6002 return;
6003 }
6004
6005 SCSIZE nC, nR;
6006 pDataMat->GetDimensions(nC, nR);
6007 bVertical = (nR >= nC);
6008 nLenMajor = bVertical ? nR : nC;
6009 }
6010 break;
6011 case svDouble:
6012 {
6013 fDataVal = GetDouble();
6014 bValueData = true;
6015 }
6016 break;
6017 case svString:
6018 {
6019 aDataStr = GetString();
6020 }
6021 break;
6022 case svSingleRef:
6023 {
6024 PopSingleRef( aDataAdr );
6025 const ScBaseCell* pDataCell = GetCell( aDataAdr );
6026 if (HasCellEmptyData( pDataCell))
6027 {
6028 // Empty cells aren't found anywhere, bail out early.
6029 SetError( NOTAVAILABLE);
6030 }
6031 else if (HasCellValueData( pDataCell))
6032 {
6033 fDataVal = GetCellValue( aDataAdr, pDataCell );
6034 bValueData = true;
6035 }
6036 else
6037 GetCellString( aDataStr, pDataCell );
6038 }
6039 break;
6040 default:
6041 SetError( errIllegalParameter);
6042 }
6043
6044
6045 if (nGlobalError)
6046 {
6047 PushError( nGlobalError);
6048 return;
6049 }
6050
6051 // Get the lookup value.
6052
6053 ScQueryParam aParam;
6054 ScQueryEntry& rEntry = aParam.GetEntry(0);
6055 if ( !FillEntry(rEntry) )
6056 return;
6057
6058 if ( eDataArrayType == svDouble || eDataArrayType == svString ||
6059 eDataArrayType == svSingleRef )
6060 {
6061 // Delta position for a single value is always 0.
6062
6063 // Found if data <= query, but not if query is string and found data is
6064 // numeric or vice versa. This is how Excel does it but doesn't
6065 // document it.
6066
6067 bool bFound = false;
6068 if ( bValueData )
6069 {
6070 if ( rEntry.bQueryByString )
6071 bFound = false;
6072 else
6073 bFound = (fDataVal <= rEntry.nVal);
6074 }
6075 else
6076 {
6077 if ( !rEntry.bQueryByString )
6078 bFound = false;
6079 else
6080 bFound = (ScGlobal::GetCollator()->compareString( aDataStr, *rEntry.pStr) <= 0);
6081 }
6082
6083 if (!bFound)
6084 {
6085 PushNA();
6086 return;
6087 }
6088
6089 if (pResMat)
6090 {
6091 if (pResMat->IsValue( 0 ))
6092 PushDouble(pResMat->GetDouble( 0 ));
6093 else
6094 PushString(pResMat->GetString( 0 ));
6095 }
6096 else if (nParamCount == 3)
6097 {
6098 switch (eResArrayType)
6099 {
6100 case svDouble:
6101 PushDouble( fResVal );
6102 break;
6103 case svString:
6104 PushString( aResStr );
6105 break;
6106 case svDoubleRef:
6107 aResAdr.Set( nResCol1, nResRow1, nResTab);
6108 // fallthru
6109 case svSingleRef:
6110 PushCellResultToken( true, aResAdr, NULL, NULL);
6111 break;
6112 default:
6113 DBG_ERRORFILE( "ScInterpreter::ScLookup: unhandled eResArrayType, single value data");
6114 }
6115 }
6116 else
6117 {
6118 switch (eDataArrayType)
6119 {
6120 case svDouble:
6121 PushDouble( fDataVal );
6122 break;
6123 case svString:
6124 PushString( aDataStr );
6125 break;
6126 case svSingleRef:
6127 PushCellResultToken( true, aDataAdr, NULL, NULL);
6128 break;
6129 default:
6130 DBG_ERRORFILE( "ScInterpreter::ScLookup: unhandled eDataArrayType, single value data");
6131 }
6132 }
6133 return;
6134 }
6135
6136 // Now, perform the search to compute the delta position (nDelta).
6137
6138 if (pDataMat)
6139 {
6140 // Data array is given as a matrix.
6141 rEntry.bDoQuery = true;
6142 rEntry.eOp = SC_LESS_EQUAL;
6143 bool bFound = false;
6144
6145 SCSIZE nC, nR;
6146 pDataMat->GetDimensions(nC, nR);
6147
6148 // In case of non-vector matrix, only search the first row or column.
6149 ScMatrixRef pDataMat2;
6150 if (bVertical)
6151 {
6152 ScMatrixRef pTempMat(new ScMatrix(1, nR));
6153 for (SCSIZE i = 0; i < nR; ++i)
6154 if (pDataMat->IsValue(0, i))
6155 pTempMat->PutDouble(pDataMat->GetDouble(0, i), 0, i);
6156 else
6157 pTempMat->PutString(pDataMat->GetString(0, i), 0, i);
6158 pDataMat2 = pTempMat;
6159 }
6160 else
6161 {
6162 ScMatrixRef pTempMat(new ScMatrix(nC, 1));
6163 for (SCSIZE i = 0; i < nC; ++i)
6164 if (pDataMat->IsValue(i, 0))
6165 pTempMat->PutDouble(pDataMat->GetDouble(i, 0), i, 0);
6166 else
6167 pTempMat->PutString(pDataMat->GetString(i, 0), i, 0);
6168 pDataMat2 = pTempMat;
6169 }
6170
6171 // binary search for non-equality mode (the source data is
6172 // assumed to be sorted in ascending order).
6173
6174 SCCOLROW nDelta = -1;
6175
6176 SCSIZE nFirst = 0, nLast = nLenMajor-1; //, nHitIndex = 0;
6177 for (SCSIZE nLen = nLast-nFirst; nLen > 0; nLen = nLast-nFirst)
6178 {
6179 SCSIZE nMid = nFirst + nLen/2;
6180 sal_Int32 nCmp = lcl_CompareMatrix2Query( nMid, *pDataMat2, rEntry);
6181 if (nCmp == 0)
6182 {
6183 // exact match. find the last item with the same value.
6184 lcl_GetLastMatch( nMid, *pDataMat2, nLenMajor, false);
6185 nDelta = nMid;
6186 bFound = true;
6187 break;
6188 }
6189
6190 if (nLen == 1) // first and last items are next to each other.
6191 {
6192 nDelta = nCmp < 0 ? nLast - 1 : nFirst - 1;
6193 // If already the 1st item is greater there's nothing found.
6194 bFound = (nDelta >= 0);
6195 break;
6196 }
6197
6198 if (nCmp < 0)
6199 nFirst = nMid;
6200 else
6201 nLast = nMid;
6202 }
6203
6204 if (nDelta == static_cast<SCCOLROW>(nLenMajor-2)) // last item
6205 {
6206 sal_Int32 nCmp = lcl_CompareMatrix2Query(nDelta+1, *pDataMat2, rEntry);
6207 if (nCmp <= 0)
6208 {
6209 // either the last item is an exact match or the real
6210 // hit is beyond the last item.
6211 nDelta += 1;
6212 bFound = true;
6213 }
6214 }
6215 else if (nDelta > 0) // valid hit must be 2nd item or higher
6216 {
6217 // non-exact match
6218 bFound = true;
6219 }
6220
6221 // With 0-9 < A-Z, if query is numeric and data found is string, or
6222 // vice versa, the (yet another undocumented) Excel behavior is to
6223 // return #N/A instead.
6224
6225 if (bFound)
6226 {
6227 SCCOLROW i = nDelta;
6228 SCSIZE n = pDataMat->GetElementCount();
6229 if (static_cast<SCSIZE>(i) >= n)
6230 i = static_cast<SCCOLROW>(n);
6231 if (bool(rEntry.bQueryByString) == bool(pDataMat->IsValue(i)))
6232 bFound = false;
6233 }
6234
6235 if (!bFound)
6236 {
6237 PushNA();
6238 return;
6239 }
6240
6241 // Now that we've found the delta, push the result back to the cell.
6242
6243 if (pResMat)
6244 {
6245 // result array is matrix.
6246 if (static_cast<SCSIZE>(nDelta) >= pResMat->GetElementCount())
6247 {
6248 PushNA();
6249 return;
6250 }
6251 if (pResMat->IsValue(nDelta))
6252 PushDouble(pResMat->GetDouble(nDelta));
6253 else
6254 PushString(pResMat->GetString(nDelta));
6255 }
6256 else if (nParamCount == 3)
6257 {
6258 // result array is cell range.
6259 ScAddress aAdr;
6260 aAdr.SetTab(nResTab);
6261 bool bResVertical = (nResRow2 - nResRow1) > 0;
6262 if (bResVertical)
6263 {
6264 SCROW nTempRow = static_cast<SCROW>(nResRow1 + nDelta);
6265 if (nTempRow > MAXROW)
6266 {
6267 PushDouble(0);
6268 return;
6269 }
6270 aAdr.SetCol(nResCol1);
6271 aAdr.SetRow(nTempRow);
6272 }
6273 else
6274 {
6275 SCCOL nTempCol = static_cast<SCCOL>(nResCol1 + nDelta);
6276 if (nTempCol > MAXCOL)
6277 {
6278 PushDouble(0);
6279 return;
6280 }
6281 aAdr.SetCol(nTempCol);
6282 aAdr.SetRow(nResRow1);
6283 }
6284 PushCellResultToken(true, aAdr, NULL, NULL);
6285 }
6286 else
6287 {
6288 // no result array. Use the data array to get the final value from.
6289 if (bVertical)
6290 {
6291 if (pDataMat->IsValue(nC-1, nDelta))
6292 PushDouble(pDataMat->GetDouble(nC-1, nDelta));
6293 else
6294 PushString(pDataMat->GetString(nC-1, nDelta));
6295 }
6296 else
6297 {
6298 if (pDataMat->IsValue(nDelta, nR-1))
6299 PushDouble(pDataMat->GetDouble(nDelta, nR-1));
6300 else
6301 PushString(pDataMat->GetString(nDelta, nR-1));
6302 }
6303 }
6304
6305 return;
6306 }
6307
6308 // Perform cell range search.
6309
6310 aParam.nCol1 = nCol1;
6311 aParam.nRow1 = nRow1;
6312 aParam.nCol2 = bVertical ? nCol1 : nCol2;
6313 aParam.nRow2 = bVertical ? nRow2 : nRow1;
6314 aParam.bByRow = bVertical;
6315 aParam.bMixedComparison = true;
6316
6317 rEntry.bDoQuery = sal_True;
6318 rEntry.eOp = SC_LESS_EQUAL;
6319 rEntry.nField = nCol1;
6320 if ( rEntry.bQueryByString )
6321 aParam.bRegExp = MayBeRegExp( *rEntry.pStr, pDok );
6322
6323 ScQueryCellIterator aCellIter(pDok, nTab1, aParam, sal_False);
6324 SCCOL nC;
6325 SCROW nR;
6326 // Advance Entry.nField in iterator upon switching columns if
6327 // lookup in row.
6328 aCellIter.SetAdvanceQueryParamEntryField(!bVertical);
6329 if ( !aCellIter.FindEqualOrSortedLastInRange(nC, nR) )
6330 {
6331 PushNA();
6332 return;
6333 }
6334
6335 SCCOLROW nDelta = bVertical ? static_cast<SCSIZE>(nR-nRow1) : static_cast<SCSIZE>(nC-nCol1);
6336
6337 if (pResMat)
6338 {
6339 // Use the matrix result array.
6340 if (pResMat->IsValue(nDelta))
6341 PushDouble(pResMat->GetDouble(nDelta));
6342 else
6343 PushString(pResMat->GetString(nDelta));
6344 }
6345 else if (nParamCount == 3)
6346 {
6347 switch (eResArrayType)
6348 {
6349 case svDoubleRef:
6350 {
6351 // Use the result array vector. Note that the result array is assumed
6352 // to be a vector (i.e. 1-dimensinoal array).
6353
6354 ScAddress aAdr;
6355 aAdr.SetTab(nResTab);
6356 bool bResVertical = (nResRow2 - nResRow1) > 0;
6357 if (bResVertical)
6358 {
6359 SCROW nTempRow = static_cast<SCROW>(nResRow1 + nDelta);
6360 if (nTempRow > MAXROW)
6361 {
6362 PushDouble(0);
6363 return;
6364 }
6365 aAdr.SetCol(nResCol1);
6366 aAdr.SetRow(nTempRow);
6367 }
6368 else
6369 {
6370 SCCOL nTempCol = static_cast<SCCOL>(nResCol1 + nDelta);
6371 if (nTempCol > MAXCOL)
6372 {
6373 PushDouble(0);
6374 return;
6375 }
6376 aAdr.SetCol(nTempCol);
6377 aAdr.SetRow(nResRow1);
6378 }
6379 PushCellResultToken( true, aAdr, NULL, NULL);
6380 }
6381 break;
6382 case svDouble:
6383 case svString:
6384 case svSingleRef:
6385 {
6386 if (nDelta != 0)
6387 PushNA();
6388 else
6389 {
6390 switch (eResArrayType)
6391 {
6392 case svDouble:
6393 PushDouble( fResVal );
6394 break;
6395 case svString:
6396 PushString( aResStr );
6397 break;
6398 case svSingleRef:
6399 PushCellResultToken( true, aResAdr, NULL, NULL);
6400 break;
6401 default:
6402 ; // nothing
6403 }
6404 }
6405 }
6406 break;
6407 default:
6408 DBG_ERRORFILE( "ScInterpreter::ScLookup: unhandled eResArrayType, range search");
6409 }
6410 }
6411 else
6412 {
6413 // Regardless of whether or not the result array exists, the last
6414 // array is always used as the "result" array.
6415
6416 ScAddress aAdr;
6417 aAdr.SetTab(nTab1);
6418 if (bVertical)
6419 {
6420 SCROW nTempRow = static_cast<SCROW>(nRow1 + nDelta);
6421 if (nTempRow > MAXROW)
6422 {
6423 PushDouble(0);
6424 return;
6425 }
6426 aAdr.SetCol(nCol2);
6427 aAdr.SetRow(nTempRow);
6428 }
6429 else
6430 {
6431 SCCOL nTempCol = static_cast<SCCOL>(nCol1 + nDelta);
6432 if (nTempCol > MAXCOL)
6433 {
6434 PushDouble(0);
6435 return;
6436 }
6437 aAdr.SetCol(nTempCol);
6438 aAdr.SetRow(nRow2);
6439 }
6440 PushCellResultToken(true, aAdr, NULL, NULL);
6441 }
6442 }
6443
6444
ScHLookup()6445 void ScInterpreter::ScHLookup()
6446 {
6447 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScHLookup" );
6448 CalculateLookup(sal_True);
6449 }
CalculateLookup(sal_Bool HLookup)6450 void ScInterpreter::CalculateLookup(sal_Bool HLookup)
6451 {
6452 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::CalculateLookup" );
6453 sal_uInt8 nParamCount = GetByte();
6454 if ( MustHaveParamCount( nParamCount, 3, 4 ) )
6455 {
6456 sal_Bool bSorted;
6457 if (nParamCount == 4)
6458 bSorted = GetBool();
6459 else
6460 bSorted = sal_True;
6461 double fIndex = ::rtl::math::approxFloor( GetDouble() ) - 1.0;
6462 ScMatrixRef pMat = NULL;
6463 SCSIZE nC = 0, nR = 0;
6464 SCCOL nCol1 = 0;
6465 SCROW nRow1 = 0;
6466 SCTAB nTab1 = 0;
6467 SCCOL nCol2 = 0;
6468 SCROW nRow2 = 0;
6469 SCTAB nTab2;
6470 if (GetStackType() == svDoubleRef)
6471 {
6472 PopDoubleRef(nCol1, nRow1, nTab1, nCol2, nRow2, nTab2);
6473 if (nTab1 != nTab2)
6474 {
6475 PushIllegalParameter();
6476 return;
6477 }
6478 }
6479 else if (GetStackType() == svMatrix)
6480 {
6481 pMat = PopMatrix();
6482 if (pMat)
6483 pMat->GetDimensions(nC, nR);
6484 else
6485 {
6486 PushIllegalParameter();
6487 return;
6488 }
6489 }
6490 else
6491 {
6492 PushIllegalParameter();
6493 return;
6494 }
6495 if ( fIndex < 0.0 || (HLookup ? (pMat ? (fIndex >= nR) : (fIndex+nRow1 > nRow2)) : (pMat ? (fIndex >= nC) : (fIndex+nCol1 > nCol2)) ) )
6496 {
6497 PushIllegalArgument();
6498 return;
6499 }
6500 SCROW nZIndex = static_cast<SCROW>(fIndex);
6501 SCCOL nSpIndex = static_cast<SCCOL>(fIndex);
6502
6503 if (!pMat)
6504 {
6505 nZIndex += nRow1; // Wertzeile
6506 nSpIndex = sal::static_int_cast<SCCOL>( nSpIndex + nCol1 ); // value column
6507 }
6508
6509 if (nGlobalError == 0)
6510 {
6511 ScQueryParam rParam;
6512 rParam.nCol1 = nCol1;
6513 rParam.nRow1 = nRow1;
6514 if ( HLookup )
6515 {
6516 rParam.nCol2 = nCol2;
6517 rParam.nRow2 = nRow1; // nur in der ersten Zeile suchen
6518 rParam.bByRow = sal_False;
6519 } // if ( HLookup )
6520 else
6521 {
6522 rParam.nCol2 = nCol1; // nur in der ersten Spalte suchen
6523 rParam.nRow2 = nRow2;
6524 rParam.nTab = nTab1;
6525 }
6526 rParam.bMixedComparison = sal_True;
6527
6528 ScQueryEntry& rEntry = rParam.GetEntry(0);
6529 rEntry.bDoQuery = sal_True;
6530 if ( bSorted )
6531 rEntry.eOp = SC_LESS_EQUAL;
6532 if ( !FillEntry(rEntry) )
6533 return;
6534 if ( rEntry.bQueryByString )
6535 rParam.bRegExp = MayBeRegExp( *rEntry.pStr, pDok );
6536 if (pMat)
6537 {
6538 SCSIZE nMatCount = HLookup ? nC : nR;
6539 SCSIZE nDelta = SCSIZE_MAX;
6540 if (rEntry.bQueryByString)
6541 {
6542 //!!!!!!!
6543 //! TODO: enable regex on matrix strings
6544 //!!!!!!!
6545 String aParamStr = *rEntry.pStr;
6546 if ( bSorted )
6547 {
6548 static CollatorWrapper* pCollator = ScGlobal::GetCollator();
6549 for (SCSIZE i = 0; i < nMatCount; i++)
6550 {
6551 if (HLookup ? pMat->IsString(i, 0) : pMat->IsString(0, i))
6552 {
6553 sal_Int32 nRes =
6554 pCollator->compareString( HLookup ? pMat->GetString(i,0) : pMat->GetString(0,i), aParamStr);
6555 if (nRes <= 0)
6556 nDelta = i;
6557 else if (i>0) // #i2168# ignore first mismatch
6558 i = nMatCount+1;
6559 }
6560 else
6561 nDelta = i;
6562 }
6563 }
6564 else
6565 {
6566 for (SCSIZE i = 0; i < nMatCount; i++)
6567 {
6568 if (HLookup ? pMat->IsString(i, 0) : pMat->IsString(0, i))
6569 {
6570 if ( ScGlobal::GetpTransliteration()->isEqual(
6571 HLookup ? pMat->GetString(i,0) : pMat->GetString(0,i), aParamStr ) )
6572 {
6573 nDelta = i;
6574 i = nMatCount + 1;
6575 }
6576 }
6577 }
6578 }
6579 }
6580 else
6581 {
6582 if ( bSorted )
6583 {
6584 // #i2168# ignore strings
6585 for (SCSIZE i = 0; i < nMatCount; i++)
6586 {
6587 if (!(HLookup ? pMat->IsString(i, 0) : pMat->IsString(0, i)))
6588 {
6589 if ((HLookup ? pMat->GetDouble(i,0) : pMat->GetDouble(0,i)) <= rEntry.nVal)
6590 nDelta = i;
6591 else
6592 i = nMatCount+1;
6593 }
6594 }
6595 }
6596 else
6597 {
6598 for (SCSIZE i = 0; i < nMatCount; i++)
6599 {
6600 if (!(HLookup ? pMat->IsString(i, 0) : pMat->IsString(0, i)))
6601 {
6602 if ((HLookup ? pMat->GetDouble(i,0) : pMat->GetDouble(0,i)) == rEntry.nVal)
6603 {
6604 nDelta = i;
6605 i = nMatCount + 1;
6606 }
6607 }
6608 }
6609 }
6610 }
6611 if ( nDelta != SCSIZE_MAX )
6612 {
6613 SCSIZE nX = static_cast<SCSIZE>(nSpIndex);
6614 SCSIZE nY = nDelta;
6615 if ( HLookup )
6616 {
6617 nX = nDelta;
6618 nY = static_cast<SCSIZE>(nZIndex);
6619 }
6620 if ( pMat->IsString( nX, nY) )
6621 PushString(pMat->GetString( nX,nY));
6622 else
6623 PushDouble(pMat->GetDouble( nX,nY));
6624 }
6625 else
6626 PushNA();
6627 }
6628 else
6629 {
6630 rEntry.nField = nCol1;
6631 sal_Bool bFound = sal_False;
6632 SCCOL nCol = 0;
6633 SCROW nRow = 0;
6634 if ( bSorted )
6635 rEntry.eOp = SC_LESS_EQUAL;
6636 if ( HLookup )
6637 {
6638 ScQueryCellIterator aCellIter(pDok, nTab1, rParam, sal_False);
6639 // advance Entry.nField in Iterator upon switching columns
6640 aCellIter.SetAdvanceQueryParamEntryField( sal_True );
6641 if ( bSorted )
6642 {
6643 SCROW nRow1_temp;
6644 bFound = aCellIter.FindEqualOrSortedLastInRange( nCol, nRow1_temp );
6645 }
6646 else if ( aCellIter.GetFirst() )
6647 {
6648 bFound = sal_True;
6649 nCol = aCellIter.GetCol();
6650 }
6651 nRow = nZIndex;
6652 } // if ( HLookup )
6653 else
6654 {
6655 ScAddress aResultPos( nCol1, nRow1, nTab1);
6656 bFound = LookupQueryWithCache( aResultPos, rParam);
6657 nRow = aResultPos.Row();
6658 nCol = nSpIndex;
6659 }
6660 if ( bFound )
6661 {
6662 ScAddress aAdr( nCol, nRow, nTab1 );
6663 PushCellResultToken( true, aAdr, NULL, NULL);
6664 }
6665 else
6666 PushNA();
6667 }
6668 }
6669 else
6670 PushIllegalParameter();
6671 }
6672 }
6673
FillEntry(ScQueryEntry & rEntry)6674 bool ScInterpreter::FillEntry(ScQueryEntry& rEntry)
6675 {
6676 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::FillEntry" );
6677 switch ( GetStackType() )
6678 {
6679 case svDouble:
6680 {
6681 rEntry.bQueryByString = sal_False;
6682 rEntry.nVal = GetDouble();
6683 }
6684 break;
6685 case svString:
6686 {
6687 const String sStr = GetString();
6688 rEntry.bQueryByString = sal_True;
6689 *rEntry.pStr = sStr;
6690 }
6691 break;
6692 case svDoubleRef :
6693 case svSingleRef :
6694 {
6695 ScAddress aAdr;
6696 if ( !PopDoubleRefOrSingleRef( aAdr ) )
6697 {
6698 PushInt(0);
6699 return false;
6700 }
6701 ScBaseCell* pCell = GetCell( aAdr );
6702 if (HasCellValueData(pCell))
6703 {
6704 rEntry.bQueryByString = sal_False;
6705 rEntry.nVal = GetCellValue( aAdr, pCell );
6706 }
6707 else
6708 {
6709 if ( GetCellType( pCell ) == CELLTYPE_NOTE )
6710 {
6711 rEntry.bQueryByString = sal_False;
6712 rEntry.nVal = 0.0;
6713 }
6714 else
6715 {
6716 String sStr;
6717 GetCellString(sStr, pCell);
6718 rEntry.bQueryByString = sal_True;
6719 *rEntry.pStr = sStr;
6720 }
6721 }
6722 }
6723 break;
6724 case svMatrix :
6725 {
6726 const ScMatValType nType = GetDoubleOrStringFromMatrix(rEntry.nVal, *rEntry.pStr);
6727 rEntry.bQueryByString = ScMatrix::IsNonValueType( nType);
6728 }
6729 break;
6730 default:
6731 {
6732 PushIllegalParameter();
6733 return false;
6734 }
6735 } // switch ( GetStackType() )
6736 return true;
6737 }
ScVLookup()6738 void ScInterpreter::ScVLookup()
6739 {
6740 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScVLookup" );
6741 CalculateLookup(sal_False);
6742 }
6743
ScSubTotal()6744 void ScInterpreter::ScSubTotal()
6745 {
6746 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScSubTotal" );
6747 sal_uInt8 nParamCount = GetByte();
6748 if ( MustHaveParamCountMin( nParamCount, 2 ) )
6749 {
6750 // We must fish the 1st parameter deep from the stack! And push it on top.
6751 const FormulaToken* p = pStack[ sp - nParamCount ];
6752 PushTempToken( *p );
6753 int nFunc = (int) ::rtl::math::approxFloor( GetDouble() );
6754 if( nFunc < 1 || nFunc > 11 )
6755 PushIllegalArgument(); // simulate return on stack, not SetError(...)
6756 else
6757 {
6758 cPar = nParamCount - 1;
6759 glSubTotal = sal_True;
6760 switch( nFunc )
6761 {
6762 case SUBTOTAL_FUNC_AVE : ScAverage(); break;
6763 case SUBTOTAL_FUNC_CNT : ScCount(); break;
6764 case SUBTOTAL_FUNC_CNT2 : ScCount2(); break;
6765 case SUBTOTAL_FUNC_MAX : ScMax(); break;
6766 case SUBTOTAL_FUNC_MIN : ScMin(); break;
6767 case SUBTOTAL_FUNC_PROD : ScProduct(); break;
6768 case SUBTOTAL_FUNC_STD : ScStDev(); break;
6769 case SUBTOTAL_FUNC_STDP : ScStDevP(); break;
6770 case SUBTOTAL_FUNC_SUM : ScSum(); break;
6771 case SUBTOTAL_FUNC_VAR : ScVar(); break;
6772 case SUBTOTAL_FUNC_VARP : ScVarP(); break;
6773 default : PushIllegalArgument(); break;
6774 }
6775 glSubTotal = sal_False;
6776 }
6777 // Get rid of the 1st (fished) parameter.
6778 double nVal = GetDouble();
6779 Pop();
6780 PushDouble( nVal );
6781 }
6782 }
6783
GetDBParams(sal_Bool & rMissingField)6784 ScDBQueryParamBase* ScInterpreter::GetDBParams( sal_Bool& rMissingField )
6785 {
6786 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::GetDBParams" );
6787 sal_Bool bAllowMissingField = sal_False;
6788 if ( rMissingField )
6789 {
6790 bAllowMissingField = sal_True;
6791 rMissingField = sal_False;
6792 }
6793 if ( GetByte() == 3 )
6794 {
6795 // First, get the query criteria range.
6796 ::std::unique_ptr<ScDBRangeBase> pQueryRef( PopDoubleRef() );
6797 if (!pQueryRef.get())
6798 return NULL;
6799
6800 sal_Bool bByVal = sal_True;
6801 double nVal = 0.0;
6802 String aStr;
6803 ScRange aMissingRange;
6804 sal_Bool bRangeFake = sal_False;
6805 switch (GetStackType())
6806 {
6807 case svDouble :
6808 nVal = ::rtl::math::approxFloor( GetDouble() );
6809 if ( bAllowMissingField && nVal == 0.0 )
6810 rMissingField = sal_True; // fake missing parameter
6811 break;
6812 case svString :
6813 bByVal = sal_False;
6814 aStr = GetString();
6815 break;
6816 case svSingleRef :
6817 {
6818 ScAddress aAdr;
6819 PopSingleRef( aAdr );
6820 ScBaseCell* pCell = GetCell( aAdr );
6821 if (HasCellValueData(pCell))
6822 nVal = GetCellValue( aAdr, pCell );
6823 else
6824 {
6825 bByVal = sal_False;
6826 GetCellString(aStr, pCell);
6827 }
6828 }
6829 break;
6830 case svDoubleRef :
6831 if ( bAllowMissingField )
6832 { // fake missing parameter for old SO compatibility
6833 bRangeFake = sal_True;
6834 PopDoubleRef( aMissingRange );
6835 }
6836 else
6837 {
6838 PopError();
6839 SetError( errIllegalParameter );
6840 }
6841 break;
6842 case svMissing :
6843 PopError();
6844 if ( bAllowMissingField )
6845 rMissingField = sal_True;
6846 else
6847 SetError( errIllegalParameter );
6848 break;
6849 default:
6850 PopError();
6851 SetError( errIllegalParameter );
6852 }
6853
6854 unique_ptr<ScDBRangeBase> pDBRef( PopDoubleRef() );
6855
6856 if (nGlobalError || !pDBRef.get())
6857 return NULL;
6858
6859 if ( bRangeFake )
6860 {
6861 // range parameter must match entire database range
6862 if (pDBRef->isRangeEqual(aMissingRange))
6863 rMissingField = sal_True;
6864 else
6865 SetError( errIllegalParameter );
6866 }
6867
6868 if (nGlobalError)
6869 return NULL;
6870
6871 SCCOL nField = pDBRef->getFirstFieldColumn();
6872 if (rMissingField)
6873 ; // special case
6874 else if (bByVal)
6875 {
6876 if (nVal > pDBRef->getColSize())
6877 return NULL; // ODF 1.3 4.11.10: a field that does not exist is an error
6878 nField = pDBRef->findFieldColumn(static_cast<SCCOL>(nVal));
6879 }
6880 else
6881 {
6882 sal_uInt16 nErr = 0;
6883 nField = pDBRef->findFieldColumn(aStr, &nErr);
6884 SetError(nErr);
6885 }
6886
6887 if (!ValidCol(nField))
6888 return NULL;
6889
6890 unique_ptr<ScDBQueryParamBase> pParam( pDBRef->createQueryParam(pQueryRef.get()) );
6891
6892 if (pParam.get())
6893 {
6894 // An allowed missing field parameter sets the result field
6895 // to any of the query fields, just to be able to return
6896 // some cell from the iterator.
6897 if ( rMissingField )
6898 nField = static_cast<SCCOL>(pParam->GetEntry(0).nField);
6899 pParam->mnField = nField;
6900
6901 SCSIZE nCount = pParam->GetEntryCount();
6902 for ( SCSIZE i=0; i < nCount; i++ )
6903 {
6904 ScQueryEntry& rEntry = pParam->GetEntry(i);
6905 if ( rEntry.bDoQuery )
6906 {
6907 sal_uInt32 nIndex = 0;
6908 rEntry.bQueryByString = !pFormatter->IsNumberFormat(
6909 *rEntry.pStr, nIndex, rEntry.nVal );
6910 if ( rEntry.bQueryByString && !pParam->bRegExp )
6911 pParam->bRegExp = MayBeRegExp( *rEntry.pStr, pDok );
6912 }
6913 else
6914 break; // for
6915 }
6916 return pParam.release();
6917 }
6918 }
6919 return NULL;
6920 }
6921
6922
DBIterator(ScIterFunc eFunc)6923 void ScInterpreter::DBIterator( ScIterFunc eFunc )
6924 {
6925 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::DBIterator" );
6926 double nErg = 0.0;
6927 double fMem = 0.0;
6928 sal_Bool bNull = sal_True;
6929 sal_uLong nCount = 0;
6930 sal_Bool bMissingField = sal_False;
6931 unique_ptr<ScDBQueryParamBase> pQueryParam( GetDBParams(bMissingField) );
6932 if (pQueryParam.get())
6933 {
6934 ScDBQueryDataIterator aValIter(pDok, pQueryParam.release());
6935 ScDBQueryDataIterator::Value aValue;
6936 if ( aValIter.GetFirst(aValue) && !aValue.mnError )
6937 {
6938 switch( eFunc )
6939 {
6940 case ifPRODUCT: nErg = 1; break;
6941 case ifMAX: nErg = -MAXDOUBLE; break;
6942 case ifMIN: nErg = MAXDOUBLE; break;
6943 default: ; // nothing
6944 }
6945 do
6946 {
6947 nCount++;
6948 switch( eFunc )
6949 {
6950 case ifAVERAGE:
6951 case ifSUM:
6952 if ( bNull && aValue.mfValue != 0.0 )
6953 {
6954 bNull = sal_False;
6955 fMem = aValue.mfValue;
6956 }
6957 else
6958 nErg += aValue.mfValue;
6959 break;
6960 case ifSUMSQ: nErg += aValue.mfValue * aValue.mfValue; break;
6961 case ifPRODUCT: nErg *= aValue.mfValue; break;
6962 case ifMAX: if( aValue.mfValue > nErg ) nErg = aValue.mfValue; break;
6963 case ifMIN: if( aValue.mfValue < nErg ) nErg = aValue.mfValue; break;
6964 default: ; // nothing
6965 }
6966 }
6967 while ( aValIter.GetNext(aValue) && !aValue.mnError );
6968 }
6969 SetError(aValue.mnError);
6970 }
6971 else
6972 SetError( errIllegalParameter);
6973 switch( eFunc )
6974 {
6975 case ifCOUNT: nErg = nCount; break;
6976 case ifSUM: nErg = ::rtl::math::approxAdd( nErg, fMem ); break;
6977 case ifAVERAGE: nErg = ::rtl::math::approxAdd( nErg, fMem ) / nCount; break;
6978 default: ; // nothing
6979 }
6980 PushDouble( nErg );
6981 }
6982
6983
ScDBSum()6984 void ScInterpreter::ScDBSum()
6985 {
6986 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScDBSum" );
6987 DBIterator( ifSUM );
6988 }
6989
6990
ScDBCount()6991 void ScInterpreter::ScDBCount()
6992 {
6993 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScDBCount" );
6994 sal_Bool bMissingField = sal_True;
6995 unique_ptr<ScDBQueryParamBase> pQueryParam( GetDBParams(bMissingField) );
6996 if (pQueryParam.get())
6997 {
6998 sal_uLong nCount = 0;
6999 if ( bMissingField )
7000 { // count all matching records, whatever their cells contain
7001 ScDBQueryDataIterator aRecIter( pDok, pQueryParam.release());
7002 nCount = aRecIter.CountRecords();
7003 }
7004 else
7005 { // count only matching records with a value in the "result" field
7006 ScDBQueryDataIterator aValIter( pDok, pQueryParam.release());
7007 ScDBQueryDataIterator::Value aValue;
7008 if ( aValIter.GetFirst(aValue) && !aValue.mnError )
7009 {
7010 do
7011 {
7012 nCount++;
7013 }
7014 while ( aValIter.GetNext(aValue) && !aValue.mnError );
7015 }
7016 SetError(aValue.mnError);
7017 }
7018 PushDouble( nCount );
7019 }
7020 else
7021 PushIllegalParameter();
7022 }
7023
7024
ScDBCount2()7025 void ScInterpreter::ScDBCount2()
7026 {
7027 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScDBCount2" );
7028 sal_Bool bMissingField = sal_True;
7029 unique_ptr<ScDBQueryParamBase> pQueryParam( GetDBParams(bMissingField) );
7030 if (pQueryParam.get())
7031 {
7032 sal_uLong nCount = 0;
7033 if ( bMissingField )
7034 { // count all matching records, whatever their cells contain
7035 ScDBQueryDataIterator aRecIter( pDok, pQueryParam.release());
7036 PushDouble( aRecIter.CountRecords() );
7037 return;
7038 }
7039 pQueryParam->mbSkipString = false;
7040 ScDBQueryDataIterator aValIter( pDok, pQueryParam.release());
7041 ScDBQueryDataIterator::Value aValue;
7042 if ( aValIter.GetFirst(aValue) && !aValue.mnError )
7043 {
7044 do
7045 {
7046 nCount++;
7047 }
7048 while ( aValIter.GetNext(aValue) && !aValue.mnError );
7049 }
7050 SetError(aValue.mnError);
7051 PushDouble( nCount );
7052 }
7053 else
7054 PushIllegalParameter();
7055 }
7056
7057
ScDBAverage()7058 void ScInterpreter::ScDBAverage()
7059 {
7060 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScDBAverage" );
7061 DBIterator( ifAVERAGE );
7062 }
7063
7064
ScDBMax()7065 void ScInterpreter::ScDBMax()
7066 {
7067 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScDBMax" );
7068 DBIterator( ifMAX );
7069 }
7070
7071
ScDBMin()7072 void ScInterpreter::ScDBMin()
7073 {
7074 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScDBMin" );
7075 DBIterator( ifMIN );
7076 }
7077
7078
ScDBProduct()7079 void ScInterpreter::ScDBProduct()
7080 {
7081 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScDBProduct" );
7082 DBIterator( ifPRODUCT );
7083 }
7084
7085
GetDBStVarParams(double & rVal,double & rValCount)7086 void ScInterpreter::GetDBStVarParams( double& rVal, double& rValCount )
7087 {
7088 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::GetDBStVarParams" );
7089 std::vector<double> values;
7090 double vSum = 0.0;
7091 double vMean = 0.0;
7092
7093 rValCount = 0.0;
7094 double fSum = 0.0;
7095 sal_Bool bMissingField = sal_False;
7096 unique_ptr<ScDBQueryParamBase> pQueryParam( GetDBParams(bMissingField) );
7097 if (pQueryParam.get())
7098 {
7099 ScDBQueryDataIterator aValIter(pDok, pQueryParam.release());
7100 ScDBQueryDataIterator::Value aValue;
7101 if (aValIter.GetFirst(aValue) && !aValue.mnError)
7102 {
7103 do
7104 {
7105 rValCount++;
7106 values.push_back(aValue.mfValue);
7107 fSum += aValue.mfValue;
7108 }
7109 while ((aValue.mnError == 0) && aValIter.GetNext(aValue));
7110 }
7111 SetError(aValue.mnError);
7112 }
7113 else
7114 SetError( errIllegalParameter);
7115
7116 vMean = fSum / values.size();
7117
7118 for (size_t i = 0; i < values.size(); i++)
7119 vSum += (values[i] - vMean) * (values[i] - vMean);
7120
7121 rVal = vSum;
7122 }
7123
7124
ScDBStdDev()7125 void ScInterpreter::ScDBStdDev()
7126 {
7127 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScDBStdDev" );
7128 double fVal, fCount;
7129 GetDBStVarParams( fVal, fCount );
7130 PushDouble( sqrt(fVal/(fCount-1)));
7131 }
7132
7133
ScDBStdDevP()7134 void ScInterpreter::ScDBStdDevP()
7135 {
7136 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScDBStdDevP" );
7137 double fVal, fCount;
7138 GetDBStVarParams( fVal, fCount );
7139 PushDouble( sqrt(fVal/fCount));
7140 }
7141
7142
ScDBVar()7143 void ScInterpreter::ScDBVar()
7144 {
7145 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScDBVar" );
7146 double fVal, fCount;
7147 GetDBStVarParams( fVal, fCount );
7148 PushDouble(fVal/(fCount-1));
7149 }
7150
7151
ScDBVarP()7152 void ScInterpreter::ScDBVarP()
7153 {
7154 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScDBVarP" );
7155 double fVal, fCount;
7156 GetDBStVarParams( fVal, fCount );
7157 PushDouble(fVal/fCount);
7158 }
7159
7160
lcl_CreateExternalRefSubroutine(const ScAddress & rPos,ScDocument * pDoc,const ScAddress::ExternalInfo & rExtInfo,const ScRefAddress & rRefAd1,const ScRefAddress * pRefAd2)7161 FormulaSubroutineToken* lcl_CreateExternalRefSubroutine( const ScAddress& rPos, ScDocument* pDoc,
7162 const ScAddress::ExternalInfo& rExtInfo, const ScRefAddress& rRefAd1,
7163 const ScRefAddress* pRefAd2 )
7164 {
7165 ScExternalRefManager* pRefMgr = pDoc->GetExternalRefManager();
7166 size_t nSheets = 1;
7167 const String* pRealTab = pRefMgr->getRealTableName( rExtInfo.mnFileId, rExtInfo.maTabName);
7168 ScTokenArray* pTokenArray = new ScTokenArray;
7169 if (pRefAd2)
7170 {
7171 ScComplexRefData aRef;
7172 aRef.InitRangeRel( ScRange( rRefAd1.GetAddress(), pRefAd2->GetAddress()), rPos);
7173 aRef.Ref1.SetColRel( rRefAd1.IsRelCol());
7174 aRef.Ref1.SetRowRel( rRefAd1.IsRelRow());
7175 aRef.Ref1.SetTabRel( rRefAd1.IsRelTab());
7176 aRef.Ref1.SetFlag3D( true);
7177 aRef.Ref2.SetColRel( pRefAd2->IsRelCol());
7178 aRef.Ref2.SetRowRel( pRefAd2->IsRelRow());
7179 aRef.Ref2.SetTabRel( pRefAd2->IsRelTab());
7180 nSheets = aRef.Ref2.nTab - aRef.Ref1.nTab + 1;
7181 aRef.Ref2.SetFlag3D( nSheets > 1 );
7182 pTokenArray->AddExternalDoubleReference( rExtInfo.mnFileId,
7183 (pRealTab ? *pRealTab : rExtInfo.maTabName), aRef);
7184 }
7185 else
7186 {
7187 ScSingleRefData aRef;
7188 aRef.InitAddressRel( rRefAd1.GetAddress(), rPos);
7189 aRef.SetColRel( rRefAd1.IsRelCol());
7190 aRef.SetRowRel( rRefAd1.IsRelRow());
7191 aRef.SetTabRel( rRefAd1.IsRelTab());
7192 aRef.SetFlag3D( true);
7193 pTokenArray->AddExternalSingleReference( rExtInfo.mnFileId,
7194 (pRealTab ? *pRealTab : rExtInfo.maTabName), aRef);
7195 }
7196 // The indirect usage of the external table can't be detected during the
7197 // store-to-file cycle, mark it as permanently referenced so it gets stored
7198 // even if not directly referenced anywhere.
7199 pRefMgr->setCacheTableReferencedPermanently( rExtInfo.mnFileId,
7200 rExtInfo.maTabName, nSheets);
7201 ScCompiler aComp( pDoc, rPos, *pTokenArray);
7202 aComp.CompileTokenArray();
7203 return new FormulaSubroutineToken( pTokenArray);
7204 }
7205
7206
ScIndirect()7207 void ScInterpreter::ScIndirect()
7208 {
7209 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScIndirect" );
7210 sal_uInt8 nParamCount = GetByte();
7211 if ( MustHaveParamCount( nParamCount, 1, 2 ) )
7212 {
7213 bool bTryXlA1 = true; // whether to try XL_A1 style as well.
7214 FormulaGrammar::AddressConvention eConv = FormulaGrammar::CONV_OOO;
7215 if (nParamCount == 2 && 0.0 == ::rtl::math::approxFloor( GetDouble()))
7216 {
7217 eConv = FormulaGrammar::CONV_XL_R1C1;
7218 bTryXlA1 = false;
7219 }
7220 const ScAddress::Details aDetails( eConv, aPos );
7221 const ScAddress::Details aDetailsXlA1( FormulaGrammar::CONV_XL_A1, aPos );
7222 SCTAB nTab = aPos.Tab();
7223 String sRefStr( GetString() );
7224 ScRefAddress aRefAd, aRefAd2;
7225 ScAddress::ExternalInfo aExtInfo;
7226 if ( ConvertDoubleRef( pDok, sRefStr, nTab, aRefAd, aRefAd2, aDetails, &aExtInfo) ||
7227 (bTryXlA1 && ConvertDoubleRef( pDok, sRefStr, nTab, aRefAd,
7228 aRefAd2, aDetailsXlA1, &aExtInfo)))
7229 {
7230 if (aExtInfo.mbExternal)
7231 {
7232 // Push a subroutine that resolves the external reference as
7233 // the next instruction.
7234 PushTempToken( lcl_CreateExternalRefSubroutine( aPos, pDok,
7235 aExtInfo, aRefAd, &aRefAd2));
7236 }
7237 else
7238 PushDoubleRef( aRefAd.Col(), aRefAd.Row(), aRefAd.Tab(),
7239 aRefAd2.Col(), aRefAd2.Row(), aRefAd2.Tab() );
7240 }
7241 else if ( ConvertSingleRef ( pDok, sRefStr, nTab, aRefAd, aDetails, &aExtInfo) ||
7242 (bTryXlA1 && ConvertSingleRef ( pDok, sRefStr, nTab, aRefAd,
7243 aDetailsXlA1, &aExtInfo)))
7244 {
7245 if (aExtInfo.mbExternal)
7246 {
7247 // Push a subroutine that resolves the external reference as
7248 // the next instruction.
7249 PushTempToken( lcl_CreateExternalRefSubroutine( aPos, pDok,
7250 aExtInfo, aRefAd, NULL));
7251 }
7252 else
7253 PushSingleRef( aRefAd.Col(), aRefAd.Row(), aRefAd.Tab() );
7254 }
7255 else
7256 {
7257 do
7258 {
7259 ScRangeName* pNames = pDok->GetRangeName();
7260 if (!pNames)
7261 break;
7262
7263 sal_uInt16 nPos = 0;
7264 if (!pNames->SearchName( sRefStr, nPos))
7265 break;
7266
7267 ScRangeData* rData = (*pNames)[nPos];
7268 if (!rData)
7269 break;
7270
7271 // We need this in order to obtain a good range.
7272 rData->ValidateTabRefs();
7273
7274 ScRange aRange;
7275 #if 0
7276 // This is some really odd Excel behavior and renders named
7277 // ranges containing relative references totally useless.
7278 if (!rData->IsReference(aRange, ScAddress( aPos.Tab(), 0, 0)))
7279 break;
7280 #else
7281 // This is the usual way to treat named ranges containing
7282 // relative references.
7283 if (!rData->IsReference( aRange, aPos))
7284 break;
7285 #endif
7286
7287 if (aRange.aStart == aRange.aEnd)
7288 PushSingleRef( aRange.aStart.Col(), aRange.aStart.Row(),
7289 aRange.aStart.Tab());
7290 else
7291 PushDoubleRef( aRange.aStart.Col(), aRange.aStart.Row(),
7292 aRange.aStart.Tab(), aRange.aEnd.Col(),
7293 aRange.aEnd.Row(), aRange.aEnd.Tab());
7294
7295 // success!
7296 return;
7297 }
7298 while (false);
7299
7300 PushIllegalArgument();
7301 }
7302 }
7303 }
7304
7305
ScAddressFunc()7306 void ScInterpreter::ScAddressFunc()
7307 {
7308 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScAddressFunc" );
7309 String sTabStr;
7310
7311 sal_uInt8 nParamCount = GetByte();
7312 if( !MustHaveParamCount( nParamCount, 2, 5 ) )
7313 return;
7314
7315 if( nParamCount >= 5 )
7316 sTabStr = GetString();
7317
7318 FormulaGrammar::AddressConvention eConv = FormulaGrammar::CONV_OOO; // default
7319 if( nParamCount >= 4 && 0.0 == ::rtl::math::approxFloor( GetDoubleWithDefault( 1.0)))
7320 eConv = FormulaGrammar::CONV_XL_R1C1;
7321
7322 sal_uInt16 nFlags = SCA_COL_ABSOLUTE | SCA_ROW_ABSOLUTE; // default
7323 if( nParamCount >= 3 )
7324 {
7325 sal_uInt16 n = (sal_uInt16) ::rtl::math::approxFloor( GetDoubleWithDefault( 1.0));
7326 switch ( n )
7327 {
7328 default :
7329 PushNoValue();
7330 return;
7331
7332 case 5:
7333 case 1 : break; // default
7334 case 6:
7335 case 2 : nFlags = SCA_ROW_ABSOLUTE; break;
7336 case 7:
7337 case 3 : nFlags = SCA_COL_ABSOLUTE; break;
7338 case 8:
7339 case 4 : nFlags = 0; break; // both relative
7340 }
7341 }
7342 nFlags |= SCA_VALID | SCA_VALID_ROW | SCA_VALID_COL;
7343
7344 SCCOL nCol = (SCCOL) ::rtl::math::approxFloor(GetDouble());
7345 SCROW nRow = (SCROW) ::rtl::math::approxFloor(GetDouble());
7346 if( eConv == FormulaGrammar::CONV_XL_R1C1 )
7347 {
7348 // YUCK! The XL interface actually treats rel R1C1 refs differently
7349 // than A1
7350 if( !(nFlags & SCA_COL_ABSOLUTE) )
7351 nCol += aPos.Col() + 1;
7352 if( !(nFlags & SCA_ROW_ABSOLUTE) )
7353 nRow += aPos.Row() + 1;
7354 }
7355
7356 --nCol;
7357 --nRow;
7358 if(!ValidCol( nCol) || !ValidRow( nRow))
7359 {
7360 PushIllegalArgument();
7361 return;
7362 }
7363
7364 String aRefStr;
7365 const ScAddress::Details aDetails( eConv, aPos );
7366 const ScAddress aAdr( nCol, nRow, 0);
7367 aAdr.Format( aRefStr, nFlags, pDok, aDetails );
7368
7369 if( nParamCount >= 5 && sTabStr.Len() )
7370 {
7371 String aDoc;
7372 if (eConv == FormulaGrammar::CONV_OOO)
7373 {
7374 // Isolate Tab from 'Doc'#Tab
7375 xub_StrLen nPos = ScCompiler::GetDocTabPos( sTabStr);
7376 if (nPos != STRING_NOTFOUND)
7377 {
7378 if (sTabStr.GetChar(nPos+1) == '$')
7379 ++nPos; // also split 'Doc'#$Tab
7380 aDoc = sTabStr.Copy( 0, nPos+1);
7381 sTabStr.Erase( 0, nPos+1);
7382 }
7383 }
7384 /* TODO: yet unsupported external reference in CONV_XL_R1C1 syntax may
7385 * need some extra handling to isolate Tab from Doc. */
7386 if (sTabStr.GetChar(0) != '\'' || sTabStr.GetChar(sTabStr.Len()-1) != '\'')
7387 ScCompiler::CheckTabQuotes( sTabStr, eConv);
7388 if (aDoc.Len())
7389 sTabStr.Insert( aDoc, 0);
7390 sTabStr += static_cast<sal_Unicode>(eConv == FormulaGrammar::CONV_XL_R1C1 ? '!' : '.');
7391 sTabStr += aRefStr;
7392 PushString( sTabStr );
7393 }
7394 else
7395 PushString( aRefStr );
7396 }
7397
7398
lcl_CreateExternalRefSubroutine(const ScAddress & rPos,ScDocument * pDoc,const FormulaTokenRef & xExtRef)7399 FormulaSubroutineToken* lcl_CreateExternalRefSubroutine( const ScAddress& rPos,
7400 ScDocument* pDoc, const FormulaTokenRef& xExtRef )
7401 {
7402 // The exact usage (which cell range) of the external table can't be
7403 // detected during the store-to-file cycle, mark it as permanently
7404 // referenced so it gets stored even if not directly referenced anywhere.
7405 ScExternalRefManager* pRefMgr = pDoc->GetExternalRefManager();
7406 pRefMgr->setCacheTableReferencedPermanently(
7407 static_cast<const ScToken*>(xExtRef.get())->GetIndex(),
7408 static_cast<const ScToken*>(xExtRef.get())->GetString(), 1);
7409 ScTokenArray* pTokenArray = new ScTokenArray;
7410 pTokenArray->AddToken( *xExtRef);
7411 ScCompiler aComp( pDoc, rPos, *pTokenArray);
7412 aComp.CompileTokenArray();
7413 return new FormulaSubroutineToken( pTokenArray);
7414 }
7415
7416
ScOffset()7417 void ScInterpreter::ScOffset()
7418 {
7419 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScOffset" );
7420 sal_uInt8 nParamCount = GetByte();
7421 if ( MustHaveParamCount( nParamCount, 3, 5 ) )
7422 {
7423 long nColNew = -1, nRowNew = -1, nColPlus, nRowPlus;
7424 if (nParamCount == 5)
7425 nColNew = (long) ::rtl::math::approxFloor(GetDouble());
7426 if (nParamCount >= 4)
7427 nRowNew = (long) ::rtl::math::approxFloor(GetDoubleWithDefault( -1.0 ));
7428 nColPlus = (long) ::rtl::math::approxFloor(GetDouble());
7429 nRowPlus = (long) ::rtl::math::approxFloor(GetDouble());
7430 SCCOL nCol1;
7431 SCROW nRow1;
7432 SCTAB nTab1;
7433 SCCOL nCol2;
7434 SCROW nRow2;
7435 SCTAB nTab2;
7436 if (nColNew == 0 || nRowNew == 0)
7437 {
7438 PushIllegalArgument();
7439 return;
7440 }
7441 FormulaTokenRef xExtRef;
7442 switch (GetStackType())
7443 {
7444 case svExternalSingleRef:
7445 xExtRef = PopToken()->Clone();
7446 // fallthru
7447 case svSingleRef:
7448 {
7449 if (xExtRef)
7450 {
7451 ScSingleRefData& rData = static_cast<ScToken*>(xExtRef.get())->GetSingleRef();
7452 rData.CalcAbsIfRel( aPos);
7453 nCol1 = rData.nCol;
7454 nRow1 = rData.nRow;
7455 nTab1 = rData.nTab;
7456 }
7457 else
7458 PopSingleRef( nCol1, nRow1, nTab1);
7459 if (nParamCount == 3 || (nColNew < 0 && nRowNew < 0))
7460 {
7461 nCol1 = (SCCOL)((long) nCol1 + nColPlus);
7462 nRow1 = (SCROW)((long) nRow1 + nRowPlus);
7463 if (!ValidCol(nCol1) || !ValidRow(nRow1))
7464 PushIllegalArgument();
7465 else if (xExtRef)
7466 {
7467 ScSingleRefData& rData = static_cast<ScToken*>(xExtRef.get())->GetSingleRef();
7468 rData.nCol = nCol1;
7469 rData.nRow = nRow1;
7470 rData.nTab = nTab1;
7471 rData.CalcRelFromAbs( aPos);
7472 // Push a subroutine that resolves the external
7473 // reference as the next instruction.
7474 PushTempToken( lcl_CreateExternalRefSubroutine( aPos, pDok, xExtRef));
7475 }
7476 else
7477 PushSingleRef(nCol1, nRow1, nTab1);
7478 }
7479 else
7480 {
7481 if (nColNew < 0)
7482 nColNew = 1;
7483 if (nRowNew < 0)
7484 nRowNew = 1;
7485 nCol1 = (SCCOL)((long)nCol1+nColPlus); // ! nCol1 is modified
7486 nRow1 = (SCROW)((long)nRow1+nRowPlus);
7487 nCol2 = (SCCOL)((long)nCol1+nColNew-1);
7488 nRow2 = (SCROW)((long)nRow1+nRowNew-1);
7489 if (!ValidCol(nCol1) || !ValidRow(nRow1) ||
7490 !ValidCol(nCol2) || !ValidRow(nRow2))
7491 PushIllegalArgument();
7492 else if (xExtRef)
7493 {
7494 // Convert SingleRef to DoubleRef.
7495 xExtRef = new ScExternalDoubleRefToken(
7496 *static_cast<const ScExternalSingleRefToken*>(xExtRef.get()));
7497 ScComplexRefData& rData = static_cast<ScToken*>(xExtRef.get())->GetDoubleRef();
7498 rData.Ref1.nCol = nCol1;
7499 rData.Ref1.nRow = nRow1;
7500 rData.Ref1.nTab = nTab1;
7501 rData.Ref2.nCol = nCol2;
7502 rData.Ref2.nRow = nRow2;
7503 rData.Ref2.nTab = nTab1;
7504 rData.CalcRelFromAbs( aPos);
7505 // Push a subroutine that resolves the external
7506 // reference as the next instruction.
7507 PushTempToken( lcl_CreateExternalRefSubroutine( aPos, pDok, xExtRef));
7508 }
7509 else
7510 PushDoubleRef(nCol1, nRow1, nTab1, nCol2, nRow2, nTab1);
7511 }
7512 }
7513 break;
7514 case svExternalDoubleRef:
7515 xExtRef = PopToken()->Clone();
7516 // fallthru
7517 case svDoubleRef:
7518 {
7519 if (xExtRef)
7520 {
7521 ScComplexRefData& rData = static_cast<ScToken*>(xExtRef.get())->GetDoubleRef();
7522 rData.CalcAbsIfRel( aPos);
7523 nCol1 = rData.Ref1.nCol;
7524 nRow1 = rData.Ref1.nRow;
7525 nTab1 = rData.Ref1.nTab;
7526 nCol2 = rData.Ref2.nCol;
7527 nRow2 = rData.Ref2.nRow;
7528 nTab2 = rData.Ref2.nTab;
7529 }
7530 else
7531 PopDoubleRef(nCol1, nRow1, nTab1, nCol2, nRow2, nTab2);
7532 if (nColNew < 0)
7533 nColNew = nCol2 - nCol1 + 1;
7534 if (nRowNew < 0)
7535 nRowNew = nRow2 - nRow1 + 1;
7536 nCol1 = (SCCOL)((long)nCol1+nColPlus);
7537 nRow1 = (SCROW)((long)nRow1+nRowPlus);
7538 nCol2 = (SCCOL)((long)nCol1+nColNew-1);
7539 nRow2 = (SCROW)((long)nRow1+nRowNew-1);
7540 if (!ValidCol(nCol1) || !ValidRow(nRow1) ||
7541 !ValidCol(nCol2) || !ValidRow(nRow2) || nTab1 != nTab2)
7542 PushIllegalArgument();
7543 else if (xExtRef)
7544 {
7545 ScComplexRefData& rData = static_cast<ScToken*>(xExtRef.get())->GetDoubleRef();
7546 rData.Ref1.nCol = nCol1;
7547 rData.Ref1.nRow = nRow1;
7548 rData.Ref1.nTab = nTab1;
7549 rData.Ref2.nCol = nCol2;
7550 rData.Ref2.nRow = nRow2;
7551 rData.Ref2.nTab = nTab1;
7552 rData.CalcRelFromAbs( aPos);
7553 // Push a subroutine that resolves the external
7554 // reference as the next instruction.
7555 PushTempToken( lcl_CreateExternalRefSubroutine( aPos, pDok, xExtRef));
7556 }
7557 else
7558 PushDoubleRef(nCol1, nRow1, nTab1, nCol2, nRow2, nTab1);
7559 }
7560 break;
7561 default:
7562 PushIllegalParameter();
7563 }
7564 }
7565 }
7566
7567
ScIndex()7568 void ScInterpreter::ScIndex()
7569 {
7570 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScIndex" );
7571 sal_uInt8 nParamCount = GetByte();
7572 if ( MustHaveParamCount( nParamCount, 1, 4 ) )
7573 {
7574 long nArea;
7575 size_t nAreaCount;
7576 SCCOL nCol;
7577 SCROW nRow;
7578 if (nParamCount == 4)
7579 nArea = (long) ::rtl::math::approxFloor(GetDouble());
7580 else
7581 nArea = 1;
7582 if (nParamCount >= 3)
7583 nCol = (SCCOL) ::rtl::math::approxFloor(GetDouble());
7584 else
7585 nCol = 0;
7586 if (nParamCount >= 2)
7587 nRow = (SCROW) ::rtl::math::approxFloor(GetDouble());
7588 else
7589 nRow = 0;
7590 if (GetStackType() == svRefList)
7591 nAreaCount = (sp ? static_cast<ScToken*>(pStack[sp-1])->GetRefList()->size() : 0);
7592 else
7593 nAreaCount = 1; // one reference or array or whatever
7594 if (nAreaCount == 0 || (size_t)nArea > nAreaCount)
7595 {
7596 PushError( errNoRef);
7597 return;
7598 }
7599 else if (nArea < 1 || nCol < 0 || nRow < 0)
7600 {
7601 PushIllegalArgument();
7602 return;
7603 }
7604 switch (GetStackType())
7605 {
7606 case svMatrix:
7607 {
7608 if (nArea != 1)
7609 SetError(errIllegalArgument);
7610 sal_uInt16 nOldSp = sp;
7611 ScMatrixRef pMat = GetMatrix();
7612 if (pMat)
7613 {
7614 SCSIZE nC, nR;
7615 pMat->GetDimensions(nC, nR);
7616 // Access one element of a vector independent of col/row
7617 // orientation?
7618 bool bVector = ((nCol == 0 || nRow == 0) && (nC == 1 || nR == 1));
7619 SCSIZE nElement = ::std::max( static_cast<SCSIZE>(nCol),
7620 static_cast<SCSIZE>(nRow));
7621 if (nC == 0 || nR == 0 ||
7622 (!bVector && (static_cast<SCSIZE>(nCol) > nC ||
7623 static_cast<SCSIZE>(nRow) > nR)) ||
7624 (bVector && nElement > nC * nR))
7625 PushIllegalArgument();
7626 else if (nCol == 0 && nRow == 0)
7627 sp = nOldSp;
7628 else if (bVector)
7629 {
7630 --nElement;
7631 if (pMat->IsString( nElement))
7632 PushString( pMat->GetString( nElement));
7633 else
7634 PushDouble( pMat->GetDouble( nElement));
7635 }
7636 else if (nCol == 0)
7637 {
7638 ScMatrixRef pResMat = GetNewMat(nC, 1);
7639 if (pResMat)
7640 {
7641 SCSIZE nRowMinus1 = static_cast<SCSIZE>(nRow - 1);
7642 for (SCSIZE i = 0; i < nC; i++)
7643 if (!pMat->IsString(i, nRowMinus1))
7644 pResMat->PutDouble(pMat->GetDouble(i,
7645 nRowMinus1), i, 0);
7646 else
7647 pResMat->PutString(pMat->GetString(i,
7648 nRowMinus1), i, 0);
7649 PushMatrix(pResMat);
7650 }
7651 else
7652 PushIllegalArgument();
7653 }
7654 else if (nRow == 0)
7655 {
7656 ScMatrixRef pResMat = GetNewMat(1, nR);
7657 if (pResMat)
7658 {
7659 SCSIZE nColMinus1 = static_cast<SCSIZE>(nCol - 1);
7660 for (SCSIZE i = 0; i < nR; i++)
7661 if (!pMat->IsString(nColMinus1, i))
7662 pResMat->PutDouble(pMat->GetDouble(nColMinus1,
7663 i), i);
7664 else
7665 pResMat->PutString(pMat->GetString(nColMinus1,
7666 i), i);
7667 PushMatrix(pResMat);
7668 }
7669 else
7670 PushIllegalArgument();
7671 }
7672 else
7673 {
7674 if (!pMat->IsString( static_cast<SCSIZE>(nCol-1),
7675 static_cast<SCSIZE>(nRow-1)))
7676 PushDouble( pMat->GetDouble(
7677 static_cast<SCSIZE>(nCol-1),
7678 static_cast<SCSIZE>(nRow-1)));
7679 else
7680 PushString( pMat->GetString(
7681 static_cast<SCSIZE>(nCol-1),
7682 static_cast<SCSIZE>(nRow-1)));
7683 }
7684 }
7685 }
7686 break;
7687 case svSingleRef:
7688 {
7689 SCCOL nCol1 = 0;
7690 SCROW nRow1 = 0;
7691 SCTAB nTab1 = 0;
7692 PopSingleRef( nCol1, nRow1, nTab1);
7693 if (nCol > 1 || nRow > 1)
7694 PushIllegalArgument();
7695 else
7696 PushSingleRef( nCol1, nRow1, nTab1);
7697 }
7698 break;
7699 case svDoubleRef:
7700 case svRefList:
7701 {
7702 SCCOL nCol1 = 0;
7703 SCROW nRow1 = 0;
7704 SCTAB nTab1 = 0;
7705 SCCOL nCol2 = 0;
7706 SCROW nRow2 = 0;
7707 SCTAB nTab2 = 0;
7708 sal_Bool bRowArray = sal_False;
7709 if (GetStackType() == svRefList)
7710 {
7711 FormulaTokenRef xRef = PopToken();
7712 if (nGlobalError || !xRef)
7713 {
7714 PushIllegalParameter();
7715 return;
7716 }
7717 ScRange aRange( ScAddress::UNINITIALIZED);
7718 DoubleRefToRange( (*(static_cast<ScToken*>(xRef.get())->GetRefList()))[nArea-1], aRange);
7719 aRange.GetVars( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2);
7720 if ( nParamCount == 2 && nRow1 == nRow2 )
7721 bRowArray = sal_True;
7722 }
7723 else
7724 {
7725 PopDoubleRef( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2);
7726 if ( nParamCount == 2 && nRow1 == nRow2 )
7727 bRowArray = sal_True;
7728 }
7729 if ( nTab1 != nTab2 ||
7730 (nCol > 0 && nCol1+nCol-1 > nCol2) ||
7731 (nRow > 0 && nRow1+nRow-1 > nRow2 && !bRowArray ) ||
7732 ( nRow > nCol2 - nCol1 + 1 && bRowArray ))
7733 PushIllegalArgument();
7734 else if (nCol == 0 && nRow == 0)
7735 {
7736 if ( nCol1 == nCol2 && nRow1 == nRow2 )
7737 PushSingleRef( nCol1, nRow1, nTab1 );
7738 else
7739 PushDoubleRef( nCol1, nRow1, nTab1, nCol2, nRow2, nTab1 );
7740 }
7741 else if (nRow == 0)
7742 {
7743 if ( nRow1 == nRow2 )
7744 PushSingleRef( nCol1+nCol-1, nRow1, nTab1 );
7745 else
7746 PushDoubleRef( nCol1+nCol-1, nRow1, nTab1,
7747 nCol1+nCol-1, nRow2, nTab1 );
7748 }
7749 else if (nCol == 0)
7750 {
7751 if ( nCol1 == nCol2 )
7752 PushSingleRef( nCol1, nRow1+nRow-1, nTab1 );
7753 else if ( bRowArray )
7754 {
7755 nCol =(SCCOL) nRow;
7756 nRow = 1;
7757 PushSingleRef( nCol1+nCol-1, nRow1+nRow-1, nTab1);
7758 }
7759 else
7760 PushDoubleRef( nCol1, nRow1+nRow-1, nTab1,
7761 nCol2, nRow1+nRow-1, nTab1);
7762 }
7763 else
7764 PushSingleRef( nCol1+nCol-1, nRow1+nRow-1, nTab1);
7765 }
7766 break;
7767 default:
7768 PushIllegalParameter();
7769 }
7770 }
7771 }
7772
7773
ScMultiArea()7774 void ScInterpreter::ScMultiArea()
7775 {
7776 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScMultiArea" );
7777 // Legacy support, convert to RefList
7778 sal_uInt8 nParamCount = GetByte();
7779 if (MustHaveParamCountMin( nParamCount, 1))
7780 {
7781 while (!nGlobalError && nParamCount-- > 1)
7782 {
7783 ScUnionFunc();
7784 }
7785 }
7786 }
7787
7788
ScAreas()7789 void ScInterpreter::ScAreas()
7790 {
7791 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScAreas" );
7792 sal_uInt8 nParamCount = GetByte();
7793 if (MustHaveParamCount( nParamCount, 1))
7794 {
7795 size_t nCount = 0;
7796 switch (GetStackType())
7797 {
7798 case svSingleRef:
7799 {
7800 FormulaTokenRef xT = PopToken();
7801 ValidateRef( static_cast<ScToken*>(xT.get())->GetSingleRef());
7802 ++nCount;
7803 }
7804 break;
7805 case svDoubleRef:
7806 {
7807 FormulaTokenRef xT = PopToken();
7808 ValidateRef( static_cast<ScToken*>(xT.get())->GetDoubleRef());
7809 ++nCount;
7810 }
7811 break;
7812 case svRefList:
7813 {
7814 FormulaTokenRef xT = PopToken();
7815 ValidateRef( *(static_cast<ScToken*>(xT.get())->GetRefList()));
7816 nCount += static_cast<ScToken*>(xT.get())->GetRefList()->size();
7817 }
7818 break;
7819 default:
7820 SetError( errIllegalParameter);
7821 }
7822 PushDouble( double(nCount));
7823 }
7824 }
7825
7826
ScCurrency()7827 void ScInterpreter::ScCurrency()
7828 {
7829 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScCurrency" );
7830 sal_uInt8 nParamCount = GetByte();
7831 if ( MustHaveParamCount( nParamCount, 1, 2 ) )
7832 {
7833 String aStr;
7834 double fDec;
7835 if (nParamCount == 2)
7836 {
7837 fDec = ::rtl::math::approxFloor(GetDouble());
7838 if (fDec < -15.0 || fDec > 15.0)
7839 {
7840 PushIllegalArgument();
7841 return;
7842 }
7843 }
7844 else
7845 fDec = 2.0;
7846 double fVal = GetDouble();
7847 double fFac;
7848 if ( fDec != 0.0 )
7849 fFac = pow( (double)10, fDec );
7850 else
7851 fFac = 1.0;
7852 if (fVal < 0.0)
7853 fVal = ceil(fVal*fFac-0.5)/fFac;
7854 else
7855 fVal = floor(fVal*fFac+0.5)/fFac;
7856 Color* pColor = NULL;
7857 if ( fDec < 0.0 )
7858 fDec = 0.0;
7859 sal_uLong nIndex = pFormatter->GetStandardFormat(
7860 NUMBERFORMAT_CURRENCY,
7861 ScGlobal::eLnge);
7862 if ( (sal_uInt16) fDec != pFormatter->GetFormatPrecision( nIndex ) )
7863 {
7864 String sFormatString;
7865 pFormatter->GenerateFormat(sFormatString,
7866 nIndex,
7867 ScGlobal::eLnge,
7868 sal_True, // mit Tausenderpunkt
7869 sal_False, // nicht rot
7870 (sal_uInt16) fDec,// Nachkommastellen
7871 1); // 1 Vorkommanull
7872 if (!pFormatter->GetPreviewString(sFormatString,
7873 fVal,
7874 aStr,
7875 &pColor,
7876 ScGlobal::eLnge))
7877 SetError(errIllegalArgument);
7878 }
7879 else
7880 {
7881 pFormatter->GetOutputString(fVal, nIndex, aStr, &pColor);
7882 }
7883 PushString(aStr);
7884 }
7885 }
7886
7887
ScReplace()7888 void ScInterpreter::ScReplace()
7889 {
7890 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScReplace" );
7891 if ( MustHaveParamCount( GetByte(), 4 ) )
7892 {
7893 String aNewStr( GetString() );
7894 double fCount = ::rtl::math::approxFloor( GetDouble());
7895 double fPos = ::rtl::math::approxFloor( GetDouble());
7896 String aOldStr( GetString() );
7897 if (fPos < 1.0 || fPos > static_cast<double>(STRING_MAXLEN)
7898 || fCount < 0.0 || fCount > static_cast<double>(STRING_MAXLEN))
7899 PushIllegalArgument();
7900 else
7901 {
7902 xub_StrLen nCount = static_cast<xub_StrLen>(fCount);
7903 xub_StrLen nPos = static_cast<xub_StrLen>(fPos);
7904 xub_StrLen nLen = aOldStr.Len();
7905 if (nPos > nLen + 1)
7906 nPos = nLen + 1;
7907 if (nCount > nLen - nPos + 1)
7908 nCount = nLen - nPos + 1;
7909 aOldStr.Erase( nPos-1, nCount );
7910 if ( CheckStringResultLen( aOldStr, aNewStr ) )
7911 aOldStr.Insert( aNewStr, nPos-1 );
7912 PushString( aOldStr );
7913 }
7914 }
7915 }
7916
7917
ScFixed()7918 void ScInterpreter::ScFixed()
7919 {
7920 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScFixed" );
7921 sal_uInt8 nParamCount = GetByte();
7922 if ( MustHaveParamCount( nParamCount, 1, 3 ) )
7923 {
7924 String aStr;
7925 double fDec;
7926 sal_Bool bThousand;
7927 if (nParamCount == 3)
7928 bThousand = !GetBool(); // Param TRUE: keine Tausenderpunkte
7929 else
7930 bThousand = sal_True;
7931 if (nParamCount >= 2)
7932 {
7933 fDec = ::rtl::math::approxFloor(GetDoubleWithDefault( 2.0 ));
7934 if (fDec < -15.0 || fDec > 15.0)
7935 {
7936 PushIllegalArgument();
7937 return;
7938 }
7939 }
7940 else
7941 fDec = 2.0;
7942 double fVal = GetDouble();
7943 double fFac;
7944 if ( fDec != 0.0 )
7945 fFac = pow( (double)10, fDec );
7946 else
7947 fFac = 1.0;
7948 if (fVal < 0.0)
7949 fVal = ceil(fVal*fFac-0.5)/fFac;
7950 else
7951 fVal = floor(fVal*fFac+0.5)/fFac;
7952 Color* pColor = NULL;
7953 String sFormatString;
7954 if (fDec < 0.0)
7955 fDec = 0.0;
7956 sal_uLong nIndex = pFormatter->GetStandardFormat(
7957 NUMBERFORMAT_NUMBER,
7958 ScGlobal::eLnge);
7959 pFormatter->GenerateFormat(sFormatString,
7960 nIndex,
7961 ScGlobal::eLnge,
7962 bThousand, // mit Tausenderpunkt
7963 sal_False, // nicht rot
7964 (sal_uInt16) fDec,// Nachkommastellen
7965 1); // 1 Vorkommanull
7966 if (!pFormatter->GetPreviewString(sFormatString,
7967 fVal,
7968 aStr,
7969 &pColor,
7970 ScGlobal::eLnge))
7971 PushIllegalArgument();
7972 else
7973 PushString(aStr);
7974 }
7975 }
7976
7977
ScFind()7978 void ScInterpreter::ScFind()
7979 {
7980 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScFind" );
7981 sal_uInt8 nParamCount = GetByte();
7982 if ( MustHaveParamCount( nParamCount, 2, 3 ) )
7983 {
7984 double fAnz;
7985 if (nParamCount == 3)
7986 fAnz = GetDouble();
7987 else
7988 fAnz = 1.0;
7989 String sStr = GetString();
7990 if( fAnz < 1.0 || fAnz > (double) sStr.Len() )
7991 PushNoValue();
7992 else
7993 {
7994 xub_StrLen nPos = sStr.Search( GetString(), (xub_StrLen) fAnz - 1 );
7995 if (nPos == STRING_NOTFOUND)
7996 PushNoValue();
7997 else
7998 PushDouble((double)(nPos + 1));
7999 }
8000 }
8001 }
8002
8003
ScExact()8004 void ScInterpreter::ScExact()
8005 {
8006 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScExact" );
8007 nFuncFmtType = NUMBERFORMAT_LOGICAL;
8008 if ( MustHaveParamCount( GetByte(), 2 ) )
8009 {
8010 String s1( GetString() );
8011 String s2( GetString() );
8012 PushInt( s1 == s2 );
8013 }
8014 }
8015
8016
ScLeft()8017 void ScInterpreter::ScLeft()
8018 {
8019 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScLeft" );
8020 sal_uInt8 nParamCount = GetByte();
8021 if ( MustHaveParamCount( nParamCount, 1, 2 ) )
8022 {
8023 xub_StrLen n;
8024 if (nParamCount == 2)
8025 {
8026 double nVal = ::rtl::math::approxFloor(GetDouble());
8027 if ( nVal < 0.0 || nVal > STRING_MAXLEN )
8028 {
8029 PushIllegalArgument();
8030 return ;
8031 }
8032 else
8033 n = (xub_StrLen) nVal;
8034 }
8035 else
8036 n = 1;
8037 String aStr( GetString() );
8038 aStr.Erase( n );
8039 PushString( aStr );
8040 }
8041 }
8042
8043 typedef struct {
8044 UBlockCode from;
8045 UBlockCode to;
8046 } UBlockScript;
8047
8048 static UBlockScript scriptList[] = {
8049 {UBLOCK_HANGUL_JAMO, UBLOCK_HANGUL_JAMO},
8050 {UBLOCK_CJK_RADICALS_SUPPLEMENT, UBLOCK_HANGUL_SYLLABLES},
8051 {UBLOCK_CJK_COMPATIBILITY_IDEOGRAPHS,UBLOCK_CJK_RADICALS_SUPPLEMENT },
8052 {UBLOCK_IDEOGRAPHIC_DESCRIPTION_CHARACTERS,UBLOCK_CJK_COMPATIBILITY_IDEOGRAPHS},
8053 {UBLOCK_CJK_COMPATIBILITY_FORMS, UBLOCK_CJK_COMPATIBILITY_FORMS},
8054 {UBLOCK_HALFWIDTH_AND_FULLWIDTH_FORMS, UBLOCK_HALFWIDTH_AND_FULLWIDTH_FORMS},
8055 {UBLOCK_CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B, UBLOCK_CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT},
8056 {UBLOCK_CJK_STROKES, UBLOCK_CJK_STROKES}
8057 };
8058 #define scriptListCount sizeof (scriptList) / sizeof (UBlockScript)
lcl_getScriptClass(sal_uInt32 currentChar)8059 bool SAL_CALL lcl_getScriptClass(sal_uInt32 currentChar)
8060 {
8061 // for the locale of ja-JP, character U+0x005c and U+0x20ac should be ScriptType::Asian
8062 if( (currentChar == 0x005c || currentChar == 0x20ac) &&
8063 (MsLangId::getSystemLanguage() == LANGUAGE_JAPANESE) )
8064 return true;
8065 sal_uInt16 i;
8066 static sal_Int16 nRet = 0;
8067 UBlockCode block = (UBlockCode)ublock_getCode((sal_uInt32)currentChar);
8068 for ( i = 0; i < scriptListCount; i++) {
8069 if (block <= scriptList[i].to) break;
8070 }
8071 nRet = (i < scriptListCount && block >= scriptList[i].from);
8072 return nRet;
8073 }
IsDBCS(sal_Unicode ch)8074 bool IsDBCS(sal_Unicode ch)
8075 {
8076 return lcl_getScriptClass(ch);
8077 }
getLengthB(String & str)8078 sal_Int32 getLengthB(String &str)
8079 {
8080 xub_StrLen index = 0;
8081 sal_Int32 length = 0;
8082 if(0 == str.Len())
8083 return 0;
8084 while(index < str.Len()){
8085 if(IsDBCS(str.GetChar(index)))
8086 length += 2;
8087 else
8088 length++;
8089 index++;
8090 }
8091 return length;
8092 }
ScLenB()8093 void ScInterpreter::ScLenB()
8094 {
8095 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "luzhang", "ScInterpreter::ScLenB" );
8096 String aStr( GetString() );
8097 PushDouble( getLengthB(aStr) );
8098 }
lcl_RightB(String & aStr,sal_Int32 n)8099 void lcl_RightB(String &aStr, sal_Int32 n)
8100 {
8101 if( n < getLengthB(aStr) )
8102 {
8103 sal_Int32 index = aStr.Len();
8104 while(index-- >= 0)
8105 {
8106 if(0 == n)
8107 {
8108 aStr.Erase( 0, index + 1);
8109 break;
8110 }
8111 if(-1 == n)
8112 {
8113 aStr.Erase( 0, index + 2 );
8114 aStr.InsertAscii(" ", 0);
8115 break;
8116 }
8117 if(IsDBCS(aStr.GetChar(index)))
8118 n -= 2;
8119 else
8120 n--;
8121 }
8122 }
8123 }
ScRightB()8124 void ScInterpreter::ScRightB()
8125 {
8126 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "luzhang", "ScInterpreter::ScRightB" );
8127 sal_uInt8 nParamCount = GetByte();
8128 if ( MustHaveParamCount( nParamCount, 1, 2 ) )
8129 {
8130 sal_Int32 n;
8131 if (nParamCount == 2)
8132 {
8133 double nVal = ::rtl::math::approxFloor(GetDouble());
8134 if ( nVal < 0.0 || nVal > STRING_MAXLEN )
8135 {
8136 PushIllegalArgument();
8137 return ;
8138 }
8139 else
8140 n = (xub_StrLen) nVal;
8141 }
8142 else
8143 n = 1;
8144 String aStr( GetString() );
8145 lcl_RightB(aStr, n);
8146 PushString( aStr );
8147 }
8148 }
lcl_LeftB(String & aStr,sal_Int32 n)8149 void lcl_LeftB(String &aStr, sal_Int32 n)
8150 {
8151 if( n < getLengthB(aStr) )
8152 {
8153 sal_Int32 index = -1;
8154 while(index++ < aStr.Len())
8155 {
8156 if(0 == n)
8157 {
8158 aStr.Erase( index );
8159 break;
8160 }
8161 if(-1 == n)
8162 {
8163 aStr.Erase( index - 1 );
8164 aStr.InsertAscii(" ");
8165 break;
8166 }
8167 if(IsDBCS(aStr.GetChar(index)))
8168 n -= 2;
8169 else
8170 n--;
8171 }
8172 }
8173 }
ScLeftB()8174 void ScInterpreter::ScLeftB()
8175 {
8176 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "luzhang", "ScInterpreter::ScLeftB" );
8177 sal_uInt8 nParamCount = GetByte();
8178 if ( MustHaveParamCount( nParamCount, 1, 2 ) )
8179 {
8180 sal_Int32 n;
8181 if (nParamCount == 2)
8182 {
8183 double nVal = ::rtl::math::approxFloor(GetDouble());
8184 if ( nVal < 0.0 || nVal > STRING_MAXLEN )
8185 {
8186 PushIllegalArgument();
8187 return ;
8188 }
8189 else
8190 n = (xub_StrLen) nVal;
8191 }
8192 else
8193 n = 1;
8194 String aStr( GetString() );
8195 lcl_LeftB(aStr, n);
8196 PushString( aStr );
8197 }
8198 }
ScMidB()8199 void ScInterpreter::ScMidB()
8200 {
8201 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "luzhang", "ScInterpreter::ScMidB" );
8202 if ( MustHaveParamCount( GetByte(), 3 ) )
8203 {
8204 double fAnz = ::rtl::math::approxFloor(GetDouble());
8205 double fAnfang = ::rtl::math::approxFloor(GetDouble());
8206 String rStr( GetString() );
8207 if (fAnfang < 1.0 || fAnz < 0.0 || fAnfang > double(STRING_MAXLEN) || fAnz > double(STRING_MAXLEN))
8208 PushIllegalArgument();
8209 else
8210 {
8211
8212 lcl_LeftB(rStr, (xub_StrLen)fAnfang + (xub_StrLen)fAnz - 1);
8213 sal_Int32 nCnt = getLengthB(rStr) - (xub_StrLen)fAnfang + 1;
8214 lcl_RightB(rStr, nCnt>0 ? nCnt:0);
8215 PushString(rStr);
8216 }
8217 }
8218 }
8219
8220 // Convert 1-based byte position to 0-based character index.
8221 // If the byte position falls inside a DBCS character, returns the index
8222 // of that character (the caller must handle partial-character boundaries).
lcl_BytePosToCharIndex(const String & aStr,sal_Int32 nBytePos)8223 static xub_StrLen lcl_BytePosToCharIndex(const String& aStr, sal_Int32 nBytePos)
8224 {
8225 sal_Int32 nByteCount = 0;
8226 xub_StrLen nCharIndex = 0;
8227 while (nCharIndex < aStr.Len())
8228 {
8229 if (IsDBCS(aStr.GetChar(nCharIndex)))
8230 {
8231 if (nBytePos <= nByteCount + 2)
8232 return nCharIndex;
8233 nByteCount += 2;
8234 }
8235 else
8236 {
8237 if (nBytePos <= nByteCount + 1)
8238 return nCharIndex;
8239 nByteCount += 1;
8240 }
8241 ++nCharIndex;
8242 }
8243 return aStr.Len(); // beyond end
8244 }
8245
8246 // Convert 0-based character index to 1-based byte position.
lcl_CharIndexToBytePos(const String & aStr,xub_StrLen nCharIndex)8247 static sal_Int32 lcl_CharIndexToBytePos(const String& aStr, xub_StrLen nCharIndex)
8248 {
8249 sal_Int32 nBytePos = 0;
8250 for (xub_StrLen i = 0; i < nCharIndex && i < aStr.Len(); ++i)
8251 {
8252 if (IsDBCS(aStr.GetChar(i)))
8253 nBytePos += 2;
8254 else
8255 nBytePos += 1;
8256 }
8257 return nBytePos + 1; // 1-based
8258 }
8259
ScFindB()8260 void ScInterpreter::ScFindB()
8261 {
8262 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScFindB" );
8263 sal_uInt8 nParamCount = GetByte();
8264 if ( MustHaveParamCount( nParamCount, 2, 3 ) )
8265 {
8266 double fStart = 1.0;
8267 if (nParamCount == 3)
8268 {
8269 fStart = ::rtl::math::approxFloor(GetDouble());
8270 if ( fStart < 1.0 )
8271 {
8272 PushNoValue();
8273 return;
8274 }
8275 }
8276 String aWithinText = GetString();
8277 String aFindText = GetString();
8278
8279 sal_Int32 nByteLen = getLengthB(aWithinText);
8280 if ( fStart > static_cast<double>(nByteLen) )
8281 {
8282 PushNoValue();
8283 return;
8284 }
8285
8286 // Convert start byte position to character index
8287 xub_StrLen nStartChar = lcl_BytePosToCharIndex(aWithinText, (sal_Int32)fStart);
8288
8289 // Case-sensitive search
8290 xub_StrLen nPos = aWithinText.Search( aFindText, nStartChar );
8291 if (nPos == STRING_NOTFOUND)
8292 PushNoValue();
8293 else
8294 {
8295 // Convert character position to byte position
8296 sal_Int32 nBytePos = lcl_CharIndexToBytePos(aWithinText, nPos);
8297 PushDouble((double)nBytePos);
8298 }
8299 }
8300 }
8301
ScSearchB()8302 void ScInterpreter::ScSearchB()
8303 {
8304 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScSearchB" );
8305 sal_uInt8 nParamCount = GetByte();
8306 if ( MustHaveParamCount( nParamCount, 2, 3 ) )
8307 {
8308 double fStart = 1.0;
8309 if (nParamCount == 3)
8310 {
8311 fStart = ::rtl::math::approxFloor(GetDouble());
8312 if ( fStart < 1.0 )
8313 {
8314 PushNoValue();
8315 return;
8316 }
8317 }
8318 String aWithinText = GetString();
8319 String aFindText = GetString();
8320
8321 sal_Int32 nByteLen = getLengthB(aWithinText);
8322 if ( fStart > static_cast<double>(nByteLen) )
8323 {
8324 PushNoValue();
8325 return;
8326 }
8327
8328 // Convert start byte position to character index
8329 xub_StrLen nStartChar = lcl_BytePosToCharIndex(aWithinText, (sal_Int32)fStart);
8330 xub_StrLen nEndPos = aWithinText.Len();
8331 if ( nStartChar >= nEndPos )
8332 {
8333 PushNoValue();
8334 return;
8335 }
8336
8337 // Case-insensitive search using TextSearch (same as ScSearch)
8338 utl::SearchParam::SearchType eSearchType =
8339 (MayBeRegExp( aFindText, pDok ) ?
8340 utl::SearchParam::SRCH_REGEXP : utl::SearchParam::SRCH_NORMAL);
8341 utl::SearchParam sPar(aFindText, eSearchType, sal_False, sal_False, sal_False);
8342 utl::TextSearch sT( sPar, *ScGlobal::pCharClass );
8343 int nBool = sT.SearchFrwrd(aWithinText, &nStartChar, &nEndPos);
8344 if (!nBool)
8345 PushNoValue();
8346 else
8347 {
8348 // Convert character position to byte position
8349 sal_Int32 nBytePos = lcl_CharIndexToBytePos(aWithinText, nStartChar);
8350 PushDouble((double)nBytePos);
8351 }
8352 }
8353 }
8354
ScReplaceB()8355 void ScInterpreter::ScReplaceB()
8356 {
8357 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScReplaceB" );
8358 if ( MustHaveParamCount( GetByte(), 4 ) )
8359 {
8360 String aNewStr( GetString() );
8361 double fCount = ::rtl::math::approxFloor( GetDouble());
8362 double fPos = ::rtl::math::approxFloor( GetDouble());
8363 String aOldStr( GetString() );
8364
8365 if ( fPos < 1.0 || fPos > static_cast<double>(STRING_MAXLEN)
8366 || fCount < 0.0 || fCount > static_cast<double>(STRING_MAXLEN) )
8367 {
8368 PushIllegalArgument();
8369 return;
8370 }
8371
8372 sal_Int32 nByteLen = getLengthB(aOldStr);
8373 sal_Int32 nStartByte = (sal_Int32) fPos;
8374
8375 // Clamp start position to end of string + 1 (append at end)
8376 if (nStartByte > nByteLen + 1)
8377 nStartByte = nByteLen + 1;
8378
8379 // Convert start byte position to character index
8380 xub_StrLen nStartChar = lcl_BytePosToCharIndex(aOldStr, nStartByte);
8381
8382 // Determine how many bytes from start position to end of string
8383 sal_Int32 nBytesFromStart = nByteLen - lcl_CharIndexToBytePos(aOldStr, nStartChar) + 1;
8384
8385 // Clamp count to available bytes
8386 sal_Int32 nCountBytes = (sal_Int32) fCount;
8387 if (nCountBytes > nBytesFromStart)
8388 nCountBytes = nBytesFromStart;
8389
8390 // Convert byte count to character count by scanning forward
8391 xub_StrLen nCountChars = 0;
8392 sal_Int32 nByteCount = 0;
8393 xub_StrLen nCharIndex = nStartChar;
8394 while (nCharIndex < aOldStr.Len() && nByteCount < nCountBytes)
8395 {
8396 if (IsDBCS(aOldStr.GetChar(nCharIndex)))
8397 {
8398 if (nByteCount + 2 > nCountBytes)
8399 break; // would split a DBCS character
8400 nByteCount += 2;
8401 }
8402 else
8403 {
8404 nByteCount += 1;
8405 }
8406 ++nCountChars;
8407 ++nCharIndex;
8408 }
8409
8410 aOldStr.Erase( nStartChar, nCountChars );
8411 if ( CheckStringResultLen( aOldStr, aNewStr ) )
8412 aOldStr.Insert( aNewStr, nStartChar );
8413 PushString( aOldStr );
8414 }
8415 }
8416
ScRight()8417 void ScInterpreter::ScRight()
8418 {
8419 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScRight" );
8420 sal_uInt8 nParamCount = GetByte();
8421 if ( MustHaveParamCount( nParamCount, 1, 2 ) )
8422 {
8423 xub_StrLen n;
8424 if (nParamCount == 2)
8425 {
8426 double nVal = ::rtl::math::approxFloor(GetDouble());
8427 if ( nVal < 0.0 || nVal > STRING_MAXLEN )
8428 {
8429 PushIllegalArgument();
8430 return ;
8431 }
8432 else
8433 n = (xub_StrLen) nVal;
8434 }
8435 else
8436 n = 1;
8437 String aStr( GetString() );
8438 if( n < aStr.Len() )
8439 aStr.Erase( 0, aStr.Len() - n );
8440 PushString( aStr );
8441 }
8442 }
8443
8444
ScSearch()8445 void ScInterpreter::ScSearch()
8446 {
8447 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScSearch" );
8448 double fAnz;
8449 sal_uInt8 nParamCount = GetByte();
8450 if ( MustHaveParamCount( nParamCount, 2, 3 ) )
8451 {
8452 if (nParamCount == 3)
8453 {
8454 fAnz = ::rtl::math::approxFloor(GetDouble());
8455 if (fAnz > double(STRING_MAXLEN))
8456 {
8457 PushIllegalArgument();
8458 return;
8459 }
8460 }
8461 else
8462 fAnz = 1.0;
8463 String sStr = GetString();
8464 String SearchStr = GetString();
8465 xub_StrLen nPos = (xub_StrLen) fAnz - 1;
8466 xub_StrLen nEndPos = sStr.Len();
8467 if( nPos >= nEndPos )
8468 PushNoValue();
8469 else
8470 {
8471 utl::SearchParam::SearchType eSearchType =
8472 (MayBeRegExp( SearchStr, pDok ) ?
8473 utl::SearchParam::SRCH_REGEXP : utl::SearchParam::SRCH_NORMAL);
8474 utl::SearchParam sPar(SearchStr, eSearchType, sal_False, sal_False, sal_False);
8475 utl::TextSearch sT( sPar, *ScGlobal::pCharClass );
8476 int nBool = sT.SearchFrwrd(sStr, &nPos, &nEndPos);
8477 if (!nBool)
8478 PushNoValue();
8479 else
8480 PushDouble((double)(nPos) + 1);
8481 }
8482 }
8483 }
8484
8485
ScMid()8486 void ScInterpreter::ScMid()
8487 {
8488 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScMid" );
8489 if ( MustHaveParamCount( GetByte(), 3 ) )
8490 {
8491 double fAnz = ::rtl::math::approxFloor(GetDouble());
8492 double fAnfang = ::rtl::math::approxFloor(GetDouble());
8493 const String& rStr = GetString();
8494 if (fAnfang < 1.0 || fAnz < 0.0 || fAnfang > double(STRING_MAXLEN) || fAnz > double(STRING_MAXLEN))
8495 PushIllegalArgument();
8496 else
8497 PushString(rStr.Copy( (xub_StrLen) fAnfang - 1, (xub_StrLen) fAnz ));
8498 }
8499 }
8500
8501
ScText()8502 void ScInterpreter::ScText()
8503 {
8504 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScText" );
8505 if ( MustHaveParamCount( GetByte(), 2 ) )
8506 {
8507 String sFormatString = GetString();
8508 String aStr;
8509 bool bString = false;
8510 double fVal = 0.0;
8511 switch (GetStackType())
8512 {
8513 case svError:
8514 PopError();
8515 break;
8516 case svDouble:
8517 fVal = PopDouble();
8518 break;
8519 default:
8520 {
8521 FormulaTokenRef xTok( PopToken());
8522 if (!nGlobalError)
8523 {
8524 PushTempToken( xTok);
8525 // Temporarily override the ConvertStringToValue()
8526 // error for GetCellValue() / GetCellValueOrZero()
8527 sal_uInt16 nSErr = mnStringNoValueError;
8528 mnStringNoValueError = errNotNumericString;
8529 fVal = GetDouble();
8530 mnStringNoValueError = nSErr;
8531 if (nGlobalError == errNotNumericString)
8532 {
8533 // Not numeric.
8534 nGlobalError = 0;
8535 PushTempToken( xTok);
8536 aStr = GetString();
8537 bString = true;
8538 }
8539 }
8540 }
8541 }
8542 if (nGlobalError)
8543 PushError( nGlobalError);
8544 else
8545 {
8546 String aResult;
8547 Color* pColor = NULL;
8548 LanguageType eCellLang;
8549 const ScPatternAttr* pPattern = pDok->GetPattern(
8550 aPos.Col(), aPos.Row(), aPos.Tab() );
8551 if ( pPattern )
8552 eCellLang = ((const SvxLanguageItem&)
8553 pPattern->GetItem( ATTR_LANGUAGE_FORMAT )).GetValue();
8554 else
8555 eCellLang = ScGlobal::eLnge;
8556 if (bString)
8557 {
8558 if (!pFormatter->GetPreviewString( sFormatString, aStr,
8559 aResult, &pColor, eCellLang))
8560 PushIllegalArgument();
8561 else
8562 PushString( aResult);
8563 }
8564 else
8565 {
8566 if (!pFormatter->GetPreviewStringGuess( sFormatString, fVal,
8567 aResult, &pColor, eCellLang))
8568 PushIllegalArgument();
8569 else
8570 PushString( aResult);
8571 }
8572 }
8573 }
8574 }
8575
8576
ScSubstitute()8577 void ScInterpreter::ScSubstitute()
8578 {
8579 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScSubstitute" );
8580 sal_uInt8 nParamCount = GetByte();
8581 if ( MustHaveParamCount( nParamCount, 3, 4 ) )
8582 {
8583 xub_StrLen nAnz;
8584 if (nParamCount == 4)
8585 {
8586 double fAnz = ::rtl::math::approxFloor(GetDouble());
8587 if( fAnz < 1 || fAnz > STRING_MAXLEN )
8588 {
8589 PushIllegalArgument();
8590 return;
8591 }
8592 else
8593 nAnz = (xub_StrLen) fAnz;
8594 }
8595 else
8596 nAnz = 0;
8597 String sNewStr = GetString();
8598 String sOldStr = GetString();
8599 String sStr = GetString();
8600 xub_StrLen nPos = 0;
8601 xub_StrLen nCount = 0;
8602 xub_StrLen nNewLen = sNewStr.Len();
8603 xub_StrLen nOldLen = sOldStr.Len();
8604 while( sal_True )
8605 {
8606 nPos = sStr.Search( sOldStr, nPos );
8607 if (nPos != STRING_NOTFOUND)
8608 {
8609 nCount++;
8610 if( !nAnz || nCount == nAnz )
8611 {
8612 sStr.Erase(nPos,nOldLen);
8613 if ( CheckStringResultLen( sStr, sNewStr ) )
8614 {
8615 sStr.Insert(sNewStr,nPos);
8616 nPos = sal::static_int_cast<xub_StrLen>( nPos + nNewLen );
8617 }
8618 else
8619 break;
8620 }
8621 else
8622 nPos++;
8623 }
8624 else
8625 break;
8626 }
8627 PushString( sStr );
8628 }
8629 }
8630
8631
ScRept()8632 void ScInterpreter::ScRept()
8633 {
8634 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScRept" );
8635 if ( MustHaveParamCount( GetByte(), 2 ) )
8636 {
8637 double fAnz = ::rtl::math::approxFloor(GetDouble());
8638 String aStr( GetString() );
8639 if ( fAnz < 0.0 )
8640 PushIllegalArgument();
8641 else if ( fAnz * aStr.Len() > STRING_MAXLEN )
8642 {
8643 PushError( errStringOverflow );
8644 }
8645 else if ( fAnz == 0.0 )
8646 PushString( EMPTY_STRING );
8647 else
8648 {
8649 xub_StrLen n = (xub_StrLen) fAnz;
8650 const xub_StrLen nLen = aStr.Len();
8651 String aRes;
8652 const sal_Unicode* const pSrc = aStr.GetBuffer();
8653 sal_Unicode* pDst = aRes.AllocBuffer( n * nLen );
8654 while( n-- )
8655 {
8656 memcpy( pDst, pSrc, nLen * sizeof(sal_Unicode) );
8657 pDst += nLen;
8658 }
8659 PushString( aRes );
8660 }
8661 }
8662 }
8663
8664
ScConcat()8665 void ScInterpreter::ScConcat()
8666 {
8667 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScConcat" );
8668 sal_uInt8 nParamCount = GetByte();
8669 String aRes;
8670 while( nParamCount-- > 0)
8671 {
8672 const String& rStr = GetString();
8673 aRes.Insert( rStr, 0 );
8674 }
8675 PushString( aRes );
8676 }
8677
8678
8679 // ODF ERROR.TYPE numbers, as the Excel export writes these errors.
lcl_ErrorTypeODF(sal_uInt16 nErr)8680 static sal_uInt16 lcl_ErrorTypeODF( sal_uInt16 nErr )
8681 {
8682 switch ( nErr )
8683 {
8684 case errNoCode: return 1; // #NULL!
8685 case errDivisionByZero: return 2; // #DIV/0!
8686 case errNoValue:
8687 case errIllegalArgument:
8688 case errIllegalParameter:
8689 case errPairExpected:
8690 case errOperatorExpected:
8691 case errVariableExpected:
8692 case errParameterExpected:
8693 case errCircularReference: return 3; // #VALUE!
8694 case errNoRef: return 4; // #REF!
8695 case errNoName:
8696 case errNoAddin:
8697 case errNoMacro: return 5; // #NAME?
8698 case errIllegalFPOperation: return 6; // #NUM!
8699 }
8700 return 7; // #N/A
8701 }
8702
ScErrorType(bool bODF)8703 void ScInterpreter::ScErrorType( bool bODF )
8704 {
8705 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScErrorType" );
8706 if ( bODF && !MustHaveParamCount( GetByte(), 1 ) )
8707 return;
8708 sal_uInt16 nErr;
8709 sal_uInt16 nOldError = nGlobalError;
8710 nGlobalError = 0;
8711 switch ( GetStackType() )
8712 {
8713 case svRefList :
8714 {
8715 FormulaTokenRef x = PopToken();
8716 if (nGlobalError)
8717 nErr = nGlobalError;
8718 else
8719 {
8720 const ScRefList* pRefList = static_cast<ScToken*>(x.get())->GetRefList();
8721 size_t n = pRefList->size();
8722 if (!n)
8723 nErr = errNoRef;
8724 else if (n > 1)
8725 nErr = errNoValue;
8726 else
8727 {
8728 ScRange aRange;
8729 DoubleRefToRange( (*pRefList)[0], aRange);
8730 if (nGlobalError)
8731 nErr = nGlobalError;
8732 else
8733 {
8734 ScAddress aAdr;
8735 if ( DoubleRefToPosSingleRef( aRange, aAdr ) )
8736 nErr = pDok->GetErrCode( aAdr );
8737 else
8738 nErr = nGlobalError;
8739 }
8740 }
8741 }
8742 }
8743 break;
8744 case svDoubleRef :
8745 {
8746 ScRange aRange;
8747 PopDoubleRef( aRange );
8748 if ( nGlobalError )
8749 nErr = nGlobalError;
8750 else
8751 {
8752 ScAddress aAdr;
8753 if ( DoubleRefToPosSingleRef( aRange, aAdr ) )
8754 nErr = pDok->GetErrCode( aAdr );
8755 else
8756 nErr = nGlobalError;
8757 }
8758 }
8759 break;
8760 case svSingleRef :
8761 {
8762 ScAddress aAdr;
8763 PopSingleRef( aAdr );
8764 if ( nGlobalError )
8765 nErr = nGlobalError;
8766 else
8767 nErr = pDok->GetErrCode( aAdr );
8768 }
8769 break;
8770 default:
8771 PopError();
8772 nErr = nGlobalError;
8773 }
8774 if ( nErr && bODF )
8775 nErr = lcl_ErrorTypeODF( nErr );
8776 if ( nErr )
8777 {
8778 nGlobalError = 0;
8779 PushDouble( nErr );
8780 }
8781 else
8782 {
8783 nGlobalError = nOldError;
8784 PushNA();
8785 }
8786 }
8787
8788
MayBeRegExp(const String & rStr,const ScDocument * pDoc)8789 sal_Bool ScInterpreter::MayBeRegExp( const String& rStr, const ScDocument* pDoc )
8790 {
8791 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::MayBeRegExp" );
8792 if ( pDoc && !pDoc->GetDocOptions().IsFormulaRegexEnabled() )
8793 return sal_False;
8794 if ( !rStr.Len() || (rStr.Len() == 1 && rStr.GetChar(0) != '.') )
8795 return sal_False; // einzelnes Metazeichen kann keine RegExp sein
8796 static const sal_Unicode cre[] = { '.','*','+','?','[',']','^','$','\\','<','>','(',')','|', 0 };
8797 const sal_Unicode* p1 = rStr.GetBuffer();
8798 sal_Unicode c1;
8799 while ( ( c1 = *p1++ ) != 0 )
8800 {
8801 const sal_Unicode* p2 = cre;
8802 while ( *p2 )
8803 {
8804 if ( c1 == *p2++ )
8805 return sal_True;
8806 }
8807 }
8808 return sal_False;
8809 }
8810
lcl_LookupQuery(ScAddress & o_rResultPos,ScDocument * pDoc,const ScQueryParam & rParam,const ScQueryEntry & rEntry)8811 static bool lcl_LookupQuery( ScAddress & o_rResultPos, ScDocument * pDoc,
8812 const ScQueryParam & rParam, const ScQueryEntry & rEntry )
8813 {
8814 bool bFound = false;
8815 ScQueryCellIterator aCellIter( pDoc, rParam.nTab, rParam, sal_False);
8816 if (rEntry.eOp != SC_EQUAL)
8817 {
8818 // range lookup <= or >=
8819 SCCOL nCol;
8820 SCROW nRow;
8821 bFound = aCellIter.FindEqualOrSortedLastInRange( nCol, nRow);
8822 if (bFound)
8823 {
8824 o_rResultPos.SetCol( nCol);
8825 o_rResultPos.SetRow( nRow);
8826 }
8827 }
8828 else if (aCellIter.GetFirst())
8829 {
8830 // EQUAL
8831 bFound = true;
8832 o_rResultPos.SetCol( aCellIter.GetCol());
8833 o_rResultPos.SetRow( aCellIter.GetRow());
8834 }
8835 return bFound;
8836 }
8837
8838 #define erDEBUG_LOOKUPCACHE 0
8839 #if erDEBUG_LOOKUPCACHE
8840 #include <cstdio>
8841 using ::std::fprintf;
8842 using ::std::fflush;
8843 static struct LookupCacheDebugCounter
8844 {
8845 unsigned long nMiss;
8846 unsigned long nHit;
LookupCacheDebugCounterLookupCacheDebugCounter8847 LookupCacheDebugCounter() : nMiss(0), nHit(0) {}
~LookupCacheDebugCounterLookupCacheDebugCounter8848 ~LookupCacheDebugCounter()
8849 {
8850 fprintf( stderr, "\nmiss: %lu, hit: %lu, total: %lu, hit/miss: %lu, hit/total %lu%\n",
8851 nMiss, nHit, nHit+nMiss, (nMiss>0 ? nHit/nMiss : 0),
8852 ((nHit+nMiss)>0 ? (100*nHit)/(nHit+nMiss) : 0));
8853 fflush( stderr);
8854 }
8855 } aLookupCacheDebugCounter;
8856 #endif
8857
LookupQueryWithCache(ScAddress & o_rResultPos,const ScQueryParam & rParam) const8858 bool ScInterpreter::LookupQueryWithCache( ScAddress & o_rResultPos,
8859 const ScQueryParam & rParam ) const
8860 {
8861 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::LookupQueryWithCache" );
8862 bool bFound = false;
8863 const ScQueryEntry& rEntry = rParam.GetEntry(0);
8864 bool bColumnsMatch = (rParam.nCol1 == rEntry.nField);
8865 DBG_ASSERT( bColumnsMatch, "ScInterpreter::LookupQueryWithCache: columns don't match");
8866 if (!bColumnsMatch)
8867 bFound = lcl_LookupQuery( o_rResultPos, pDok, rParam, rEntry);
8868 else
8869 {
8870 ScRange aLookupRange( rParam.nCol1, rParam.nRow1, rParam.nTab,
8871 rParam.nCol2, rParam.nRow2, rParam.nTab);
8872 ScLookupCache& rCache = pDok->GetLookupCache( aLookupRange);
8873 ScLookupCache::QueryCriteria aCriteria( rEntry);
8874 ScLookupCache::Result eCacheResult = rCache.lookup( o_rResultPos,
8875 aCriteria, aPos);
8876 switch (eCacheResult)
8877 {
8878 case ScLookupCache::NOT_CACHED :
8879 case ScLookupCache::CRITERIA_DIFFERENT :
8880 #if erDEBUG_LOOKUPCACHE
8881 ++aLookupCacheDebugCounter.nMiss;
8882 #if erDEBUG_LOOKUPCACHE > 1
8883 fprintf( stderr, "miss %d,%d,%d\n", (int)aPos.Col(), (int)aPos.Row(), (int)aPos.Tab());
8884 #endif
8885 #endif
8886 bFound = lcl_LookupQuery( o_rResultPos, pDok, rParam, rEntry);
8887 if (eCacheResult == ScLookupCache::NOT_CACHED)
8888 rCache.insert( o_rResultPos, aCriteria, aPos, bFound);
8889 break;
8890 case ScLookupCache::FOUND :
8891 #if erDEBUG_LOOKUPCACHE
8892 ++aLookupCacheDebugCounter.nHit;
8893 #if erDEBUG_LOOKUPCACHE > 1
8894 fprintf( stderr, "hit %d,%d,%d\n", (int)aPos.Col(), (int)aPos.Row(), (int)aPos.Tab());
8895 #endif
8896 #endif
8897 bFound = true;
8898 break;
8899 case ScLookupCache::NOT_AVAILABLE :
8900 ; // nothing, bFound remains FALSE
8901 break;
8902 }
8903 }
8904 return bFound;
8905 }
8906