e3d0c0b21495f448b928f915012980aca17fd155
1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # CSV output facilities
18 # A CSV document representation.
24 # Contains the name of all fields in this table.
25 var header
: Array[String] = new Array[String] is writable
27 # The list of the records.
29 # All records must have the same length than `header`.
30 var records
: Array[Array[String]] = new Array[Array[String]]
32 # Replace the header by the specified row.
33 fun set_header
(values
: Object...) do
35 for value
in values
do header
.add
(value
.to_s
)
38 # Append the specfied record.
39 fun add_record
(values
: Object...) do
40 assert values
.length
== header
.length
else
41 sys
.stderr
.write
"CSV error: Header declares {header.length} columns, record contains {values.length} values.\n"
43 var record
= new Array[String]
44 for value
in values
do record
.add
(value
.to_s
)
48 private fun write_line_to
(line
: Collection[String], stream
: OStream)
63 redef fun write_to
(stream
) do
64 write_line_to
(header
, stream
)
65 for record
in records
do write_line_to
(record
, stream
)
68 # Deprecated alias for `write_to_file`.
69 fun save
(file
: String) do write_to_file
(file
)