Read the whole file into a deferred-len line array.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | path | |||
| character(len=:), | intent(out), | allocatable | :: | lines(:) | ||
| integer, | intent(out) | :: | n_lines | |||
| logical, | intent(out) | :: | ok |
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| character(len=4096), | private | :: | buf | ||||
| integer, | private | :: | cap | ||||
| integer, | private | :: | i | ||||
| character(len=256), | private | :: | imsg | ||||
| integer, | private | :: | ios | ||||
| integer, | private | :: | maxlen | ||||
| character(len=:), | private, | allocatable | :: | tmp(:) | |||
| integer, | private | :: | u |
subroutine read_lines(path, lines, n_lines, ok) !! Read the whole file into a deferred-len line array. character(len=*), intent(in) :: path character(len=:), allocatable, intent(out) :: lines(:) integer, intent(out) :: n_lines logical, intent(out) :: ok integer :: u, ios, cap, maxlen character(len=4096) :: buf character(len=256) :: imsg character(len=:), allocatable :: tmp(:) integer :: i n_lines = 0 ok = .false. open (newunit=u, file=path, status="old", action="read", iostat=ios, iomsg=imsg) if (ios /= 0) return ok = .true. cap = 64 maxlen = 256 allocate (character(len=maxlen) :: lines(cap)) do read (u, "(A)", iostat=ios, iomsg=imsg) buf if (ios /= 0) exit if (n_lines >= cap .or. len_trim(buf) > maxlen) then maxlen = max(maxlen, len_trim(buf)) allocate (character(len=maxlen) :: tmp(max(2*cap, n_lines + 1))) do i = 1, n_lines tmp(i) = lines(i) end do call move_alloc(tmp, lines) cap = size(lines) end if n_lines = n_lines + 1 lines(n_lines) = buf end do close (u) end subroutine read_lines