mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
Simplify _xdg_user_dirs_lookup
This commit is contained in:
+17
-15
@@ -28,6 +28,7 @@ General_Error :: enum u32 {
|
|||||||
Pattern_Has_Separator,
|
Pattern_Has_Separator,
|
||||||
|
|
||||||
No_HOME_Variable,
|
No_HOME_Variable,
|
||||||
|
Wordexp_Failed,
|
||||||
|
|
||||||
Unsupported,
|
Unsupported,
|
||||||
}
|
}
|
||||||
@@ -61,21 +62,22 @@ error_string :: proc(ferr: Error) -> string {
|
|||||||
case General_Error:
|
case General_Error:
|
||||||
switch e {
|
switch e {
|
||||||
case .None: return ""
|
case .None: return ""
|
||||||
case .Permission_Denied: return "permission denied"
|
case .Permission_Denied: return "permission denied"
|
||||||
case .Exist: return "file already exists"
|
case .Exist: return "file already exists"
|
||||||
case .Not_Exist: return "file does not exist"
|
case .Not_Exist: return "file does not exist"
|
||||||
case .Closed: return "file already closed"
|
case .Closed: return "file already closed"
|
||||||
case .Timeout: return "i/o timeout"
|
case .Timeout: return "i/o timeout"
|
||||||
case .Broken_Pipe: return "Broken pipe"
|
case .Broken_Pipe: return "Broken pipe"
|
||||||
case .No_Size: return "file has no definite size"
|
case .No_Size: return "file has no definite size"
|
||||||
case .Invalid_File: return "invalid file"
|
case .Invalid_File: return "invalid file"
|
||||||
case .Invalid_Dir: return "invalid directory"
|
case .Invalid_Dir: return "invalid directory"
|
||||||
case .Invalid_Path: return "invalid path"
|
case .Invalid_Path: return "invalid path"
|
||||||
case .Invalid_Callback: return "invalid callback"
|
case .Invalid_Callback: return "invalid callback"
|
||||||
case .Invalid_Command: return "invalid command"
|
case .Invalid_Command: return "invalid command"
|
||||||
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 {
|
||||||
|
|||||||
+20
-42
@@ -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
|
||||||
|
user_dirs_path := concatenate({config_dir, "/user-dirs.dirs"}, temp_allocator) or_return
|
||||||
|
content := read_entire_file(user_dirs_path, temp_allocator) or_return
|
||||||
|
|
||||||
config_dir := user_config_dir(temp_allocator) or_return
|
it := ini.Iterator{
|
||||||
|
section = "",
|
||||||
user_dirs_path := concatenate({config_dir, "/user-dirs.dirs"}, temp_allocator) or_return
|
_src = string(content),
|
||||||
user_dirs_content_bytes, read_err := read_entire_file(user_dirs_path, temp_allocator)
|
options = ini.Options{
|
||||||
if read_err == .Not_Exist {
|
comment = "#",
|
||||||
return
|
key_lower_case = false,
|
||||||
} 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
|
|
||||||
|
|
||||||
home_env := get_env("HOME", temp_allocator)
|
|
||||||
if home_env == "" {
|
|
||||||
err = .No_HOME_Variable
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
we: posix.wordexp_t
|
||||||
sl := strings.trim_space(ss[0])
|
defer posix.wordfree(&we)
|
||||||
sr := ss[1]
|
|
||||||
|
|
||||||
(sl == xdg_key) or_continue
|
if _err := posix.wordexp(strings.clone_to_cstring(v, temp_allocator), &we, nil); _err != nil || we.we_wordc != 1 {
|
||||||
|
return "", .Wordexp_Failed
|
||||||
|
}
|
||||||
|
|
||||||
(len(sr) > 2) or_continue
|
return strings.clone_from_cstring(we.we_wordv[0], allocator)
|
||||||
|
}
|
||||||
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_err := posix.wordexp(strings.clone_to_cstring(sr, temp_allocator), &we, nil)
|
|
||||||
(we_err == nil) or_continue
|
|
||||||
defer posix.wordfree(&we)
|
|
||||||
|
|
||||||
(we.we_wordc == 1) or_continue
|
|
||||||
|
|
||||||
dir = strings.clone_from_cstring(we.we_wordv[0], allocator) or_return
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user