Add csv>string and string>csv
parent
18c0935b64
commit
5eff2e0e9a
|
@ -21,6 +21,20 @@ HELP: csv>file
|
||||||
}
|
}
|
||||||
{ $description "Writes a comma-separated-value structure to a file." } ;
|
{ $description "Writes a comma-separated-value structure to a file." } ;
|
||||||
|
|
||||||
|
HELP: string>csv
|
||||||
|
{ $values
|
||||||
|
{ "string" string }
|
||||||
|
{ "csv" "csv" }
|
||||||
|
}
|
||||||
|
{ $description "Parses a string into a sequence of comma-separated-value fields." } ;
|
||||||
|
|
||||||
|
HELP: csv>string
|
||||||
|
{ $values
|
||||||
|
{ "rows" "a sequence of sequences of strings" }
|
||||||
|
{ "string" string }
|
||||||
|
}
|
||||||
|
{ $description "Writes a comma-separated-value structure to a string." } ;
|
||||||
|
|
||||||
HELP: csv-row
|
HELP: csv-row
|
||||||
{ $values { "stream" "an input stream" }
|
{ $values { "stream" "an input stream" }
|
||||||
{ "row" "an array of fields" } }
|
{ "row" "an array of fields" } }
|
||||||
|
@ -42,6 +56,10 @@ ARTICLE: "csv" "Comma-separated-values parsing and writing"
|
||||||
{ $subsections file>csv }
|
{ $subsections file>csv }
|
||||||
"Writing a csv file:"
|
"Writing a csv file:"
|
||||||
{ $subsections csv>file }
|
{ $subsections csv>file }
|
||||||
|
"Reading a string to csv:"
|
||||||
|
{ $subsections string>csv }
|
||||||
|
"Writing csv to a string:"
|
||||||
|
{ $subsections csv>string }
|
||||||
"Changing the delimiter from a comma:"
|
"Changing the delimiter from a comma:"
|
||||||
{ $subsections with-delimiter }
|
{ $subsections with-delimiter }
|
||||||
"Reading from a stream:"
|
"Reading from a stream:"
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
! Copyright (C) 2007, 2008 Phil Dawes
|
! Copyright (C) 2007, 2008 Phil Dawes
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: kernel sequences io namespaces make combinators
|
USING: kernel sequences io namespaces make combinators
|
||||||
unicode.categories io.files combinators.short-circuit ;
|
unicode.categories io.files combinators.short-circuit
|
||||||
|
io.streams.string ;
|
||||||
IN: csv
|
IN: csv
|
||||||
|
|
||||||
SYMBOL: delimiter
|
SYMBOL: delimiter
|
||||||
|
@ -65,6 +66,9 @@ PRIVATE>
|
||||||
[ [ (csv) ] { } make ] with-input-stream
|
[ [ (csv) ] { } make ] with-input-stream
|
||||||
dup last { "" } = [ but-last ] when ;
|
dup last { "" } = [ but-last ] when ;
|
||||||
|
|
||||||
|
: string>csv ( string -- csv )
|
||||||
|
<string-reader> csv ;
|
||||||
|
|
||||||
: file>csv ( path encoding -- csv )
|
: file>csv ( path encoding -- csv )
|
||||||
<file-reader> csv ;
|
<file-reader> csv ;
|
||||||
|
|
||||||
|
@ -96,8 +100,18 @@ PRIVATE>
|
||||||
: write-row ( row -- )
|
: write-row ( row -- )
|
||||||
[ delimiter get write1 ]
|
[ delimiter get write1 ]
|
||||||
[ escape-if-required write ] interleave nl ; inline
|
[ escape-if-required write ] interleave nl ; inline
|
||||||
|
|
||||||
: write-csv ( rows stream -- )
|
|
||||||
[ [ write-row ] each ] with-output-stream ;
|
|
||||||
|
|
||||||
|
<PRIVATE
|
||||||
|
|
||||||
|
: (write-csv) ( rows -- )
|
||||||
|
[ write-row ] each ;
|
||||||
|
|
||||||
|
PRIVATE>
|
||||||
|
|
||||||
|
: write-csv ( rows stream -- )
|
||||||
|
[ (write-csv) ] with-output-stream ;
|
||||||
|
|
||||||
|
: csv>string ( csv -- string )
|
||||||
|
[ (write-csv) ] with-string-writer ;
|
||||||
|
|
||||||
: csv>file ( rows path encoding -- ) <file-writer> write-csv ;
|
: csv>file ( rows path encoding -- ) <file-writer> write-csv ;
|
||||||
|
|
Loading…
Reference in New Issue