forked from dingusdev/dingusppc
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move floppy disk image reading to be behind an ImgFile class
Allows different implementations for native (C++ streams) and JS (workerApi.disks.* functions) versions. Updates mihaip/infinite-mac#219
- Loading branch information
Showing
5 changed files
with
216 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") | ||
include(PlatformGlob) | ||
|
||
include_directories("${PROJECT_SOURCE_DIR}") | ||
|
||
file(GLOB SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp") | ||
platform_glob(SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp") | ||
|
||
add_library(utils OBJECT ${SOURCES}) | ||
target_link_libraries(utils PRIVATE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
DingusPPC - The Experimental PowerPC Macintosh emulator | ||
Copyright (C) 2018-23 divingkatae and maximum | ||
(theweirdo) spatium | ||
(Contact divingkatae#1017 or powermax#2286 on Discord for more info) | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/** @file Image file abstraction for floppy, hard drive and CD-ROM images | ||
* (implemented on each platform). */ | ||
|
||
#ifndef IMGFILE_H | ||
#define IMGFILE_H | ||
|
||
#include <cstddef> | ||
#include <string> | ||
|
||
class ImgFile { | ||
public: | ||
ImgFile(); | ||
~ImgFile(); | ||
|
||
bool open(const std::string& img_path); | ||
void close(); | ||
|
||
size_t size() const; | ||
|
||
size_t read(void* buf, off_t offset, size_t length) const; | ||
size_t write(const void* buf, off_t offset, size_t length); | ||
private: | ||
class Impl; // Holds private fields | ||
std::unique_ptr<Impl> impl; | ||
}; | ||
|
||
#endif // IMGFILE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
DingusPPC - The Experimental PowerPC Macintosh emulator | ||
Copyright (C) 2018-23 divingkatae and maximum | ||
(theweirdo) spatium | ||
(Contact divingkatae#1017 or powermax#2286 on Discord for more info) | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include <utils/imgfile.h> | ||
|
||
#include <emscripten.h> | ||
|
||
class ImgFile::Impl { | ||
public: | ||
int disk_id; | ||
}; | ||
|
||
ImgFile::ImgFile(): impl(std::make_unique<Impl>()) | ||
{ | ||
|
||
} | ||
|
||
ImgFile::~ImgFile() = default; | ||
|
||
bool ImgFile::open(const std::string &img_path) | ||
{ | ||
int disk_id = EM_ASM_INT({ | ||
return workerApi.disks.open(UTF8ToString($0)); | ||
}, img_path.c_str()); | ||
if (disk_id == -1) { | ||
return false; | ||
} | ||
|
||
impl->disk_id = disk_id; | ||
return true; | ||
} | ||
|
||
void ImgFile::close() | ||
{ | ||
EM_ASM_({ workerApi.disks.close($0); }, impl->disk_id); | ||
} | ||
|
||
size_t ImgFile::size() const | ||
{ | ||
return EM_ASM_INT({ return workerApi.disks.size($0); }, impl->disk_id); | ||
} | ||
|
||
size_t ImgFile::read(void* buf, off_t offset, size_t length) const | ||
{ | ||
int read_size = EM_ASM_INT({ | ||
return workerApi.disks.read($0, $1, $2, $3); | ||
}, impl->disk_id, buf, int(offset), int(length)); | ||
return read_size; | ||
} | ||
|
||
size_t ImgFile::write(const void* buf, off_t offset, size_t length) | ||
{ | ||
int write_size = EM_ASM_INT({ | ||
return workerApi.disks.write($0, $1, $2, $3); | ||
}, impl->disk_id, buf, int(offset), int(length)); | ||
return write_size; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
DingusPPC - The Experimental PowerPC Macintosh emulator | ||
Copyright (C) 2018-23 divingkatae and maximum | ||
(theweirdo) spatium | ||
(Contact divingkatae#1017 or powermax#2286 on Discord for more info) | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include <utils/imgfile.h> | ||
|
||
#include <fstream> | ||
|
||
class ImgFile::Impl { | ||
public: | ||
std::fstream stream; | ||
}; | ||
|
||
ImgFile::ImgFile(): impl(std::make_unique<Impl>()) | ||
{ | ||
|
||
} | ||
|
||
ImgFile::~ImgFile() = default; | ||
|
||
bool ImgFile::open(const std::string &img_path) | ||
{ | ||
impl->stream.open(img_path, std::ios::in | std::ios::out | std::ios::binary); | ||
return !impl->stream.fail(); | ||
} | ||
|
||
void ImgFile::close() | ||
{ | ||
impl->stream.close(); | ||
} | ||
|
||
size_t ImgFile::size() const | ||
{ | ||
impl->stream.seekg(0, impl->stream.end); | ||
return impl->stream.tellg(); | ||
} | ||
|
||
size_t ImgFile::read(void* buf, off_t offset, size_t length) const | ||
{ | ||
impl->stream.seekg(offset, std::ios::beg); | ||
impl->stream.read((char *)buf, length); | ||
return impl->stream.gcount(); | ||
} | ||
|
||
size_t ImgFile::write(const void* buf, off_t offset, size_t length) | ||
{ | ||
impl->stream.seekg(offset, std::ios::beg); | ||
impl->stream.write((const char *)buf, length); | ||
return impl->stream.gcount(); | ||
} |