kio Library API Documentation

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., 59 Temple Place - Suite 330,
00016    Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #ifndef klimitediodevice_h
00020 #define klimitediodevice_h
00021 
00029 class KLimitedIODevice : public QIODevice
00030 {
00031 public:
00039     KLimitedIODevice( QIODevice *dev, int start, int length )
00040         : m_dev( dev ), m_start( start ), m_length( length )
00041     {
00042         //kdDebug(7005) << "KLimitedIODevice::KLimitedIODevice start=" << start << " length=" << length << endl;
00043         setType( IO_Direct ); // we support sequential too, but then atEnd() tries getch/ungetch !
00044         open( IO_ReadOnly );
00045     }
00046     virtual ~KLimitedIODevice() {}
00047 
00048     virtual bool open( int m ) {
00049         //kdDebug(7005) << "KLimitedIODevice::open m=" << m << endl;
00050         if ( m & IO_ReadOnly ) {
00051             /*bool ok = false;
00052             if ( m_dev->isOpen() )
00053                 ok = ( m_dev->mode() == IO_ReadOnly );
00054             else
00055                 ok = m_dev->open( m );
00056             if ( ok )*/
00057                 m_dev->at( m_start ); // No concurrent access !
00058         }
00059         else
00060             kdWarning(7005) << "KLimitedIODevice::open only supports IO_ReadOnly!" << endl;
00061         setState( IO_Open );
00062         setMode( m );
00063         return true;
00064     }
00065     virtual void close() {}
00066     virtual void flush() {}
00067 
00068     virtual Offset size() const { return m_length; }
00069 
00070     virtual Q_LONG readBlock ( char * data, Q_ULONG maxlen )
00071     {
00072         maxlen = QMIN( maxlen, m_length - at() ); // Apply upper limit
00073         return m_dev->readBlock( data, maxlen );
00074     }
00075     virtual Q_LONG writeBlock ( const char *, Q_ULONG ) { return -1; } // unsupported
00076     virtual int putch( int ) { return -1; } // unsupported
00077 
00078     virtual int getch() {
00079         char c[2];
00080         if ( readBlock(c, 1) == -1)
00081             return -1;
00082         else
00083             return c[0];
00084     }
00085     virtual int ungetch( int c ) { return m_dev->ungetch(c); } // ## apply lower limit ?
00086     virtual Offset at() const { return m_dev->at() - m_start; }
00087     virtual bool at( Offset pos ) {
00088         Q_ASSERT( pos <= m_length );
00089         pos = QMIN( pos, m_length ); // Apply upper limit
00090         return m_dev->at( m_start + pos );
00091     }
00092     virtual bool atEnd() const { return m_dev->at() >= m_start + m_length; }
00093 private:
00094     QIODevice* m_dev;
00095     Q_ULONG m_start;
00096     Q_ULONG m_length;
00097 };
00098 
00099 #endif
KDE Logo
This file is part of the documentation for kio Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun May 16 22:03:16 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003