|
| 1 | +/* |
| 2 | +DingusPPC - The Experimental PowerPC Macintosh emulator |
| 3 | +Copyright (C) 2018-23 divingkatae and maximum |
| 4 | + (theweirdo) spatium |
| 5 | +
|
| 6 | +(Contact divingkatae#1017 or powermax#2286 on Discord for more info) |
| 7 | +
|
| 8 | +This program is free software: you can redistribute it and/or modify |
| 9 | +it under the terms of the GNU General Public License as published by |
| 10 | +the Free Software Foundation, either version 3 of the License, or |
| 11 | +(at your option) any later version. |
| 12 | +
|
| 13 | +This program is distributed in the hope that it will be useful, |
| 14 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +GNU General Public License for more details. |
| 17 | +
|
| 18 | +You should have received a copy of the GNU General Public License |
| 19 | +along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 | +*/ |
| 21 | + |
| 22 | +#include <utils/imgfile.h> |
| 23 | + |
| 24 | +#include <emscripten.h> |
| 25 | + |
| 26 | +class ImgFile::Impl { |
| 27 | +public: |
| 28 | + int disk_id; |
| 29 | +}; |
| 30 | + |
| 31 | +ImgFile::ImgFile(): impl(std::make_unique<Impl>()) |
| 32 | +{ |
| 33 | + |
| 34 | +} |
| 35 | + |
| 36 | +ImgFile::~ImgFile() = default; |
| 37 | + |
| 38 | +bool ImgFile::open(const std::string &img_path) |
| 39 | +{ |
| 40 | + int disk_id = EM_ASM_INT({ |
| 41 | + return workerApi.disks.open(UTF8ToString($0)); |
| 42 | + }, img_path.c_str()); |
| 43 | + if (disk_id == -1) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + impl->disk_id = disk_id; |
| 48 | + return true; |
| 49 | +} |
| 50 | + |
| 51 | +void ImgFile::close() |
| 52 | +{ |
| 53 | + EM_ASM_({ workerApi.disks.close($0); }, impl->disk_id); |
| 54 | +} |
| 55 | + |
| 56 | +size_t ImgFile::size() const |
| 57 | +{ |
| 58 | + return EM_ASM_INT({ return workerApi.disks.size($0); }, impl->disk_id); |
| 59 | +} |
| 60 | + |
| 61 | +size_t ImgFile::read(void* buf, off_t offset, size_t length) const |
| 62 | +{ |
| 63 | + int read_size = EM_ASM_INT({ |
| 64 | + return workerApi.disks.read($0, $1, $2, $3); |
| 65 | + }, impl->disk_id, buf, int(offset), int(length)); |
| 66 | + return read_size; |
| 67 | +} |
| 68 | + |
| 69 | +size_t ImgFile::write(const void* buf, off_t offset, size_t length) |
| 70 | +{ |
| 71 | + int write_size = EM_ASM_INT({ |
| 72 | + return workerApi.disks.write($0, $1, $2, $3); |
| 73 | + }, impl->disk_id, buf, int(offset), int(length)); |
| 74 | + return write_size; |
| 75 | +} |
0 commit comments