read_lines Subroutine

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

Arguments

Type IntentOptional AttributesName
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.

Description

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.

Called By

proc~~read_lines~~CalledByGraph proc~read_lines read_lines proc~read_file read_file proc~read_file->proc~read_lines
Help

Variables

TypeVisibility AttributesNameInitial
type(string), public :: form_

Format of unit, local variable.

integer, public :: iostat_

IO status code, local variable.

character(len=:), public, allocatable:: iomsg_

IO status message, local variable.

character(kind=CK,len=1), public :: ch

Character storage.

integer, public :: l

Counter.


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.
  !---------------------------------------------------------------------------------------------------------------------------------
  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_
  return
  !---------------------------------------------------------------------------------------------------------------------------------
  endsubroutine read_lines