22package os2
33
44import " base:runtime"
5+ import " core:encoding/ini"
56import " core:strings"
67import " 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