filter_watch_files Subroutine

public subroutine filter_watch_files(files, include, ignore, masks)

Apply include/ignore rules to the watch list.

  • If include patterns are empty, all files are included by default.
  • If include patterns are present, only matching files are kept.
  • If ignore patterns match a file, it is removed even if included.

The masks array (when present) is filtered in sync with files.

Arguments

Type IntentOptional Attributes Name
type(string_t), intent(inout), allocatable :: files(:)
type(string_t), intent(in), allocatable :: include(:)
type(string_t), intent(in), allocatable :: ignore(:)
integer(kind=int64), intent(inout), optional, allocatable :: masks(:)

Calls

proc~~filter_watch_files~~CallsGraph proc~filter_watch_files filter_watch_files basename basename proc~filter_watch_files->basename glob glob proc~filter_watch_files->glob proc~normalize_path normalize_path proc~filter_watch_files->proc~normalize_path

Called by

proc~~filter_watch_files~~CalledByGraph proc~filter_watch_files filter_watch_files proc~compute_watch_files_from_settings compute_watch_files_from_settings proc~compute_watch_files_from_settings->proc~filter_watch_files proc~rebuild_watch_list rebuild_watch_list proc~rebuild_watch_list->proc~compute_watch_files_from_settings proc~handle_manifest_change handle_manifest_change proc~handle_manifest_change->proc~rebuild_watch_list proc~watcher_init watcher_t%watcher_init proc~watcher_init->proc~rebuild_watch_list proc~watcher_run watcher_t%watcher_run proc~watcher_run->proc~rebuild_watch_list proc~watcher_run->proc~handle_manifest_change

Source Code

   subroutine filter_watch_files(files, include, ignore, masks)
      type(string_t), allocatable, intent(inout) :: files(:)
      type(string_t), allocatable, intent(in)    :: include(:), ignore(:)
      integer(int64), allocatable, intent(inout), optional :: masks(:)

      logical, allocatable :: keep(:)
      type(string_t), allocatable :: out(:)
      integer(int64), allocatable :: outm(:)
      integer :: i, k, n, m

      if (.not. allocated(files)) return

      n = size(files)
      allocate(keep(n))
      do i = 1, n
         keep(i) = should_keep(files(i)%s, include, ignore)
      end do

      m = count(keep)
      allocate(out(m))
      if (present(masks)) then
         if (.not. allocated(masks)) then
            allocate(masks(n))
            masks = 0_int64
         end if
         allocate(outm(m))
      end if

      k = 0
      do i = 1, n
         if (.not. keep(i)) cycle
         k = k + 1
         out(k)%s = files(i)%s
         if (present(masks)) outm(k) = masks(i)
      end do

      call move_alloc(out, files)
      if (present(masks)) call move_alloc(outm, masks)
      deallocate(keep)

   contains

      logical function should_keep(path, include_pats, ignore_pats) result(ok)
         character(len=*), intent(in) :: path
         type(string_t), allocatable, intent(in) :: include_pats(:), ignore_pats(:)
         character(len=:), allocatable :: b
         b = basename(trim(path))
         if (b == "fpm.toml") then
            ok = .true.
            return
         end if
         ok = match_include(path, include_pats) .and. (.not. match_any(path, ignore_pats))
      end function should_keep

      logical function match_any(path, pats) result(ok)
         character(len=*), intent(in) :: path
         type(string_t), allocatable, intent(in) :: pats(:)
         integer :: j
         character(len=:), allocatable :: pat
         ok = .false.
         if (.not. allocated(pats)) return
         do j = 1, size(pats)
            if (len_trim(pats(j)%s) == 0) cycle
            pat = normalize_path(trim(pats(j)%s))
            if (glob(pat, trim(path))) then
               ok = .true.
               return
            end if
         end do
      end function match_any

      logical function match_include(path, pats) result(ok)
         character(len=*), intent(in) :: path
         type(string_t), allocatable, intent(in) :: pats(:)
         if (.not. allocated(pats)) then
            ok = .true.
            return
         end if
         if (size(pats) == 0) then
            ok = .true.
            return
         end if
         ok = match_any(path, pats)
      end function match_include

   end subroutine filter_watch_files