1/* This file is part of the KDE libraries
2 SPDX-FileCopyrightText: 2001, 2002, 2007 David Faure <faure@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "klimitediodevice_p.h"
8#include "loggingcategory.h"
9
10KLimitedIODevice::KLimitedIODevice(QIODevice *dev, qint64 start, qint64 length)
11 : m_dev(dev)
12 , m_start(start)
13 , m_length(length)
14{
15 // qCDebug(KArchiveLog) << "start=" << start << "length=" << length;
16 open(m: QIODevice::ReadOnly); // krazy:exclude=syscalls
17}
18
19bool KLimitedIODevice::open(QIODevice::OpenMode m)
20{
21 // qCDebug(KArchiveLog) << "m=" << m;
22 if (m & QIODevice::ReadOnly) {
23 /*bool ok = false;
24 if ( m_dev->isOpen() )
25 ok = ( m_dev->mode() == QIODevice::ReadOnly );
26 else
27 ok = m_dev->open( m );
28 if ( ok )*/
29 m_dev->seek(pos: m_start); // No concurrent access !
30 } else {
31 // qCWarning(KArchiveLog) << "KLimitedIODevice::open only supports QIODevice::ReadOnly!";
32 }
33 setOpenMode(QIODevice::ReadOnly);
34 return true;
35}
36
37void KLimitedIODevice::close()
38{
39}
40
41qint64 KLimitedIODevice::size() const
42{
43 return m_length;
44}
45
46qint64 KLimitedIODevice::readData(char *data, qint64 maxlen)
47{
48 maxlen = qMin(a: maxlen, b: m_length - pos()); // Apply upper limit
49 return m_dev->read(data, maxlen);
50}
51
52bool KLimitedIODevice::seek(qint64 pos)
53{
54 Q_ASSERT(pos <= m_length);
55 pos = qMin(a: pos, b: m_length); // Apply upper limit
56 bool ret = m_dev->seek(pos: m_start + pos);
57 if (ret) {
58 QIODevice::seek(pos);
59 }
60 return ret;
61}
62
63qint64 KLimitedIODevice::bytesAvailable() const
64{
65 return QIODevice::bytesAvailable();
66}
67
68bool KLimitedIODevice::isSequential() const
69{
70 return m_dev->isSequential();
71}
72
73#include "moc_klimitediodevice_p.cpp"
74

source code of karchive/src/klimitediodevice.cpp