mirror of
https://github.com/NoelFB/blah.git
synced 2024-11-25 16:18:57 +08:00
refactored FileMode to make more sense
This commit is contained in:
parent
9e8a181fd2
commit
65f7194e4f
|
@ -9,10 +9,17 @@ namespace Blah
|
|||
|
||||
enum class FileMode
|
||||
{
|
||||
None = 0,
|
||||
Read = 1 << 0,
|
||||
Write = 1 << 1,
|
||||
ReadWrite = Read | Write,
|
||||
// Opens an existing file for reading.
|
||||
OpenRead,
|
||||
|
||||
// Opens an existing file for reading and writing.
|
||||
Open,
|
||||
|
||||
// Creates a new file or overwrites an existing file for writing.
|
||||
CreateWrite,
|
||||
|
||||
// Creates a new file or overwrites an existing file for reading and writing.
|
||||
Create,
|
||||
};
|
||||
|
||||
namespace Directory
|
||||
|
@ -43,7 +50,7 @@ namespace Blah
|
|||
bool remove(const FilePath& path);
|
||||
|
||||
// Opens the given file and returns a stream
|
||||
FileStream open(const FilePath& path, FileMode mode = FileMode::ReadWrite);
|
||||
FileStream open(const FilePath& path, FileMode mode);
|
||||
}
|
||||
|
||||
namespace Path
|
||||
|
|
|
@ -8,7 +8,7 @@ namespace Blah
|
|||
{
|
||||
public:
|
||||
FileStream();
|
||||
FileStream(const FilePath& path, FileMode mode = FileMode::ReadWrite);
|
||||
FileStream(const FilePath& path, FileMode mode);
|
||||
FileStream(FileStream&& fs) noexcept;
|
||||
FileStream& operator=(FileStream&& fs) noexcept;
|
||||
~FileStream();
|
||||
|
|
|
@ -17,7 +17,7 @@ Aseprite::Aseprite()
|
|||
|
||||
Aseprite::Aseprite(const FilePath& path)
|
||||
{
|
||||
FileStream fs(path, FileMode::Read);
|
||||
FileStream fs(path, FileMode::OpenRead);
|
||||
parse(fs);
|
||||
}
|
||||
|
||||
|
|
|
@ -58,7 +58,7 @@ Font::Font(Stream& stream) : Font()
|
|||
|
||||
Font::Font(const FilePath& path) : Font()
|
||||
{
|
||||
FileStream fs(path, FileMode::Read);
|
||||
FileStream fs(path, FileMode::OpenRead);
|
||||
if (fs.is_readable())
|
||||
load(fs);
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ Image::Image(const FilePath& file)
|
|||
pixels = nullptr;
|
||||
m_stbi_ownership = false;
|
||||
|
||||
FileStream fs(file, FileMode::Read);
|
||||
FileStream fs(file, FileMode::OpenRead);
|
||||
if (fs.is_readable())
|
||||
from_stream(fs);
|
||||
}
|
||||
|
@ -205,7 +205,7 @@ void Image::set_pixels(const RectI& rect, Color* data)
|
|||
|
||||
bool Image::save_png(const FilePath& file) const
|
||||
{
|
||||
FileStream fs(file, FileMode::Write);
|
||||
FileStream fs(file, FileMode::CreateWrite);
|
||||
return save_png(fs);
|
||||
}
|
||||
|
||||
|
@ -234,7 +234,7 @@ bool Image::save_png(Stream& stream) const
|
|||
|
||||
bool Image::save_jpg(const FilePath& file, int quality) const
|
||||
{
|
||||
FileStream fs(file, FileMode::Write);
|
||||
FileStream fs(file, FileMode::CreateWrite);
|
||||
return save_jpg(fs, quality);
|
||||
}
|
||||
|
||||
|
|
|
@ -647,11 +647,25 @@ void PlatformBackend::dir_explore(const char* path)
|
|||
|
||||
bool PlatformBackend::file_open(const char* path, PlatformBackend::FileHandle* handle, FileMode mode)
|
||||
{
|
||||
const char* sdlMode = "rb";
|
||||
if (mode == FileMode::Write)
|
||||
sdlMode = "wb";
|
||||
const char* sdl_mode = "";
|
||||
|
||||
auto ptr = SDL_RWFromFile(path, sdlMode);
|
||||
switch (mode)
|
||||
{
|
||||
case FileMode::OpenRead:
|
||||
sdl_mode = "rb";
|
||||
break;
|
||||
case FileMode::OpenReadWrite:
|
||||
sdl_mode = "r+b";
|
||||
break;
|
||||
case FileMode::CreateWrite:
|
||||
sdl_mode = "wb";
|
||||
break;
|
||||
case FileMode::CreateReadWrite:
|
||||
sdl_mode = "w+b";
|
||||
break;
|
||||
}
|
||||
|
||||
auto ptr = SDL_RWFromFile(path, sdl_mode);
|
||||
*handle = (PlatformBackend::FileHandle)ptr;
|
||||
return ptr != nullptr;
|
||||
}
|
||||
|
|
|
@ -489,16 +489,24 @@ bool PlatformBackend::file_open(const char* path, PlatformBackend::FileHandle* h
|
|||
int access = 0;
|
||||
int creation = 0;
|
||||
|
||||
if (((int)mode & (int)FileMode::Read) == (int)FileMode::Read)
|
||||
switch (mode)
|
||||
{
|
||||
access |= GENERIC_READ;
|
||||
case FileMode::OpenRead:
|
||||
access = GENERIC_READ;
|
||||
creation = OPEN_EXISTING;
|
||||
}
|
||||
|
||||
if (((int)mode & (int)FileMode::Write) == (int)FileMode::Write)
|
||||
{
|
||||
access |= GENERIC_WRITE;
|
||||
creation = OPEN_ALWAYS;
|
||||
break;
|
||||
case FileMode::Open:
|
||||
access = GENERIC_READ | GENERIC_WRITE;
|
||||
creation = OPEN_EXISTING;
|
||||
break;
|
||||
case FileMode::CreateWrite:
|
||||
access = GENERIC_WRITE;
|
||||
creation = CREATE_ALWAYS;
|
||||
break;
|
||||
case FileMode::Create:
|
||||
access = GENERIC_READ | GENERIC_WRITE;
|
||||
creation = CREATE_ALWAYS;
|
||||
break;
|
||||
}
|
||||
|
||||
auto result = CreateFile(path, access, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, creation, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
|
|
|
@ -8,7 +8,7 @@ using namespace Blah;
|
|||
FileStream::FileStream()
|
||||
{
|
||||
m_handle = nullptr;
|
||||
m_mode = FileMode::None;
|
||||
m_mode = FileMode::OpenRead;
|
||||
}
|
||||
|
||||
FileStream::FileStream(const FilePath& path, FileMode mode)
|
||||
|
@ -70,12 +70,12 @@ bool FileStream::is_open() const
|
|||
|
||||
bool FileStream::is_readable() const
|
||||
{
|
||||
return m_handle != nullptr && (m_mode == FileMode::ReadWrite || m_mode == FileMode::Read);
|
||||
return m_handle != nullptr && (m_mode != FileMode::CreateWrite);
|
||||
}
|
||||
|
||||
bool FileStream::is_writable() const
|
||||
{
|
||||
return m_handle != nullptr && (m_mode == FileMode::ReadWrite || m_mode == FileMode::Write);
|
||||
return m_handle != nullptr && (m_mode != FileMode::OpenRead);
|
||||
}
|
||||
|
||||
i64 FileStream::read_into(void* ptr, i64 length)
|
||||
|
@ -108,5 +108,4 @@ void FileStream::close()
|
|||
if (m_handle != nullptr)
|
||||
PlatformBackend::file_close(m_handle);
|
||||
m_handle = nullptr;
|
||||
m_mode = FileMode::None;
|
||||
}
|
Loading…
Reference in New Issue
Block a user