filters

stream.cpp
1#include <qpro/common.h>
2
3
4#include "qpro/stream.h"
5
6#ifdef __DECCXX
7#include <fstream.h> // needed for filebuf
8#endif
9#include <string.h>
10
11#ifdef USE_QT
12
13#include <tqbuffer.h>
14
15QpIStream::QpIStream(unsigned char* pBuffer, unsigned int pLen)
16 : cBuffer(pBuffer), cLen(pLen)
17{
18 cByteArray.setRawData( (char*)cBuffer, (int) cLen );
19
20 cBuf.setBuffer( cByteArray );
21 cBuf.open( IO_ReadOnly );
22
23 setDevice( &cBuf );
24 setByteOrder(TQDataStream::LittleEndian);
25}
26
27QpIStream::~QpIStream()
28{
29 cByteArray.resetRawData( (char*)cBuffer, (int) cLen );
30}
31
32#else
33#include <string>
34#include <fstream>
35#include <sstream>
36
37using namespace std;
38
39QpIStream::QpIStream(const char* pFileName)
40 : cIn(0)
41 , cOffset(0L)
42 , cStreamBuf(0)
43{
44 filebuf* lFileBuf = new filebuf;
45
46 cStreamBuf = lFileBuf;
47
48 lFileBuf->open(pFileName, ios::in);
49
50 if( lFileBuf->is_open())
51 {
52 cIn = new istream(cStreamBuf);
53 }
54}
55
56QpIStream::QpIStream(unsigned char* pBuffer, unsigned int pLen)
57 : cIn(0)
58 , cOffset(0L)
59 , cStreamBuf(0)
60{
61 cStreamBuf = new std::stringbuf ((char*)(pBuffer)); //cIn will read into char anyway....
62
63 cIn = new istream(cStreamBuf);
64}
65
66QpIStream::~QpIStream()
67{
68 delete cIn;
69 cIn = 0;
70
71 delete cStreamBuf;
72 cStreamBuf = 0;
73}
74
75int
76QpIStream::get()
77{
78 int lResult;
79
80 if( (cIn==0) || cIn->rdstate())
81 {
82 lResult = EOF;
83 }
84 else
85 if((lResult=cIn->get()) == EOF)
86 {
87 // note - clear() sets bits! not clears them
88 cIn->clear(ios::eofbit|ios::failbit);
89 }
90 else
91 {
92 ++cOffset;
93 }
94
95 return lResult;
96}
97
98QpIStream&
99QpIStream::read(char* pBuf, TQP_INT16 pLen)
100{
101 if( cIn )
102 {
103 cIn->read(pBuf, pLen);
104 }
105
106 return *this;
107}
108
109QpIStream::operator void* ()
110{
111 if( cIn == 0 )
112 return 0;
113 else
114 return cIn;
115}
116
117int
118QpIStream::operator !()
119{
120 return ( cIn ? !*cIn : -1 );
121}
122
123
124QpIStream&
125QpIStream::operator >> (TQP_INT8 &pI8)
126{
127 pI8 = get();
128
129 return *this;
130}
131
132QpIStream&
133QpIStream::operator >> (TQP_UINT8 &pI8)
134{
135 pI8 = get();
136
137 return *this;
138}
139
140QpIStream&
141QpIStream::operator >> (TQP_INT16 &pI16)
142{
143 pI16 = get();
144 pI16 = pI16 | (get() << 8);
145
146 return *this;
147}
148
149QpIStream&
150QpIStream::operator >> (TQP_INT32 &pI32)
151{
152 pI32 = get();
153 pI32 = pI32 | (get() << 8);
154 pI32 = pI32 | (get() << 16);
155 pI32 = pI32 | (get() << 24);
156
157 return *this;
158}
159
160QpIStream&
161QpIStream::operator >> (TQP_INT64 &pI64)
162{
163// ??? sort out this
164/******
165 pI64 = get();
166 pI64 = pI64 | (get() << 8);
167 pI64 = pI64 | (get() << 16);
168 pI64 = pI64 | (get() << 24);
169 pI64 = pI64 | (get() << 32);
170 pI64 = pI64 | (get() << 40);
171 pI64 = pI64 | (get() << 48);
172 pI64 = pI64 | (get() << 56);
173***/
174
175 union
176 {
177 char lChar[8];
178 double lDble;
179 };
180
181 lDble = 0.0; // HACK: prevent gcc internal compiler error
182 lChar[0] = get();
183 lChar[1] = get();
184 lChar[2] = get();
185 lChar[3] = get();
186 lChar[4] = get();
187 lChar[5] = get();
188 lChar[6] = get();
189 lChar[7] = get();
190
191 pI64 = lDble;
192
193 return *this;
194}
195
196QpIStream&
197QpIStream::operator >> (char*& pStr)
198{
199 int lIdx=0;
200 int lMax=10;
201
202 char* lStr = new char[lMax];
203
204 while( cIn->get(lStr[lIdx]), lStr[lIdx] != '\0' && cIn->good() )
205 {
206 if( ++lIdx == lMax )
207 {
208 lMax += 10;
209 char* lNew = new char[lMax];
210
211 memcpy(lNew, lStr, lIdx);
212 delete [] lStr;
213 lStr = lNew;
214 }
215 }
216
217 pStr = lStr;
218
219 return *this;
220}
221
222#endif // USE_QT
223