1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <rtl/strbuf.hxx>
22 #include <tools/stream.hxx>
23 #include <o3tl/string_view.hxx>
24 #include "dxfgrprd.hxx"
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
30 static OString
DXFReadLine(SvStream
& rIStm
)
34 sal_uInt64 nOldFilePos
= rIStm
.Tell();
37 OStringBuffer
aBuf(512);
39 while( !bEnd
&& !rIStm
.GetError() ) // !!! do not check for EOF
40 // !!! because we read blockwise
42 sal_uInt16 nLen
= static_cast<sal_uInt16
>(rIStm
.ReadBytes(buf
, sizeof(buf
)-1));
51 for( sal_uInt16 n
= 0; n
< nLen
; n
++ )
54 if( c
!= '\n' && c
!= '\r' )
68 if( !bEnd
&& !rIStm
.GetError() && !aBuf
.isEmpty() )
71 nOldFilePos
+= aBuf
.getLength();
72 if( rIStm
.Tell() > nOldFilePos
)
74 rIStm
.Seek( nOldFilePos
); // seek because of BlockRead above!
76 if( bEnd
&& (c
=='\r' || c
=='\n')) // special treatment of DOS files
79 rIStm
.ReadBytes(&cTemp
, 1);
80 if( cTemp
== c
|| (cTemp
!= '\n' && cTemp
!= '\r') )
81 rIStm
.Seek( nOldFilePos
);
84 return aBuf
.makeStringAndClear();
87 static void DXFSkipLine(SvStream
& rIStm
)
92 sal_uInt16 nLen
= static_cast<sal_uInt16
>(rIStm
.ReadBytes(buf
, sizeof(buf
) - 1));
93 for (sal_uInt16 n
= 0; n
< nLen
; n
++)
96 if ((c
== '\n') || (c
== '\r'))
98 rIStm
.SeekRel(n
-nLen
+1); // return stream to next to current position
100 rIStm
.ReadBytes(&c1
, 1);
101 if (c1
== c
|| (c1
!= '\n' && c1
!= '\r'))
109 DXFGroupReader::DXFGroupReader(SvStream
& rIStream
)
118 sal_uInt16
DXFGroupReader::Read()
123 nG
= static_cast<sal_uInt16
>(ReadI());
127 else if (nG
< 60) F
= ReadF();
128 else if (nG
< 80) I
= ReadI();
129 else if (nG
< 90) DXFSkipLine(rIS
);
130 else if (nG
< 99) I
= ReadI();
131 else if (nG
==100) ReadS();
132 else if (nG
==102) ReadS();
133 else if (nG
==105) DXFSkipLine(rIS
);
134 else if (nG
< 140) DXFSkipLine(rIS
);
135 else if (nG
< 148) F
= ReadF();
136 else if (nG
< 170) DXFSkipLine(rIS
);
137 else if (nG
< 176) I
= ReadI();
138 else if (nG
< 180) DXFSkipLine(rIS
); // ReadI();
139 else if (nG
< 210) DXFSkipLine(rIS
);
140 else if (nG
< 240) F
= ReadF();
141 else if (nG
<=369) DXFSkipLine(rIS
);
142 else if (nG
< 999) DXFSkipLine(rIS
);
143 else if (nG
<1010) ReadS();
144 else if (nG
<1060) F
= ReadF();
145 else if (nG
<1072) I
= ReadI();
146 else bStatus
= false;
158 tools::Long
DXFGroupReader::ReadI()
160 OString s
= DXFReadLine(rIS
);
161 char *p
=s
.pData
->buffer
;
162 const char *end
= s
.pData
->buffer
+ s
.pData
->length
;
164 while((p
!= end
) && (*p
==0x20)) p
++;
166 if ((p
== end
) || ((*p
<'0' || *p
>'9') && *p
!='-')) {
171 OStringBuffer aNumber
;
173 aNumber
.append(*p
++);
176 while ((p
!= end
) && *p
>= '0' && *p
<= '9') {
177 aNumber
.append(*p
++);
180 while ((p
!= end
) && (*p
==0x20)) p
++;
186 return o3tl::toInt32(aNumber
);
189 double DXFGroupReader::ReadF()
191 OString s
= DXFReadLine(rIS
);
192 char *p
= s
.pData
->buffer
;
193 const char *end
= s
.pData
->buffer
+ s
.pData
->length
;
195 while((p
!= end
) && (*p
==0x20)) p
++;
196 if ((p
== end
) || ((*p
<'0' || *p
>'9') && *p
!='.' && *p
!='-')) {
203 void DXFGroupReader::ReadS()
205 S
= DXFReadLine(rIS
);
208 sal_uInt64
DXFGroupReader::remainingSize() const
210 return rIS
.remainingSize();
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */