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 : /** Type of a formula. */
153 : enum XclFormulaType
154 : {
155 : EXC_FMLATYPE_CELL, /// Simple cell formula, also used in change tracking.
156 : EXC_FMLATYPE_MATRIX, /// Matrix (array) formula.
157 : EXC_FMLATYPE_SHARED, /// Shared formula.
158 : EXC_FMLATYPE_CONDFMT, /// Conditional format.
159 : EXC_FMLATYPE_DATAVAL, /// Data validation.
160 : EXC_FMLATYPE_NAME, /// Defined name.
161 : EXC_FMLATYPE_CHART, /// Chart source ranges.
162 : EXC_FMLATYPE_CONTROL, /// Spreadsheet links in form controls.
163 : EXC_FMLATYPE_WQUERY, /// Web query source range.
164 : EXC_FMLATYPE_LISTVAL /// List (cell range) validation.
165 : };
166 :
167 : // Function parameter info ====================================================
168 :
169 : /** Enumerates validity modes for a function parameter. */
170 : enum XclFuncParamValidity
171 : {
172 : EXC_PARAM_NONE = 0, /// Default for an unspecified entry in a C-array.
173 : EXC_PARAM_REGULAR, /// Parameter supported by Calc and Excel.
174 : EXC_PARAM_CALCONLY, /// Parameter supported by Calc only.
175 : EXC_PARAM_EXCELONLY /// Parameter supported by Excel only.
176 : };
177 :
178 : /** Enumerates different types of token class conversion in function parameters. */
179 : enum XclFuncParamConv
180 : {
181 : EXC_PARAMCONV_ORG, /// Use original class of current token.
182 : EXC_PARAMCONV_VAL, /// Convert tokens to VAL class.
183 : EXC_PARAMCONV_ARR, /// Convert tokens to ARR class.
184 : EXC_PARAMCONV_RPT, /// Repeat parent conversion in VALTYPE parameters.
185 : EXC_PARAMCONV_RPX, /// Repeat parent conversion in REFTYPE parameters.
186 : EXC_PARAMCONV_RPO /// Repeat parent conversion in operands of operators.
187 : };
188 :
189 : /** Structure that contains all needed information for a parameter in a
190 : function.
191 :
192 : The member meValid specifies which application supports the parameter. If
193 : set to CALCONLY, import filters have to insert a default value for this
194 : parameter, and export filters have to skip the parameter. If set to
195 : EXCELONLY, import filters have to skip the parameter, and export filters
196 : have to insert a default value for this parameter.
197 :
198 : The member mbValType specifies whether the parameter requires tokens to be
199 : of value type (VAL or ARR class).
200 :
201 : If set to false, the parameter is called to be REFTYPE. Tokens with REF
202 : default class can be inserted for the parameter (e.g. tAreaR tokens).
203 :
204 : If set to true, the parameter is called to be VALTYPE. Tokens with REF
205 : class need to be converted to VAL tokens first (e.g. tAreaR will be
206 : converted to tAreaV), and further conversion is done according to this
207 : new token class.
208 :
209 : The member meConv specifies how to convert the current token class of the
210 : token inserted for the parameter. If the token class is still REF this
211 : means that the token has default REF class and the parameter is REFTYPE
212 : (see member mbValType), the token will not be converted at all and remains
213 : in REF class. Otherwise, token class conversion is depending on the actual
214 : token class of the return value of the function containing this parameter.
215 : The function may return REF class (tFuncR, tFuncVarR, tFuncCER), or it may
216 : return VAL or ARR class (tFuncV, tFuncA, tFuncVarV, tFuncVarA, tFuncCEV,
217 : tFuncCEA). Even if the function is able to return REF class, it may return
218 : VAL or ARR class instead due to the VALTYPE data type of the parent
219 : function parameter that calls the own function. Example: The INDIRECT
220 : function returns REF class by default. But if called from a VALTYPE
221 : function parameter, e.g. in the formula =ABS(INDIRECT("A1")), it returns
222 : VAL or ARR class instead. Additionally, the repeating conversion types RPT
223 : and RPX rely on the conversion executed for the function token class.
224 :
225 : 1) ORG:
226 : Use the original class of the token (VAL or ARR), regardless of any
227 : conversion done for the function return class.
228 :
229 : 2) VAL:
230 : Convert ARR tokens to VAL class, regardless of any conversion done for
231 : the function return class.
232 :
233 : 3) ARR:
234 : Convert VAL tokens to ARR class, regardless of any conversion done for
235 : the function return class.
236 :
237 : 4) RPT:
238 : If the own function returns REF class (thus it is called from a REFTYPE
239 : parameter, see above), and the parent conversion type (for the function
240 : return class) was ORG, VAL, or ARR, ignore that conversion and always
241 : use VAL conversion for the own token instead. If the parent conversion
242 : type was RPT or RPX, repeat the conversion that would have been used if
243 : the function would return value type.
244 : If the own function returns value type (VAL or ARR class, see above),
245 : and the parent conversion type (for the function return class) was ORG,
246 : VAL, ARR, or RPT, repeat this conversion for the own token. If the
247 : parent conversion type was RPX, always use ORG conversion type for the
248 : own token instead.
249 :
250 : 5) RPX:
251 : This type of conversion only occurs in functions returning VAL class by
252 : default. If the own token is value type, and the VAL return class of
253 : the own function has been changed to ARR class (due to direct ARR
254 : conversion, or due to ARR conversion repeated by RPT or RPX), set the
255 : own token to ARR type. Otherwise use the original token type (VAL
256 : conversion from parent parameter will not be repeated at all). If
257 : nested functions have RPT or value-type RPX parameters, they will not
258 : repeat this conversion type, but will use ORG conversion instead (see
259 : description of RPT above).
260 :
261 : 6) RPO:
262 : This type of conversion is only used for the operands of all operators
263 : (unary and binary arithmetic operators, comparison operators, and range
264 : operators). It is not used for function parameters. On conversion, it
265 : will be replaced by the last conversion type that was not the RPO
266 : conversion. This leads to a slightly different behaviour than the RPT
267 : conversion for operands in conjunction with a parent RPX conversion.
268 : */
269 : struct XclFuncParamInfo
270 : {
271 : XclFuncParamValidity meValid; /// Parameter validity.
272 : XclFuncParamConv meConv; /// Token class conversion type.
273 : bool mbValType; /// Data type (false = REFTYPE, true = VALTYPE).
274 : };
275 :
276 : // Function data ==============================================================
277 :
278 : const sal_uInt8 EXC_FUNC_MAXPARAM = 30; /// Maximum parameter count.
279 :
280 : const size_t EXC_FUNCINFO_PARAMINFO_COUNT = 5; /// Number of parameter info entries.
281 :
282 : const sal_uInt8 EXC_FUNCFLAG_VOLATILE = 0x01; /// Result is volatile (e.g. NOW() function).
283 : const sal_uInt8 EXC_FUNCFLAG_IMPORTONLY = 0x02; /// Only used in import filter.
284 : const sal_uInt8 EXC_FUNCFLAG_EXPORTONLY = 0x04; /// Only used in export filter.
285 : const sal_uInt8 EXC_FUNCFLAG_PARAMPAIRS = 0x08; /// Optional parameters are expected to appear in pairs.
286 : const sal_uInt8 EXC_FUNCFLAG_ADDINEQUIV = 0x10; /// Function is an add-in equivalent
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
314 : a macro call (UTF-8) EXC_FUNCFLAG_ADDINEQUIV is 0;
315 : or programmatical add-in name
316 : if stored as such and
317 : EXC_FUNCFLAG_ADDINEQUIV is set. */
318 :
319 : /** Returns true, if the function is volatile. */
320 0 : inline bool IsVolatile() const { return ::get_flag( mnFlags, EXC_FUNCFLAG_VOLATILE ); }
321 : /** Returns true, if optional parameters are expected to appear in pairs. */
322 0 : inline bool IsParamPairs() const { return ::get_flag( mnFlags, EXC_FUNCFLAG_PARAMPAIRS ); }
323 : /** Returns true, if the function parameter count is fixed. */
324 0 : inline bool IsFixedParamCount() const { return (mnXclFunc != EXC_FUNCID_EXTERNCALL) && (mnMinParamCount == mnMaxParamCount); }
325 : /** Returns true, if the function is simulated by a macro call. */
326 0 : inline bool IsMacroFunc() const { return mpcMacroName != 0 && !(mnFlags & EXC_FUNCFLAG_ADDINEQUIV); }
327 : /** Returns true, if the function is stored as an add-in call. */
328 0 : inline bool IsAddInEquivalent() const { return mpcMacroName != 0 && (mnFlags & EXC_FUNCFLAG_ADDINEQUIV); }
329 : /** Returns the name of the external function as string. */
330 : OUString GetMacroFuncName() const;
331 : /** Returns the programmatical name of the Add-In function as string. */
332 : OUString GetAddInEquivalentFuncName() const;
333 : };
334 :
335 : class XclRoot;
336 :
337 : /** Provides access to function info structs for all available functions. */
338 0 : class XclFunctionProvider
339 : {
340 : public:
341 : explicit XclFunctionProvider( const XclRoot& rRoot );
342 :
343 : /** Returns the function data for an Excel function index, or 0 on error. */
344 : const XclFunctionInfo* GetFuncInfoFromXclFunc( sal_uInt16 nXclFunc ) const;
345 : /** Returns the function data for an Excel function simulated by a macro call, or 0 on error. */
346 : const XclFunctionInfo* GetFuncInfoFromXclMacroName( const OUString& rXclMacroName ) const;
347 : /** Returns the function data for a Calc opcode, or 0 on error. */
348 : const XclFunctionInfo* GetFuncInfoFromOpCode( OpCode eOpCode ) const;
349 :
350 : private:
351 : void FillXclFuncMap( const XclFunctionInfo* pBeg, const XclFunctionInfo* pEnd );
352 : void FillScFuncMap( const XclFunctionInfo* pBeg, const XclFunctionInfo* pEnd );
353 :
354 : private:
355 : typedef ::std::map< sal_uInt16, const XclFunctionInfo* > XclFuncMap;
356 : typedef ::std::map< OUString, const XclFunctionInfo* > XclMacroNameMap;
357 : typedef ::std::map< OpCode, const XclFunctionInfo* > ScFuncMap;
358 :
359 : XclFuncMap maXclFuncMap; /// Maps Excel function indexes to function data.
360 : XclMacroNameMap maXclMacroNameMap; /// Maps macro function names to function data.
361 : ScFuncMap maScFuncMap; /// Maps Calc opcodes to function data.
362 : };
363 :
364 : // Token array ================================================================
365 :
366 : class XclImpStream;
367 : class XclExpStream;
368 :
369 : /** Binary representation of an Excel token array. */
370 0 : class XclTokenArray
371 : {
372 : public:
373 : /** Creates an empty token array. */
374 : explicit XclTokenArray( bool bVolatile = false );
375 : /** Creates a token array, swaps passed token vectors into own data. */
376 : explicit XclTokenArray( ScfUInt8Vec& rTokVec, ScfUInt8Vec& rExtDataVec, bool bVolatile = false );
377 :
378 : /** Returns true, if the token array is empty. */
379 0 : inline bool Empty() const { return maTokVec.empty(); }
380 : /** Returns the size of the token array in bytes. */
381 : sal_uInt16 GetSize() const;
382 : /** Returns read-only access to the byte vector storing token data. */
383 0 : inline const sal_uInt8* GetData() const { return maTokVec.empty() ? 0 : &maTokVec.front(); }
384 : /** Returns true, if the formula contains a volatile function. */
385 0 : inline bool IsVolatile() const { return mbVolatile; }
386 :
387 : /** Reads the size field of the token array. */
388 : void ReadSize( XclImpStream& rStrm );
389 : /** Reads the tokens of the token array (without size field). */
390 : void ReadArray( XclImpStream& rStrm );
391 : /** Reads size field and the tokens. */
392 : void Read( XclImpStream& rStrm );
393 :
394 : /** Writes the size field of the token array. */
395 : void WriteSize( XclExpStream& rStrm ) const;
396 : /** Writes the tokens of the token array (without size field). */
397 : void WriteArray( XclExpStream& rStrm ) const;
398 : /** Writes size field and the tokens. */
399 : void Write( XclExpStream& rStrm ) const;
400 :
401 : /** Compares this token array with the passed. */
402 : bool operator==( const XclTokenArray& rTokArr ) const;
403 :
404 : private:
405 : ScfUInt8Vec maTokVec; /// Byte vector containing token data.
406 : ScfUInt8Vec maExtDataVec; /// Byte vector containing extended data (arrays, stacked NLRs).
407 : bool mbVolatile; /// True = Formula contains volatile function.
408 : };
409 :
410 : typedef boost::shared_ptr< XclTokenArray > XclTokenArrayRef;
411 :
412 : /** Calls the Read() function at the passed token array. */
413 : XclImpStream& operator>>( XclImpStream& rStrm, XclTokenArray& rTokArr );
414 : /** Calls the Read() function at the passed token array. */
415 : XclImpStream& operator>>( XclImpStream& rStrm, XclTokenArrayRef& rxTokArr );
416 : /** Calls the Write() function at the passed token array. */
417 : XclExpStream& operator<<( XclExpStream& rStrm, const XclTokenArray& rTokArr );
418 : /** Calls the Write() function at the passed token array. */
419 : XclExpStream& operator<<( XclExpStream& rStrm, const XclTokenArrayRef& rxTokArr );
420 :
421 : namespace formula { class FormulaToken; }
422 : class ScTokenArray;
423 :
424 : /** Special token array iterator for the Excel filters.
425 :
426 : Iterates over a Calc token array without modifying it (therefore the
427 : iterator can be used with constant token arrays).
428 :
429 : Usage: Construct a new iterator object and pass a Calc token array, or use
430 : the Init() function to assign another Calc token array. As long as the Is()
431 : function returns true, the accessor functions can be used to get the
432 : current Calc token.
433 : */
434 : class XclTokenArrayIterator
435 : {
436 : public:
437 : explicit XclTokenArrayIterator();
438 : explicit XclTokenArrayIterator( const ScTokenArray& rScTokArr, bool bSkipSpaces );
439 : /** Copy constructor that allows to change the skip-spaces mode. */
440 : explicit XclTokenArrayIterator( const XclTokenArrayIterator& rTokArrIt, bool bSkipSpaces );
441 :
442 : void Init( const ScTokenArray& rScTokArr, bool bSkipSpaces );
443 :
444 0 : inline bool Is() const { return mppScToken != 0; }
445 0 : inline bool operator!() const { return !Is(); }
446 0 : inline const ::formula::FormulaToken* Get() const { return mppScToken ? *mppScToken : 0; }
447 0 : inline const ::formula::FormulaToken* operator->() const { return Get(); }
448 0 : inline const ::formula::FormulaToken& operator*() const { return *Get(); }
449 :
450 : XclTokenArrayIterator& operator++();
451 :
452 : private:
453 : void NextRawToken();
454 : void SkipSpaces();
455 :
456 : private:
457 : const ::formula::FormulaToken*const* mppScTokenBeg; /// Pointer to first token pointer of token array.
458 : const ::formula::FormulaToken*const* mppScTokenEnd; /// Pointer behind last token pointer of token array.
459 : const ::formula::FormulaToken*const* mppScToken; /// Pointer to current token pointer of token array.
460 : bool mbSkipSpaces; /// true = Skip whitespace tokens.
461 : };
462 :
463 : /** Contains all cell references that can be extracted from a multiple operations formula. */
464 0 : struct XclMultipleOpRefs
465 : {
466 : ScAddress maFmlaScPos; /// Position of the (first) formula cell.
467 : ScAddress maColFirstScPos;
468 : ScAddress maColRelScPos;
469 : ScAddress maRowFirstScPos;
470 : ScAddress maRowRelScPos;
471 : bool mbDblRefMode; /// true = One formula with row and column values.
472 : };
473 :
474 : /** A helper with Excel specific token array functions.
475 :
476 : The purpose to not add these functions to ScTokenArray is to prevent code
477 : changes in low-level Calc headers and to keep the Excel specific source
478 : code in the filter directory. Deriving from ScTokenArray is not viable
479 : because that would need expensive copy-constructions of the token arrays.
480 : */
481 0 : class XclTokenArrayHelper
482 : {
483 : public:
484 : // token identifiers ------------------------------------------------------
485 :
486 : /** Returns the base token ID of the passed (classified) token ID. */
487 : inline static sal_uInt8 GetBaseTokenId( sal_uInt8 nTokenId ) { return nTokenId & EXC_TOKID_MASK; }
488 : /** Returns the classified token ID from a base ID and the token class. */
489 : inline static sal_uInt8 GetTokenId( sal_uInt8 nBaseId, sal_uInt8 nTokenClass );
490 :
491 : /** Returns the token class of the passed token ID. */
492 0 : inline static sal_uInt8 GetTokenClass( sal_uInt8 nTokenId ) { return nTokenId & EXC_TOKCLASS_MASK; }
493 : /** Changes the token class in the passed classified token ID. */
494 : inline static void ChangeTokenClass( sal_uInt8& rnTokenId, sal_uInt8 nTokenClass );
495 :
496 : // strings and string lists -----------------------------------------------
497 :
498 : /** Tries to extract a string from the passed token.
499 : @param rString (out-parameter) The string contained in the token.
500 : @return true = Passed token is a string token, rString parameter is valid. */
501 : static bool GetTokenString( OUString& rString, const ::formula::FormulaToken& rScToken );
502 :
503 : /** Parses the passed formula and tries to find a single string token, i.e. "abc".
504 : @param rString (out-parameter) The string contained in the formula.
505 : @return true = String token found, rString parameter is valid. */
506 : static bool GetString( OUString& rString, const ScTokenArray& rScTokArr );
507 :
508 : /** Parses the passed formula and tries to find a string token list, i.e. "abc";"def";"ghi".
509 : @descr Returns the unquoted (!) strings in a single string, separated with the
510 : passed character. If a comma is specified, the function will return abc,def,ghi from
511 : the example above.
512 : @param rStringList (out-parameter) All strings contained in the formula as list.
513 : @param cSep List separator character.
514 : @return true = String token list found, rString parameter is valid. */
515 : static bool GetStringList( OUString& rStringList, const ScTokenArray& rScTokArr, sal_Unicode cSep );
516 :
517 : /** Tries to convert a formula that consists of a single string token to a list of strings.
518 : @descr Example: The formula ="abc\ndef\nghi" will be converted to the formula
519 : ="abc";"def";"ghi", if the LF character is specified as separator.
520 : @param rScTokArr (in/out-parameter) The token array to modify.
521 : @param cStringSep The separator in the source string.
522 : @param bTrimLeadingSpaces true = remove leading spaces from each token. */
523 : static void ConvertStringToList( ScTokenArray& rScTokArr, sal_Unicode cStringSep, bool bTrimLeadingSpaces );
524 :
525 : // multiple operations ----------------------------------------------------
526 :
527 : /** Parses the passed formula and tries to extract references of a multiple operation.
528 : @descr Requires that the formula contains a single MULTIPLE.OPERATION function call.
529 : Spaces in the formula are silently ignored.
530 : @return true = Multiple operation found, and all references successfully extracted. */
531 : static bool GetMultipleOpRefs( XclMultipleOpRefs& rRefs, const ScTokenArray& rScTokArr, const ScAddress& rScPos );
532 : };
533 :
534 0 : inline sal_uInt8 XclTokenArrayHelper::GetTokenId( sal_uInt8 nBaseId, sal_uInt8 nTokenClass )
535 : {
536 : OSL_ENSURE( !::get_flag( nBaseId, static_cast< sal_uInt8 >( ~EXC_TOKID_MASK ) ), "XclTokenArrayHelper::GetTokenId - invalid token ID" );
537 : OSL_ENSURE( !::get_flag( nTokenClass, static_cast< sal_uInt8 >( ~EXC_TOKCLASS_MASK ) ), "XclTokenArrayHelper::GetTokenId - invalid token class" );
538 0 : return nBaseId | nTokenClass;
539 : }
540 :
541 0 : inline void XclTokenArrayHelper::ChangeTokenClass( sal_uInt8& rnTokenId, sal_uInt8 nTokenClass )
542 : {
543 : OSL_ENSURE( !::get_flag( nTokenClass, static_cast< sal_uInt8 >( ~EXC_TOKCLASS_MASK ) ), "XclTokenArrayHelper::ChangeTokenClass - invalid token class" );
544 0 : ::set_flag( rnTokenId, EXC_TOKCLASS_MASK, false );
545 0 : ::set_flag( rnTokenId, nTokenClass );
546 0 : }
547 :
548 : #endif
549 :
550 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|