xref: /trunk/main/rsc/source/parser/rscpar.cxx (revision 477794c1)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_rsc.hxx"
26 /****************** I N C L U D E S **************************************/
27 // C and C++ Includes.
28 #include <string.h>
29 #include <rscpar.hxx>
30 #include <rscdb.hxx>
31 
32 /****************** R s c F i l e I n s t ********************************/
33 /****************** C O D E **********************************************/
34 /*************************************************************************
35 |*
36 |*	  RscFileInst::Init()
37 |*
38 |*	  Beschreibung
39 |*	  Ersterstellung	MM 05.11.91
40 |*	  Letzte Aenderung	MM 17.02.93
41 |*
42 *************************************************************************/
Init()43 void RscFileInst::Init()
44 {
45 	nLineNo = 0;
46 	nLineBufLen = 256;
47 	pLine = (char *)rtl_allocateMemory( nLineBufLen );
48 	*pLine = '\0';
49 	nScanPos = 0;
50 	cLastChar = '\0';
51 	bEof = sal_False;
52 };
53 
54 /*************************************************************************
55 |*
56 |*	  RscFileInst::RscFileInst()
57 |*
58 |*	  Beschreibung
59 |*	  Ersterstellung	MM 06.06.91
60 |*	  Letzte Aenderung	MM 06.06.91
61 |*
62 *************************************************************************/
RscFileInst(RscTypCont * pTC,sal_uLong lIndexSrc,sal_uLong lFIndex,FILE * fFile)63 RscFileInst::RscFileInst( RscTypCont * pTC, sal_uLong lIndexSrc,
64 						  sal_uLong lFIndex, FILE * fFile )
65 {
66 	pTypCont = pTC;
67 	Init();
68 
69 	lFileIndex = lFIndex;
70 	lSrcIndex = lIndexSrc;
71 	fInputFile = fFile;
72 
73 	//Status: Zeiger am Ende des Lesepuffers
74 	nInputPos = nInputEndPos = nInputBufLen = READBUFFER_MAX;
75 	pInput	  = (char *)rtl_allocateMemory( nInputBufLen );
76 }
77 
RscFileInst(RscTypCont * pTC,sal_uLong lIndexSrc,sal_uLong lFIndex,const ByteString & rBuf)78 RscFileInst::RscFileInst( RscTypCont * pTC, sal_uLong lIndexSrc,
79 						  sal_uLong lFIndex, const ByteString& rBuf )
80 {
81 	pTypCont	 = pTC;
82 	Init();
83 	lFileIndex	 = lFIndex;
84 	lSrcIndex	 = lIndexSrc;
85 	fInputFile	 = NULL;
86 	nInputPos	 = 0;
87 	nInputEndPos = rBuf.Len();
88 
89 	// Muss groesser sein wegen Eingabeende bei nInputBufLen < nInputEndPos
90 	nInputBufLen = nInputEndPos +1;
91 	pInput		 = (char *)rtl_allocateMemory( nInputBufLen +100 );
92 	memcpy( pInput, rBuf.GetBuffer(), nInputEndPos );
93 }
94 
95 /*************************************************************************
96 |*
97 |*	  RscFileInst::~RscFileInst()
98 |*
99 |*	  Beschreibung
100 |*	  Ersterstellung	MM 06.06.91
101 |*	  Letzte Aenderung	MM 06.06.91
102 |*
103 *************************************************************************/
~RscFileInst()104 RscFileInst::~RscFileInst(){
105 	if( pInput )
106 		rtl_freeMemory( pInput );
107 	if( pLine )
108 		rtl_freeMemory( pLine );
109 }
110 
111 /*************************************************************************
112 |*
113 |*	  RscFileInst::GetChar()
114 |*
115 |*	  Beschreibung
116 |*	  Ersterstellung	MM 01.06.91
117 |*	  Letzte Aenderung	MM 09.08.91
118 |*
119 *************************************************************************/
GetChar()120 int RscFileInst::GetChar()
121 {
122 	if( pLine[ nScanPos ] )
123 		return( pLine[ nScanPos++ ] );
124 	else if( nInputPos >= nInputEndPos && nInputEndPos != nInputBufLen )
125 	{
126 		// Dateiende
127 		bEof = sal_True;
128 		return 0;
129 	}
130 	else
131 	{
132 		GetNewLine();
133 		return( '\n' );
134 	}
135 }
136 
137 /*************************************************************************
138 |*
139 |*	  RscFileInst::GetNewLine()
140 |*
141 |*	  Beschreibung
142 |*	  Ersterstellung	MM 06.06.91
143 |*	  Letzte Aenderung	MM 06.06.91
144 |*
145 *************************************************************************/
GetNewLine()146 void RscFileInst::GetNewLine()
147 {
148 	nLineNo++;
149 	nScanPos = 0;
150 
151 	//laeuft bis Dateiende
152 	sal_uInt32 nLen = 0;
153 	while( (nInputPos < nInputEndPos) || (nInputEndPos == nInputBufLen) )
154 	{
155 		if( (nInputPos >= nInputEndPos) && fInputFile )
156 		{
157 			nInputEndPos = fread( pInput, 1, nInputBufLen, fInputFile );
158 			nInputPos = 0;
159 		}
160 
161 		while( nInputPos < nInputEndPos )
162 		{
163 			//immer eine Zeile lesen
164 			if( nLen >= nLineBufLen )
165 			{
166 				nLineBufLen += 256;
167 				// einen dazu fuer '\0'
168 				pLine = (char*)rtl_reallocateMemory( pLine, nLineBufLen +1 );
169 			}
170 
171 			// cr lf, lf cr, lf oder cr wird '\0'
172 			if( pInput[ nInputPos ] == '\n' ){
173 				nInputPos++;
174 				if( cLastChar != '\r' ){
175 					cLastChar = '\n';
176 					pLine[ nLen++ ] = '\0';
177 					goto END;
178 				}
179 			}
180 			else if( pInput[ nInputPos ] == '\r' ){
181 				nInputPos++;
182 				if( cLastChar != '\n' ){
183 					cLastChar = '\r';
184 					pLine[ nLen++ ] = '\0';
185 					goto END;
186 				}
187 			}
188 			else
189             {
190 				pLine[ nLen++ ] = pInput[ nInputPos++ ];
191                 if( nLen > 2 )
192                 {
193                     if( (unsigned char)pLine[nLen-3] == 0xef &&
194                         (unsigned char)pLine[nLen-2] == 0xbb &&
195                         (unsigned char)pLine[nLen-1] == 0xbf )
196                     {
197                         nLen -= 3;
198                     }
199                 }
200             }
201 		};
202 	};
203 
204 	// Abbruch ueber EOF
205 	pLine[ nLen ] = '\0';
206 
207 END:
208 	if( pTypCont->pEH->GetListFile() ){
209 		char buf[ 10 ];
210 
211 		sprintf( buf, "%5d ", (int)GetLineNo() );
212 		pTypCont->pEH->LstOut( buf );
213 		pTypCont->pEH->LstOut( GetLine() );
214 		pTypCont->pEH->LstOut( "\n" );
215 	}
216 }
217 
218 /*************************************************************************
219 |*
220 |*	  RscFileInst::SetError()
221 |*
222 |*	  Beschreibung
223 |*	  Ersterstellung	MM 05.11.91
224 |*	  Letzte Aenderung	MM 05.11.91
225 |*
226 *************************************************************************/
SetError(ERRTYPE aError)227 void RscFileInst::SetError( ERRTYPE aError )
228 {
229 	if( aError.IsOk() )
230 	{
231 		aFirstError = aError;
232 		nErrorLine	= GetLineNo();
233 		nErrorPos	= GetScanPos() -1;
234 	};
235 };
236