diff options
-rw-r--r-- | backends/fs/ds/ds-fs.cpp | 46 | ||||
-rw-r--r-- | backends/fs/ds/ds-fs.h | 6 |
2 files changed, 12 insertions, 40 deletions
diff --git a/backends/fs/ds/ds-fs.cpp b/backends/fs/ds/ds-fs.cpp index f52bf6f501..fc4e5f5376 100644 --- a/backends/fs/ds/ds-fs.cpp +++ b/backends/fs/ds/ds-fs.cpp @@ -40,9 +40,13 @@ namespace DS { // DSFileSystemNode - Flash ROM file system using Zip files // ////////////////////////////////////////////////////////////// -ZipFile* DSFileSystemNode::_zipFile = NULL; -char currentDir[128]; -bool readPastEndOfFile = false; +ZipFile* DSFileSystemNode::_zipFile = NULL; // FIXME: Avoid non-const global vars +char currentDir[128]; // FIXME: Avoid non-const global vars +bool readPastEndOfFile = false; // FIXME: Avoid non-const global vars + +enum { + WRITE_BUFFER_SIZE = 512 +}; DSFileSystemNode::DSFileSystemNode() { _displayName = "ds:/"; @@ -205,7 +209,8 @@ Common::SeekableReadStream *DSFileSystemNode::createReadStream() { } Common::WriteStream *DSFileSystemNode::createWriteStream() { - return DSFileStream::makeFromPath(getPath(), true); + Common::WriteStream *stream = DSFileStream::makeFromPath(getPath(), true); + return Common::wrapBufferedWriteStream(stream, WRITE_BUFFER_SIZE, DisposeAfterUse::YES); } ////////////////////////////////////////////////////////////////////////// @@ -386,21 +391,17 @@ Common::SeekableReadStream *GBAMPFileSystemNode::createReadStream() { } Common::WriteStream *GBAMPFileSystemNode::createWriteStream() { - return DSFileStream::makeFromPath(getPath(), true); + Common::WriteStream *stream = DSFileStream::makeFromPath(getPath(), true); + return Common::wrapBufferedWriteStream(stream, WRITE_BUFFER_SIZE, DisposeAfterUse::YES); } - DSFileStream::DSFileStream(void *handle) : _handle(handle) { assert(handle); - _writeBufferPos = 0; } DSFileStream::~DSFileStream() { - if (_writeBufferPos > 0) { - flush(); - } std_fclose((FILE *)_handle); } @@ -417,12 +418,10 @@ bool DSFileStream::eos() const { } int32 DSFileStream::pos() const { - assert(_writeBufferPos == 0); // This method may only be called when reading! return std_ftell((FILE *)_handle); } int32 DSFileStream::size() const { - assert(_writeBufferPos == 0); // This method may only be called when reading! int32 oldPos = std_ftell((FILE *)_handle); std_fseek((FILE *)_handle, 0, SEEK_END); int32 length = std_ftell((FILE *)_handle); @@ -432,39 +431,18 @@ int32 DSFileStream::size() const { } bool DSFileStream::seek(int32 offs, int whence) { - if (_writeBufferPos > 0) { - flush(); - } return std_fseek((FILE *)_handle, offs, whence) == 0; } uint32 DSFileStream::read(void *ptr, uint32 len) { - if (_writeBufferPos > 0) { - flush(); - } return std_fread(ptr, 1, len, (FILE *)_handle); } uint32 DSFileStream::write(const void *ptr, uint32 len) { - if (_writeBufferPos + len < WRITE_BUFFER_SIZE) { - memcpy(_writeBuffer + _writeBufferPos, ptr, len); - _writeBufferPos += len; - return len; - } else { - if (_writeBufferPos > 0) { - flush(); - } - return std_fwrite(ptr, 1, len, (FILE *)_handle); - } + return std_fwrite(ptr, 1, len, (FILE *)_handle); } bool DSFileStream::flush() { - - if (_writeBufferPos > 0) { - std_fwrite(_writeBuffer, 1, _writeBufferPos, (FILE *) _handle); - _writeBufferPos = 0; - } - return std_fflush((FILE *)_handle) == 0; } diff --git a/backends/fs/ds/ds-fs.h b/backends/fs/ds/ds-fs.h index 0a9ba1e48b..c891dac8f9 100644 --- a/backends/fs/ds/ds-fs.h +++ b/backends/fs/ds/ds-fs.h @@ -171,16 +171,10 @@ struct fileHandle { class DSFileStream : public Common::SeekableReadStream, public Common::WriteStream, public Common::NonCopyable { protected: - enum { - WRITE_BUFFER_SIZE = 512 - }; /** File handle to the actual file. */ void *_handle; - char _writeBuffer[WRITE_BUFFER_SIZE]; - int _writeBufferPos; - public: /** * Given a path, invokes fopen on that path and wrap the result in a |