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