join_path Function

public function join_path(a1, a2) result(path)

Join directory and leaf path segments with OS file separator

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: a1

Leading path segment

character(len=*), intent(in) :: a2

Trailing path segment

Return Value character(len=:), allocatable

Joined path string


Source Code

function join_path(a1, a2) result(path)

   !> Leading path segment
   character(len=*), intent(in) :: a1

   !> Trailing path segment
   character(len=*), intent(in) :: a2

   !> Joined path string
   character(len=:), allocatable :: path

   character :: filesep
   integer :: n1

   n1 = len_trim(a1)
   if (n1 == 0) then
      path = trim(a2)
      return
   end if

   if (a1(n1:n1) == "/" .or. a1(n1:n1) == "\") then
      path = a1(:n1) // trim(a2)
      return
   end if

   filesep = merge("\", "/", is_windows())
   path = a1(:n1) // filesep // trim(a2)

end function join_path