//- utf-8 <-> utf-16 internal int win32_utf8_to_wide(const char *utf8, s64 utf8_size, WCHAR *out, int out_count) { if(out_count <= 0) return 0; int length = MultiByteToWideChar(CP_UTF8, 0, utf8, (int)utf8_size, out, out_count - 1); if(length < 0) length = 0; // NOTE: with utf8_size == -1 the terminator is counted (and written) too if(utf8_size < 0 && length > 0) --length; out[length] = 0; return length; } internal int win32_wide_to_utf8(const WCHAR *wide, s64 wide_count, char *out, int out_size) { if(out_size <= 0) return 0; int length = WideCharToMultiByte(CP_UTF8, 0, wide, (int)wide_count, out, out_size - 1, NULL, NULL); if(length < 0) length = 0; if(wide_count < 0 && length > 0) --length; out[length] = 0; return length; } // NOTE: String8 paths aren't terminated, convert straight from the view internal void win32_path_wide(String8 path, WCHAR *out, int out_count) { win32_utf8_to_wide((const char *)path.data, (s64)path.size, out, out_count); } //- args internal char **win32_utf8_args(int *argc, char **argv) { int count = 0; LPWSTR *wide = CommandLineToArgvW(GetCommandLineW(), &count); if(!wide) return argv; char **out = (char **)calloc((size_t)count + 1, sizeof(char *)); for(int index = 0; index < count; ++index) { int size = WideCharToMultiByte(CP_UTF8, 0, wide[index], -1, NULL, 0, NULL, NULL); out[index] = (char *)calloc((size_t)Max(size, 1), 1); WideCharToMultiByte(CP_UTF8, 0, wide[index], -1, out[index], size, NULL, NULL); } LocalFree(wide); *argc = count; return out; } //- logging internal void win32_log_with_args(const char *format, ...) { char buffer[4096]; va_list arguments; va_start(arguments, format); int length = vsnprintf(buffer, sizeof(buffer) - 2, format, arguments); va_end(arguments); if(length < 0) return; if((u64)length > sizeof(buffer) - 2) length = (int)sizeof(buffer) - 2; buffer[length++] = '\n'; buffer[length] = 0; // NOTE: stdout when there is one (console build, or redirected), the debugger always fwrite(buffer, 1, (size_t)length, stdout); fflush(stdout); OutputDebugStringA(buffer); } internal void win32_os_exit(int code) { ExitProcess((UINT)code); } //- shared libraries internal SharedLibrary win32_load_shared_library(Arena *arena, String8 path) { SharedLibrary lib = {0}; WCHAR wide[MAX_PATH * 2]; win32_path_wide(path, wide, ArrayCount(wide)); lib.mem = (void *)LoadLibraryW(wide); AssertLog(lib.mem, "failed to load library " Str8Fmt " (%lu)", Str8Arg(path), GetLastError()); return lib; } internal void *win32_shared_library_get_proc_from_name(SharedLibrary *library, const char *name) { void *symbol = (void *)GetProcAddress((HMODULE)library->mem, name); AssertLog(symbol, "failed to load symbol %s (%lu)", name, GetLastError()); return symbol; } internal int win32_shared_library_close(SharedLibrary *library) { b32 ok = FreeLibrary((HMODULE)library->mem); AssertLog(ok, "failed to close the library (%lu)", GetLastError()); library->mem = NULL; return ok ? 0 : -1; } //- files internal b32 win32_file_exists(Arena *arena, String8 path) { WCHAR wide[MAX_PATH * 2]; win32_path_wide(path, wide, ArrayCount(wide)); DWORD attributes = GetFileAttributesW(wide); return attributes != INVALID_FILE_ATTRIBUTES && !(attributes & FILE_ATTRIBUTE_DIRECTORY); } internal b32 win32_delete_file(Arena *arena, String8 path) { WCHAR wide[MAX_PATH * 2]; win32_path_wide(path, wide, ArrayCount(wide)); return DeleteFileW(wide) != 0; } internal String8 win32_file_load(Arena *arena, String8 path) { if(!path.data || path.size == 0) { Log("FileLoad: empty path"); return str8_zero(); } WCHAR wide[MAX_PATH * 2]; win32_path_wide(path, wide, ArrayCount(wide)); HANDLE file = CreateFileW(wide, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(file == INVALID_HANDLE_VALUE) { Log("File was not loaded: " Str8Fmt, Str8Arg(path)); return str8_zero(); } LARGE_INTEGER size = {}; if(!GetFileSizeEx(file, &size)) { CloseHandle(file); return str8_zero(); } u64 file_size = (u64)size.QuadPart; String8 result = PushString8(arena, file_size); u64 total = 0; while(total < file_size) { DWORD chunk = (DWORD)Min(file_size - total, (u64)0x40000000ull); DWORD got = 0; if(!ReadFile(file, result.data + total, chunk, &got, NULL)) { Log("Failed, reading file was interrupted"); CloseHandle(file); return str8_zero(); } if(got == 0) break; total += got; } result.size = total; CloseHandle(file); return result; } internal b32 win32_file_write(String8 data, String8 path) { WCHAR wide[MAX_PATH * 2]; win32_path_wide(path, wide, ArrayCount(wide)); HANDLE file = CreateFileW(wide, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if(file == INVALID_HANDLE_VALUE) { return false; } u64 written = 0; while(written < data.size) { DWORD chunk = (DWORD)Min(data.size - written, (u64)0x40000000ull); DWORD wrote = 0; if(!WriteFile(file, data.data + written, chunk, &wrote, NULL)) { CloseHandle(file); return false; } written += wrote; } CloseHandle(file); return true; } internal String8 win32_read_self(Arena *arena) { WCHAR wide[MAX_PATH * 2]; DWORD length = GetModuleFileNameW(NULL, wide, ArrayCount(wide)); AssertLog(length > 0, "GetModuleFileNameW failed (%lu)", GetLastError()); char utf8[MAX_PATH * 4]; int size = win32_wide_to_utf8(wide, (s64)length, utf8, sizeof(utf8)); String8 view = {(u8 *)utf8, (u64)size}; return str8_clone(arena, &view); } // NOTE: regular files only, like linux_open_dir internal String8List *win32_open_dir(Arena *arena, String8 *path) { String8List *files_list = PushStructZero(arena, String8List); files_list->first = &nil_str8_node; files_list->last = &nil_str8_node; WCHAR pattern[MAX_PATH * 2]; int length = win32_utf8_to_wide((const char *)path->data, (s64)path->size, pattern, ArrayCount(pattern) - 3); if(length > 0 && pattern[length - 1] != L'\\' && pattern[length - 1] != L'/') pattern[length++] = L'\\'; pattern[length++] = L'*'; pattern[length] = 0; WIN32_FIND_DATAW data; HANDLE find = FindFirstFileW(pattern, &data); if(find == INVALID_HANDLE_VALUE) { Log("FindFirstFileW(" Str8Fmt ") failed", Str8Arg(*path)); return files_list; } do { if(data.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE)) continue; char utf8[MAX_PATH * 4]; int size = win32_wide_to_utf8(data.cFileName, -1, utf8, sizeof(utf8)); String8 name = {(u8 *)utf8, (u64)size}; String8Node *file_node = PushStruct(arena, String8Node); *file_node = ToString8Node(str8_clone(arena, &name)); str8_list_push_node(files_list, file_node); } while(FindNextFileW(find, &data)); FindClose(find); return files_list; }