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 "tokstack.hxx"
21 : #include "compiler.hxx"
22 : #include "global.hxx"
23 : #include "scmatrix.hxx"
24 :
25 : #include <svl/sharedstringpool.hxx>
26 :
27 : #include <string.h>
28 :
29 : const sal_uInt16 TokenPool::nScTokenOff = 8192;
30 :
31 338 : TokenStack::TokenStack( sal_uInt16 nNewSize )
32 : {
33 338 : pStack = new TokenId[ nNewSize ];
34 :
35 338 : Reset();
36 338 : nSize = nNewSize;
37 338 : }
38 :
39 338 : TokenStack::~TokenStack()
40 : {
41 338 : delete[] pStack;
42 338 : }
43 :
44 : // !ATTENTION!": to the outside the numbering starts with 1!
45 : // !ATTENTION!": SC-Token are stored with an offset nScTokenOff
46 : // -> to distinguish from other tokens
47 :
48 338 : TokenPool::TokenPool( svl::SharedStringPool& rSPool ) :
49 338 : mrStringPool(rSPool)
50 : {
51 338 : sal_uInt16 nLauf = nScTokenOff;
52 :
53 : // pool for Id-sequences
54 338 : nP_Id = 256;
55 338 : pP_Id = new sal_uInt16[ nP_Id ];
56 :
57 : // pool for Ids
58 338 : nElement = 32;
59 338 : pElement = new sal_uInt16[ nElement ];
60 338 : pType = new E_TYPE[ nElement ];
61 338 : pSize = new sal_uInt16[ nElement ];
62 338 : nP_IdLast = 0;
63 :
64 : // pool for strings
65 338 : nP_Str = 4;
66 338 : ppP_Str = new OUString *[ nP_Str ];
67 1690 : for( nLauf = 0 ; nLauf < nP_Str ; nLauf++ )
68 1352 : ppP_Str[ nLauf ] = NULL;
69 :
70 : // pool for double
71 338 : nP_Dbl = 8;
72 338 : pP_Dbl = new double[ nP_Dbl ];
73 :
74 : // pool for error codes
75 338 : nP_Err = 8;
76 338 : pP_Err = new sal_uInt16[ nP_Err ];
77 :
78 : // pool for References
79 338 : nP_RefTr = 32;
80 338 : ppP_RefTr = new ScSingleRefData *[ nP_RefTr ];
81 11154 : for( nLauf = 0 ; nLauf < nP_RefTr ; nLauf++ )
82 10816 : ppP_RefTr[ nLauf ] = NULL;
83 :
84 338 : nP_Ext = 32;
85 338 : ppP_Ext = new EXTCONT*[ nP_Ext ];
86 338 : memset( ppP_Ext, 0, sizeof( EXTCONT* ) * nP_Ext );
87 :
88 338 : nP_Nlf = 16;
89 338 : ppP_Nlf = new NLFCONT*[ nP_Nlf ];
90 338 : memset( ppP_Nlf, 0, sizeof( NLFCONT* ) * nP_Nlf );
91 :
92 338 : nP_Matrix = 16;
93 338 : ppP_Matrix = new ScMatrix*[ nP_Matrix ];
94 338 : memset( ppP_Matrix, 0, sizeof( ScMatrix* ) * nP_Matrix );
95 :
96 338 : pScToken = new ScTokenArray;
97 :
98 338 : Reset();
99 338 : }
100 :
101 676 : TokenPool::~TokenPool()
102 : {
103 : sal_uInt16 n;
104 :
105 338 : delete[] pP_Id;
106 338 : delete[] pElement;
107 338 : delete[] pType;
108 338 : delete[] pSize;
109 338 : delete[] pP_Dbl;
110 338 : delete[] pP_Err;
111 :
112 11154 : for( n = 0 ; n < nP_RefTr ; n++ )
113 10816 : delete ppP_RefTr[ n ];
114 338 : delete[] ppP_RefTr;
115 :
116 1694 : for( n = 0 ; n < nP_Str ; n++ )
117 1356 : delete ppP_Str[ n ];
118 338 : delete[] ppP_Str;
119 :
120 11154 : for( n = 0 ; n < nP_Ext ; n++ )
121 10816 : delete ppP_Ext[ n ];
122 338 : delete[] ppP_Ext;
123 :
124 5746 : for( n = 0 ; n < nP_Nlf ; n++ )
125 5408 : delete ppP_Nlf[ n ];
126 338 : delete[] ppP_Nlf;
127 :
128 5746 : for( n = 0 ; n < nP_Matrix ; n++ )
129 : {
130 5408 : if( ppP_Matrix[ n ] )
131 3 : ppP_Matrix[ n ]->DecRef( );
132 : }
133 338 : delete[] ppP_Matrix;
134 :
135 338 : delete pScToken;
136 338 : }
137 :
138 : /** Returns the new number of elements, or 0 if overflow. */
139 3 : static sal_uInt16 lcl_canGrow( sal_uInt16 nOld, sal_uInt16 nByMin = 1 )
140 : {
141 3 : if (!nOld)
142 0 : return nByMin ? nByMin : 1;
143 3 : if (nOld == SAL_MAX_UINT16)
144 0 : return 0;
145 3 : sal_uInt32 nNew = ::std::max( static_cast<sal_uInt32>(nOld) * 2,
146 6 : static_cast<sal_uInt32>(nOld) + nByMin);
147 3 : if (nNew > SAL_MAX_UINT16)
148 0 : nNew = SAL_MAX_UINT16;
149 3 : if (nNew - nByMin < nOld)
150 0 : nNew = 0;
151 3 : return static_cast<sal_uInt16>(nNew);
152 : }
153 :
154 1 : bool TokenPool::GrowString()
155 : {
156 1 : sal_uInt16 nP_StrNew = lcl_canGrow( nP_Str);
157 1 : if (!nP_StrNew)
158 0 : return false;
159 :
160 : sal_uInt16 nL;
161 :
162 1 : OUString** ppP_StrNew = new (::std::nothrow) OUString *[ nP_StrNew ];
163 1 : if (!ppP_StrNew)
164 0 : return false;
165 :
166 5 : for( nL = 0 ; nL < nP_Str ; nL++ )
167 4 : ppP_StrNew[ nL ] = ppP_Str[ nL ];
168 5 : for( nL = nP_Str ; nL < nP_StrNew ; nL++ )
169 4 : ppP_StrNew[ nL ] = NULL;
170 :
171 1 : nP_Str = nP_StrNew;
172 :
173 1 : delete[] ppP_Str;
174 1 : ppP_Str = ppP_StrNew;
175 1 : return true;
176 : }
177 :
178 0 : bool TokenPool::GrowDouble()
179 : {
180 0 : sal_uInt16 nP_DblNew = lcl_canGrow( nP_Dbl);
181 0 : if (!nP_DblNew)
182 0 : return false;
183 :
184 0 : double* pP_DblNew = new (::std::nothrow) double[ nP_DblNew ];
185 0 : if (!pP_DblNew)
186 0 : return false;
187 :
188 0 : for( sal_uInt16 nL = 0 ; nL < nP_Dbl ; nL++ )
189 0 : pP_DblNew[ nL ] = pP_Dbl[ nL ];
190 :
191 0 : nP_Dbl = nP_DblNew;
192 :
193 0 : delete[] pP_Dbl;
194 0 : pP_Dbl = pP_DblNew;
195 0 : return true;
196 : }
197 :
198 : /* TODO: in case we had FormulaTokenArray::AddError() */
199 : #if 0
200 : void TokenPool::GrowError()
201 : {
202 : sal_uInt16 nP_ErrNew = lcl_canGrow( nP_Err);
203 : if (!nP_ErrNew)
204 : return false;
205 :
206 : sal_uInt16* pP_ErrNew = new (::std::nothrow) sal_uInt16[ nP_ErrNew ];
207 : if (!pP_ErrNew)
208 : return false;
209 :
210 : for( sal_uInt16 nL = 0 ; nL < nP_Err ; nL++ )
211 : pP_ErrNew[ nL ] = pP_Err[ nL ];
212 :
213 : nP_Err = nP_ErrNew;
214 :
215 : delete[] pP_Err;
216 : pP_Err = pP_ErrNew;
217 : return true;
218 : }
219 : #endif
220 :
221 0 : bool TokenPool::GrowTripel( sal_uInt16 nByMin )
222 : {
223 0 : sal_uInt16 nP_RefTrNew = lcl_canGrow( nP_RefTr, nByMin);
224 0 : if (!nP_RefTrNew)
225 0 : return false;
226 :
227 : sal_uInt16 nL;
228 :
229 0 : ScSingleRefData** ppP_RefTrNew = new (::std::nothrow) ScSingleRefData *[ nP_RefTrNew ];
230 0 : if (!ppP_RefTrNew)
231 0 : return false;
232 :
233 0 : for( nL = 0 ; nL < nP_RefTr ; nL++ )
234 0 : ppP_RefTrNew[ nL ] = ppP_RefTr[ nL ];
235 0 : for( nL = nP_RefTr ; nL < nP_RefTrNew ; nL++ )
236 0 : ppP_RefTrNew[ nL ] = NULL;
237 :
238 0 : nP_RefTr = nP_RefTrNew;
239 :
240 0 : delete[] ppP_RefTr;
241 0 : ppP_RefTr = ppP_RefTrNew;
242 0 : return true;
243 : }
244 :
245 0 : bool TokenPool::GrowId()
246 : {
247 0 : sal_uInt16 nP_IdNew = lcl_canGrow( nP_Id);
248 0 : if (!nP_IdNew)
249 0 : return false;
250 :
251 0 : sal_uInt16* pP_IdNew = new (::std::nothrow) sal_uInt16[ nP_IdNew ];
252 0 : if (!pP_IdNew)
253 0 : return false;
254 :
255 0 : for( sal_uInt16 nL = 0 ; nL < nP_Id ; nL++ )
256 0 : pP_IdNew[ nL ] = pP_Id[ nL ];
257 :
258 0 : nP_Id = nP_IdNew;
259 :
260 0 : delete[] pP_Id;
261 0 : pP_Id = pP_IdNew;
262 0 : return true;
263 : }
264 :
265 2 : bool TokenPool::GrowElement()
266 : {
267 2 : sal_uInt16 nElementNew = lcl_canGrow( nElement);
268 2 : if (!nElementNew)
269 0 : return false;
270 :
271 2 : sal_uInt16* pElementNew = new (::std::nothrow) sal_uInt16[ nElementNew ];
272 2 : E_TYPE* pTypeNew = new (::std::nothrow) E_TYPE[ nElementNew ];
273 2 : sal_uInt16* pSizeNew = new (::std::nothrow) sal_uInt16[ nElementNew ];
274 2 : if (!pElementNew || !pTypeNew || !pSizeNew)
275 : {
276 0 : delete [] pElementNew;
277 0 : delete [] pTypeNew;
278 0 : delete [] pSizeNew;
279 0 : return false;
280 : }
281 :
282 66 : for( sal_uInt16 nL = 0 ; nL < nElement ; nL++ )
283 : {
284 64 : pElementNew[ nL ] = pElement[ nL ];
285 64 : pTypeNew[ nL ] = pType[ nL ];
286 64 : pSizeNew[ nL ] = pSize[ nL ];
287 : }
288 :
289 2 : nElement = nElementNew;
290 :
291 2 : delete[] pElement;
292 2 : delete[] pType;
293 2 : delete[] pSize;
294 2 : pElement = pElementNew;
295 2 : pType = pTypeNew;
296 2 : pSize = pSizeNew;
297 2 : return true;
298 : }
299 :
300 0 : bool TokenPool::GrowExt()
301 : {
302 0 : sal_uInt16 nNewSize = lcl_canGrow( nP_Ext);
303 0 : if (!nNewSize)
304 0 : return false;
305 :
306 0 : EXTCONT** ppNew = new (::std::nothrow) EXTCONT*[ nNewSize ];
307 0 : if (!ppNew)
308 0 : return false;
309 :
310 0 : memset( ppNew, 0, sizeof( EXTCONT* ) * nNewSize );
311 0 : memcpy( ppNew, ppP_Ext, sizeof( EXTCONT* ) * nP_Ext );
312 :
313 0 : delete[] ppP_Ext;
314 0 : ppP_Ext = ppNew;
315 0 : nP_Ext = nNewSize;
316 0 : return true;
317 : }
318 :
319 0 : bool TokenPool::GrowNlf()
320 : {
321 0 : sal_uInt16 nNewSize = lcl_canGrow( nP_Nlf);
322 0 : if (!nNewSize)
323 0 : return false;
324 :
325 0 : NLFCONT** ppNew = new (::std::nothrow) NLFCONT*[ nNewSize ];
326 0 : if (!ppNew)
327 0 : return false;
328 :
329 0 : memset( ppNew, 0, sizeof( NLFCONT* ) * nNewSize );
330 0 : memcpy( ppNew, ppP_Nlf, sizeof( NLFCONT* ) * nP_Nlf );
331 :
332 0 : delete[] ppP_Nlf;
333 0 : ppP_Nlf = ppNew;
334 0 : nP_Nlf = nNewSize;
335 0 : return true;
336 : }
337 :
338 0 : bool TokenPool::GrowMatrix()
339 : {
340 0 : sal_uInt16 nNewSize = lcl_canGrow( nP_Matrix);
341 0 : if (!nNewSize)
342 0 : return false;
343 :
344 0 : ScMatrix** ppNew = new (::std::nothrow) ScMatrix*[ nNewSize ];
345 0 : if (!ppNew)
346 0 : return false;
347 :
348 0 : memset( ppNew, 0, sizeof( ScMatrix* ) * nNewSize );
349 0 : memcpy( ppNew, ppP_Matrix, sizeof( ScMatrix* ) * nP_Matrix );
350 :
351 0 : delete[] ppP_Matrix;
352 0 : ppP_Matrix = ppNew;
353 0 : nP_Matrix = nNewSize;
354 0 : return true;
355 : }
356 :
357 14618 : bool TokenPool::GetElement( const sal_uInt16 nId )
358 : {
359 : OSL_ENSURE( nId < nElementAkt, "*TokenPool::GetElement(): Id too large!?" );
360 14618 : if (nId >= nElementAkt)
361 0 : return false;
362 :
363 14618 : bool bRet = true;
364 14618 : if( pType[ nId ] == T_Id )
365 2912 : bRet = GetElementRek( nId );
366 : else
367 : {
368 11706 : switch( pType[ nId ] )
369 : {
370 : case T_Str:
371 : {
372 2737 : sal_uInt16 n = pElement[ nId ];
373 2737 : OUString* p = ( n < nP_Str )? ppP_Str[ n ] : NULL;
374 2737 : if (p)
375 2737 : pScToken->AddString(mrStringPool.intern(*p));
376 : else
377 0 : bRet = false;
378 : }
379 2737 : break;
380 : case T_D:
381 : {
382 1388 : sal_uInt16 n = pElement[ nId ];
383 1388 : if (n < nP_Dbl)
384 1388 : pScToken->AddDouble( pP_Dbl[ n ] );
385 : else
386 0 : bRet = false;
387 : }
388 1388 : break;
389 : case T_Err:
390 : /* TODO: in case we had FormulaTokenArray::AddError() */
391 : #if 0
392 : {
393 : sal_uInt16 n = pElement[ nId ];
394 : if (n < nP_Err)
395 : pScToken->AddError( pP_Err[ n ] );
396 : else
397 : bRet = false;
398 : }
399 : #endif
400 0 : break;
401 : case T_RefC:
402 : {
403 5189 : sal_uInt16 n = pElement[ nId ];
404 5189 : ScSingleRefData* p = ( n < nP_RefTr )? ppP_RefTr[ n ] : NULL;
405 5189 : if (p)
406 5189 : pScToken->AddSingleReference( *p );
407 : else
408 0 : bRet = false;
409 : }
410 5189 : break;
411 : case T_RefA:
412 : {
413 2362 : sal_uInt16 n = pElement[ nId ];
414 2362 : if (n < nP_RefTr && ppP_RefTr[ n ] && n+1 < nP_RefTr && ppP_RefTr[ n + 1 ])
415 : {
416 : ScComplexRefData aScComplexRefData;
417 2362 : aScComplexRefData.Ref1 = *ppP_RefTr[ n ];
418 2362 : aScComplexRefData.Ref2 = *ppP_RefTr[ n + 1 ];
419 2362 : pScToken->AddDoubleReference( aScComplexRefData );
420 : }
421 : else
422 0 : bRet = false;
423 : }
424 2362 : break;
425 : case T_RN:
426 : {
427 20 : sal_uInt16 n = pElement[nId];
428 20 : if (n < maRangeNames.size())
429 : {
430 20 : const RangeName& r = maRangeNames[n];
431 20 : pScToken->AddRangeName(r.mnIndex, r.mbGlobal);
432 : }
433 : }
434 20 : break;
435 : case T_Ext:
436 : {
437 1 : sal_uInt16 n = pElement[ nId ];
438 1 : EXTCONT* p = ( n < nP_Ext )? ppP_Ext[ n ] : NULL;
439 :
440 1 : if( p )
441 : {
442 1 : if( p->eId == ocEuroConvert )
443 0 : pScToken->AddOpCode( p->eId );
444 : else
445 1 : pScToken->AddExternal( p->aText, p->eId );
446 : }
447 : else
448 0 : bRet = false;
449 : }
450 1 : break;
451 : case T_Nlf:
452 : {
453 0 : sal_uInt16 n = pElement[ nId ];
454 0 : NLFCONT* p = ( n < nP_Nlf )? ppP_Nlf[ n ] : NULL;
455 :
456 0 : if( p )
457 0 : pScToken->AddColRowName( p->aRef );
458 : else
459 0 : bRet = false;
460 : }
461 0 : break;
462 : case T_Matrix:
463 : {
464 7 : sal_uInt16 n = pElement[ nId ];
465 7 : ScMatrix* p = ( n < nP_Matrix )? ppP_Matrix[ n ] : NULL;
466 :
467 7 : if( p )
468 7 : pScToken->AddMatrix( p );
469 : else
470 0 : bRet = false;
471 : }
472 7 : break;
473 : case T_ExtName:
474 : {
475 0 : sal_uInt16 n = pElement[nId];
476 0 : if (n < maExtNames.size())
477 : {
478 0 : const ExtName& r = maExtNames[n];
479 0 : pScToken->AddExternalName(r.mnFileId, r.maName);
480 : }
481 : else
482 0 : bRet = false;
483 : }
484 0 : break;
485 : case T_ExtRefC:
486 : {
487 2 : sal_uInt16 n = pElement[nId];
488 2 : if (n < maExtCellRefs.size())
489 : {
490 2 : const ExtCellRef& r = maExtCellRefs[n];
491 2 : pScToken->AddExternalSingleReference(r.mnFileId, r.maTabName, r.maRef);
492 : }
493 : else
494 0 : bRet = false;
495 : }
496 2 : break;
497 : case T_ExtRefA:
498 : {
499 0 : sal_uInt16 n = pElement[nId];
500 0 : if (n < maExtAreaRefs.size())
501 : {
502 0 : const ExtAreaRef& r = maExtAreaRefs[n];
503 0 : pScToken->AddExternalDoubleReference(r.mnFileId, r.maTabName, r.maRef);
504 : }
505 : else
506 0 : bRet = false;
507 : }
508 0 : break;
509 : default:
510 : OSL_FAIL("-TokenPool::GetElement(): undefined state!?");
511 0 : bRet = false;
512 : }
513 : }
514 14618 : return bRet;
515 : }
516 :
517 9676 : bool TokenPool::GetElementRek( const sal_uInt16 nId )
518 : {
519 : #ifdef DBG_UTIL
520 : m_nRek++;
521 : OSL_ENSURE(m_nRek <= nP_Id, "*TokenPool::GetElement(): recursion loops!?");
522 : #endif
523 :
524 : OSL_ENSURE( nId < nElementAkt, "*TokenPool::GetElementRek(): nId >= nElementAkt" );
525 :
526 9676 : if (nId >= nElementAkt)
527 : {
528 : SAL_WARN("sc.filter", "*TokenPool::GetElementRek(): nId >= nElementAkt");
529 : #ifdef DBG_UTIL
530 : m_nRek--;
531 : #endif
532 0 : return false;
533 : }
534 :
535 9676 : if (pType[ nId ] != T_Id)
536 : {
537 : SAL_WARN("sc.filter", "-TokenPool::GetElementRek(): pType[ nId ] != T_Id");
538 : #ifdef DBG_UTIL
539 : m_nRek--;
540 : #endif
541 0 : return false;
542 : }
543 :
544 9676 : bool bRet = true;
545 9676 : sal_uInt16 nAnz = pSize[ nId ];
546 9676 : sal_uInt16 nFirstId = pElement[ nId ];
547 9676 : if (nFirstId >= nP_Id)
548 : {
549 : SAL_WARN("sc.filter", "TokenPool::GetElementRek: nFirstId >= nP_Id");
550 0 : nAnz = 0;
551 0 : bRet = false;
552 : }
553 9676 : sal_uInt16* pAkt = nAnz ? &pP_Id[ nFirstId ] : NULL;
554 9676 : if (nAnz > nP_Id - nFirstId)
555 : {
556 : SAL_WARN("sc.filter", "TokenPool::GetElementRek: nAnz > nP_Id - nFirstId");
557 0 : nAnz = nP_Id - nFirstId;
558 0 : bRet = false;
559 : }
560 48620 : for( ; nAnz > 0 ; nAnz--, pAkt++ )
561 : {
562 38944 : if( *pAkt < nScTokenOff )
563 : {// recursion or not?
564 18207 : if (*pAkt >= nElementAkt)
565 : {
566 : SAL_WARN("sc.filter", "TokenPool::GetElementRek: *pAkt >= nElementAkt");
567 0 : bRet = false;
568 : }
569 : else
570 : {
571 18207 : if (pType[ *pAkt ] == T_Id)
572 6764 : bRet = GetElementRek( *pAkt );
573 : else
574 11443 : bRet = GetElement( *pAkt );
575 : }
576 : }
577 : else // elementary SC_Token
578 20737 : pScToken->AddOpCode( ( DefTokenId ) ( *pAkt - nScTokenOff ) );
579 : }
580 :
581 : #ifdef DBG_UTIL
582 : m_nRek--;
583 : #endif
584 9676 : return bRet;
585 : }
586 :
587 10009 : void TokenPool::operator >>( TokenId& rId )
588 : {
589 10009 : rId = static_cast<TokenId>( nElementAkt + 1 );
590 :
591 10009 : if( nElementAkt >= nElement )
592 2 : if (!GrowElement())
593 10009 : return;
594 :
595 10009 : pElement[ nElementAkt ] = nP_IdLast; // Start of Token-sequence
596 10009 : pType[ nElementAkt ] = T_Id; // set Typeinfo
597 10009 : pSize[ nElementAkt ] = nP_IdAkt - nP_IdLast;
598 : // write from nP_IdLast to nP_IdAkt-1 -> length of the sequence
599 :
600 10009 : nElementAkt++; // start value for next sequence
601 10009 : nP_IdLast = nP_IdAkt;
602 : }
603 :
604 1720 : const TokenId TokenPool::Store( const double& rDouble )
605 : {
606 1720 : if( nElementAkt >= nElement )
607 0 : if (!GrowElement())
608 0 : return static_cast<const TokenId>(nElementAkt+1);
609 :
610 1720 : if( nP_DblAkt >= nP_Dbl )
611 0 : if (!GrowDouble())
612 0 : return static_cast<const TokenId>(nElementAkt+1);
613 :
614 1720 : pElement[ nElementAkt ] = nP_DblAkt; // Index in Double-Array
615 1720 : pType[ nElementAkt ] = T_D; // set Typeinfo Double
616 :
617 1720 : pP_Dbl[ nP_DblAkt ] = rDouble;
618 :
619 1720 : pSize[ nElementAkt ] = 1; // does not matter
620 :
621 1720 : nElementAkt++;
622 1720 : nP_DblAkt++;
623 :
624 1720 : return static_cast<const TokenId>(nElementAkt); // return old value + 1!
625 : }
626 :
627 0 : const TokenId TokenPool::Store( const sal_uInt16 nIndex )
628 : {
629 0 : return StoreName(nIndex, true);
630 : }
631 :
632 2738 : const TokenId TokenPool::Store( const OUString& rString )
633 : {
634 : // mostly copied to Store( const sal_Char* ), to avoid a temporary string
635 2738 : if( nElementAkt >= nElement )
636 0 : if (!GrowElement())
637 0 : return static_cast<const TokenId>(nElementAkt+1);
638 :
639 2738 : if( nP_StrAkt >= nP_Str )
640 1 : if (!GrowString())
641 0 : return static_cast<const TokenId>(nElementAkt+1);
642 :
643 2738 : pElement[ nElementAkt ] = nP_StrAkt; // Index in String-Array
644 2738 : pType[ nElementAkt ] = T_Str; // set Typeinfo String
645 :
646 : // create String
647 2738 : if( !ppP_Str[ nP_StrAkt ] )
648 : //...but only, if it does not exist already
649 22 : ppP_Str[ nP_StrAkt ] = new (::std::nothrow) OUString( rString );
650 : else
651 : //...copy otherwise
652 2716 : *ppP_Str[ nP_StrAkt ] = rString;
653 :
654 2738 : if (ppP_Str[ nP_StrAkt ])
655 : {
656 : /* attention trucate to 16 bits */
657 2738 : pSize[ nElementAkt ] = ( sal_uInt16 ) ppP_Str[ nP_StrAkt ]->getLength();
658 : }
659 :
660 2738 : nElementAkt++;
661 2738 : nP_StrAkt++;
662 :
663 2738 : return static_cast<const TokenId>(nElementAkt); // return old value + 1!
664 : }
665 :
666 5189 : const TokenId TokenPool::Store( const ScSingleRefData& rTr )
667 : {
668 5189 : if( nElementAkt >= nElement )
669 0 : if (!GrowElement())
670 0 : return static_cast<const TokenId>(nElementAkt+1);
671 :
672 5189 : if( nP_RefTrAkt >= nP_RefTr )
673 0 : if (!GrowTripel())
674 0 : return static_cast<const TokenId>(nElementAkt+1);
675 :
676 5189 : pElement[ nElementAkt ] = nP_RefTrAkt;
677 5189 : pType[ nElementAkt ] = T_RefC; // set Typeinfo Cell-Ref
678 :
679 5189 : if( !ppP_RefTr[ nP_RefTrAkt ] )
680 255 : ppP_RefTr[ nP_RefTrAkt ] = new ScSingleRefData( rTr );
681 : else
682 4934 : *ppP_RefTr[ nP_RefTrAkt ] = rTr;
683 :
684 5189 : nElementAkt++;
685 5189 : nP_RefTrAkt++;
686 :
687 5189 : return static_cast<const TokenId>(nElementAkt); // return old value + 1!
688 : }
689 :
690 2363 : const TokenId TokenPool::Store( const ScComplexRefData& rTr )
691 : {
692 2363 : if( nElementAkt >= nElement )
693 0 : if (!GrowElement())
694 0 : return static_cast<const TokenId>(nElementAkt+1);
695 :
696 2363 : if( nP_RefTrAkt + 1 >= nP_RefTr )
697 0 : if (!GrowTripel( 2))
698 0 : return static_cast<const TokenId>(nElementAkt+1);
699 :
700 2363 : pElement[ nElementAkt ] = nP_RefTrAkt;
701 2363 : pType[ nElementAkt ] = T_RefA; // setTypeinfo Area-Ref
702 :
703 2363 : if( !ppP_RefTr[ nP_RefTrAkt ] )
704 41 : ppP_RefTr[ nP_RefTrAkt ] = new ScSingleRefData( rTr.Ref1 );
705 : else
706 2322 : *ppP_RefTr[ nP_RefTrAkt ] = rTr.Ref1;
707 2363 : nP_RefTrAkt++;
708 :
709 2363 : if( !ppP_RefTr[ nP_RefTrAkt ] )
710 51 : ppP_RefTr[ nP_RefTrAkt ] = new ScSingleRefData( rTr.Ref2 );
711 : else
712 2312 : *ppP_RefTr[ nP_RefTrAkt ] = rTr.Ref2;
713 2363 : nP_RefTrAkt++;
714 :
715 2363 : nElementAkt++;
716 :
717 2363 : return static_cast<const TokenId>(nElementAkt); // return old value + 1!
718 : }
719 :
720 177 : const TokenId TokenPool::Store( const DefTokenId e, const OUString& r )
721 : {
722 177 : if( nElementAkt >= nElement )
723 0 : if (!GrowElement())
724 0 : return static_cast<const TokenId>(nElementAkt+1);
725 :
726 177 : if( nP_ExtAkt >= nP_Ext )
727 0 : if (!GrowExt())
728 0 : return static_cast<const TokenId>(nElementAkt+1);
729 :
730 177 : pElement[ nElementAkt ] = nP_ExtAkt;
731 177 : pType[ nElementAkt ] = T_Ext; // set Typeinfo String
732 :
733 177 : if( ppP_Ext[ nP_ExtAkt ] )
734 : {
735 175 : ppP_Ext[ nP_ExtAkt ]->eId = e;
736 175 : ppP_Ext[ nP_ExtAkt ]->aText = r;
737 : }
738 : else
739 2 : ppP_Ext[ nP_ExtAkt ] = new EXTCONT( e, r );
740 :
741 177 : nElementAkt++;
742 177 : nP_ExtAkt++;
743 :
744 177 : return static_cast<const TokenId>(nElementAkt); // return old value + 1!
745 : }
746 :
747 0 : const TokenId TokenPool::StoreNlf( const ScSingleRefData& rTr )
748 : {
749 0 : if( nElementAkt >= nElement )
750 0 : if (!GrowElement())
751 0 : return static_cast<const TokenId>(nElementAkt+1);
752 :
753 0 : if( nP_NlfAkt >= nP_Nlf )
754 0 : if (!GrowNlf())
755 0 : return static_cast<const TokenId>(nElementAkt+1);
756 :
757 0 : pElement[ nElementAkt ] = nP_NlfAkt;
758 0 : pType[ nElementAkt ] = T_Nlf;
759 :
760 0 : if( ppP_Nlf[ nP_NlfAkt ] )
761 : {
762 0 : ppP_Nlf[ nP_NlfAkt ]->aRef = rTr;
763 : }
764 : else
765 0 : ppP_Nlf[ nP_NlfAkt ] = new NLFCONT( rTr );
766 :
767 0 : nElementAkt++;
768 0 : nP_NlfAkt++;
769 :
770 0 : return static_cast<const TokenId>(nElementAkt);
771 : }
772 :
773 7 : const TokenId TokenPool::StoreMatrix()
774 : {
775 : ScMatrix* pM;
776 :
777 7 : if( nElementAkt >= nElement )
778 0 : if (!GrowElement())
779 0 : return static_cast<const TokenId>(nElementAkt+1);
780 :
781 7 : if( nP_MatrixAkt >= nP_Matrix )
782 0 : if (!GrowMatrix())
783 0 : return static_cast<const TokenId>(nElementAkt+1);
784 :
785 7 : pElement[ nElementAkt ] = nP_MatrixAkt;
786 7 : pType[ nElementAkt ] = T_Matrix;
787 :
788 7 : pM = new ScMatrix( 0, 0 );
789 7 : pM->IncRef( );
790 7 : ppP_Matrix[ nP_MatrixAkt ] = pM;
791 :
792 7 : nElementAkt++;
793 7 : nP_MatrixAkt++;
794 :
795 7 : return static_cast<const TokenId>(nElementAkt);
796 : }
797 :
798 20 : const TokenId TokenPool::StoreName( sal_uInt16 nIndex, bool bGlobal )
799 : {
800 20 : if ( nElementAkt >= nElement )
801 0 : if (!GrowElement())
802 0 : return static_cast<const TokenId>(nElementAkt+1);
803 :
804 20 : pElement[nElementAkt] = static_cast<sal_uInt16>(maRangeNames.size());
805 20 : pType[nElementAkt] = T_RN;
806 :
807 20 : maRangeNames.push_back(RangeName());
808 20 : RangeName& r = maRangeNames.back();
809 20 : r.mnIndex = nIndex;
810 20 : r.mbGlobal = bGlobal;
811 :
812 20 : ++nElementAkt;
813 :
814 20 : return static_cast<const TokenId>(nElementAkt);
815 : }
816 :
817 0 : const TokenId TokenPool::StoreExtName( sal_uInt16 nFileId, const OUString& rName )
818 : {
819 0 : if ( nElementAkt >= nElement )
820 0 : if (!GrowElement())
821 0 : return static_cast<const TokenId>(nElementAkt+1);
822 :
823 0 : pElement[nElementAkt] = static_cast<sal_uInt16>(maExtNames.size());
824 0 : pType[nElementAkt] = T_ExtName;
825 :
826 0 : maExtNames.push_back(ExtName());
827 0 : ExtName& r = maExtNames.back();
828 0 : r.mnFileId = nFileId;
829 0 : r.maName = rName;
830 :
831 0 : ++nElementAkt;
832 :
833 0 : return static_cast<const TokenId>(nElementAkt);
834 : }
835 :
836 2 : const TokenId TokenPool::StoreExtRef( sal_uInt16 nFileId, const OUString& rTabName, const ScSingleRefData& rRef )
837 : {
838 2 : if ( nElementAkt >= nElement )
839 0 : if (!GrowElement())
840 0 : return static_cast<const TokenId>(nElementAkt+1);
841 :
842 2 : pElement[nElementAkt] = static_cast<sal_uInt16>(maExtCellRefs.size());
843 2 : pType[nElementAkt] = T_ExtRefC;
844 :
845 2 : maExtCellRefs.push_back(ExtCellRef());
846 2 : ExtCellRef& r = maExtCellRefs.back();
847 2 : r.mnFileId = nFileId;
848 2 : r.maTabName = rTabName;
849 2 : r.maRef = rRef;
850 :
851 2 : ++nElementAkt;
852 :
853 2 : return static_cast<const TokenId>(nElementAkt);
854 : }
855 :
856 0 : const TokenId TokenPool::StoreExtRef( sal_uInt16 nFileId, const OUString& rTabName, const ScComplexRefData& rRef )
857 : {
858 0 : if ( nElementAkt >= nElement )
859 0 : if (!GrowElement())
860 0 : return static_cast<const TokenId>(nElementAkt+1);
861 :
862 0 : pElement[nElementAkt] = static_cast<sal_uInt16>(maExtAreaRefs.size());
863 0 : pType[nElementAkt] = T_ExtRefA;
864 :
865 0 : maExtAreaRefs.push_back(ExtAreaRef());
866 0 : ExtAreaRef& r = maExtAreaRefs.back();
867 0 : r.mnFileId = nFileId;
868 0 : r.maTabName = rTabName;
869 0 : r.maRef = rRef;
870 :
871 0 : ++nElementAkt;
872 :
873 0 : return static_cast<const TokenId>(nElementAkt);
874 : }
875 :
876 4669 : void TokenPool::Reset()
877 : {
878 4669 : nP_IdAkt = nP_IdLast = nElementAkt = nP_StrAkt = nP_DblAkt = nP_ErrAkt = nP_RefTrAkt = nP_ExtAkt = nP_NlfAkt = nP_MatrixAkt = 0;
879 4669 : maRangeNames.clear();
880 4669 : maExtNames.clear();
881 4669 : maExtCellRefs.clear();
882 4669 : maExtAreaRefs.clear();
883 4669 : }
884 :
885 10 : bool TokenPool::IsSingleOp( const TokenId& rId, const DefTokenId eId ) const
886 : {
887 10 : sal_uInt16 nId = (sal_uInt16) rId;
888 10 : if( nId && nId <= nElementAkt )
889 : {// existent?
890 10 : nId--;
891 10 : if( T_Id == pType[ nId ] )
892 : {// Token-Sequence?
893 5 : if( pSize[ nId ] == 1 )
894 : {// EXACTLY 1 Token
895 0 : sal_uInt16 nPid = pElement[ nId ];
896 0 : if (nPid < nP_Id)
897 : {
898 0 : sal_uInt16 nSecId = pP_Id[ nPid ];
899 0 : if( nSecId >= nScTokenOff )
900 : {// Default-Token?
901 0 : return ( DefTokenId ) ( nSecId - nScTokenOff ) == eId; // wanted?
902 : }
903 : }
904 : }
905 : }
906 : }
907 :
908 10 : return false;
909 : }
910 :
911 177 : const OUString* TokenPool::GetExternal( const TokenId& rId ) const
912 : {
913 177 : const OUString* p = NULL;
914 177 : sal_uInt16 n = (sal_uInt16) rId;
915 177 : if( n && n <= nElementAkt )
916 : {
917 177 : n--;
918 177 : if( pType[ n ] == T_Ext )
919 : {
920 177 : sal_uInt16 nExt = pElement[ n ];
921 177 : if ( nExt < nP_Ext && ppP_Ext[ nExt ] )
922 177 : p = &ppP_Ext[ nExt ]->aText;
923 : }
924 : }
925 :
926 177 : return p;
927 : }
928 :
929 7 : ScMatrix* TokenPool::GetMatrix( unsigned int n ) const
930 : {
931 7 : if( n < nP_MatrixAkt )
932 7 : return ppP_Matrix[ n ];
933 : else
934 : SAL_WARN("sc.filter", "GetMatrix: " << n << " >= " << nP_MatrixAkt << "\n");
935 0 : return NULL;
936 30 : }
937 :
938 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|