mirror of
https://github.com/Ed94/Odin.git
synced 2026-06-25 23:14:59 -07:00
Factor out into helper.
This commit is contained in:
+44
-176
@@ -4,256 +4,102 @@ package os2
|
||||
import "base:runtime"
|
||||
|
||||
_user_cache_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
|
||||
temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
|
||||
|
||||
#partial switch ODIN_OS {
|
||||
case .Darwin:
|
||||
dir = get_env("HOME", temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, "/Library/Caches"}, allocator) or_return
|
||||
return _xdg_lookup("", "HOME", "/Library/Caches", allocator)
|
||||
case: // All other UNIX systems
|
||||
dir = get_env("XDG_CACHE_HOME", allocator)
|
||||
if dir == "" {
|
||||
dir = get_env("HOME", temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, "/.cache"}, allocator) or_return
|
||||
}
|
||||
return _xdg_lookup("XDG_CACHE_HOME", "HOME", "/.cache", allocator)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
_user_config_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
|
||||
temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
|
||||
|
||||
#partial switch ODIN_OS {
|
||||
case .Darwin:
|
||||
dir = get_env("HOME", temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, "/.config"}, allocator) or_return
|
||||
return _xdg_lookup("", "HOME", "/.config", allocator)
|
||||
case: // All other UNIX systems
|
||||
dir = get_env("XDG_CONFIG_HOME", allocator)
|
||||
if dir == "" {
|
||||
dir = get_env("HOME", temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, "/.config"}, allocator) or_return
|
||||
}
|
||||
return _xdg_lookup("XDG_CONFIG_HOME", "HOME", "/.config", allocator)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
_user_state_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
|
||||
temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
|
||||
|
||||
#partial switch ODIN_OS {
|
||||
case .Darwin:
|
||||
dir = get_env("HOME", temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, "/.local/state"}, allocator) or_return
|
||||
return _xdg_lookup("", "HOME", "/.local/state", allocator)
|
||||
case: // All other UNIX systems
|
||||
dir = get_env("XDG_STATE_HOME", allocator)
|
||||
if dir == "" {
|
||||
dir = get_env("HOME", temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, "/.local/state"}, allocator) or_return
|
||||
}
|
||||
return _xdg_lookup("XDG_STATE_HOME", "HOME", "/.local/state", allocator)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
_user_data_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
|
||||
temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
|
||||
|
||||
#partial switch ODIN_OS {
|
||||
case .Darwin:
|
||||
dir = get_env("HOME", temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, "/.local/share"}, allocator) or_return
|
||||
return _xdg_lookup("", "HOME", "/.local/share", allocator)
|
||||
case: // All other UNIX systems
|
||||
dir = get_env("XDG_DATA_HOME", allocator)
|
||||
if dir == "" {
|
||||
dir = get_env("HOME", temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, "/.local/share"}, allocator) or_return
|
||||
}
|
||||
return _xdg_lookup("XDG_DATA_HOME", "HOME", "/.local/share", allocator)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
_user_music_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
|
||||
temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
|
||||
|
||||
#partial switch ODIN_OS {
|
||||
case .Darwin:
|
||||
dir = get_env("HOME", temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, "/Music"}, allocator) or_return
|
||||
return _xdg_lookup("", "HOME", "/Music", allocator)
|
||||
case: // All other UNIX systems
|
||||
dir = get_env("XDG_MUSIC_DIR", allocator)
|
||||
if dir == "" {
|
||||
dir = get_env("HOME", temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, "/Music"}, allocator) or_return
|
||||
}
|
||||
return _xdg_lookup("XDG_MUSIC_DIR", "HOME", "/Music", allocator)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
_user_desktop_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
|
||||
temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
|
||||
|
||||
#partial switch ODIN_OS {
|
||||
case .Darwin:
|
||||
dir = get_env("HOME", temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, "/Desktop"}, allocator) or_return
|
||||
return _xdg_lookup("", "HOME", "/Desktop", allocator)
|
||||
case: // All other UNIX systems
|
||||
dir = get_env("XDG_DESKTOP_DIR", allocator)
|
||||
if dir == "" {
|
||||
dir = get_env("HOME", temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, "/Desktop"}, allocator) or_return
|
||||
}
|
||||
return _xdg_lookup("XDG_DESKTOP_DIR", "HOME", "/Desktop", allocator)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
_user_documents_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
|
||||
temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
|
||||
|
||||
#partial switch ODIN_OS {
|
||||
case .Darwin:
|
||||
dir = get_env("HOME", temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, "/Documents"}, allocator) or_return
|
||||
return _xdg_lookup("", "HOME", "/Documents", allocator)
|
||||
case: // All other UNIX systems
|
||||
dir = get_env("XDG_DOCUMENTS_DIR", allocator)
|
||||
if dir == "" {
|
||||
dir = get_env("HOME", temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, "/Documents"}, allocator) or_return
|
||||
}
|
||||
return _xdg_lookup("XDG_DOCUMENTS_DIR", "HOME", "/Documents", allocator)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
_user_downloads_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
|
||||
temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
|
||||
|
||||
#partial switch ODIN_OS {
|
||||
case .Darwin:
|
||||
dir = get_env("HOME", temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, "/Downloads"}, allocator) or_return
|
||||
return _xdg_lookup("", "HOME", "/Downloads", allocator)
|
||||
case: // All other UNIX systems
|
||||
dir = get_env("XDG_DOWNLOAD_DIR", allocator)
|
||||
if dir == "" {
|
||||
dir = get_env("HOME", temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, "/Downloads"}, allocator) or_return
|
||||
}
|
||||
return _xdg_lookup("XDG_DOWNLOAD_DIR", "HOME", "/Downloads", allocator)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
_user_pictures_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
|
||||
temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
|
||||
|
||||
#partial switch ODIN_OS {
|
||||
case .Darwin:
|
||||
dir = get_env("HOME", temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, "/Pictures"}, allocator) or_return
|
||||
return _xdg_lookup("", "HOME", "/Pictures", allocator)
|
||||
case: // All other UNIX systems
|
||||
dir = get_env("XDG_PICTURES_DIR", allocator)
|
||||
if dir == "" {
|
||||
dir = get_env("HOME", temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, "/Pictures"}, allocator) or_return
|
||||
}
|
||||
return _xdg_lookup("XDG_PICTURES_DIR", "HOME", "/Pictures", allocator)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
_user_public_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
|
||||
temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
|
||||
|
||||
#partial switch ODIN_OS {
|
||||
case .Darwin:
|
||||
dir = get_env("HOME", temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, "/Public"}, allocator) or_return
|
||||
return _xdg_lookup("", "HOME", "/Public", allocator)
|
||||
case: // All other UNIX systems
|
||||
dir = get_env("XDG_PUBLIC_DIR", allocator)
|
||||
if dir == "" {
|
||||
dir = get_env("HOME", temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, "/Public"}, allocator) or_return
|
||||
}
|
||||
return _xdg_lookup("XDG_PUBLIC_DIR", "HOME", "/Public", allocator)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
_user_videos_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
|
||||
temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
|
||||
|
||||
#partial switch ODIN_OS {
|
||||
case .Darwin:
|
||||
dir = get_env("HOME", temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, "/Movies"}, allocator) or_return
|
||||
return _xdg_lookup("", "HOME", "/Movies", allocator)
|
||||
case: // All other UNIX systems
|
||||
dir = get_env("XDG_VIDEOS_DIR", allocator)
|
||||
if dir == "" {
|
||||
dir = get_env("HOME", temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, "/Videos"}, allocator) or_return
|
||||
}
|
||||
return _xdg_lookup("XDG_VIDEOS_DIR", "HOME", "/Videos", allocator)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
_user_home_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
|
||||
@@ -261,4 +107,26 @@ _user_home_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error
|
||||
return v, nil
|
||||
}
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
|
||||
_xdg_lookup :: proc(xdg_env, fallback_env: string, fallback_suffix: string, allocator: runtime.Allocator) -> (dir: string, err: Error) {
|
||||
temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
|
||||
|
||||
if xdg_env == "" { // Darwin doesn't have XDG paths.
|
||||
dir = get_env(fallback_env, temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
return concatenate({dir, fallback_suffix}, allocator)
|
||||
} else {
|
||||
dir = get_env(xdg_env, allocator)
|
||||
if dir == "" {
|
||||
dir = get_env(fallback_env, temp_allocator)
|
||||
if dir == "" {
|
||||
return "", .Invalid_Path
|
||||
}
|
||||
dir = concatenate({dir, fallback_suffix}, allocator) or_return
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user