Stop using stack in win32

This commit is contained in:
Krzosa Karol
2024-08-09 09:53:40 +02:00
parent 47ec2b55c7
commit 6d9792b1a6
2 changed files with 33 additions and 30 deletions

View File

@@ -61,9 +61,9 @@ String ReadFile(Allocator arena, String path) {
bool success = false; bool success = false;
String result = {}; String result = {};
wchar_t wpath[1024]; Scratch scratch(arena);
CreateWidecharFromChar(wpath, Lengthof(wpath), path.data, path.len); String16 string16 = ToString16(scratch, path);
HANDLE handle = CreateFileW(wpath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); HANDLE handle = CreateFileW(string16.data, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (handle != INVALID_HANDLE_VALUE) { if (handle != INVALID_HANDLE_VALUE) {
LARGE_INTEGER file_size; LARGE_INTEGER file_size;
if (GetFileSizeEx(handle, &file_size)) { if (GetFileSizeEx(handle, &file_size)) {
@@ -96,10 +96,10 @@ typedef struct Win32_FileIter {
} Win32_FileIter; } Win32_FileIter;
String GetAbsolutePath(Allocator arena, String relative) { String GetAbsolutePath(Allocator arena, String relative) {
wchar_t wpath[1024]; Scratch scratch(arena);
CreateWidecharFromChar(wpath, Lengthof(wpath), relative.data, relative.len); String16 wpath = ToString16(scratch, relative);
wchar_t wpath_abs[1024]; wchar_t *wpath_abs = AllocArray(scratch, wchar_t, 4096);
DWORD written = GetFullPathNameW((wchar_t *)wpath, Lengthof(wpath_abs), wpath_abs, 0); DWORD written = GetFullPathNameW(wpath.data, 4096, wpath_abs, 0);
if (written == 0) if (written == 0)
return {}; return {};
String path = ToString(arena, {(wchar_t *)wpath_abs, written}); String path = ToString(arena, {(wchar_t *)wpath_abs, written});
@@ -187,12 +187,13 @@ double get_time_in_micros(void) {
bool WriteFile(String path, String data) { bool WriteFile(String path, String data) {
bool result = false; bool result = false;
wchar_t wpath[1024];
CreateWidecharFromChar(wpath, Lengthof(wpath), path.data, path.len); Scratch scratch;
String16 wpath = ToString16(scratch, path);
DWORD access = GENERIC_WRITE; DWORD access = GENERIC_WRITE;
DWORD creation_disposition = CREATE_ALWAYS; DWORD creation_disposition = CREATE_ALWAYS;
HANDLE handle = CreateFileW(wpath, access, 0, NULL, creation_disposition, FILE_ATTRIBUTE_NORMAL, NULL); HANDLE handle = CreateFileW(wpath.data, access, 0, NULL, creation_disposition, FILE_ATTRIBUTE_NORMAL, NULL);
if (handle != INVALID_HANDLE_VALUE) { if (handle != INVALID_HANDLE_VALUE) {
DWORD bytes_written = 0; DWORD bytes_written = 0;
Assert(data.len == (DWORD)data.len); // @Todo: can only read 32 byte size files? Assert(data.len == (DWORD)data.len); // @Todo: can only read 32 byte size files?
@@ -209,8 +210,9 @@ bool WriteFile(String path, String data) {
} }
String GetExePath(Allocator allocator) { String GetExePath(Allocator allocator) {
wchar_t wbuffer[1024]; Scratch scratch(allocator);
DWORD wsize = GetModuleFileNameW(0, wbuffer, Lengthof(wbuffer)); wchar_t *wbuffer = AllocArray(scratch, wchar_t, 4096);
DWORD wsize = GetModuleFileNameW(0, wbuffer, 4096);
Assert(wsize != 0); Assert(wsize != 0);
String path = ToString(allocator, wbuffer, wsize); String path = ToString(allocator, wbuffer, wsize);
@@ -227,31 +229,32 @@ String GetExeDir(Allocator allocator) {
} }
bool FileExists(String path) { bool FileExists(String path) {
wchar_t wbuff[1024]; Scratch scratch;
CreateWidecharFromChar(wbuff, Lengthof(wbuff), path.data, path.len); String16 wbuff = ToString16(scratch, path);
DWORD attribs = GetFileAttributesW(wbuff); DWORD attribs = GetFileAttributesW(wbuff.data);
bool result = attribs == INVALID_FILE_ATTRIBUTES ? false : true; bool result = attribs == INVALID_FILE_ATTRIBUTES ? false : true;
return result; return result;
} }
bool IsDir(String path) { bool IsDir(String path) {
wchar_t wbuff[1024]; Scratch scratch;
CreateWidecharFromChar(wbuff, Lengthof(wbuff), path.data, path.len); String16 wbuff = ToString16(scratch, path);
DWORD dwAttrib = GetFileAttributesW(wbuff); DWORD dwAttrib = GetFileAttributesW(wbuff.data);
return dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY); return dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
} }
bool IsFile(String path) { bool IsFile(String path) {
wchar_t wbuff[1024]; Scratch scratch;
CreateWidecharFromChar(wbuff, Lengthof(wbuff), path.data, path.len); String16 wbuff = ToString16(scratch, path);
DWORD dwAttrib = GetFileAttributesW(wbuff); DWORD dwAttrib = GetFileAttributesW(wbuff.data);
bool is_file = (dwAttrib & FILE_ATTRIBUTE_DIRECTORY) == 0; bool is_file = (dwAttrib & FILE_ATTRIBUTE_DIRECTORY) == 0;
return dwAttrib != INVALID_FILE_ATTRIBUTES && is_file; return dwAttrib != INVALID_FILE_ATTRIBUTES && is_file;
} }
String GetWorkingDir(Allocator arena) { String GetWorkingDir(Allocator arena) {
wchar_t wbuffer[1024]; Scratch scratch(arena);
DWORD wsize = GetCurrentDirectoryW(Lengthof(wbuffer), wbuffer); wchar_t *wbuffer = AllocArray(scratch, wchar_t, 4096);
DWORD wsize = GetCurrentDirectoryW(4096, wbuffer);
Assert(wsize != 0); Assert(wsize != 0);
wbuffer[wsize] = 0; wbuffer[wsize] = 0;
@@ -266,9 +269,10 @@ bool IsAbsolute(String path) {
} }
bool DeleteFile(String path) { bool DeleteFile(String path) {
wchar_t wpath[1024]; Scratch scratch;
CreateWidecharFromChar(wpath, Lengthof(wpath), path.data, path.len); String16 wpath = ToString16(scratch, path);
BOOL success = DeleteFileW(wpath);
BOOL success = DeleteFileW(wpath.data);
bool result = true; bool result = true;
if (success == 0) result = false; if (success == 0) result = false;
return result; return result;

View File

@@ -1,7 +1,6 @@
- Remove pointers and use ViewIDs (enable array debug while doing this) - Remove pointers and use ViewIDs (enable array debug while doing this)
- try using git grep for search for now, combine with fuzzy search buffer - try using git grep for search for now, combine with fuzzy search buffer
- lua maybe try heuristic matching paths from left? - lua maybe try heuristic matching paths from left?
- Append to console and change console directory
- win32: change all stack buffers to arena - win32: change all stack buffers to arena
- search as a command to execute which is going to be in the title bar - search as a command to execute which is going to be in the title bar