read_file Subroutine

private subroutine read_file(self, file, form, iostat, iomsg)

Arguments

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

The string.

character(len=*), intent(in) :: file

File name.

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 a file as a single string stream.


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.

integer, public :: unit

Logical unit.

logical, public :: does_exist

Check if file exist.


Source Code

  subroutine read_file(self, file, form, iostat, iomsg)
  !---------------------------------------------------------------------------------------------------------------------------------
  !< Read a file as a single string 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.
  !<
  !< @note For unformatted read only `access='stream'` is supported with new_line as line terminator.
  !---------------------------------------------------------------------------------------------------------------------------------
  class(string),    intent(inout)           :: self       !< The string.
  character(len=*), intent(in)              :: file       !< File name.
  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.
  integer                                   :: unit       !< Logical unit.
  logical                                   :: does_exist !< Check if file exist.
  !---------------------------------------------------------------------------------------------------------------------------------

  !---------------------------------------------------------------------------------------------------------------------------------
  iomsg_ = repeat(' ', 99) ; if (present(iomsg)) iomsg_ = iomsg
  inquire(file=file, iomsg=iomsg_, iostat=iostat_, exist=does_exist)
  if (does_exist) then
    form_ = 'FORMATTED' ; if (present(form)) form_ = form ; form_ = form_%upper()
    select case(form_%chars())
    case('FORMATTED')
      open(newunit=unit, file=file, status='OLD', action='READ', iomsg=iomsg_, iostat=iostat_, err=10)
    case('UNFORMATTED')
      open(newunit=unit, file=file, status='OLD', action='READ', form='UNFORMATTED', access='STREAM', &
           iomsg=iomsg_, iostat=iostat_, err=10)
    endselect
    call self%read_lines(unit=unit, form=form, iomsg=iomsg_, iostat=iostat_)
    10 close(unit)
  endif
  if (present(iostat)) iostat = iostat_
  if (present(iomsg)) iomsg = iomsg_
  return
  !---------------------------------------------------------------------------------------------------------------------------------
  endsubroutine read_file