1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
23 #include <boost/ptr_container/ptr_vector.hpp>
25 #include <sal/types.h>
27 #include <tools/stream.hxx>
29 enum SVTOKEN_ENUM
{ SVTOKEN_EMPTY
, SVTOKEN_COMMENT
,
30 SVTOKEN_INTEGER
, SVTOKEN_STRING
,
31 SVTOKEN_BOOL
, SVTOKEN_IDENTIFIER
,
32 SVTOKEN_CHAR
, SVTOKEN_RTTIBASE
,
33 SVTOKEN_EOF
, SVTOKEN_HASHID
};
37 friend class SvTokenStream
;
38 sal_uLong nLine
, nColumn
;
46 SvStringHashEntry
* pHash
;
50 SvToken( const SvToken
& rObj
);
51 SvToken( sal_uLong n
);
52 SvToken( SVTOKEN_ENUM nTypeP
, sal_Bool b
);
54 SvToken( SVTOKEN_ENUM nTypeP
, const OString
& rStr
);
55 SvToken( SVTOKEN_ENUM nTypeP
);
57 SvToken
& operator = ( const SvToken
& rObj
);
59 OString
GetTokenAsString() const;
60 SVTOKEN_ENUM
GetType() const { return nType
; }
62 void SetLine( sal_uLong nLineP
) { nLine
= nLineP
; }
63 sal_uLong
GetLine() const { return nLine
; }
65 void SetColumn( sal_uLong nColumnP
) { nColumn
= nColumnP
; }
66 sal_uLong
GetColumn() const { return nColumn
; }
68 sal_Bool
IsEmpty() const { return nType
== SVTOKEN_EMPTY
; }
69 sal_Bool
IsComment() const { return nType
== SVTOKEN_COMMENT
; }
70 sal_Bool
IsInteger() const { return nType
== SVTOKEN_INTEGER
; }
71 sal_Bool
IsString() const { return nType
== SVTOKEN_STRING
; }
72 sal_Bool
IsBool() const { return nType
== SVTOKEN_BOOL
; }
73 sal_Bool
IsIdentifierHash() const
74 { return nType
== SVTOKEN_HASHID
; }
75 sal_Bool
IsIdentifier() const
77 return nType
== SVTOKEN_IDENTIFIER
78 || nType
== SVTOKEN_HASHID
;
80 sal_Bool
IsChar() const { return nType
== SVTOKEN_CHAR
; }
81 sal_Bool
IsRttiBase() const { return nType
== SVTOKEN_RTTIBASE
; }
82 sal_Bool
IsEof() const { return nType
== SVTOKEN_EOF
; }
84 const OString
& GetString() const
86 return IsIdentifierHash()
90 sal_uLong
GetNumber() const { return nLong
; }
91 sal_Bool
GetBool() const { return bBool
; }
92 char GetChar() const { return cChar
; }
94 void SetHash( SvStringHashEntry
* pHashP
)
95 { pHash
= pHashP
; nType
= SVTOKEN_HASHID
; }
96 sal_Bool
HasHash() const
97 { return nType
== SVTOKEN_HASHID
; }
98 SvStringHashEntry
* GetHash() const { return pHash
; }
99 sal_Bool
Is( SvStringHashEntry
* pEntry
) const
100 { return IsIdentifierHash() && pHash
== pEntry
; }
103 inline SvToken::SvToken()
104 : nType( SVTOKEN_EMPTY
) {}
106 inline SvToken::SvToken( sal_uLong n
)
107 : nType( SVTOKEN_INTEGER
), nLong( n
) {}
109 inline SvToken::SvToken( SVTOKEN_ENUM nTypeP
, sal_Bool b
)
110 : nType( nTypeP
), bBool( b
) {}
112 inline SvToken::SvToken( char c
)
113 : nType( SVTOKEN_CHAR
), cChar( c
) {}
115 inline SvToken::SvToken( SVTOKEN_ENUM nTypeP
, const OString
& rStr
)
116 : nType( nTypeP
), aString( rStr
) {}
118 inline SvToken::SvToken( SVTOKEN_ENUM nTypeP
)
123 sal_uLong nLine
, nColumn
;
125 int c
; // next character
126 sal_uInt16 nTabSize
; // length of tabulator
131 SvFileStream
* pInStream
;
132 SvStream
& rInStream
;
134 boost::ptr_vector
<SvToken
> aTokList
;
135 boost::ptr_vector
<SvToken
>::iterator pCurToken
;
141 int GetFastNextChar()
143 return aBufStr
[nBufPos
++];
146 void FillTokenList();
147 sal_uLong
GetNumber();
148 sal_Bool
MakeToken( SvToken
& );
149 sal_Bool
IsEof() const { return rInStream
.IsEof(); }
152 sal_uLong n
= Tell();
158 // if end of line spare calculation
164 nColumn
+= aBufStr
[n
++] == '\t' ? nTabSize
: 1;
168 SvTokenStream( const String
& rFileName
);
169 SvTokenStream( SvStream
& rInStream
, const String
& rFileName
);
172 const String
& GetFileName() const { return aFileName
; }
173 SvStream
& GetStream() { return rInStream
; }
175 void SetTabSize( sal_uInt16 nTabSizeP
)
176 { nTabSize
= nTabSizeP
; }
177 sal_uInt16
GetTabSize() const { return nTabSize
; }
179 SvToken
* GetToken_PrevAll()
181 boost::ptr_vector
<SvToken
>::iterator pRetToken
= pCurToken
;
183 // current iterator always valid
184 if(pCurToken
!= aTokList
.begin())
187 return &(*pRetToken
);
190 SvToken
* GetToken_NextAll()
192 boost::ptr_vector
<SvToken
>::iterator pRetToken
= pCurToken
++;
194 if (pCurToken
== aTokList
.end())
195 pCurToken
= pRetToken
;
199 return &(*pRetToken
);
202 SvToken
* GetToken_Next()
204 // comments get removed initially
205 return GetToken_NextAll();
208 SvToken
* GetToken() const { return &(*pCurToken
); }
210 sal_Bool
Read( char cChar
)
212 if( pCurToken
->IsChar()
213 && cChar
== pCurToken
->GetChar() )
224 if( pCurToken
->IsChar()
225 && (';' == pCurToken
->GetChar()
226 || ',' == pCurToken
->GetChar()) )
232 sal_uInt32
Tell() const { return pCurToken
-aTokList
.begin(); }
234 void Seek( sal_uInt32 nPos
)
236 pCurToken
= aTokList
.begin() + nPos
;
240 void SeekRel( sal_uInt32 nRelPos
)
242 sal_uInt32 relIdx
= Tell() + nRelPos
;
244 if ( relIdx
< aTokList
.size())
246 pCurToken
= aTokList
.begin()+ (Tell() + nRelPos
);
253 pCurToken
= aTokList
.begin()+nMaxPos
;
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */