The files under directory root with one of the extensions (without the dot) that are not
excluded, sorted by path within each directory.
| Type | Intent | Optional | 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, |
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