|
| 1 | +submodule(stdlib_system) stdlib_system_path |
| 2 | + use stdlib_ascii, only: reverse |
| 3 | + use stdlib_strings, only: chomp, find, join |
| 4 | +contains |
| 5 | + module pure function join2(p1, p2) result(path) |
| 6 | + character(:), allocatable :: path |
| 7 | + character(*), intent(in) :: p1, p2 |
| 8 | + |
| 9 | + path = trim(p1) // pathsep // trim(p2) |
| 10 | + end function join2 |
| 11 | + |
| 12 | + module pure function joinarr(p) result(path) |
| 13 | + character(:), allocatable :: path |
| 14 | + character(*), intent(in) :: p(:) |
| 15 | + |
| 16 | + path = join(p, pathsep) |
| 17 | + end function joinarr |
| 18 | + |
| 19 | + module pure function join_op(p1, p2) result(path) |
| 20 | + character(:), allocatable :: path |
| 21 | + character(*), intent(in) :: p1, p2 |
| 22 | + |
| 23 | + path = joinpath(p1, p2) |
| 24 | + end function join_op |
| 25 | + |
| 26 | + module subroutine splitpath(p, head, tail) |
| 27 | + character(*), intent(in) :: p |
| 28 | + character(:), allocatable, intent(out) :: head, tail |
| 29 | + character(:), allocatable :: temp |
| 30 | + integer :: i |
| 31 | + |
| 32 | + ! Empty string, return (.,'') |
| 33 | + if (trim(p) == '') then |
| 34 | + head = '.' |
| 35 | + tail = '' |
| 36 | + return |
| 37 | + end if |
| 38 | + |
| 39 | + ! Remove trailing path separators |
| 40 | + temp = trim(chomp(trim(p), pathsep)) |
| 41 | + |
| 42 | + if (temp == '') then |
| 43 | + head = pathsep |
| 44 | + tail = '' |
| 45 | + return |
| 46 | + end if |
| 47 | + |
| 48 | + i = find(reverse(temp), pathsep) |
| 49 | + |
| 50 | + ! if no `pathsep`, then it probably was a root dir like `C:\` |
| 51 | + if (i == 0) then |
| 52 | + head = temp // pathsep |
| 53 | + tail = '' |
| 54 | + return |
| 55 | + end if |
| 56 | + |
| 57 | + head = temp(:len(temp)-i) |
| 58 | + |
| 59 | + if (head == '') then |
| 60 | + head = pathsep |
| 61 | + end if |
| 62 | + |
| 63 | + tail = temp(len(temp)-i+2:) |
| 64 | + end subroutine splitpath |
| 65 | + |
| 66 | + module function basename(p) result(base) |
| 67 | + character(:), allocatable :: base, temp |
| 68 | + character(*), intent(in) :: p |
| 69 | + |
| 70 | + call splitpath(p, temp, base) |
| 71 | + end function basename |
| 72 | + |
| 73 | + module function dirname(p) result(dir) |
| 74 | + character(:), allocatable :: dir, temp |
| 75 | + character(*), intent(in) :: p |
| 76 | + |
| 77 | + call splitpath(p, dir, temp) |
| 78 | + end function dirname |
| 79 | +end submodule stdlib_system_path |
0 commit comments