Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "formulaparser.hxx"
21 :
22 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 : #include <com/sun/star/sheet/ComplexReference.hpp>
24 : #include <com/sun/star/sheet/ExternalReference.hpp>
25 : #include <com/sun/star/sheet/FormulaToken.hpp>
26 : #include <com/sun/star/sheet/NameToken.hpp>
27 : #include <com/sun/star/sheet/ReferenceFlags.hpp>
28 : #include <com/sun/star/sheet/SingleReference.hpp>
29 : #include "oox/core/filterbase.hxx"
30 : #include "oox/token/properties.hxx"
31 : #include "addressconverter.hxx"
32 : #include "biffinputstream.hxx"
33 : #include "defnamesbuffer.hxx"
34 : #include "externallinkbuffer.hxx"
35 : #include "tablebuffer.hxx"
36 : #include "worksheethelper.hxx"
37 :
38 : namespace oox {
39 : namespace xls {
40 :
41 : // ============================================================================
42 :
43 : using namespace ::com::sun::star::sheet;
44 : using namespace ::com::sun::star::sheet::ReferenceFlags;
45 : using namespace ::com::sun::star::table;
46 : using namespace ::com::sun::star::uno;
47 :
48 :
49 : // ============================================================================
50 :
51 : namespace {
52 :
53 0 : sal_uInt16 lclReadFmlaSize( BiffInputStream& rStrm, BiffType eBiff, const sal_uInt16* pnFmlaSize )
54 : {
55 0 : return pnFmlaSize ? *pnFmlaSize : ((eBiff == BIFF2) ? rStrm.readuInt8() : rStrm.readuInt16());
56 : }
57 :
58 : } // namespace
59 :
60 : // formula finalizer ==========================================================
61 :
62 21 : FormulaFinalizer::FormulaFinalizer( const OpCodeProvider& rOpCodeProv ) :
63 : OpCodeProvider( rOpCodeProv ),
64 21 : ApiOpCodes( getOpCodes() )
65 : {
66 21 : maTokens.reserve( 0x2000 );
67 21 : }
68 :
69 47 : ApiTokenSequence FormulaFinalizer::finalizeTokenArray( const ApiTokenSequence& rTokens )
70 : {
71 47 : maTokens.clear();
72 47 : if( rTokens.hasElements() )
73 : {
74 47 : const ApiToken* pToken = rTokens.getConstArray();
75 47 : processTokens( pToken, pToken + rTokens.getLength() );
76 : }
77 47 : return ContainerHelper::vectorToSequence( maTokens );
78 : }
79 :
80 0 : const FunctionInfo* FormulaFinalizer::resolveBadFuncName( const OUString& ) const
81 : {
82 0 : return 0;
83 : }
84 :
85 0 : OUString FormulaFinalizer::resolveDefinedName( sal_Int32 ) const
86 : {
87 0 : return OUString();
88 : }
89 :
90 75 : const FunctionInfo* FormulaFinalizer::getFunctionInfo( ApiToken& orFuncToken )
91 : {
92 : // first, try to find a regular function info from token op-code
93 75 : if( const FunctionInfo* pRegFuncInfo = getFuncInfoFromApiToken( orFuncToken ) )
94 6 : return pRegFuncInfo;
95 :
96 : // try to recognize a function from an external library
97 69 : if( (orFuncToken.OpCode == OPCODE_BAD) && orFuncToken.Data.has< OUString >() )
98 : {
99 : // virtual call to resolveBadFuncName()
100 2 : if( const FunctionInfo* pLibFuncInfo = resolveBadFuncName( orFuncToken.Data.get< OUString >() ) )
101 : {
102 : // write function op-code to the OPCODE_BAD token
103 0 : orFuncToken.OpCode = pLibFuncInfo->mnApiOpCode;
104 : // if it is an external function, insert programmatic function name
105 0 : if( (orFuncToken.OpCode == OPCODE_EXTERNAL) && (!pLibFuncInfo->maExtProgName.isEmpty()) )
106 0 : orFuncToken.Data <<= pLibFuncInfo->maExtProgName;
107 : else
108 0 : orFuncToken.Data.clear(); // clear string from OPCODE_BAD
109 0 : return pLibFuncInfo;
110 : }
111 : }
112 :
113 : // no success - return null
114 69 : return 0;
115 : }
116 :
117 0 : const FunctionInfo* FormulaFinalizer::getExternCallInfo( ApiToken& orFuncToken, const ApiToken& rECToken )
118 : {
119 : // try to resolve the passed token to a supported sheet function
120 0 : if( const FunctionInfo* pFuncInfo = getFuncInfoFromApiToken( rECToken ) )
121 : {
122 0 : orFuncToken.OpCode = pFuncInfo->mnApiOpCode;
123 : // programmatic add-in function name
124 0 : if( (pFuncInfo->mnApiOpCode == OPCODE_EXTERNAL) && !pFuncInfo->maExtProgName.isEmpty() )
125 0 : orFuncToken.Data <<= pFuncInfo->maExtProgName;
126 : // name of unsupported function, convert to OPCODE_BAD to preserve the name
127 0 : else if( (pFuncInfo->mnApiOpCode == OPCODE_BAD) && !pFuncInfo->maOoxFuncName.isEmpty() )
128 0 : orFuncToken.Data <<= pFuncInfo->maOoxFuncName;
129 0 : return pFuncInfo;
130 : }
131 :
132 : // macro call or unknown function name, move data to function token
133 0 : if( (rECToken.OpCode == OPCODE_MACRO) || (rECToken.OpCode == OPCODE_BAD) )
134 0 : orFuncToken = rECToken;
135 :
136 : // defined name used as function call, convert to OPCODE_BAD to preserve the name
137 0 : if( (rECToken.OpCode == OPCODE_NAME) && rECToken.Data.has< sal_Int32 >() )
138 : {
139 0 : OUString aDefName = resolveDefinedName( rECToken.Data.get< sal_Int32 >() );
140 0 : if( !aDefName.isEmpty() )
141 : {
142 0 : orFuncToken.OpCode = OPCODE_BAD;
143 0 : orFuncToken.Data <<= aDefName;
144 0 : }
145 : }
146 :
147 0 : return 0;
148 : }
149 :
150 61 : void FormulaFinalizer::processTokens( const ApiToken* pToken, const ApiToken* pTokenEnd )
151 : {
152 197 : while( pToken < pTokenEnd )
153 : {
154 : // push the current token into the vector
155 75 : bool bValid = appendFinalToken( *pToken );
156 : // try to process a function
157 75 : if( const FunctionInfo* pFuncInfo = bValid ? getFunctionInfo( maTokens.back() ) : 0 )
158 6 : pToken = processParameters( *pFuncInfo, pToken + 1, pTokenEnd );
159 : // otherwise, go to next token
160 : else
161 69 : ++pToken;
162 : }
163 61 : }
164 :
165 6 : const ApiToken* FormulaFinalizer::processParameters(
166 : const FunctionInfo& rFuncInfo, const ApiToken* pToken, const ApiToken* pTokenEnd )
167 : {
168 : // remember position of the token containing the function op-code
169 6 : size_t nFuncNameIdx = maTokens.size() - 1;
170 :
171 : // process a function, if an OPCODE_OPEN token is following
172 : OSL_ENSURE( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_OPEN), "FormulaFinalizer::processParameters - OPCODE_OPEN expected" );
173 6 : if( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_OPEN) )
174 : {
175 : // append the OPCODE_OPEN token to the vector
176 6 : maTokens.append( OPCODE_OPEN );
177 :
178 : // store positions of OPCODE_OPEN, parameter separators, and OPCODE_CLOSE
179 6 : ParameterPosVector aParams;
180 6 : pToken = findParameters( aParams, pToken, pTokenEnd );
181 : OSL_ENSURE( aParams.size() >= 2, "FormulaFinalizer::processParameters - missing tokens" );
182 6 : size_t nParamCount = aParams.size() - 1;
183 :
184 6 : if( (nParamCount == 1) && isEmptyParameter( aParams[ 0 ] + 1, aParams[ 1 ] ) )
185 : {
186 : /* Empty pair of parentheses -> function call without parameters,
187 : process parameter, there might be spaces between parentheses. */
188 0 : processTokens( aParams[ 0 ] + 1, aParams[ 1 ] );
189 : }
190 : else
191 : {
192 6 : const FunctionInfo* pRealFuncInfo = &rFuncInfo;
193 6 : ParameterPosVector::const_iterator aPosIt = aParams.begin();
194 :
195 : /* Preprocess EXTERN.CALL functions. The actual function name is
196 : contained as reference to a defined name in the first (hidden)
197 : parameter. */
198 6 : if( rFuncInfo.mnBiffFuncId == BIFF_FUNC_EXTERNCALL )
199 : {
200 0 : ApiToken& rFuncToken = maTokens[ nFuncNameIdx ];
201 0 : rFuncToken.OpCode = OPCODE_NONAME;
202 :
203 : // try to initialize function token from first parameter
204 0 : if( const ApiToken* pECToken = getSingleToken( *aPosIt + 1, *(aPosIt + 1) ) )
205 0 : if( const FunctionInfo* pECFuncInfo = getExternCallInfo( rFuncToken, *pECToken ) )
206 0 : pRealFuncInfo = pECFuncInfo;
207 :
208 : /* On success (something has been inserted into rFuncToken),
209 : skip the first parameter. */
210 0 : if( rFuncToken.OpCode != OPCODE_NONAME )
211 : {
212 0 : --nParamCount;
213 0 : ++aPosIt;
214 : }
215 : }
216 :
217 : // process all parameters
218 6 : FunctionParamInfoIterator aParamInfoIt( *pRealFuncInfo );
219 6 : size_t nLastValidSize = maTokens.size();
220 6 : size_t nLastValidCount = 0;
221 20 : for( size_t nParam = 0; nParam < nParamCount; ++nParam, ++aPosIt, ++aParamInfoIt )
222 : {
223 : // add embedded Calc-only parameters
224 14 : if( aParamInfoIt.isCalcOnlyParam() )
225 : {
226 0 : appendCalcOnlyParameter( *pRealFuncInfo, nParam );
227 0 : while( aParamInfoIt.isCalcOnlyParam() ) ++aParamInfoIt;
228 : }
229 :
230 14 : const ApiToken* pParamBegin = *aPosIt + 1;
231 14 : const ApiToken* pParamEnd = *(aPosIt + 1);
232 14 : bool bIsEmpty = isEmptyParameter( pParamBegin, pParamEnd );
233 :
234 14 : if( !aParamInfoIt.isExcelOnlyParam() )
235 : {
236 : // handle empty parameters
237 14 : if( bIsEmpty )
238 : {
239 : // append leading space tokens from original token array
240 0 : while( (pParamBegin < pParamEnd) && (pParamBegin->OpCode == OPCODE_SPACES) )
241 0 : maTokens.push_back( *pParamBegin++ );
242 : // add default values for some empty parameters, or the OPCODE_MISSING token
243 0 : appendEmptyParameter( *pRealFuncInfo, nParam );
244 : // reset bIsEmpty flag, if something has been appended in appendEmptyParameter()
245 0 : bIsEmpty = maTokens.back().OpCode == OPCODE_MISSING;
246 : // skip OPCODE_MISSING token in the original token array
247 : OSL_ENSURE( (pParamBegin == pParamEnd) || (pParamBegin->OpCode == OPCODE_MISSING), "FormulaFinalizer::processParameters - OPCODE_MISSING expected" );
248 0 : if( pParamBegin < pParamEnd ) ++pParamBegin;
249 : // append trailing space tokens from original token array
250 0 : while( (pParamBegin < pParamEnd) && (pParamBegin->OpCode == OPCODE_SPACES) )
251 0 : maTokens.push_back( *pParamBegin++ );
252 : }
253 : else
254 : {
255 : // if parameter is not empty, process all tokens of the parameter
256 14 : processTokens( pParamBegin, pParamEnd );
257 : }
258 :
259 : // append parameter separator token
260 14 : maTokens.append( OPCODE_SEP );
261 : }
262 :
263 : /* #84453# Update size of new token sequence with valid parameters
264 : to be able to remove trailing optional empty parameters. */
265 14 : if( !bIsEmpty || (nParam < pRealFuncInfo->mnMinParamCount) )
266 : {
267 14 : nLastValidSize = maTokens.size();
268 14 : nLastValidCount = nParam + 1;
269 : }
270 : }
271 :
272 : // #84453# remove trailing optional empty parameters
273 6 : maTokens.resize( nLastValidSize );
274 :
275 : // add trailing Calc-only parameters
276 6 : if( aParamInfoIt.isCalcOnlyParam() )
277 0 : appendCalcOnlyParameter( *pRealFuncInfo, nLastValidCount );
278 :
279 : // add optional parameters that are required in Calc
280 6 : appendRequiredParameters( *pRealFuncInfo, nLastValidCount );
281 :
282 : // remove last parameter separator token
283 6 : if( maTokens.back().OpCode == OPCODE_SEP )
284 6 : maTokens.pop_back();
285 : }
286 :
287 : /* Append the OPCODE_CLOSE token to the vector, but only if there is
288 : no OPCODE_BAD token at the end, this token already contains the
289 : trailing closing parentheses. */
290 6 : if( (pTokenEnd - 1)->OpCode != OPCODE_BAD )
291 6 : maTokens.append( OPCODE_CLOSE );
292 : }
293 :
294 : /* Replace OPCODE_EXTERNAL with OPCODE_NONAME to get #NAME! error in cell,
295 : if no matching add-in function was found. */
296 6 : ApiToken& rFuncNameToken = maTokens[ nFuncNameIdx ];
297 6 : if( (rFuncNameToken.OpCode == OPCODE_EXTERNAL) && !rFuncNameToken.Data.hasValue() )
298 0 : rFuncNameToken.OpCode = OPCODE_NONAME;
299 :
300 6 : return pToken;
301 : }
302 :
303 16 : bool FormulaFinalizer::isEmptyParameter( const ApiToken* pToken, const ApiToken* pTokenEnd ) const
304 : {
305 16 : while( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_SPACES) ) ++pToken;
306 16 : if( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_MISSING) ) ++pToken;
307 16 : while( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_SPACES) ) ++pToken;
308 16 : return pToken == pTokenEnd;
309 : }
310 :
311 0 : const ApiToken* FormulaFinalizer::getSingleToken( const ApiToken* pToken, const ApiToken* pTokenEnd ) const
312 : {
313 0 : const ApiToken* pSingleToken = 0;
314 : // skip leading whitespace tokens
315 0 : while( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_SPACES) ) ++pToken;
316 : // remember first non-whitespace token
317 0 : if( pToken < pTokenEnd ) pSingleToken = pToken++;
318 : // skip trailing whitespace tokens
319 0 : while( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_SPACES) ) ++pToken;
320 : // return null, if other non-whitespace tokens follow
321 0 : return (pToken == pTokenEnd) ? pSingleToken : 0;
322 : }
323 :
324 0 : const ApiToken* FormulaFinalizer::skipParentheses( const ApiToken* pToken, const ApiToken* pTokenEnd ) const
325 : {
326 : // skip tokens between OPCODE_OPEN and OPCODE_CLOSE
327 : OSL_ENSURE( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_OPEN), "skipParentheses - OPCODE_OPEN expected" );
328 0 : ++pToken;
329 0 : while( (pToken < pTokenEnd) && (pToken->OpCode != OPCODE_CLOSE) )
330 : {
331 0 : if( pToken->OpCode == OPCODE_OPEN )
332 0 : pToken = skipParentheses( pToken, pTokenEnd );
333 : else
334 0 : ++pToken;
335 : }
336 : // skip the OPCODE_CLOSE token
337 : OSL_ENSURE( ((pToken < pTokenEnd) && (pToken->OpCode == OPCODE_CLOSE)) || ((pTokenEnd - 1)->OpCode == OPCODE_BAD), "skipParentheses - OPCODE_CLOSE expected" );
338 0 : return (pToken < pTokenEnd) ? (pToken + 1) : pTokenEnd;
339 : }
340 :
341 6 : const ApiToken* FormulaFinalizer::findParameters( ParameterPosVector& rParams,
342 : const ApiToken* pToken, const ApiToken* pTokenEnd ) const
343 : {
344 : // push position of OPCODE_OPEN
345 : OSL_ENSURE( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_OPEN), "FormulaFinalizer::findParameters - OPCODE_OPEN expected" );
346 6 : rParams.push_back( pToken++ );
347 :
348 : // find positions of parameter separators
349 36 : while( (pToken < pTokenEnd) && (pToken->OpCode != OPCODE_CLOSE) )
350 : {
351 24 : if( pToken->OpCode == OPCODE_OPEN )
352 0 : pToken = skipParentheses( pToken, pTokenEnd );
353 24 : else if( pToken->OpCode == OPCODE_SEP )
354 8 : rParams.push_back( pToken++ );
355 : else
356 16 : ++pToken;
357 : }
358 :
359 : // push position of OPCODE_CLOSE
360 : OSL_ENSURE( ((pToken < pTokenEnd) && (pToken->OpCode == OPCODE_CLOSE)) || ((pTokenEnd - 1)->OpCode == OPCODE_BAD), "FormulaFinalizer::findParameters - OPCODE_CLOSE expected" );
361 6 : rParams.push_back( pToken );
362 6 : return (pToken < pTokenEnd) ? (pToken + 1) : pTokenEnd;
363 : }
364 :
365 0 : void FormulaFinalizer::appendEmptyParameter( const FunctionInfo& rFuncInfo, size_t nParam )
366 : {
367 : // remember old size of the token array
368 0 : size_t nTokenArraySize = maTokens.size();
369 :
370 0 : switch( rFuncInfo.mnBiff12FuncId )
371 : {
372 : case BIFF_FUNC_IF:
373 0 : if( (nParam == 1) || (nParam == 2) )
374 0 : maTokens.append< double >( OPCODE_PUSH, 0.0 );
375 0 : break;
376 : default:;
377 : }
378 :
379 : // if no token has been added, append a OPCODE_MISSING token
380 0 : if( nTokenArraySize == maTokens.size() )
381 0 : maTokens.append( OPCODE_MISSING );
382 0 : }
383 :
384 0 : void FormulaFinalizer::appendCalcOnlyParameter( const FunctionInfo& rFuncInfo, size_t nParam )
385 : {
386 : (void)nParam; // prevent 'unused' warning
387 0 : switch( rFuncInfo.mnBiff12FuncId )
388 : {
389 : case BIFF_FUNC_FLOOR:
390 : case BIFF_FUNC_CEILING:
391 : OSL_ENSURE( nParam == 2, "FormulaFinalizer::appendCalcOnlyParameter - unexpected parameter index" );
392 0 : maTokens.append< double >( OPCODE_PUSH, 1.0 );
393 0 : maTokens.append( OPCODE_SEP );
394 0 : break;
395 : }
396 0 : }
397 :
398 6 : void FormulaFinalizer::appendRequiredParameters( const FunctionInfo& rFuncInfo, size_t nParamCount )
399 : {
400 6 : switch( rFuncInfo.mnBiff12FuncId )
401 : {
402 : case BIFF_FUNC_WEEKNUM:
403 0 : if( nParamCount == 1 )
404 : {
405 0 : maTokens.append< double >( OPCODE_PUSH, 1.0 );
406 0 : maTokens.append( OPCODE_SEP );
407 : }
408 0 : break;
409 : }
410 6 : }
411 :
412 75 : bool FormulaFinalizer::appendFinalToken( const ApiToken& rToken )
413 : {
414 : // replace OPCODE_MACRO without macro name with #NAME? error code
415 75 : bool bValid = (rToken.OpCode != OPCODE_MACRO) || rToken.Data.hasValue();
416 75 : if( bValid )
417 : {
418 75 : maTokens.push_back( rToken );
419 : }
420 : else
421 : {
422 0 : maTokens.append( OPCODE_ARRAY_OPEN );
423 0 : maTokens.append( OPCODE_PUSH, BiffHelper::calcDoubleFromError( BIFF_ERR_NAME ) );
424 0 : maTokens.append( OPCODE_ARRAY_CLOSE );
425 : }
426 75 : return bValid;
427 : }
428 :
429 : // parser implementation base =================================================
430 :
431 21 : class FormulaParserImpl : public FormulaFinalizer, public WorkbookHelper
432 : {
433 : public:
434 : explicit FormulaParserImpl( const FormulaParser& rParent );
435 :
436 : /** Converts an OOXML formula string. */
437 : virtual ApiTokenSequence importOoxFormula(
438 : const CellAddress& rBaseAddress,
439 : const OUString& rFormulaString );
440 :
441 : /** Imports and converts a BIFF12 token array from the passed stream. */
442 : virtual ApiTokenSequence importBiff12Formula(
443 : const CellAddress& rBaseAddress,
444 : FormulaType eType,
445 : SequenceInputStream& rStrm );
446 :
447 : /** Imports and converts a BIFF2-BIFF8 token array from the passed stream. */
448 : virtual ApiTokenSequence importBiffFormula(
449 : const CellAddress& rBaseAddress,
450 : FormulaType eType,
451 : BiffInputStream& rStrm, const sal_uInt16* pnFmlaSize );
452 :
453 : /** Tries to resolve the passed ref-id to an OLE target URL. */
454 : OUString resolveOleTarget( sal_Int32 nRefId, bool bUseRefSheets ) const;
455 :
456 : protected:
457 : typedef ::std::pair< sal_Int32, bool > WhiteSpace;
458 : typedef ::std::vector< WhiteSpace > WhiteSpaceVec;
459 :
460 : /** Initializes the formula parser before importing a formula. */
461 : void initializeImport( const CellAddress& rBaseAddress, FormulaType eType );
462 : /** Finalizes the internal token storage after import. */
463 : ApiTokenSequence finalizeImport();
464 :
465 : // token array ------------------------------------------------------------
466 :
467 : bool resetSpaces();
468 : static void appendSpaces( WhiteSpaceVec& orSpaces, sal_Int32 nCount, bool bLineFeed );
469 : void appendLeadingSpaces( sal_Int32 nCount, bool bLineFeed );
470 : void appendOpeningSpaces( sal_Int32 nCount, bool bLineFeed );
471 : void appendClosingSpaces( sal_Int32 nCount, bool bLineFeed );
472 :
473 : size_t getFormulaSize() const;
474 : Any& appendRawToken( sal_Int32 nOpCode );
475 : Any& insertRawToken( sal_Int32 nOpCode, size_t nIndexFromEnd );
476 : size_t appendWhiteSpaceTokens( const WhiteSpaceVec* pSpaces );
477 : size_t insertWhiteSpaceTokens( const WhiteSpaceVec* pSpaces, size_t nIndexFromEnd );
478 :
479 : size_t getOperandSize( size_t nOpCountFromEnd, size_t nOpIndex ) const;
480 : void pushOperandSize( size_t nSize );
481 : size_t popOperandSize();
482 :
483 : ApiToken& getOperandToken( size_t nOpCountFromEnd, size_t nOpIndex, size_t nTokenIndex );
484 : void removeOperand( size_t nOpCountFromEnd, size_t nOpIndex );
485 :
486 : bool pushOperandToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces = 0 );
487 : bool pushAnyOperandToken( const Any& rAny, sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces = 0 );
488 : template< typename Type >
489 : bool pushValueOperandToken( const Type& rValue, sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces = 0 );
490 : template< typename Type >
491 0 : inline bool pushValueOperandToken( const Type& rValue, const WhiteSpaceVec* pSpaces = 0 )
492 0 : { return pushValueOperandToken( rValue, OPCODE_PUSH, pSpaces ); }
493 : bool pushParenthesesOperandToken( const WhiteSpaceVec* pOpeningSpaces = 0, const WhiteSpaceVec* pClosingSpaces = 0 );
494 : bool pushUnaryPreOperatorToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces = 0 );
495 : bool pushUnaryPostOperatorToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces = 0 );
496 : bool pushBinaryOperatorToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces = 0 );
497 : bool pushParenthesesOperatorToken( const WhiteSpaceVec* pOpeningSpaces = 0, const WhiteSpaceVec* pClosingSpaces = 0 );
498 : bool pushFunctionOperatorToken( sal_Int32 nOpCode, size_t nParamCount, const WhiteSpaceVec* pLeadingSpaces = 0, const WhiteSpaceVec* pClosingSpaces = 0 );
499 : bool pushFunctionOperatorToken( const FunctionInfo& rFuncInfo, size_t nParamCount, const WhiteSpaceVec* pLeadingSpaces = 0, const WhiteSpaceVec* pClosingSpaces = 0 );
500 :
501 : bool pushOperand( sal_Int32 nOpCode );
502 : bool pushAnyOperand( const Any& rAny, sal_Int32 nOpCode );
503 : template< typename Type >
504 : bool pushValueOperand( const Type& rValue, sal_Int32 nOpCode );
505 : template< typename Type >
506 0 : inline bool pushValueOperand( const Type& rValue )
507 0 : { return pushValueOperand( rValue, OPCODE_PUSH ); }
508 : bool pushBoolOperand( bool bValue );
509 : bool pushErrorOperand( double fEncodedError );
510 : bool pushBiffBoolOperand( sal_uInt8 nValue );
511 : bool pushBiffErrorOperand( sal_uInt8 nErrorCode );
512 : bool pushReferenceOperand( const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset );
513 : bool pushReferenceOperand( const BinComplexRef2d& rRef, bool bDeleted, bool bRelativeAsOffset );
514 : template< typename Type >
515 : bool pushReferenceOperand( const LinkSheetRange& rSheetRange, const Type& rApiRef );
516 : bool pushReferenceOperand( const LinkSheetRange& rSheetRange, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset );
517 : bool pushReferenceOperand( const LinkSheetRange& rSheetRange, const BinComplexRef2d& rRef, bool bDeleted, bool bRelativeAsOffset );
518 : bool pushNlrOperand( const BinSingleRef2d& rRef );
519 : bool pushEmbeddedRefOperand( const DefinedNameBase& rName, bool bPushBadToken );
520 : bool pushDefinedNameOperand( const DefinedNameRef& rxDefName );
521 : bool pushExternalFuncOperand( const FunctionInfo& rFuncInfo );
522 : bool pushDdeLinkOperand( const OUString& rDdeServer, const OUString& rDdeTopic, const OUString& rDdeItem );
523 : bool pushExternalNameOperand( const ExternalNameRef& rxExtName, const ExternalLink& rExtLink );
524 : bool pushSpecialTokenOperand( const BinAddress& rBaseAddr, bool bTable );
525 :
526 : bool pushUnaryPreOperator( sal_Int32 nOpCode );
527 : bool pushUnaryPostOperator( sal_Int32 nOpCode );
528 : bool pushBinaryOperator( sal_Int32 nOpCode );
529 : bool pushParenthesesOperator();
530 : bool pushFunctionOperator( sal_Int32 nOpCode, size_t nParamCount );
531 : bool pushFunctionOperator( const FunctionInfo& rFuncInfo, size_t nParamCount );
532 :
533 : private:
534 : // reference conversion ---------------------------------------------------
535 :
536 : void initReference2d( SingleReference& orApiRef ) const;
537 : void initReference3d( SingleReference& orApiRef, sal_Int32 nSheet, bool bSameSheet ) const;
538 : void convertColRow( SingleReference& orApiRef, const BinSingleRef2d& rRef, bool bRelativeAsOffset ) const;
539 : void convertReference( SingleReference& orApiRef, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset ) const;
540 : void convertReference( ComplexReference& orApiRef, const BinSingleRef2d& rRef1, const BinSingleRef2d& rRef2, bool bDeleted, bool bRelativeAsOffset ) const;
541 : void convertReference2d( SingleReference& orApiRef, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset ) const;
542 : void convertReference2d( ComplexReference& orApiRef, const BinSingleRef2d& rRef1, const BinSingleRef2d& rRef2, bool bDeleted, bool bRelativeAsOffset ) const;
543 : void convertReference3d( SingleReference& orApiRef, sal_Int32 nSheet, bool bSameSheet, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset ) const;
544 : void convertReference3d( ComplexReference& orApiRef, const LinkSheetRange& rSheetRange, const BinSingleRef2d& rRef1, const BinSingleRef2d& rRef2, bool bDeleted, bool bRelativeAsOffset ) const;
545 :
546 : private:
547 : // finalize token sequence ------------------------------------------------
548 :
549 : virtual const FunctionInfo* resolveBadFuncName( const OUString& rTokenData ) const;
550 : virtual OUString resolveDefinedName( sal_Int32 nTokenIndex ) const;
551 :
552 : protected:
553 : const sal_Int32 mnMaxApiCol; /// Maximum column index in own document.
554 : const sal_Int32 mnMaxApiRow; /// Maximum row index in own document.
555 : const sal_Int32 mnMaxXlsCol; /// Maximum column index in imported document.
556 : const sal_Int32 mnMaxXlsRow; /// Maximum row index in imported document.
557 :
558 : CellAddress maBaseAddr; /// Base address for relative references.
559 : bool mbRelativeAsOffset; /// True = relative row/column index is (signed) offset, false = explicit index.
560 : bool mb2dRefsAs3dRefs; /// True = convert all 2D references to 3D references in sheet specified by base address.
561 : bool mbSpecialTokens; /// True = special handling for tExp and tTbl tokens, false = exit with error.
562 : bool mbAllowNulChars; /// True = keep NUL characters in string tokens.
563 :
564 : private:
565 : typedef ::std::vector< size_t > SizeTypeVector;
566 :
567 : ApiTokenVector maTokenStorage; /// Raw unordered token storage.
568 : SizeTypeVector maTokenIndexes; /// Indexes into maTokenStorage.
569 : SizeTypeVector maOperandSizeStack; /// Stack with token sizes per operand.
570 : WhiteSpaceVec maLeadingSpaces; /// List of whitespaces before next token.
571 : WhiteSpaceVec maOpeningSpaces; /// List of whitespaces before opening parenthesis.
572 : WhiteSpaceVec maClosingSpaces; /// List of whitespaces before closing parenthesis.
573 : };
574 :
575 : // ----------------------------------------------------------------------------
576 :
577 21 : FormulaParserImpl::FormulaParserImpl( const FormulaParser& rParent ) :
578 : FormulaFinalizer( rParent ),
579 : WorkbookHelper( rParent ),
580 21 : mnMaxApiCol( rParent.getAddressConverter().getMaxApiAddress().Column ),
581 21 : mnMaxApiRow( rParent.getAddressConverter().getMaxApiAddress().Row ),
582 21 : mnMaxXlsCol( rParent.getAddressConverter().getMaxXlsAddress().Column ),
583 21 : mnMaxXlsRow( rParent.getAddressConverter().getMaxXlsAddress().Row ),
584 : mbRelativeAsOffset( false ),
585 : mb2dRefsAs3dRefs( false ),
586 : mbSpecialTokens( false ),
587 105 : mbAllowNulChars( false )
588 : {
589 : // reserve enough space to make resize(), push_back() etc. cheap
590 21 : maTokenStorage.reserve( 0x2000 );
591 21 : maTokenIndexes.reserve( 0x2000 );
592 21 : maOperandSizeStack.reserve( 256 );
593 21 : maLeadingSpaces.reserve( 256 );
594 21 : maOpeningSpaces.reserve( 256 );
595 21 : maClosingSpaces.reserve( 256 );
596 21 : }
597 :
598 0 : ApiTokenSequence FormulaParserImpl::importOoxFormula( const CellAddress&, const OUString& )
599 : {
600 : OSL_FAIL( "FormulaParserImpl::importOoxFormula - not implemented" );
601 0 : return ApiTokenSequence();
602 : }
603 :
604 0 : ApiTokenSequence FormulaParserImpl::importBiff12Formula( const CellAddress&, FormulaType, SequenceInputStream& )
605 : {
606 : OSL_FAIL( "FormulaParserImpl::importBiff12Formula - not implemented" );
607 0 : return ApiTokenSequence();
608 : }
609 :
610 0 : ApiTokenSequence FormulaParserImpl::importBiffFormula( const CellAddress&, FormulaType, BiffInputStream&, const sal_uInt16* )
611 : {
612 : OSL_FAIL( "FormulaParserImpl::importBiffFormula - not implemented" );
613 0 : return ApiTokenSequence();
614 : }
615 :
616 0 : OUString FormulaParserImpl::resolveOleTarget( sal_Int32 nRefId, bool bUseRefSheets ) const
617 : {
618 0 : const ExternalLink* pExtLink = getExternalLinks().getExternalLink( nRefId, bUseRefSheets ).get();
619 : OSL_ENSURE( pExtLink && (pExtLink->getLinkType() == LINKTYPE_OLE), "FormulaParserImpl::resolveOleTarget - missing or wrong link" );
620 0 : if( pExtLink && (pExtLink->getLinkType() == LINKTYPE_OLE) )
621 0 : return getBaseFilter().getAbsoluteUrl( pExtLink->getTargetUrl() );
622 0 : return OUString();
623 : }
624 :
625 0 : void FormulaParserImpl::initializeImport( const CellAddress& rBaseAddr, FormulaType eType )
626 : {
627 0 : maBaseAddr = rBaseAddr;
628 0 : mbRelativeAsOffset = mb2dRefsAs3dRefs = mbSpecialTokens = mbAllowNulChars = false;
629 0 : switch( eType )
630 : {
631 : case FORMULATYPE_CELL:
632 0 : mbSpecialTokens = true;
633 0 : break;
634 : case FORMULATYPE_ARRAY:
635 0 : break;
636 : case FORMULATYPE_SHAREDFORMULA:
637 0 : mbRelativeAsOffset = true;
638 0 : break;
639 : case FORMULATYPE_CONDFORMAT:
640 0 : mbRelativeAsOffset = true;
641 0 : break;
642 : case FORMULATYPE_VALIDATION:
643 0 : mbRelativeAsOffset = true;
644 : // enable NUL characters in BIFF import, string list is single tStr token with NUL separators
645 0 : mbAllowNulChars = getFilterType() == FILTER_BIFF;
646 0 : break;
647 : case FORMULATYPE_DEFINEDNAME:
648 0 : mbRelativeAsOffset = true;
649 : // BIFF2-BIFF4: convert 2D referebces to absolute 3D references
650 0 : mb2dRefsAs3dRefs = (getFilterType() == FILTER_BIFF) && (getBiff() <= BIFF4);
651 0 : break;
652 : }
653 :
654 0 : maTokenStorage.clear();
655 0 : maTokenIndexes.clear();
656 0 : maOperandSizeStack.clear();
657 0 : }
658 :
659 0 : ApiTokenSequence FormulaParserImpl::finalizeImport()
660 : {
661 0 : ApiTokenSequence aTokens( static_cast< sal_Int32 >( maTokenIndexes.size() ) );
662 0 : if( aTokens.hasElements() )
663 : {
664 0 : ApiToken* pToken = aTokens.getArray();
665 0 : for( SizeTypeVector::const_iterator aIt = maTokenIndexes.begin(), aEnd = maTokenIndexes.end(); aIt != aEnd; ++aIt, ++pToken )
666 0 : *pToken = maTokenStorage[ *aIt ];
667 : }
668 0 : return finalizeTokenArray( aTokens );
669 : }
670 :
671 : // token array ----------------------------------------------------------------
672 :
673 0 : bool FormulaParserImpl::resetSpaces()
674 : {
675 0 : maLeadingSpaces.clear();
676 0 : maOpeningSpaces.clear();
677 0 : maClosingSpaces.clear();
678 0 : return true;
679 : }
680 :
681 0 : void FormulaParserImpl::appendSpaces( WhiteSpaceVec& orSpaces, sal_Int32 nCount, bool bLineFeed )
682 : {
683 : OSL_ENSURE( nCount >= 0, "FormulaParserImpl::appendSpaces - negative count" );
684 0 : if( nCount > 0 )
685 0 : orSpaces.push_back( WhiteSpace( nCount, bLineFeed ) );
686 0 : }
687 :
688 0 : void FormulaParserImpl::appendLeadingSpaces( sal_Int32 nCount, bool bLineFeed )
689 : {
690 0 : appendSpaces( maLeadingSpaces, nCount, bLineFeed );
691 0 : }
692 :
693 0 : void FormulaParserImpl::appendOpeningSpaces( sal_Int32 nCount, bool bLineFeed )
694 : {
695 0 : appendSpaces( maOpeningSpaces, nCount, bLineFeed );
696 0 : }
697 :
698 0 : void FormulaParserImpl::appendClosingSpaces( sal_Int32 nCount, bool bLineFeed )
699 : {
700 0 : appendSpaces( maClosingSpaces, nCount, bLineFeed );
701 0 : }
702 :
703 0 : size_t FormulaParserImpl::getFormulaSize() const
704 : {
705 0 : return maTokenIndexes.size();
706 : }
707 :
708 0 : Any& FormulaParserImpl::appendRawToken( sal_Int32 nOpCode )
709 : {
710 0 : maTokenIndexes.push_back( maTokenStorage.size() );
711 0 : return maTokenStorage.append( nOpCode );
712 : }
713 :
714 0 : Any& FormulaParserImpl::insertRawToken( sal_Int32 nOpCode, size_t nIndexFromEnd )
715 : {
716 0 : maTokenIndexes.insert( maTokenIndexes.end() - nIndexFromEnd, maTokenStorage.size() );
717 0 : return maTokenStorage.append( nOpCode );
718 : }
719 :
720 0 : size_t FormulaParserImpl::appendWhiteSpaceTokens( const WhiteSpaceVec* pSpaces )
721 : {
722 0 : if( pSpaces && !pSpaces->empty() )
723 0 : for( WhiteSpaceVec::const_iterator aIt = pSpaces->begin(), aEnd = pSpaces->end(); aIt != aEnd; ++aIt )
724 0 : appendRawToken( OPCODE_SPACES ) <<= aIt->first;
725 0 : return pSpaces ? pSpaces->size() : 0;
726 : }
727 :
728 0 : size_t FormulaParserImpl::insertWhiteSpaceTokens( const WhiteSpaceVec* pSpaces, size_t nIndexFromEnd )
729 : {
730 0 : if( pSpaces && !pSpaces->empty() )
731 0 : for( WhiteSpaceVec::const_iterator aIt = pSpaces->begin(), aEnd = pSpaces->end(); aIt != aEnd; ++aIt )
732 0 : insertRawToken( OPCODE_SPACES, nIndexFromEnd ) <<= aIt->first;
733 0 : return pSpaces ? pSpaces->size() : 0;
734 : }
735 :
736 0 : size_t FormulaParserImpl::getOperandSize( size_t nOpCountFromEnd, size_t nOpIndex ) const
737 : {
738 : OSL_ENSURE( (nOpIndex < nOpCountFromEnd) && (nOpCountFromEnd <= maOperandSizeStack.size()),
739 : "FormulaParserImpl::getOperandSize - invalid parameters" );
740 0 : return maOperandSizeStack[ maOperandSizeStack.size() - nOpCountFromEnd + nOpIndex ];
741 : }
742 :
743 0 : void FormulaParserImpl::pushOperandSize( size_t nSize )
744 : {
745 0 : maOperandSizeStack.push_back( nSize );
746 0 : }
747 :
748 0 : size_t FormulaParserImpl::popOperandSize()
749 : {
750 : OSL_ENSURE( !maOperandSizeStack.empty(), "FormulaParserImpl::popOperandSize - invalid call" );
751 0 : size_t nOpSize = maOperandSizeStack.back();
752 0 : maOperandSizeStack.pop_back();
753 0 : return nOpSize;
754 : }
755 :
756 0 : ApiToken& FormulaParserImpl::getOperandToken( size_t nOpCountFromEnd, size_t nOpIndex, size_t nTokenIndex )
757 : {
758 : OSL_ENSURE( getOperandSize( nOpCountFromEnd, nOpIndex ) > nTokenIndex,
759 : "FormulaParserImpl::getOperandToken - invalid parameters" );
760 0 : SizeTypeVector::const_iterator aIndexIt = maTokenIndexes.end();
761 0 : for( SizeTypeVector::const_iterator aEnd = maOperandSizeStack.end(), aIt = aEnd - nOpCountFromEnd + nOpIndex; aIt != aEnd; ++aIt )
762 0 : aIndexIt -= *aIt;
763 0 : return maTokenStorage[ *(aIndexIt + nTokenIndex) ];
764 : }
765 :
766 0 : bool FormulaParserImpl::pushOperandToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces )
767 : {
768 0 : size_t nSpacesSize = appendWhiteSpaceTokens( pSpaces );
769 0 : appendRawToken( nOpCode );
770 0 : pushOperandSize( nSpacesSize + 1 );
771 0 : return true;
772 : }
773 :
774 0 : bool FormulaParserImpl::pushAnyOperandToken( const Any& rAny, sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces )
775 : {
776 0 : size_t nSpacesSize = appendWhiteSpaceTokens( pSpaces );
777 0 : appendRawToken( nOpCode ) = rAny;
778 0 : pushOperandSize( nSpacesSize + 1 );
779 0 : return true;
780 : }
781 :
782 : template< typename Type >
783 0 : bool FormulaParserImpl::pushValueOperandToken( const Type& rValue, sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces )
784 : {
785 0 : size_t nSpacesSize = appendWhiteSpaceTokens( pSpaces );
786 0 : appendRawToken( nOpCode ) <<= rValue;
787 0 : pushOperandSize( nSpacesSize + 1 );
788 0 : return true;
789 : }
790 :
791 0 : bool FormulaParserImpl::pushParenthesesOperandToken( const WhiteSpaceVec* pOpeningSpaces, const WhiteSpaceVec* pClosingSpaces )
792 : {
793 0 : size_t nSpacesSize = appendWhiteSpaceTokens( pOpeningSpaces );
794 0 : appendRawToken( OPCODE_OPEN );
795 0 : nSpacesSize += appendWhiteSpaceTokens( pClosingSpaces );
796 0 : appendRawToken( OPCODE_CLOSE );
797 0 : pushOperandSize( nSpacesSize + 2 );
798 0 : return true;
799 : }
800 :
801 0 : bool FormulaParserImpl::pushUnaryPreOperatorToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces )
802 : {
803 0 : bool bOk = maOperandSizeStack.size() >= 1;
804 0 : if( bOk )
805 : {
806 0 : size_t nOpSize = popOperandSize();
807 0 : size_t nSpacesSize = insertWhiteSpaceTokens( pSpaces, nOpSize );
808 0 : insertRawToken( nOpCode, nOpSize );
809 0 : pushOperandSize( nOpSize + nSpacesSize + 1 );
810 : }
811 0 : return bOk;
812 : }
813 :
814 0 : bool FormulaParserImpl::pushUnaryPostOperatorToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces )
815 : {
816 0 : bool bOk = maOperandSizeStack.size() >= 1;
817 0 : if( bOk )
818 : {
819 0 : size_t nOpSize = popOperandSize();
820 0 : size_t nSpacesSize = appendWhiteSpaceTokens( pSpaces );
821 0 : appendRawToken( nOpCode );
822 0 : pushOperandSize( nOpSize + nSpacesSize + 1 );
823 : }
824 0 : return bOk;
825 : }
826 :
827 0 : bool FormulaParserImpl::pushBinaryOperatorToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces )
828 : {
829 0 : bool bOk = maOperandSizeStack.size() >= 2;
830 0 : if( bOk )
831 : {
832 0 : size_t nOp2Size = popOperandSize();
833 0 : size_t nOp1Size = popOperandSize();
834 0 : size_t nSpacesSize = insertWhiteSpaceTokens( pSpaces, nOp2Size );
835 0 : insertRawToken( nOpCode, nOp2Size );
836 0 : pushOperandSize( nOp1Size + nSpacesSize + 1 + nOp2Size );
837 : }
838 0 : return bOk;
839 : }
840 :
841 0 : bool FormulaParserImpl::pushParenthesesOperatorToken( const WhiteSpaceVec* pOpeningSpaces, const WhiteSpaceVec* pClosingSpaces )
842 : {
843 0 : bool bOk = maOperandSizeStack.size() >= 1;
844 0 : if( bOk )
845 : {
846 0 : size_t nOpSize = popOperandSize();
847 0 : size_t nSpacesSize = insertWhiteSpaceTokens( pOpeningSpaces, nOpSize );
848 0 : insertRawToken( OPCODE_OPEN, nOpSize );
849 0 : nSpacesSize += appendWhiteSpaceTokens( pClosingSpaces );
850 0 : appendRawToken( OPCODE_CLOSE );
851 0 : pushOperandSize( nOpSize + nSpacesSize + 2 );
852 : }
853 0 : return bOk;
854 : }
855 :
856 0 : bool FormulaParserImpl::pushFunctionOperatorToken( sal_Int32 nOpCode, size_t nParamCount, const WhiteSpaceVec* pLeadingSpaces, const WhiteSpaceVec* pClosingSpaces )
857 : {
858 : /* #i70925# if there are not enough tokens available on token stack, do
859 : not exit with error, but reduce parameter count. */
860 0 : nParamCount = ::std::min( maOperandSizeStack.size(), nParamCount );
861 :
862 : // convert all parameters on stack to a single operand separated with OPCODE_SEP
863 0 : bool bOk = true;
864 0 : for( size_t nParam = 1; bOk && (nParam < nParamCount); ++nParam )
865 0 : bOk = pushBinaryOperatorToken( OPCODE_SEP );
866 :
867 : // add function parentheses and function name
868 0 : return bOk &&
869 0 : ((nParamCount > 0) ? pushParenthesesOperatorToken( 0, pClosingSpaces ) : pushParenthesesOperandToken( 0, pClosingSpaces )) &&
870 0 : pushUnaryPreOperatorToken( nOpCode, pLeadingSpaces );
871 : }
872 :
873 0 : bool FormulaParserImpl::pushFunctionOperatorToken( const FunctionInfo& rFuncInfo, size_t nParamCount, const WhiteSpaceVec* pLeadingSpaces, const WhiteSpaceVec* pClosingSpaces )
874 : {
875 0 : bool bOk = pushFunctionOperatorToken( rFuncInfo.mnApiOpCode, nParamCount, pLeadingSpaces, pClosingSpaces );
876 0 : if( bOk )
877 : {
878 : // create an external add-in call for the passed built-in function
879 0 : if( (rFuncInfo.mnApiOpCode == OPCODE_EXTERNAL) && !rFuncInfo.maExtProgName.isEmpty() )
880 0 : getOperandToken( 1, 0, 0 ).Data <<= rFuncInfo.maExtProgName;
881 : // create a bad token with unsupported function name
882 0 : else if( (rFuncInfo.mnApiOpCode == OPCODE_BAD) && !rFuncInfo.maOoxFuncName.isEmpty() )
883 0 : getOperandToken( 1, 0, 0 ).Data <<= rFuncInfo.maOoxFuncName;
884 : }
885 0 : return bOk;
886 : }
887 :
888 0 : bool FormulaParserImpl::pushOperand( sal_Int32 nOpCode )
889 : {
890 0 : return pushOperandToken( nOpCode, &maLeadingSpaces ) && resetSpaces();
891 : }
892 :
893 0 : bool FormulaParserImpl::pushAnyOperand( const Any& rAny, sal_Int32 nOpCode )
894 : {
895 0 : return pushAnyOperandToken( rAny, nOpCode, &maLeadingSpaces ) && resetSpaces();
896 : }
897 :
898 : template< typename Type >
899 0 : bool FormulaParserImpl::pushValueOperand( const Type& rValue, sal_Int32 nOpCode )
900 : {
901 0 : return pushValueOperandToken( rValue, nOpCode, &maLeadingSpaces ) && resetSpaces();
902 : }
903 :
904 0 : bool FormulaParserImpl::pushBoolOperand( bool bValue )
905 : {
906 0 : if( const FunctionInfo* pFuncInfo = getFuncInfoFromBiff12FuncId( bValue ? BIFF_FUNC_TRUE : BIFF_FUNC_FALSE ) )
907 0 : return pushFunctionOperator( pFuncInfo->mnApiOpCode, 0 );
908 0 : return pushValueOperand< double >( bValue ? 1.0 : 0.0 );
909 : }
910 :
911 0 : bool FormulaParserImpl::pushErrorOperand( double fEncodedError )
912 : {
913 : // HACK: enclose all error codes into an 1x1 matrix
914 : // start token array with opening brace and leading spaces
915 0 : pushOperand( OPCODE_ARRAY_OPEN );
916 0 : size_t nOpSize = popOperandSize();
917 0 : size_t nOldArraySize = maTokenIndexes.size();
918 : // push a double containing the Calc error code
919 0 : appendRawToken( OPCODE_PUSH ) <<= fEncodedError;
920 : // close token array and set resulting operand size
921 0 : appendRawToken( OPCODE_ARRAY_CLOSE );
922 0 : pushOperandSize( nOpSize + maTokenIndexes.size() - nOldArraySize );
923 0 : return true;
924 : }
925 :
926 0 : bool FormulaParserImpl::pushBiffBoolOperand( sal_uInt8 nValue )
927 : {
928 0 : return pushBoolOperand( nValue != BIFF_TOK_BOOL_FALSE );
929 : }
930 :
931 0 : bool FormulaParserImpl::pushBiffErrorOperand( sal_uInt8 nErrorCode )
932 : {
933 0 : return pushErrorOperand( BiffHelper::calcDoubleFromError( nErrorCode ) );
934 : }
935 :
936 0 : bool FormulaParserImpl::pushReferenceOperand( const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset )
937 : {
938 0 : SingleReference aApiRef;
939 0 : convertReference2d( aApiRef, rRef, bDeleted, bRelativeAsOffset );
940 0 : return pushValueOperand( aApiRef );
941 : }
942 :
943 0 : bool FormulaParserImpl::pushReferenceOperand( const BinComplexRef2d& rRef, bool bDeleted, bool bRelativeAsOffset )
944 : {
945 0 : ComplexReference aApiRef;
946 0 : convertReference2d( aApiRef, rRef.maRef1, rRef.maRef2, bDeleted, bRelativeAsOffset );
947 0 : return pushValueOperand( aApiRef );
948 : }
949 :
950 : template< typename Type >
951 0 : bool FormulaParserImpl::pushReferenceOperand( const LinkSheetRange& rSheetRange, const Type& rApiRef )
952 : {
953 0 : if( rSheetRange.isExternal() )
954 : {
955 0 : ExternalReference aApiExtRef;
956 0 : aApiExtRef.Index = rSheetRange.getDocLinkIndex();
957 0 : aApiExtRef.Reference <<= rApiRef;
958 0 : return pushValueOperand( aApiExtRef );
959 : }
960 0 : return pushValueOperand( rApiRef );
961 : }
962 :
963 0 : bool FormulaParserImpl::pushReferenceOperand( const LinkSheetRange& rSheetRange, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset )
964 : {
965 0 : if( rSheetRange.is3dRange() )
966 : {
967 : // single-cell-range over several sheets, needs to create a ComplexReference
968 0 : ComplexReference aApiRef;
969 0 : convertReference3d( aApiRef, rSheetRange, rRef, rRef, bDeleted, bRelativeAsOffset );
970 0 : return pushReferenceOperand( rSheetRange, aApiRef );
971 : }
972 0 : SingleReference aApiRef;
973 0 : convertReference3d( aApiRef, rSheetRange.getFirstSheet(), rSheetRange.isSameSheet(), rRef, bDeleted, bRelativeAsOffset );
974 0 : return pushReferenceOperand( rSheetRange, aApiRef );
975 : }
976 :
977 0 : bool FormulaParserImpl::pushReferenceOperand( const LinkSheetRange& rSheetRange, const BinComplexRef2d& rRef, bool bDeleted, bool bRelativeAsOffset )
978 : {
979 0 : ComplexReference aApiRef;
980 0 : convertReference3d( aApiRef, rSheetRange, rRef.maRef1, rRef.maRef2, bDeleted, bRelativeAsOffset );
981 0 : return pushReferenceOperand( rSheetRange, aApiRef );
982 : }
983 :
984 0 : bool FormulaParserImpl::pushNlrOperand( const BinSingleRef2d& rRef )
985 : {
986 0 : SingleReference aApiRef;
987 0 : convertReference2d( aApiRef, rRef, false, false );
988 0 : return pushValueOperand( aApiRef, OPCODE_NLR );
989 : }
990 :
991 0 : bool FormulaParserImpl::pushEmbeddedRefOperand( const DefinedNameBase& rName, bool bPushBadToken )
992 : {
993 0 : Any aRefAny = rName.getReference( maBaseAddr );
994 0 : if( aRefAny.hasValue() )
995 0 : return pushAnyOperand( aRefAny, OPCODE_PUSH );
996 0 : if( bPushBadToken && !rName.getModelName().isEmpty() && (rName.getModelName()[ 0 ] >= ' ') )
997 0 : return pushValueOperand( rName.getModelName(), OPCODE_BAD );
998 0 : return pushBiffErrorOperand( BIFF_ERR_NAME );
999 : }
1000 :
1001 0 : bool FormulaParserImpl::pushDefinedNameOperand( const DefinedNameRef& rxDefName )
1002 : {
1003 0 : if( !rxDefName || rxDefName->getModelName().isEmpty() )
1004 0 : return pushBiffErrorOperand( BIFF_ERR_NAME );
1005 0 : if( rxDefName->isMacroFunction() )
1006 0 : return pushValueOperand( rxDefName->getModelName(), OPCODE_MACRO );
1007 0 : if( rxDefName->getTokenIndex() >= 0 )
1008 0 : return pushValueOperand( rxDefName->getTokenIndex(), OPCODE_NAME );
1009 0 : return pushEmbeddedRefOperand( *rxDefName, true );
1010 : }
1011 :
1012 0 : bool FormulaParserImpl::pushExternalFuncOperand( const FunctionInfo& rFuncInfo )
1013 : {
1014 0 : return (rFuncInfo.mnApiOpCode == OPCODE_EXTERNAL) ?
1015 0 : pushValueOperand( rFuncInfo.maExtProgName, OPCODE_EXTERNAL ) :
1016 0 : pushOperand( rFuncInfo.mnApiOpCode );
1017 : }
1018 :
1019 0 : bool FormulaParserImpl::pushDdeLinkOperand( const OUString& rDdeServer, const OUString& rDdeTopic, const OUString& rDdeItem )
1020 : {
1021 : // create the function call DDE("server";"topic";"item")
1022 : return
1023 0 : pushValueOperandToken( rDdeServer ) &&
1024 0 : pushValueOperandToken( rDdeTopic ) &&
1025 0 : pushValueOperandToken( rDdeItem ) &&
1026 0 : pushFunctionOperator( OPCODE_DDE, 3 );
1027 : }
1028 :
1029 0 : bool FormulaParserImpl::pushExternalNameOperand( const ExternalNameRef& rxExtName, const ExternalLink& rExtLink )
1030 : {
1031 0 : if( rxExtName.get() ) switch( rExtLink.getLinkType() )
1032 : {
1033 : case LINKTYPE_INTERNAL:
1034 : case LINKTYPE_EXTERNAL:
1035 0 : return pushEmbeddedRefOperand( *rxExtName, false );
1036 :
1037 : case LINKTYPE_ANALYSIS:
1038 : // TODO: need support for localized addin function names
1039 0 : if( const FunctionInfo* pFuncInfo = getFuncInfoFromOoxFuncName( rxExtName->getUpcaseModelName() ) )
1040 0 : return pushExternalFuncOperand( *pFuncInfo );
1041 0 : break;
1042 :
1043 : case LINKTYPE_LIBRARY:
1044 0 : if( const FunctionInfo* pFuncInfo = getFuncInfoFromOoxFuncName( rxExtName->getUpcaseModelName() ) )
1045 0 : if( (pFuncInfo->meFuncLibType != FUNCLIB_UNKNOWN) && (pFuncInfo->meFuncLibType == rExtLink.getFuncLibraryType()) )
1046 0 : return pushExternalFuncOperand( *pFuncInfo );
1047 0 : break;
1048 :
1049 : case LINKTYPE_DDE:
1050 : {
1051 0 : OUString aDdeServer, aDdeTopic, aDdeItem;
1052 0 : if( rxExtName->getDdeLinkData( aDdeServer, aDdeTopic, aDdeItem ) )
1053 0 : return pushDdeLinkOperand( aDdeServer, aDdeTopic, aDdeItem );
1054 : }
1055 0 : break;
1056 :
1057 : default:
1058 : OSL_ENSURE( rExtLink.getLinkType() != LINKTYPE_SELF, "FormulaParserImpl::pushExternalNameOperand - invalid call" );
1059 : }
1060 0 : return pushBiffErrorOperand( BIFF_ERR_NAME );
1061 : }
1062 :
1063 0 : bool FormulaParserImpl::pushSpecialTokenOperand( const BinAddress& rBaseAddr, bool bTable )
1064 : {
1065 0 : CellAddress aBaseAddr( maBaseAddr.Sheet, rBaseAddr.mnCol, rBaseAddr.mnRow );
1066 0 : ApiSpecialTokenInfo aTokenInfo( aBaseAddr, bTable );
1067 0 : return mbSpecialTokens && (getFormulaSize() == 0) && pushValueOperand( aTokenInfo, OPCODE_BAD );
1068 : }
1069 :
1070 0 : bool FormulaParserImpl::pushUnaryPreOperator( sal_Int32 nOpCode )
1071 : {
1072 0 : return pushUnaryPreOperatorToken( nOpCode, &maLeadingSpaces ) && resetSpaces();
1073 : }
1074 :
1075 0 : bool FormulaParserImpl::pushUnaryPostOperator( sal_Int32 nOpCode )
1076 : {
1077 0 : return pushUnaryPostOperatorToken( nOpCode, &maLeadingSpaces ) && resetSpaces();
1078 : }
1079 :
1080 0 : bool FormulaParserImpl::pushBinaryOperator( sal_Int32 nOpCode )
1081 : {
1082 0 : return pushBinaryOperatorToken( nOpCode, &maLeadingSpaces ) && resetSpaces();
1083 : }
1084 :
1085 0 : bool FormulaParserImpl::pushParenthesesOperator()
1086 : {
1087 0 : return pushParenthesesOperatorToken( &maOpeningSpaces, &maClosingSpaces ) && resetSpaces();
1088 : }
1089 :
1090 0 : bool FormulaParserImpl::pushFunctionOperator( sal_Int32 nOpCode, size_t nParamCount )
1091 : {
1092 0 : return pushFunctionOperatorToken( nOpCode, nParamCount, &maLeadingSpaces, &maClosingSpaces ) && resetSpaces();
1093 : }
1094 :
1095 0 : bool FormulaParserImpl::pushFunctionOperator( const FunctionInfo& rFuncInfo, size_t nParamCount )
1096 : {
1097 0 : return pushFunctionOperatorToken( rFuncInfo, nParamCount, &maLeadingSpaces, &maClosingSpaces ) && resetSpaces();
1098 : }
1099 :
1100 : // reference conversion -------------------------------------------------------
1101 :
1102 0 : void FormulaParserImpl::initReference2d( SingleReference& orApiRef ) const
1103 : {
1104 0 : if( mb2dRefsAs3dRefs )
1105 : {
1106 0 : initReference3d( orApiRef, maBaseAddr.Sheet, false );
1107 : }
1108 : else
1109 : {
1110 0 : orApiRef.Flags = SHEET_RELATIVE;
1111 : // #i10184# absolute sheet index needed for relative references in shared formulas
1112 0 : orApiRef.Sheet = maBaseAddr.Sheet;
1113 0 : orApiRef.RelativeSheet = 0;
1114 : }
1115 0 : }
1116 :
1117 0 : void FormulaParserImpl::initReference3d( SingleReference& orApiRef, sal_Int32 nSheet, bool bSameSheet ) const
1118 : {
1119 0 : orApiRef.Flags = SHEET_3D;
1120 0 : if( nSheet < 0 )
1121 : {
1122 0 : orApiRef.Sheet = 0;
1123 0 : orApiRef.Flags |= SHEET_DELETED;
1124 : }
1125 0 : else if( bSameSheet )
1126 : {
1127 : OSL_ENSURE( nSheet == 0, "FormulaParserImpl::initReference3d - invalid sheet index" );
1128 0 : orApiRef.Flags |= SHEET_RELATIVE;
1129 0 : orApiRef.RelativeSheet = 0;
1130 : }
1131 : else
1132 : {
1133 0 : orApiRef.Sheet = nSheet;
1134 : }
1135 0 : }
1136 :
1137 0 : void FormulaParserImpl::convertReference( SingleReference& orApiRef, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset ) const
1138 : {
1139 0 : if( bDeleted )
1140 : {
1141 0 : orApiRef.Column = 0;
1142 0 : orApiRef.Row = 0;
1143 : // no explicit information about whether row or column is deleted
1144 0 : orApiRef.Flags |= COLUMN_DELETED | ROW_DELETED;
1145 : }
1146 : else
1147 : {
1148 : // column/row indexes and flags
1149 0 : setFlag( orApiRef.Flags, COLUMN_RELATIVE, rRef.mbColRel );
1150 0 : setFlag( orApiRef.Flags, ROW_RELATIVE, rRef.mbRowRel );
1151 0 : (rRef.mbColRel ? orApiRef.RelativeColumn : orApiRef.Column) = rRef.mnCol;
1152 0 : (rRef.mbRowRel ? orApiRef.RelativeRow : orApiRef.Row) = rRef.mnRow;
1153 : // convert absolute indexes to relative offsets used in API
1154 0 : if( !bRelativeAsOffset )
1155 : {
1156 0 : if( rRef.mbColRel )
1157 0 : orApiRef.RelativeColumn -= maBaseAddr.Column;
1158 0 : if( rRef.mbRowRel )
1159 0 : orApiRef.RelativeRow -= maBaseAddr.Row;
1160 : }
1161 : }
1162 0 : }
1163 :
1164 0 : void FormulaParserImpl::convertReference( ComplexReference& orApiRef, const BinSingleRef2d& rRef1, const BinSingleRef2d& rRef2, bool bDeleted, bool bRelativeAsOffset ) const
1165 : {
1166 0 : convertReference( orApiRef.Reference1, rRef1, bDeleted, bRelativeAsOffset );
1167 0 : convertReference( orApiRef.Reference2, rRef2, bDeleted, bRelativeAsOffset );
1168 : /* Handle references to complete rows or columns (e.g. $1:$2 or C:D),
1169 : need to expand or shrink to limits of own document. */
1170 0 : if( !bDeleted && !rRef1.mbColRel && !rRef2.mbColRel && (orApiRef.Reference1.Column == 0) && (orApiRef.Reference2.Column == mnMaxXlsCol) )
1171 0 : orApiRef.Reference2.Column = mnMaxApiCol;
1172 0 : if( !bDeleted && !rRef1.mbRowRel && !rRef2.mbRowRel && (orApiRef.Reference1.Row == 0) && (orApiRef.Reference2.Row == mnMaxXlsRow) )
1173 0 : orApiRef.Reference2.Row = mnMaxApiRow;
1174 0 : }
1175 :
1176 0 : void FormulaParserImpl::convertReference2d( SingleReference& orApiRef, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset ) const
1177 : {
1178 0 : initReference2d( orApiRef );
1179 0 : convertReference( orApiRef, rRef, bDeleted, bRelativeAsOffset );
1180 0 : }
1181 :
1182 0 : void FormulaParserImpl::convertReference2d( ComplexReference& orApiRef, const BinSingleRef2d& rRef1, const BinSingleRef2d& rRef2, bool bDeleted, bool bRelativeAsOffset ) const
1183 : {
1184 0 : initReference2d( orApiRef.Reference1 );
1185 0 : initReference2d( orApiRef.Reference2 );
1186 0 : convertReference( orApiRef, rRef1, rRef2, bDeleted, bRelativeAsOffset );
1187 : // remove sheet name from second part of reference
1188 0 : setFlag( orApiRef.Reference2.Flags, SHEET_3D, false );
1189 0 : }
1190 :
1191 0 : void FormulaParserImpl::convertReference3d( SingleReference& orApiRef, sal_Int32 nSheet, bool bSameSheet, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset ) const
1192 : {
1193 0 : initReference3d( orApiRef, nSheet, bSameSheet );
1194 0 : convertReference( orApiRef, rRef, bDeleted, bRelativeAsOffset );
1195 0 : }
1196 :
1197 0 : void FormulaParserImpl::convertReference3d( ComplexReference& orApiRef, const LinkSheetRange& rSheetRange, const BinSingleRef2d& rRef1, const BinSingleRef2d& rRef2, bool bDeleted, bool bRelativeAsOffset ) const
1198 : {
1199 0 : bool bSameSheet = rSheetRange.isSameSheet();
1200 0 : initReference3d( orApiRef.Reference1, rSheetRange.getFirstSheet(), bSameSheet );
1201 0 : initReference3d( orApiRef.Reference2, rSheetRange.getLastSheet(), bSameSheet );
1202 0 : convertReference( orApiRef, rRef1, rRef2, bDeleted, bRelativeAsOffset );
1203 : // remove sheet name from second part of reference
1204 0 : setFlag( orApiRef.Reference2.Flags, SHEET_3D, rSheetRange.is3dRange() );
1205 0 : }
1206 :
1207 : // finalize token sequence ----------------------------------------------------
1208 :
1209 2 : const FunctionInfo* FormulaParserImpl::resolveBadFuncName( const OUString& rTokenData ) const
1210 : {
1211 : /* Try to parse calls to library functions. The format of such a function
1212 : call is "[n]!funcname", n>0 being the link identifier of the function
1213 : library spreadsheet file. */
1214 2 : sal_Int32 nBracketOpen = rTokenData.indexOf( '[' );
1215 2 : sal_Int32 nBracketClose = rTokenData.indexOf( ']' );
1216 2 : sal_Int32 nExclamation = rTokenData.indexOf( '!' );
1217 2 : if( (0 == nBracketOpen) && (nBracketOpen + 1 < nBracketClose) && (nBracketClose + 1 == nExclamation) && (nExclamation + 1 < rTokenData.getLength()) )
1218 : {
1219 0 : sal_Int32 nRefId = rTokenData.copy( nBracketOpen + 1, nBracketClose - nBracketOpen - 1 ).toInt32();
1220 0 : const ExternalLink* pExtLink = getExternalLinks().getExternalLink( nRefId ).get();
1221 0 : if( pExtLink && (pExtLink->getLinkType() == LINKTYPE_LIBRARY) )
1222 : {
1223 0 : OUString aFuncName = rTokenData.copy( nExclamation + 1 ).toAsciiUpperCase();
1224 0 : if( const FunctionInfo* pFuncInfo = getFuncInfoFromOoxFuncName( aFuncName ) )
1225 0 : if( (pFuncInfo->meFuncLibType != FUNCLIB_UNKNOWN) && (pFuncInfo->meFuncLibType == pExtLink->getFuncLibraryType()) )
1226 0 : return pFuncInfo;
1227 : }
1228 : }
1229 2 : return 0;
1230 : }
1231 :
1232 0 : OUString FormulaParserImpl::resolveDefinedName( sal_Int32 nTokenIndex ) const
1233 : {
1234 0 : if( const DefinedName* pDefName = getDefinedNames().getByTokenIndex( nTokenIndex ).get() )
1235 0 : return pDefName->getCalcName();
1236 0 : return OUString();
1237 : }
1238 :
1239 : // OOXML/BIFF12 parser implementation =========================================
1240 :
1241 42 : class OoxFormulaParserImpl : public FormulaParserImpl
1242 : {
1243 : public:
1244 : explicit OoxFormulaParserImpl( const FormulaParser& rParent );
1245 :
1246 : virtual ApiTokenSequence importOoxFormula(
1247 : const CellAddress& rBaseAddr,
1248 : const OUString& rFormulaString );
1249 :
1250 : virtual ApiTokenSequence importBiff12Formula(
1251 : const CellAddress& rBaseAddr,
1252 : FormulaType eType,
1253 : SequenceInputStream& rStrm );
1254 :
1255 : private:
1256 : // import token contents and create API formula token ---------------------
1257 :
1258 : bool importAttrToken( SequenceInputStream& rStrm );
1259 : bool importSpaceToken( SequenceInputStream& rStrm );
1260 : bool importTableToken( SequenceInputStream& rStrm );
1261 : bool importArrayToken( SequenceInputStream& rStrm );
1262 : bool importRefToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
1263 : bool importAreaToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
1264 : bool importRef3dToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
1265 : bool importArea3dToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
1266 : bool importMemAreaToken( SequenceInputStream& rStrm, bool bAddData );
1267 : bool importMemFuncToken( SequenceInputStream& rStrm );
1268 : bool importNameToken( SequenceInputStream& rStrm );
1269 : bool importNameXToken( SequenceInputStream& rStrm );
1270 : bool importFuncToken( SequenceInputStream& rStrm );
1271 : bool importFuncVarToken( SequenceInputStream& rStrm );
1272 : bool importExpToken( SequenceInputStream& rStrm );
1273 :
1274 : LinkSheetRange readSheetRange( SequenceInputStream& rStrm );
1275 :
1276 : void swapStreamPosition( SequenceInputStream& rStrm );
1277 : void skipMemAreaAddData( SequenceInputStream& rStrm );
1278 :
1279 : // convert BIN token and push API operand or operator ---------------------
1280 :
1281 : bool pushBiff12Name( sal_Int32 nNameId );
1282 : bool pushBiff12ExtName( sal_Int32 nRefId, sal_Int32 nNameId );
1283 : bool pushBiff12Function( sal_uInt16 nFuncId );
1284 : bool pushBiff12Function( sal_uInt16 nFuncId, sal_uInt8 nParamCount );
1285 :
1286 : private:
1287 : ApiParserWrapper maApiParser; /// Wrapper for the API formula parser object.
1288 : sal_Int64 mnAddDataPos; /// Current stream position for additional data (tExp, tArray, tMemArea).
1289 : bool mbNeedExtRefs; /// True = parser needs initialization of external reference info.
1290 : };
1291 :
1292 : // ----------------------------------------------------------------------------
1293 :
1294 21 : OoxFormulaParserImpl::OoxFormulaParserImpl( const FormulaParser& rParent ) :
1295 : FormulaParserImpl( rParent ),
1296 21 : maApiParser( rParent.getBaseFilter().getModelFactory(), rParent ),
1297 : mnAddDataPos( 0 ),
1298 42 : mbNeedExtRefs( true )
1299 : {
1300 21 : }
1301 :
1302 47 : ApiTokenSequence OoxFormulaParserImpl::importOoxFormula( const CellAddress& rBaseAddr, const OUString& rFormulaString )
1303 : {
1304 47 : if( mbNeedExtRefs )
1305 : {
1306 8 : maApiParser.getParserProperties().setProperty( PROP_ExternalLinks, getExternalLinks().getLinkInfos() );
1307 8 : mbNeedExtRefs = false;
1308 : }
1309 47 : return finalizeTokenArray( maApiParser.parseFormula( rFormulaString, rBaseAddr ) );
1310 : }
1311 :
1312 0 : ApiTokenSequence OoxFormulaParserImpl::importBiff12Formula( const CellAddress& rBaseAddr, FormulaType eType, SequenceInputStream& rStrm )
1313 : {
1314 0 : initializeImport( rBaseAddr, eType );
1315 :
1316 0 : sal_Int32 nFmlaSize = rStrm.readInt32();
1317 0 : sal_Int64 nFmlaPos = rStrm.tell();
1318 0 : sal_Int64 nFmlaEndPos = nFmlaPos + nFmlaSize;
1319 :
1320 0 : rStrm.seek( nFmlaEndPos );
1321 0 : sal_Int32 nAddDataSize = rStrm.readInt32();
1322 0 : mnAddDataPos = rStrm.tell();
1323 0 : sal_Int64 nAddDataEndPos = mnAddDataPos + nAddDataSize;
1324 0 : rStrm.seek( nFmlaPos );
1325 :
1326 0 : bool bOk = (nFmlaSize >= 0) && (nAddDataSize >= 0);
1327 0 : bool bRelativeAsOffset = mbRelativeAsOffset;
1328 :
1329 0 : while( bOk && !rStrm.isEof() && (rStrm.tell() < nFmlaEndPos) )
1330 : {
1331 : sal_uInt8 nTokenId;
1332 0 : rStrm >> nTokenId;
1333 0 : sal_uInt8 nTokenClass = nTokenId & BIFF_TOKCLASS_MASK;
1334 0 : sal_uInt8 nBaseId = nTokenId & BIFF_TOKID_MASK;
1335 :
1336 0 : if( nTokenClass == BIFF_TOKCLASS_NONE )
1337 : {
1338 : // base tokens
1339 0 : switch( nBaseId )
1340 : {
1341 0 : case BIFF_TOKID_EXP: bOk = importExpToken( rStrm ); break;
1342 0 : case BIFF_TOKID_ADD: bOk = pushBinaryOperator( OPCODE_ADD ); break;
1343 0 : case BIFF_TOKID_SUB: bOk = pushBinaryOperator( OPCODE_SUB ); break;
1344 0 : case BIFF_TOKID_MUL: bOk = pushBinaryOperator( OPCODE_MULT ); break;
1345 0 : case BIFF_TOKID_DIV: bOk = pushBinaryOperator( OPCODE_DIV ); break;
1346 0 : case BIFF_TOKID_POWER: bOk = pushBinaryOperator( OPCODE_POWER ); break;
1347 0 : case BIFF_TOKID_CONCAT: bOk = pushBinaryOperator( OPCODE_CONCAT ); break;
1348 0 : case BIFF_TOKID_LT: bOk = pushBinaryOperator( OPCODE_LESS ); break;
1349 0 : case BIFF_TOKID_LE: bOk = pushBinaryOperator( OPCODE_LESS_EQUAL ); break;
1350 0 : case BIFF_TOKID_EQ: bOk = pushBinaryOperator( OPCODE_EQUAL ); break;
1351 0 : case BIFF_TOKID_GE: bOk = pushBinaryOperator( OPCODE_GREATER_EQUAL ); break;
1352 0 : case BIFF_TOKID_GT: bOk = pushBinaryOperator( OPCODE_GREATER ); break;
1353 0 : case BIFF_TOKID_NE: bOk = pushBinaryOperator( OPCODE_NOT_EQUAL ); break;
1354 0 : case BIFF_TOKID_ISECT: bOk = pushBinaryOperator( OPCODE_INTERSECT ); break;
1355 0 : case BIFF_TOKID_LIST: bOk = pushBinaryOperator( OPCODE_LIST ); break;
1356 0 : case BIFF_TOKID_RANGE: bOk = pushBinaryOperator( OPCODE_RANGE ); break;
1357 0 : case BIFF_TOKID_UPLUS: bOk = pushUnaryPreOperator( OPCODE_PLUS_SIGN ); break;
1358 0 : case BIFF_TOKID_UMINUS: bOk = pushUnaryPreOperator( OPCODE_MINUS_SIGN ); break;
1359 0 : case BIFF_TOKID_PERCENT: bOk = pushUnaryPostOperator( OPCODE_PERCENT ); break;
1360 0 : case BIFF_TOKID_PAREN: bOk = pushParenthesesOperator(); break;
1361 0 : case BIFF_TOKID_MISSARG: bOk = pushOperand( OPCODE_MISSING ); break;
1362 0 : case BIFF_TOKID_STR: bOk = pushValueOperand( BiffHelper::readString( rStrm, false ) ); break;
1363 0 : case BIFF_TOKID_NLR: bOk = importTableToken( rStrm ); break;
1364 0 : case BIFF_TOKID_ATTR: bOk = importAttrToken( rStrm ); break;
1365 0 : case BIFF_TOKID_ERR: bOk = pushBiffErrorOperand( rStrm.readuInt8() ); break;
1366 0 : case BIFF_TOKID_BOOL: bOk = pushBiffBoolOperand( rStrm.readuInt8() ); break;
1367 0 : case BIFF_TOKID_INT: bOk = pushValueOperand< double >( rStrm.readuInt16() ); break;
1368 0 : case BIFF_TOKID_NUM: bOk = pushValueOperand( rStrm.readDouble() ); break;
1369 0 : default: bOk = false;
1370 : }
1371 : }
1372 : else
1373 : {
1374 : // classified tokens
1375 0 : switch( nBaseId )
1376 : {
1377 0 : case BIFF_TOKID_ARRAY: bOk = importArrayToken( rStrm ); break;
1378 0 : case BIFF_TOKID_FUNC: bOk = importFuncToken( rStrm ); break;
1379 0 : case BIFF_TOKID_FUNCVAR: bOk = importFuncVarToken( rStrm ); break;
1380 0 : case BIFF_TOKID_NAME: bOk = importNameToken( rStrm ); break;
1381 0 : case BIFF_TOKID_REF: bOk = importRefToken( rStrm, false, false ); break;
1382 0 : case BIFF_TOKID_AREA: bOk = importAreaToken( rStrm, false, false ); break;
1383 0 : case BIFF_TOKID_MEMAREA: bOk = importMemAreaToken( rStrm, true ); break;
1384 0 : case BIFF_TOKID_MEMERR: bOk = importMemAreaToken( rStrm, false ); break;
1385 0 : case BIFF_TOKID_MEMNOMEM: bOk = importMemAreaToken( rStrm, false ); break;
1386 0 : case BIFF_TOKID_MEMFUNC: bOk = importMemFuncToken( rStrm ); break;
1387 0 : case BIFF_TOKID_REFERR: bOk = importRefToken( rStrm, true, false ); break;
1388 0 : case BIFF_TOKID_AREAERR: bOk = importAreaToken( rStrm, true, false ); break;
1389 0 : case BIFF_TOKID_REFN: bOk = importRefToken( rStrm, false, true ); break;
1390 0 : case BIFF_TOKID_AREAN: bOk = importAreaToken( rStrm, false, true ); break;
1391 0 : case BIFF_TOKID_MEMAREAN: bOk = importMemFuncToken( rStrm ); break;
1392 0 : case BIFF_TOKID_MEMNOMEMN: bOk = importMemFuncToken( rStrm ); break;
1393 0 : case BIFF_TOKID_NAMEX: bOk = importNameXToken( rStrm ); break;
1394 0 : case BIFF_TOKID_REF3D: bOk = importRef3dToken( rStrm, false, bRelativeAsOffset ); break;
1395 0 : case BIFF_TOKID_AREA3D: bOk = importArea3dToken( rStrm, false, bRelativeAsOffset ); break;
1396 0 : case BIFF_TOKID_REFERR3D: bOk = importRef3dToken( rStrm, true, bRelativeAsOffset ); break;
1397 0 : case BIFF_TOKID_AREAERR3D: bOk = importArea3dToken( rStrm, true, bRelativeAsOffset ); break;
1398 0 : default: bOk = false;
1399 : }
1400 : }
1401 : }
1402 :
1403 : // build and finalize the token sequence
1404 0 : ApiTokenSequence aFinalTokens;
1405 0 : if( bOk && (rStrm.tell() == nFmlaEndPos) && (mnAddDataPos == nAddDataEndPos) )
1406 0 : aFinalTokens = finalizeImport();
1407 :
1408 : // seek behind token array
1409 0 : if( (nFmlaSize >= 0) && (nAddDataSize >= 0) )
1410 0 : rStrm.seek( nAddDataEndPos );
1411 :
1412 : // return the final token sequence
1413 0 : return aFinalTokens;
1414 : }
1415 :
1416 : // import token contents and create API formula token -------------------------
1417 :
1418 0 : bool OoxFormulaParserImpl::importAttrToken( SequenceInputStream& rStrm )
1419 : {
1420 0 : bool bOk = true;
1421 : sal_uInt8 nType;
1422 0 : rStrm >> nType;
1423 : // equal flags in all BIFFs
1424 0 : switch( nType )
1425 : {
1426 : case 0: // sometimes, tAttrSkip tokens miss the type flag
1427 : case BIFF_TOK_ATTR_VOLATILE:
1428 : case BIFF_TOK_ATTR_IF:
1429 : case BIFF_TOK_ATTR_SKIP:
1430 : case BIFF_TOK_ATTR_ASSIGN:
1431 : case BIFF_TOK_ATTR_IFERROR:
1432 0 : rStrm.skip( 2 );
1433 0 : break;
1434 : case BIFF_TOK_ATTR_CHOOSE:
1435 0 : rStrm.skip( 2 * rStrm.readuInt16() + 2 );
1436 0 : break;
1437 : case BIFF_TOK_ATTR_SUM:
1438 0 : rStrm.skip( 2 );
1439 0 : bOk = pushBiff12Function( BIFF_FUNC_SUM, 1 );
1440 0 : break;
1441 : case BIFF_TOK_ATTR_SPACE:
1442 : case BIFF_TOK_ATTR_SPACE_VOLATILE:
1443 0 : bOk = importSpaceToken( rStrm );
1444 0 : break;
1445 : default:
1446 0 : bOk = false;
1447 : }
1448 0 : return bOk;
1449 : }
1450 :
1451 0 : bool OoxFormulaParserImpl::importSpaceToken( SequenceInputStream& rStrm )
1452 : {
1453 : // equal constants in BIFF and OOX
1454 : sal_uInt8 nType, nCount;
1455 0 : rStrm >> nType >> nCount;
1456 0 : switch( nType )
1457 : {
1458 : case BIFF_TOK_ATTR_SPACE_SP:
1459 0 : appendLeadingSpaces( nCount, false );
1460 0 : break;
1461 : case BIFF_TOK_ATTR_SPACE_BR:
1462 0 : appendLeadingSpaces( nCount, true );
1463 0 : break;
1464 : case BIFF_TOK_ATTR_SPACE_SP_OPEN:
1465 0 : appendOpeningSpaces( nCount, false );
1466 0 : break;
1467 : case BIFF_TOK_ATTR_SPACE_BR_OPEN:
1468 0 : appendOpeningSpaces( nCount, true );
1469 0 : break;
1470 : case BIFF_TOK_ATTR_SPACE_SP_CLOSE:
1471 0 : appendClosingSpaces( nCount, false );
1472 0 : break;
1473 : case BIFF_TOK_ATTR_SPACE_BR_CLOSE:
1474 0 : appendClosingSpaces( nCount, true );
1475 0 : break;
1476 : }
1477 0 : return true;
1478 : }
1479 :
1480 0 : bool OoxFormulaParserImpl::importTableToken( SequenceInputStream& rStrm )
1481 : {
1482 : sal_uInt16 nFlags, nTableId, nCol1, nCol2;
1483 0 : rStrm.skip( 3 );
1484 0 : rStrm >> nFlags >> nTableId;
1485 0 : rStrm.skip( 2 );
1486 0 : rStrm >> nCol1 >> nCol2;
1487 0 : TableRef xTable = getTables().getTable( nTableId );
1488 0 : sal_Int32 nTokenIndex = xTable.get() ? xTable->getTokenIndex() : -1;
1489 0 : if( nTokenIndex >= 0 )
1490 : {
1491 0 : sal_Int32 nWidth = xTable->getWidth();
1492 0 : sal_Int32 nHeight = xTable->getHeight();
1493 0 : sal_Int32 nStartCol = 0;
1494 0 : sal_Int32 nEndCol = nWidth - 1;
1495 0 : sal_Int32 nStartRow = 0;
1496 0 : sal_Int32 nEndRow = nHeight - 1;
1497 0 : bool bFixedStartRow = true;
1498 0 : bool bFixedHeight = false;
1499 :
1500 0 : bool bSingleCol = getFlag( nFlags, BIFF12_TOK_TABLE_COLUMN );
1501 0 : bool bColRange = getFlag( nFlags, BIFF12_TOK_TABLE_COLRANGE );
1502 0 : bool bValidRef = !bSingleCol || !bColRange;
1503 : OSL_ENSURE( bValidRef, "OoxFormulaParserImpl::importTableToken - illegal combination of single column and column range" );
1504 0 : if( bValidRef )
1505 : {
1506 0 : if( bSingleCol )
1507 0 : nStartCol = nEndCol = nCol1;
1508 0 : else if( bColRange )
1509 0 : { nStartCol = nCol1; nEndCol = nCol2; }
1510 0 : bValidRef = (nStartCol <= nEndCol) && (nEndCol < nWidth);
1511 : OSL_ENSURE( bValidRef, "OoxFormulaParserImpl::importTableToken - invalid column range" );
1512 : }
1513 :
1514 0 : if( bValidRef )
1515 : {
1516 0 : bool bAllRows = getFlag( nFlags, BIFF12_TOK_TABLE_ALL );
1517 0 : bool bHeaderRows = getFlag( nFlags, BIFF12_TOK_TABLE_HEADERS );
1518 0 : bool bDataRows = getFlag( nFlags, BIFF12_TOK_TABLE_DATA );
1519 0 : bool bTotalsRows = getFlag( nFlags, BIFF12_TOK_TABLE_TOTALS );
1520 0 : bool bThisRow = getFlag( nFlags, BIFF12_TOK_TABLE_THISROW );
1521 :
1522 0 : sal_Int32 nStartDataRow = xTable->getHeaderRows();
1523 0 : sal_Int32 nEndDataRow = nEndRow - xTable->getTotalsRows();
1524 0 : bValidRef = (nStartRow <= nStartDataRow) && (nStartDataRow <= nEndDataRow) && (nEndDataRow <= nEndRow);
1525 : OSL_ENSURE( bValidRef, "OoxFormulaParserImpl::importTableToken - invalid data row range" );
1526 0 : if( bValidRef )
1527 : {
1528 0 : if( bAllRows )
1529 : {
1530 0 : bValidRef = !bHeaderRows && !bDataRows && !bTotalsRows && !bThisRow;
1531 : OSL_ENSURE( bValidRef, "OoxFormulaParserImpl::importTableToken - unexpected flags in [#All] table token" );
1532 : }
1533 0 : else if( bHeaderRows )
1534 : {
1535 0 : bValidRef = !bTotalsRows && !bThisRow;
1536 : OSL_ENSURE( bValidRef, "OoxFormulaParserImpl::importTableToken - unexpected flags in [#Headers] table token" );
1537 0 : nEndRow = bDataRows ? nEndDataRow : (nStartDataRow - 1);
1538 0 : bFixedHeight = !bDataRows;
1539 : }
1540 0 : else if( bDataRows )
1541 : {
1542 0 : bValidRef = !bThisRow;
1543 : OSL_ENSURE( bValidRef, "OoxFormulaParserImpl::importTableToken - unexpected flags in [#Data] table token" );
1544 0 : nStartRow = nStartDataRow;
1545 0 : if( !bTotalsRows ) nEndRow = nEndDataRow;
1546 : }
1547 0 : else if( bTotalsRows )
1548 : {
1549 0 : bValidRef = !bThisRow;
1550 : OSL_ENSURE( bValidRef, "OoxFormulaParserImpl::importTableToken - unexpected flags in [#Totals] table token" );
1551 0 : nStartRow = nEndDataRow + 1;
1552 0 : bFixedStartRow = false;
1553 0 : bFixedHeight = !bDataRows;
1554 : }
1555 0 : else if( bThisRow )
1556 : {
1557 0 : nStartRow = nEndRow = maBaseAddr.Row - xTable->getRange().StartRow;
1558 0 : bFixedHeight = true;
1559 : }
1560 : else
1561 : {
1562 : // nothing is the same as [#Data]
1563 0 : nStartRow = nStartDataRow;
1564 0 : nEndRow = nEndDataRow;
1565 : }
1566 : }
1567 0 : if( bValidRef )
1568 0 : bValidRef = (0 <= nStartRow) && (nStartRow <= nEndRow) && (nEndRow < nHeight);
1569 : }
1570 0 : if( bValidRef )
1571 : {
1572 : // push single database area token, if table token refers to entire table
1573 0 : if( (nStartCol == 0) && (nEndCol + 1 == nWidth) && (nStartRow == 0) && (nEndRow + 1 == nHeight) )
1574 0 : return pushValueOperand( nTokenIndex, OPCODE_DBAREA );
1575 : // create an OFFSET function call to refer to a subrange of the table
1576 0 : const FunctionInfo* pRowsInfo = getFuncInfoFromBiff12FuncId( BIFF_FUNC_ROWS );
1577 0 : const FunctionInfo* pColumnsInfo = getFuncInfoFromBiff12FuncId( BIFF_FUNC_COLUMNS );
1578 : return
1579 0 : pRowsInfo && pColumnsInfo &&
1580 0 : pushValueOperandToken( nTokenIndex, OPCODE_DBAREA ) &&
1581 : (bFixedStartRow ?
1582 0 : pushValueOperandToken< double >( nStartRow ) :
1583 0 : (pushValueOperandToken( nTokenIndex, OPCODE_DBAREA ) &&
1584 0 : pushFunctionOperatorToken( *pRowsInfo, 1 ) &&
1585 0 : pushValueOperandToken< double >( nHeight - nStartRow ) &&
1586 0 : pushBinaryOperatorToken( OPCODE_SUB ))) &&
1587 0 : pushValueOperandToken< double >( nStartCol ) &&
1588 : (bFixedHeight ?
1589 0 : pushValueOperandToken< double >( nEndRow - nStartRow + 1 ) :
1590 0 : (pushValueOperandToken( nTokenIndex, OPCODE_DBAREA ) &&
1591 0 : pushFunctionOperatorToken( *pRowsInfo, 1 ) &&
1592 0 : (((nStartRow == 0) && (nEndRow + 1 == nHeight)) ||
1593 0 : (pushValueOperandToken< double >( nHeight - (nEndRow - nStartRow + 1) ) &&
1594 0 : pushBinaryOperatorToken( OPCODE_SUB ))))) &&
1595 0 : (((nStartCol == 0) && (nEndCol + 1 == nWidth)) ?
1596 0 : (pushValueOperandToken( nTokenIndex, OPCODE_DBAREA ) &&
1597 0 : pushFunctionOperatorToken( *pColumnsInfo, 1 )) :
1598 0 : pushValueOperandToken< double >( nEndCol - nStartCol + 1 )) &&
1599 0 : pushBiff12Function( BIFF_FUNC_OFFSET, 5 );
1600 : }
1601 : }
1602 0 : return pushBiffErrorOperand( BIFF_ERR_REF );
1603 : }
1604 :
1605 0 : bool OoxFormulaParserImpl::importArrayToken( SequenceInputStream& rStrm )
1606 : {
1607 0 : rStrm.skip( 14 );
1608 :
1609 : // start token array with opening brace and leading spaces
1610 0 : pushOperand( OPCODE_ARRAY_OPEN );
1611 0 : size_t nOpSize = popOperandSize();
1612 0 : size_t nOldArraySize = getFormulaSize();
1613 :
1614 : // read array size
1615 0 : swapStreamPosition( rStrm );
1616 0 : sal_Int32 nRows = rStrm.readInt32();
1617 0 : sal_Int32 nCols = rStrm.readInt32();
1618 : OSL_ENSURE( (nCols > 0) && (nRows > 0), "OoxFormulaParserImpl::importArrayToken - empty array" );
1619 :
1620 : // read array values and build token array
1621 0 : for( sal_Int32 nRow = 0; !rStrm.isEof() && (nRow < nRows); ++nRow )
1622 : {
1623 0 : if( nRow > 0 )
1624 0 : appendRawToken( OPCODE_ARRAY_ROWSEP );
1625 0 : for( sal_Int32 nCol = 0; !rStrm.isEof() && (nCol < nCols); ++nCol )
1626 : {
1627 0 : if( nCol > 0 )
1628 0 : appendRawToken( OPCODE_ARRAY_COLSEP );
1629 0 : switch( rStrm.readuInt8() )
1630 : {
1631 : case BIFF_TOK_ARRAY_DOUBLE:
1632 0 : appendRawToken( OPCODE_PUSH ) <<= rStrm.readDouble();
1633 0 : break;
1634 : case BIFF_TOK_ARRAY_STRING:
1635 0 : appendRawToken( OPCODE_PUSH ) <<= BiffHelper::readString( rStrm, false );
1636 0 : break;
1637 : case BIFF_TOK_ARRAY_BOOL:
1638 0 : appendRawToken( OPCODE_PUSH ) <<= (static_cast< double >( (rStrm.readuInt8() == BIFF_TOK_BOOL_FALSE) ? 0.0 : 1.0 ));
1639 0 : break;
1640 : case BIFF_TOK_ARRAY_ERROR:
1641 0 : appendRawToken( OPCODE_PUSH ) <<= BiffHelper::calcDoubleFromError( rStrm.readuInt8() );
1642 0 : rStrm.skip( 3 );
1643 0 : break;
1644 : default:
1645 : OSL_FAIL( "OoxFormulaParserImpl::importArrayToken - unknown data type" );
1646 0 : appendRawToken( OPCODE_PUSH ) <<= BiffHelper::calcDoubleFromError( BIFF_ERR_NA );
1647 : }
1648 : }
1649 : }
1650 0 : swapStreamPosition( rStrm );
1651 :
1652 : // close token array and set resulting operand size
1653 0 : appendRawToken( OPCODE_ARRAY_CLOSE );
1654 0 : pushOperandSize( nOpSize + getFormulaSize() - nOldArraySize );
1655 0 : return true;
1656 : }
1657 :
1658 0 : bool OoxFormulaParserImpl::importRefToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
1659 : {
1660 0 : BinSingleRef2d aRef;
1661 0 : aRef.readBiff12Data( rStrm, bRelativeAsOffset );
1662 0 : return pushReferenceOperand( aRef, bDeleted, bRelativeAsOffset );
1663 : }
1664 :
1665 0 : bool OoxFormulaParserImpl::importAreaToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
1666 : {
1667 0 : BinComplexRef2d aRef;
1668 0 : aRef.readBiff12Data( rStrm, bRelativeAsOffset );
1669 0 : return pushReferenceOperand( aRef, bDeleted, bRelativeAsOffset );
1670 : }
1671 :
1672 0 : bool OoxFormulaParserImpl::importRef3dToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
1673 : {
1674 0 : LinkSheetRange aSheetRange = readSheetRange( rStrm );
1675 0 : BinSingleRef2d aRef;
1676 0 : aRef.readBiff12Data( rStrm, bRelativeAsOffset );
1677 0 : return pushReferenceOperand( aSheetRange, aRef, bDeleted, bRelativeAsOffset );
1678 : }
1679 :
1680 0 : bool OoxFormulaParserImpl::importArea3dToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
1681 : {
1682 0 : LinkSheetRange aSheetRange = readSheetRange( rStrm );
1683 0 : BinComplexRef2d aRef;
1684 0 : aRef.readBiff12Data( rStrm, bRelativeAsOffset );
1685 0 : return pushReferenceOperand( aSheetRange, aRef, bDeleted, bRelativeAsOffset );
1686 : }
1687 :
1688 0 : bool OoxFormulaParserImpl::importMemAreaToken( SequenceInputStream& rStrm, bool bAddData )
1689 : {
1690 0 : rStrm.skip( 6 );
1691 0 : if( bAddData )
1692 0 : skipMemAreaAddData( rStrm );
1693 0 : return true;
1694 : }
1695 :
1696 0 : bool OoxFormulaParserImpl::importMemFuncToken( SequenceInputStream& rStrm )
1697 : {
1698 0 : rStrm.skip( 2 );
1699 0 : return true;
1700 : }
1701 :
1702 0 : bool OoxFormulaParserImpl::importNameToken( SequenceInputStream& rStrm )
1703 : {
1704 0 : return pushBiff12Name( rStrm.readInt32() );
1705 : }
1706 :
1707 0 : bool OoxFormulaParserImpl::importNameXToken( SequenceInputStream& rStrm )
1708 : {
1709 0 : sal_Int32 nRefId = rStrm.readInt16();
1710 0 : sal_Int32 nNameId = rStrm.readInt32();
1711 0 : return pushBiff12ExtName( nRefId, nNameId );
1712 : }
1713 :
1714 0 : bool OoxFormulaParserImpl::importFuncToken( SequenceInputStream& rStrm )
1715 : {
1716 : sal_uInt16 nFuncId;
1717 0 : rStrm >> nFuncId;
1718 0 : return pushBiff12Function( nFuncId );
1719 : }
1720 :
1721 0 : bool OoxFormulaParserImpl::importFuncVarToken( SequenceInputStream& rStrm )
1722 : {
1723 : sal_uInt8 nParamCount;
1724 : sal_uInt16 nFuncId;
1725 0 : rStrm >> nParamCount >> nFuncId;
1726 0 : return pushBiff12Function( nFuncId, nParamCount );
1727 : }
1728 :
1729 0 : bool OoxFormulaParserImpl::importExpToken( SequenceInputStream& rStrm )
1730 : {
1731 0 : BinAddress aBaseAddr;
1732 0 : rStrm >> aBaseAddr.mnRow;
1733 0 : swapStreamPosition( rStrm );
1734 0 : rStrm >> aBaseAddr.mnCol;
1735 0 : swapStreamPosition( rStrm );
1736 0 : return pushSpecialTokenOperand( aBaseAddr, false );
1737 : }
1738 :
1739 0 : LinkSheetRange OoxFormulaParserImpl::readSheetRange( SequenceInputStream& rStrm )
1740 : {
1741 0 : return getExternalLinks().getSheetRange( rStrm.readInt16() );
1742 : }
1743 :
1744 0 : void OoxFormulaParserImpl::swapStreamPosition( SequenceInputStream& rStrm )
1745 : {
1746 0 : sal_Int64 nRecPos = rStrm.tell();
1747 0 : rStrm.seek( mnAddDataPos );
1748 0 : mnAddDataPos = nRecPos;
1749 0 : }
1750 :
1751 0 : void OoxFormulaParserImpl::skipMemAreaAddData( SequenceInputStream& rStrm )
1752 : {
1753 0 : swapStreamPosition( rStrm );
1754 0 : rStrm.skip( 16 * rStrm.readInt32() );
1755 0 : swapStreamPosition( rStrm );
1756 0 : }
1757 :
1758 : // convert BIN token and push API operand or operator -------------------------
1759 :
1760 0 : bool OoxFormulaParserImpl::pushBiff12Name( sal_Int32 nNameId )
1761 : {
1762 : // one-based in BIFF12 formulas
1763 0 : return pushDefinedNameOperand( getDefinedNames().getByIndex( nNameId - 1 ) );
1764 : }
1765 :
1766 0 : bool OoxFormulaParserImpl::pushBiff12ExtName( sal_Int32 nRefId, sal_Int32 nNameId )
1767 : {
1768 0 : if( const ExternalLink* pExtLink = getExternalLinks().getExternalLink( nRefId ).get() )
1769 : {
1770 0 : if( pExtLink->getLinkType() == LINKTYPE_SELF )
1771 0 : return pushBiff12Name( nNameId );
1772 : // external name indexes are one-based in BIFF12
1773 0 : ExternalNameRef xExtName = pExtLink->getNameByIndex( nNameId - 1 );
1774 0 : return pushExternalNameOperand( xExtName, *pExtLink );
1775 : }
1776 0 : return pushBiffErrorOperand( BIFF_ERR_NAME );
1777 : }
1778 :
1779 0 : bool OoxFormulaParserImpl::pushBiff12Function( sal_uInt16 nFuncId )
1780 : {
1781 0 : if( const FunctionInfo* pFuncInfo = getFuncInfoFromBiff12FuncId( nFuncId ) )
1782 0 : if( pFuncInfo->mnMinParamCount == pFuncInfo->mnMaxParamCount )
1783 0 : return pushFunctionOperator( *pFuncInfo, pFuncInfo->mnMinParamCount );
1784 0 : return pushFunctionOperator( OPCODE_NONAME, 0 );
1785 : }
1786 :
1787 0 : bool OoxFormulaParserImpl::pushBiff12Function( sal_uInt16 nFuncId, sal_uInt8 nParamCount )
1788 : {
1789 0 : if( getFlag( nFuncId, BIFF_TOK_FUNCVAR_CMD ) )
1790 0 : nParamCount &= BIFF_TOK_FUNCVAR_COUNTMASK;
1791 0 : if( const FunctionInfo* pFuncInfo = getFuncInfoFromBiff12FuncId( nFuncId ) )
1792 0 : return pushFunctionOperator( *pFuncInfo, nParamCount );
1793 0 : return pushFunctionOperator( OPCODE_NONAME, nParamCount );
1794 : }
1795 :
1796 : // BIFF parser implementation =================================================
1797 :
1798 : namespace {
1799 :
1800 : /** A natural language reference struct with relative flag. */
1801 : struct BiffNlr
1802 : {
1803 : sal_Int32 mnCol; /// Column index.
1804 : sal_Int32 mnRow; /// Row index.
1805 : bool mbRel; /// True = relative column/row reference.
1806 :
1807 : explicit BiffNlr();
1808 :
1809 : void readBiff8Data( BiffInputStream& rStrm );
1810 : };
1811 :
1812 0 : BiffNlr::BiffNlr() :
1813 : mnCol( 0 ),
1814 : mnRow( 0 ),
1815 0 : mbRel( false )
1816 : {
1817 0 : }
1818 :
1819 0 : void BiffNlr::readBiff8Data( BiffInputStream& rStrm )
1820 : {
1821 : sal_uInt16 nRow, nCol;
1822 0 : rStrm >> nRow >> nCol;
1823 0 : mnCol = nCol & BIFF_TOK_NLR_MASK;
1824 0 : mnRow = nRow;
1825 0 : mbRel = getFlag( nCol, BIFF_TOK_NLR_REL );
1826 0 : }
1827 :
1828 0 : bool lclIsValidNlrStack( const BinAddress& rAddr1, const BinAddress& rAddr2, bool bRow )
1829 : {
1830 : return bRow ?
1831 0 : ((rAddr1.mnRow == rAddr2.mnRow) && (rAddr1.mnCol + 1 == rAddr2.mnCol)) :
1832 0 : ((rAddr1.mnCol == rAddr2.mnCol) && (rAddr1.mnRow + 1 == rAddr2.mnRow));
1833 : }
1834 :
1835 0 : bool lclIsValidNlrRange( const BiffNlr& rNlr, const BinRange& rRange, bool bRow )
1836 : {
1837 : return bRow ?
1838 0 : ((rNlr.mnRow == rRange.maFirst.mnRow) && (rNlr.mnCol + 1 == rRange.maFirst.mnCol) && (rRange.maFirst.mnRow == rRange.maLast.mnRow)) :
1839 0 : ((rNlr.mnCol == rRange.maFirst.mnCol) && (rNlr.mnRow + 1 == rRange.maFirst.mnRow) && (rRange.maFirst.mnCol == rRange.maLast.mnCol));
1840 : }
1841 :
1842 : } // namespace
1843 :
1844 : // ----------------------------------------------------------------------------
1845 :
1846 0 : class BiffFormulaParserImpl : public FormulaParserImpl
1847 : {
1848 : public:
1849 : explicit BiffFormulaParserImpl( const FormulaParser& rParent );
1850 :
1851 : virtual ApiTokenSequence importBiffFormula(
1852 : const CellAddress& rBaseAddr,
1853 : FormulaType eType,
1854 : BiffInputStream& rStrm, const sal_uInt16* pnFmlaSize );
1855 :
1856 : private:
1857 : // import token contents and create API formula token ---------------------
1858 :
1859 : bool importTokenNotAvailable( BiffInputStream& rStrm );
1860 : bool importRefTokenNotAvailable( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
1861 : bool importStrToken2( BiffInputStream& rStrm );
1862 : bool importStrToken8( BiffInputStream& rStrm );
1863 : bool importAttrToken( BiffInputStream& rStrm );
1864 : bool importSpaceToken3( BiffInputStream& rStrm );
1865 : bool importSpaceToken4( BiffInputStream& rStrm );
1866 : bool importSheetToken2( BiffInputStream& rStrm );
1867 : bool importSheetToken3( BiffInputStream& rStrm );
1868 : bool importEndSheetToken2( BiffInputStream& rStrm );
1869 : bool importEndSheetToken3( BiffInputStream& rStrm );
1870 : bool importNlrToken( BiffInputStream& rStrm );
1871 : bool importArrayToken( BiffInputStream& rStrm );
1872 : bool importRefToken2( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
1873 : bool importRefToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
1874 : bool importAreaToken2( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
1875 : bool importAreaToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
1876 : bool importRef3dToken5( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
1877 : bool importRef3dToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
1878 : bool importArea3dToken5( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
1879 : bool importArea3dToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
1880 : bool importMemAreaToken( BiffInputStream& rStrm, bool bAddData );
1881 : bool importMemFuncToken( BiffInputStream& rStrm );
1882 : bool importNameToken( BiffInputStream& rStrm );
1883 : bool importNameXToken( BiffInputStream& rStrm );
1884 : bool importFuncToken2( BiffInputStream& rStrm );
1885 : bool importFuncToken4( BiffInputStream& rStrm );
1886 : bool importFuncVarToken2( BiffInputStream& rStrm );
1887 : bool importFuncVarToken4( BiffInputStream& rStrm );
1888 : bool importFuncCEToken( BiffInputStream& rStrm );
1889 : bool importExpToken( BiffInputStream& rStrm );
1890 : bool importTblToken( BiffInputStream& rStrm );
1891 :
1892 : bool importNlrAddrToken( BiffInputStream& rStrm, bool bRow );
1893 : bool importNlrRangeToken( BiffInputStream& rStrm );
1894 : bool importNlrSAddrToken( BiffInputStream& rStrm, bool bRow );
1895 : bool importNlrSRangeToken( BiffInputStream& rStrm );
1896 : bool importNlrErrToken( BiffInputStream& rStrm, sal_uInt16 nSkip );
1897 :
1898 : sal_Int32 readRefId( BiffInputStream& rStrm );
1899 : sal_uInt16 readNameId( BiffInputStream& rStrm );
1900 : LinkSheetRange readSheetRange5( BiffInputStream& rStrm );
1901 : LinkSheetRange readSheetRange8( BiffInputStream& rStrm );
1902 :
1903 : void swapStreamPosition( BiffInputStream& rStrm );
1904 : void skipMemAreaAddData( BiffInputStream& rStrm );
1905 : bool readNlrSAddrAddData( BiffNlr& orNlr, BiffInputStream& rStrm, bool bRow );
1906 : bool readNlrSRangeAddData( BiffNlr& orNlr, bool& orbIsRow, BiffInputStream& rStrm );
1907 :
1908 : // convert BIFF token and push API operand or operator --------------------
1909 :
1910 : bool pushBiffReference( const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset );
1911 : bool pushBiffReference( const BinComplexRef2d& rRef, bool bDeleted, bool bRelativeAsOffset );
1912 : bool pushBiffNlrAddr( const BiffNlr& rNlr, bool bRow );
1913 : bool pushBiffNlrRange( const BiffNlr& rNlr, const BinRange& rRange );
1914 : bool pushBiffNlrSAddr( const BiffNlr& rNlr, bool bRow );
1915 : bool pushBiffNlrSRange( const BiffNlr& rNlr, const BinRange& rRange, bool bRow );
1916 : bool pushBiffName( sal_uInt16 nNameId );
1917 : bool pushBiffExtName( sal_Int32 nRefId, sal_uInt16 nNameId );
1918 : bool pushBiffFunction( sal_uInt16 nFuncId );
1919 : bool pushBiffFunction( sal_uInt16 nFuncId, sal_uInt8 nParamCount );
1920 :
1921 : // ------------------------------------------------------------------------
1922 : private:
1923 : typedef bool (BiffFormulaParserImpl::*ImportTokenFunc)( BiffInputStream& );
1924 : typedef bool (BiffFormulaParserImpl::*ImportRefTokenFunc)( BiffInputStream&, bool, bool );
1925 :
1926 : ImportTokenFunc mpImportStrToken; /// Pointer to tStr import function (string constant).
1927 : ImportTokenFunc mpImportSpaceToken; /// Pointer to tAttrSpace import function (spaces/line breaks).
1928 : ImportTokenFunc mpImportSheetToken; /// Pointer to tSheet import function (external reference).
1929 : ImportTokenFunc mpImportEndSheetToken; /// Pointer to tEndSheet import function (end of external reference).
1930 : ImportTokenFunc mpImportNlrToken; /// Pointer to tNlr import function (natural language reference).
1931 : ImportRefTokenFunc mpImportRefToken; /// Pointer to tRef import function (2d cell reference).
1932 : ImportRefTokenFunc mpImportAreaToken; /// Pointer to tArea import function (2d area reference).
1933 : ImportRefTokenFunc mpImportRef3dToken; /// Pointer to tRef3d import function (3d cell reference).
1934 : ImportRefTokenFunc mpImportArea3dToken; /// Pointer to tArea3d import function (3d area reference).
1935 : ImportTokenFunc mpImportNameXToken; /// Pointer to tNameX import function (external name).
1936 : ImportTokenFunc mpImportFuncToken; /// Pointer to tFunc import function (function with fixed parameter count).
1937 : ImportTokenFunc mpImportFuncVarToken; /// Pointer to tFuncVar import function (function with variable parameter count).
1938 : ImportTokenFunc mpImportFuncCEToken; /// Pointer to tFuncCE import function (command macro call).
1939 : sal_Int64 mnAddDataPos; /// Current stream position for additional data (tArray, tMemArea, tNlr).
1940 : sal_Int32 mnCurrRefId; /// Current ref-id from tSheet token (BIFF2-BIFF4 only).
1941 : sal_uInt16 mnAttrDataSize; /// Size of one tAttr data element.
1942 : sal_uInt16 mnArraySize; /// Size of tArray data.
1943 : sal_uInt16 mnNameSize; /// Size of tName data.
1944 : sal_uInt16 mnMemAreaSize; /// Size of tMemArea data.
1945 : sal_uInt16 mnMemFuncSize; /// Size of tMemFunc data.
1946 : sal_uInt16 mnRefIdSize; /// Size of unused data following a reference identifier.
1947 : };
1948 :
1949 : // ----------------------------------------------------------------------------
1950 :
1951 0 : BiffFormulaParserImpl::BiffFormulaParserImpl( const FormulaParser& rParent ) :
1952 : FormulaParserImpl( rParent ),
1953 : mnAddDataPos( 0 ),
1954 0 : mnCurrRefId( 0 )
1955 : {
1956 0 : switch( getBiff() )
1957 : {
1958 : case BIFF2:
1959 0 : mpImportStrToken = &BiffFormulaParserImpl::importStrToken2;
1960 0 : mpImportSpaceToken = &BiffFormulaParserImpl::importTokenNotAvailable;
1961 0 : mpImportSheetToken = &BiffFormulaParserImpl::importSheetToken2;
1962 0 : mpImportEndSheetToken = &BiffFormulaParserImpl::importEndSheetToken2;
1963 0 : mpImportNlrToken = &BiffFormulaParserImpl::importTokenNotAvailable;
1964 0 : mpImportRefToken = &BiffFormulaParserImpl::importRefToken2;
1965 0 : mpImportAreaToken = &BiffFormulaParserImpl::importAreaToken2;
1966 0 : mpImportRef3dToken = &BiffFormulaParserImpl::importRefTokenNotAvailable;
1967 0 : mpImportArea3dToken = &BiffFormulaParserImpl::importRefTokenNotAvailable;
1968 0 : mpImportNameXToken = &BiffFormulaParserImpl::importTokenNotAvailable;
1969 0 : mpImportFuncToken = &BiffFormulaParserImpl::importFuncToken2;
1970 0 : mpImportFuncVarToken = &BiffFormulaParserImpl::importFuncVarToken2;
1971 0 : mpImportFuncCEToken = &BiffFormulaParserImpl::importFuncCEToken;
1972 0 : mnAttrDataSize = 1;
1973 0 : mnArraySize = 6;
1974 0 : mnNameSize = 5;
1975 0 : mnMemAreaSize = 4;
1976 0 : mnMemFuncSize = 1;
1977 0 : mnRefIdSize = 1;
1978 0 : break;
1979 : case BIFF3:
1980 0 : mpImportStrToken = &BiffFormulaParserImpl::importStrToken2;
1981 0 : mpImportSpaceToken = &BiffFormulaParserImpl::importSpaceToken3;
1982 0 : mpImportSheetToken = &BiffFormulaParserImpl::importSheetToken3;
1983 0 : mpImportEndSheetToken = &BiffFormulaParserImpl::importEndSheetToken3;
1984 0 : mpImportNlrToken = &BiffFormulaParserImpl::importTokenNotAvailable;
1985 0 : mpImportRefToken = &BiffFormulaParserImpl::importRefToken2;
1986 0 : mpImportAreaToken = &BiffFormulaParserImpl::importAreaToken2;
1987 0 : mpImportRef3dToken = &BiffFormulaParserImpl::importRefTokenNotAvailable;
1988 0 : mpImportArea3dToken = &BiffFormulaParserImpl::importRefTokenNotAvailable;
1989 0 : mpImportNameXToken = &BiffFormulaParserImpl::importTokenNotAvailable;
1990 0 : mpImportFuncToken = &BiffFormulaParserImpl::importFuncToken2;
1991 0 : mpImportFuncVarToken = &BiffFormulaParserImpl::importFuncVarToken2;
1992 0 : mpImportFuncCEToken = &BiffFormulaParserImpl::importFuncCEToken;
1993 0 : mnAttrDataSize = 2;
1994 0 : mnArraySize = 7;
1995 0 : mnNameSize = 8;
1996 0 : mnMemAreaSize = 6;
1997 0 : mnMemFuncSize = 2;
1998 0 : mnRefIdSize = 2;
1999 0 : break;
2000 : case BIFF4:
2001 0 : mpImportStrToken = &BiffFormulaParserImpl::importStrToken2;
2002 0 : mpImportSpaceToken = &BiffFormulaParserImpl::importSpaceToken4;
2003 0 : mpImportSheetToken = &BiffFormulaParserImpl::importSheetToken3;
2004 0 : mpImportEndSheetToken = &BiffFormulaParserImpl::importEndSheetToken3;
2005 0 : mpImportNlrToken = &BiffFormulaParserImpl::importTokenNotAvailable;
2006 0 : mpImportRefToken = &BiffFormulaParserImpl::importRefToken2;
2007 0 : mpImportAreaToken = &BiffFormulaParserImpl::importAreaToken2;
2008 0 : mpImportRef3dToken = &BiffFormulaParserImpl::importRefTokenNotAvailable;
2009 0 : mpImportArea3dToken = &BiffFormulaParserImpl::importRefTokenNotAvailable;
2010 0 : mpImportNameXToken = &BiffFormulaParserImpl::importTokenNotAvailable;
2011 0 : mpImportFuncToken = &BiffFormulaParserImpl::importFuncToken4;
2012 0 : mpImportFuncVarToken = &BiffFormulaParserImpl::importFuncVarToken4;
2013 0 : mpImportFuncCEToken = &BiffFormulaParserImpl::importTokenNotAvailable;
2014 0 : mnAttrDataSize = 2;
2015 0 : mnArraySize = 7;
2016 0 : mnNameSize = 8;
2017 0 : mnMemAreaSize = 6;
2018 0 : mnMemFuncSize = 2;
2019 0 : mnRefIdSize = 2;
2020 0 : break;
2021 : case BIFF5:
2022 0 : mpImportStrToken = &BiffFormulaParserImpl::importStrToken2;
2023 0 : mpImportSpaceToken = &BiffFormulaParserImpl::importSpaceToken4;
2024 0 : mpImportSheetToken = &BiffFormulaParserImpl::importTokenNotAvailable;
2025 0 : mpImportEndSheetToken = &BiffFormulaParserImpl::importTokenNotAvailable;
2026 0 : mpImportNlrToken = &BiffFormulaParserImpl::importTokenNotAvailable;
2027 0 : mpImportRefToken = &BiffFormulaParserImpl::importRefToken2;
2028 0 : mpImportAreaToken = &BiffFormulaParserImpl::importAreaToken2;
2029 0 : mpImportRef3dToken = &BiffFormulaParserImpl::importRef3dToken5;
2030 0 : mpImportArea3dToken = &BiffFormulaParserImpl::importArea3dToken5;
2031 0 : mpImportNameXToken = &BiffFormulaParserImpl::importNameXToken;
2032 0 : mpImportFuncToken = &BiffFormulaParserImpl::importFuncToken4;
2033 0 : mpImportFuncVarToken = &BiffFormulaParserImpl::importFuncVarToken4;
2034 0 : mpImportFuncCEToken = &BiffFormulaParserImpl::importTokenNotAvailable;
2035 0 : mnAttrDataSize = 2;
2036 0 : mnArraySize = 7;
2037 0 : mnNameSize = 12;
2038 0 : mnMemAreaSize = 6;
2039 0 : mnMemFuncSize = 2;
2040 0 : mnRefIdSize = 8;
2041 0 : break;
2042 : case BIFF8:
2043 0 : mpImportStrToken = &BiffFormulaParserImpl::importStrToken8;
2044 0 : mpImportSpaceToken = &BiffFormulaParserImpl::importSpaceToken4;
2045 0 : mpImportSheetToken = &BiffFormulaParserImpl::importTokenNotAvailable;
2046 0 : mpImportEndSheetToken = &BiffFormulaParserImpl::importTokenNotAvailable;
2047 0 : mpImportNlrToken = &BiffFormulaParserImpl::importNlrToken;
2048 0 : mpImportRefToken = &BiffFormulaParserImpl::importRefToken8;
2049 0 : mpImportAreaToken = &BiffFormulaParserImpl::importAreaToken8;
2050 0 : mpImportRef3dToken = &BiffFormulaParserImpl::importRef3dToken8;
2051 0 : mpImportArea3dToken = &BiffFormulaParserImpl::importArea3dToken8;
2052 0 : mpImportNameXToken = &BiffFormulaParserImpl::importNameXToken;
2053 0 : mpImportFuncToken = &BiffFormulaParserImpl::importFuncToken4;
2054 0 : mpImportFuncVarToken = &BiffFormulaParserImpl::importFuncVarToken4;
2055 0 : mpImportFuncCEToken = &BiffFormulaParserImpl::importTokenNotAvailable;
2056 0 : mnAttrDataSize = 2;
2057 0 : mnArraySize = 7;
2058 0 : mnNameSize = 2;
2059 0 : mnMemAreaSize = 6;
2060 0 : mnMemFuncSize = 2;
2061 0 : mnRefIdSize = 0;
2062 0 : break;
2063 0 : case BIFF_UNKNOWN: break;
2064 : }
2065 0 : }
2066 :
2067 0 : ApiTokenSequence BiffFormulaParserImpl::importBiffFormula( const CellAddress& rBaseAddr,
2068 : FormulaType eType, BiffInputStream& rStrm, const sal_uInt16* pnFmlaSize )
2069 : {
2070 0 : initializeImport( rBaseAddr, eType );
2071 0 : mnCurrRefId = 0;
2072 :
2073 0 : sal_uInt16 nFmlaSize = lclReadFmlaSize( rStrm, getBiff(), pnFmlaSize );
2074 0 : sal_Int64 nEndPos = mnAddDataPos = rStrm.tell() + nFmlaSize;
2075 :
2076 0 : bool bOk = true;
2077 0 : while( bOk && !rStrm.isEof() && (rStrm.tell() < nEndPos) )
2078 : {
2079 : sal_uInt8 nTokenId;
2080 0 : rStrm >> nTokenId;
2081 0 : sal_uInt8 nTokenClass = nTokenId & BIFF_TOKCLASS_MASK;
2082 0 : sal_uInt8 nBaseId = nTokenId & BIFF_TOKID_MASK;
2083 :
2084 0 : bOk = !getFlag( nTokenId, BIFF_TOKFLAG_INVALID );
2085 0 : if( bOk )
2086 : {
2087 0 : if( nTokenClass == BIFF_TOKCLASS_NONE )
2088 : {
2089 : // base tokens
2090 0 : switch( nBaseId )
2091 : {
2092 0 : case BIFF_TOKID_EXP: bOk = importExpToken( rStrm ); break;
2093 0 : case BIFF_TOKID_TBL: bOk = importTblToken( rStrm ); break;
2094 0 : case BIFF_TOKID_ADD: bOk = pushBinaryOperator( OPCODE_ADD ); break;
2095 0 : case BIFF_TOKID_SUB: bOk = pushBinaryOperator( OPCODE_SUB ); break;
2096 0 : case BIFF_TOKID_MUL: bOk = pushBinaryOperator( OPCODE_MULT ); break;
2097 0 : case BIFF_TOKID_DIV: bOk = pushBinaryOperator( OPCODE_DIV ); break;
2098 0 : case BIFF_TOKID_POWER: bOk = pushBinaryOperator( OPCODE_POWER ); break;
2099 0 : case BIFF_TOKID_CONCAT: bOk = pushBinaryOperator( OPCODE_CONCAT ); break;
2100 0 : case BIFF_TOKID_LT: bOk = pushBinaryOperator( OPCODE_LESS ); break;
2101 0 : case BIFF_TOKID_LE: bOk = pushBinaryOperator( OPCODE_LESS_EQUAL ); break;
2102 0 : case BIFF_TOKID_EQ: bOk = pushBinaryOperator( OPCODE_EQUAL ); break;
2103 0 : case BIFF_TOKID_GE: bOk = pushBinaryOperator( OPCODE_GREATER_EQUAL ); break;
2104 0 : case BIFF_TOKID_GT: bOk = pushBinaryOperator( OPCODE_GREATER ); break;
2105 0 : case BIFF_TOKID_NE: bOk = pushBinaryOperator( OPCODE_NOT_EQUAL ); break;
2106 0 : case BIFF_TOKID_ISECT: bOk = pushBinaryOperator( OPCODE_INTERSECT ); break;
2107 0 : case BIFF_TOKID_LIST: bOk = pushBinaryOperator( OPCODE_LIST ); break;
2108 0 : case BIFF_TOKID_RANGE: bOk = pushBinaryOperator( OPCODE_RANGE ); break;
2109 0 : case BIFF_TOKID_UPLUS: bOk = pushUnaryPreOperator( OPCODE_PLUS_SIGN ); break;
2110 0 : case BIFF_TOKID_UMINUS: bOk = pushUnaryPreOperator( OPCODE_MINUS_SIGN ); break;
2111 0 : case BIFF_TOKID_PERCENT: bOk = pushUnaryPostOperator( OPCODE_PERCENT ); break;
2112 0 : case BIFF_TOKID_PAREN: bOk = pushParenthesesOperator(); break;
2113 0 : case BIFF_TOKID_MISSARG: bOk = pushOperand( OPCODE_MISSING ); break;
2114 0 : case BIFF_TOKID_STR: bOk = (this->*mpImportStrToken)( rStrm ); break;
2115 0 : case BIFF_TOKID_NLR: bOk = (this->*mpImportNlrToken)( rStrm ); break;
2116 0 : case BIFF_TOKID_ATTR: bOk = importAttrToken( rStrm ); break;
2117 0 : case BIFF_TOKID_SHEET: bOk = (this->*mpImportSheetToken)( rStrm ); break;
2118 0 : case BIFF_TOKID_ENDSHEET: bOk = (this->*mpImportEndSheetToken)( rStrm ); break;
2119 0 : case BIFF_TOKID_ERR: bOk = pushBiffErrorOperand( rStrm.readuInt8() ); break;
2120 0 : case BIFF_TOKID_BOOL: bOk = pushBiffBoolOperand( rStrm.readuInt8() ); break;
2121 0 : case BIFF_TOKID_INT: bOk = pushValueOperand< double >( rStrm.readuInt16() ); break;
2122 0 : case BIFF_TOKID_NUM: bOk = pushValueOperand( rStrm.readDouble() ); break;
2123 0 : default: bOk = false;
2124 : }
2125 : }
2126 : else
2127 : {
2128 : // classified tokens
2129 0 : switch( nBaseId )
2130 : {
2131 0 : case BIFF_TOKID_ARRAY: bOk = importArrayToken( rStrm ); break;
2132 0 : case BIFF_TOKID_FUNC: bOk = (this->*mpImportFuncToken)( rStrm ); break;
2133 0 : case BIFF_TOKID_FUNCVAR: bOk = (this->*mpImportFuncVarToken)( rStrm ); break;
2134 0 : case BIFF_TOKID_NAME: bOk = importNameToken( rStrm ); break;
2135 0 : case BIFF_TOKID_REF: bOk = (this->*mpImportRefToken)( rStrm, false, false ); break;
2136 0 : case BIFF_TOKID_AREA: bOk = (this->*mpImportAreaToken)( rStrm, false, false ); break;
2137 0 : case BIFF_TOKID_MEMAREA: bOk = importMemAreaToken( rStrm, true ); break;
2138 0 : case BIFF_TOKID_MEMERR: bOk = importMemAreaToken( rStrm, false ); break;
2139 0 : case BIFF_TOKID_MEMNOMEM: bOk = importMemAreaToken( rStrm, false ); break;
2140 0 : case BIFF_TOKID_MEMFUNC: bOk = importMemFuncToken( rStrm ); break;
2141 0 : case BIFF_TOKID_REFERR: bOk = (this->*mpImportRefToken)( rStrm, true, false ); break;
2142 0 : case BIFF_TOKID_AREAERR: bOk = (this->*mpImportAreaToken)( rStrm, true, false ); break;
2143 0 : case BIFF_TOKID_REFN: bOk = (this->*mpImportRefToken)( rStrm, false, true ); break;
2144 0 : case BIFF_TOKID_AREAN: bOk = (this->*mpImportAreaToken)( rStrm, false, true ); break;
2145 0 : case BIFF_TOKID_MEMAREAN: bOk = importMemFuncToken( rStrm ); break;
2146 0 : case BIFF_TOKID_MEMNOMEMN: bOk = importMemFuncToken( rStrm ); break;
2147 0 : case BIFF_TOKID_FUNCCE: bOk = (this->*mpImportFuncCEToken)( rStrm ); break;
2148 0 : case BIFF_TOKID_NAMEX: bOk = (this->*mpImportNameXToken)( rStrm ); break;
2149 0 : case BIFF_TOKID_REF3D: bOk = (this->*mpImportRef3dToken)( rStrm, false, mbRelativeAsOffset ); break;
2150 0 : case BIFF_TOKID_AREA3D: bOk = (this->*mpImportArea3dToken)( rStrm, false, mbRelativeAsOffset ); break;
2151 0 : case BIFF_TOKID_REFERR3D: bOk = (this->*mpImportRef3dToken)( rStrm, true, mbRelativeAsOffset ); break;
2152 0 : case BIFF_TOKID_AREAERR3D: bOk = (this->*mpImportArea3dToken)( rStrm, true, mbRelativeAsOffset ); break;
2153 0 : default: bOk = false;
2154 : }
2155 : }
2156 : }
2157 : }
2158 :
2159 : // build and finalize the token sequence
2160 0 : ApiTokenSequence aFinalTokens;
2161 0 : if( bOk && (rStrm.tell() == nEndPos) )
2162 0 : aFinalTokens = finalizeImport();
2163 :
2164 : // seek behind additional token data of tArray, tMemArea, tNlr tokens
2165 0 : rStrm.seek( mnAddDataPos );
2166 :
2167 : // return the final token sequence
2168 0 : return aFinalTokens;
2169 : }
2170 :
2171 : // import token contents and create API formula token -------------------------
2172 :
2173 0 : bool BiffFormulaParserImpl::importTokenNotAvailable( BiffInputStream& )
2174 : {
2175 : // dummy function for pointer-to-member-function
2176 0 : return false;
2177 : }
2178 :
2179 0 : bool BiffFormulaParserImpl::importRefTokenNotAvailable( BiffInputStream&, bool, bool )
2180 : {
2181 : // dummy function for pointer-to-member-function
2182 0 : return false;
2183 : }
2184 :
2185 0 : bool BiffFormulaParserImpl::importStrToken2( BiffInputStream& rStrm )
2186 : {
2187 0 : return pushValueOperand( rStrm.readByteStringUC( false, getTextEncoding(), mbAllowNulChars ) );
2188 : }
2189 :
2190 0 : bool BiffFormulaParserImpl::importStrToken8( BiffInputStream& rStrm )
2191 : {
2192 : // read flags field for empty strings also
2193 0 : return pushValueOperand( rStrm.readUniStringBody( rStrm.readuInt8(), mbAllowNulChars ) );
2194 : }
2195 :
2196 0 : bool BiffFormulaParserImpl::importAttrToken( BiffInputStream& rStrm )
2197 : {
2198 0 : bool bOk = true;
2199 : sal_uInt8 nType;
2200 0 : rStrm >> nType;
2201 0 : switch( nType )
2202 : {
2203 : case 0: // sometimes, tAttrSkip tokens miss the type flag
2204 : case BIFF_TOK_ATTR_VOLATILE:
2205 : case BIFF_TOK_ATTR_IF:
2206 : case BIFF_TOK_ATTR_SKIP:
2207 : case BIFF_TOK_ATTR_ASSIGN:
2208 0 : rStrm.skip( mnAttrDataSize );
2209 0 : break;
2210 : case BIFF_TOK_ATTR_CHOOSE:
2211 0 : rStrm.skip( mnAttrDataSize * (1 + ((getBiff() == BIFF2) ? rStrm.readuInt8() : rStrm.readuInt16())) );
2212 0 : break;
2213 : case BIFF_TOK_ATTR_SUM:
2214 0 : rStrm.skip( mnAttrDataSize );
2215 0 : bOk = pushBiffFunction( BIFF_FUNC_SUM, 1 );
2216 0 : break;
2217 : case BIFF_TOK_ATTR_SPACE:
2218 : case BIFF_TOK_ATTR_SPACE_VOLATILE:
2219 0 : bOk = (this->*mpImportSpaceToken)( rStrm );
2220 0 : break;
2221 : default:
2222 0 : bOk = false;
2223 : }
2224 0 : return bOk;
2225 : }
2226 :
2227 0 : bool BiffFormulaParserImpl::importSpaceToken3( BiffInputStream& rStrm )
2228 : {
2229 0 : rStrm.skip( 2 );
2230 0 : return true;
2231 : }
2232 :
2233 0 : bool BiffFormulaParserImpl::importSpaceToken4( BiffInputStream& rStrm )
2234 : {
2235 : sal_uInt8 nType, nCount;
2236 0 : rStrm >> nType >> nCount;
2237 0 : switch( nType )
2238 : {
2239 : case BIFF_TOK_ATTR_SPACE_SP:
2240 0 : appendLeadingSpaces( nCount, false );
2241 0 : break;
2242 : case BIFF_TOK_ATTR_SPACE_BR:
2243 0 : appendLeadingSpaces( nCount, true );
2244 0 : break;
2245 : case BIFF_TOK_ATTR_SPACE_SP_OPEN:
2246 0 : appendOpeningSpaces( nCount, false );
2247 0 : break;
2248 : case BIFF_TOK_ATTR_SPACE_BR_OPEN:
2249 0 : appendOpeningSpaces( nCount, true );
2250 0 : break;
2251 : case BIFF_TOK_ATTR_SPACE_SP_CLOSE:
2252 0 : appendClosingSpaces( nCount, false );
2253 0 : break;
2254 : case BIFF_TOK_ATTR_SPACE_BR_CLOSE:
2255 0 : appendClosingSpaces( nCount, true );
2256 0 : break;
2257 : }
2258 0 : return true;
2259 : }
2260 :
2261 0 : bool BiffFormulaParserImpl::importSheetToken2( BiffInputStream& rStrm )
2262 : {
2263 0 : rStrm.skip( 4 );
2264 0 : mnCurrRefId = readRefId( rStrm );
2265 0 : return true;
2266 : }
2267 :
2268 0 : bool BiffFormulaParserImpl::importSheetToken3( BiffInputStream& rStrm )
2269 : {
2270 0 : rStrm.skip( 6 );
2271 0 : mnCurrRefId = readRefId( rStrm );
2272 0 : return true;
2273 : }
2274 :
2275 0 : bool BiffFormulaParserImpl::importEndSheetToken2( BiffInputStream& rStrm )
2276 : {
2277 0 : rStrm.skip( 3 );
2278 0 : mnCurrRefId = 0;
2279 0 : return true;
2280 : }
2281 :
2282 0 : bool BiffFormulaParserImpl::importEndSheetToken3( BiffInputStream& rStrm )
2283 : {
2284 0 : rStrm.skip( 4 );
2285 0 : mnCurrRefId = 0;
2286 0 : return true;
2287 : }
2288 :
2289 0 : bool BiffFormulaParserImpl::importNlrToken( BiffInputStream& rStrm )
2290 : {
2291 0 : bool bOk = true;
2292 : sal_uInt8 nNlrType;
2293 0 : rStrm >> nNlrType;
2294 0 : switch( nNlrType )
2295 : {
2296 0 : case BIFF_TOK_NLR_ERR: bOk = importNlrErrToken( rStrm, 4 ); break;
2297 0 : case BIFF_TOK_NLR_ROWR: bOk = importNlrAddrToken( rStrm, true ); break;
2298 0 : case BIFF_TOK_NLR_COLR: bOk = importNlrAddrToken( rStrm, false ); break;
2299 0 : case BIFF_TOK_NLR_ROWV: bOk = importNlrAddrToken( rStrm, true ); break;
2300 0 : case BIFF_TOK_NLR_COLV: bOk = importNlrAddrToken( rStrm, false ); break;
2301 0 : case BIFF_TOK_NLR_RANGE: bOk = importNlrRangeToken( rStrm ); break;
2302 0 : case BIFF_TOK_NLR_SRANGE: bOk = importNlrSRangeToken( rStrm ); break;
2303 0 : case BIFF_TOK_NLR_SROWR: bOk = importNlrSAddrToken( rStrm, true ); break;
2304 0 : case BIFF_TOK_NLR_SCOLR: bOk = importNlrSAddrToken( rStrm, false ); break;
2305 0 : case BIFF_TOK_NLR_SROWV: bOk = importNlrSAddrToken( rStrm, true ); break;
2306 0 : case BIFF_TOK_NLR_SCOLV: bOk = importNlrSAddrToken( rStrm, false ); break;
2307 0 : case BIFF_TOK_NLR_RANGEERR: bOk = importNlrErrToken( rStrm, 13 ); break;
2308 0 : case BIFF_TOK_NLR_SXNAME: bOk = importNlrErrToken( rStrm, 4 ); break;
2309 0 : default: bOk = false;
2310 : }
2311 0 : return bOk;
2312 : }
2313 :
2314 0 : bool BiffFormulaParserImpl::importArrayToken( BiffInputStream& rStrm )
2315 : {
2316 0 : rStrm.skip( mnArraySize );
2317 :
2318 : // start token array with opening brace and leading spaces
2319 0 : pushOperand( OPCODE_ARRAY_OPEN );
2320 0 : size_t nOpSize = popOperandSize();
2321 0 : size_t nOldArraySize = getFormulaSize();
2322 0 : bool bBiff8 = getBiff() == BIFF8;
2323 :
2324 : // read array size
2325 0 : swapStreamPosition( rStrm );
2326 0 : sal_uInt16 nCols = rStrm.readuInt8();
2327 0 : sal_uInt16 nRows = rStrm.readuInt16();
2328 0 : if( bBiff8 ) { ++nCols; ++nRows; } else if( nCols == 0 ) nCols = 256;
2329 : OSL_ENSURE( (nCols > 0) && (nRows > 0), "BiffFormulaParserImpl::importArrayToken - empty array" );
2330 :
2331 : // read array values and build token array
2332 0 : for( sal_uInt16 nRow = 0; !rStrm.isEof() && (nRow < nRows); ++nRow )
2333 : {
2334 0 : if( nRow > 0 )
2335 0 : appendRawToken( OPCODE_ARRAY_ROWSEP );
2336 0 : for( sal_uInt16 nCol = 0; !rStrm.isEof() && (nCol < nCols); ++nCol )
2337 : {
2338 0 : if( nCol > 0 )
2339 0 : appendRawToken( OPCODE_ARRAY_COLSEP );
2340 0 : switch( rStrm.readuInt8() )
2341 : {
2342 : case BIFF_DATATYPE_EMPTY:
2343 0 : appendRawToken( OPCODE_PUSH ) <<= OUString();
2344 0 : rStrm.skip( 8 );
2345 0 : break;
2346 : case BIFF_DATATYPE_DOUBLE:
2347 0 : appendRawToken( OPCODE_PUSH ) <<= rStrm.readDouble();
2348 0 : break;
2349 : case BIFF_DATATYPE_STRING:
2350 0 : appendRawToken( OPCODE_PUSH ) <<= bBiff8 ?
2351 : rStrm.readUniString( mbAllowNulChars ) :
2352 0 : rStrm.readByteStringUC( false, getTextEncoding(), mbAllowNulChars );
2353 0 : break;
2354 : case BIFF_DATATYPE_BOOL:
2355 0 : appendRawToken( OPCODE_PUSH ) <<= (static_cast< double >( (rStrm.readuInt8() == BIFF_TOK_BOOL_FALSE) ? 0.0 : 1.0 ));
2356 0 : rStrm.skip( 7 );
2357 0 : break;
2358 : case BIFF_DATATYPE_ERROR:
2359 0 : appendRawToken( OPCODE_PUSH ) <<= BiffHelper::calcDoubleFromError( rStrm.readuInt8() );
2360 0 : rStrm.skip( 7 );
2361 0 : break;
2362 : default:
2363 : OSL_FAIL( "BiffFormulaParserImpl::importArrayToken - unknown data type" );
2364 0 : appendRawToken( OPCODE_PUSH ) <<= BiffHelper::calcDoubleFromError( BIFF_ERR_NA );
2365 : }
2366 : }
2367 : }
2368 0 : swapStreamPosition( rStrm );
2369 :
2370 : // close token array and set resulting operand size
2371 0 : appendRawToken( OPCODE_ARRAY_CLOSE );
2372 0 : pushOperandSize( nOpSize + getFormulaSize() - nOldArraySize );
2373 0 : return true;
2374 : }
2375 :
2376 0 : bool BiffFormulaParserImpl::importRefToken2( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
2377 : {
2378 0 : BinSingleRef2d aRef;
2379 0 : aRef.readBiff2Data( rStrm, bRelativeAsOffset );
2380 0 : return pushBiffReference( aRef, bDeleted, bRelativeAsOffset );
2381 : }
2382 :
2383 0 : bool BiffFormulaParserImpl::importRefToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
2384 : {
2385 0 : BinSingleRef2d aRef;
2386 0 : aRef.readBiff8Data( rStrm, bRelativeAsOffset );
2387 0 : return pushBiffReference( aRef, bDeleted, bRelativeAsOffset );
2388 : }
2389 :
2390 0 : bool BiffFormulaParserImpl::importAreaToken2( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
2391 : {
2392 0 : BinComplexRef2d aRef;
2393 0 : aRef.readBiff2Data( rStrm, bRelativeAsOffset );
2394 0 : return pushBiffReference( aRef, bDeleted, bRelativeAsOffset );
2395 : }
2396 :
2397 0 : bool BiffFormulaParserImpl::importAreaToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
2398 : {
2399 0 : BinComplexRef2d aRef;
2400 0 : aRef.readBiff8Data( rStrm, bRelativeAsOffset );
2401 0 : return pushBiffReference( aRef, bDeleted, bRelativeAsOffset );
2402 : }
2403 :
2404 0 : bool BiffFormulaParserImpl::importRef3dToken5( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
2405 : {
2406 0 : LinkSheetRange aSheetRange = readSheetRange5( rStrm );
2407 0 : BinSingleRef2d aRef;
2408 0 : aRef.readBiff2Data( rStrm, bRelativeAsOffset );
2409 0 : return pushReferenceOperand( aSheetRange, aRef, bDeleted, bRelativeAsOffset );
2410 : }
2411 :
2412 0 : bool BiffFormulaParserImpl::importRef3dToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
2413 : {
2414 0 : LinkSheetRange aSheetRange = readSheetRange8( rStrm );
2415 0 : BinSingleRef2d aRef;
2416 0 : aRef.readBiff8Data( rStrm, bRelativeAsOffset );
2417 0 : return pushReferenceOperand( aSheetRange, aRef, bDeleted, bRelativeAsOffset );
2418 : }
2419 :
2420 0 : bool BiffFormulaParserImpl::importArea3dToken5( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
2421 : {
2422 0 : LinkSheetRange aSheetRange = readSheetRange5( rStrm );
2423 0 : BinComplexRef2d aRef;
2424 0 : aRef.readBiff2Data( rStrm, bRelativeAsOffset );
2425 0 : return pushReferenceOperand( aSheetRange, aRef, bDeleted, bRelativeAsOffset );
2426 : }
2427 :
2428 0 : bool BiffFormulaParserImpl::importArea3dToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
2429 : {
2430 0 : LinkSheetRange aSheetRange = readSheetRange8( rStrm );
2431 0 : BinComplexRef2d aRef;
2432 0 : aRef.readBiff8Data( rStrm, bRelativeAsOffset );
2433 0 : return pushReferenceOperand( aSheetRange, aRef, bDeleted, bRelativeAsOffset );
2434 : }
2435 :
2436 0 : bool BiffFormulaParserImpl::importMemAreaToken( BiffInputStream& rStrm, bool bAddData )
2437 : {
2438 0 : rStrm.skip( mnMemAreaSize );
2439 0 : if( bAddData )
2440 0 : skipMemAreaAddData( rStrm );
2441 0 : return true;
2442 : }
2443 :
2444 0 : bool BiffFormulaParserImpl::importMemFuncToken( BiffInputStream& rStrm )
2445 : {
2446 0 : rStrm.skip( mnMemFuncSize );
2447 0 : return true;
2448 : }
2449 :
2450 0 : bool BiffFormulaParserImpl::importNameToken( BiffInputStream& rStrm )
2451 : {
2452 0 : sal_uInt16 nNameId = readNameId( rStrm );
2453 0 : return (mnCurrRefId > 0) ? pushBiffExtName( mnCurrRefId, nNameId ) : pushBiffName( nNameId );
2454 : }
2455 :
2456 0 : bool BiffFormulaParserImpl::importNameXToken( BiffInputStream& rStrm )
2457 : {
2458 0 : sal_Int32 nRefId = readRefId( rStrm );
2459 0 : sal_uInt16 nNameId = readNameId( rStrm );
2460 0 : return pushBiffExtName( nRefId, nNameId );
2461 : }
2462 :
2463 0 : bool BiffFormulaParserImpl::importFuncToken2( BiffInputStream& rStrm )
2464 : {
2465 : sal_uInt8 nFuncId;
2466 0 : rStrm >> nFuncId;
2467 0 : return pushBiffFunction( nFuncId );
2468 : }
2469 :
2470 0 : bool BiffFormulaParserImpl::importFuncToken4( BiffInputStream& rStrm )
2471 : {
2472 : sal_uInt16 nFuncId;
2473 0 : rStrm >> nFuncId;
2474 0 : return pushBiffFunction( nFuncId );
2475 : }
2476 :
2477 0 : bool BiffFormulaParserImpl::importFuncVarToken2( BiffInputStream& rStrm )
2478 : {
2479 : sal_uInt8 nParamCount, nFuncId;
2480 0 : rStrm >> nParamCount >> nFuncId;
2481 0 : return pushBiffFunction( nFuncId, nParamCount );
2482 : }
2483 :
2484 0 : bool BiffFormulaParserImpl::importFuncVarToken4( BiffInputStream& rStrm )
2485 : {
2486 : sal_uInt8 nParamCount;
2487 : sal_uInt16 nFuncId;
2488 0 : rStrm >> nParamCount >> nFuncId;
2489 0 : return pushBiffFunction( nFuncId, nParamCount & BIFF_TOK_FUNCVAR_COUNTMASK );
2490 : }
2491 :
2492 0 : bool BiffFormulaParserImpl::importFuncCEToken( BiffInputStream& rStrm )
2493 : {
2494 : sal_uInt8 nParamCount, nFuncId;
2495 0 : rStrm >> nParamCount >> nFuncId;
2496 0 : sal_uInt16 nCmdId = nFuncId;
2497 0 : setFlag( nCmdId, BIFF_TOK_FUNCVAR_CMD );
2498 0 : return pushBiffFunction( nCmdId, nParamCount );
2499 : }
2500 :
2501 0 : bool BiffFormulaParserImpl::importExpToken( BiffInputStream& rStrm )
2502 : {
2503 0 : BinAddress aBaseAddr;
2504 0 : aBaseAddr.read( rStrm );
2505 0 : return pushSpecialTokenOperand( aBaseAddr, false );
2506 : }
2507 :
2508 0 : bool BiffFormulaParserImpl::importTblToken( BiffInputStream& rStrm )
2509 : {
2510 0 : BinAddress aBaseAddr;
2511 0 : aBaseAddr.read( rStrm );
2512 0 : return pushSpecialTokenOperand( aBaseAddr, true );
2513 : }
2514 :
2515 0 : bool BiffFormulaParserImpl::importNlrAddrToken( BiffInputStream& rStrm, bool bRow )
2516 : {
2517 0 : BiffNlr aNlr;
2518 0 : aNlr.readBiff8Data( rStrm );
2519 0 : return pushBiffNlrAddr( aNlr, bRow );
2520 : }
2521 :
2522 0 : bool BiffFormulaParserImpl::importNlrRangeToken( BiffInputStream& rStrm )
2523 : {
2524 0 : BiffNlr aNlr;
2525 0 : aNlr.readBiff8Data( rStrm );
2526 0 : rStrm.skip( 1 );
2527 0 : BinRange aRange;
2528 0 : rStrm >> aRange;
2529 0 : return pushBiffNlrRange( aNlr, aRange );
2530 : }
2531 :
2532 0 : bool BiffFormulaParserImpl::importNlrSAddrToken( BiffInputStream& rStrm, bool bRow )
2533 : {
2534 0 : rStrm.skip( 4 );
2535 0 : BiffNlr aNlr;
2536 0 : return readNlrSAddrAddData( aNlr, rStrm, bRow ) ? pushBiffNlrSAddr( aNlr, bRow ) : pushBiffErrorOperand( BIFF_ERR_REF );
2537 : }
2538 :
2539 0 : bool BiffFormulaParserImpl::importNlrSRangeToken( BiffInputStream& rStrm )
2540 : {
2541 0 : rStrm.skip( 5 );
2542 0 : BinRange aRange;
2543 0 : rStrm >> aRange;
2544 0 : BiffNlr aNlr;
2545 : bool bRow;
2546 0 : return readNlrSRangeAddData( aNlr, bRow, rStrm ) ? pushBiffNlrSRange( aNlr, aRange, bRow ) : pushBiffErrorOperand( BIFF_ERR_REF );
2547 : }
2548 :
2549 0 : bool BiffFormulaParserImpl::importNlrErrToken( BiffInputStream& rStrm, sal_uInt16 nIgnore )
2550 : {
2551 0 : rStrm.skip( nIgnore );
2552 0 : return pushBiffErrorOperand( BIFF_ERR_NAME );
2553 : }
2554 :
2555 0 : sal_Int32 BiffFormulaParserImpl::readRefId( BiffInputStream& rStrm )
2556 : {
2557 : sal_Int16 nRefId;
2558 0 : rStrm >> nRefId;
2559 0 : rStrm.skip( mnRefIdSize );
2560 0 : return nRefId;
2561 : }
2562 :
2563 0 : sal_uInt16 BiffFormulaParserImpl::readNameId( BiffInputStream& rStrm )
2564 : {
2565 : sal_uInt16 nNameId;
2566 0 : rStrm >> nNameId;
2567 0 : rStrm.skip( mnNameSize );
2568 0 : return nNameId;
2569 : }
2570 :
2571 0 : LinkSheetRange BiffFormulaParserImpl::readSheetRange5( BiffInputStream& rStrm )
2572 : {
2573 0 : sal_Int32 nRefId = readRefId( rStrm );
2574 : sal_Int16 nTab1, nTab2;
2575 0 : rStrm >> nTab1 >> nTab2;
2576 0 : return getExternalLinks().getSheetRange( nRefId, nTab1, nTab2 );
2577 : }
2578 :
2579 0 : LinkSheetRange BiffFormulaParserImpl::readSheetRange8( BiffInputStream& rStrm )
2580 : {
2581 0 : return getExternalLinks().getSheetRange( readRefId( rStrm ) );
2582 : }
2583 :
2584 0 : void BiffFormulaParserImpl::swapStreamPosition( BiffInputStream& rStrm )
2585 : {
2586 0 : sal_Int64 nRecPos = rStrm.tell();
2587 0 : rStrm.seek( mnAddDataPos );
2588 0 : mnAddDataPos = nRecPos;
2589 0 : }
2590 :
2591 0 : void BiffFormulaParserImpl::skipMemAreaAddData( BiffInputStream& rStrm )
2592 : {
2593 0 : swapStreamPosition( rStrm );
2594 0 : sal_Int32 nCount = rStrm.readuInt16();
2595 0 : rStrm.skip( ((getBiff() == BIFF8) ? 8 : 6) * nCount );
2596 0 : swapStreamPosition( rStrm );
2597 0 : }
2598 :
2599 0 : bool BiffFormulaParserImpl::readNlrSAddrAddData( BiffNlr& orNlr, BiffInputStream& rStrm, bool bRow )
2600 : {
2601 0 : bool bIsRow = false;
2602 0 : return readNlrSRangeAddData( orNlr, bIsRow, rStrm ) && (bIsRow == bRow);
2603 : }
2604 :
2605 0 : bool BiffFormulaParserImpl::readNlrSRangeAddData( BiffNlr& orNlr, bool& orbIsRow, BiffInputStream& rStrm )
2606 : {
2607 0 : orbIsRow = false;
2608 0 : swapStreamPosition( rStrm );
2609 : // read number of cell addresses and relative flag
2610 : sal_uInt32 nCount;
2611 0 : rStrm >> nCount;
2612 0 : bool bRel = getFlag( nCount, BIFF_TOK_NLR_ADDREL );
2613 0 : nCount &= BIFF_TOK_NLR_ADDMASK;
2614 0 : sal_Int64 nEndPos = rStrm.tell() + 4 * nCount;
2615 : // read list of cell addresses
2616 0 : bool bValid = false;
2617 0 : if( nCount >= 2 )
2618 : {
2619 : // detect column/row orientation
2620 0 : BinAddress aAddr1, aAddr2;
2621 0 : rStrm >> aAddr1 >> aAddr2;
2622 0 : orbIsRow = aAddr1.mnRow == aAddr2.mnRow;
2623 0 : bValid = lclIsValidNlrStack( aAddr1, aAddr2, orbIsRow );
2624 : // read and verify additional cell positions
2625 0 : for( sal_uInt32 nIndex = 2; bValid && (nIndex < nCount); ++nIndex )
2626 : {
2627 0 : aAddr1 = aAddr2;
2628 0 : rStrm >> aAddr2;
2629 0 : bValid = !rStrm.isEof() && lclIsValidNlrStack( aAddr1, aAddr2, orbIsRow );
2630 : }
2631 : // check that last imported position (aAddr2) is not at the end of the sheet
2632 0 : bValid = bValid && (orbIsRow ? (aAddr2.mnCol < mnMaxApiCol) : (aAddr2.mnRow < mnMaxApiRow));
2633 : // fill the NLR struct with the last imported position
2634 0 : if( bValid )
2635 : {
2636 0 : orNlr.mnCol = aAddr2.mnCol;
2637 0 : orNlr.mnRow = aAddr2.mnRow;
2638 0 : orNlr.mbRel = bRel;
2639 : }
2640 : }
2641 : // seek to end of additional data for this token
2642 0 : rStrm.seek( nEndPos );
2643 0 : swapStreamPosition( rStrm );
2644 :
2645 0 : return bValid;
2646 : }
2647 :
2648 : // convert BIFF token and push API operand or operator ------------------------
2649 :
2650 0 : bool BiffFormulaParserImpl::pushBiffReference( const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset )
2651 : {
2652 0 : return (mnCurrRefId > 0) ?
2653 0 : pushReferenceOperand( getExternalLinks().getSheetRange( mnCurrRefId, 0, 0 ), rRef, bDeleted, bRelativeAsOffset ) :
2654 0 : pushReferenceOperand( rRef, bDeleted, bRelativeAsOffset );
2655 : }
2656 :
2657 0 : bool BiffFormulaParserImpl::pushBiffReference( const BinComplexRef2d& rRef, bool bDeleted, bool bRelativeAsOffset )
2658 : {
2659 0 : return (mnCurrRefId > 0) ?
2660 0 : pushReferenceOperand( getExternalLinks().getSheetRange( mnCurrRefId, 0, 0 ), rRef, bDeleted, bRelativeAsOffset ) :
2661 0 : pushReferenceOperand( rRef, bDeleted, bRelativeAsOffset );
2662 : }
2663 :
2664 0 : bool BiffFormulaParserImpl::pushBiffNlrAddr( const BiffNlr& rNlr, bool bRow )
2665 : {
2666 0 : BinSingleRef2d aRef;
2667 0 : aRef.mnCol = rNlr.mnCol;
2668 0 : aRef.mnRow = rNlr.mnRow;
2669 0 : aRef.mbColRel = !bRow;
2670 0 : aRef.mbRowRel = bRow;
2671 0 : return pushNlrOperand( aRef );
2672 : }
2673 :
2674 0 : bool BiffFormulaParserImpl::pushBiffNlrRange( const BiffNlr& rNlr, const BinRange& rRange )
2675 : {
2676 0 : bool bRow = rNlr.mnRow == rRange.maFirst.mnRow;
2677 0 : return lclIsValidNlrRange( rNlr, rRange, bRow ) ?
2678 0 : pushBiffNlrAddr( rNlr, bRow ) : pushBiffErrorOperand( BIFF_ERR_REF );
2679 : }
2680 :
2681 0 : bool BiffFormulaParserImpl::pushBiffNlrSAddr( const BiffNlr& rNlr, bool bRow )
2682 : {
2683 0 : BinRange aRange;
2684 0 : aRange.maFirst.mnCol = rNlr.mnCol + (bRow ? 1 : 0);
2685 0 : aRange.maFirst.mnRow = rNlr.mnRow + (bRow ? 0 : 1);
2686 0 : aRange.maLast.mnCol = bRow ? mnMaxApiCol : rNlr.mnCol;
2687 0 : aRange.maLast.mnRow = bRow ? rNlr.mnRow : mnMaxApiRow;
2688 0 : return pushBiffNlrSRange( rNlr, aRange, bRow );
2689 : }
2690 :
2691 0 : bool BiffFormulaParserImpl::pushBiffNlrSRange( const BiffNlr& rNlr, const BinRange& rRange, bool bRow )
2692 : {
2693 0 : if( lclIsValidNlrRange( rNlr, rRange, bRow ) )
2694 : {
2695 0 : BinComplexRef2d aRef;
2696 0 : aRef.maRef1.mnCol = rRange.maFirst.mnCol;
2697 0 : aRef.maRef1.mnRow = rRange.maFirst.mnRow;
2698 0 : aRef.maRef2.mnCol = rRange.maLast.mnCol;
2699 0 : aRef.maRef2.mnRow = rRange.maLast.mnRow;
2700 0 : aRef.maRef1.mbColRel = aRef.maRef2.mbColRel = !bRow && rNlr.mbRel;
2701 0 : aRef.maRef1.mbRowRel = aRef.maRef2.mbRowRel = bRow && rNlr.mbRel;
2702 0 : return pushReferenceOperand( aRef, false, false );
2703 : }
2704 0 : return pushBiffErrorOperand( BIFF_ERR_REF );
2705 : }
2706 :
2707 0 : bool BiffFormulaParserImpl::pushBiffName( sal_uInt16 nNameId )
2708 : {
2709 : // one-based in BIFF formulas
2710 0 : return pushDefinedNameOperand( getDefinedNames().getByIndex( static_cast< sal_Int32 >( nNameId ) - 1 ) );
2711 : }
2712 :
2713 0 : bool BiffFormulaParserImpl::pushBiffExtName( sal_Int32 nRefId, sal_uInt16 nNameId )
2714 : {
2715 0 : if( const ExternalLink* pExtLink = getExternalLinks().getExternalLink( nRefId ).get() )
2716 : {
2717 0 : if( pExtLink->getLinkType() == LINKTYPE_SELF )
2718 0 : return pushBiffName( nNameId );
2719 : // external name indexes are one-based in BIFF
2720 0 : ExternalNameRef xExtName = pExtLink->getNameByIndex( static_cast< sal_Int32 >( nNameId ) - 1 );
2721 0 : return pushExternalNameOperand( xExtName, *pExtLink );
2722 : }
2723 0 : return pushBiffErrorOperand( BIFF_ERR_NAME );
2724 : }
2725 :
2726 0 : bool BiffFormulaParserImpl::pushBiffFunction( sal_uInt16 nFuncId )
2727 : {
2728 0 : if( const FunctionInfo* pFuncInfo = getFuncInfoFromBiffFuncId( nFuncId ) )
2729 0 : if( pFuncInfo->mnMinParamCount == pFuncInfo->mnMaxParamCount )
2730 0 : return pushFunctionOperator( *pFuncInfo, pFuncInfo->mnMinParamCount );
2731 0 : return pushFunctionOperator( OPCODE_NONAME, 0 );
2732 : }
2733 :
2734 0 : bool BiffFormulaParserImpl::pushBiffFunction( sal_uInt16 nFuncId, sal_uInt8 nParamCount )
2735 : {
2736 0 : if( getFlag( nFuncId, BIFF_TOK_FUNCVAR_CMD ) )
2737 0 : nParamCount &= BIFF_TOK_FUNCVAR_COUNTMASK;
2738 0 : if( const FunctionInfo* pFuncInfo = getFuncInfoFromBiffFuncId( nFuncId ) )
2739 0 : return pushFunctionOperator( *pFuncInfo, nParamCount );
2740 0 : return pushFunctionOperator( OPCODE_NONAME, nParamCount );
2741 : }
2742 :
2743 : // ============================================================================
2744 :
2745 : namespace {
2746 :
2747 : /** Extracts the reference identifier and the remaining data from a formula in
2748 : the format '[RefID]Remaining'. */
2749 0 : bool lclExtractRefId( sal_Int32& rnRefId, OUString& rRemainder, const OUString& rFormulaString )
2750 : {
2751 0 : if( (rFormulaString.getLength() >= 4) && (rFormulaString[ 0 ] == '[') )
2752 : {
2753 0 : sal_Int32 nBracketClose = rFormulaString.indexOf( ']', 1 );
2754 0 : if( nBracketClose >= 2 )
2755 : {
2756 0 : rnRefId = rFormulaString.copy( 1, nBracketClose - 1 ).toInt32();
2757 0 : rRemainder = rFormulaString.copy( nBracketClose + 1 );
2758 0 : return !rRemainder.isEmpty();
2759 : }
2760 : }
2761 0 : return false;
2762 : }
2763 :
2764 : }
2765 :
2766 : // ----------------------------------------------------------------------------
2767 :
2768 21 : FormulaParser::FormulaParser( const WorkbookHelper& rHelper ) :
2769 21 : FormulaProcessorBase( rHelper )
2770 : {
2771 21 : switch( getFilterType() )
2772 : {
2773 21 : case FILTER_OOXML: mxImpl.reset( new OoxFormulaParserImpl( *this ) ); break;
2774 0 : case FILTER_BIFF: mxImpl.reset( new BiffFormulaParserImpl( *this ) ); break;
2775 0 : case FILTER_UNKNOWN: break;
2776 : }
2777 21 : }
2778 :
2779 42 : FormulaParser::~FormulaParser()
2780 : {
2781 42 : }
2782 :
2783 47 : ApiTokenSequence FormulaParser::importFormula( const CellAddress& rBaseAddress, const OUString& rFormulaString ) const
2784 : {
2785 47 : return mxImpl->importOoxFormula( rBaseAddress, rFormulaString );
2786 : }
2787 :
2788 0 : ApiTokenSequence FormulaParser::importFormula( const CellAddress& rBaseAddress, FormulaType eType, SequenceInputStream& rStrm ) const
2789 : {
2790 0 : return mxImpl->importBiff12Formula( rBaseAddress, eType, rStrm );
2791 : }
2792 :
2793 0 : ApiTokenSequence FormulaParser::importFormula( const CellAddress& rBaseAddress, FormulaType eType, BiffInputStream& rStrm, const sal_uInt16* pnFmlaSize ) const
2794 : {
2795 0 : return mxImpl->importBiffFormula( rBaseAddress, eType, rStrm, pnFmlaSize );
2796 : }
2797 :
2798 0 : ApiTokenSequence FormulaParser::convertBoolToFormula( bool bValue ) const
2799 : {
2800 0 : if( const FunctionInfo* pFuncInfo = getFuncInfoFromBiffFuncId( bValue ? BIFF_FUNC_TRUE : BIFF_FUNC_FALSE ) )
2801 : {
2802 0 : ApiTokenSequence aTokens( 3 );
2803 0 : aTokens[ 0 ].OpCode = pFuncInfo->mnApiOpCode;
2804 0 : aTokens[ 1 ].OpCode = OPCODE_OPEN;
2805 0 : aTokens[ 2 ].OpCode = OPCODE_CLOSE;
2806 0 : return aTokens;
2807 : }
2808 0 : return ApiTokenSequence();
2809 : }
2810 :
2811 0 : ApiTokenSequence FormulaParser::convertErrorToFormula( sal_uInt8 nErrorCode ) const
2812 : {
2813 0 : ApiTokenSequence aTokens( 3 );
2814 : // HACK: enclose all error codes into an 1x1 matrix
2815 0 : aTokens[ 0 ].OpCode = OPCODE_ARRAY_OPEN;
2816 0 : aTokens[ 1 ].OpCode = OPCODE_PUSH;
2817 0 : aTokens[ 1 ].Data <<= BiffHelper::calcDoubleFromError( nErrorCode );
2818 0 : aTokens[ 2 ].OpCode = OPCODE_ARRAY_CLOSE;
2819 0 : return aTokens;
2820 : }
2821 :
2822 17 : ApiTokenSequence FormulaParser::convertNameToFormula( sal_Int32 nTokenIndex ) const
2823 : {
2824 17 : if( nTokenIndex < 0 )
2825 0 : return convertErrorToFormula( BIFF_ERR_REF );
2826 :
2827 17 : ApiTokenSequence aTokens( 1 );
2828 17 : aTokens[ 0 ].OpCode = OPCODE_NAME;
2829 17 : NameToken aNameTokenData;
2830 17 : aNameTokenData.Global = sal_True;
2831 17 : aNameTokenData.Index = nTokenIndex;
2832 17 : aTokens[ 0 ].Data <<= aNameTokenData;
2833 17 : return aTokens;
2834 : }
2835 :
2836 0 : OUString FormulaParser::importOleTargetLink( const OUString& rFormulaString )
2837 : {
2838 0 : sal_Int32 nRefId = -1;
2839 0 : OUString aRemainder;
2840 0 : if( lclExtractRefId( nRefId, aRemainder, rFormulaString ) && (aRemainder.getLength() >= 3) &&
2841 0 : (aRemainder[ 0 ] == '!') && (aRemainder[ 1 ] == '\'') && (aRemainder[ aRemainder.getLength() - 1 ] == '\'') )
2842 0 : return mxImpl->resolveOleTarget( nRefId, false );
2843 0 : return OUString();
2844 : }
2845 :
2846 0 : OUString FormulaParser::importOleTargetLink( SequenceInputStream& rStrm )
2847 : {
2848 0 : OUString aTargetLink;
2849 0 : sal_Int32 nFmlaSize = rStrm.readInt32();
2850 0 : sal_Int64 nFmlaEndPos = rStrm.tell() + ::std::max< sal_Int32 >( nFmlaSize, 0 );
2851 0 : if( (nFmlaSize == 7) && (rStrm.getRemaining() >= 7) )
2852 : {
2853 : sal_uInt8 nToken;
2854 : sal_Int16 nRefId;
2855 : sal_Int32 nNameId;
2856 0 : rStrm >> nToken >> nRefId >> nNameId;
2857 0 : if( nToken == (BIFF_TOKCLASS_VAL|BIFF_TOKID_NAMEX) )
2858 0 : aTargetLink = mxImpl->resolveOleTarget( nRefId, true );
2859 : }
2860 0 : rStrm.seek( nFmlaEndPos );
2861 0 : return aTargetLink;
2862 : }
2863 :
2864 0 : OUString FormulaParser::importMacroName( const OUString& rFormulaString )
2865 : {
2866 : /* Valid macros are either sheet macros or VBA macros. OOXML and all BIFF
2867 : documents store defined names for sheet macros, but OOXML documents do
2868 : not store any defined name for VBA macros (while BIFF documents do).
2869 : Sheet macros may be defined locally to a sheet, or globally to the
2870 : document. As a result, all of the following macro specifiers are valid:
2871 :
2872 : 1) Macros located in the own document:
2873 : [0]!MySheetMacro (global sheet macro 'MySheetMacro')
2874 : Macro1!MyMacro (sheet-local sheet macro 'MyMacro')
2875 : [0]!MyVBAProc (VBA macro 'MyVBAProc')
2876 : [0]!Mod1.MyVBAProc (VBA macro 'MyVBAProc' from code module 'Mod1')
2877 :
2878 : 2) Macros from an external document:
2879 : [2]!MySheetMacro (global external sheet macro 'MySheetMacro')
2880 : [2]Macro1!MyMacro (sheet-local external sheet macro 'MyMacro')
2881 : [2]!MyVBAProc (external VBA macro 'MyVBAProc')
2882 : [2]!Mod1.MyVBAProc (external VBA macro from code module 'Mod1')
2883 :
2884 : This implementation is only interested in VBA macros from the own
2885 : document, ignoring the valid syntax 'Macro1!MyMacro' for sheet-local
2886 : sheet macros.
2887 : */
2888 0 : sal_Int32 nRefId = -1;
2889 0 : OUString aRemainder;
2890 0 : if( lclExtractRefId( nRefId, aRemainder, rFormulaString ) && (aRemainder.getLength() > 1) && (aRemainder[ 0 ] == '!') )
2891 : {
2892 : /* In BIFF12 documents, the reference identifier is always the
2893 : one-based index of the external link as it is in OOXML documents
2894 : (it is not an index into the list of reference sheets as used in
2895 : cell formulas). Index 0 is an implicit placeholder for the own
2896 : document. In BIFF12 documents, the reference to the own document is
2897 : stored explicitly, mostly at the top of the list, so index 1 may
2898 : resolve to the own document too.
2899 : Passing 'false' to getExternalLink() specifies to ignore the
2900 : reference sheets list (if existing) and to access the list of
2901 : external links directly. */
2902 0 : const ExternalLink* pExtLink = getExternalLinks().getExternalLink( nRefId, false ).get();
2903 : OSL_ENSURE( pExtLink, "FormulaParser::importMacroName - missing link" );
2904 : // do not accept macros in external documents (not supported)
2905 0 : if( pExtLink && (pExtLink->getLinkType() == LINKTYPE_SELF) )
2906 : {
2907 : // ignore sheet macros (defined name for VBA macros may not exist, see above)
2908 0 : OUString aMacroName = aRemainder.copy( 1 );
2909 0 : const DefinedName* pDefName = getDefinedNames().getByModelName( aMacroName ).get();
2910 0 : if( !pDefName || pDefName->isVBName() )
2911 0 : return aMacroName;
2912 : }
2913 : }
2914 0 : return OUString();
2915 : }
2916 :
2917 : // ============================================================================
2918 :
2919 : } // namespace xls
2920 15 : } // namespace oox
2921 :
2922 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|