summaryrefslogtreecommitdiffstats
path: root/source/base/base_platform_win32.cpp
blob: b706bebaf23c19aba01763374902afb02e8158e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//- 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;
}