diff --git a/core/os/os2/env.odin b/core/os/os2/env.odin index f1a3e40c7..f25290a59 100644 --- a/core/os/os2/env.odin +++ b/core/os/os2/env.odin @@ -1,11 +1,20 @@ package os2 -// get_env gets the value of the environment variable named by the key +// 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 +get_env :: proc(key: string, allocator := context.allocator) -> string { + value, _ := lookup_env(key, allocator) + return value +} + +// lookup_env gets the value of the environment variable named by the key // If the variable is found in the environment the value (which can be empty) is returned and the boolean is true // Otherwise the returned value will be empty and the boolean will be false // NOTE: the value will be allocated with the supplied allocator -get_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) { - return _get_env(key, allocator) +lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) { + return _lookup_env(key, allocator) } // set_env sets the value of the environment variable named by the key diff --git a/core/os/os2/user.odin b/core/os/os2/user.odin index 14c0ce961..1fb653b85 100644 --- a/core/os/os2/user.odin +++ b/core/os/os2/user.odin @@ -4,58 +4,56 @@ import "core:strings" import "core:runtime" user_cache_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { - found: bool #partial switch ODIN_OS { case .Windows: - dir, found = get_env("LocalAppData") - if found { + dir = get_env("LocalAppData") + if dir != "" { dir = strings.clone_safe(dir, allocator) or_return } case .Darwin: - dir, found = get_env("HOME") - if found { + dir = get_env("HOME") + if dir != "" { dir = strings.concatenate_safe({dir, "/Library/Caches"}, allocator) or_return } case: // All other UNIX systems - dir, found = get_env("XDG_CACHE_HOME") - if found { - dir, found = get_env("HOME") - if !found { + dir = get_env("XDG_CACHE_HOME") + if dir == "" { + dir = get_env("HOME") + if dir == "" { return } dir = strings.concatenate_safe({dir, "/.cache"}, allocator) or_return } } - if !found || dir == "" { + if dir == "" { err = .Invalid_Path } return } user_config_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { - found: bool #partial switch ODIN_OS { case .Windows: - dir, found = get_env("AppData") - if found { + dir = get_env("AppData") + if dir != "" { dir = strings.clone_safe(dir, allocator) or_return } case .Darwin: - dir, found = get_env("HOME") - if found { + dir = get_env("HOME") + if dir != "" { dir = strings.concatenate_safe({dir, "/Library/Application Support"}, allocator) or_return } case: // All other UNIX systems - dir, found = get_env("XDG_CACHE_HOME") - if !found { - dir, found = get_env("HOME") - if !found { + dir = get_env("XDG_CACHE_HOME") + if dir == "" { + dir = get_env("HOME") + if dir == "" { return } dir = strings.concatenate_safe({dir, "/.config"}, allocator) or_return } } - if !found || dir == "" { + if dir == "" { err = .Invalid_Path } return @@ -67,7 +65,7 @@ user_home_dir :: proc() -> (dir: string, err: Error) { case .Windows: env = "USERPROFILE" } - if v, found := get_env(env); found { + if v := get_env(env); v != "" { return v, nil } return "", .Invalid_Path