LibreOffice Module vcl (master) 1
dxfgrprd.cxx
Go to the documentation of this file.
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 <stdlib.h>
21#include <rtl/strbuf.hxx>
22#include <tools/stream.hxx>
23#include <o3tl/string_view.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
30static OString DXFReadLine(SvStream& rIStm)
31{
32 char buf[256 + 1];
33 bool bEnd = false;
34 sal_uInt64 nOldFilePos = rIStm.Tell();
35 char c = 0;
36
37 OStringBuffer aBuf(512);
38
39 while( !bEnd && !rIStm.GetError() ) // !!! do not check for EOF
40 // !!! because we read blockwise
41 {
42 sal_uInt16 nLen = static_cast<sal_uInt16>(rIStm.ReadBytes(buf, sizeof(buf)-1));
43 if( !nLen )
44 {
45 if( aBuf.isEmpty() )
46 return OString();
47 else
48 break;
49 }
50
51 for( sal_uInt16 n = 0; n < nLen ; n++ )
52 {
53 c = buf[n];
54 if( c != '\n' && c != '\r' )
55 {
56 if( !c )
57 c = ' ';
58 aBuf.append(c);
59 }
60 else
61 {
62 bEnd = true;
63 break;
64 }
65 }
66 }
67
68 if( !bEnd && !rIStm.GetError() && !aBuf.isEmpty() )
69 bEnd = true;
70
71 nOldFilePos += aBuf.getLength();
72 if( rIStm.Tell() > nOldFilePos )
73 nOldFilePos++;
74 rIStm.Seek( nOldFilePos ); // seek because of BlockRead above!
75
76 if( bEnd && (c=='\r' || c=='\n')) // special treatment of DOS files
77 {
78 char cTemp(0);
79 rIStm.ReadBytes(&cTemp, 1);
80 if( cTemp == c || (cTemp != '\n' && cTemp != '\r') )
81 rIStm.Seek( nOldFilePos );
82 }
83
84 return aBuf.makeStringAndClear();
85}
86
87static void DXFSkipLine(SvStream& rIStm)
88{
89 while (rIStm.good())
90 {
91 char buf[256 + 1];
92 sal_uInt16 nLen = static_cast<sal_uInt16>(rIStm.ReadBytes(buf, sizeof(buf) - 1));
93 for (sal_uInt16 n = 0; n < nLen; n++)
94 {
95 char c = buf[n];
96 if ((c == '\n') || (c == '\r'))
97 {
98 rIStm.SeekRel(n-nLen+1); // return stream to next to current position
99 char c1 = 0;
100 rIStm.ReadBytes(&c1, 1);
101 if (c1 == c || (c1 != '\n' && c1!= '\r'))
102 rIStm.SeekRel(-1);
103 return;
104 }
105 }
106 }
107}
108
110 : rIS(rIStream)
111 , bStatus(true)
112 , nLastG(0)
113 , I(0)
114{
115 rIS.Seek(0);
116}
117
119{
120 sal_uInt16 nG = 0;
121 if ( bStatus )
122 {
123 nG = static_cast<sal_uInt16>(ReadI());
124 if ( bStatus )
125 {
126 if (nG< 10) ReadS();
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;
147 }
148 }
149 if ( !bStatus )
150 {
151 nG = 0;
152 S = "EOF";
153 }
154 nLastG = nG;
155 return nG;
156}
157
159{
160 OString s = DXFReadLine(rIS);
161 char *p=s.pData->buffer;
162 const char *end = s.pData->buffer + s.pData->length;
163
164 while((p != end) && (*p==0x20)) p++;
165
166 if ((p == end) || ((*p<'0' || *p>'9') && *p!='-')) {
167 bStatus=false;
168 return 0;
169 }
170
171 OStringBuffer aNumber;
172 if (*p == '-') {
173 aNumber.append(*p++);
174 }
175
176 while ((p != end) && *p >= '0' && *p <= '9') {
177 aNumber.append(*p++);
178 }
179
180 while ((p != end) && (*p==0x20)) p++;
181 if (p != end) {
182 bStatus=false;
183 return 0;
184 }
185
186 return o3tl::toInt32(aNumber);
187}
188
190{
191 OString s = DXFReadLine(rIS);
192 char *p = s.pData->buffer;
193 const char *end = s.pData->buffer + s.pData->length;
194
195 while((p != end) && (*p==0x20)) p++;
196 if ((p == end) || ((*p<'0' || *p>'9') && *p!='.' && *p!='-')) {
197 bStatus=false;
198 return 0.0;
199 }
200 return atof(p);
201}
202
204{
205 S = DXFReadLine(rIS);
206}
207
209{
210 return rIS.remainingSize();
211}
212
213/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt16 nLastG
Definition: dxfgrprd.hxx:70
tools::Long ReadI()
Definition: dxfgrprd.cxx:158
tools::Long I
Definition: dxfgrprd.hxx:75
sal_uInt16 Read()
Definition: dxfgrprd.cxx:118
sal_uInt64 remainingSize() const
Definition: dxfgrprd.cxx:208
DXFGroupReader(SvStream &rIStream)
Definition: dxfgrprd.cxx:109
SvStream & rIS
Definition: dxfgrprd.hxx:68
double ReadF()
Definition: dxfgrprd.cxx:189
sal_uInt64 Tell() const
bool good() const
sal_uInt64 Seek(sal_uInt64 nPos)
std::size_t ReadBytes(void *pData, std::size_t nSize)
sal_uInt64 SeekRel(sal_Int64 nPos)
ErrCode GetError() const
sal_uInt64 remainingSize()
static void DXFSkipLine(SvStream &rIStm)
Definition: dxfgrprd.cxx:87
static OString DXFReadLine(SvStream &rIStm)
Definition: dxfgrprd.cxx:30
void * p
sal_Int64 n
aBuf
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
end
long Long