klimitediodevice.h
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2001, 2002 David Faure <david@mandrakesoft.com> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 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 //kdDebug(7005) << "KLimitedIODevice::KLimitedIODevice start=" << start << " length=" << length << endl; 00045 setType( IO_Direct ); // we support sequential too, but then atEnd() tries getch/ungetch ! 00046 open( IO_ReadOnly ); 00047 } 00048 virtual ~KLimitedIODevice() {} 00049 00050 virtual bool open( int m ) { 00051 //kdDebug(7005) << "KLimitedIODevice::open m=" << m << endl; 00052 if ( m & IO_ReadOnly ) { 00053 /*bool ok = false; 00054 if ( m_dev->isOpen() ) 00055 ok = ( m_dev->mode() == IO_ReadOnly ); 00056 else 00057 ok = m_dev->open( m ); 00058 if ( ok )*/ 00059 m_dev->at( m_start ); // No concurrent access ! 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() ); // Apply upper limit 00075 return m_dev->readBlock( data, maxlen ); 00076 } 00077 virtual Q_LONG writeBlock ( const char *, Q_ULONG ) { return -1; } // unsupported 00078 virtual int putch( int ) { return -1; } // unsupported 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); } // ## apply lower limit ? 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 ); // Apply upper limit 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