move Subroutine

public subroutine move(unit, change, iostat, err_msg)

Move current position in file based on relative change

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: unit

Unit to read from

integer, intent(in) :: change

Relative change in position

integer, intent(out), optional :: iostat

I/O status

character(len=*), intent(out), optional :: err_msg

Error message


Source Code

  subroutine move(unit, change, iostat, err_msg)
    !! Move current position in file based on relative change
    implicit none

    ! Arguments
    integer, intent(in) :: unit
    !! Unit to read from
    integer, intent(in) :: change
    !! Relative change in position
    integer, intent(out), optional :: iostat
    !! I/O status
    character(*), intent(out), optional :: err_msg
    !! Error message

    ! Local variables
    integer :: iostat_
    !! I/O status
    integer :: i
    !! Loop index
    character(256) :: err_msg_
    !! Error message
    logical :: opened
    !! File existence check

    if(present(iostat)) iostat = 0
    if(present(err_msg)) err_msg = ""
    if(change.eq.0) return
    inquire(unit = unit, iostat = iostat_, opened = opened)
    if(iostat_ .ne. 0)then
       write(err_msg_, '(A,I0)') &
            'Cannot move in file, unit ', unit
       if(present(iostat)) iostat = iostat_
       if(present(err_msg))then
          err_msg = err_msg_
       else
          call stop_program(err_msg_)
       end if
       return
    elseif( .not.opened)then
       write(err_msg_, '(A,I0)') &
            'File is not opened, unit ', unit
       if(present(iostat)) iostat = 44
       if(present(err_msg))then
          err_msg = err_msg_
       else
          call stop_program(err_msg_)
       end if
       return
    end if
    if(change.gt.0)then
       do i = 1, change
          read(unit, '(A)', iostat = iostat_)
          if(iostat_ .ne. 0)then
             write(err_msg_, '(A,I0)') &
                  'Cannot move forward in file, unit ', unit
             if(present(iostat)) iostat = iostat_
             if(present(err_msg))then
                err_msg = err_msg_
             else
                call stop_program(err_msg_)
             end if
             return
          end if
       end do
    else
       do i = 1, abs(change)
          backspace(unit)
          if(iostat .ne. 0)then
             write(err_msg_, '(A,I0)') &
                  'Cannot move backward in file, unit ', unit
             if(present(iostat)) iostat = iostat_
             if(present(err_msg))then
                err_msg = err_msg_
             else
                call stop_program(err_msg_)
             end if
             return
          end if
       end do
    end if

  end subroutine move