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 "dxfgrprd.hxx"
24 
25 // we use an own ReadLine function, because Stream::ReadLine stops if
26 // a 0-sign occurs; this function converts 0-signs to blanks and reads
27 // a complete line until a cr/lf is found
28 
29 static OString DXFReadLine(SvStream& rIStm)
30 {
31  char buf[256 + 1];
32  bool bEnd = false;
33  sal_uInt64 nOldFilePos = rIStm.Tell();
34  char c = 0;
35 
36  OStringBuffer aBuf(512);
37 
38  while( !bEnd && !rIStm.GetError() ) // !!! do not check for EOF
39  // !!! because we read blockwise
40  {
41  sal_uInt16 nLen = static_cast<sal_uInt16>(rIStm.ReadBytes(buf, sizeof(buf)-1));
42  if( !nLen )
43  {
44  if( aBuf.isEmpty() )
45  return OString();
46  else
47  break;
48  }
49 
50  for( sal_uInt16 n = 0; n < nLen ; n++ )
51  {
52  c = buf[n];
53  if( c != '\n' && c != '\r' )
54  {
55  if( !c )
56  c = ' ';
57  aBuf.append(c);
58  }
59  else
60  {
61  bEnd = true;
62  break;
63  }
64  }
65  }
66 
67  if( !bEnd && !rIStm.GetError() && !aBuf.isEmpty() )
68  bEnd = true;
69 
70  nOldFilePos += aBuf.getLength();
71  if( rIStm.Tell() > nOldFilePos )
72  nOldFilePos++;
73  rIStm.Seek( nOldFilePos ); // seek because of BlockRead above!
74 
75  if( bEnd && (c=='\r' || c=='\n')) // special treatment of DOS files
76  {
77  char cTemp(0);
78  rIStm.ReadBytes(&cTemp, 1);
79  if( cTemp == c || (cTemp != '\n' && cTemp != '\r') )
80  rIStm.Seek( nOldFilePos );
81  }
82 
83  return aBuf.makeStringAndClear();
84 }
85 
86 static void DXFSkipLine(SvStream& rIStm)
87 {
88  while (rIStm.good())
89  {
90  char buf[256 + 1];
91  sal_uInt16 nLen = static_cast<sal_uInt16>(rIStm.ReadBytes(buf, sizeof(buf) - 1));
92  for (sal_uInt16 n = 0; n < nLen; n++)
93  {
94  char c = buf[n];
95  if ((c == '\n') || (c == '\r'))
96  {
97  rIStm.SeekRel(n-nLen+1); // return stream to next to current position
98  char c1 = 0;
99  rIStm.ReadBytes(&c1, 1);
100  if (c1 == c || (c1 != '\n' && c1!= '\r'))
101  rIStm.SeekRel(-1);
102  return;
103  }
104  }
105  }
106 }
107 
109  : rIS(rIStream)
110  , bStatus(true)
111  , nLastG(0)
112  , S()
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 aNumber.toString().toInt32();
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: */
long Long
sal_Int64 n
aBuf
sal_uInt64 Seek(sal_uInt64 nPos)
sal_uInt64 SeekRel(sal_Int64 nPos)
ErrCode GetError() const
sal_uInt16 Read()
Definition: dxfgrprd.cxx:118
sal_uInt64 remainingSize()
sal_uInt16 nLastG
Definition: dxfgrprd.hxx:71
tools::Long I
Definition: dxfgrprd.hxx:76
DXFGroupReader(SvStream &rIStream)
Definition: dxfgrprd.cxx:108
SvStream & rIS
Definition: dxfgrprd.hxx:69
static void DXFSkipLine(SvStream &rIStm)
Definition: dxfgrprd.cxx:86
double ReadF()
Definition: dxfgrprd.cxx:189
static OString DXFReadLine(SvStream &rIStm)
Definition: dxfgrprd.cxx:29
enumrange< T >::Iterator end(enumrange< T >)
std::size_t ReadBytes(void *pData, std::size_t nSize)
sal_uInt64 Tell() const
void * p
bool good() const
sal_uInt64 remainingSize() const
Definition: dxfgrprd.cxx:208
tools::Long ReadI()
Definition: dxfgrprd.cxx:158