summaryrefslogtreecommitdiff
path: root/sc/source/filter/inc/tokstack.hxx
blob: d0972786c3d259f24c4a5fdca3d2b035e7685265 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#ifndef SC_TOKSTACK_HXX
#define SC_TOKSTACK_HXX

#include <string.h>
#include <tools/debug.hxx>
#include "compiler.hxx"
#include "tokenarray.hxx"

#include <vector>

typedef OpCode DefTokenId;
// in PRODUCT version: ambiguity between OpCode (being USHORT) and UINT16
// Unfortunately a typedef is just a dumb alias and not a real type ...
//typedef UINT16 TokenId;
struct TokenId
{
        UINT16			nId;

                        TokenId() : nId( 0 ) {}
                        TokenId( UINT16 n ) : nId( n ) {}
                        TokenId( const TokenId& r ) : nId( r.nId ) {}
    inline	TokenId&	operator =( const TokenId& r ) { nId = r.nId; return *this; }
    inline	TokenId&	operator =( UINT16 n ) { nId = n; return *this; }
    inline				operator UINT16&() { return nId; }
    inline				operator const UINT16&() const { return nId; }
    inline	BOOL		operator <( UINT16 n ) const { return nId < n; }
    inline	BOOL		operator >( UINT16 n ) const { return nId > n; }
    inline	BOOL		operator <=( UINT16 n ) const { return nId <= n; }
    inline	BOOL		operator >=( UINT16 n ) const { return nId >= n; }
    inline	BOOL		operator ==( UINT16 n ) const { return nId == n; }
    inline	BOOL		operator !=( UINT16 n ) const { return nId != n; }
};


//------------------------------------------------------------------------
struct ScComplexRefData;
class TokenStack;
class ScToken;


enum E_TYPE
{
    T_Id,		// Id-Folge
    T_Str,		// String
    T_D,		// Double
    T_Err,      // Error code
    T_RefC,		// Cell Reference
    T_RefA,		// Area Reference
    T_RN,		// Range Name
    T_Ext,		// irgendwas Unbekanntes mit Funktionsnamen
    T_Nlf,		// token for natural language formula
    T_Matrix,	// token for inline arrays
    T_ExtName,  // token for external names
    T_ExtRefC,
    T_ExtRefA,
    T_Error		// fuer Abfrage im Fehlerfall
};




class TokenPool
{
    // !ACHTUNG!: externe Id-Basis ist 1, interne 0!
    // Ausgabe Id = 0 -> Fehlerfall
    private:
        String**					ppP_Str;	// Pool fuer Strings
        UINT16						nP_Str;		// ...mit Groesse
        UINT16						nP_StrAkt;	// ...und Schreibmarke

        double*						pP_Dbl;		// Pool fuer Doubles
        UINT16						nP_Dbl;
        UINT16						nP_DblAkt;

        USHORT*                     pP_Err;     // Pool for error codes
        UINT16                      nP_Err;
        UINT16                      nP_ErrAkt;

        ScSingleRefData**				ppP_RefTr;	// Pool fuer Referenzen
        UINT16						nP_RefTr;
        UINT16						nP_RefTrAkt;

        UINT16*						pP_Id;		// Pool fuer Id-Folgen
        UINT16						nP_Id;
        UINT16						nP_IdAkt;
        UINT16						nP_IdLast;	// letzter Folgen-Beginn

        struct	EXTCONT
        {
            DefTokenId				eId;
            String					aText;
                                    EXTCONT( const DefTokenId e, const String& r ) :
                                        eId( e ), aText( r ){}
        };
        EXTCONT**					ppP_Ext;
        UINT16						nP_Ext;
        UINT16						nP_ExtAkt;

        struct	NLFCONT
        {
            ScSingleRefData			aRef;
                                    NLFCONT( const ScSingleRefData& r ) : aRef( r )	{}
        };
        NLFCONT**					ppP_Nlf;
        UINT16						nP_Nlf;
        UINT16						nP_NlfAkt;

