 ! Simple CSV Parser
 ! Phil Dawes phil@phildawes.net
 
-USING: kernel sequences io namespaces combinators unicode.categories vars ;
+USING: kernel sequences io namespaces combinators unicode.categories ;
 IN: csv
 
-DEFER: quoted-field
+SYMBOL: delimiter
 
-VAR: delimiter
+CHAR: , delimiter set-global
+
+: delimiter> delimiter get ; inline
+    
+DEFER: quoted-field ( -- endchar )
     
 ! trims whitespace from either end of string
 : trim-whitespace ( str -- str )


: (row) ( -- sep )
  field , 
  dup delimiter> = [ drop (row) ] when ;

: row ( -- eof? array[string] )
  [ (row) ] { } make ;

  dup needs-escaping? [ escape-quotes enclose-in-quotes ] when ; inline
    
: write-row ( row -- )
  [ delimiter get write1 ] [ escape-if-required write ] interleave nl ; inline
    
: write-csv ( rows outstream -- )
  [ [ write-row ] each ] with-output-stream ;
