File indexing completed on 2025-01-05 05:23:24

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2010 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "bytearraymodeliodevice.hpp"
0010 
0011 // Okteta core
0012 #include <Okteta/AbstractByteArrayModel>
0013 
0014 namespace Okteta {
0015 
0016 ByteArrayModelIoDevice::ByteArrayModelIoDevice(AbstractByteArrayModel* byteArrayModel, QObject* parent)
0017     : QIODevice(parent)
0018     , mByteArrayModel(byteArrayModel)
0019 {
0020     open(ReadOnly);   // krazy:exclude=syscalls
0021 }
0022 
0023 ByteArrayModelIoDevice::~ByteArrayModelIoDevice() = default;
0024 
0025 qint64 ByteArrayModelIoDevice::size() const
0026 {
0027     return mByteArrayModel->size();
0028 }
0029 
0030 bool ByteArrayModelIoDevice::canReadLine() const
0031 {
0032     return
0033         isOpen()
0034         && (mByteArrayModel->indexOf("\n", 1, pos()) != -1
0035             || QIODevice::canReadLine());
0036 }
0037 
0038 bool ByteArrayModelIoDevice::open(OpenMode openMode)
0039 {
0040     QIODevice::open(openMode);
0041 
0042     openMode ^= WriteOnly | Append;
0043     setOpenMode(openMode);
0044 
0045     if (!isReadable()) {
0046         return false;
0047     }
0048 
0049     seek(0);
0050 
0051     return true;
0052 }
0053 
0054 bool ByteArrayModelIoDevice::seek(qint64 pos)
0055 {
0056     if (pos > mByteArrayModel->size() || pos < 0) {
0057         return false;
0058     }
0059 
0060     mReadOffset = pos;
0061 
0062     return QIODevice::seek(pos);
0063 }
0064 
0065 qint64 ByteArrayModelIoDevice::readData(char* data, qint64 maxlength)
0066 {
0067     const Size copiedLength =
0068         mByteArrayModel->copyTo(reinterpret_cast<Byte*>(data), mReadOffset, maxlength);
0069 
0070     mReadOffset += copiedLength;
0071 
0072     return copiedLength;
0073 }
0074 
0075 qint64 ByteArrayModelIoDevice::writeData(const char* data, qint64 length)
0076 {
0077     Q_UNUSED(data);
0078     Q_UNUSED(length);
0079     return -1;
0080 }
0081 
0082 }
0083 
0084 #include "moc_bytearraymodeliodevice.cpp"