glob_string Subroutine

private subroutine glob_string(self, pattern, list)

Glob search (string output), finds all the pathnames matching a given pattern according to the rules used by the Unix shell.

 type(string)                  :: astring
 type(string),     allocatable :: alist_str(:)
 integer, parameter            :: Nf=5
 character(14)                 :: files(1:Nf)
 integer                       :: file_unit
 integer                       :: f
 integer                       :: ff
 logical                       :: test_passed

 do f=1, Nf
    files(f) = astring%tempname(prefix='foo-')
    open(newunit=file_unit, file=files(f))
    write(file_unit, *)f
    close(unit=file_unit)
 enddo
 call astring%glob(pattern='foo-*', list=alist_str)
 do f=1, Nf
    open(newunit=file_unit, file=files(f))
    close(unit=file_unit, status='delete')
 enddo
 test_passed = .false.
 outer_str: do f=1, size(alist_str, dim=1)
    do ff=1, Nf
       test_passed = alist_str(f) == files(ff)
       if (test_passed) cycle outer_str
    enddo
 enddo outer_str
 print '(L1)', test_passed

Type Bound

string

Arguments

Type IntentOptional Attributes Name
class(string), intent(in) :: self

The string.

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

Given pattern.

type(string), intent(out), allocatable :: list(:)

List of matching pathnames.


Calls

proc~~glob_string~~CallsGraph proc~glob_string stringifor_string_t::string%glob_string proc~read_file~2 stringifor_string_t::string%read_file proc~glob_string->proc~read_file~2 proc~split stringifor_string_t::string%split proc~glob_string->proc~split proc~chars stringifor_string_t::string%chars proc~read_file~2->proc~chars proc~read_lines~2 stringifor_string_t::string%read_lines proc~read_file~2->proc~read_lines~2 proc~upper stringifor_string_t::string%upper proc~read_file~2->proc~upper proc~partition stringifor_string_t::string%partition proc~split->proc~partition proc~unique stringifor_string_t::string%unique proc~split->proc~unique raw raw proc~partition->raw proc~read_line stringifor_string_t::string%read_line proc~read_lines~2->proc~read_line proc~replace stringifor_string_t::string%replace proc~unique->proc~replace proc~read_line->proc~chars proc~read_line->proc~upper proc~replace_one_occurrence stringifor_string_t::string%replace_one_occurrence proc~replace->proc~replace_one_occurrence

Called by

proc~~glob_string~~CalledByGraph proc~glob_string stringifor_string_t::string%glob_string interface~glob stringifor_string_t::glob interface~glob->proc~glob_string proc~glob_character stringifor_string_t::string%glob_character interface~glob->proc~glob_character proc~glob_character->interface~glob program~volatile_doctest~16 volatile_doctest program~volatile_doctest~16->interface~glob program~volatile_doctest~69 volatile_doctest program~volatile_doctest~69->interface~glob program~volatile_doctest~80 volatile_doctest program~volatile_doctest~80->interface~glob

Contents

Source Code


Source Code

   subroutine glob_string(self, pattern, list)
   !< Glob search (string output), finds all the pathnames matching a given pattern according to the rules used by the Unix shell.
   !<
   !< @note Method not portable: works only on Unix/GNU Linux OS.
   !<
   !<```fortran
   !< type(string)                  :: astring
   !< type(string),     allocatable :: alist_str(:)
   !< integer, parameter            :: Nf=5
   !< character(14)                 :: files(1:Nf)
   !< integer                       :: file_unit
   !< integer                       :: f
   !< integer                       :: ff
   !< logical                       :: test_passed
   !<
   !< do f=1, Nf
   !<    files(f) = astring%tempname(prefix='foo-')
   !<    open(newunit=file_unit, file=files(f))
   !<    write(file_unit, *)f
   !<    close(unit=file_unit)
   !< enddo
   !< call astring%glob(pattern='foo-*', list=alist_str)
   !< do f=1, Nf
   !<    open(newunit=file_unit, file=files(f))
   !<    close(unit=file_unit, status='delete')
   !< enddo
   !< test_passed = .false.
   !< outer_str: do f=1, size(alist_str, dim=1)
   !<    do ff=1, Nf
   !<       test_passed = alist_str(f) == files(ff)
   !<       if (test_passed) cycle outer_str
   !<    enddo
   !< enddo outer_str
   !< print '(L1)', test_passed
   !<```
   !=> T <<<
   class(string),             intent(in)  :: self     !< The string.
   character(*),              intent(in)  :: pattern  !< Given pattern.
   type(string), allocatable, intent(out) :: list(:)  !< List of matching pathnames.
   type(string)                           :: tempfile !< Safe temporary file.
   character(len=:), allocatable          :: tempname !< Safe temporary name.
   integer(I4P)                           :: tempunit !< Unit of temporary file.

   tempname = self%tempname()
   call execute_command_line('ls -1 '//trim(adjustl(pattern))//' > '//tempname)
   call tempfile%read_file(file=tempname)
   call tempfile%split(sep=new_line('a'), tokens=list)
   open(newunit=tempunit, file=tempname)
   close(unit=tempunit, status='delete')
   endsubroutine glob_string