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 INCLUDED_SC_SOURCE_FILTER_INC_TOKSTACK_HXX
21 : #define INCLUDED_SC_SOURCE_FILTER_INC_TOKSTACK_HXX
22 :
23 : #include <string.h>
24 : #include "compiler.hxx"
25 : #include "tokenarray.hxx"
26 :
27 : #include <vector>
28 :
29 : namespace svl {
30 :
31 : class SharedStringPool;
32 :
33 : }
34 :
35 : typedef OpCode DefTokenId;
36 : // in PRODUCT version: ambiguity between OpCode (being sal_uInt16) and UINT16
37 : // Unfortunately a typedef is just a dumb alias and not a real type ...
38 : //typedef sal_uInt16 TokenId;
39 : struct TokenId
40 : {
41 : sal_uInt16 nId;
42 :
43 2430916 : TokenId() : nId( 0 ) {}
44 44392 : TokenId( sal_uInt16 n ) : nId( n ) {}
45 44346 : TokenId( const TokenId& r ) : nId( r.nId ) {}
46 107132 : inline TokenId& operator =( const TokenId& r ) { nId = r.nId; return *this; }
47 0 : inline TokenId& operator =( sal_uInt16 n ) { nId = n; return *this; }
48 : inline operator sal_uInt16&() { return nId; }
49 50056 : inline operator const sal_uInt16&() const { return nId; }
50 : inline bool operator <( sal_uInt16 n ) const { return nId < n; }
51 : inline bool operator >( sal_uInt16 n ) const { return nId > n; }
52 : inline bool operator <=( sal_uInt16 n ) const { return nId <= n; }
53 : inline bool operator >=( sal_uInt16 n ) const { return nId >= n; }
54 : inline bool operator ==( sal_uInt16 n ) const { return nId == n; }
55 : inline bool operator !=( sal_uInt16 n ) const { return nId != n; }
56 : };
57 :
58 : struct ScComplexRefData;
59 : class TokenStack;
60 :
61 : enum E_TYPE
62 : {
63 : T_Id, // Id-Folge
64 : T_Str, // String
65 : T_D, // Double
66 : T_Err, // Error code
67 : T_RefC, // Cell Reference
68 : T_RefA, // Area Reference
69 : T_RN, // Range Name
70 : T_Ext, // something unknown with function name
71 : T_Nlf, // token for natural language formula
72 : T_Matrix, // token for inline arrays
73 : T_ExtName, // token for external names
74 : T_ExtRefC,
75 : T_ExtRefA,
76 : T_Error // for check in case of error
77 : };
78 :
79 : class TokenPool
80 : {
81 : // !ATTENTION!: external Id-Basis is 1, internal 0!
82 : // return Id = 0 -> Error
83 : private:
84 : svl::SharedStringPool& mrStringPool;
85 :
86 : OUString** ppP_Str; // Pool for Strings
87 : sal_uInt16 nP_Str; // ...with size
88 : sal_uInt16 nP_StrAkt; // ...and Write-Mark
89 :
90 : double* pP_Dbl; // Pool for Doubles
91 : sal_uInt16 nP_Dbl;
92 : sal_uInt16 nP_DblAkt;
93 :
94 : sal_uInt16* pP_Err; // Pool for error codes
95 : sal_uInt16 nP_Err;
96 : sal_uInt16 nP_ErrAkt;
97 :
98 : ScSingleRefData** ppP_RefTr; // Pool for References
99 : sal_uInt16 nP_RefTr;
100 : sal_uInt16 nP_RefTrAkt;
101 :
102 : sal_uInt16* pP_Id; // Pool for Id-sets
103 : sal_uInt16 nP_Id;
104 : sal_uInt16 nP_IdAkt;
105 : sal_uInt16 nP_IdLast; // last set-start
106 :
107 4 : struct EXTCONT
108 : {
109 : DefTokenId eId;
110 : OUString aText;
111 4 : EXTCONT( const DefTokenId e, const OUString& r ) :
112 4 : eId( e ), aText( r ){}
113 : };
114 : EXTCONT** ppP_Ext;
115 : sal_uInt16 nP_Ext;
116 : sal_uInt16 nP_ExtAkt;
117 :
118 : struct NLFCONT
119 : {
120 : ScSingleRefData aRef;
121 0 : NLFCONT( const ScSingleRefData& r ) : aRef( r ) {}
122 : };
123 : NLFCONT** ppP_Nlf;
124 : sal_uInt16 nP_Nlf;
125 : sal_uInt16 nP_NlfAkt;
126 :
127 : ScMatrix** ppP_Matrix; // Pool for Matrices
128 : sal_uInt16 nP_Matrix;
129 : sal_uInt16 nP_MatrixAkt;
130 :
131 : /** for storage of named ranges */
132 : struct RangeName
133 : {
134 : sal_uInt16 mnIndex;
135 : bool mbGlobal;
136 : };
137 : ::std::vector<RangeName> maRangeNames;
138 :
139 : /** for storage of external names */
140 0 : struct ExtName
141 : {
142 : sal_uInt16 mnFileId;
143 : OUString maName;
144 : };
145 : ::std::vector<ExtName> maExtNames;
146 :
147 : /** for storage of external cell references */
148 16 : struct ExtCellRef
149 : {
150 : sal_uInt16 mnFileId;
151 : OUString maTabName;
152 : ScSingleRefData maRef;
153 : };
154 : ::std::vector<ExtCellRef> maExtCellRefs;
155 :
156 : /** for storage of external area references */
157 0 : struct ExtAreaRef
158 : {
159 : sal_uInt16 mnFileId;
160 : OUString maTabName;
161 : ScComplexRefData maRef;
162 : };
163 : ::std::vector<ExtAreaRef> maExtAreaRefs;
164 :
165 : sal_uInt16* pElement; // Array with Indices for elements
166 : E_TYPE* pType; // ...with Type-Info
167 : sal_uInt16* pSize; // ...with size (Anz. sal_uInt16)
168 : sal_uInt16 nElement;
169 : sal_uInt16 nElementAkt;
170 :
171 : static const sal_uInt16 nScTokenOff;// Offset for SC-Token
172 : #ifdef DBG_UTIL
173 : sal_uInt16 m_nRek; // recursion counter
174 : #endif
175 : ScTokenArray* pScToken; // Token array
176 :
177 : bool GrowString( void );
178 : bool GrowDouble( void );
179 : /* TODO: in case we had FormulaTokenArray::AddError() */
180 : #if 0
181 : bool GrowError( void );
182 : #endif
183 : bool GrowTripel( sal_uInt16 nByMin = 1 );
184 : bool GrowId( void );
185 : bool GrowElement( void );
186 : bool GrowExt( void );
187 : bool GrowNlf( void );
188 : bool GrowMatrix( void );
189 : bool GetElement( const sal_uInt16 nId );
190 : bool GetElementRek( const sal_uInt16 nId );
191 : public:
192 : TokenPool( svl::SharedStringPool& rSPool );
193 : ~TokenPool();
194 : inline TokenPool& operator <<( const TokenId nId );
195 : inline TokenPool& operator <<( const DefTokenId eId );
196 : inline TokenPool& operator <<( TokenStack& rStack );
197 : void operator >>( TokenId& rId );
198 : inline void operator >>( TokenStack& rStack );
199 : inline const TokenId Store( void );
200 : const TokenId Store( const double& rDouble );
201 :
202 : // only for Range-Names
203 : const TokenId Store( const sal_uInt16 nIndex );
204 : inline const TokenId Store( const sal_Int16 nWert );
205 : const TokenId Store( const OUString& rString );
206 : const TokenId Store( const ScSingleRefData& rTr );
207 : const TokenId Store( const ScComplexRefData& rTr );
208 :
209 : const TokenId Store( const DefTokenId eId, const OUString& rName );
210 : // 4 externals (e.g. AddIns, Macros...)
211 : const TokenId StoreNlf( const ScSingleRefData& rTr );
212 : const TokenId StoreMatrix();
213 : const TokenId StoreName( sal_uInt16 nIndex, bool bGlobal );
214 : const TokenId StoreExtName( sal_uInt16 nFileId, const OUString& rName );
215 : const TokenId StoreExtRef( sal_uInt16 nFileId, const OUString& rTabName, const ScSingleRefData& rRef );
216 : const TokenId StoreExtRef( sal_uInt16 nFileId, const OUString& rTabName, const ScComplexRefData& rRef );
217 :
218 : inline const TokenId LastId( void ) const;
219 : inline const ScTokenArray* operator []( const TokenId nId );
220 : void Reset( void );
221 : inline E_TYPE GetType( const TokenId& nId ) const;
222 : bool IsSingleOp( const TokenId& nId, const DefTokenId eId ) const;
223 : const OUString* GetExternal( const TokenId& nId ) const;
224 : ScMatrix* GetMatrix( unsigned int n ) const;
225 : };
226 :
227 : class TokenStack
228 : // Stack for Token-Ids: reserve Id=0 for error; e.g. Get() returns 0 on error
229 :
230 : {
231 : private:
232 : TokenId* pStack; // Stack as Array
233 : sal_uInt16 nPos; // Write-mark
234 : sal_uInt16 nSize; // first Index outside of stack
235 : public:
236 : TokenStack( sal_uInt16 nNewSize = 1024 );
237 : ~TokenStack();
238 : inline TokenStack& operator <<( const TokenId nNewId );
239 : inline void operator >>( TokenId &rId );
240 :
241 : inline void Reset( void );
242 :
243 20534 : inline bool HasMoreTokens() const { return nPos > 0; }
244 : inline const TokenId Get( void );
245 : };
246 :
247 18728 : inline const TokenId TokenStack::Get( void )
248 : {
249 : OSL_ENSURE( nPos > 0,
250 : "*TokenStack::Get(): is empty, is empty, ..." );
251 :
252 18728 : TokenId nRet;
253 :
254 18728 : if( nPos == 0 )
255 0 : nRet = 0;
256 : else
257 : {
258 18728 : nPos--;
259 18728 : nRet = pStack[ nPos ];
260 : }
261 :
262 18728 : return nRet;
263 : }
264 :
265 43064 : inline TokenStack &TokenStack::operator <<( const TokenId nNewId )
266 : {// Element on Stack
267 : OSL_ENSURE( nPos < nSize, "*TokenStack::<<(): Stack overflow" );
268 43064 : if( nPos < nSize )
269 : {
270 43064 : pStack[ nPos ] = nNewId;
271 43064 : nPos++;
272 : }
273 :
274 43064 : return *this;
275 : }
276 :
277 24332 : inline void TokenStack::operator >>( TokenId& rId )
278 : {// Element of Stack
279 : OSL_ENSURE( nPos > 0,
280 : "*TokenStack::>>(): is empty, is empty, ..." );
281 24332 : if( nPos > 0 )
282 : {
283 24332 : nPos--;
284 24332 : rId = pStack[ nPos ];
285 : }
286 24332 : }
287 :
288 9082 : inline void TokenStack::Reset( void )
289 : {
290 9082 : nPos = 0;
291 9082 : }
292 :
293 24644 : inline TokenPool& TokenPool::operator <<( const TokenId nId )
294 : {
295 : // POST: nId's are stored consecutively in Pool under a new Id;
296 : // finalize with >> or Store()
297 : // nId -> ( sal_uInt16 ) nId - 1;
298 : OSL_ENSURE( ( sal_uInt16 ) nId < nScTokenOff,
299 : "-TokenPool::operator <<: TokenId in DefToken-Range!" );
300 :
301 24644 : if( nP_IdAkt >= nP_Id )
302 0 : if (!GrowId())
303 0 : return *this;
304 :
305 24644 : pP_Id[ nP_IdAkt ] = ( ( sal_uInt16 ) nId ) - 1;
306 24644 : nP_IdAkt++;
307 :
308 24644 : return *this;
309 : }
310 :
311 42760 : inline TokenPool& TokenPool::operator <<( const DefTokenId eId )
312 : {
313 : OSL_ENSURE( ( sal_uInt32 ) eId + nScTokenOff < 0xFFFF,
314 : "-TokenPool::operator<<: enmum too large!" );
315 :
316 42760 : if( nP_IdAkt >= nP_Id )
317 0 : if (!GrowId())
318 0 : return *this;
319 :
320 42760 : pP_Id[ nP_IdAkt ] = ( ( sal_uInt16 ) eId ) + nScTokenOff;
321 42760 : nP_IdAkt++;
322 :
323 42760 : return *this;
324 : }
325 :
326 12418 : inline TokenPool& TokenPool::operator <<( TokenStack& rStack )
327 : {
328 12418 : if( nP_IdAkt >= nP_Id )
329 0 : if (!GrowId())
330 0 : return *this;
331 :
332 12418 : pP_Id[ nP_IdAkt ] = ( ( sal_uInt16 ) rStack.Get() ) - 1;
333 12418 : nP_IdAkt++;
334 :
335 12418 : return *this;
336 : }
337 :
338 19338 : inline void TokenPool::operator >>( TokenStack& rStack )
339 : {
340 19338 : TokenId nId;
341 19338 : *this >> nId;
342 19338 : rStack << nId;
343 19338 : }
344 :
345 664 : inline const TokenId TokenPool::Store( void )
346 : {
347 664 : TokenId nId;
348 664 : *this >> nId;
349 664 : return nId;
350 : }
351 :
352 : inline const TokenId TokenPool::Store( const sal_Int16 nWert )
353 : {
354 : return Store( ( double ) nWert );
355 : }
356 :
357 : inline const TokenId TokenPool::LastId( void ) const
358 : {
359 : return static_cast<TokenId>(nElementAkt); // correct, as Ausgabe with Offset 1!
360 : }
361 :
362 6310 : const inline ScTokenArray* TokenPool::operator []( const TokenId nId )
363 : {
364 6310 : pScToken->Clear();
365 :
366 6310 : if( nId )
367 : {//...only if nId > 0!
368 : #ifdef DBG_UTIL
369 : m_nRek = 0;
370 : #endif
371 6310 : GetElement( ( sal_uInt16 ) nId - 1 );
372 : }
373 :
374 6310 : return pScToken;
375 : }
376 :
377 : inline E_TYPE TokenPool::GetType( const TokenId& rId ) const
378 : {
379 : E_TYPE nRet;
380 :
381 : sal_uInt16 nId = (sal_uInt16) rId - 1;
382 :
383 : if( nId < nElementAkt )
384 : nRet = pType[ nId ] ;
385 : else
386 : nRet = T_Error;
387 :
388 : return nRet;
389 : }
390 :
391 : #endif
392 :
393 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|