mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 23:58:50 +00:00
match user.odin and env.odin to master
This commit is contained in:
+12
-3
@@ -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
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user