Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <string.h>
21 : #include <rscpar.hxx>
22 : #include <rscdb.hxx>
23 :
24 509 : void RscFileInst::Init()
25 : {
26 509 : nLineNo = 0;
27 509 : nLineBufLen = 256;
28 509 : pLine = (char *)rtl_allocateMemory( nLineBufLen );
29 509 : *pLine = '\0';
30 509 : nScanPos = 0;
31 509 : cLastChar = '\0';
32 509 : bEof = false;
33 509 : }
34 :
35 509 : RscFileInst::RscFileInst( RscTypCont * pTC, sal_uLong lIndexSrc,
36 : sal_uLong lFIndex, FILE * fFile )
37 : : nErrorLine(0)
38 509 : , nErrorPos(0)
39 : {
40 509 : pTypCont = pTC;
41 509 : Init();
42 :
43 509 : lFileIndex = lFIndex;
44 509 : lSrcIndex = lIndexSrc;
45 509 : fInputFile = fFile;
46 :
47 : //Status: Zeiger am Ende des Lesepuffers
48 509 : nInputPos = nInputEndPos = nInputBufLen = READBUFFER_MAX;
49 509 : pInput = (char *)rtl_allocateMemory( nInputBufLen );
50 509 : }
51 :
52 509 : RscFileInst::~RscFileInst()
53 : {
54 509 : if( pInput )
55 509 : rtl_freeMemory( pInput );
56 509 : if( pLine )
57 509 : rtl_freeMemory( pLine );
58 509 : }
59 :
60 233589 : int RscFileInst::GetChar()
61 : {
62 233589 : if( pLine[ nScanPos ] )
63 0 : return pLine[ nScanPos++ ];
64 233589 : else if( nInputPos >= nInputEndPos && nInputEndPos != nInputBufLen )
65 : {
66 : // Dateiende
67 904 : bEof = true;
68 904 : return 0;
69 : }
70 : else
71 : {
72 232685 : GetNewLine();
73 232685 : return '\n';
74 : }
75 : }
76 :
77 232685 : void RscFileInst::GetNewLine()
78 : {
79 232685 : nLineNo++;
80 232685 : nScanPos = 0;
81 :
82 : //laeuft bis Dateiende
83 232685 : sal_uInt32 nLen = 0;
84 480547 : while( (nInputPos < nInputEndPos) || (nInputEndPos == nInputBufLen) )
85 : {
86 247862 : if( (nInputPos >= nInputEndPos) && fInputFile )
87 : {
88 16551 : nInputEndPos = fread( pInput, 1, nInputBufLen, fInputFile );
89 16551 : nInputPos = 0;
90 : }
91 :
92 4428383 : while( nInputPos < nInputEndPos )
93 : {
94 : //immer eine Zeile lesen
95 4165344 : if( nLen >= nLineBufLen )
96 : {
97 122 : nLineBufLen += 256;
98 : // einen dazu fuer '\0'
99 122 : pLine = (char*)rtl_reallocateMemory( pLine, nLineBufLen +1 );
100 : }
101 :
102 : // cr lf, lf cr, lf oder cr wird '\0'
103 4165344 : if( pInput[ nInputPos ] == '\n' )
104 : {
105 232685 : nInputPos++;
106 232685 : if( cLastChar != '\r' )
107 : {
108 232685 : cLastChar = '\n';
109 232685 : pLine[ nLen++ ] = '\0';
110 232685 : goto END;
111 : }
112 : }
113 3932659 : else if( pInput[ nInputPos ] == '\r' )
114 : {
115 0 : nInputPos++;
116 0 : if( cLastChar != '\n' )
117 : {
118 0 : cLastChar = '\r';
119 0 : pLine[ nLen++ ] = '\0';
120 0 : goto END;
121 : }
122 : }
123 : else
124 : {
125 3932659 : pLine[ nLen++ ] = pInput[ nInputPos++ ];
126 3932659 : if( nLen > 2 )
127 : {
128 3512336 : if( (unsigned char)pLine[nLen-3] == 0xef &&
129 904 : (unsigned char)pLine[nLen-2] == 0xbb &&
130 452 : (unsigned char)pLine[nLen-1] == 0xbf )
131 : {
132 452 : nLen -= 3;
133 : }
134 : }
135 : }
136 : };
137 : };
138 :
139 : // Abbruch ueber EOF
140 0 : pLine[ nLen ] = '\0';
141 :
142 : END:
143 232685 : if( pTypCont->pEH->GetListFile() )
144 : {
145 : char buf[ 10 ];
146 :
147 0 : sprintf( buf, "%5d ", (int)GetLineNo() );
148 0 : pTypCont->pEH->LstOut( buf );
149 0 : pTypCont->pEH->LstOut( GetLine() );
150 0 : pTypCont->pEH->LstOut( "\n" );
151 : }
152 232685 : }
153 :
154 452 : void RscFileInst::SetError( ERRTYPE aError )
155 : {
156 452 : if( aError.IsOk() )
157 : {
158 452 : aFirstError = aError;
159 452 : nErrorLine = GetLineNo();
160 452 : nErrorPos = GetScanPos() -1;
161 : };
162 452 : };
163 :
164 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|