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_filter.hxx"
26
27 #include <string.h>
28 #include <stdlib.h>
29 #include <tools/stream.hxx>
30 #include "dxfgrprd.hxx"
31
32 // ----------------------------------------------------------------------------
33
34 // we use an own ReadLine function, because Stream::ReadLine stops if
35 // a 0-sign occurs; this functions converts 0-signs to blanks and reads
36 // a complete line until a cr/lf is found
37
DXFReadLine(SvStream & rIStm,ByteString & rStr)38 sal_Bool DXFReadLine( SvStream& rIStm, ByteString& rStr )
39 {
40 char buf[256 + 1];
41 sal_Bool bEnd = sal_False;
42 sal_uLong nOldFilePos = rIStm.Tell();
43 char c = 0;
44
45 rStr.Erase();
46
47 while( !bEnd && !rIStm.GetError() ) // !!! nicht auf EOF testen,
48 // !!! weil wir blockweise
49 // !!! lesen
50 {
51 sal_uInt16 nLen = (sal_uInt16)rIStm.Read( buf, sizeof(buf)-1 );
52 if( !nLen )
53 {
54 if( rStr.Len() == 0 )
55 return sal_False;
56 else
57 break;
58 }
59
60 for( sal_uInt16 n = 0; n < nLen ; n++ )
61 {
62 c = buf[n];
63 if( c != '\n' && c != '\r' )
64 {
65 if( !c )
66 c = ' ';
67 rStr += c;
68 }
69 else
70 {
71 bEnd = sal_True;
72 break;
73 }
74 }
75 }
76
77 if( !bEnd && !rIStm.GetError() && rStr.Len() )
78 bEnd = sal_True;
79
80 nOldFilePos += rStr.Len();
81 if( rIStm.Tell() > nOldFilePos )
82 nOldFilePos++;
83 rIStm.Seek( nOldFilePos ); // seeken wg. obigem BlockRead!
84
85 if( bEnd && (c=='\r' || c=='\n')) // Sonderbehandlung DOS-Dateien
86 {
87 char cTemp;
88 rIStm.Read((char*)&cTemp , sizeof(cTemp) );
89 if( cTemp == c || (cTemp != '\n' && cTemp != '\r') )
90 rIStm.Seek( nOldFilePos );
91 }
92
93 return bEnd;
94 }
95
96 // ------------------
97
DXFGroupReader(SvStream & rIStream,sal_uInt16 nminpercent,sal_uInt16 nmaxpercent)98 DXFGroupReader::DXFGroupReader(SvStream & rIStream, sal_uInt16 nminpercent, sal_uInt16 nmaxpercent ) :
99 rIS(rIStream)
100 {
101 sal_uInt16 i;
102
103 nIBuffPos=0;
104 nIBuffSize=0;
105 bStatus=sal_True;
106 nLastG=0;
107 nGCount=0;
108
109 nMinPercent=(sal_uLong)nminpercent;
110 nMaxPercent=(sal_uLong)nmaxpercent;
111 nLastPercent=nMinPercent;
112
113 rIS.Seek(STREAM_SEEK_TO_END);
114 nFileSize=rIS.Tell();
115 rIS.Seek(0);
116
117 for (i=0; i<10; i++) S0_9[i][0]=0;
118 S100[ 0 ] = S102[ 0 ] = 0;
119 for (i=0; i<50; i++) F10_59[i]=0.0;
120 for (i=0; i<20; i++) I60_79[i]=0;
121 for (i=0; i<10; i++) I90_99[i]=0;
122 for (i=0; i< 8; i++) F140_147[i]=0.0;
123 for (i=0; i< 6; i++) I170_175[i]=0;
124 for (i=0; i<30; i++) F210_239[i]=0.0;
125 for (i=0; i<11; i++) S999_1009[i][0]=0;
126 for (i=0; i<50; i++) F1010_1059[i]=0.0;
127 for (i=0; i<20; i++) I1060_1079[i]=0;
128
129 }
130
131
Read()132 sal_uInt16 DXFGroupReader::Read()
133 {
134 sal_uInt16 nG = 0;
135 if ( bStatus )
136 {
137 nGCount++;
138 nG = (sal_uInt16)ReadI();
139 if ( bStatus )
140 {
141 char aTmp[ DXF_MAX_STRING_LEN + 1 ];
142
143 if (nG< 10) ReadS(S0_9[nG]);
144 else if (nG< 60) F10_59[nG-10]=ReadF();
145 else if (nG< 80) I60_79[nG-60]=ReadI();
146 else if (nG< 90) ReadS( aTmp );
147 else if (nG< 99) I90_99[nG-90]=ReadI();
148 else if (nG==100) ReadS(S100);
149 else if (nG==102) ReadS(S102);
150 else if (nG==105) ReadS( aTmp );
151 else if (nG< 140) ReadS( aTmp );
152 else if (nG< 148) F140_147[nG-140]=ReadF();
153 else if (nG< 170) ReadS( aTmp );
154 else if (nG< 176) I170_175[nG-175]=ReadI();
155 else if (nG< 180) ReadI();
156 else if (nG< 210) ReadS( aTmp );
157 else if (nG< 240) F210_239[nG-210]=ReadF();
158 else if (nG<=369) ReadS( aTmp );
159 else if (nG< 999) ReadS( aTmp );
160 else if (nG<1010) ReadS(S999_1009[nG-999]);
161 else if (nG<1060) F1010_1059[nG-1010]=ReadF();
162 else if (nG<1080) I1060_1079[nG-1060]=ReadI();
163 else bStatus = sal_False;
164 }
165 }
166 if ( bStatus )
167 nLastG = nG;
168 else
169 {
170 nG = 0;
171 SetS( 0, "EOF" );
172 if ( nGCount != 0xffffffff )
173 {
174 // InfoBox(NULL,String("Fehler ab Gruppe Nr ")+String(nGCount)).Execute();
175 nGCount=0xffffffff;
176 }
177 }
178 nLastG = nG;
179 return nG;
180 }
181
182
GetI(sal_uInt16 nG)183 long DXFGroupReader::GetI(sal_uInt16 nG)
184 {
185 sal_Int32 nRetValue = 0;
186 if ( ( nG >= 60 ) && ( nG <= 79 ) )
187 nRetValue = I60_79[ nG - 60 ];
188 else if ( ( nG >= 90 ) && ( nG <= 99 ) )
189 nRetValue = I90_99[ nG - 90 ];
190 else if ( ( nG >= 170 ) && ( nG <= 175 ) )
191 nRetValue = I170_175[ nG - 170 ];
192 else if ( ( nG >= 1060 ) && ( nG <= 1079 ) )
193 nRetValue = I1060_1079[ nG - 1060 ];
194 return nRetValue;
195 }
196
GetF(sal_uInt16 nG)197 double DXFGroupReader::GetF(sal_uInt16 nG)
198 {
199 nG-=10;
200 if (nG<50) return F10_59[nG];
201 else {
202 nG-=130;
203 if (nG<8) return F140_147[nG];
204 else {
205 nG-=70;
206 if (nG<30) return F210_239[nG];
207 else {
208 nG-=800;
209 if (nG<50) return F1010_1059[nG];
210 else return 0;
211 }
212 }
213 }
214 }
215
GetS(sal_uInt16 nG)216 const char * DXFGroupReader::GetS(sal_uInt16 nG)
217 {
218 if (nG<10) return S0_9[nG];
219 else if ( nG == 100 )
220 return S100;
221 else if ( nG == 102 )
222 return S102;
223 else
224 {
225 nG-=999;
226 if (nG<11) return S999_1009[nG];
227 else return NULL;
228 }
229 }
230
SetF(sal_uInt16 nG,double fF)231 void DXFGroupReader::SetF(sal_uInt16 nG, double fF)
232 {
233 nG-=10;
234 if (nG<50) F10_59[nG]=fF;
235 else {
236 nG-=130;
237 if (nG<8) F140_147[nG]=fF;
238 else {
239 nG-=70;
240 if (nG<30) F210_239[nG]=fF;
241 else {
242 nG-=800;
243 if (nG<50) F1010_1059[nG]=fF;
244 }
245 }
246 }
247 }
248
249
SetS(sal_uInt16 nG,const char * sS)250 void DXFGroupReader::SetS(sal_uInt16 nG, const char * sS)
251 {
252 char* pPtr = NULL;
253 if ( nG < 10 )
254 pPtr = S0_9[ nG ];
255 else if ( nG == 100 )
256 pPtr = S100;
257 else if ( nG == 102 )
258 pPtr = S102;
259 else
260 {
261 nG -= 999;
262 if ( nG < 11 )
263 pPtr = S999_1009[ nG ];
264 }
265 if ( pPtr )
266 strncpy( pPtr, sS, DXF_MAX_STRING_LEN + 1 );
267 }
268
269
ReadLine(char * ptgt)270 void DXFGroupReader::ReadLine(char * ptgt)
271 {
272 ByteString aStr;
273 sal_uLong nLen;
274
275 DXFReadLine( rIS, aStr );
276
277 nLen = aStr.Len();
278 if ( nLen > DXF_MAX_STRING_LEN )
279 nLen = DXF_MAX_STRING_LEN;
280
281 memcpy( ptgt, aStr.GetBuffer(), nLen );
282 ptgt[ nLen ] = 0x00;
283 /*
284 if ( pCallback )
285 {
286 const sal_uLong nPercent= nMinPercent + (nMaxPercent-nMinPercent)*rIS.Tell() / nFileSize;
287
288 if ( nPercent >= nLastPercent + 4 )
289 {
290 nLastPercent=nPercent;
291 if (((*pCallback)(pCallerData,(sal_uInt16)nPercent))==sal_True)
292 bStatus=sal_False;
293 }
294 }
295 */
296 }
297
298
ReadI()299 long DXFGroupReader::ReadI()
300 {
301 char sl[DXF_MAX_STRING_LEN+1],*p;
302
303 ReadLine(sl);
304
305 p=sl;
306
307 while(*p==0x20) p++;
308
309 if ((*p<'0' || *p>'9') && *p!='-') {
310 bStatus=sal_False;
311 return 0;
312 }
313
314 char *start = p;
315 if (*p=='-') {
316 p++;
317 }
318 while (*p>='0' && *p<='9') {
319 p++;
320 }
321
322 char prev = *p;
323 *p = '\0';
324 char *end;
325 long res = strtol(start, &end, 10);
326 *p = prev;
327 if (end != p) {
328 bStatus=sal_False;
329 return 0;
330 }
331
332 while (*p==0x20) p++;
333 if (*p!=0) {
334 bStatus=sal_False;
335 return 0;
336 }
337
338 return res;
339 }
340
341
ReadF()342 double DXFGroupReader::ReadF()
343 {
344 char sl[DXF_MAX_STRING_LEN+1],*p;
345
346 ReadLine(sl);
347 p=sl;
348 while(*p==0x20) p++;
349 if ((*p<'0' || *p>'9') && *p!='.' && *p!='-') {
350 bStatus=sal_False;
351 return 0.0;
352 }
353 return atof(p);
354 }
355
356
ReadS(char * ptgt)357 void DXFGroupReader::ReadS(char * ptgt)
358 {
359 ReadLine(ptgt);
360 }
361
362
363