|
1 | 1 | submodule(stdlib_system) stdlib_system_path
|
2 | 2 | use stdlib_ascii, only: reverse
|
3 |
| - use stdlib_strings, only: chomp, join |
| 3 | + use stdlib_strings, only: chomp, join, starts_with |
4 | 4 | use stdlib_string_type, only: string_type, char, move
|
5 | 5 | contains
|
6 | 6 | module function join2_char_char(p1, p2) result(path)
|
@@ -167,4 +167,58 @@ module function dir_name_string(p) result(dir)
|
167 | 167 |
|
168 | 168 | call split_path(p, dir, temp)
|
169 | 169 | end function dir_name_string
|
| 170 | + |
| 171 | + module logical function is_abs_char(p) |
| 172 | + character(len=*), intent(in) :: p |
| 173 | + character(len=1) :: sep |
| 174 | + |
| 175 | + sep = path_sep() |
| 176 | + |
| 177 | + if (sep == '/') then |
| 178 | + ! should start with '/' |
| 179 | + is_abs_char = starts_with(p, sep) |
| 180 | + else |
| 181 | + ! should be either an UNC path like '\\server\host...' |
| 182 | + ! or should be starting with a drive letter like 'C:\Users\...' |
| 183 | + is_abs_char = starts_with(p(2:), ':\') .or. starts_with(p, '\\') |
| 184 | + end if |
| 185 | + end function is_abs_char |
| 186 | + |
| 187 | + module logical function is_abs_string(p) |
| 188 | + type(string_type), intent(in) :: p |
| 189 | + |
| 190 | + is_abs_string = is_abs(char(p)) |
| 191 | + end function is_abs_string |
| 192 | + |
| 193 | + module function abs_path_char(p, err) result(abs_p) |
| 194 | + character(len=*), intent(in) :: p |
| 195 | + type(state_type), optional, intent(out) :: err |
| 196 | + character(len=:), allocatable :: abs_p |
| 197 | + |
| 198 | + type(state_type) :: err0 |
| 199 | + character(:), allocatable :: cwd |
| 200 | + |
| 201 | + ! get the current working directory |
| 202 | + call get_cwd(cwd, err0) |
| 203 | + |
| 204 | + if (err0%error()) then |
| 205 | + abs_p = '' |
| 206 | + call err0%handle(err) |
| 207 | + end if |
| 208 | + |
| 209 | + ! join the cwd and path |
| 210 | + abs_p = cwd / p |
| 211 | + end function abs_path_char |
| 212 | + |
| 213 | + module function abs_path_string(p, err) result(abs_p) |
| 214 | + type(string_type), intent(in) :: p |
| 215 | + type(state_type), optional, intent(out) :: err |
| 216 | + type(string_type) :: abs_p |
| 217 | + |
| 218 | + character(len=:), allocatable :: res |
| 219 | + |
| 220 | + res = abs_path(char(p), err) |
| 221 | + |
| 222 | + call move(res, abs_p) |
| 223 | + end function abs_path_string |
170 | 224 | end submodule stdlib_system_path
|
0 commit comments