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 <stdlib.h>
22 : #include <rtl/strbuf.hxx>
23 : #include <tools/stream.hxx>
24 : #include "dxfgrprd.hxx"
25 :
26 : // we use an own ReadLine function, because Stream::ReadLine stops if
27 : // a 0-sign occurs; this function converts 0-signs to blanks and reads
28 : // a complete line until a cr/lf is found
29 :
30 399 : OString DXFReadLine(SvStream& rIStm)
31 : {
32 : char buf[256 + 1];
33 399 : bool bEnd = false;
34 399 : sal_uLong nOldFilePos = rIStm.Tell();
35 399 : char c = 0;
36 :
37 399 : OStringBuffer aBuf;
38 :
39 1201 : while( !bEnd && !rIStm.GetError() ) // !!! do not check for EOF
40 : // !!! because we read blockwise
41 : {
42 403 : sal_uInt16 nLen = (sal_uInt16)rIStm.Read( buf, sizeof(buf)-1 );
43 403 : if( !nLen )
44 : {
45 0 : if( aBuf.isEmpty() )
46 0 : return OString();
47 : else
48 0 : break;
49 : }
50 :
51 4067 : for( sal_uInt16 n = 0; n < nLen ; n++ )
52 : {
53 4063 : c = buf[n];
54 4063 : if( c != '\n' && c != '\r' )
55 : {
56 3664 : if( !c )
57 1 : c = ' ';
58 3664 : aBuf.append(c);
59 : }
60 : else
61 : {
62 399 : bEnd = true;
63 399 : break;
64 : }
65 : }
66 : }
67 :
68 399 : if( !bEnd && !rIStm.GetError() && !aBuf.isEmpty() )
69 0 : bEnd = true;
70 :
71 399 : nOldFilePos += aBuf.getLength();
72 399 : if( rIStm.Tell() > nOldFilePos )
73 399 : nOldFilePos++;
74 399 : rIStm.Seek( nOldFilePos ); // seek because of BlockRead above!
75 :
76 399 : if( bEnd && (c=='\r' || c=='\n')) // special treatment of DOS files
77 : {
78 399 : char cTemp(0);
79 399 : rIStm.Read(&cTemp, 1);
80 399 : if( cTemp == c || (cTemp != '\n' && cTemp != '\r') )
81 399 : rIStm.Seek( nOldFilePos );
82 : }
83 :
84 399 : return aBuf.makeStringAndClear();
85 : }
86 :
87 0 : void DXFSkipLine(SvStream& rIStm)
88 : {
89 0 : while (!rIStm.GetError()) // !!! do not check for EOF
90 : // !!! because we read blockwise
91 : {
92 : char buf[256 + 1];
93 0 : sal_uInt16 nLen = (sal_uInt16)rIStm.Read(buf, sizeof(buf) - 1);
94 0 : for (sal_uInt16 n = 0; n < nLen; n++)
95 : {
96 0 : char c = buf[n];
97 0 : if ((c == '\n') || (c == '\r'))
98 : {
99 0 : rIStm.SeekRel(n-nLen+1); // return stream to next to current position
100 0 : char c1 = 0;
101 0 : rIStm.Read(&c1, 1);
102 0 : if (c1 == c || (c1 != '\n' && c1!= '\r'))
103 0 : rIStm.SeekRel(-1);
104 0 : return;
105 : }
106 : }
107 : }
108 : }
109 :
110 1 : DXFGroupReader::DXFGroupReader(SvStream & rIStream)
111 : : rIS(rIStream)
112 : , bStatus(true)
113 : , nLastG(0)
114 : , nGCount(0)
115 : , S()
116 1 : , I(0)
117 : {
118 1 : rIS.Seek(STREAM_SEEK_TO_END);
119 1 : nFileSize=rIS.Tell();
120 1 : rIS.Seek(0);
121 1 : }
122 :
123 200 : sal_uInt16 DXFGroupReader::Read()
124 : {
125 200 : sal_uInt16 nG = 0;
126 200 : if ( bStatus )
127 : {
128 200 : nGCount++;
129 200 : nG = (sal_uInt16)ReadI();
130 200 : if ( bStatus )
131 : {
132 199 : if (nG< 10) ReadS();
133 146 : else if (nG< 60) F = ReadF();
134 2 : else if (nG< 80) I = ReadI();
135 2 : else if (nG< 90) DXFSkipLine(rIS);
136 2 : else if (nG< 99) I = ReadI();
137 2 : else if (nG==100) ReadS();
138 2 : else if (nG==102) ReadS();
139 2 : else if (nG==105) DXFSkipLine(rIS);
140 2 : else if (nG< 140) DXFSkipLine(rIS);
141 2 : else if (nG< 148) F = ReadF();
142 2 : else if (nG< 170) DXFSkipLine(rIS);
143 2 : else if (nG< 176) I = ReadI();
144 2 : else if (nG< 180) DXFSkipLine(rIS); // ReadI();
145 2 : else if (nG< 210) DXFSkipLine(rIS);
146 2 : else if (nG< 240) F = ReadF();
147 2 : else if (nG<=369) DXFSkipLine(rIS);
148 2 : else if (nG< 999) DXFSkipLine(rIS);
149 2 : else if (nG<1010) ReadS();
150 0 : else if (nG<1060) F = ReadF();
151 0 : else if (nG<1072) I = ReadI();
152 0 : else bStatus = false;
153 : }
154 : }
155 200 : if ( !bStatus )
156 : {
157 1 : nG = 0;
158 1 : SetS();
159 1 : if ( nGCount != 0xffffffff )
160 : {
161 : // InfoBox(NULL,String("Error in group # ")+String(nGCount)).Execute();
162 1 : nGCount=0xffffffff;
163 : }
164 : }
165 200 : nLastG = nG;
166 200 : return nG;
167 : }
168 :
169 1 : void DXFGroupReader::SetS()
170 : {
171 1 : S = "EOF";
172 1 : }
173 :
174 200 : long DXFGroupReader::ReadI()
175 : {
176 200 : OString s = DXFReadLine(rIS);
177 200 : char *p=s.pData->buffer;
178 200 : const char *end = s.pData->buffer + s.pData->length;
179 :
180 200 : while((p != end) && (*p==0x20)) p++;
181 :
182 200 : if ((p == end) || ((*p<'0' || *p>'9') && *p!='-')) {
183 1 : bStatus=false;
184 1 : return 0;
185 : }
186 :
187 199 : long res = 0, nv = 1;
188 199 : if (*p == '-') {
189 0 : nv=-1;
190 0 : p++;
191 : }
192 :
193 745 : while ((p != end) && *p >= '0' && *p <= '9') {
194 347 : res=res*10+static_cast<long>(*p-'0');
195 347 : p++;
196 : }
197 :
198 199 : while ((p != end) && (*p==0x20)) p++;
199 199 : if (p != end) {
200 0 : bStatus=false;
201 0 : return 0;
202 : }
203 :
204 199 : return res*nv;
205 : }
206 :
207 144 : double DXFGroupReader::ReadF()
208 : {
209 144 : OString s = DXFReadLine(rIS);
210 144 : char *p = s.pData->buffer;
211 144 : const char *end = s.pData->buffer + s.pData->length;
212 :
213 144 : while((p != end) && (*p==0x20)) p++;
214 144 : if ((p == end) || ((*p<'0' || *p>'9') && *p!='.' && *p!='-')) {
215 0 : bStatus=false;
216 0 : return 0.0;
217 : }
218 144 : return atof(p);
219 : }
220 :
221 55 : void DXFGroupReader::ReadS()
222 : {
223 55 : S = DXFReadLine(rIS);
224 55 : }
225 :
226 :
227 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|