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  , I(0)
113 {
114  rIS.Seek(0);
115 }
116 
118 {
119  sal_uInt16 nG = 0;
120  if ( bStatus )
121  {
122  nG = static_cast<sal_uInt16>(ReadI());
123  if ( bStatus )
124  {
125  if (nG< 10) ReadS();
126  else if (nG< 60) F = ReadF();
127  else if (nG< 80) I = ReadI();
128  else if (nG< 90) DXFSkipLine(rIS);
129  else if (nG< 99) I = ReadI();
130  else if (nG==100) ReadS();
131  else if (nG==102) ReadS();
132  else if (nG==105) DXFSkipLine(rIS);
133  else if (nG< 140) DXFSkipLine(rIS);
134  else if (nG< 148) F = ReadF();
135  else if (nG< 170) DXFSkipLine(rIS);
136  else if (nG< 176) I = ReadI();
137  else if (nG< 180) DXFSkipLine(rIS); // ReadI();
138  else if (nG< 210) DXFSkipLine(rIS);
139  else if (nG< 240) F = ReadF();
140  else if (nG<=369) DXFSkipLine(rIS);
141  else if (nG< 999) DXFSkipLine(rIS);
142  else if (nG<1010) ReadS();
143  else if (nG<1060) F = ReadF();
144  else if (nG<1072) I = ReadI();
145  else bStatus = false;
146  }
147  }
148  if ( !bStatus )
149  {
150  nG = 0;
151  S = "EOF";
152  }
153  nLastG = nG;
154  return nG;
155 }
156 
158 {
159  OString s = DXFReadLine(rIS);
160  char *p=s.pData->buffer;
161  const char *end = s.pData->buffer + s.pData->length;
162 
163  while((p != end) && (*p==0x20)) p++;
164 
165  if ((p == end) || ((*p<'0' || *p>'9') && *p!='-')) {
166  bStatus=false;
167  return 0;
168  }
169 
170  OStringBuffer aNumber;
171  if (*p == '-') {
172  aNumber.append(*p++);
173  }
174 
175  while ((p != end) && *p >= '0' && *p <= '9') {
176  aNumber.append(*p++);
177  }
178 
179  while ((p != end) && (*p==0x20)) p++;
180  if (p != end) {
181  bStatus=false;
182  return 0;
183  }
184 
185  return aNumber.toString().toInt32();
186 }
187 
189 {
190  OString s = DXFReadLine(rIS);
191  char *p = s.pData->buffer;
192  const char *end = s.pData->buffer + s.pData->length;
193 
194  while((p != end) && (*p==0x20)) p++;
195  if ((p == end) || ((*p<'0' || *p>'9') && *p!='.' && *p!='-')) {
196  bStatus=false;
197  return 0.0;
198  }
199  return atof(p);
200 }
201 
203 {
204  S = DXFReadLine(rIS);
205 }
206 
208 {
209  return rIS.remainingSize();
210 }
211 
212 /* 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:117
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:188
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:207
tools::Long ReadI()
Definition: dxfgrprd.cxx:157