basedir Function

private elemental function basedir(self, sep)

Arguments

Type IntentOptional AttributesName
class(string), intent(in) :: self

The string.

character(kind=CK,len=*), intent(in), optional :: sep

Directory separator.

Return Value type(string)

Base directory name.

Description

Return the base directory name of a string containing a file name.

Example

 type(string) :: astring
 astring = '/bar/foo.tar.bz2'
 print '(A)', astring%basedir()//'' ! print "/bar"

Variables

TypeVisibility AttributesNameInitial
character(kind=CK,len=:), public, allocatable:: sep_

Separator, default value.

integer, public :: pos

Character position.


Source Code

  elemental function basedir(self, sep)
  !---------------------------------------------------------------------------------------------------------------------------------
  !< Return the base directory name of a string containing a file name.
  !<
  !<### Example
  !<
  !<```fortran
  !< type(string) :: astring
  !< astring = '/bar/foo.tar.bz2'
  !< print '(A)', astring%basedir()//'' ! print "/bar"
  !<```
  !---------------------------------------------------------------------------------------------------------------------------------
  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
  return
  !---------------------------------------------------------------------------------------------------------------------------------
  endfunction basedir