rta: add `live_methods_to_tree` to provide human-readable infos on methods
[nit.git] / lib / csv.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # CSV output facilities
16 module csv
17
18 # A CSV document representation
19 class CSVDocument
20 var header: Array[String] writable = new Array[String]
21 var lines: Array[Array[String]] = new Array[Array[String]]
22
23 fun set_header(values: Object...) do
24 header.clear
25 for value in values do
26 header.add(value.to_s)
27 end
28 end
29
30 fun add_line(values: Object...) do
31 if values.length != header.length then
32 print "CSV error: header declares {header.length} columns, line contains {values.length} values"
33 abort
34 end
35 var line = new Array[String]
36 for value in values do
37 line.add(value.to_s)
38 end
39 lines.add(line)
40 end
41
42 redef fun to_s do
43 var str = header.join(";") + "\n"
44 for line in lines do str += line.join(";") + "\n"
45 return str
46 end
47
48 fun save(file: String) do
49 var out = new OFStream.open(file)
50 out.write(self.to_s)
51 out.close
52 end
53 end