File indexing completed on 2024-04-28 15:18:42

0001 /* This file is part of the KDE libraries
0002    SPDX-FileCopyrightText: 2001, 2002, 2007 David Faure <faure@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "klimitediodevice_p.h"
0008 #include "loggingcategory.h"
0009 
0010 KLimitedIODevice::KLimitedIODevice(QIODevice *dev, qint64 start, qint64 length)
0011     : m_dev(dev)
0012     , m_start(start)
0013     , m_length(length)
0014 {
0015     // qCDebug(KArchiveLog) << "start=" << start << "length=" << length;
0016     open(QIODevice::ReadOnly); // krazy:exclude=syscalls
0017 }
0018 
0019 bool KLimitedIODevice::open(QIODevice::OpenMode m)
0020 {
0021     // qCDebug(KArchiveLog) << "m=" << m;
0022     if (m & QIODevice::ReadOnly) {
0023         /*bool ok = false;
0024           if ( m_dev->isOpen() )
0025           ok = ( m_dev->mode() == QIODevice::ReadOnly );
0026           else
0027           ok = m_dev->open( m );
0028           if ( ok )*/
0029         m_dev->seek(m_start); // No concurrent access !
0030     } else {
0031         // qCWarning(KArchiveLog) << "KLimitedIODevice::open only supports QIODevice::ReadOnly!";
0032     }
0033     setOpenMode(QIODevice::ReadOnly);
0034     return true;
0035 }
0036 
0037 void KLimitedIODevice::close()
0038 {
0039 }
0040 
0041 qint64 KLimitedIODevice::size() const
0042 {
0043     return m_length;
0044 }
0045 
0046 qint64 KLimitedIODevice::readData(char *data, qint64 maxlen)
0047 {
0048     maxlen = qMin(maxlen, m_length - pos()); // Apply upper limit
0049     return m_dev->read(data, maxlen);
0050 }
0051 
0052 bool KLimitedIODevice::seek(qint64 pos)
0053 {
0054     Q_ASSERT(pos <= m_length);
0055     pos = qMin(pos, m_length); // Apply upper limit
0056     bool ret = m_dev->seek(m_start + pos);
0057     if (ret) {
0058         QIODevice::seek(pos);
0059     }
0060     return ret;
0061 }
0062 
0063 qint64 KLimitedIODevice::bytesAvailable() const
0064 {
0065     return QIODevice::bytesAvailable();
0066 }
0067 
0068 bool KLimitedIODevice::isSequential() const
0069 {
0070     return m_dev->isSequential();
0071 }
0072 
0073 #include "moc_klimitediodevice_p.cpp"