read_lines Subroutine

private subroutine read_lines(self, unit, form, iostat, iomsg)

Arguments

Type IntentOptional AttributesName
class(string), intent(inout) :: self

The string.

integer, intent(in) :: unit

Logical unit.

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 (all) lines (records) from a connected unit as a single ascii stream.


Variables

TypeVisibility AttributesNameInitial
integer, public :: iostat_

IO status code, local variable.

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

IO status message, local variable.

type(string), public :: lines

Lines storage.

type(string), public :: line

Line storage.


Source Code

  subroutine read_lines(self, unit, form, iostat, iomsg)
  !---------------------------------------------------------------------------------------------------------------------------------
  !< Read (all) lines (records) from a connected unit as a single ascii stream.
  !<
  !< @note All the lines are stored into the string self as a single ascii stream. Each line (record) is separated by a `new_line`
  !< character. The line is read as an ascii stream read until the eor is reached.
  !<
  !< @note The connected unit is rewinded. At a successful exit current record is at eof, at the beginning otherwise.
  !<
  !< @note For unformatted read only `access='stream'` is supported with new_line as line terminator.
  !---------------------------------------------------------------------------------------------------------------------------------
  class(string),    intent(inout)           :: self    !< The string.
  integer,          intent(in)              :: unit    !< Logical unit.
  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.
  integer                                   :: iostat_ !< IO status code, local variable.
  character(len=:), allocatable             :: iomsg_  !< IO status message, local variable.
  type(string)                              :: lines   !< Lines storage.
  type(string)                              :: line    !< Line storage.
  !---------------------------------------------------------------------------------------------------------------------------------

  !---------------------------------------------------------------------------------------------------------------------------------
  iomsg_ = repeat(' ', 99) ; if (present(iomsg)) iomsg_ = iomsg
  rewind(unit)
  iostat_ = 0
  lines%raw = ''
  do
    line%raw = ''
    call line%read_line(unit=unit, form=form, iostat=iostat_, iomsg=iomsg_)
    if (iostat_/=0.and..not.is_iostat_eor(iostat_)) then
      exit
    elseif (line/='') then
      lines%raw = lines%raw//line%raw//new_line('a')
    endif
  enddo
  if (lines%raw/='') self%raw = lines%raw
  if (present(iostat)) iostat = iostat_
  if (present(iomsg)) iomsg = iomsg_
  return
  !---------------------------------------------------------------------------------------------------------------------------------
  endsubroutine read_lines