collect_files Subroutine

public subroutine collect_files(root, extensions, exclude, files)

The files under directory root with one of the extensions (without the dot) that are not excluded, sorted by path within each directory.

Arguments

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

the directory to search

type(string_t), intent(in) :: extensions(:)

extensions of the files to collect, without the dot

type(string_t), intent(in) :: exclude(:)

glob patterns of the files and directories to skip

type(string_t), intent(out), allocatable :: files(:)

paths of the files, root joined with their relative paths (just those if root is .)


Calls

proc~~collect_files~~CallsGraph proc~collect_files collect_files fullmatch fullmatch proc~collect_files->fullmatch proc~child child proc~collect_files->proc~child proc~glob_to_regex glob_to_regex proc~collect_files->proc~glob_to_regex proc~list_directory list_directory proc~collect_files->proc~list_directory proc~sort_entries sort_entries proc~collect_files->proc~sort_entries regex regex proc~collect_files->regex proc~join_path join_path proc~child->proc~join_path re_escape re_escape proc~glob_to_regex->re_escape interface~c_closedir c_closedir proc~list_directory->interface~c_closedir interface~c_opendir c_opendir proc~list_directory->interface~c_opendir interface~c_readdir c_readdir proc~list_directory->interface~c_readdir proc~byte_at byte_at proc~list_directory->proc~byte_at proc~c_string c_string proc~list_directory->proc~c_string proc~entry_name entry_name proc~list_directory->proc~entry_name proc~find_layout find_layout proc~list_directory->proc~find_layout proc~is_directory is_directory proc~list_directory->proc~is_directory proc~list_directory->proc~join_path proc~find_layout->interface~c_closedir proc~find_layout->interface~c_opendir proc~find_layout->interface~c_readdir proc~find_layout->proc~byte_at proc~find_layout->proc~c_string proc~is_directory->interface~c_closedir proc~is_directory->interface~c_opendir proc~is_directory->proc~c_string

Called by

proc~~collect_files~~CalledByGraph proc~collect_files collect_files proc~directory_files directory_files proc~directory_files->proc~collect_files proc~run run proc~run->proc~directory_files

Source Code

subroutine collect_files(root, extensions, exclude, files)
    !! The files under directory `root` with one of the `extensions` (without the dot) that are not
    !! excluded, sorted by path within each directory.
    character(len=*), intent(in) :: root   !! the directory to search
    type(string_t), intent(in) :: extensions(:)   !! extensions of the files to collect, without the dot
    type(string_t), intent(in) :: exclude(:)                !! glob patterns of the files and directories to skip
    type(string_t), allocatable, intent(out) :: files(:)    !! paths of the files, `root` joined with their
                                                            !! relative paths (just those if `root` is `.`)
    type(regex_t), allocatable :: patterns(:)
    logical, allocatable :: by_path(:)
    integer :: k

    allocate (files(0), patterns(size(exclude)), by_path(size(exclude)))
    do k = 1, size(exclude)
        patterns(k) = regex(glob_to_regex(exclude(k)%s))
        by_path(k) = index(exclude(k)%s, '/') > 0
    end do
    call walk(root, '', 0)

contains

    recursive subroutine walk(dir, rel, depth)
        !! Add the files under directory `dir` to `files`, descending at most `MAX_DEPTH` levels.
        character(len=*), intent(in) :: dir     !! the directory's path
        character(len=*), intent(in) :: rel     !! its path relative to `root` (`''` for `root`)
        integer, intent(in) :: depth            !! its depth below `root`
        type(dir_entry_t), allocatable :: entries(:)
        character(len=:), allocatable :: rel_path, path
        integer :: k, stat

        if (depth > MAX_DEPTH) return
        call list_directory(dir, entries, stat)
        if (stat /= 0) return
        call sort_entries(entries)
        do k = 1, size(entries)
            if (len(rel) == 0) then
                rel_path = entries(k)%name
            else
                rel_path = rel//'/'//entries(k)%name
            end if
            if (excluded(entries(k)%name, rel_path)) cycle
            path = child(dir, entries(k)%name)
            select case (entries(k)%kind)
            case (ENTRY_DIR)
                call walk(path, rel_path, depth + 1)
            case (ENTRY_FILE)
                if (has_extension(entries(k)%name)) files = [files, string_t(path)]
            end select
        end do
    end subroutine walk

    logical function excluded(name, rel_path)
        !! True if an exclude pattern matches the entry: its `name`, or its `rel_path` for a
        !! pattern with a `/`.
        character(len=*), intent(in) :: name        !! the entry's name
        character(len=*), intent(in) :: rel_path    !! its path relative to `root`
        integer :: k
        excluded = .false.
        do k = 1, size(patterns)
            if (by_path(k)) then
                excluded = patterns(k)%fullmatch(rel_path)
            else
                excluded = patterns(k)%fullmatch(name)
            end if
            if (excluded) return
        end do
    end function excluded

    logical function has_extension(name)
        !! True if file name `name` ends with `.` and one of the `extensions`, after at least one
        !! other character.
        character(len=*), intent(in) :: name   !! the file name
        integer :: k, n
        has_extension = .false.
        do k = 1, size(extensions)
            n = len(extensions(k)%s) + 1
            if (len(name) > n) then
                if (name(len(name) - n + 1:) == '.'//extensions(k)%s) then
                    has_extension = .true.
                    return
                end if
            end if
        end do
    end function has_extension

end subroutine collect_files