s3_read_line Subroutine

public subroutine s3_read_line(unit, line, iostat)

Read a line from an open S3 file.

Reads the next line from the file buffer. Lines are delimited by newline characters. The file must be opened in read mode.

@param[in] unit The unit number to read from @param[out] line The line content (truncated if longer than buffer) @param[out] iostat Status code: 0 on success, -1 on EOF or error

Example

character(len=1024) :: line
integer :: iostat

do
    call s3_read_line(unit, line, iostat)
    if (iostat /= 0) exit
    print *, trim(line)
end do

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: unit
character(len=*), intent(out) :: line
integer, intent(out) :: iostat

Source Code

    subroutine s3_read_line(unit, line, iostat)
        integer, intent(in) :: unit
        character(len=*), intent(out) :: line
        integer, intent(out) :: iostat
        integer :: i, line_end, buffer_len

        iostat = 0
        line = ''

        if (unit < 1 .or. unit > MAX_FILES) then
            iostat = -1
            return
        end if

        if (.not. files(unit)%is_open .or. files(unit)%is_write) then
            iostat = -1
            return
        end if

        if (.not. allocated(files(unit)%buffer)) then
            iostat = -1
            return
        end if

        buffer_len = len(files(unit)%buffer)

        ! Check if at end of file
        if (files(unit)%position > buffer_len) then
            iostat = -1  ! EOF
            return
        end if

        ! Find next newline
        line_end = 0
        do i = files(unit)%position, buffer_len
            if (files(unit)%buffer(i:i) == new_line('')) then
                line_end = i - 1
                exit
            end if
        end do

        ! If no newline found, read to end
        if (line_end == 0) then
            line_end = buffer_len
        end if

        ! Extract line
        if (line_end >= files(unit)%position) then
            line = files(unit)%buffer(files(unit)%position:line_end)
            files(unit)%position = line_end + 2  ! Skip newline
        else
            iostat = -1
        end if
    end subroutine s3_read_line