aboutsummaryrefslogtreecommitdiff
path: root/src/common/util_windows.odin
blob: b877f4b01dfd18eab371808a226b256c38bb5fbd (plain)
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
package common

import "core:strings"
import "core:mem"
import "core:fmt"
import "core:log"

import win32 "core:sys/windows"

FORMAT_MESSAGE_FROM_SYSTEM :: 0x00001000
FORMAT_MESSAGE_IGNORE_INSERTS :: 0x00000200


foreign import kernel32 "system:kernel32.lib"

@(default_calling_convention = "std")
foreign kernel32 {
	@(link_name = "FormatMessageA")
	format_message_a :: proc(
		flags: u32,
		source: rawptr,
		message_id: u32,
		langauge_id: u32,
		buffer: cstring,
		size: u32,
		va: rawptr,
	) -> u32 ---
}

get_case_sensitive_path :: proc(path: string, allocator := context.temp_allocator) -> string {
	wide := win32.utf8_to_utf16(path)
	file := win32.CreateFileW(&wide[0], 0, win32.FILE_SHARE_READ, nil, win32.OPEN_EXISTING, win32.FILE_FLAG_BACKUP_SEMANTICS, nil)

	if(file == win32.INVALID_HANDLE)
    	{
		log_last_error()
        	return "";
    	}

	buffer := make([]u16, 512, context.temp_allocator)

	ret := win32.GetFinalPathNameByHandleW(file, &buffer[0], cast(u32)len(buffer), 0)

	res, _ := win32.utf16_to_utf8(buffer[4:], allocator)

	win32.CloseHandle(file)

	return res
}

log_last_error :: proc() {
	err_text: [512]byte

	err := win32.GetLastError()

	error_string := cstring(&err_text[0])

	if (format_message_a(
		   FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
		   nil,
		   err,
		   (1 << 10) | 0,
		   error_string,
		   len(err_text) - 1,
		   nil,
	   ) != 0) {
		log.error(error_string)
	}
}


run_executable :: proc(command: string, stdout: ^[]byte) -> (u32, bool, []byte) {
	stdout_read:  win32.HANDLE
	stdout_write: win32.HANDLE

	attributes: win32.SECURITY_ATTRIBUTES
	attributes.nLength = size_of(win32.SECURITY_ATTRIBUTES)
	attributes.bInheritHandle = true
	attributes.lpSecurityDescriptor = nil

	if win32.CreatePipe(&stdout_read, &stdout_write, &attributes, 0) == false {
		return 0, false, stdout[0:]
	}

	if !win32.SetHandleInformation(stdout_read, win32.HANDLE_FLAG_INHERIT, 0) {
		return 0, false, stdout[0:]
	}

	startup_info: win32.STARTUPINFO
	process_info: win32.PROCESS_INFORMATION

	startup_info.cb = size_of(win32.STARTUPINFO)

	startup_info.hStdError = stdout_write
	startup_info.hStdOutput = stdout_write
	startup_info.dwFlags |= win32.STARTF_USESTDHANDLES

	if !win32.CreateProcessW(nil, &win32.utf8_to_utf16(command)[0], nil, nil, true, 0, nil, nil, &startup_info, &process_info) {
		return 0, false, stdout[0:]
	}

	win32.CloseHandle(stdout_write)

	index: int
	read:  u32

	read_buffer: [50]byte

	success: win32.BOOL = true

	for success {
		success = win32.ReadFile(stdout_read, &read_buffer[0], len(read_buffer), &read, nil)

		if read > 0 && index + cast(int)read <= len(stdout) {
			mem.copy(&stdout[index], &read_buffer[0], cast(int)read)
		}

		index += cast(int)read
	}

	stdout[index + 1] = 0

	exit_code: u32

	win32.WaitForSingleObject(process_info.hProcess, win32.INFINITE)

	win32.GetExitCodeProcess(process_info.hProcess, &exit_code)

	win32.CloseHandle(stdout_read)

	return exit_code, true, stdout[0:index]
}

import "core:mem/virtual"

Growing_Arena :: virtual.Growing_Arena
growing_arena_allocator :: virtual.growing_arena_allocator