1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: gi_parse.cxx,v $
10 * $Revision: 1.6.16.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_soltools.hxx"
34 #include <gi_parse.hxx>
39 #include <gilacces.hxx>
45 const char * C_sLineEnd
= "\r\n";
50 WriteStr( ostream
& o_rOut
, const Simstr
& i_rStr
)
52 o_rOut
.write( i_rStr
.str(), i_rStr
.l() );
56 WriteStr( ostream
& o_rOut
, const char * i_rStr
)
58 o_rOut
.write( i_rStr
, strlen(i_rStr
) );
62 GenericInfo_Parser::SetError( E_Error i_eError
)
64 eErrorCode
= i_eError
;
65 nErrorLine
= nCurLine
;
69 GenericInfo_Parser::GenericInfo_Parser()
70 : sCurParsePosition(""),
82 GenericInfo_Parser::~GenericInfo_Parser()
87 GenericInfo_Parser::LoadList( GenericInfoList_Builder
& o_rResult
,
88 const Simstr
& i_sSourceFileName
)
90 ifstream
aFile( i_sSourceFileName
.str() );
93 SetError(cannot_open
);
97 aFile
.seekg(0, ios::end
);
98 UINT32 nTextSize
= aFile
.tellg();
99 if ( nTextSize
== 0 || nTextSize
== UINT32(-1) )
101 dpBuffer
= new char[nTextSize
+2];
104 aFile
.read( dpBuffer
, nTextSize
);
108 char * sLastChar
= dpBuffer
+ nTextSize
- 1;
110 while ( sFilePtr
!= sLastChar
&& *sFilePtr
<= 32 )
112 if ( sFilePtr
== sLastChar
)
114 if ( *sFilePtr
<= 32 )
117 else while ( *sLastChar
<= 32 )
122 *(sLastChar
+1) = '\n';
123 *(sLastChar
+2) = '\0';
125 ResetState(o_rResult
);
127 for ( ReadLine(); bGoon
; ReadLine() )
129 bool bOk
= InterpretLine();
132 SetError(syntax_error
);
137 if ( nLevel
> 0 && eErrorCode
== ok
)
139 SetError(unexpected_eof
);
141 else if ( nLevel
< 0 )
143 SetError(unexpected_list_end
);
150 return eErrorCode
== ok
;
154 GenericInfo_Parser::SaveList( const Simstr
& i_rOutputFile
,
155 GenericInfoList_Browser
& io_rListBrowser
)
157 ofstream
aFile( i_rOutputFile
.str() );
160 SetError(cannot_open
);
164 ResetState(io_rListBrowser
);
169 return eErrorCode
== ok
;
173 GenericInfo_Parser::ResetState( GenericInfoList_Builder
& io_rResult
)
175 sCurParsePosition
= "";
182 pResult
= &io_rResult
;
187 GenericInfo_Parser::ResetState( GenericInfoList_Browser
& io_rSrc
)
189 sCurParsePosition
= "";
197 pResource
= &io_rSrc
;
202 GenericInfo_Parser::ReadLine()
204 if ( *sFilePtr
== '\0' ) // See initialising of dpBuffer and sLastChar in LoadList().
210 sCurParsePosition
= sFilePtr
;
211 while ( *sFilePtr
!= '\n' )
215 // Remove leading and trailing whitespace from line:
216 while ( sCurParsePosition
!= sFilePtr
&& *sCurParsePosition
<= 32 )
219 char * sEndOfLine
= sFilePtr
;
220 while ( sEndOfLine
!= sCurParsePosition
&& *sEndOfLine
<= 32 )
222 if ( sCurParsePosition
!= sEndOfLine
|| *sCurParsePosition
> 32 )
226 ++sFilePtr
; // Go beyond line end to first character of next line.
230 GenericInfo_Parser::InterpretLine()
232 switch ( ClassifyLine() )
234 case lt_key
: ReadKey();
236 case lt_open_list
: PushLevel_Read();
238 case lt_close_list
: PopLevel_Read();
240 case lt_comment
: AddCurLine2CurComment();
242 case lt_empty
: AddCurLine2CurComment();
250 GenericInfo_Parser::E_LineType
251 GenericInfo_Parser::ClassifyLine()
253 switch ( *sCurParsePosition
)
255 case '{': return lt_open_list
;
256 case '}': return lt_close_list
;
257 case '#': return lt_comment
;
258 case '\0': return lt_empty
;
265 GenericInfo_Parser::ReadKey()
267 const char * pSearch
= sCurParsePosition
;
269 for ( ; *pSearch
> 32; ++pSearch
) ;
270 UINT32 nKeyLength
= pSearch
- sCurParsePosition
;
272 for ( ; *pSearch
<= 32 && *pSearch
> '\0'; ++pSearch
) ;
274 pResult
->AddKey( sCurParsePosition
, nKeyLength
,
275 pSearch
, strlen(pSearch
),
276 sCurComment
.str(), sCurComment
.l()
282 GenericInfo_Parser::PushLevel_Read()
289 GenericInfo_Parser::PopLevel_Read()
292 pResult
->CloseList();
296 GenericInfo_Parser::AddCurLine2CurComment()
298 sCurComment
+= sCurParsePosition
;
299 sCurComment
+= C_sLineEnd
;
303 GenericInfo_Parser::WriteList( ostream
& o_rFile
)
305 static char sBuffer
[32000];
307 for ( bGoon
= pResource
->Start_CurList();
309 bGoon
= pResource
->NextOf_CurList() )
311 pResource
->Get_CurComment(&sBuffer
[0]);
312 WriteComment(o_rFile
,sBuffer
);
314 pResource
->Get_CurKey(&sBuffer
[0]);
315 WriteKey(o_rFile
,sBuffer
);
317 pResource
->Get_CurValue(&sBuffer
[0]);
318 WriteValue(o_rFile
,sBuffer
);
320 if ( pResource
->HasSubList_CurKey() )
326 o_rFile.write("{",1);
327 o_rFile.write(C_sLineEnd, C_nLineEndLength);
333 o_rFile.write("}",1);
334 o_rFile.write(C_sLineEnd, C_nLineEndLength);
342 GenericInfo_Parser::PushLevel_Write()
345 pResource
->Push_CurList();
349 GenericInfo_Parser::PopLevel_Write()
352 pResource
->Pop_CurList();
356 GenericInfo_Parser::WriteComment( ostream
& o_rFile
,
357 const char * i_sStr
)
359 WriteStr( o_rFile
, i_sStr
);
360 if ( i_sStr
[ strlen(i_sStr
)-1 ] != '\n' )
361 WriteStr( o_rFile
, C_sLineEnd
);
365 GenericInfo_Parser::WriteKey( ostream
& o_rFile
,
366 const char * i_sStr
)
368 WriteIndentation(o_rFile
);
369 WriteStr( o_rFile
, i_sStr
);
373 GenericInfo_Parser::WriteValue( ostream
& o_rFile
,
374 const char * i_sStr
)
376 if ( i_sStr
!= 0 ? strlen(i_sStr
) > 0 : false )
378 WriteStr(o_rFile
," ");
379 WriteStr(o_rFile
,i_sStr
);
382 WriteStr(o_rFile
,C_sLineEnd
);
386 GenericInfo_Parser::WriteIndentation( ostream
& o_rFile
)
388 const int nIndentBound
= 60;
390 static const char sIndentation
[nIndentBound
+1] =
391 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
392 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
393 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
398 if ( nLevel
<= nIndentBound
)
399 o_rFile
.write( sIndentation
, nLevel
);
402 INT16 iLevel
= nLevel
;
403 for ( ; iLevel
> nIndentBound
; iLevel
-=nIndentBound
)
404 o_rFile
.write( sIndentation
, nIndentBound
);
405 o_rFile
.write( sIndentation
, iLevel
);