        ScMatrix**					ppP_Matrix;		// Pool fuer Matricies
        UINT16						nP_Matrix;
        UINT16						nP_MatrixAkt;

        /** for storage of external names */
        struct ExtName
        {
            sal_uInt16  mnFileId;
            String      maName;
        };
        ::std::vector<ExtName>      maExtNames;

        /** for storage of external cell references */
        struct ExtCellRef
        {
            sal_uInt16      mnFileId;
            String			maTabName;
            ScSingleRefData   maRef;
        };
        ::std::vector<ExtCellRef>   maExtCellRefs;

        /** for storage of external area references */
        struct ExtAreaRef
        {
            sal_uInt16      mnFileId;
            String			maTabName;
            ScComplexRefData    maRef;
        };
        ::std::vector<ExtAreaRef>   maExtAreaRefs;

        UINT16*						pElement;	// Array mit Indizes fuer Elemente
        E_TYPE*						pType;		// ...mit Typ-Info
        UINT16*						pSize;		// ...mit Laengenangabe (Anz. UINT16)
        UINT16						nElement;
        UINT16						nElementAkt;

        static const UINT16			nScTokenOff;// Offset fuer SC-Token
#ifdef DBG_UTIL
        UINT16						nRek;		// Rekursionszaehler
#endif
        ScTokenArray*				pScToken;	// Tokenbastler

        void						GrowString( void );
        void						GrowDouble( void );
//UNUSED2009-05 void                        GrowError( void );
        void						GrowTripel( void );
        void						GrowId( void );
        void						GrowElement( void );
        void						GrowExt( void );
        void						GrowNlf( void );
        void						GrowMatrix( void );
        void						GetElement( const UINT16 nId );
        void						GetElementRek( const UINT16 nId );
    public:
                                    TokenPool( void );
                                    ~TokenPool();
        inline TokenPool&			operator <<( const TokenId nId );
        inline TokenPool&			operator <<( const DefTokenId eId );
        inline TokenPool&			operator <<( TokenStack& rStack );
        void						operator >>( TokenId& rId );
        inline void					operator >>( TokenStack& rStack );
        inline const TokenId		Store( void );
        const TokenId				Store( const double& rDouble );
//UNUSED2008-05  const TokenId               StoreError( USHORT nError );

                                    // nur fuer Range-Names
        const TokenId				Store( const UINT16 nIndex );
        inline const TokenId		Store( const INT16 nWert );
        const TokenId				Store( const String& rString );
        const TokenId				Store( const ScSingleRefData& rTr );
        const TokenId				Store( const ScComplexRefData& rTr );

        const TokenId				Store( const DefTokenId eId, const String& rName );
                                        // 4 externals (e.g. AddIns, Makros...)
        const TokenId				StoreNlf( const ScSingleRefData& rTr );
        const TokenId               StoreMatrix();
        const TokenId               StoreExtName( sal_uInt16 nFileId, const String& rName );
        const TokenId               StoreExtRef( sal_uInt16 nFileId, const String& rTabName, const ScSingleRefData& rRef );
        const TokenId               StoreExtRef( sal_uInt16 nFileId, const String& rTabName, const ScComplexRefData& rRef );

        inline const TokenId		LastId( void ) const;
        inline const ScTokenArray*	operator []( const TokenId nId );
        void						Reset( void );
        inline E_TYPE				GetType( const TokenId& nId ) const;
        BOOL						IsSingleOp( const TokenId& nId, const DefTokenId eId ) const;
        const String*               GetExternal( const TokenId& nId ) const;
//UNUSED2008-05  const String*               GetString( const TokenId& nId ) const;
        ScMatrix*					GetMatrix( unsigned int n ) const;
};




class TokenStack
    // Stack fuer Token-Ids: Id 0 sollte reserviert bleiben als
    //	fehlerhafte Id,	da z.B. Get() im Fehlerfall 0 liefert
{
    private:
        TokenId*					pStack;		// Stack als Array
        UINT16						nPos;		// Schreibmarke
        UINT16						nSize;		// Erster Index ausserhalb des Stacks
    public:
                                    TokenStack( UINT16 nNewSize = 1024 );
                                    ~TokenStack();
        inline TokenStack&			operator <<( const TokenId nNewId );
        inline void					operator >>( TokenId &rId );

        inline void					Reset( void );

        inline bool                 HasMoreTokens() const { return nPos > 0; }
        inline const TokenId		Get( void );
};




