1*7a4715d3SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*7a4715d3SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*7a4715d3SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*7a4715d3SAndrew Rist * distributed with this work for additional information
6*7a4715d3SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*7a4715d3SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*7a4715d3SAndrew Rist * "License"); you may not use this file except in compliance
9*7a4715d3SAndrew Rist * with the License. You may obtain a copy of the License at
10*7a4715d3SAndrew Rist *
11*7a4715d3SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*7a4715d3SAndrew Rist *
13*7a4715d3SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*7a4715d3SAndrew Rist * software distributed under the License is distributed on an
15*7a4715d3SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*7a4715d3SAndrew Rist * KIND, either express or implied. See the License for the
17*7a4715d3SAndrew Rist * specific language governing permissions and limitations
18*7a4715d3SAndrew Rist * under the License.
19*7a4715d3SAndrew Rist *
20*7a4715d3SAndrew Rist *************************************************************/
21*7a4715d3SAndrew Rist
22*7a4715d3SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_soltools.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <gi_parse.hxx>
28cdf0e10cSrcweir
29cdf0e10cSrcweir #include <stdio.h>
30cdf0e10cSrcweir #include <string.h>
31cdf0e10cSrcweir #include <fstream>
32cdf0e10cSrcweir #include <gilacces.hxx>
33cdf0e10cSrcweir
34cdf0e10cSrcweir
35cdf0e10cSrcweir using namespace std;
36cdf0e10cSrcweir
37cdf0e10cSrcweir
38cdf0e10cSrcweir const char * C_sLineEnd = "\r\n";
39cdf0e10cSrcweir
40cdf0e10cSrcweir
41cdf0e10cSrcweir
42cdf0e10cSrcweir inline void
WriteStr(ostream & o_rOut,const Simstr & i_rStr)43cdf0e10cSrcweir WriteStr( ostream & o_rOut, const Simstr & i_rStr )
44cdf0e10cSrcweir {
45cdf0e10cSrcweir o_rOut.write( i_rStr.str(), i_rStr.l() );
46cdf0e10cSrcweir }
47cdf0e10cSrcweir
48cdf0e10cSrcweir inline void
WriteStr(ostream & o_rOut,const char * i_rStr)49cdf0e10cSrcweir WriteStr( ostream & o_rOut, const char * i_rStr )
50cdf0e10cSrcweir {
51cdf0e10cSrcweir o_rOut.write( i_rStr, strlen(i_rStr) );
52cdf0e10cSrcweir }
53cdf0e10cSrcweir
54cdf0e10cSrcweir inline void
SetError(E_Error i_eError)55cdf0e10cSrcweir GenericInfo_Parser::SetError( E_Error i_eError )
56cdf0e10cSrcweir {
57cdf0e10cSrcweir eErrorCode = i_eError;
58cdf0e10cSrcweir nErrorLine = nCurLine;
59cdf0e10cSrcweir }
60cdf0e10cSrcweir
61cdf0e10cSrcweir
GenericInfo_Parser()62cdf0e10cSrcweir GenericInfo_Parser::GenericInfo_Parser()
63cdf0e10cSrcweir : sCurParsePosition(""),
64cdf0e10cSrcweir nCurLine(0),
65cdf0e10cSrcweir nLevel(0),
66cdf0e10cSrcweir bGoon(false),
67cdf0e10cSrcweir // sCurComment,
68cdf0e10cSrcweir eErrorCode(ok),
69cdf0e10cSrcweir nErrorLine(0),
70cdf0e10cSrcweir pResult(0),
71cdf0e10cSrcweir pResource(0)
72cdf0e10cSrcweir {
73cdf0e10cSrcweir }
74cdf0e10cSrcweir
~GenericInfo_Parser()75cdf0e10cSrcweir GenericInfo_Parser::~GenericInfo_Parser()
76cdf0e10cSrcweir {
77cdf0e10cSrcweir }
78cdf0e10cSrcweir
79cdf0e10cSrcweir bool
LoadList(GenericInfoList_Builder & o_rResult,const Simstr & i_sSourceFileName)80cdf0e10cSrcweir GenericInfo_Parser::LoadList( GenericInfoList_Builder & o_rResult,
81cdf0e10cSrcweir const Simstr & i_sSourceFileName )
82cdf0e10cSrcweir {
83cdf0e10cSrcweir ifstream aFile( i_sSourceFileName.str() );
84cdf0e10cSrcweir if ( aFile.fail() )
85cdf0e10cSrcweir {
86cdf0e10cSrcweir SetError(cannot_open);
87cdf0e10cSrcweir return false;
88cdf0e10cSrcweir }
89cdf0e10cSrcweir
90cdf0e10cSrcweir aFile.seekg(0, ios::end);
91cdf0e10cSrcweir UINT32 nTextSize = aFile.tellg();
92cdf0e10cSrcweir if ( nTextSize == 0 || nTextSize == UINT32(-1) )
93cdf0e10cSrcweir return true;
94cdf0e10cSrcweir dpBuffer = new char[nTextSize+2];
95cdf0e10cSrcweir
96cdf0e10cSrcweir aFile.seekg(0);
97cdf0e10cSrcweir aFile.read( dpBuffer, nTextSize );
98cdf0e10cSrcweir aFile.close();
99cdf0e10cSrcweir
100cdf0e10cSrcweir sFilePtr = dpBuffer;
101cdf0e10cSrcweir char * sLastChar = dpBuffer + nTextSize - 1;
102cdf0e10cSrcweir
103cdf0e10cSrcweir while ( sFilePtr != sLastChar && *sFilePtr <= 32 )
104cdf0e10cSrcweir ++sCurParsePosition;
105cdf0e10cSrcweir if ( sFilePtr == sLastChar )
106cdf0e10cSrcweir {
107cdf0e10cSrcweir if ( *sFilePtr <= 32 )
108cdf0e10cSrcweir return true;
109cdf0e10cSrcweir }
110cdf0e10cSrcweir else while ( *sLastChar <= 32 )
111cdf0e10cSrcweir {
112cdf0e10cSrcweir --sLastChar;
113cdf0e10cSrcweir }
114cdf0e10cSrcweir
115cdf0e10cSrcweir *(sLastChar+1) = '\n';
116cdf0e10cSrcweir *(sLastChar+2) = '\0';
117cdf0e10cSrcweir
118cdf0e10cSrcweir ResetState(o_rResult);
119cdf0e10cSrcweir
120cdf0e10cSrcweir for ( ReadLine(); bGoon; ReadLine() )
121cdf0e10cSrcweir {
122cdf0e10cSrcweir bool bOk = InterpretLine();
123cdf0e10cSrcweir if ( !bOk)
124cdf0e10cSrcweir {
125cdf0e10cSrcweir SetError(syntax_error);
126cdf0e10cSrcweir break;
127cdf0e10cSrcweir }
128cdf0e10cSrcweir }
129cdf0e10cSrcweir
130cdf0e10cSrcweir if ( nLevel > 0 && eErrorCode == ok)
131cdf0e10cSrcweir {
132cdf0e10cSrcweir SetError(unexpected_eof);
133cdf0e10cSrcweir }
134cdf0e10cSrcweir else if ( nLevel < 0 )
135cdf0e10cSrcweir {
136cdf0e10cSrcweir SetError(unexpected_list_end);
137cdf0e10cSrcweir }
138cdf0e10cSrcweir
139cdf0e10cSrcweir delete [] dpBuffer;
140cdf0e10cSrcweir dpBuffer = 0;
141cdf0e10cSrcweir sFilePtr = 0;
142cdf0e10cSrcweir
143cdf0e10cSrcweir return eErrorCode == ok;
144cdf0e10cSrcweir }
145cdf0e10cSrcweir
146cdf0e10cSrcweir bool
SaveList(const Simstr & i_rOutputFile,GenericInfoList_Browser & io_rListBrowser)147cdf0e10cSrcweir GenericInfo_Parser::SaveList( const Simstr & i_rOutputFile,
148cdf0e10cSrcweir GenericInfoList_Browser & io_rListBrowser )
149cdf0e10cSrcweir {
150cdf0e10cSrcweir ofstream aFile( i_rOutputFile.str() );
151cdf0e10cSrcweir if ( aFile.fail() )
152cdf0e10cSrcweir {
153cdf0e10cSrcweir SetError(cannot_open);
154cdf0e10cSrcweir return false;
155cdf0e10cSrcweir }
156cdf0e10cSrcweir
157cdf0e10cSrcweir ResetState(io_rListBrowser);
158cdf0e10cSrcweir
159cdf0e10cSrcweir WriteList(aFile);
160cdf0e10cSrcweir
161cdf0e10cSrcweir aFile.close();
162cdf0e10cSrcweir return eErrorCode == ok;
163cdf0e10cSrcweir }
164cdf0e10cSrcweir
165cdf0e10cSrcweir void
ResetState(GenericInfoList_Builder & io_rResult)166cdf0e10cSrcweir GenericInfo_Parser::ResetState( GenericInfoList_Builder & io_rResult )
167cdf0e10cSrcweir {
168cdf0e10cSrcweir sCurParsePosition = "";
169cdf0e10cSrcweir nCurLine = 0;
170cdf0e10cSrcweir nLevel = 0;
171cdf0e10cSrcweir bGoon = true;
172cdf0e10cSrcweir sCurComment = "";
173cdf0e10cSrcweir eErrorCode = ok;
174cdf0e10cSrcweir nErrorLine = 0;
175cdf0e10cSrcweir pResult = &io_rResult;
176cdf0e10cSrcweir pResource = 0;
177cdf0e10cSrcweir }
178cdf0e10cSrcweir
179cdf0e10cSrcweir void
ResetState(GenericInfoList_Browser & io_rSrc)180cdf0e10cSrcweir GenericInfo_Parser::ResetState( GenericInfoList_Browser & io_rSrc )
181cdf0e10cSrcweir {
182cdf0e10cSrcweir sCurParsePosition = "";
183cdf0e10cSrcweir nCurLine = 0;
184cdf0e10cSrcweir nLevel = 0;
185cdf0e10cSrcweir bGoon = false;
186cdf0e10cSrcweir sCurComment = "";
187cdf0e10cSrcweir eErrorCode = ok;
188cdf0e10cSrcweir nErrorLine = 0;
189cdf0e10cSrcweir pResult = 0;
190cdf0e10cSrcweir pResource = &io_rSrc;
191cdf0e10cSrcweir }
192cdf0e10cSrcweir
193cdf0e10cSrcweir
194cdf0e10cSrcweir void
ReadLine()195cdf0e10cSrcweir GenericInfo_Parser::ReadLine()
196cdf0e10cSrcweir {
197cdf0e10cSrcweir if ( *sFilePtr == '\0' ) // See initialising of dpBuffer and sLastChar in LoadList().
198cdf0e10cSrcweir {
199cdf0e10cSrcweir bGoon = false;
200cdf0e10cSrcweir return;
201cdf0e10cSrcweir }
202cdf0e10cSrcweir
203cdf0e10cSrcweir sCurParsePosition = sFilePtr;
204cdf0e10cSrcweir while ( *sFilePtr != '\n' )
205cdf0e10cSrcweir ++sFilePtr;
206cdf0e10cSrcweir nCurLine++;
207cdf0e10cSrcweir
208cdf0e10cSrcweir // Remove leading and trailing whitespace from line:
209cdf0e10cSrcweir while ( sCurParsePosition != sFilePtr && *sCurParsePosition <= 32 )
210cdf0e10cSrcweir ++sCurParsePosition;
211cdf0e10cSrcweir
212cdf0e10cSrcweir char * sEndOfLine = sFilePtr;
213cdf0e10cSrcweir while ( sEndOfLine != sCurParsePosition && *sEndOfLine <= 32 )
214cdf0e10cSrcweir --sEndOfLine;
215cdf0e10cSrcweir if ( sCurParsePosition != sEndOfLine || *sCurParsePosition > 32 )
216cdf0e10cSrcweir ++sEndOfLine;
217cdf0e10cSrcweir *sEndOfLine = '\0';
218cdf0e10cSrcweir
219cdf0e10cSrcweir ++sFilePtr; // Go beyond line end to first character of next line.
220cdf0e10cSrcweir }
221cdf0e10cSrcweir
222cdf0e10cSrcweir bool
InterpretLine()223cdf0e10cSrcweir GenericInfo_Parser::InterpretLine()
224cdf0e10cSrcweir {
225cdf0e10cSrcweir switch ( ClassifyLine() )
226cdf0e10cSrcweir {
227cdf0e10cSrcweir case lt_key: ReadKey();
228cdf0e10cSrcweir break;
229cdf0e10cSrcweir case lt_open_list: PushLevel_Read();
230cdf0e10cSrcweir break;
231cdf0e10cSrcweir case lt_close_list: PopLevel_Read();
232cdf0e10cSrcweir break;
233cdf0e10cSrcweir case lt_comment: AddCurLine2CurComment();
234cdf0e10cSrcweir break;
235cdf0e10cSrcweir case lt_empty: AddCurLine2CurComment();
236cdf0e10cSrcweir break;
237cdf0e10cSrcweir default:
238cdf0e10cSrcweir return false;
239cdf0e10cSrcweir }
240cdf0e10cSrcweir return true;
241cdf0e10cSrcweir }
242cdf0e10cSrcweir
243cdf0e10cSrcweir GenericInfo_Parser::E_LineType
ClassifyLine()244cdf0e10cSrcweir GenericInfo_Parser::ClassifyLine()
245cdf0e10cSrcweir {
246cdf0e10cSrcweir switch ( *sCurParsePosition )
247cdf0e10cSrcweir {
248cdf0e10cSrcweir case '{': return lt_open_list;
249cdf0e10cSrcweir case '}': return lt_close_list;
250cdf0e10cSrcweir case '#': return lt_comment;
251cdf0e10cSrcweir case '\0': return lt_empty;
252cdf0e10cSrcweir }
253cdf0e10cSrcweir
254cdf0e10cSrcweir return lt_key;
255cdf0e10cSrcweir }
256cdf0e10cSrcweir
257cdf0e10cSrcweir void
ReadKey()258cdf0e10cSrcweir GenericInfo_Parser::ReadKey()
259cdf0e10cSrcweir {
260cdf0e10cSrcweir const char * pSearch = sCurParsePosition;
261cdf0e10cSrcweir
262cdf0e10cSrcweir for ( ; *pSearch > 32; ++pSearch ) ;
263cdf0e10cSrcweir UINT32 nKeyLength = pSearch - sCurParsePosition;
264cdf0e10cSrcweir
265cdf0e10cSrcweir for ( ; *pSearch <= 32 && *pSearch > '\0'; ++pSearch ) ;
266cdf0e10cSrcweir
267cdf0e10cSrcweir pResult->AddKey( sCurParsePosition, nKeyLength,
268cdf0e10cSrcweir pSearch, strlen(pSearch),
269cdf0e10cSrcweir sCurComment.str(), sCurComment.l()
270cdf0e10cSrcweir );
271cdf0e10cSrcweir sCurComment = "";
272cdf0e10cSrcweir }
273cdf0e10cSrcweir
274cdf0e10cSrcweir void
PushLevel_Read()275cdf0e10cSrcweir GenericInfo_Parser::PushLevel_Read()
276cdf0e10cSrcweir {
277cdf0e10cSrcweir nLevel++;
278cdf0e10cSrcweir pResult->OpenList();
279cdf0e10cSrcweir }
280cdf0e10cSrcweir
281cdf0e10cSrcweir void
PopLevel_Read()282cdf0e10cSrcweir GenericInfo_Parser::PopLevel_Read()
283cdf0e10cSrcweir {
284cdf0e10cSrcweir nLevel--;
285cdf0e10cSrcweir pResult->CloseList();
286cdf0e10cSrcweir }
287cdf0e10cSrcweir
288cdf0e10cSrcweir void
AddCurLine2CurComment()289cdf0e10cSrcweir GenericInfo_Parser::AddCurLine2CurComment()
290cdf0e10cSrcweir {
291cdf0e10cSrcweir sCurComment += sCurParsePosition;
292cdf0e10cSrcweir sCurComment += C_sLineEnd;
293cdf0e10cSrcweir }
294cdf0e10cSrcweir
295cdf0e10cSrcweir void
WriteList(ostream & o_rFile)296cdf0e10cSrcweir GenericInfo_Parser::WriteList( ostream & o_rFile )
297cdf0e10cSrcweir {
298cdf0e10cSrcweir static char sBuffer[32000];
299cdf0e10cSrcweir
300cdf0e10cSrcweir for ( bGoon = pResource->Start_CurList();
301cdf0e10cSrcweir bGoon;
302cdf0e10cSrcweir bGoon = pResource->NextOf_CurList() )
303cdf0e10cSrcweir {
304cdf0e10cSrcweir pResource->Get_CurComment(&sBuffer[0]);
305cdf0e10cSrcweir WriteComment(o_rFile,sBuffer);
306cdf0e10cSrcweir
307cdf0e10cSrcweir pResource->Get_CurKey(&sBuffer[0]);
308cdf0e10cSrcweir WriteKey(o_rFile,sBuffer);
309cdf0e10cSrcweir
310cdf0e10cSrcweir pResource->Get_CurValue(&sBuffer[0]);
311cdf0e10cSrcweir WriteValue(o_rFile,sBuffer);
312cdf0e10cSrcweir
313cdf0e10cSrcweir if ( pResource->HasSubList_CurKey() )
314cdf0e10cSrcweir {
315cdf0e10cSrcweir PushLevel_Write();
316cdf0e10cSrcweir
317cdf0e10cSrcweir /*
318cdf0e10cSrcweir WriteIndentation();
319cdf0e10cSrcweir o_rFile.write("{",1);
320cdf0e10cSrcweir o_rFile.write(C_sLineEnd, C_nLineEndLength);
321cdf0e10cSrcweir */
322cdf0e10cSrcweir WriteList(o_rFile);
323cdf0e10cSrcweir
324cdf0e10cSrcweir /*
325cdf0e10cSrcweir WriteIndentation();
326cdf0e10cSrcweir o_rFile.write("}",1);
327cdf0e10cSrcweir o_rFile.write(C_sLineEnd, C_nLineEndLength);
328cdf0e10cSrcweir */
329cdf0e10cSrcweir PopLevel_Write();
330cdf0e10cSrcweir }
331cdf0e10cSrcweir } // end for
332cdf0e10cSrcweir }
333cdf0e10cSrcweir
334cdf0e10cSrcweir void
PushLevel_Write()335cdf0e10cSrcweir GenericInfo_Parser::PushLevel_Write()
336cdf0e10cSrcweir {
337cdf0e10cSrcweir nLevel++;
338cdf0e10cSrcweir pResource->Push_CurList();
339cdf0e10cSrcweir }
340cdf0e10cSrcweir
341cdf0e10cSrcweir void
PopLevel_Write()342cdf0e10cSrcweir GenericInfo_Parser::PopLevel_Write()
343cdf0e10cSrcweir {
344cdf0e10cSrcweir nLevel--;
345cdf0e10cSrcweir pResource->Pop_CurList();
346cdf0e10cSrcweir }
347cdf0e10cSrcweir
348cdf0e10cSrcweir void
WriteComment(ostream & o_rFile,const char * i_sStr)349cdf0e10cSrcweir GenericInfo_Parser::WriteComment( ostream & o_rFile,
350cdf0e10cSrcweir const char * i_sStr )
351cdf0e10cSrcweir {
352cdf0e10cSrcweir WriteStr( o_rFile, i_sStr );
353cdf0e10cSrcweir if ( i_sStr[ strlen(i_sStr)-1 ] != '\n' )
354cdf0e10cSrcweir WriteStr( o_rFile, C_sLineEnd );
355cdf0e10cSrcweir }
356cdf0e10cSrcweir
357cdf0e10cSrcweir void
WriteKey(ostream & o_rFile,const char * i_sStr)358cdf0e10cSrcweir GenericInfo_Parser::WriteKey( ostream & o_rFile,
359cdf0e10cSrcweir const char * i_sStr )
360cdf0e10cSrcweir {
361cdf0e10cSrcweir WriteIndentation(o_rFile);
362cdf0e10cSrcweir WriteStr( o_rFile, i_sStr );
363cdf0e10cSrcweir }
364cdf0e10cSrcweir
365cdf0e10cSrcweir void
WriteValue(ostream & o_rFile,const char * i_sStr)366cdf0e10cSrcweir GenericInfo_Parser::WriteValue( ostream & o_rFile,
367cdf0e10cSrcweir const char * i_sStr )
368cdf0e10cSrcweir {
369cdf0e10cSrcweir if ( i_sStr != 0 ? strlen(i_sStr) > 0 : false )
370cdf0e10cSrcweir {
371cdf0e10cSrcweir WriteStr(o_rFile," ");
372cdf0e10cSrcweir WriteStr(o_rFile,i_sStr);
373cdf0e10cSrcweir }
374cdf0e10cSrcweir
375cdf0e10cSrcweir WriteStr(o_rFile,C_sLineEnd);
376cdf0e10cSrcweir }
377cdf0e10cSrcweir
378cdf0e10cSrcweir void
WriteIndentation(ostream & o_rFile)379cdf0e10cSrcweir GenericInfo_Parser::WriteIndentation( ostream & o_rFile )
380cdf0e10cSrcweir {
381cdf0e10cSrcweir const int nIndentBound = 60;
382cdf0e10cSrcweir
383cdf0e10cSrcweir static const char sIndentation[nIndentBound+1] =
384cdf0e10cSrcweir "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
385cdf0e10cSrcweir "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
386cdf0e10cSrcweir "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
387cdf0e10cSrcweir
388cdf0e10cSrcweir if ( nLevel == 0 )
389cdf0e10cSrcweir return;
390cdf0e10cSrcweir
391cdf0e10cSrcweir if ( nLevel <= nIndentBound )
392cdf0e10cSrcweir o_rFile.write( sIndentation, nLevel );
393cdf0e10cSrcweir else
394cdf0e10cSrcweir {
395cdf0e10cSrcweir INT16 iLevel = nLevel;
396cdf0e10cSrcweir for ( ; iLevel > nIndentBound; iLevel-=nIndentBound )
397cdf0e10cSrcweir o_rFile.write( sIndentation, nIndentBound );
398cdf0e10cSrcweir o_rFile.write( sIndentation, iLevel );
399cdf0e10cSrcweir }
400cdf0e10cSrcweir }
401cdf0e10cSrcweir
402cdf0e10cSrcweir
403cdf0e10cSrcweir
404