get_fields_string Subroutine

public subroutine get_fields_string(line, delims, n_max, fields, n_found, fully_read)

Routine to find entries in a string

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: line

The line from which we want to read

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

A string with delimiters. For example delims = " ,'"""//char(9)

integer, intent(in) :: n_max

Maximum number of entries to read in

character(len=*), intent(inout) :: fields(n_max)

Fields in the strings

integer, intent(inout) :: n_found

Number of entries found

logical, intent(out), optional :: fully_read

Called by

proc~~get_fields_string~~CalledByGraph proc~get_fields_string get_fields_string proc~read_arguments read_arguments proc~read_arguments->proc~get_fields_string proc~main main proc~main->proc~read_arguments program~agile agile program~agile->proc~main

Source Code

  subroutine get_fields_string(line, delims, n_max, fields, n_found,&
      fully_read)
    !> The line from which we want to read
    character(len=*), intent(in)    :: line
    !> A string with delimiters. For example delims = " ,'"""//char(9)
    character(len=*), intent(in)    :: delims
    !> Maximum number of entries to read in
    integer, intent(in)             :: n_max
    !> Number of entries found
    integer, intent(inout)          :: n_found
    !> Fields in the strings
    character(len=*), intent(inout) :: fields(n_max)
    logical, intent(out), optional  :: fully_read

    integer :: ixs_start(n_max)
    integer :: ixs_end(n_max)
    integer :: ix, ix_prev

    ix_prev = 0
    n_found = 0

    do while (n_found < n_max)
       ! Find the starting point of the next entry (a non-delimiter value)
       ix = verify(line(ix_prev+1:), delims)
       if (ix == 0) exit

       n_found            = n_found + 1
       ixs_start(n_found) = ix_prev + ix !This is the absolute position in 'line'

       ! Get the end point of the current entry (next delimiter index minus one)
       ix = scan(line(ixs_start(n_found)+1:), delims) - 1

       if (ix == -1) then              ! If there is no last delimiter,
          ixs_end(n_found) = len(line) ! the end of the line is the endpoint
       else
          ixs_end(n_found) = ixs_start(n_found) + ix
       end if

       fields(n_found) = line(ixs_start(n_found):ixs_end(n_found))
       ix_prev = ixs_end(n_found) ! We continue to search from here
    end do

    if (present(fully_read)) then
      ix = verify(line(ix_prev+1:), delims)
      fully_read = (ix == 0)    ! Are there only delimiters?
    end if

  end subroutine get_fields_string