diff --git a/src/filesystem_native.cpp b/src/filesystem_native.cpp index 1ae415fcdfd..a77bf047c2b 100644 --- a/src/filesystem_native.cpp +++ b/src/filesystem_native.cpp @@ -30,7 +30,7 @@ #include "output.h" #include "platform.h" -#ifdef USE_CUSTOM_FILE_READBUF +#ifdef USE_CUSTOM_FILEBUF # include # include #endif @@ -55,14 +55,14 @@ int64_t NativeFilesystem::GetFilesize(StringView path) const { } std::streambuf* NativeFilesystem::CreateInputStreambuffer(StringView path, std::ios_base::openmode mode) const { -#ifdef USE_CUSTOM_FILE_READBUF +#ifdef USE_CUSTOM_FILEBUF (void)mode; int fd = open(ToString(path).c_str(), O_RDONLY); if (fd < 0) { return nullptr; } - return new Filesystem_Stream::FdStreamBuf(fd); + return new Filesystem_Stream::FdStreamBuf(fd, true); #else auto buf = new std::filebuf(); @@ -84,6 +84,20 @@ std::streambuf* NativeFilesystem::CreateInputStreambuffer(StringView path, std:: } std::streambuf* NativeFilesystem::CreateOutputStreambuffer(StringView path, std::ios_base::openmode mode) const { +#ifdef USE_CUSTOM_FILEBUF + int flags = O_TRUNC; + + if ((mode & std::ios_base::app) == std::ios_base::app) { + flags = O_APPEND; + } + + int fd = open(ToString(path).c_str(), O_WRONLY | O_CREAT | flags, S_IRUSR | S_IWUSR); + if (fd < 0) { + return nullptr; + } + + return new Filesystem_Stream::FdStreamBuf(fd, false); +#else auto* buf = new std::filebuf(); buf->open( #ifdef _MSC_VER @@ -99,6 +113,7 @@ std::streambuf* NativeFilesystem::CreateOutputStreambuffer(StringView path, std: } return buf; +#endif } bool NativeFilesystem::GetDirectoryContent(StringView path, std::vector& entries) const { diff --git a/src/filesystem_stream.cpp b/src/filesystem_stream.cpp index 5d20a592497..180a0989587 100644 --- a/src/filesystem_stream.cpp +++ b/src/filesystem_stream.cpp @@ -19,7 +19,7 @@ #include -#ifdef USE_CUSTOM_FILE_READBUF +#ifdef USE_CUSTOM_FILEBUF # include #endif @@ -127,14 +127,21 @@ Filesystem_Stream::InputMemoryStreamBuf::InputMemoryStreamBuf(std::vector buffer; }; -#ifdef USE_CUSTOM_FILE_READBUF +#ifdef USE_CUSTOM_FILEBUF class FdStreamBuf : public std::streambuf { public: - FdStreamBuf(int fd); + FdStreamBuf(int fd, bool is_read); FdStreamBuf(FdStreamBuf const& other) = delete; FdStreamBuf const& operator=(FdStreamBuf const& other) = delete; ~FdStreamBuf(); protected: - int_type underflow(); - std::streambuf::pos_type seekoff(std::streambuf::off_type offset, std::ios_base::seekdir dir, std::ios_base::openmode mode); - std::streambuf::pos_type seekpos(std::streambuf::pos_type pos, std::ios_base::openmode); + // Reading + int_type underflow() override; + std::streambuf::pos_type seekoff(std::streambuf::off_type offset, std::ios_base::seekdir dir, std::ios_base::openmode mode) override; + std::streambuf::pos_type seekpos(std::streambuf::pos_type pos, std::ios_base::openmode) override; + // Writing + int_type overflow(int c = EOF) override; + int sync() override; private: + // Reading void clear_buffer(); ssize_t bytes_remaining() const; + off_t file_offset = 0; + // Both int fd; - off_t file_offset = 0; - std::array buffer; + bool is_read; // Streams can be read and write but we only always use one mode + std::array buffer; }; #endif diff --git a/src/system.h b/src/system.h index 012a5a16215..05c1aabafc5 100644 --- a/src/system.h +++ b/src/system.h @@ -60,21 +60,21 @@ #elif defined(__3DS__) # define SUPPORT_JOYSTICK # define SUPPORT_JOYSTICK_AXIS -# define USE_CUSTOM_FILE_READBUF 4 * 1024 +# define USE_CUSTOM_FILEBUF 4 * 1024 #elif defined(__vita__) # define SUPPORT_JOYSTICK # define SUPPORT_JOYSTICK_AXIS -# define USE_CUSTOM_FILE_READBUF 4 * 1024 +# define USE_CUSTOM_FILEBUF 4 * 1024 #elif defined(__wii__) # include # define SUPPORT_JOYSTICK # define SUPPORT_JOYSTICK_AXIS -# define USE_CUSTOM_FILE_READBUF 4 * 1024 +# define USE_CUSTOM_FILEBUF 4 * 1024 #elif defined(__WIIU__) # define SUPPORT_JOYSTICK # define SUPPORT_JOYSTICK_AXIS # define SUPPORT_TOUCH -# define USE_CUSTOM_FILE_READBUF 16 * 1024 +# define USE_CUSTOM_FILEBUF 16 * 1024 #elif defined(_WIN32) # define SUPPORT_ZOOM # define SUPPORT_MOUSE @@ -84,7 +84,7 @@ #elif defined(__SWITCH__) # define SUPPORT_JOYSTICK # define SUPPORT_JOYSTICK_AXIS -# define USE_CUSTOM_FILE_READBUF 16 * 1024 +# define USE_CUSTOM_FILEBUF 16 * 1024 #elif defined(PLAYER_AMIGA) && !defined(__AROS__) # define SUPPORT_ZOOM # define SUPPORT_MOUSE