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 <tqiodevice.h> 00031 class KIO_EXPORT KLimitedIODevice : public TQIODevice 00032 { 00033 public: 00041 KLimitedIODevice( TQIODevice *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( TQ_OpenMode 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 #ifdef USE_QT4 00071 virtual qint64 size() const { return m_length; } 00072 #else // USE_QT4 00073 virtual Offset size() const { return m_length; } 00074 #endif // USE_QT4 00075 00076 virtual TQT_TQIO_LONG tqreadBlock ( char * data, TQT_TQIO_ULONG maxlen ) 00077 { 00078 maxlen = TQMIN( maxlen, m_length - at() ); // Apply upper limit 00079 return m_dev->readBlock( data, maxlen ); 00080 } 00081 virtual TQT_TQIO_LONG tqwriteBlock ( const char *, TQT_TQIO_ULONG ) { return -1; } // unsupported 00082 virtual int putch( int ) { return -1; } // unsupported 00083 00084 virtual int getch() { 00085 char c[2]; 00086 if ( tqreadBlock(c, 1) == -1) 00087 return -1; 00088 else 00089 return c[0]; 00090 } 00091 virtual int ungetch( int c ) { return m_dev->ungetch(c); } // ## apply lower limit ? 00092 virtual Offset at() const { return m_dev->at() - m_start; } 00093 virtual bool at( Offset pos ) { 00094 Q_ASSERT( pos <= m_length ); 00095 pos = QMIN( pos, m_length ); // Apply upper limit 00096 return m_dev->at( m_start + pos ); 00097 } 00098 virtual bool atEnd() const { return m_dev->atEnd() || m_dev->at() >= m_start + m_length; } 00099 private: 00100 TQIODevice* m_dev; 00101 TQ_ULONG m_start; 00102 TQ_ULONG m_length; 00103 }; 00104 00105 #endif