xref: /trunk/main/rsc/source/parser/rscpar.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_rsc.hxx"
30 /****************** I N C L U D E S **************************************/
31 // C and C++ Includes.
32 #include <string.h>
33 #include <rscpar.hxx>
34 #include <rscdb.hxx>
35 
36 /****************** R s c F i l e I n s t ********************************/
37 /****************** C O D E **********************************************/
38 /*************************************************************************
39 |*
40 |*    RscFileInst::Init()
41 |*
42 |*    Beschreibung
43 |*    Ersterstellung    MM 05.11.91
44 |*    Letzte Aenderung  MM 17.02.93
45 |*
46 *************************************************************************/
47 void RscFileInst::Init()
48 {
49     nLineNo = 0;
50     nLineBufLen = 256;
51     pLine = (char *)rtl_allocateMemory( nLineBufLen );
52     *pLine = '\0';
53     nScanPos = 0;
54     cLastChar = '\0';
55     bEof = sal_False;
56 };
57 
58 /*************************************************************************
59 |*
60 |*    RscFileInst::RscFileInst()
61 |*
62 |*    Beschreibung
63 |*    Ersterstellung    MM 06.06.91
64 |*    Letzte Aenderung  MM 06.06.91
65 |*
66 *************************************************************************/
67 RscFileInst::RscFileInst( RscTypCont * pTC, sal_uLong lIndexSrc,
68                           sal_uLong lFIndex, FILE * fFile )
69 {
70     pTypCont = pTC;
71     Init();
72 
73     lFileIndex = lFIndex;
74     lSrcIndex = lIndexSrc;
75     fInputFile = fFile;
76 
77     //Status: Zeiger am Ende des Lesepuffers
78     nInputPos = nInputEndPos = nInputBufLen = READBUFFER_MAX;
79     pInput    = (char *)rtl_allocateMemory( nInputBufLen );
80 }
81 
82 RscFileInst::RscFileInst( RscTypCont * pTC, sal_uLong lIndexSrc,
83                           sal_uLong lFIndex, const ByteString& rBuf )
84 {
85     pTypCont     = pTC;
86     Init();
87     lFileIndex   = lFIndex;
88     lSrcIndex    = lIndexSrc;
89     fInputFile   = NULL;
90     nInputPos    = 0;
91     nInputEndPos = rBuf.Len();
92 
93     // Muss groesser sein wegen Eingabeende bei nInputBufLen < nInputEndPos
94     nInputBufLen = nInputEndPos +1;
95     pInput       = (char *)rtl_allocateMemory( nInputBufLen +100 );
96     memcpy( pInput, rBuf.GetBuffer(), nInputEndPos );
97 }
98 
99 /*************************************************************************
100 |*
101 |*    RscFileInst::~RscFileInst()
102 |*
103 |*    Beschreibung
104 |*    Ersterstellung    MM 06.06.91
105 |*    Letzte Aenderung  MM 06.06.91
106 |*
107 *************************************************************************/
108 RscFileInst::~RscFileInst(){
109     if( pInput )
110         rtl_freeMemory( pInput );
111     if( pLine )
112         rtl_freeMemory( pLine );
113 }
114 
115 /*************************************************************************
116 |*
117 |*    RscFileInst::GetChar()
118 |*
119 |*    Beschreibung
120 |*    Ersterstellung    MM 01.06.91
121 |*    Letzte Aenderung  MM 09.08.91
122 |*
123 *************************************************************************/
124 int RscFileInst::GetChar()
125 {
126     if( pLine[ nScanPos ] )
127         return( pLine[ nScanPos++ ] );
128     else if( nInputPos >= nInputEndPos && nInputEndPos != nInputBufLen )
129     {
130         // Dateiende
131         bEof = sal_True;
132         return 0;
133     }
134     else
135     {
136         GetNewLine();
137         return( '\n' );
138     }
139 }
140 
141 /*************************************************************************
142 |*
143 |*    RscFileInst::GetNewLine()
144 |*
145 |*    Beschreibung
146 |*    Ersterstellung    MM 06.06.91
147 |*    Letzte Aenderung  MM 06.06.91
148 |*
149 *************************************************************************/
150 void RscFileInst::GetNewLine()
151 {
152     nLineNo++;
153     nScanPos = 0;
154 
155     //laeuft bis Dateiende
156     sal_uInt32 nLen = 0;
157     while( (nInputPos < nInputEndPos) || (nInputEndPos == nInputBufLen) )
158     {
159         if( (nInputPos >= nInputEndPos) && fInputFile )
160         {
161             nInputEndPos = fread( pInput, 1, nInputBufLen, fInputFile );
162             nInputPos = 0;
163         }
164 
165         while( nInputPos < nInputEndPos )
166         {
167             //immer eine Zeile lesen
168             if( nLen >= nLineBufLen )
169             {
170                 nLineBufLen += 256;
171                 // einen dazu fuer '\0'
172                 pLine = (char*)rtl_reallocateMemory( pLine, nLineBufLen +1 );
173             }
174 
175             // cr lf, lf cr, lf oder cr wird '\0'
176             if( pInput[ nInputPos ] == '\n' ){
177                 nInputPos++;
178                 if( cLastChar != '\r' ){
179                     cLastChar = '\n';
180                     pLine[ nLen++ ] = '\0';
181                     goto END;
182                 }
183             }
184             else if( pInput[ nInputPos ] == '\r' ){
185                 nInputPos++;
186                 if( cLastChar != '\n' ){
187                     cLastChar = '\r';
188                     pLine[ nLen++ ] = '\0';
189                     goto END;
190                 }
191             }
192             else
193             {
194                 pLine[ nLen++ ] = pInput[ nInputPos++ ];
195                 if( nLen > 2 )
196                 {
197                     if( (unsigned char)pLine[nLen-3] == 0xef &&
198                         (unsigned char)pLine[nLen-2] == 0xbb &&
199                         (unsigned char)pLine[nLen-1] == 0xbf )
200                     {
201                         nLen -= 3;
202                     }
203                 }
204             }
205         };
206     };
207 
208     // Abbruch ueber EOF
209     pLine[ nLen ] = '\0';
210 
211 END:
212     if( pTypCont->pEH->GetListFile() ){
213         char buf[ 10 ];
214 
215         sprintf( buf, "%5d ", (int)GetLineNo() );
216         pTypCont->pEH->LstOut( buf );
217         pTypCont->pEH->LstOut( GetLine() );
218         pTypCont->pEH->LstOut( "\n" );
219     }
220 }
221 
222 /*************************************************************************
223 |*
224 |*    RscFileInst::SetError()
225 |*
226 |*    Beschreibung
227 |*    Ersterstellung    MM 05.11.91
228 |*    Letzte Aenderung  MM 05.11.91
229 |*
230 *************************************************************************/
231 void RscFileInst::SetError( ERRTYPE aError )
232 {
233     if( aError.IsOk() )
234     {
235         aFirstError = aError;
236         nErrorLine  = GetLineNo();
237         nErrorPos   = GetScanPos() -1;
238     };
239 };
240