inline const TokenId TokenStack::Get( void )
{
    DBG_ASSERT( nPos > 0,
        "*TokenStack::Get(): Leer ist leer, ist leer, ist leer, ist..." );

    TokenId nRet;

    if( nPos == 0 )
        nRet = 0;
    else
    {
        nPos--;
        nRet = pStack[ nPos ];
    }

    return nRet;
}


inline TokenStack &TokenStack::operator <<( const TokenId nNewId )
{// Element auf Stack
    DBG_ASSERT( nPos < nSize, "*TokenStack::<<(): Stackueberlauf" );
    if( nPos < nSize )
    {
        pStack[ nPos ] = nNewId;
        nPos++;
    }

    return *this;
}


inline void TokenStack::operator >>( TokenId& rId )
{// Element von Stack
    DBG_ASSERT( nPos > 0,
        "*TokenStack::>>(): Leer ist leer, ist leer, ist leer, ..." );
    if( nPos > 0 )
    {
        nPos--;
        rId = pStack[ nPos ];
    }
}


inline void TokenStack::Reset( void )
{
    nPos = 0;
}




inline TokenPool& TokenPool::operator <<( const TokenId nId )
{
    // POST: nId's werden hintereinander im Pool unter einer neuen Id
    //		 abgelegt. Vorgang wird mit >> oder Store() abgeschlossen
    // nId -> ( UINT16 ) nId - 1;
    DBG_ASSERT( ( UINT16 ) nId < nScTokenOff,
        "-TokenPool::operator <<: TokenId im DefToken-Bereich!" );

    if( nP_IdAkt >= nP_Id )
        GrowId();

    pP_Id[ nP_IdAkt ] = ( ( UINT16 ) nId ) - 1;
    nP_IdAkt++;

    return *this;
}


inline TokenPool& TokenPool::operator <<( const DefTokenId eId )
{
    DBG_ASSERT( ( UINT32 ) eId + nScTokenOff < 0xFFFF,
        "-TokenPool::operator<<: enmum zu gross!" );

    if( nP_IdAkt >= nP_Id )
        GrowId();

    pP_Id[ nP_IdAkt ] = ( ( UINT16 ) eId ) + nScTokenOff;
    nP_IdAkt++;

    return *this;
}


inline TokenPool& TokenPool::operator <<( TokenStack& rStack )
{
    if( nP_IdAkt >= nP_Id )
        GrowId();

    pP_Id[ nP_IdAkt ] = ( ( UINT16 ) rStack.Get() ) - 1;
    nP_IdAkt++;

    return *this;
}


inline void TokenPool::operator >>( TokenStack& rStack )
{
    TokenId nId;
    *this >> nId;
    rStack << nId;
}


inline const TokenId TokenPool::Store( void )
{
    TokenId nId;
    *this >> nId;
    return nId;
}


inline const TokenId TokenPool::Store( const INT16 nWert )
{
    return Store( ( double ) nWert );
}


inline const TokenId TokenPool::LastId( void ) const
{
    return ( TokenId ) nElementAkt; // stimmt, da Ausgabe mit Offset 1!
}


const inline ScTokenArray* TokenPool::operator []( const TokenId nId )
{
    pScToken->Clear();

    if( nId )
    {//...nur wenn nId > 0!
#ifdef DBG_UTIL
        nRek = 0;
#endif
        GetElement( ( UINT16 ) nId - 1 );
    }

    return pScToken;
}


inline E_TYPE TokenPool::GetType( const TokenId& rId ) const
{
    E_TYPE nRet;

    UINT16 nId = (UINT16) rId - 1;

    if( nId < nElementAkt )
        nRet = pType[ nId ] ;
    else
        nRet = T_Error;

    return nRet;
}


#endif