scan_modules Subroutine

public subroutine scan_modules(filename, modules, n_modules)

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: filename
type(module_info), intent(out), dimension(:), allocatable :: modules
integer, intent(out) :: n_modules

Source Code

    subroutine scan_modules(filename, modules, n_modules)
        character(len=*), intent(in) :: filename
        type(module_info), dimension(:), allocatable, intent(out) :: modules
        integer, intent(out) :: n_modules

        integer :: unit, iostat, i
        character(len=512) :: line
        character(len=128) :: module_name
        type(module_info), dimension(:), allocatable :: temp_modules
        integer :: max_modules
        logical :: is_intrinsic

        max_modules = 100
        allocate (temp_modules(max_modules))
        n_modules = 0

        open (newunit=unit, file=filename, status='old', iostat=iostat)
        if (iostat /= 0) then
            call global_logger%log_error('Cannot open file '//trim(filename))
            return
        end if

        do
            read (unit, '(a)', iostat=iostat) line
            if (iostat /= 0) exit

            ! Process the line
            call process_use_statement(line, module_name)

            if (len_trim(module_name) > 0) then
                ! Check if it's an intrinsic module
                is_intrinsic = .false.
                do i = 1, size(intrinsic_modules)
                    if (trim(module_name) == trim(intrinsic_modules(i))) then
                        is_intrinsic = .true.
                        exit
                    end if
                end do

                if (.not. is_intrinsic) then
                    n_modules = n_modules + 1
                    if (n_modules <= max_modules) then
                        temp_modules(n_modules)%name = module_name
                    end if
                end if
            end if
        end do

        close (unit)

        ! Allocate output array
        if (n_modules > 0) then
            ! Handle case where we found more modules than max_modules
            if (n_modules > max_modules) then
                allocate (modules(max_modules))
                modules = temp_modules(1:max_modules)
                n_modules = max_modules  ! Update count to reflect actual stored
            else
                allocate (modules(n_modules))
                modules = temp_modules(1:n_modules)
            end if
        else
            allocate (modules(0))
        end if

        deallocate (temp_modules)

    end subroutine scan_modules