match user.odin and env.odin to master

This commit is contained in:
jason
2022-05-16 15:28:56 -04:00
parent 43432f92ec
commit 5a6836ab99
2 changed files with 31 additions and 24 deletions
+12 -3
View File
@@ -1,11 +1,20 @@
package os2 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 // 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 // Otherwise the returned value will be empty and the boolean will be false
// NOTE: the value will be allocated with the supplied allocator // NOTE: the value will be allocated with the supplied allocator
get_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) { lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) {
return _get_env(key, allocator) return _lookup_env(key, allocator)
} }
// set_env sets the value of the environment variable named by the key // set_env sets the value of the environment variable named by the key
+19 -21
View File
@@ -4,58 +4,56 @@ import "core:strings"
import "core:runtime" import "core:runtime"
user_cache_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { user_cache_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
found: bool
#partial switch ODIN_OS { #partial switch ODIN_OS {
case .Windows: case .Windows:
dir, found = get_env("LocalAppData") dir = get_env("LocalAppData")
if found { if dir != "" {
dir = strings.clone_safe(dir, allocator) or_return dir = strings.clone_safe(dir, allocator) or_return
} }
case .Darwin: case .Darwin:
dir, found = get_env("HOME") dir = get_env("HOME")
if found { if dir != "" {
dir = strings.concatenate_safe({dir, "/Library/Caches"}, allocator) or_return dir = strings.concatenate_safe({dir, "/Library/Caches"}, allocator) or_return
} }
case: // All other UNIX systems case: // All other UNIX systems
dir, found = get_env("XDG_CACHE_HOME") dir = get_env("XDG_CACHE_HOME")
if found { if dir == "" {
dir, found = get_env("HOME") dir = get_env("HOME")
if !found { if dir == "" {
return return
} }
dir = strings.concatenate_safe({dir, "/.cache"}, allocator) or_return dir = strings.concatenate_safe({dir, "/.cache"}, allocator) or_return
} }
} }
if !found || dir == "" { if dir == "" {
err = .Invalid_Path err = .Invalid_Path
} }
return return
} }
user_config_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { user_config_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
found: bool
#partial switch ODIN_OS { #partial switch ODIN_OS {
case .Windows: case .Windows:
dir, found = get_env("AppData") dir = get_env("AppData")
if found { if dir != "" {
dir = strings.clone_safe(dir, allocator) or_return dir = strings.clone_safe(dir, allocator) or_return
} }
case .Darwin: case .Darwin:
dir, found = get_env("HOME") dir = get_env("HOME")
if found { if dir != "" {
dir = strings.concatenate_safe({dir, "/Library/Application Support"}, allocator) or_return dir = strings.concatenate_safe({dir, "/Library/Application Support"}, allocator) or_return
} }
case: // All other UNIX systems case: // All other UNIX systems
dir, found = get_env("XDG_CACHE_HOME") dir = get_env("XDG_CACHE_HOME")
if !found { if dir == "" {
dir, found = get_env("HOME") dir = get_env("HOME")
if !found { if dir == "" {
return return
} }
dir = strings.concatenate_safe({dir, "/.config"}, allocator) or_return dir = strings.concatenate_safe({dir, "/.config"}, allocator) or_return
} }
} }
if !found || dir == "" { if dir == "" {
err = .Invalid_Path err = .Invalid_Path
} }
return return
@@ -67,7 +65,7 @@ user_home_dir :: proc() -> (dir: string, err: Error) {
case .Windows: case .Windows:
env = "USERPROFILE" env = "USERPROFILE"
} }
if v, found := get_env(env); found { if v := get_env(env); v != "" {
return v, nil return v, nil
} }
return "", .Invalid_Path return "", .Invalid_Path