File indexing completed on 2024-04-14 03:50:29

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 #ifndef klimitediodevice_p_h
0008 #define klimitediodevice_p_h
0009 
0010 #include <QDebug>
0011 #include <QIODevice>
0012 /**
0013  * A readonly device that reads from an underlying device
0014  * from a given point to another (e.g. to give access to a single
0015  * file inside an archive).
0016  * @author David Faure <faure@kde.org>
0017  * @internal - used by KArchive
0018  */
0019 class KLimitedIODevice : public QIODevice
0020 {
0021     Q_OBJECT
0022 public:
0023     /**
0024      * Creates a new KLimitedIODevice.
0025      * @param dev the underlying device, opened or not
0026      * This device itself auto-opens (in readonly mode), no need to open it.
0027      * @param start where to start reading (position in bytes)
0028      * @param length the length of the data to read (in bytes)
0029      */
0030     KLimitedIODevice(QIODevice *dev, qint64 start, qint64 length);
0031     ~KLimitedIODevice() override
0032     {
0033     }
0034 
0035     bool isSequential() const override;
0036 
0037     bool open(QIODevice::OpenMode m) override;
0038     void close() override;
0039 
0040     qint64 size() const override;
0041 
0042     qint64 readData(char *data, qint64 maxlen) override;
0043     qint64 writeData(const char *, qint64) override
0044     {
0045         return -1; // unsupported
0046     }
0047 
0048     // virtual qint64 pos() const { return m_dev->pos() - m_start; }
0049     bool seek(qint64 pos) override;
0050     qint64 bytesAvailable() const override;
0051 
0052 private:
0053     QIODevice *m_dev;
0054     qint64 m_start;
0055     qint64 m_length;
0056 };
0057 
0058 #endif