1 ! Copyright (C) 2007, 2008 Phil Dawes
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel sequences io namespaces make combinators
4 unicode.categories io.files combinators.short-circuit ;
9 CHAR: , delimiter set-global
13 : delimiter> ( -- delimiter ) delimiter get ; inline
15 DEFER: quoted-field ( -- endchar )
17 : trim-whitespace ( str -- str )
18 [ blank? ] trim ; inline
20 : skip-to-field-end ( -- endchar )
21 "\n" delimiter> suffix read-until nip ; inline
23 : not-quoted-field ( -- endchar )
24 "\"\n" delimiter> suffix read-until
26 { CHAR: " [ 2drop quoted-field ] }
27 { delimiter> [ swap trim-whitespace % ] }
28 { CHAR: \n [ swap trim-whitespace % ] }
29 { f [ swap trim-whitespace % ] }
32 : maybe-escaped-quote ( -- endchar )
34 { CHAR: " [ , quoted-field ] }
37 [ 2drop skip-to-field-end ]
40 : quoted-field ( -- endchar )
42 drop % maybe-escaped-quote ;
44 : field ( -- sep string )
45 [ not-quoted-field ] "" make ;
49 dup delimiter> = [ drop (row) ] when ;
51 : row ( -- eof? array[string] )
56 dup [ empty? ] all? [ drop ] [ , ] if
61 : csv-row ( stream -- row )
62 [ row nip ] with-input-stream ;
64 : csv ( stream -- rows )
65 [ [ (csv) ] { } make ] with-input-stream
66 dup peek { "" } = [ but-last ] when ;
68 : file>csv ( path encoding -- csv )
71 : with-delimiter ( ch quot -- )
72 [ delimiter ] dip with-variable ; inline
76 : needs-escaping? ( cell -- ? )
77 [ { [ "\n\"" member? ] [ delimiter get = ] } 1|| ] any? ; inline
79 : escape-quotes ( cell -- cell' )
83 [ dup CHAR: " = [ , ] [ drop ] if ] bi
87 : enclose-in-quotes ( cell -- cell' )
88 "\"" dup surround ; inline
90 : escape-if-required ( cell -- cell' )
92 [ escape-quotes enclose-in-quotes ] when ; inline
96 : write-row ( row -- )
97 [ delimiter get write1 ]
98 [ escape-if-required write ] interleave nl ; inline
100 : write-csv ( rows stream -- )
101 [ [ write-row ] each ] with-output-stream ;
103 : csv>file ( rows path encoding -- ) <file-writer> write-csv ;