basedir Function

private elemental function basedir(self, sep)

Arguments

TypeIntentOptionalAttributesName
class(string), intent(in) :: self
character(kind=CK,len=*), intent(in), optional :: sep

Return Value type(string)


Contents

Source Code


Source Code

   elemental function basedir(self, sep)
   !< Return the base directory name of a string containing a file name.
   !<
   !<```fortran
   !< type(string) :: string1
   !< logical      :: test_passed(4)
   !< string1 = '/bar/foo.tar.bz2'
   !< test_passed(1) = string1%basedir()//''=='/bar'
   !< string1 = './bar/foo.tar.bz2'
   !< test_passed(2) = string1%basedir()//''=='./bar'
   !< string1 = 'bar/foo.tar.bz2'
   !< test_passed(3) = string1%basedir()//''=='bar'
   !< string1 = '\bar\foo.tar.bz2'
   !< test_passed(4) = string1%basedir(sep='\')//''=='\bar'
   !< print '(L1)', all(test_passed)
   !<```
   !=> T <<<
   class(string),             intent(in)           :: self    !< The string.
   character(kind=CK, len=*), intent(in), optional :: sep     !< Directory separator.
   type(string)                                    :: basedir !< Base directory name.
   character(kind=CK, len=:), allocatable          :: sep_    !< Separator, default value.
   integer                                         :: pos     !< Character position.

   if (allocated(self%raw)) then
     sep_ = UIX_DIR_SEP ; if (present(sep)) sep_ = sep
     basedir = self
     pos = index(self%raw, sep_, back=.true.)
     if (pos>0) basedir%raw = self%raw(1:pos-1)
   endif
   endfunction basedir