kio
klimitediodevice.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef klimitediodevice_h
00020 #define klimitediodevice_h
00021
00022 #include <kdebug.h>
00023 #include <qiodevice.h>
00031 class KIO_EXPORT KLimitedIODevice : public QIODevice
00032 {
00033 public:
00041 KLimitedIODevice( QIODevice *dev, int start, int length )
00042 : m_dev( dev ), m_start( start ), m_length( length )
00043 {
00044
00045 setType( IO_Direct );
00046 open( IO_ReadOnly );
00047 }
00048 virtual ~KLimitedIODevice() {}
00049
00050 virtual bool open( int m ) {
00051
00052 if ( m & IO_ReadOnly ) {
00053
00054
00055
00056
00057
00058
00059 m_dev->at( m_start );
00060 }
00061 else
00062 kdWarning(7005) << "KLimitedIODevice::open only supports IO_ReadOnly!" << endl;
00063 setState( IO_Open );
00064 setMode( m );
00065 return true;
00066 }
00067 virtual void close() {}
00068 virtual void flush() {}
00069
00070 virtual Offset size() const { return m_length; }
00071
00072 virtual Q_LONG readBlock ( char * data, Q_ULONG maxlen )
00073 {
00074 maxlen = QMIN( maxlen, m_length - at() );
00075 return m_dev->readBlock( data, maxlen );
00076 }
00077 virtual Q_LONG writeBlock ( const char *, Q_ULONG ) { return -1; }
00078 virtual int putch( int ) { return -1; }
00079
00080 virtual int getch() {
00081 char c[2];
00082 if ( readBlock(c, 1) == -1)
00083 return -1;
00084 else
00085 return c[0];
00086 }
00087 virtual int ungetch( int c ) { return m_dev->ungetch(c); }
00088 virtual Offset at() const { return m_dev->at() - m_start; }
00089 virtual bool at( Offset pos ) {
00090 Q_ASSERT( pos <= m_length );
00091 pos = QMIN( pos, m_length );
00092 return m_dev->at( m_start + pos );
00093 }
00094 virtual bool atEnd() const { return m_dev->atEnd() || m_dev->at() >= m_start + m_length; }
00095 private:
00096 QIODevice* m_dev;
00097 Q_ULONG m_start;
00098 Q_ULONG m_length;
00099 };
00100
00101 #endif