Read (all) lines (records) from a connected unit as a single ascii stream.
@note 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 Note The connected unit is rewinded. At a successful exit current record is at eof, at the beginning otherwise.
@note Note
For unformatted read only access='stream'
is supported with new_line as line terminator.
type(string) :: astring
type(string), allocatable :: strings(:)
type(string) :: line(3)
integer :: iostat
character(len=99) :: iomsg
integer :: scratch
integer :: l
logical :: test_passed(8)
line(1) = ' Hello World! '
line(2) = 'How are you? '
line(3) = ' All say: "Fine thanks"'
open(newunit=scratch, status='SCRATCH')
write(scratch, "(A)") line(1)%chars()
write(scratch, "(A)") line(2)%chars()
write(scratch, "(A)") line(3)%chars()
call astring%read_lines(unit=scratch, iostat=iostat, iomsg=iomsg)
call astring%split(tokens=strings, sep=new_line('a'))
test_passed(1) = (size(strings, dim=1)==size(line, dim=1))
do l=1, size(strings, dim=1)
test_passed(l+1) = (strings(l)==line(l))
enddo
close(scratch)
open(newunit=scratch, status='SCRATCH', form='UNFORMATTED', access='STREAM')
write(scratch) line(1)%chars()//new_line('a')
write(scratch) line(2)%chars()//new_line('a')
write(scratch) line(3)%chars()//new_line('a')
call astring%read_lines(unit=scratch, form='unformatted', iostat=iostat, iomsg=iomsg)
call astring%split(tokens=strings, sep=new_line('a'))
test_passed(5) = (size(strings, dim=1)==size(line, dim=1))
do l=1, size(strings, dim=1)
test_passed(l+5) = (strings(l)==line(l))
enddo
close(scratch)
print '(L1)', all(test_passed)
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
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. |
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. !< !<```fortran !< type(string) :: astring !< type(string), allocatable :: strings(:) !< type(string) :: line(3) !< integer :: iostat !< character(len=99) :: iomsg !< integer :: scratch !< integer :: l !< logical :: test_passed(8) !< !< line(1) = ' Hello World! ' !< line(2) = 'How are you? ' !< line(3) = ' All say: "Fine thanks"' !< open(newunit=scratch, status='SCRATCH') !< write(scratch, "(A)") line(1)%chars() !< write(scratch, "(A)") line(2)%chars() !< write(scratch, "(A)") line(3)%chars() !< call astring%read_lines(unit=scratch, iostat=iostat, iomsg=iomsg) !< call astring%split(tokens=strings, sep=new_line('a')) !< test_passed(1) = (size(strings, dim=1)==size(line, dim=1)) !< do l=1, size(strings, dim=1) !< test_passed(l+1) = (strings(l)==line(l)) !< enddo !< close(scratch) !< open(newunit=scratch, status='SCRATCH', form='UNFORMATTED', access='STREAM') !< write(scratch) line(1)%chars()//new_line('a') !< write(scratch) line(2)%chars()//new_line('a') !< write(scratch) line(3)%chars()//new_line('a') !< call astring%read_lines(unit=scratch, form='unformatted', iostat=iostat, iomsg=iomsg) !< call astring%split(tokens=strings, sep=new_line('a')) !< test_passed(5) = (size(strings, dim=1)==size(line, dim=1)) !< do l=1, size(strings, dim=1) !< test_passed(l+5) = (strings(l)==line(l)) !< enddo !< close(scratch) !< print '(L1)', all(test_passed) !<``` !=> T <<< 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_ endsubroutine read_lines