join_path Function

public function join_path(a1, a2, a3, a4, a5) result(path)

Construct path by joining strings with os file separator

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: a1
character(len=*), intent(in) :: a2
character(len=*), intent(in), optional :: a3
character(len=*), intent(in), optional :: a4
character(len=*), intent(in), optional :: a5

Return Value character(len=:), allocatable


Source Code

function join_path(a1,a2,a3,a4,a5) result(path)

    character(len=*), intent(in)           :: a1, a2
    character(len=*), intent(in), optional :: a3, a4, a5
    character(len=:), allocatable          :: path
    character(len=1)                       :: filesep
    logical, save                          :: has_cache = .false.
    character(len=1), save                 :: cache = '/'
    !$omp threadprivate(has_cache, cache)

    if (has_cache) then
        filesep = cache
    else
        select case (get_os_type())
            case default
                filesep = '/'
            case (OS_WINDOWS)
                filesep = '\'
        end select

        cache = filesep
        has_cache = .true.
    end if

    if (a1 == "") then
        path = a2
    else
        path = a1 // filesep // a2
    end if

    if (present(a3)) then
        path = path // filesep // a3
    else
        return
    end if

    if (present(a4)) then
        path = path // filesep // a4
    else
        return
    end if

    if (present(a5)) then
        path = path // filesep // a5
    else
        return
    end if

end function join_path