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 <globals.hxx>
24 #include <rtl/strbuf.hxx>
25 #include<rtl/character.hxx>
27 OString
SvToken::GetTokenAsString() const
32 case SVTOKENTYPE::Empty
:
34 case SVTOKENTYPE::Comment
:
37 case SVTOKENTYPE::Integer
:
38 aStr
= OString::number(nLong
);
40 case SVTOKENTYPE::String
:
43 case SVTOKENTYPE::Bool
:
44 aStr
= bBool
? "TRUE" : "FALSE";
46 case SVTOKENTYPE::Identifier
:
49 case SVTOKENTYPE::Char
:
50 aStr
= OString(cChar
);
52 case SVTOKENTYPE::EndOfFile
:
53 case SVTOKENTYPE::HashId
:
60 SvToken
& SvToken::operator = ( const SvToken
& rObj
)
65 nColumn
= rObj
.nColumn
;
67 aString
= rObj
.aString
;
73 void SvTokenStream::InitCtor()
75 aStrTrue
= "TRUE"_ostr
;
76 aStrFalse
= "FALSE"_ostr
;
84 SvTokenStream::SvTokenStream( const OUString
& rFileName
)
85 : pInStream( new SvFileStream( rFileName
, StreamMode::STD_READ
) )
86 , aFileName( rFileName
)
91 SvTokenStream::~SvTokenStream()
95 void SvTokenStream::FillTokenList()
97 SvToken
* pToken
= new SvToken();
98 aTokList
.push_back(std::unique_ptr
<SvToken
>(pToken
));
101 if( !MakeToken( *pToken
) )
103 if (!aTokList
.empty())
106 std::vector
<std::unique_ptr
<SvToken
> >::const_iterator it
= aTokList
.begin();
108 pToken
->SetLine((*it
)->GetLine());
109 pToken
->SetColumn((*it
)->GetColumn());
113 else if( pToken
->IsComment() )
115 else if( pToken
->IsEof() )
119 pToken
= new SvToken();
120 aTokList
.push_back(std::unique_ptr
<SvToken
>(pToken
));
123 while( !pToken
->IsEof() );
124 pCurToken
= aTokList
.begin();
127 char SvTokenStream::GetNextChar()
130 while (aBufStr
.getLength() <= nBufPos
)
132 if (pInStream
->ReadLine(aBufStr
))
146 nChar
= aBufStr
[nBufPos
++];
147 nColumn
+= nChar
== '\t' ? nTabSize
: 1;
151 sal_uInt64
SvTokenStream::GetNumber()
158 c
= GetFastNextChar();
162 c
= GetFastNextChar();
168 while( rtl::isAsciiHexDigit( static_cast<unsigned char>(c
) ) )
170 if( rtl::isAsciiDigit( static_cast<unsigned char>(c
) ) )
171 l
= l
* nLog
+ (c
- '0');
174 + (rtl::toAsciiUpperCase( static_cast<unsigned char>(c
) )
176 c
= GetFastNextChar();
181 while( rtl::isAsciiDigit( static_cast<unsigned char>(c
) ) || 'x' == c
)
183 l
= l
* nLog
+ (c
- '0');
184 c
= GetFastNextChar();
191 bool SvTokenStream::MakeToken( SvToken
& rToken
)
198 while( rtl::isAsciiWhiteSpace( static_cast<unsigned char>(c
) )
201 c
= GetFastNextChar();
202 nColumn
+= c
== '\t' ? nTabSize
: 1;
205 while( 0 == c
&& !IsEof() && ( ERRCODE_NONE
== pInStream
->GetError() ) );
207 sal_uInt64 nLastLine
= nLine
;
208 sal_uInt64 nLastColumn
= nColumn
;
212 // time optimization, no comments
214 c
= GetFastNextChar();
219 c
= GetFastNextChar();
222 rToken
.nType
= SVTOKENTYPE::Comment
;
226 c
= GetFastNextChar();
238 c
= GetFastNextChar();
240 c
= GetFastNextChar();
242 while( '/' != c
&& !IsEof() && ( ERRCODE_NONE
== pInStream
->GetError() ) );
243 if( IsEof() || ( ERRCODE_NONE
!= pInStream
->GetError() ) )
246 rToken
.nType
= SVTOKENTYPE::Comment
;
251 rToken
.nType
= SVTOKENTYPE::Char
;
257 OStringBuffer
aStr(128);
259 while( !bDone
&& !IsEof() && c
)
261 c
= GetFastNextChar();
264 // read strings beyond end of line
272 c
= GetFastNextChar();
278 if( IsEof() || ( ERRCODE_NONE
!= pInStream
->GetError() ) )
280 rToken
.nType
= SVTOKENTYPE::String
;
281 rToken
.aString
= aStr
.makeStringAndClear();
283 else if( rtl::isAsciiDigit( static_cast<unsigned char>(c
) ) )
285 rToken
.nType
= SVTOKENTYPE::Integer
;
286 rToken
.nLong
= GetNumber();
289 else if( rtl::isAsciiAlpha (static_cast<unsigned char>(c
)) || (c
== '_') )
291 OStringBuffer
aBuf(64);
292 while( rtl::isAsciiAlphanumeric( static_cast<unsigned char>(c
) )
293 || c
== '_' || c
== ':')
296 c
= GetFastNextChar();
298 OString aStr
= aBuf
.makeStringAndClear();
299 if( aStr
.equalsIgnoreAsciiCase( aStrTrue
) )
301 rToken
.nType
= SVTOKENTYPE::Bool
;
304 else if( aStr
.equalsIgnoreAsciiCase( aStrFalse
) )
306 rToken
.nType
= SVTOKENTYPE::Bool
;
307 rToken
.bBool
= false;
312 if( GetIdlApp().pHashTable
->Test( aStr
, &nHashId
) )
313 rToken
.SetHash( GetIdlApp().pHashTable
->Get( nHashId
) );
316 rToken
.nType
= SVTOKENTYPE::Identifier
;
317 rToken
.aString
= aStr
;
323 rToken
.nType
= SVTOKENTYPE::EndOfFile
;
327 rToken
.nType
= SVTOKENTYPE::Char
;
329 c
= GetFastNextChar();
331 rToken
.SetLine( nLastLine
);
332 rToken
.SetColumn( nLastColumn
);
333 return pInStream
->GetError() == ERRCODE_NONE
;
336 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */