get_current_line_from_file_stream Subroutine

private subroutine get_current_line_from_file_stream(json, iunit, line)

Rewind the file to the beginning of the current line, and return this line. The file is assumed to be opened. This is the STREAM version (see also get_current_line_from_file_sequential).

Arguments

Type IntentOptional AttributesName
class(json_core), intent(inout) :: json
integer(kind=IK), intent(in) :: iunit

file unit number

character(kind=CK,len=:), intent(out), allocatable:: line

current line


Contents


Source Code

    subroutine get_current_line_from_file_stream(json,iunit,line)

    implicit none

    class(json_core),intent(inout)                   :: json
    integer(IK),intent(in)                           :: iunit  !! file unit number
    character(kind=CK,len=:),allocatable,intent(out) :: line   !! current line

    integer(IK)              :: istart  !! start position of current line
    integer(IK)              :: iend    !! end position of current line
    integer(IK)              :: ios     !! file read `iostat` code
    character(kind=CK,len=1) :: c       !! a character read from the file

    istart = json%ipos
    do
        if (istart<=1) then
            istart = 1
            exit
        end if
        read(iunit,pos=istart,iostat=ios) c
        if (c==newline .or. ios/=0) then
            if (istart/=1) istart = istart - 1
            exit
        end if
        istart = istart-1  !rewind until the beginning of the line
    end do
    iend = json%ipos
    do
        read(iunit,pos=iend,iostat=ios) c
        if (IS_IOSTAT_END(ios)) then
            ! account for end of file without linebreak
            iend=iend-1
            exit
        end if
        if (c==newline .or. ios/=0) exit
        iend=iend+1
    end do
    allocate( character(kind=CK,len=iend-istart+1) :: line )
    read(iunit,pos=istart,iostat=ios) line

    end subroutine get_current_line_from_file_stream