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 get = [ drop (row) ] when ;
51 : row ( -- eof? array[string] )
55 row harvest [ , ] unless-empty [ (csv) ] when ;
59 : csv-row ( stream -- row )
60 [ row nip ] with-input-stream ;
62 : csv ( stream -- rows )
63 [ [ (csv) ] { } make ] with-input-stream ;
65 : file>csv ( path encoding -- csv )
68 : with-delimiter ( ch quot -- )
69 [ delimiter ] dip with-variable ; inline
73 : needs-escaping? ( cell -- ? )
74 [ { [ "\n\"" member? ] [ delimiter get = ] } 1|| ] any? ; inline
76 : escape-quotes ( cell -- cell' )
80 [ dup CHAR: " = [ , ] [ drop ] if ] bi
84 : enclose-in-quotes ( cell -- cell' )
85 "\"" dup surround ; inline
87 : escape-if-required ( cell -- cell' )
89 [ escape-quotes enclose-in-quotes ] when ; inline
93 : write-row ( row -- )
94 [ delimiter get write1 ]
95 [ escape-if-required write ] interleave nl ; inline
97 : write-csv ( rows stream -- )
98 [ [ write-row ] each ] with-output-stream ;
100 : csv>file ( rows path encoding -- ) <file-writer> write-csv ;