Skip to content

Commit 77f4199

Browse files
committed
Simplify _xdg_user_dirs_lookup
1 parent 00b6783 commit 77f4199

File tree

2 files changed

+41
-61
lines changed

2 files changed

+41
-61
lines changed

core/os/os2/errors.odin

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ General_Error :: enum u32 {
2828
Pattern_Has_Separator,
2929

3030
No_HOME_Variable,
31+
Wordexp_Failed,
3132

3233
Unsupported,
3334
}
@@ -61,21 +62,22 @@ error_string :: proc(ferr: Error) -> string {
6162
case General_Error:
6263
switch e {
6364
case .None: return ""
64-
case .Permission_Denied: return "permission denied"
65-
case .Exist: return "file already exists"
66-
case .Not_Exist: return "file does not exist"
67-
case .Closed: return "file already closed"
68-
case .Timeout: return "i/o timeout"
69-
case .Broken_Pipe: return "Broken pipe"
70-
case .No_Size: return "file has no definite size"
71-
case .Invalid_File: return "invalid file"
72-
case .Invalid_Dir: return "invalid directory"
73-
case .Invalid_Path: return "invalid path"
74-
case .Invalid_Callback: return "invalid callback"
75-
case .Invalid_Command: return "invalid command"
76-
case .Unsupported: return "unsupported"
77-
case .Pattern_Has_Separator: return "pattern has separator"
78-
case .No_HOME_Variable: return "no $HOME variable"
65+
case .Permission_Denied: return "permission denied"
66+
case .Exist: return "file already exists"
67+
case .Not_Exist: return "file does not exist"
68+
case .Closed: return "file already closed"
69+
case .Timeout: return "i/o timeout"
70+
case .Broken_Pipe: return "Broken pipe"
71+
case .No_Size: return "file has no definite size"
72+
case .Invalid_File: return "invalid file"
73+
case .Invalid_Dir: return "invalid directory"
74+
case .Invalid_Path: return "invalid path"
75+
case .Invalid_Callback: return "invalid callback"
76+
case .Invalid_Command: return "invalid command"
77+
case .Unsupported: return "unsupported"
78+
case .Pattern_Has_Separator: return "pattern has separator"
79+
case .No_HOME_Variable: return "no $HOME variable"
80+
case .Wordexp_Failed: return "posix.wordexp was unable to expand"
7981
}
8082
case io.Error:
8183
switch e {

core/os/os2/user_posix.odin

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package os2
33

44
import "base:runtime"
5+
import "core:encoding/ini"
56
import "core:strings"
67
import "core:sys/posix"
78

@@ -153,53 +154,30 @@ _xdg_lookup :: proc(xdg_key: string, fallback_suffix: string, allocator: runtime
153154
// If `<config-dir>/user-dirs.dirs` doesn't exist, or `xdg_key` can't be found there: returns `""`
154155
_xdg_user_dirs_lookup :: proc(xdg_key: string, allocator: runtime.Allocator) -> (dir: string, err: Error) {
155156
temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
157+
config_dir := user_config_dir(temp_allocator) or_return
158+
user_dirs_path := concatenate({config_dir, "/user-dirs.dirs"}, temp_allocator) or_return
159+
content := read_entire_file(user_dirs_path, temp_allocator) or_return
160+
161+
it := ini.Iterator{
162+
section = "",
163+
_src = string(content),
164+
options = ini.Options{
165+
comment = "#",
166+
key_lower_case = false,
167+
},
168+
}
169+
170+
for k, v in ini.iterate(&it) {
171+
if k == xdg_key {
172+
we: posix.wordexp_t
173+
defer posix.wordfree(&we)
174+
175+
if _err := posix.wordexp(strings.clone_to_cstring(v, temp_allocator), &we, nil); _err != nil || we.we_wordc != 1 {
176+
return "", .Wordexp_Failed
177+
}
156178

157-
config_dir := user_config_dir(temp_allocator) or_return
158-
159-
user_dirs_path := concatenate({config_dir, "/user-dirs.dirs"}, temp_allocator) or_return
160-
user_dirs_content_bytes, read_err := read_entire_file(user_dirs_path, temp_allocator)
161-
if read_err == .Not_Exist {
162-
return
163-
} else if read_err != nil {
164-
err = read_err
165-
return
166-
}
167-
user_dirs_content := string(user_dirs_content_bytes)
168-
169-
lines := strings.split_lines(user_dirs_content, temp_allocator) or_return
170-
171-
home_env := get_env("HOME", temp_allocator)
172-
if home_env == "" {
173-
err = .No_HOME_Variable
174-
return
175-
}
176-
177-
for line in lines {
178-
ss := strings.split_n(line, "=", 2, temp_allocator) or_return
179-
(len(ss) == 2) or_continue
180-
sl := strings.trim_space(ss[0])
181-
sr := ss[1]
182-
183-
(sl == xdg_key) or_continue
184-
185-
(len(sr) > 2) or_continue
186-
187-
lq := strings.index_byte(sr, '"')
188-
(lq != -1) or_continue
189-
rq := strings.index_byte(sr[lq+1:], '"') + lq+1
190-
(rq != -1) or_continue
191-
192-
sr = sr[lq+1:rq]
193-
194-
we: posix.wordexp_t
195-
we_err := posix.wordexp(strings.clone_to_cstring(sr, temp_allocator), &we, nil)
196-
(we_err == nil) or_continue
197-
defer posix.wordfree(&we)
198-
199-
(we.we_wordc == 1) or_continue
200-
201-
dir = strings.clone_from_cstring(we.we_wordv[0], allocator) or_return
202-
return
179+
return strings.clone_from_cstring(we.we_wordv[0], allocator)
180+
}
203181
}
204182
return
205183
}

0 commit comments

Comments
 (0)