filters
stream.cc00001 #include <qpro/common.h>
00002
00003
00004 #include "qpro/stream.h"
00005
00006 #ifdef __DECCXX
00007 #include <fstream.h>
00008 #endif
00009 #include <string.h>
00010
00011 #ifdef USE_QT
00012
00013 #include <qbuffer.h>
00014
00015 QpIStream::QpIStream(unsigned char* pBuffer, unsigned int pLen)
00016 : cBuffer(pBuffer), cLen(pLen)
00017 {
00018 cByteArray.setRawData( (char*)cBuffer, (int) cLen );
00019
00020 cBuf.setBuffer( cByteArray );
00021 cBuf.open( IO_ReadOnly );
00022
00023 setDevice( &cBuf );
00024 setByteOrder(QDataStream::LittleEndian);
00025 }
00026
00027 QpIStream::~QpIStream()
00028 {
00029 cByteArray.resetRawData( (char*)cBuffer, (int) cLen );
00030 }
00031
00032 #else
00033 #include <string>
00034 #include <fstream>
00035 #include <strstream>
00036
00037
00038 namespace std {}
00039
00040 using namespace std;
00041
00042 QpIStream::QpIStream(const char* pFileName)
00043 : cIn(0)
00044 , cOffset(0L)
00045 , cStreamBuf(0)
00046 {
00047 filebuf* lFileBuf = new filebuf;
00048
00049 cStreamBuf = lFileBuf;
00050
00051 lFileBuf->open(pFileName, ios::in);
00052
00053 if( lFileBuf->is_open())
00054 {
00055 cIn = new istream(cStreamBuf);
00056 }
00057 }
00058
00059 QpIStream::QpIStream(unsigned char* pBuffer, unsigned int pLen)
00060 : cIn(0)
00061 , cOffset(0L)
00062 , cStreamBuf(0)
00063 {
00064 cStreamBuf = new std::strstreambuf (pBuffer, pLen);
00065
00066 cIn = new istream(cStreamBuf);
00067 }
00068
00069 QpIStream::~QpIStream()
00070 {
00071 delete cIn;
00072 cIn = 0;
00073
00074 delete cStreamBuf;
00075 cStreamBuf = 0;
00076 }
00077
00078 int
00079 QpIStream::get()
00080 {
00081 int lResult;
00082
00083 if( (cIn==0) || cIn->rdstate())
00084 {
00085 lResult = EOF;
00086 }
00087 else
00088 if((lResult=cIn->get()) == EOF)
00089 {
00090
00091 cIn->clear(ios::eofbit|ios::failbit);
00092 }
00093 else
00094 {
00095 ++cOffset;
00096 }
00097
00098 return lResult;
00099 }
00100
00101 QpIStream&
00102 QpIStream::read(char* pBuf, QP_INT16 pLen)
00103 {
00104 if( cIn )
00105 {
00106 cIn->read(pBuf, pLen);
00107 }
00108
00109 return *this;
00110 }
00111
00112 QpIStream::operator void* ()
00113 {
00114 if( cIn == 0 )
00115 return 0;
00116 else
00117 return *cIn;
00118 }
00119
00120 int
00121 QpIStream::operator !()
00122 {
00123 return ( cIn ? !*cIn : -1 );
00124 }
00125
00126
00127 QpIStream&
00128 QpIStream::operator >> (QP_INT8 &pI8)
00129 {
00130 pI8 = get();
00131
00132 return *this;
00133 }
00134
00135 QpIStream&
00136 QpIStream::operator >> (QP_UINT8 &pI8)
00137 {
00138 pI8 = get();
00139
00140 return *this;
00141 }
00142
00143 QpIStream&
00144 QpIStream::operator >> (QP_INT16 &pI16)
00145 {
00146 pI16 = get();
00147 pI16 = pI16 | (get() << 8);
00148
00149 return *this;
00150 }
00151
00152 QpIStream&
00153 QpIStream::operator >> (QP_INT32 &pI32)
00154 {
00155 pI32 = get();
00156 pI32 = pI32 | (get() << 8);
00157 pI32 = pI32 | (get() << 16);
00158 pI32 = pI32 | (get() << 24);
00159
00160 return *this;
00161 }
00162
00163 QpIStream&
00164 QpIStream::operator >> (QP_INT64 &pI64)
00165 {
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 union
00179 {
00180 char lChar[8];
00181 double lDble;
00182 };
00183
00184 lDble = 0.0;
00185 lChar[0] = get();
00186 lChar[1] = get();
00187 lChar[2] = get();
00188 lChar[3] = get();
00189 lChar[4] = get();
00190 lChar[5] = get();
00191 lChar[6] = get();
00192 lChar[7] = get();
00193
00194 pI64 = lDble;
00195
00196 return *this;
00197 }
00198
00199 QpIStream&
00200 QpIStream::operator >> (char*& pStr)
00201 {
00202 int lIdx=0;
00203 int lMax=10;
00204
00205 char* lStr = new char[lMax];
00206
00207 while( cIn->get(lStr[lIdx]), lStr[lIdx] != '\0' && cIn->good() )
00208 {
00209 if( ++lIdx == lMax )
00210 {
00211 lMax += 10;
00212 char* lNew = new char[lMax];
00213
00214 memcpy(lNew, lStr, lIdx);
00215 delete [] lStr;
00216 lStr = lNew;
00217 }
00218 }
00219
00220 pStr = lStr;
00221
00222 return *this;
00223 }
00224
00225 #endif // USE_QT
00226
|