#include #include #include "IDiscIO.hpp" namespace NOD { class DiscIOWBFS : public IDiscIO { std::string filepath; public: DiscIOWBFS(const std::string& fpin) : filepath(fpin) {} class ReadStream : public IReadStream { friend class DiscIOWBFS; FILE* fp; ReadStream(FILE* fpin) : fp(fpin) {} ~ReadStream() {fclose(fp);} public: size_t read(void* buf, size_t length) {return fread(buf, 1, length, fp);} void seek(size_t offset, int whence) {fseek(fp, offset, whence);} }; std::unique_ptr beginReadStream(size_t offset) const { FILE* fp = fopen(filepath.c_str(), "rb"); if (!fp) { throw std::runtime_error("Unable to open '" + filepath + "' for reading"); return std::unique_ptr(); } fseek(fp, offset, SEEK_SET); return std::unique_ptr(new ReadStream(fp)); } class WriteStream : public IWriteStream { friend class DiscIOWBFS; FILE* fp; WriteStream(FILE* fpin) : fp(fpin) {} ~WriteStream() {fclose(fp);} public: size_t write(void* buf, size_t length) {return fwrite(buf, 1, length, fp);} }; std::unique_ptr beginWriteStream(size_t offset) const { FILE* fp = fopen(filepath.c_str(), "wb"); if (!fp) { throw std::runtime_error("Unable to open '" + filepath + "' for writing"); return std::unique_ptr(); } fseek(fp, offset, SEEK_SET); return std::unique_ptr(new WriteStream(fp)); } }; std::unique_ptr NewDiscIOWBFS(const char* path) { return std::unique_ptr(new DiscIOWBFS(path)); } }