read_lines Subroutine

public subroutine read_lines(unit, lines, form, iostat, iomsg)

Arguments

TypeIntentOptionalAttributesName
integer, intent(in) :: unit
type(string), intent(out), allocatable:: lines(:)
character(len=*), intent(in), optional :: form
integer, intent(out), optional :: iostat
character(len=*), intent(inout), optional :: iomsg

Called by

proc~~read_lines~3~~CalledByGraph proc~read_lines~3 read_lines proc~read_file~3 read_file proc~read_file~3->proc~read_lines~3

Contents

Source Code


Source Code

   subroutine read_lines(unit, lines, form, iostat, iomsg)
   !< Read lines (records) from a connected-formatted unit.
   !<
   !< @note The connected unit is rewinded. At a successful exit current record is at eof, at the beginning otherwise.
   !<
   !< The lines are returned as an array of strings that are read until the eof is reached.
   !< The line is read as an ascii stream read until the eor is reached.
   !<
   !< @note For unformatted read only `access='stream'` is supported with new_line as line terminator.
   !<
   !< @note There is no doctests, this being tested by means of [[read_file]] doctests.
   integer,          intent(in)               :: unit     !< Logical unit.
   type(string),     intent(out), allocatable :: lines(:) !< The lines.
   character(len=*), intent(in),    optional  :: form     !< Format of unit.
   integer,          intent(out),   optional  :: iostat   !< IO status code.
   character(len=*), intent(inout), optional  :: iomsg    !< IO status message.
   type(string)                               :: form_    !< Format of unit, local variable.
   integer                                    :: iostat_  !< IO status code, local variable.
   character(len=:), allocatable              :: iomsg_   !< IO status message, local variable.
   character(kind=CK, len=1)                  :: ch       !< Character storage.
   integer                                    :: l        !< Counter.

   form_ = 'FORMATTED' ; if (present(form)) form_ = form ; form_ = form_%upper()
   iomsg_ = repeat(' ', 99) ; if (present(iomsg)) iomsg_ = iomsg
   rewind(unit)
   select case(form_%chars())
   case('FORMATTED')
      l = 0
      do
         read(unit, *, err=10, end=10)
         l = l + 1
      enddo
   case('UNFORMATTED')
      l = 0
      do
         read(unit, err=10, end=10) ch
         if (ch==new_line('a')) l = l + 1
      enddo
   endselect
   10 rewind(unit)
   if (l>0) then
      allocate(lines(1:l))
      l = 1
      iostat_ = 0
      do
         call lines(l)%read_line(unit=unit, form=form, iostat=iostat_, iomsg=iomsg_)
         if ((iostat_/=0.and..not.is_iostat_eor(iostat_)).or.(l>=size(lines, dim=1))) then
            exit
         endif
         l = l + 1
      enddo
   endif
   if (present(iostat)) iostat = iostat_
   if (present(iomsg)) iomsg = iomsg_
   endsubroutine read_lines