|
@@ -8,7 +8,7 @@ import "base:runtime"
|
|
|
// Otherwise the returned value will be empty and the boolean will be false
|
|
|
// NOTE: the value will be allocated with the supplied allocator
|
|
|
@(require_results)
|
|
|
-lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) {
|
|
|
+lookup_env_alloc :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) {
|
|
|
if key == "" {
|
|
|
return
|
|
|
}
|
|
@@ -29,17 +29,77 @@ lookup_env :: proc(key: string, allocator := context.allocator) -> (value: strin
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+// This version of `lookup_env` doesn't allocate and instead requires the user to provide a buffer.
|
|
|
+// Note that it is limited to environment names and values of 512 utf-16 values each
|
|
|
+// due to the necessary utf-8 <> utf-16 conversion.
|
|
|
+@(require_results)
|
|
|
+lookup_env_buffer :: proc(buf: []u8, key: string) -> (value: string, err: Error) {
|
|
|
+ key_buf: [513]u16
|
|
|
+ n1 := win32.MultiByteToWideChar(win32.CP_UTF8, win32.MB_ERR_INVALID_CHARS, raw_data(key), i32(len(key)), nil, 0)
|
|
|
+ if n1 == 0 {
|
|
|
+ return "", nil
|
|
|
+ }
|
|
|
+
|
|
|
+ n1 = win32.MultiByteToWideChar(win32.CP_UTF8, win32.MB_ERR_INVALID_CHARS, raw_data(key), i32(len(key)), raw_data(key_buf[:]), n1)
|
|
|
+ if n1 == 0 {
|
|
|
+ return "", nil
|
|
|
+ }
|
|
|
+
|
|
|
+ n2 := win32.GetEnvironmentVariableW(raw_data(key_buf[:]), nil, 0)
|
|
|
+ if n2 == 0 && get_last_error() == ERROR_ENVVAR_NOT_FOUND {
|
|
|
+ return "", .Env_Var_Not_Found
|
|
|
+ }
|
|
|
+
|
|
|
+ val_buf: [513]u16
|
|
|
+ n2 = win32.GetEnvironmentVariableW(raw_data(key_buf[:]), raw_data(val_buf[:]), u32(len(val_buf[:])))
|
|
|
+ if n2 == 0 && get_last_error() == ERROR_ENVVAR_NOT_FOUND {
|
|
|
+ return "", .Env_Var_Not_Found
|
|
|
+ } else if int(n2) > len(buf) {
|
|
|
+ return "", .Buffer_Full
|
|
|
+ }
|
|
|
+
|
|
|
+ n3 := win32.WideCharToMultiByte(win32.CP_UTF8, win32.WC_ERR_INVALID_CHARS, raw_data(val_buf[:]), -1, nil, 0, nil, nil)
|
|
|
+ if n3 == 0 {
|
|
|
+ return
|
|
|
+ } else if int(n3) > len(buf) {
|
|
|
+ return "", .Buffer_Full
|
|
|
+ }
|
|
|
+
|
|
|
+ n4 := win32.WideCharToMultiByte(win32.CP_UTF8, win32.WC_ERR_INVALID_CHARS, raw_data(val_buf[:]), -1, raw_data(buf), n3, nil, nil)
|
|
|
+ if n4 == 0 {
|
|
|
+ return
|
|
|
+ } else if int(n4) > len(buf) {
|
|
|
+ return "", .Buffer_Full
|
|
|
+ }
|
|
|
+
|
|
|
+ for i in 0..<n3 {
|
|
|
+ if buf[i] == 0 {
|
|
|
+ n3 = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return string(buf[:n3]), nil
|
|
|
+}
|
|
|
+lookup_env :: proc{lookup_env_alloc, lookup_env_buffer}
|
|
|
|
|
|
// get_env retrieves the value of the environment variable named by the key
|
|
|
// It returns the value, which will be empty if the variable is not present
|
|
|
// To distinguish between an empty value and an unset value, use lookup_env
|
|
|
// NOTE: the value will be allocated with the supplied allocator
|
|
|
@(require_results)
|
|
|
-get_env :: proc(key: string, allocator := context.allocator) -> (value: string) {
|
|
|
+get_env_alloc :: proc(key: string, allocator := context.allocator) -> (value: string) {
|
|
|
value, _ = lookup_env(key, allocator)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+@(require_results)
|
|
|
+get_env_buf :: proc(buf: []u8, key: string) -> (value: string) {
|
|
|
+ value, _ = lookup_env(buf, key)
|
|
|
+ return
|
|
|
+}
|
|
|
+get_env :: proc{get_env_alloc, get_env_buf}
|
|
|
+
|
|
|
+
|
|
|
// set_env sets the value of the environment variable named by the key
|
|
|
set_env :: proc(key, value: string) -> Error {
|
|
|
k := win32.utf8_to_wstring(key)
|