Class: Coopy::Csv
- Inherits:
-
Object
- Object
- Coopy::Csv
- Defined in:
- lib/lib/coopy/csv.rb
Instance Method Summary collapse
-
#initialize ⇒ Csv
constructor
A new instance of Csv.
- #parse_cell(txt) ⇒ Object
- #parse_single_cell(txt) ⇒ Object
- #parse_table(txt) ⇒ Object
- #render_cell(v, d) ⇒ Object
- #render_table(t) ⇒ Object
Constructor Details
#initialize ⇒ Csv
Returns a new instance of Csv.
7 8 9 10 |
# File 'lib/lib/coopy/csv.rb', line 7 def initialize @cursor = 0 @row_ended = false end |
Instance Method Details
#parse_cell(txt) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/lib/coopy/csv.rb', line 109 def parse_cell(txt) return nil if txt == nil @row_ended = false first_non_underscore = txt.length last_processed = 0 quoting = false quote = 0 result = "" start = @cursor begin _g1 = @cursor _g = txt.length while(_g1 < _g) i = _g1 _g1+=1 ch = (txt[i].ord rescue nil) last_processed = i first_non_underscore = i if ch != 95 && i < first_non_underscore if @has_structure if !quoting break if ch == 44 if ch == 13 || ch == 10 ch2 = (txt[i + 1].ord rescue nil) if ch2 != nil if ch2 != ch last_processed+=1 if ch2 == 13 || ch2 == 10 end end @row_ended = true break end if ch == 34 || ch == 39 if i == @cursor quoting = true quote = ch result += [ch].pack("U") if i != start next elsif ch == quote quoting = true end end result += [ch].pack("U") next end if ch == quote quoting = false next end end result += [ch].pack("U") end end @cursor = last_processed if quote == 0 return nil if result == "NULL" if first_non_underscore > start del = first_non_underscore - start return result[1..-1] if result[del..-1] == "NULL" end end return result end |
#parse_single_cell(txt) ⇒ Object
172 173 174 175 176 177 |
# File 'lib/lib/coopy/csv.rb', line 172 def parse_single_cell(txt) @cursor = 0 @row_ended = false @has_structure = false return self.parse_cell(txt) end |
#parse_table(txt) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/lib/coopy/csv.rb', line 91 def parse_table(txt) @cursor = 0 @row_ended = false @has_structure = true result = Array.new row = Array.new while(@cursor < txt.length) cell = self.parse_cell(txt) row.push(cell) if @row_ended result.push(row) row = Array.new end @cursor+=1 end return result end |
#render_cell(v, d) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/lib/coopy/csv.rb', line 46 def render_cell(v,d) return "NULL" if d == nil return "NULL" if v.equals(d,nil) str = v.to_s(d) delim = "," need_quote = false begin _g1 = 0 _g = str.length while(_g1 < _g) i = _g1 _g1+=1 ch = str[i] if ch == "\"" || ch == "'" || ch == delim || ch == "\r" || ch == "\n" || ch == "\t" || ch == " " need_quote = true break end end end result = "" result += "\"" if need_quote line_buf = "" begin _g11 = 0 _g2 = str.length while(_g11 < _g2) i1 = _g11 _g11+=1 ch1 = str[i1] result += "\"" if ch1 == "\"" if ch1 != "\r" && ch1 != "\n" if line_buf.length > 0 result += line_buf line_buf = "" end result += ch1 else line_buf += ch1 end end end result += "\"" if need_quote return result end |
#render_table(t) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/lib/coopy/csv.rb', line 20 def render_table(t) result = "" w = t.get_width h = t.get_height txt = "" v = t.get_cell_view begin _g = 0 while(_g < h) y = _g _g+=1 begin _g1 = 0 while(_g1 < w) x = _g1 _g1+=1 txt += "," if x > 0 txt += self.render_cell(v,t.get_cell(x,y)) end end txt += "\r\n" end end return txt end |