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