src/platform/python/vfs-py.c (view raw)
1/* Copyright (c) 2013-2016 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 <mgba-util/vfs.h>
7
8struct VFilePy {
9 struct VFile d;
10 void* fileobj;
11};
12
13bool _vfpClose(struct VFile* vf);
14off_t _vfpSeek(struct VFile* vf, off_t offset, int whence);
15ssize_t _vfpRead(struct VFile* vf, void* buffer, size_t size);
16ssize_t _vfpWrite(struct VFile* vf, const void* buffer, size_t size);
17void* _vfpMap(struct VFile* vf, size_t size, int flags);
18void _vfpUnmap(struct VFile* vf, void* memory, size_t size);
19void _vfpTruncate(struct VFile* vf, size_t size);
20ssize_t _vfpSize(struct VFile* vf);
21bool _vfpSync(struct VFile* vf, const void* buffer, size_t size);
22
23struct VFile* VFileFromPython(void* fileobj) {
24 if (!fileobj) {
25 return 0;
26 }
27
28 struct VFilePy* vfp = malloc(sizeof(struct VFilePy));
29 if (!vfp) {
30 return 0;
31 }
32
33 vfp->fileobj = fileobj;
34 vfp->d.close = _vfpClose;
35 vfp->d.seek = _vfpSeek;
36 vfp->d.read = _vfpRead;
37 vfp->d.readline = VFileReadline;
38 vfp->d.write = _vfpWrite;
39 vfp->d.map = _vfpMap;
40 vfp->d.unmap = _vfpUnmap;
41 vfp->d.truncate = _vfpTruncate;
42 vfp->d.size = _vfpSize;
43 vfp->d.sync = _vfpSync;
44
45 return &vfp->d;
46}