src/platform/qt/VFileDevice.cpp (view raw)
1/* Copyright (c) 2013-2014 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "VFileDevice.h"
7
8#include <mgba-util/vfs.h>
9
10using namespace QGBA;
11
12VFileDevice::VFileDevice(VFile* vf, QObject* parent)
13 : QIODevice(parent)
14 , m_vf(vf)
15{
16 // TODO: Correct mode
17 if (vf) {
18 setOpenMode(QIODevice::ReadWrite);
19 }
20}
21
22void VFileDevice::close() {
23 QIODevice::close();
24 m_vf->close(m_vf);
25 m_vf = nullptr;
26}
27
28bool VFileDevice::resize(qint64 sz) {
29 m_vf->truncate(m_vf, sz);
30 return true;
31}
32
33bool VFileDevice::seek(qint64 pos) {
34 QIODevice::seek(pos);
35 return m_vf->seek(m_vf, pos, SEEK_SET) == pos;
36}
37
38VFileDevice& VFileDevice::operator=(VFile* vf) {
39 close();
40 m_vf = vf;
41 setOpenMode(QIODevice::ReadWrite);
42 return *this;
43}
44
45qint64 VFileDevice::readData(char* data, qint64 maxSize) {
46 return m_vf->read(m_vf, data, maxSize);
47}
48
49qint64 VFileDevice::writeData(const char* data, qint64 maxSize) {
50 return m_vf->write(m_vf, data, maxSize);
51}
52
53qint64 VFileDevice::size() const {
54 return m_vf->size(m_vf);
55}
56
57VFile* VFileDevice::open(const QString& path, int mode) {
58 return VFileOpen(path.toUtf8().constData(), mode);
59}
60
61VDir* VFileDevice::openDir(const QString& path) {
62 return VDirOpen(path.toUtf8().constData());
63}
64VDir* VFileDevice::openArchive(const QString& path) {
65 return VDirOpenArchive(path.toUtf8().constData());
66}