2020-02-05 07:31:43 +00:00
|
|
|
//
|
|
|
|
// File: win32_foldhaus_fileio.h
|
|
|
|
// Author: Peter Slattery
|
|
|
|
// Creation Date: 2020-02-04
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// NOTE: Relies on having imported foldhaus_platform.h prior to this file
|
|
|
|
//
|
|
|
|
#ifndef WIN32_FOLDHAUS_FILEIO_H
|
|
|
|
|
|
|
|
PLATFORM_READ_ENTIRE_FILE(Win32ReadEntireFile)
|
|
|
|
{
|
|
|
|
platform_memory_result Result = {};
|
2020-02-05 08:03:56 +00:00
|
|
|
Result.Error = PlatformMemory_NoError;
|
2020-02-05 07:31:43 +00:00
|
|
|
|
2020-05-30 22:09:06 +00:00
|
|
|
Assert(IsNullTerminated(Path));
|
|
|
|
HANDLE FileHandle = CreateFileA (Path.Memory, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
2020-02-05 07:31:43 +00:00
|
|
|
|
|
|
|
if (FileHandle != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
DWORD FileSize = GetFileSize(FileHandle, NULL);
|
2020-02-05 07:35:21 +00:00
|
|
|
Result.Base = (u8*)VirtualAlloc(NULL, FileSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
|
2020-05-30 22:09:06 +00:00
|
|
|
if (Result.Base)
|
2020-02-05 07:31:43 +00:00
|
|
|
{
|
|
|
|
Result.Size = FileSize;
|
|
|
|
|
|
|
|
s32 BytesRead = 0;
|
|
|
|
if (ReadFile(FileHandle, (LPVOID)Result.Base, FileSize, (LPDWORD)(&BytesRead), NULL))
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-05 08:03:56 +00:00
|
|
|
u32 Error = GetLastError();
|
2020-02-06 04:24:34 +00:00
|
|
|
// TODO(Peter): :ErrorLogging
|
2020-02-05 07:31:43 +00:00
|
|
|
Result.Size = 0;
|
2020-02-05 08:03:56 +00:00
|
|
|
Result.Error = PlatformMemory_UnknownError;
|
2020-02-05 07:31:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CloseHandle(FileHandle);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-05 08:03:56 +00:00
|
|
|
Result.Error = PlatformMemory_FileNotFound;
|
2020-02-06 04:24:34 +00:00
|
|
|
// TODO(Peter): :ErrorLogging
|
2020-02-05 07:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
PLATFORM_WRITE_ENTIRE_FILE(Win32WriteEntireFile)
|
|
|
|
{
|
2020-05-30 22:09:06 +00:00
|
|
|
Assert(IsNullTerminated(Path));
|
2020-02-05 07:31:43 +00:00
|
|
|
b32 Result = false;
|
|
|
|
HANDLE FileHandle = CreateFileA (
|
2020-05-30 22:09:06 +00:00
|
|
|
Path.Memory,
|
2020-02-05 07:31:43 +00:00
|
|
|
GENERIC_WRITE,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
CREATE_ALWAYS,
|
|
|
|
FILE_ATTRIBUTE_NORMAL,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (FileHandle != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
DWORD BytesWritten = 0;
|
|
|
|
|
|
|
|
b32 WriteSuccess = WriteFile(FileHandle,
|
|
|
|
Contents, Size,
|
|
|
|
&BytesWritten,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (WriteSuccess && BytesWritten == (u32)Size)
|
|
|
|
{
|
|
|
|
CloseHandle(FileHandle);
|
|
|
|
Result = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal FILETIME
|
|
|
|
GetFileLastWriteTime(char* Path)
|
|
|
|
{
|
|
|
|
FILETIME Result = {};
|
|
|
|
|
|
|
|
WIN32_FIND_DATA FindData = {};
|
|
|
|
HANDLE FileHandle = FindFirstFileA(Path, &FindData);
|
|
|
|
|
|
|
|
if (FileHandle != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
Result = FindData.ftLastWriteTime;
|
|
|
|
FindClose(FileHandle);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-06 04:24:34 +00:00
|
|
|
// TODO(Peter): :ErrorLogging
|
2020-02-05 07:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
PLATFORM_GET_FILE_PATH(Win32SystemDialogueOpenFile)
|
|
|
|
{
|
|
|
|
b32 Result = false;
|
|
|
|
|
2020-05-30 22:09:06 +00:00
|
|
|
PathBuffer->Memory[0] = 0;
|
2020-02-05 07:31:43 +00:00
|
|
|
|
|
|
|
OPENFILENAMEA OpenFileName = {};
|
|
|
|
OpenFileName.lStructSize = sizeof(OpenFileName);
|
|
|
|
OpenFileName.hwndOwner = NULL;
|
|
|
|
OpenFileName.lpstrFilter = FilterStrings;
|
|
|
|
OpenFileName.lpstrCustomFilter = NULL; // NOTE(Peter): for preserving last filter string chosen
|
|
|
|
OpenFileName.nMaxCustFilter = 0; // NOTE(Peter): ignored since we left CustomFilter null
|
|
|
|
OpenFileName.nFilterIndex = 1;
|
2020-05-30 22:09:06 +00:00
|
|
|
OpenFileName.lpstrFile = PathBuffer->Memory;
|
|
|
|
OpenFileName.nMaxFile = PathBuffer->Max;
|
2020-02-05 07:31:43 +00:00
|
|
|
OpenFileName.lpstrFileTitle = NULL;
|
|
|
|
OpenFileName.nMaxFileTitle = 0; // NOTE(Peter): Ignored since fileTitle is null
|
|
|
|
OpenFileName.lpstrInitialDir = NULL;
|
|
|
|
OpenFileName.lpstrTitle = NULL;
|
|
|
|
OpenFileName.Flags = OFN_ENABLESIZING | OFN_FILEMUSTEXIST;
|
|
|
|
OpenFileName.lpstrDefExt = NULL;
|
|
|
|
|
|
|
|
Result = GetOpenFileNameA (&OpenFileName);
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2020-02-05 08:03:56 +00:00
|
|
|
internal directory_listing
|
|
|
|
EnumerateDirectory(char* Path, memory_arena* Storage)
|
|
|
|
{
|
|
|
|
directory_listing Result = {};
|
2020-05-30 22:09:06 +00:00
|
|
|
// TODO(Peter):
|
2020-02-05 08:03:56 +00:00
|
|
|
return Result;
|
|
|
|
}
|
2020-02-05 07:31:43 +00:00
|
|
|
|
|
|
|
#define WIN32_FOLDHAUS_FILEIO_H
|
|
|
|
#endif // WIN32_FOLDHAUS_FILEIO_H
|