From 6d9792b1a6ebaefbf489ce6104e1494978b67f4f Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 9 Aug 2024 09:53:40 +0200 Subject: [PATCH] Stop using stack in win32 --- src/basic/win32.cpp | 62 +++++++++++++++++++++------------------- src/text_editor/todo.txt | 1 - 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/src/basic/win32.cpp b/src/basic/win32.cpp index 7af6f28..ac5d1cd 100644 --- a/src/basic/win32.cpp +++ b/src/basic/win32.cpp @@ -61,9 +61,9 @@ String ReadFile(Allocator arena, String path) { bool success = false; String result = {}; - wchar_t wpath[1024]; - CreateWidecharFromChar(wpath, Lengthof(wpath), path.data, path.len); - HANDLE handle = CreateFileW(wpath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + Scratch scratch(arena); + String16 string16 = ToString16(scratch, path); + HANDLE handle = CreateFileW(string16.data, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (handle != INVALID_HANDLE_VALUE) { LARGE_INTEGER file_size; if (GetFileSizeEx(handle, &file_size)) { @@ -96,10 +96,10 @@ typedef struct Win32_FileIter { } Win32_FileIter; String GetAbsolutePath(Allocator arena, String relative) { - wchar_t wpath[1024]; - CreateWidecharFromChar(wpath, Lengthof(wpath), relative.data, relative.len); - wchar_t wpath_abs[1024]; - DWORD written = GetFullPathNameW((wchar_t *)wpath, Lengthof(wpath_abs), wpath_abs, 0); + Scratch scratch(arena); + String16 wpath = ToString16(scratch, relative); + wchar_t *wpath_abs = AllocArray(scratch, wchar_t, 4096); + DWORD written = GetFullPathNameW(wpath.data, 4096, wpath_abs, 0); if (written == 0) return {}; String path = ToString(arena, {(wchar_t *)wpath_abs, written}); @@ -186,13 +186,14 @@ double get_time_in_micros(void) { #endif bool WriteFile(String path, String data) { - bool result = false; - wchar_t wpath[1024]; - CreateWidecharFromChar(wpath, Lengthof(wpath), path.data, path.len); + bool result = false; + + Scratch scratch; + String16 wpath = ToString16(scratch, path); DWORD access = GENERIC_WRITE; 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) { DWORD bytes_written = 0; 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) { - wchar_t wbuffer[1024]; - DWORD wsize = GetModuleFileNameW(0, wbuffer, Lengthof(wbuffer)); + Scratch scratch(allocator); + wchar_t *wbuffer = AllocArray(scratch, wchar_t, 4096); + DWORD wsize = GetModuleFileNameW(0, wbuffer, 4096); Assert(wsize != 0); String path = ToString(allocator, wbuffer, wsize); @@ -227,31 +229,32 @@ String GetExeDir(Allocator allocator) { } bool FileExists(String path) { - wchar_t wbuff[1024]; - CreateWidecharFromChar(wbuff, Lengthof(wbuff), path.data, path.len); - DWORD attribs = GetFileAttributesW(wbuff); - bool result = attribs == INVALID_FILE_ATTRIBUTES ? false : true; + Scratch scratch; + String16 wbuff = ToString16(scratch, path); + DWORD attribs = GetFileAttributesW(wbuff.data); + bool result = attribs == INVALID_FILE_ATTRIBUTES ? false : true; return result; } bool IsDir(String path) { - wchar_t wbuff[1024]; - CreateWidecharFromChar(wbuff, Lengthof(wbuff), path.data, path.len); - DWORD dwAttrib = GetFileAttributesW(wbuff); + Scratch scratch; + String16 wbuff = ToString16(scratch, path); + DWORD dwAttrib = GetFileAttributesW(wbuff.data); return dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY); } bool IsFile(String path) { - wchar_t wbuff[1024]; - CreateWidecharFromChar(wbuff, Lengthof(wbuff), path.data, path.len); - DWORD dwAttrib = GetFileAttributesW(wbuff); - bool is_file = (dwAttrib & FILE_ATTRIBUTE_DIRECTORY) == 0; + Scratch scratch; + String16 wbuff = ToString16(scratch, path); + DWORD dwAttrib = GetFileAttributesW(wbuff.data); + bool is_file = (dwAttrib & FILE_ATTRIBUTE_DIRECTORY) == 0; return dwAttrib != INVALID_FILE_ATTRIBUTES && is_file; } String GetWorkingDir(Allocator arena) { - wchar_t wbuffer[1024]; - DWORD wsize = GetCurrentDirectoryW(Lengthof(wbuffer), wbuffer); + Scratch scratch(arena); + wchar_t *wbuffer = AllocArray(scratch, wchar_t, 4096); + DWORD wsize = GetCurrentDirectoryW(4096, wbuffer); Assert(wsize != 0); wbuffer[wsize] = 0; @@ -266,9 +269,10 @@ bool IsAbsolute(String path) { } bool DeleteFile(String path) { - wchar_t wpath[1024]; - CreateWidecharFromChar(wpath, Lengthof(wpath), path.data, path.len); - BOOL success = DeleteFileW(wpath); + Scratch scratch; + String16 wpath = ToString16(scratch, path); + + BOOL success = DeleteFileW(wpath.data); bool result = true; if (success == 0) result = false; return result; diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index f83404c..af7e9d2 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -1,7 +1,6 @@ - Remove pointers and use ViewIDs (enable array debug while doing this) - try using git grep for search for now, combine with fuzzy search buffer - lua maybe try heuristic matching paths from left? -- Append to console and change console directory - win32: change all stack buffers to arena - search as a command to execute which is going to be in the title bar