Filesystem stuff on Windows
Minor image acquire/present refactor Unsigned integer limits Fixed typo in arena push/push copy macro
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
#if !defined(LD_OS_CORE_H_)
|
||||
#define LD_OS_CORE_H_
|
||||
|
||||
typedef struct OS_Handle OS_Handle;
|
||||
struct OS_Handle {
|
||||
U64 v[1];
|
||||
};
|
||||
|
||||
// Virtual memory
|
||||
|
||||
function U64 VM_PageSize();
|
||||
@@ -11,4 +16,6 @@ function B32 VM_Commit(void *base, U64 size);
|
||||
function void VM_Decommit(void *base, U64 size);
|
||||
function void VM_Release(void *base, U64 size);
|
||||
|
||||
#include "filesystem.h"
|
||||
|
||||
#endif // LD_OS_CORE_H_
|
||||
|
||||
16
code/os/filesystem.h
Normal file
16
code/os/filesystem.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#if !defined(LD_OS_FILESYSTEM_H_)
|
||||
#define LD_OS_FILESYSTEM_H_
|
||||
|
||||
typedef U32 FS_AccessFlags;
|
||||
enum {
|
||||
FS_ACCESS_READ = (1 << 0),
|
||||
FS_ACCESS_WRITE = (1 << 1)
|
||||
};
|
||||
|
||||
function OS_Handle FS_FileOpen(Str8 path, FS_AccessFlags access);
|
||||
function void FS_FileClose(OS_Handle file);
|
||||
|
||||
function void FS_FileRead(OS_Handle file, void *ptr, U64 size, U64 offset);
|
||||
function void FS_FileWrite(OS_Handle file, void *ptr, U64 size, U64 offset);
|
||||
|
||||
#endif // LD_OS_FILESYSTEM_H_
|
||||
@@ -1,3 +1,19 @@
|
||||
// Windows utilities
|
||||
|
||||
internal Str8 Win32_WideStr(M_Arena *arena, Str8 v) {
|
||||
Str8 result = { 0 };
|
||||
|
||||
S32 nchars = MultiByteToWideChar(CP_UTF8, 0, (LPCCH) v.data, (int) v.count, 0, 0) + 1;
|
||||
U64 nbytes = nchars << 1;
|
||||
|
||||
result.count = nbytes - 2; // -2 because we don't include the null-terminator in the count
|
||||
result.data = M_ArenaPush(arena, U8, .count = nbytes);
|
||||
|
||||
MultiByteToWideChar(CP_UTF8, 0, (LPCCH) v.data, (int) v.count, (LPWSTR) result.data, nchars);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Virtual memory
|
||||
|
||||
U64 VM_PageSize() {
|
||||
@@ -36,3 +52,4 @@ void VM_Release(void *base, U64 size) {
|
||||
VirtualFree(base, 0, MEM_RELEASE);
|
||||
}
|
||||
|
||||
#include "filesystem.c"
|
||||
|
||||
86
code/os/impl/windows/filesystem.c
Normal file
86
code/os/impl/windows/filesystem.c
Normal file
@@ -0,0 +1,86 @@
|
||||
OS_Handle FS_FileOpen(Str8 path, FS_AccessFlags access) {
|
||||
OS_Handle result = { 0 };
|
||||
|
||||
M_TempScope(0, 0) {
|
||||
Str8 wpath = Win32_WideStr(temp.arena, path);
|
||||
|
||||
DWORD dwAccess = 0;
|
||||
DWORD dwShare = FILE_SHARE_READ;
|
||||
DWORD dwCreate = OPEN_EXISTING;
|
||||
|
||||
if (access & FS_ACCESS_WRITE) {
|
||||
dwCreate = CREATE_ALWAYS;
|
||||
dwAccess = GENERIC_WRITE;
|
||||
}
|
||||
|
||||
if (access & FS_ACCESS_READ) {
|
||||
dwAccess |= GENERIC_READ;
|
||||
}
|
||||
|
||||
HANDLE hFile = CreateFileW((LPCWSTR) wpath.data, dwAccess, dwShare, 0, dwCreate, 0, 0);
|
||||
|
||||
result.v[0] = (hFile == INVALID_HANDLE_VALUE) ? 0 : ((U64) hFile);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void FS_FileClose(OS_Handle file) {
|
||||
HANDLE hFile = cast(HANDLE) file.v[0];
|
||||
if (hFile) {
|
||||
CloseHandle(hFile);
|
||||
}
|
||||
}
|
||||
|
||||
void FS_FileRead(OS_Handle file, void *ptr, U64 size, U64 offset) {
|
||||
HANDLE hFile = cast(HANDLE) file.v[0];
|
||||
if (hFile) {
|
||||
U8 *buffer = ptr;
|
||||
U64 current = offset;
|
||||
U64 remainder = size;
|
||||
|
||||
while (remainder != 0) {
|
||||
OVERLAPPED overlapped = { 0 };
|
||||
overlapped.Offset = cast(DWORD) (current & 0xFFFFFFFF);
|
||||
overlapped.OffsetHigh = cast(DWORD) ((current >> 32) & 0xFFFFFFFF);
|
||||
|
||||
DWORD toread = (remainder > U32_MAX) ? U32_MAX : (DWORD) remainder;
|
||||
DWORD nread = 0;
|
||||
|
||||
if (!ReadFile(hFile, buffer, toread, &nread, &overlapped)) {
|
||||
break;
|
||||
}
|
||||
|
||||
buffer += nread;
|
||||
current += nread;
|
||||
remainder -= nread;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FS_FileWrite(OS_Handle file, void *ptr, U64 size, U64 offset) {
|
||||
HANDLE hFile = cast(HANDLE) file.v[0];
|
||||
if (hFile) {
|
||||
U8 *buffer = ptr;
|
||||
U64 current = offset;
|
||||
U64 remainder = size;
|
||||
|
||||
while (remainder != 0) {
|
||||
OVERLAPPED overlapped = { 0 };
|
||||
overlapped.Offset = cast(DWORD) (current & 0xFFFFFFFF);
|
||||
overlapped.OffsetHigh = cast(DWORD) ((current >> 32) & 0xFFFFFFFF);
|
||||
|
||||
DWORD towrite = (remainder > U32_MAX) ? U32_MAX : (DWORD) remainder;
|
||||
DWORD nwritten = 0;
|
||||
|
||||
if (!WriteFile(hFile, buffer, towrite, &nwritten, &overlapped)) {
|
||||
break;
|
||||
}
|
||||
|
||||
buffer += nwritten;
|
||||
current += nwritten;
|
||||
remainder -= nwritten;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user