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 : #ifndef SC_XLFORMULA_HXX
21 : #define SC_XLFORMULA_HXX
22 :
23 : #include <map>
24 : #include <formula/opcode.hxx>
25 : #include "address.hxx"
26 : #include "ftools.hxx"
27 : #include <boost/shared_ptr.hpp>
28 :
29 : // Constants ==================================================================
30 :
31 : const size_t EXC_TOKARR_MAXLEN = 4096; /// Maximum size of a token array.
32 :
33 : // Token class flags ----------------------------------------------------------
34 :
35 : const sal_uInt8 EXC_TOKCLASS_MASK = 0x60;
36 : const sal_uInt8 EXC_TOKCLASS_NONE = 0x00; /// 00-1F: Base tokens.
37 : const sal_uInt8 EXC_TOKCLASS_REF = 0x20; /// 20-3F: Reference class tokens.
38 : const sal_uInt8 EXC_TOKCLASS_VAL = 0x40; /// 40-5F: Value class tokens.
39 : const sal_uInt8 EXC_TOKCLASS_ARR = 0x60; /// 60-7F: Array class tokens.
40 :
41 : // Base tokens ----------------------------------------------------------------
42 :
43 : const sal_uInt8 EXC_TOKID_MASK = 0x1F;
44 :
45 : const sal_uInt8 EXC_TOKID_NONE = 0x00; /// Placeholder for invalid token id.
46 : const sal_uInt8 EXC_TOKID_EXP = 0x01; /// Array or shared formula reference.
47 : const sal_uInt8 EXC_TOKID_TBL = 0x02; /// Multiple operation reference.
48 : const sal_uInt8 EXC_TOKID_ADD = 0x03; /// Addition operator.
49 : const sal_uInt8 EXC_TOKID_SUB = 0x04; /// Subtraction operator.
50 : const sal_uInt8 EXC_TOKID_MUL = 0x05; /// Multiplication operator.
51 : const sal_uInt8 EXC_TOKID_DIV = 0x06; /// Division operator.
52 : const sal_uInt8 EXC_TOKID_POWER = 0x07; /// Power operator.
53 : const sal_uInt8 EXC_TOKID_CONCAT = 0x08; /// String concatenation operator.
54 : const sal_uInt8 EXC_TOKID_LT = 0x09; /// Less than operator.
55 : const sal_uInt8 EXC_TOKID_LE = 0x0A; /// Less than or equal operator.
56 : const sal_uInt8 EXC_TOKID_EQ = 0x0B; /// Equal operator.
57 : const sal_uInt8 EXC_TOKID_GE = 0x0C; /// Greater than or equal operator.
58 : const sal_uInt8 EXC_TOKID_GT = 0x0D; /// Greater than operator.
59 : const sal_uInt8 EXC_TOKID_NE = 0x0E; /// Not equal operator.
60 : const sal_uInt8 EXC_TOKID_ISECT = 0x0F; /// Intersection operator.
61 : const sal_uInt8 EXC_TOKID_LIST = 0x10; /// List operator.
62 : const sal_uInt8 EXC_TOKID_RANGE = 0x11; /// Range operator.
63 : const sal_uInt8 EXC_TOKID_UPLUS = 0x12; /// Unary plus.
64 : const sal_uInt8 EXC_TOKID_UMINUS = 0x13; /// Unary minus.
65 : const sal_uInt8 EXC_TOKID_PERCENT = 0x14; /// Percent sign.
66 : const sal_uInt8 EXC_TOKID_PAREN = 0x15; /// Parentheses.
67 : const sal_uInt8 EXC_TOKID_MISSARG = 0x16; /// Missing argument.
68 : const sal_uInt8 EXC_TOKID_STR = 0x17; /// String constant.
69 : const sal_uInt8 EXC_TOKID_NLR = 0x18; /// Natural language reference (NLR).
70 : const sal_uInt8 EXC_TOKID_ATTR = 0x19; /// Special attribute.
71 : const sal_uInt8 EXC_TOKID_SHEET = 0x1A; /// Start of a sheet reference (BIFF2-BIFF4).
72 : const sal_uInt8 EXC_TOKID_ENDSHEET = 0x1B; /// End of a sheet reference (BIFF2-BIFF4).
73 : const sal_uInt8 EXC_TOKID_ERR = 0x1C; /// Error constant.
74 : const sal_uInt8 EXC_TOKID_BOOL = 0x1D; /// Boolean constant.
75 : const sal_uInt8 EXC_TOKID_INT = 0x1E; /// Integer constant.
76 : const sal_uInt8 EXC_TOKID_NUM = 0x1F; /// Floating-point constant.
77 :
78 : // Base IDs of classified tokens ----------------------------------------------
79 :
80 : const sal_uInt8 EXC_TOKID_ARRAY = 0x00; /// Array constant.
81 : const sal_uInt8 EXC_TOKID_FUNC = 0x01; /// Function, fixed number of arguments.
82 : const sal_uInt8 EXC_TOKID_FUNCVAR = 0x02; /// Function, variable number of arguments.
83 : const sal_uInt8 EXC_TOKID_NAME = 0x03; /// Defined name.
84 : const sal_uInt8 EXC_TOKID_REF = 0x04; /// 2D cell reference.
85 : const sal_uInt8 EXC_TOKID_AREA = 0x05; /// 2D area reference.
86 : const sal_uInt8 EXC_TOKID_MEMAREA = 0x06; /// Constant reference subexpression.
87 : const sal_uInt8 EXC_TOKID_MEMERR = 0x07; /// Deleted reference subexpression.
88 : const sal_uInt8 EXC_TOKID_MEMNOMEM = 0x08; /// Constant reference subexpression without result.
89 : const sal_uInt8 EXC_TOKID_MEMFUNC = 0x09; /// Variable reference subexpression.
90 : const sal_uInt8 EXC_TOKID_REFERR = 0x0A; /// Deleted 2D cell reference.
91 : const sal_uInt8 EXC_TOKID_AREAERR = 0x0B; /// Deleted 2D area reference.
92 : const sal_uInt8 EXC_TOKID_REFN = 0x0C; /// Relative 2D cell reference (in names).
93 : const sal_uInt8 EXC_TOKID_AREAN = 0x0D; /// Relative 2D area reference (in names).
94 : const sal_uInt8 EXC_TOKID_MEMAREAN = 0x0E; /// Reference subexpression (in names).
95 : const sal_uInt8 EXC_TOKID_MEMNOMEMN = 0x0F; /// Reference subexpression (in names) without result.
96 : const sal_uInt8 EXC_TOKID_FUNCCE = 0x18;
97 : const sal_uInt8 EXC_TOKID_NAMEX = 0x19; /// External reference.
98 : const sal_uInt8 EXC_TOKID_REF3D = 0x1A; /// 3D cell reference.
99 : const sal_uInt8 EXC_TOKID_AREA3D = 0x1B; /// 3D area reference.
100 : const sal_uInt8 EXC_TOKID_REFERR3D = 0x1C; /// Deleted 3D cell reference.
101 : const sal_uInt8 EXC_TOKID_AREAERR3D = 0x1D; /// Deleted 3D area reference
102 :
103 : // specific token constants ---------------------------------------------------
104 :
105 : const sal_uInt16 EXC_TOK_STR_MAXLEN = 255; /// Maximum string length of a tStr token.
106 :
107 : const sal_uInt8 EXC_TOK_BOOL_FALSE = 0; /// sal_False value of a tBool token.
108 : const sal_uInt8 EXC_TOK_BOOL_TRUE = 1; /// sal_True value of a tBool token.
109 :
110 : const sal_uInt8 EXC_TOK_ATTR_VOLATILE = 0x01; /// Volatile function.
111 : const sal_uInt8 EXC_TOK_ATTR_IF = 0x02; /// Start of true condition in IF function.
112 : const sal_uInt8 EXC_TOK_ATTR_CHOOSE = 0x04; /// Jump array of CHOOSE function.
113 : const sal_uInt8 EXC_TOK_ATTR_GOTO = 0x08; /// Jump to token.
114 : const sal_uInt8 EXC_TOK_ATTR_SUM = 0x10; /// SUM function with one parameter.
115 : const sal_uInt8 EXC_TOK_ATTR_ASSIGN = 0x20; /// BASIC style assignment.
116 : const sal_uInt8 EXC_TOK_ATTR_SPACE = 0x40; /// Spaces in formula representation.
117 :
118 : const sal_uInt8 EXC_TOK_ATTR_SPACE_SP = 0x00; /// Spaces before next token.
119 : const sal_uInt8 EXC_TOK_ATTR_SPACE_BR = 0x01; /// Line breaks before next token.
120 : const sal_uInt8 EXC_TOK_ATTR_SPACE_SP_OPEN = 0x02; /// Spaces before opening parenthesis.
121 : const sal_uInt8 EXC_TOK_ATTR_SPACE_BR_OPEN = 0x03; /// Line breaks before opening parenthesis.
122 : const sal_uInt8 EXC_TOK_ATTR_SPACE_SP_CLOSE = 0x04; /// Spaces before closing parenthesis.
123 : const sal_uInt8 EXC_TOK_ATTR_SPACE_BR_CLOSE = 0x05; /// Line breaks before closing parenthesis.
124 : const sal_uInt8 EXC_TOK_ATTR_SPACE_SP_PRE = 0x06; /// Spaces before formula (BIFF3).
125 :
126 : const sal_uInt16 EXC_TOK_FUNCVAR_CMD = 0x8000; /// Macro command.
127 : const sal_uInt16 EXC_TOK_FUNCVAR_INDEXMASK = 0x7FFF; /// Mask for function/command index.
128 : const sal_uInt8 EXC_TOK_FUNCVAR_PROMPT = 0x80; /// User prompt for macro commands.
129 : const sal_uInt8 EXC_TOK_FUNCVAR_COUNTMASK = 0x7F; /// Mask for parameter count.
130 :
131 : const sal_uInt16 EXC_TOK_REF_COLREL = 0x4000; /// True = Column is relative.
132 : const sal_uInt16 EXC_TOK_REF_ROWREL = 0x8000; /// True = Row is relative.
133 :
134 : const sal_uInt8 EXC_TOK_NLR_ERR = 0x01; /// NLR: Invalid/deleted.
135 : const sal_uInt8 EXC_TOK_NLR_ROWR = 0x02; /// NLR: Row index.
136 : const sal_uInt8 EXC_TOK_NLR_COLR = 0x03; /// NLR: Column index.
137 : const sal_uInt8 EXC_TOK_NLR_ROWV = 0x06; /// NLR: Value in row.
138 : const sal_uInt8 EXC_TOK_NLR_COLV = 0x07; /// NLR: Value in column.
139 : const sal_uInt8 EXC_TOK_NLR_RANGE = 0x0A; /// NLR: Range.
140 : const sal_uInt8 EXC_TOK_NLR_SRANGE = 0x0B; /// Stacked NLR: Range.
141 : const sal_uInt8 EXC_TOK_NLR_SROWR = 0x0C; /// Stacked NLR: Row index.
142 : const sal_uInt8 EXC_TOK_NLR_SCOLR = 0x0D; /// Stacked NLR: Column index.
143 : const sal_uInt8 EXC_TOK_NLR_SROWV = 0x0E; /// Stacked NLR: Value in row.
144 : const sal_uInt8 EXC_TOK_NLR_SCOLV = 0x0F; /// Stacked NLR: Value in column.
145 : const sal_uInt8 EXC_TOK_NLR_RANGEERR = 0x10; /// NLR: Invalid/deleted range.
146 : const sal_uInt8 EXC_TOK_NLR_SXNAME = 0x1D; /// NLR: Pivot table name.
147 : const sal_uInt16 EXC_TOK_NLR_REL = 0x8000; /// True = Natural language ref is relative.
148 :
149 : const sal_uInt32 EXC_TOK_NLR_ADDREL = 0x80000000; /// NLR relative (in appended data).
150 : const sal_uInt32 EXC_TOK_NLR_ADDMASK = 0x3FFFFFFF; /// Mask for number of appended ranges.
151 :
152 : // ----------------------------------------------------------------------------
153 :
154 : /** Type of a formula. */
155 : enum XclFormulaType
156 : {
157 : EXC_FMLATYPE_CELL, /// Simple cell formula, also used in change tracking.
158 : EXC_FMLATYPE_MATRIX, /// Matrix (array) formula.
159 : EXC_FMLATYPE_SHARED, /// Shared formula.
160 : EXC_FMLATYPE_CONDFMT, /// Conditional format.
161 : EXC_FMLATYPE_DATAVAL, /// Data validation.
162 : EXC_FMLATYPE_NAME, /// Defined name.
163 : EXC_FMLATYPE_CHART, /// Chart source ranges.
164 : EXC_FMLATYPE_CONTROL, /// Spreadsheet links in form controls.
165 : EXC_FMLATYPE_WQUERY, /// Web query source range.
166 : EXC_FMLATYPE_LISTVAL /// List (cell range) validation.
167 : };
168 :
169 : // Function parameter info ====================================================
170 :
171 : /** Enumerates validity modes for a function parameter. */
172 : enum XclFuncParamValidity
173 : {
174 : EXC_PARAM_NONE = 0, /// Default for an unspecified entry in a C-array.
175 : EXC_PARAM_REGULAR, /// Parameter supported by Calc and Excel.
176 : EXC_PARAM_CALCONLY, /// Parameter supported by Calc only.
177 : EXC_PARAM_EXCELONLY /// Parameter supported by Excel only.
178 : };
179 :
180 : /** Enumerates different types of token class conversion in function parameters. */
181 : enum XclFuncParamConv
182 : {
183 : EXC_PARAMCONV_ORG, /// Use original class of current token.
184 : EXC_PARAMCONV_VAL, /// Convert tokens to VAL class.
185 : EXC_PARAMCONV_ARR, /// Convert tokens to ARR class.
186 : EXC_PARAMCONV_RPT, /// Repeat parent conversion in VALTYPE parameters.
187 : EXC_PARAMCONV_RPX, /// Repeat parent conversion in REFTYPE parameters.
188 : EXC_PARAMCONV_RPO /// Repeat parent conversion in operands of operators.
189 : };
190 :
191 : /** Structure that contains all needed information for a parameter in a
192 : function.
193 :
194 : The member meValid specifies which application supports the parameter. If
195 : set to CALCONLY, import filters have to insert a default value for this
196 : parameter, and export filters have to skip the parameter. If set to
197 : EXCELONLY, import filters have to skip the parameter, and export filters
198 : have to insert a default value for this parameter.
199 :
200 : The member mbValType specifies whether the parameter requires tokens to be
201 : of value type (VAL or ARR class).
202 :
203 : If set to false, the parameter is called to be REFTYPE. Tokens with REF
204 : default class can be inserted for the parameter (e.g. tAreaR tokens).
205 :
206 : If set to true, the parameter is called to be VALTYPE. Tokens with REF
207 : class need to be converted to VAL tokens first (e.g. tAreaR will be
208 : converted to tAreaV), and further conversion is done according to this
209 : new token class.
210 :
211 : The member meConv specifies how to convert the current token class of the
212 : token inserted for the parameter. If the token class is still REF this
213 : means that the token has default REF class and the parameter is REFTYPE
214 : (see member mbValType), the token will not be converted at all and remains
215 : in REF class. Otherwise, token class conversion is depending on the actual
216 : token class of the return value of the function containing this parameter.
217 : The function may return REF class (tFuncR, tFuncVarR, tFuncCER), or it may
218 : return VAL or ARR class (tFuncV, tFuncA, tFuncVarV, tFuncVarA, tFuncCEV,
219 : tFuncCEA). Even if the function is able to return REF class, it may return
220 : VAL or ARR class instead due to the VALTYPE data type of the parent
221 : function parameter that calls the own function. Example: The INDIRECT
222 : function returns REF class by default. But if called from a VALTYPE
223 : function parameter, e.g. in the formula =ABS(INDIRECT("A1")), it returns
224 : VAL or ARR class instead. Additionally, the repeating conversion types RPT
225 : and RPX rely on the conversion executed for the function token class.
226 :
227 : 1) ORG:
228 : Use the original class of the token (VAL or ARR), regardless of any
229 : conversion done for the function return class.
230 :
231 : 2) VAL:
232 : Convert ARR tokens to VAL class, regardless of any conversion done for
233 : the function return class.
234 :
235 : 3) ARR:
236 : Convert VAL tokens to ARR class, regardless of any conversion done for
237 : the function return class.
238 :
239 : 4) RPT:
240 : If the own function returns REF class (thus it is called from a REFTYPE
241 : parameter, see above), and the parent conversion type (for the function
242 : return class) was ORG, VAL, or ARR, ignore that conversion and always
243 : use VAL conversion for the own token instead. If the parent conversion
244 : type was RPT or RPX, repeat the conversion that would have been used if
245 : the function would return value type.
246 : If the own function returns value type (VAL or ARR class, see above),
247 : and the parent conversion type (for the function return class) was ORG,
248 : VAL, ARR, or RPT, repeat this conversion for the own token. If the
249 : parent conversion type was RPX, always use ORG conversion type for the
250 : own token instead.
251 :
252 : 5) RPX:
253 : This type of conversion only occurs in functions returning VAL class by
254 : default. If the own token is value type, and the VAL return class of
255 : the own function has been changed to ARR class (due to direct ARR
256 : conversion, or due to ARR conversion repeated by RPT or RPX), set the
257 : own token to ARR type. Otherwise use the original token type (VAL
258 : conversion from parent parameter will not be repeated at all). If
259 : nested functions have RPT or value-type RPX parameters, they will not
260 : repeat this conversion type, but will use ORG conversion instead (see
261 : description of RPT above).
262 :
263 : 6) RPO:
264 : This type of conversion is only used for the operands of all operators
265 : (unary and binary arithmetic operators, comparison operators, and range
266 : operators). It is not used for function parameters. On conversion, it
267 : will be replaced by the last conversion type that was not the RPO
268 : conversion. This leads to a slightly different behaviour than the RPT
269 : conversion for operands in conjunction with a parent RPX conversion.
270 : */
271 : struct XclFuncParamInfo
272 : {
273 : XclFuncParamValidity meValid; /// Parameter validity.
274 : XclFuncParamConv meConv; /// Token class conversion type.
275 : bool mbValType; /// Data type (false = REFTYPE, true = VALTYPE).
276 : };
277 :
278 : // Function data ==============================================================
279 :
280 : const sal_uInt8 EXC_FUNC_MAXPARAM = 30; /// Maximum parameter count.
281 :
282 : const size_t EXC_FUNCINFO_PARAMINFO_COUNT = 5; /// Number of parameter info entries.
283 :
284 : const sal_uInt8 EXC_FUNCFLAG_VOLATILE = 0x01; /// Result is volatile (e.g. NOW() function).
285 : const sal_uInt8 EXC_FUNCFLAG_IMPORTONLY = 0x02; /// Only used in import filter.
286 : const sal_uInt8 EXC_FUNCFLAG_EXPORTONLY = 0x04; /// Only used in export filter.
287 :
288 : // selected function IDs
289 : const sal_uInt16 EXC_FUNCID_IF = 1;
290 : const sal_uInt16 EXC_FUNCID_SUM = 4;
291 : const sal_uInt16 EXC_FUNCID_AND = 36;
292 : const sal_uInt16 EXC_FUNCID_OR = 37;
293 : const sal_uInt16 EXC_FUNCID_CHOOSE = 100;
294 : const sal_uInt16 EXC_FUNCID_EXTERNCALL = 255;
295 :
296 : /** Represents information for a spreadsheet function for import and export.
297 :
298 : The member mpParamInfos points to an array of type information structures
299 : for all parameters of the function. The last initialized structure
300 : describing a regular parameter (member meValid == EXC_PARAMVALID_ALWAYS) in
301 : this array is used repeatedly for all following parameters supported by a
302 : function.
303 : */
304 : struct XclFunctionInfo
305 : {
306 : OpCode meOpCode; /// Calc function opcode.
307 : sal_uInt16 mnXclFunc; /// Excel function index.
308 : sal_uInt8 mnMinParamCount; /// Minimum number of parameters.
309 : sal_uInt8 mnMaxParamCount; /// Maximum number of parameters.
310 : sal_uInt8 mnRetClass; /// Token class of the return value.
311 : XclFuncParamInfo mpParamInfos[ EXC_FUNCINFO_PARAMINFO_COUNT ]; /// Information for all parameters.
312 : sal_uInt8 mnFlags; /// Additional flags (EXC_FUNCFLAG_* constants).
313 : const sal_Char* mpcMacroName; /// Function name, if simulated by a macro call (UTF-8).
314 :
315 : /** Returns true, if the function is volatile. */
316 0 : inline bool IsVolatile() const { return ::get_flag( mnFlags, EXC_FUNCFLAG_VOLATILE ); }
317 : /** Returns true, if the function parameter count is fixed. */
318 0 : inline bool IsFixedParamCount() const { return (mnXclFunc != EXC_FUNCID_EXTERNCALL) && (mnMinParamCount == mnMaxParamCount); }
319 : /** Returns true, if the function is simulated by a macro call. */
320 42108 : inline bool IsMacroFunc() const { return mpcMacroName != 0; }
321 : /** Returns the name of the external function as string. */
322 : String GetMacroFuncName() const;
323 : };
324 :
325 : // ----------------------------------------------------------------------------
326 :
327 : class XclRoot;
328 :
329 : /** Provides access to function info structs for all available functions. */
330 134 : class XclFunctionProvider
331 : {
332 : public:
333 : explicit XclFunctionProvider( const XclRoot& rRoot );
334 :
335 : /** Returns the function data for an Excel function index, or 0 on error. */
336 : const XclFunctionInfo* GetFuncInfoFromXclFunc( sal_uInt16 nXclFunc ) const;
337 : /** Returns the function data for an Excel function simulated by a macro call, or 0 on error. */
338 : const XclFunctionInfo* GetFuncInfoFromXclMacroName( const String& rXclMacroName ) const;
339 : /** Returns the function data for a Calc opcode, or 0 on error. */
340 : const XclFunctionInfo* GetFuncInfoFromOpCode( OpCode eOpCode ) const;
341 :
342 : private:
343 : void FillXclFuncMap( const XclFunctionInfo* pBeg, const XclFunctionInfo* pEnd );
344 : void FillScFuncMap( const XclFunctionInfo* pBeg, const XclFunctionInfo* pEnd );
345 :
346 : private:
347 : typedef ::std::map< sal_uInt16, const XclFunctionInfo* > XclFuncMap;
348 : typedef ::std::map< String, const XclFunctionInfo* > XclMacroNameMap;
349 : typedef ::std::map< OpCode, const XclFunctionInfo* > ScFuncMap;
350 :
351 : XclFuncMap maXclFuncMap; /// Maps Excel function indexes to function data.
352 : XclMacroNameMap maXclMacroNameMap; /// Maps macro function names to function data.
353 : ScFuncMap maScFuncMap; /// Maps Calc opcodes to function data.
354 : };
355 :
356 : // Token array ================================================================
357 :
358 : class XclImpStream;
359 : class XclExpStream;
360 :
361 : /** Binary representation of an Excel token array. */
362 182 : class XclTokenArray
363 : {
364 : public:
365 : /** Creates an empty token array. */
366 : explicit XclTokenArray( bool bVolatile = false );
367 : /** Creates a token array, swaps passed token vectors into own data. */
368 : explicit XclTokenArray( ScfUInt8Vec& rTokVec, ScfUInt8Vec& rExtDataVec, bool bVolatile = false );
369 :
370 : /** Returns true, if the token array is empty. */
371 166 : inline bool Empty() const { return maTokVec.empty(); }
372 : /** Returns the size of the token array in bytes. */
373 : sal_uInt16 GetSize() const;
374 : /** Returns read-only access to the byte vector storing token data. */
375 166 : inline const sal_uInt8* GetData() const { return maTokVec.empty() ? 0 : &maTokVec.front(); }
376 : /** Returns true, if the formula contains a volatile function. */
377 0 : inline bool IsVolatile() const { return mbVolatile; }
378 :
379 : /** Reads the size field of the token array. */
380 : void ReadSize( XclImpStream& rStrm );
381 : /** Reads the tokens of the token array (without size field). */
382 : void ReadArray( XclImpStream& rStrm );
383 : /** Reads size field and the tokens. */
384 : void Read( XclImpStream& rStrm );
385 :
386 : /** Writes the size field of the token array. */
387 : void WriteSize( XclExpStream& rStrm ) const;
388 : /** Writes the tokens of the token array (without size field). */
389 : void WriteArray( XclExpStream& rStrm ) const;
390 : /** Writes size field and the tokens. */
391 : void Write( XclExpStream& rStrm ) const;
392 :
393 : /** Compares this token array with the passed. */
394 : bool operator==( const XclTokenArray& rTokArr ) const;
395 :
396 : private:
397 : ScfUInt8Vec maTokVec; /// Byte vector containing token data.
398 : ScfUInt8Vec maExtDataVec; /// Byte vector containing extended data (arrays, stacked NLRs).
399 : bool mbVolatile; /// True = Formula contains volatile function.
400 : };
401 :
402 : typedef boost::shared_ptr< XclTokenArray > XclTokenArrayRef;
403 :
404 : /** Calls the Read() function at the passed token array. */
405 : XclImpStream& operator>>( XclImpStream& rStrm, XclTokenArray& rTokArr );
406 : /** Calls the Read() function at the passed token array. */
407 : XclImpStream& operator>>( XclImpStream& rStrm, XclTokenArrayRef& rxTokArr );
408 : /** Calls the Write() function at the passed token array. */
409 : XclExpStream& operator<<( XclExpStream& rStrm, const XclTokenArray& rTokArr );
410 : /** Calls the Write() function at the passed token array. */
411 : XclExpStream& operator<<( XclExpStream& rStrm, const XclTokenArrayRef& rxTokArr );
412 :
413 : // ----------------------------------------------------------------------------
414 :
415 : namespace formula { class FormulaToken; }
416 : class ScTokenArray;
417 :
418 : /** Special token array iterator for the Excel filters.
419 :
420 : Iterates over a Calc token array without modifying it (therefore the
421 : iterator can be used with constant token arrays).
422 :
423 : Usage: Construct a new iterator object and pass a Calc token array, or use
424 : the Init() function to assign another Calc token array. As long as the Is()
425 : function returns true, the accessor functions can be used to get the
426 : current Calc token.
427 : */
428 : class XclTokenArrayIterator
429 : {
430 : public:
431 : explicit XclTokenArrayIterator();
432 : explicit XclTokenArrayIterator( const ScTokenArray& rScTokArr, bool bSkipSpaces );
433 : /** Copy constructor that allows to change the skip-spaces mode. */
434 : explicit XclTokenArrayIterator( const XclTokenArrayIterator& rTokArrIt, bool bSkipSpaces );
435 :
436 : void Init( const ScTokenArray& rScTokArr, bool bSkipSpaces );
437 :
438 0 : inline bool Is() const { return mppScToken != 0; }
439 0 : inline bool operator!() const { return !Is(); }
440 32 : inline const ::formula::FormulaToken* Get() const { return mppScToken ? *mppScToken : 0; }
441 0 : inline const ::formula::FormulaToken* operator->() const { return Get(); }
442 0 : inline const ::formula::FormulaToken& operator*() const { return *Get(); }
443 :
444 : XclTokenArrayIterator& operator++();
445 :
446 : private:
447 : void NextRawToken();
448 : void SkipSpaces();
449 :
450 : private:
451 : const ::formula::FormulaToken*const* mppScTokenBeg; /// Pointer to first token pointer of token array.
452 : const ::formula::FormulaToken*const* mppScTokenEnd; /// Pointer behind last token pointer of token array.
453 : const ::formula::FormulaToken*const* mppScToken; /// Pointer to current token pointer of token array.
454 : bool mbSkipSpaces; /// true = Skip whitespace tokens.
455 : };
456 :
457 : // ----------------------------------------------------------------------------
458 :
459 : /** Contains all cell references that can be extracted from a multiple operations formula. */
460 0 : struct XclMultipleOpRefs
461 : {
462 : ScAddress maFmlaScPos; /// Position of the (first) formula cell.
463 : ScAddress maColFirstScPos;
464 : ScAddress maColRelScPos;
465 : ScAddress maRowFirstScPos;
466 : ScAddress maRowRelScPos;
467 : bool mbDblRefMode; /// true = One formula with row and column values.
468 : };
469 :
470 : // ----------------------------------------------------------------------------
471 :
472 : /** A helper with Excel specific token array functions.
473 :
474 : The purpose to not add these functions to ScTokenArray is to prevent code
475 : changes in low-level Calc headers and to keep the Excel specific source
476 : code in the filter directory. Deriving from ScTokenArray is not viable
477 : because that would need expensive copy-constructions of the token arrays.
478 : */
479 46 : class XclTokenArrayHelper
480 : {
481 : public:
482 : // token identifiers ------------------------------------------------------
483 :
484 : /** Returns the base token ID of the passed (classified) token ID. */
485 : inline static sal_uInt8 GetBaseTokenId( sal_uInt8 nTokenId ) { return nTokenId & EXC_TOKID_MASK; }
486 : /** Returns the classified token ID from a base ID and the token class. */
487 : inline static sal_uInt8 GetTokenId( sal_uInt8 nBaseId, sal_uInt8 nTokenClass );
488 :
489 : /** Returns the token class of the passed token ID. */
490 16 : inline static sal_uInt8 GetTokenClass( sal_uInt8 nTokenId ) { return nTokenId & EXC_TOKCLASS_MASK; }
491 : /** Changes the token class in the passed classified token ID. */
492 : inline static void ChangeTokenClass( sal_uInt8& rnTokenId, sal_uInt8 nTokenClass );
493 :
494 : // strings and string lists -----------------------------------------------
495 :
496 : /** Tries to extract a string from the passed token.
497 : @param rString (out-parameter) The string contained in the token.
498 : @return true = Passed token is a string token, rString parameter is valid. */
499 : static bool GetTokenString( String& rString, const ::formula::FormulaToken& rScToken );
500 :
501 : /** Parses the passed formula and tries to find a single string token, i.e. "abc".
502 : @param rString (out-parameter) The string contained in the formula.
503 : @return true = String token found, rString parameter is valid. */
504 : static bool GetString( String& rString, const ScTokenArray& rScTokArr );
505 :
506 : /** Parses the passed formula and tries to find a string token list, i.e. "abc";"def";"ghi".
507 : @descr Returns the unquoted (!) strings in a single string, separated with the
508 : passed character. If a comma is specified, the function will return abc,def,ghi from
509 : the example above.
510 : @param rStringList (out-parameter) All strings contained in the formula as list.
511 : @param cSep List separator character.
512 : @return true = String token list found, rString parameter is valid. */
513 : static bool GetStringList( String& rStringList, const ScTokenArray& rScTokArr, sal_Unicode cSep );
514 :
515 : /** Tries to convert a formula that consists of a single string token to a list of strings.
516 : @descr Example: The formula ="abc\ndef\nghi" will be converted to the formula
517 : ="abc";"def";"ghi", if the LF character is specified as separator.
518 : @param rScTokArr (in/out-parameter) The token array to modify.
519 : @param cStringSep The separator in the source string.
520 : @param bTrimLeadingSpaces true = remove leading spaces from each token. */
521 : static void ConvertStringToList( ScTokenArray& rScTokArr, sal_Unicode cStringSep, bool bTrimLeadingSpaces );
522 :
523 : // shared formulas --------------------------------------------------------
524 :
525 : /** Tries to extract the definition of a shared formula from the passed token array.
526 : @descr Shared formulas are stored as hidden defined names in Calc. This
527 : function looks if the passed token array consists of the reference to
528 : such a hidden defined name and returns its definition on success. */
529 : static const ScTokenArray* GetSharedFormula( const XclRoot& rRoot, const ScTokenArray& rScTokArr );
530 :
531 : // multiple operations ----------------------------------------------------
532 :
533 : /** Parses the passed formula and tries to extract references of a multiple operation.
534 : @descr Requires that the formula contains a single MULTIPLE.OPERATION function call.
535 : Spaces in the formula are silently ignored.
536 : @return true = Multiple operation found, and all references successfully extracted. */
537 : static bool GetMultipleOpRefs( XclMultipleOpRefs& rRefs, const ScTokenArray& rScTokArr );
538 : };
539 :
540 : // ----------------------------------------------------------------------------
541 :
542 16 : inline sal_uInt8 XclTokenArrayHelper::GetTokenId( sal_uInt8 nBaseId, sal_uInt8 nTokenClass )
543 : {
544 : OSL_ENSURE( !::get_flag( nBaseId, static_cast< sal_uInt8 >( ~EXC_TOKID_MASK ) ), "XclTokenArrayHelper::GetTokenId - invalid token ID" );
545 : OSL_ENSURE( !::get_flag( nTokenClass, static_cast< sal_uInt8 >( ~EXC_TOKCLASS_MASK ) ), "XclTokenArrayHelper::GetTokenId - invalid token class" );
546 16 : return nBaseId | nTokenClass;
547 : }
548 :
549 0 : inline void XclTokenArrayHelper::ChangeTokenClass( sal_uInt8& rnTokenId, sal_uInt8 nTokenClass )
550 : {
551 : OSL_ENSURE( !::get_flag( nTokenClass, static_cast< sal_uInt8 >( ~EXC_TOKCLASS_MASK ) ), "XclTokenArrayHelper::ChangeTokenClass - invalid token class" );
552 0 : ::set_flag( rnTokenId, EXC_TOKCLASS_MASK, false );
553 0 : ::set_flag( rnTokenId, nTokenClass );
554 0 : }
555 :
556 : // ============================================================================
557 :
558 : #endif
559 :
560 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|