Skip to content

Commit

Permalink
Added join function.
Browse files Browse the repository at this point in the history
  • Loading branch information
interkosmos committed Oct 19, 2024
1 parent c9b633a commit 5130086
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
30 changes: 30 additions & 0 deletions src/dm_path.f90
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,38 @@ module dm_path
implicit none (type, external)
private

public :: dm_path_join
public :: dm_path_parsed
contains
pure function dm_path_join(path1, path2) result(path)
!! Joins paths and adds `/` between them.
character(len=*), intent(in) :: path1 !! First path.
character(len=*), intent(in) :: path2 !! Second path.
character(len=:), allocatable :: path !! Joined path.

integer :: n1, n2

n1 = len_trim(path1)
n2 = len_trim(path2)

if (n1 == 0 .and. n2 == 0) then
path = ''
return
else if (n1 == 0) then
path = path2(:n2)
return
else if (n2 == 0) then
path = path1(:n1)
return
end if

if (path1(n1:n1) /= '/' .and. path2(1:1) /= '/') then
path = path1(:n1) // '/' // path2(:n2)
else
path = path1(:n1) // path2(:n2)
end if
end function dm_path_join

function dm_path_parsed(path) result(parsed)
!! Returns a parsed path or an empty string on error. The following
!! format descriptors are allowed:
Expand Down
20 changes: 18 additions & 2 deletions test/dmtestpath.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ program dmtestpath
implicit none (type, external)

character(len=*), parameter :: TEST_NAME = 'dmtestpath'
integer, parameter :: NTESTS = 1
integer, parameter :: NTESTS = 2

type(test_type) :: tests(NTESTS)
logical :: stats(NTESTS)

tests = [ &
test_type('test01', test01) &
test_type('test01', test01), &
test_type('test02', test02) &
]

call dm_init()
Expand All @@ -35,4 +36,19 @@ logical function test01() result(stat)

stat = TEST_PASSED
end function test01

logical function test02() result(stat)
stat = TEST_FAILED

print *, 'Joining paths ...'

if (dm_path_join('a', 'b') /= 'a/b') return
if (dm_path_join('a/', 'b') /= 'a/b') return
if (dm_path_join('a', '/b') /= 'a/b') return
if (dm_path_join('a', '') /= 'a') return
if (dm_path_join('', 'b') /= 'b') return
if (dm_path_join('', '') /= '') return

stat = TEST_PASSED
end function test02
end program dmtestpath

0 comments on commit 5130086

Please sign in to comment.