Simplify _xdg_user_dirs_lookup

This commit is contained in:
Jeroen van Rijn
2025-06-08 14:56:41 +02:00
parent 00b67831d1
commit 77f4199af6
2 changed files with 37 additions and 57 deletions
+2
View File
@@ -28,6 +28,7 @@ General_Error :: enum u32 {
Pattern_Has_Separator, Pattern_Has_Separator,
No_HOME_Variable, No_HOME_Variable,
Wordexp_Failed,
Unsupported, Unsupported,
} }
@@ -76,6 +77,7 @@ error_string :: proc(ferr: Error) -> string {
case .Unsupported: return "unsupported" case .Unsupported: return "unsupported"
case .Pattern_Has_Separator: return "pattern has separator" case .Pattern_Has_Separator: return "pattern has separator"
case .No_HOME_Variable: return "no $HOME variable" case .No_HOME_Variable: return "no $HOME variable"
case .Wordexp_Failed: return "posix.wordexp was unable to expand"
} }
case io.Error: case io.Error:
switch e { switch e {
+16 -38
View File
@@ -2,6 +2,7 @@
package os2 package os2
import "base:runtime" import "base:runtime"
import "core:encoding/ini"
import "core:strings" import "core:strings"
import "core:sys/posix" import "core:sys/posix"
@@ -153,53 +154,30 @@ _xdg_lookup :: proc(xdg_key: string, fallback_suffix: string, allocator: runtime
// If `<config-dir>/user-dirs.dirs` doesn't exist, or `xdg_key` can't be found there: returns `""` // If `<config-dir>/user-dirs.dirs` doesn't exist, or `xdg_key` can't be found there: returns `""`
_xdg_user_dirs_lookup :: proc(xdg_key: string, allocator: runtime.Allocator) -> (dir: string, err: Error) { _xdg_user_dirs_lookup :: proc(xdg_key: string, allocator: runtime.Allocator) -> (dir: string, err: Error) {
temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator }) temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
config_dir := user_config_dir(temp_allocator) or_return config_dir := user_config_dir(temp_allocator) or_return
user_dirs_path := concatenate({config_dir, "/user-dirs.dirs"}, temp_allocator) or_return user_dirs_path := concatenate({config_dir, "/user-dirs.dirs"}, temp_allocator) or_return
user_dirs_content_bytes, read_err := read_entire_file(user_dirs_path, temp_allocator) content := read_entire_file(user_dirs_path, temp_allocator) or_return
if read_err == .Not_Exist {
return
} else if read_err != nil {
err = read_err
return
}
user_dirs_content := string(user_dirs_content_bytes)
lines := strings.split_lines(user_dirs_content, temp_allocator) or_return it := ini.Iterator{
section = "",
home_env := get_env("HOME", temp_allocator) _src = string(content),
if home_env == "" { options = ini.Options{
err = .No_HOME_Variable comment = "#",
return key_lower_case = false,
},
} }
for line in lines { for k, v in ini.iterate(&it) {
ss := strings.split_n(line, "=", 2, temp_allocator) or_return if k == xdg_key {
(len(ss) == 2) or_continue
sl := strings.trim_space(ss[0])
sr := ss[1]
(sl == xdg_key) or_continue
(len(sr) > 2) or_continue
lq := strings.index_byte(sr, '"')
(lq != -1) or_continue
rq := strings.index_byte(sr[lq+1:], '"') + lq+1
(rq != -1) or_continue
sr = sr[lq+1:rq]
we: posix.wordexp_t we: posix.wordexp_t
we_err := posix.wordexp(strings.clone_to_cstring(sr, temp_allocator), &we, nil)
(we_err == nil) or_continue
defer posix.wordfree(&we) defer posix.wordfree(&we)
(we.we_wordc == 1) or_continue if _err := posix.wordexp(strings.clone_to_cstring(v, temp_allocator), &we, nil); _err != nil || we.we_wordc != 1 {
return "", .Wordexp_Failed
}
dir = strings.clone_from_cstring(we.we_wordv[0], allocator) or_return return strings.clone_from_cstring(we.we_wordv[0], allocator)
return }
} }
return return
} }