refactored FileMode to make more sense

This commit is contained in:
Noel Berry
2021-04-05 01:07:16 -07:00
parent 9e8a181fd2
commit 65f7194e4f
8 changed files with 55 additions and 27 deletions

View File

@ -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;
}

View File

@ -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);