From 51300868df15467ac754212564eab607377791af Mon Sep 17 00:00:00 2001 From: Philipp Date: Sat, 19 Oct 2024 23:43:29 +0200 Subject: [PATCH] Added join function. --- src/dm_path.f90 | 30 ++++++++++++++++++++++++++++++ test/dmtestpath.f90 | 20 ++++++++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/dm_path.f90 b/src/dm_path.f90 index dae3340..c4b3e5e 100644 --- a/src/dm_path.f90 +++ b/src/dm_path.f90 @@ -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: diff --git a/test/dmtestpath.f90 b/test/dmtestpath.f90 index 3279b91..d16c16d 100644 --- a/test/dmtestpath.f90 +++ b/test/dmtestpath.f90 @@ -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() @@ -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