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