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
22VFileDevice::VFileDevice(const QString& filename, QIODevice::OpenMode mode, QObject* parent)
23 : QIODevice(parent)
24{
25 int posixMode = 0;
26 if ((mode & QIODevice::ReadWrite) == QIODevice::ReadWrite) {
27 posixMode = O_RDWR;
28 } else if (mode & QIODevice::ReadOnly) {
29 posixMode = O_RDONLY;
30 } else if (mode & QIODevice::WriteOnly) {
31 posixMode = O_WRONLY;
32 }
33 m_vf = open(filename, posixMode);
34 if (m_vf) {
35 setOpenMode(mode);
36 }
37}
38
39void VFileDevice::close() {
40 if (!m_vf) {
41 return;
42 }
43 QIODevice::close();
44 m_vf->close(m_vf);
45 m_vf = nullptr;
46}
47
48bool VFileDevice::resize(qint64 sz) {
49 m_vf->truncate(m_vf, sz);
50 return true;
51}
52
53bool VFileDevice::seek(qint64 pos) {
54 QIODevice::seek(pos);
55 return m_vf->seek(m_vf, pos, SEEK_SET) == pos;
56}
57
58VFileDevice& VFileDevice::operator=(VFile* vf) {
59 close();
60 m_vf = vf;
61 setOpenMode(QIODevice::ReadWrite);
62 return *this;
63}
64
65qint64 VFileDevice::readData(char* data, qint64 maxSize) {
66 return m_vf->read(m_vf, data, maxSize);
67}
68
69qint64 VFileDevice::writeData(const char* data, qint64 maxSize) {
70 return m_vf->write(m_vf, data, maxSize);
71}
72
73qint64 VFileDevice::size() const {
74 return m_vf->size(m_vf);
75}
76
77VFile* VFileDevice::open(const QString& path, int mode) {
78 return VFileOpen(path.toUtf8().constData(), mode);
79}
80
81VDir* VFileDevice::openDir(const QString& path) {
82 return VDirOpen(path.toUtf8().constData());
83}
84VDir* VFileDevice::openArchive(const QString& path) {
85 return VDirOpenArchive(path.toUtf8().constData());
86}