Get env stuff working on Windows

This commit is contained in:
gingerBill
2022-02-21 12:35:52 +00:00
parent 14cb19c2df
commit 345032f804
6 changed files with 112 additions and 86 deletions
+44 -28
View File
@@ -1,50 +1,56 @@
//+private
package os2
import "core:runtime"
import "core:mem"
import win32 "core:sys/windows"
_lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) {
_get_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) {
if key == "" {
return
}
wkey := win32.utf8_to_wstring(key)
b := make([dynamic]u16, 100, context.temp_allocator)
for {
n := win32.GetEnvironmentVariableW(wkey, raw_data(b), u32(len(b)))
if n == 0 {
err := win32.GetLastError()
if err == win32.ERROR_ENVVAR_NOT_FOUND {
return "", false
}
}
if n <= u32(len(b)) {
value = win32.utf16_to_utf8(b[:n], allocator)
found = true
return
}
resize(&b, len(b)*2)
// https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-getenvironmentvariablew
buf_len := win32.GetEnvironmentVariableW(wkey, nil, 0)
if buf_len == 0 {
return
}
buf := make([dynamic]u16, buf_len, context.temp_allocator)
n := win32.GetEnvironmentVariableW(wkey, raw_data(buf), buf_len)
if n == 0 {
if win32.GetLastError() == win32.ERROR_ENVVAR_NOT_FOUND {
return "", false
}
value = ""
found = true
return
}
value = win32.utf16_to_utf8(buf[:n], allocator)
found = true
return
}
_set_env :: proc(key, value: string) -> bool {
k := win32.utf8_to_wstring(key)
v := win32.utf8_to_wstring(value)
// https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-setenvironmentvariablew
return bool(win32.SetEnvironmentVariableW(k, v))
}
_unset_env :: proc(key: string) -> bool {
k := win32.utf8_to_wstring(key)
// https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-setenvironmentvariablew
return bool(win32.SetEnvironmentVariableW(k, nil))
}
_clear_env :: proc() {
envs := environ(context.temp_allocator)
for env in envs {
for j in 1..<len(env) {
#no_bounds_check for j in 1..<len(env) {
if env[j] == '=' {
unset_env(env[0:j])
break
@@ -54,23 +60,33 @@ _clear_env :: proc() {
}
_environ :: proc(allocator := context.allocator) -> []string {
envs := win32.GetEnvironmentStringsW()
envs := ([^]u16)(win32.GetEnvironmentStringsW())
if envs == nil {
return nil
}
defer win32.FreeEnvironmentStringsW(envs)
r := make([dynamic]string, 0, 50, allocator)
for from, i, p := 0, 0, envs; true; i += 1 {
c := (^u16)(uintptr(p) + uintptr(i*2))^
if c == 0 {
if i <= from {
break
length := 0
n := 0
count_loop: for {
if envs[length] == 0 {
n += 1
if envs[length+1] == 0 {
break count_loop
}
w := mem.slice_ptr(mem.ptr_offset(p, from), i-from)
}
append(&r, win32.utf16_to_utf8(w, allocator))
from = i + 1
length += 1
}
r := make([dynamic]string, 0, n, allocator)
for offset, i := 0, 0; i < length && len(r) < n; i += 1 {
c := envs[i]
if c == 0 {
wstr := envs[offset:i]
append(&r, win32.utf16_to_utf8(wstr, allocator))
i += 1
offset = i
}
}