1
/* This file is part of the KDE libraries
2
Copyright (C) 2001, 2002, 2007 David Faure <faure@kde.org>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License version 2 as published by the Free Software Foundation.
8
This library is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
Library General Public License for more details.
13
You should have received a copy of the GNU Library General Public License
14
along with this library; see the file COPYING.LIB. If not, write to
15
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16
Boston, MA 02110-1301, USA.
19
#include "klimitediodevice.h"
22
KLimitedIODevice::KLimitedIODevice( QIODevice *dev, int start, int length )
23
: m_dev( dev ), m_start( start ), m_length( length )
25
//kDebug(7005) << "start=" << start << "length=" << length;
26
open( QIODevice::ReadOnly ); //krazy:exclude=syscalls
29
bool KLimitedIODevice::open( QIODevice::OpenMode m )
31
//kDebug(7005) << "m=" << m;
32
if ( m & QIODevice::ReadOnly ) {
34
if ( m_dev->isOpen() )
35
ok = ( m_dev->mode() == QIODevice::ReadOnly );
37
ok = m_dev->open( m );
39
m_dev->seek( m_start ); // No concurrent access !
42
qWarning() << "KLimitedIODevice::open only supports QIODevice::ReadOnly!";
43
setOpenMode( QIODevice::ReadOnly );
47
void KLimitedIODevice::close()
51
qint64 KLimitedIODevice::size() const
56
qint64 KLimitedIODevice::readData( char * data, qint64 maxlen )
58
maxlen = qMin( maxlen, m_length - pos() ); // Apply upper limit
59
return m_dev->read( data, maxlen );
62
bool KLimitedIODevice::seek( qint64 pos )
64
Q_ASSERT( pos <= m_length );
65
pos = qMin( pos, m_length ); // Apply upper limit
66
return m_dev->seek( m_start + pos );
69
qint64 KLimitedIODevice::bytesAvailable() const
71
return m_length + QIODevice::bytesAvailable();
74
bool KLimitedIODevice::isSequential() const
76
return m_dev->isSequential();