read_lines Subroutine

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

Read lines (records) from a connected-formatted unit.

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.

Arguments

Type IntentOptional Attributes Name
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.


Calls

proc~~read_lines~~CallsGraph proc~read_lines stringifor::read_lines chars chars proc~read_lines->chars upper upper proc~read_lines->upper

Called by

proc~~read_lines~~CalledByGraph proc~read_lines stringifor::read_lines proc~read_file stringifor::read_file proc~read_file->proc~read_lines program~stringifor_test_csv_naive_parser stringifor_test_csv_naive_parser program~stringifor_test_csv_naive_parser->proc~read_file program~stringifor_test_parse_large_csv stringifor_test_parse_large_csv program~stringifor_test_parse_large_csv->proc~read_file program~volatile_doctest~110 volatile_doctest program~volatile_doctest~110->proc~read_file program~volatile_doctest~111 volatile_doctest program~volatile_doctest~111->proc~read_file

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