tokenize_csv_line Subroutine

private subroutine tokenize_csv_line(me, line, cells)

Tokenize a line from a CSV file. The result is an array of csv_string types.

Notes

  • Quotes are removed if the entire cell is contained in quotes.

Warning

It does not account for delimiters in quotes (these are treated as a new cell). Need to fix!

Type Bound

csv_file

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(inout) :: me
character(len=*), intent(in) :: line
type(csv_string), intent(out), dimension(:), allocatable :: cells

Calls

proc~~tokenize_csv_line~~CallsGraph proc~tokenize_csv_line csv_file%tokenize_csv_line proc~split split proc~tokenize_csv_line->proc~split proc~expand_vector expand_vector proc~split->proc~expand_vector

Called by

proc~~tokenize_csv_line~~CalledByGraph proc~tokenize_csv_line csv_file%tokenize_csv_line proc~read_csv_file csv_file%read_csv_file proc~read_csv_file->proc~tokenize_csv_line

Source Code

    subroutine tokenize_csv_line(me,line,cells)

    implicit none

    class(csv_file),intent(inout) :: me
    character(len=*),intent(in) :: line
    type(csv_string),dimension(:),allocatable,intent(out) :: cells

    integer :: i !! counter
    character(len=:),allocatable :: tmp !! a temp string with whitespace removed
    integer :: n !! length of compressed string

    call split(line,me%delimiter,me%chunk_size,cells)

    ! remove quotes if present:
    do i = 1, size(cells)

        ! remove whitespace from the string:
        tmp = trim(adjustl(cells(i)%str))
        n = len(tmp)

        if (n>1) then
            ! if the first and last non-blank character is
            ! a quote, then remove them and replace with what
            ! is inside the quotes. Otherwise, leave it as is.
            if (tmp(1:1)==me%quote .and. tmp(n:n)==me%quote) then
                if (n>2) then
                    cells(i)%str = tmp(2:n-1)  ! remove the quotes
                else
                    cells(i)%str = ''  ! empty string
                end if
            end if
        end if

    end do

    end subroutine tokenize_csv_line