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 .
25 #include <globals.hxx>
26 #include <rtl/strbuf.hxx>
28 OString
SvToken::GetTokenAsString() const
39 aStr
= OString::number(nLong
);
45 aStr
= bBool
? "TRUE" : "FALSE";
47 case SVTOKEN_IDENTIFIER
:
51 aStr
= OString(cChar
);
53 case SVTOKEN_RTTIBASE
:
64 SvToken::SvToken( const SvToken
& rObj
)
67 nColumn
= rObj
.nColumn
;
69 aString
= rObj
.aString
;
73 SvToken
& SvToken::operator = ( const SvToken
& rObj
)
78 nColumn
= rObj
.nColumn
;
80 aString
= rObj
.aString
;
86 void SvTokenStream::InitCtor()
88 aStrTrue
= OString("TRUE");
89 aStrFalse
= OString("FALSE");
98 SvTokenStream::SvTokenStream( const OUString
& rFileName
)
99 : pInStream( new SvFileStream( rFileName
, STREAM_STD_READ
| StreamMode::NOCREATE
) )
100 , rInStream( *pInStream
)
101 , aFileName( rFileName
)
106 SvTokenStream::SvTokenStream( SvStream
& rStream
, const OUString
& rFileName
)
108 , rInStream( rStream
)
109 , aFileName( rFileName
)
114 SvTokenStream::~SvTokenStream()
119 void SvTokenStream::FillTokenList()
121 SvToken
* pToken
= new SvToken();
122 aTokList
.push_back(pToken
);
125 if( !MakeToken( *pToken
) )
127 if (!aTokList
.empty())
130 boost::ptr_vector
<SvToken
>::const_iterator it
= aTokList
.begin();
132 pToken
->SetLine(it
->GetLine());
133 pToken
->SetColumn(it
->GetColumn());
137 else if( pToken
->IsComment() )
139 else if( pToken
->IsEof() )
143 pToken
= new SvToken();
144 aTokList
.push_back(pToken
);
147 while( !pToken
->IsEof() );
148 pCurToken
= aTokList
.begin();
151 int SvTokenStream::GetNextChar()
154 while (aBufStr
.getLength() <= nBufPos
)
156 if (rInStream
.ReadLine(aBufStr
))
170 nChar
= aBufStr
[nBufPos
++];
171 nColumn
+= nChar
== '\t' ? nTabSize
: 1;
175 sal_uLong
SvTokenStream::GetNumber()
182 c
= GetFastNextChar();
186 c
= GetFastNextChar();
192 while( isxdigit( c
) )
195 l
= l
* nLog
+ (c
- '0');
197 l
= l
* nLog
+ (toupper( c
) - 'A' + 10 );
198 c
= GetFastNextChar();
203 while( isdigit( c
) || 'x' == c
)
205 l
= l
* nLog
+ (c
- '0');
206 c
= GetFastNextChar();
213 bool SvTokenStream::MakeToken( SvToken
& rToken
)
220 while( isspace( c
) || 26 == c
)
222 c
= GetFastNextChar();
223 nColumn
+= c
== '\t' ? nTabSize
: 1;
226 while( 0 == c
&& !IsEof() && ( SVSTREAM_OK
== rInStream
.GetError() ) );
228 sal_uLong nLastLine
= nLine
;
229 sal_uLong nLastColumn
= nColumn
;
233 // time optimization, no comments
235 c
= GetFastNextChar();
240 c
= GetFastNextChar();
243 rToken
.nType
= SVTOKEN_COMMENT
;
247 c
= GetFastNextChar();
259 c
= GetFastNextChar();
261 c
= GetFastNextChar();
263 while( '/' != c
&& !IsEof() && ( SVSTREAM_OK
== rInStream
.GetError() ) );
264 if( IsEof() || ( SVSTREAM_OK
!= rInStream
.GetError() ) )
267 rToken
.nType
= SVTOKEN_COMMENT
;
272 rToken
.nType
= SVTOKEN_CHAR
;
273 rToken
.cChar
= (char)c1
;
280 while( !bDone
&& !IsEof() && c
)
282 c
= GetFastNextChar();
285 // read strings beyond end of line
293 c
= GetFastNextChar();
305 c
= GetFastNextChar();
307 aStr
.append(static_cast<char>(c
));
310 aStr
.append(static_cast<char>(c
));
312 if( IsEof() || ( SVSTREAM_OK
!= rInStream
.GetError() ) )
314 rToken
.nType
= SVTOKEN_STRING
;
315 rToken
.aString
= aStr
.makeStringAndClear();
317 else if( isdigit( c
) )
319 rToken
.nType
= SVTOKEN_INTEGER
;
320 rToken
.nLong
= GetNumber();
323 else if( isalpha (c
) || (c
== '_') )
326 while( isalnum( c
) || c
== '_' )
328 aBuf
.append(static_cast<char>(c
));
329 c
= GetFastNextChar();
331 OString aStr
= aBuf
.makeStringAndClear();
332 if( aStr
.equalsIgnoreAsciiCase( aStrTrue
) )
334 rToken
.nType
= SVTOKEN_BOOL
;
337 else if( aStr
.equalsIgnoreAsciiCase( aStrFalse
) )
339 rToken
.nType
= SVTOKEN_BOOL
;
340 rToken
.bBool
= false;
345 if( IDLAPP
->pHashTable
->Test( aStr
, &nHashId
) )
346 rToken
.SetHash( IDLAPP
->pHashTable
->Get( nHashId
) );
349 rToken
.nType
= SVTOKEN_IDENTIFIER
;
350 rToken
.aString
= aStr
;
356 rToken
.nType
= SVTOKEN_EOF
;
360 rToken
.nType
= SVTOKEN_CHAR
;
361 rToken
.cChar
= (char)c
;
362 c
= GetFastNextChar();
364 rToken
.SetLine( nLastLine
);
365 rToken
.SetColumn( nLastColumn
);
366 return rInStream
.GetError() == SVSTREAM_OK
;
369 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */