Parent

Files

Class Index [+]

Quicksearch

TaskJuggler::CSVFile

This is a very lightweight version of the Ruby library class CSV. That class changed significantly from 1.8 to 1.9 and is a compatibility nightmare. Hence we use our own class.

Public Class Methods

new(data, separator = ';', quote = '"') click to toggle source

At construction time you need to specify the data container. This is an Array of Arrays that holds the table. Optionally, you can specify a separator and a quote string for the CSV file.

    # File lib/reports/CSVFile.rb, line 25
25:     def initialize(data, separator = ';', quote = '"')
26:       @data = data
27:       @separator = separator
28:       @quote = quote
29:     end

Public Instance Methods

parse(str) click to toggle source

Read the data as Array of Arrays from a CSV formated String str.

    # File lib/reports/CSVFile.rb, line 76
76:     def parse(str)
77:       csv = []
78:       str.each_line do |line|
79:         csv << parseLine(line)
80:       end
81:     end
read(fileName) click to toggle source

Read the data as Array of Arrays from a CSV formated file fileName. In case ’.’ is used for the fileName the data is read from $stdin.

    # File lib/reports/CSVFile.rb, line 59
59:     def read(fileName)
60:       if (fileName == '.')
61:         file = $stdin
62:       else
63:         file = File.open(fileName, 'r')
64:       end
65: 
66:       csv = []
67:       file.each_line do |line|
68:         csv << parseLine(line)
69:       end
70: 
71:       file.close unless fileName == '.'
72:       csv
73:     end
write(fileName) click to toggle source

Use this function to write the table into a CSV file fileName. ’.’ can be used to write to $stdout.

    # File lib/reports/CSVFile.rb, line 33
33:     def write(fileName)
34:       if (fileName == '.')
35:         file = $stdout
36:       else
37:         file = File.open(fileName, 'w')
38:       end
39: 
40:       @data.each do |line|
41:         first = true
42:         line.each do |field|
43:           # Don't output a separator before the first field of the line.
44:            if first
45:              first = false
46:            else
47:              file.write @separator
48:            end
49:            file.write(marshal(field))
50:         end
51:         file.write "\n"
52:       end
53: 
54:       file.close unless fileName == '.'
55:     end

Private Instance Methods

closeField() click to toggle source
     # File lib/reports/CSVFile.rb, line 137
137:     def closeField
138:       @fields << @field
139:       @field = ''
140:       @state = 0
141:     end
marshal(field) click to toggle source

This function is used to properly quote @quote and @separation characters contained in the field.

    # File lib/reports/CSVFile.rb, line 87
87:     def marshal(field)
88:       if field.include?(@quote) || field.include?(@separator)
89:         field.gsub!(/@quote/, '""')
90:         field = '"' + field + '"'
91:       end
92:       field
93:     end
parseLine(line) click to toggle source
     # File lib/reports/CSVFile.rb, line 95
 95:     def parseLine(line)
 96:       @state = 0 # start of field
 97:       @fields = []
 98:       @field = ''
 99:       line.each_utf8_char do |c|
100:         case @state
101:         when 0 # start of field
102:           if c == @quote
103:             @state = 1
104:           else
105:             @field << c
106:             @state = 2
107:           end
108:         when 1 # in quoted field
109:           if c == @quote
110:             @state = 3
111:           elsif c == @separator
112:             closeField
113:           else
114:             @field << c
115:           end
116:         when 2 # in unquoted field
117:           if c == @separator
118:             closeField
119:           else
120:             @field << c
121:           end
122:         when 3 # quote found in quoted field
123:           if c == @quote
124:             @field << c
125:             @state = 2
126:           else
127:             closeField
128:           end
129:         else
130:           raise "Unknown state #{state}"
131:         end
132:       end
133:       closeField if @state != 0
134:       @fields
135:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.