tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / vcl / source / filter / idxf / dxfgrprd.cxx
blob3319882e32459ce9811f370d585f8d9804e11d2f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <stdlib.h>
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)
32 char buf[256 + 1];
33 bool bEnd = false;
34 sal_uInt64 nOldFilePos = rIStm.Tell();
35 char c = 0;
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));
43 if( !nLen )
45 if( aBuf.isEmpty() )
46 return OString();
47 else
48 break;
51 for( sal_uInt16 n = 0; n < nLen ; n++ )
53 c = buf[n];
54 if( c != '\n' && c != '\r' )
56 if( !c )
57 c = ' ';
58 aBuf.append(c);
60 else
62 bEnd = true;
63 break;
68 if( !bEnd && !rIStm.GetError() && !aBuf.isEmpty() )
69 bEnd = true;
71 nOldFilePos += aBuf.getLength();
72 if( rIStm.Tell() > nOldFilePos )
73 nOldFilePos++;
74 rIStm.Seek( nOldFilePos ); // seek because of BlockRead above!
76 if( bEnd && (c=='\r' || c=='\n')) // special treatment of DOS files
78 char cTemp(0);
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)
89 while (rIStm.good())
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++)
95 char c = buf[n];
96 if ((c == '\n') || (c == '\r'))
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;
109 DXFGroupReader::DXFGroupReader(SvStream & rIStream)
110 : rIS(rIStream)
111 , bStatus(true)
112 , nLastG(0)
113 , I(0)
115 rIS.Seek(0);
118 sal_uInt16 DXFGroupReader::Read()
120 sal_uInt16 nG = 0;
121 if ( bStatus )
123 nG = static_cast<sal_uInt16>(ReadI());
124 if ( bStatus )
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;
149 if ( !bStatus )
151 nG = 0;
152 S = "EOF"_ostr;
154 nLastG = nG;
155 return nG;
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!='-')) {
167 bStatus=false;
168 return 0;
171 OStringBuffer aNumber;
172 if (*p == '-') {
173 aNumber.append(*p++);
176 while ((p != end) && *p >= '0' && *p <= '9') {
177 aNumber.append(*p++);
180 while ((p != end) && (*p==0x20)) p++;
181 if (p != end) {
182 bStatus=false;
183 return 0;
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!='-')) {
197 bStatus=false;
198 return 0.0;
200 return atof(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: */