Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | column | |||
integer, | intent(in) | :: | file_id | |||
logical, | intent(in), | optional | :: | required |
integer function header_column_index(column, file_id, required)
! Return index of column in first line of file_id, return 0 if not found
character(len=*), intent(in) :: column
integer, intent(in) :: file_id
logical, intent(in), optional :: required
integer iostat, ie
character(len=read_line_buffer_size) buffer
character(len=read_line_buffer_size*2) scanpart, current_col
logical column_not_found
rewind(file_id)
scanpart = ''
column_not_found = .True.
header_column_index = 0
iostat = 0
do while (column_not_found .and. iostat == 0)
read(file_id, "(a)", advance="no", iostat=iostat) buffer
scanpart = trim(adjustl(scanpart))//buffer
ie = 1
do while (column_not_found .and. ie > 0)
ie = scan(scanpart, ",")
if (ie > 0) then
current_col = trim(adjustl(scanpart(:ie-1)))
scanpart = scanpart(ie+1:)
header_column_index = header_column_index + 1
column_not_found = trim(adjustl(current_col)) /= trim(adjustl(column))
else if (iostat < 0) then ! last column
current_col = scanpart
header_column_index = header_column_index + 1
column_not_found = trim(adjustl(current_col)) /= trim(adjustl(column))
end if
end do
end do
if (column_not_found) then
header_column_index = 0
if (present(required)) then
if (required) call input_error_column_not_found(column, file_id)
end if
end if
! Make sure we move to next line if not already
if (iostat == 0) call move_lines(file_id, 1)
end function header_column_index