!  add " treatment   2011.Jan.12
!
      implicit none

      integer, parameter  :: maxch=24
      character(len=1024) :: buf, eachcol(maxch)
      integer :: i, num


 100  continue
      read(*, '(A)', end=200) buf
      call devidecsv( buf, eachcol, maxch, num )
      do i=1, num
        write(*,*) i, eachcol(i)(1:len_trim(eachcol(i)))
      end do
 101  continue
c      if (num.ge.24) then
          go to 100
c        else
c          num = num + 1
c          write(*,*) num
c          go to 101
c      end if
 200  continue
      
      end


      subroutine devidecsv( buf, eachcol, maxch, num )
      implicit none
      integer   :: maxch, num
      character(len=*) :: buf, eachcol(maxch)
      integer :: i, j, prev, now, now1, ll
      logical :: quote

      quote = .FALSE.

      ll = len_trim(buf)
      prev=0; now=1; i=1
      do j=1, ll
        if( buf(j:j) == '"' ) then
            quote = .NOT. quote
        end if
        if( quote .eqv. .FALSE. ) then
            if(buf(j:j) == ',') then
                now = j
                now1 = now -1
                if( now1 < prev+1 ) then
                    eachcol(i) = ''
                  else
                    eachcol(i) = buf(prev+1:now1)
                end if
                i=i+1
                if( i > maxch ) then
                    write(0,*) 'maxch is too small'
                    stop
                end if
                prev=now
            end if
        end if
      end do

      if( prev < ll ) then
          eachcol(i) =  buf(prev+1:ll)
          num = i
        else if ( prev == ll ) then
          eachcol(i) = ''
          num = i
        else
          num=i-1
      end if

      end
