Class: Coopy::Csv
- Inherits:
-
Object
- Object
- Coopy::Csv
- Defined in:
- lib/lib/coopy/csv.rb
Instance Method Summary collapse
- #get_discovered_eol ⇒ Object
-
#initialize(delim = ",", eol = nil) ⇒ Csv
constructor
A new instance of Csv.
- #make_table(txt) ⇒ Object
- #parse_cell(txt) ⇒ Object
- #parse_table(txt, tab) ⇒ Object
- #render_cell(v, d) ⇒ Object
- #render_table(t) ⇒ Object
- #set_preferred_eol(eol) ⇒ Object
Constructor Details
#initialize(delim = ",", eol = nil) ⇒ Csv
Returns a new instance of Csv.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/lib/coopy/csv.rb', line 7 def initialize(delim = ",",eol = nil) @cursor = 0 @row_ended = false if delim == nil @delim = "," else @delim = delim end @discovered_eol = nil @preferred_eol = eol end |
Instance Method Details
#get_discovered_eol ⇒ Object
279 280 281 |
# File 'lib/lib/coopy/csv.rb', line 279 def get_discovered_eol @discovered_eol end |
#make_table(txt) ⇒ Object
174 175 176 177 178 |
# File 'lib/lib/coopy/csv.rb', line 174 def make_table(txt) tab = ::Coopy::SimpleTable.new(0,0) self.parse_table(txt,tab) tab end |
#parse_cell(txt) ⇒ Object
272 273 274 275 276 277 |
# File 'lib/lib/coopy/csv.rb', line 272 def parse_cell(txt) @cursor = 0 @row_ended = false @has_structure = false self.parse_cell_part(txt) end |
#parse_table(txt, tab) ⇒ Object
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 171 172 |
# File 'lib/lib/coopy/csv.rb', line 124 def parse_table(txt,tab) return false if !tab.is_resizable @cursor = 0 @row_ended = false @has_structure = true tab.resize(0,0) w = 0 h = 0 at = 0 yat = 0 while(@cursor < txt.length) cell = self.parse_cell_part(txt) if yat >= h h = yat + 1 tab.resize(w,h) end if at >= w if yat > 0 if cell != "" && cell != nil context = "" begin _g = 0 while(_g < w) i = _g _g+=1 context += "," if i > 0 begin s = tab.get_cell(i,yat) context += s.to_s end end end puts "Ignored overflowing row " + _hx_str(yat) + " with cell '" + _hx_str(cell) + "' after: " + _hx_str(context) end else w = at + 1 tab.resize(w,h) end end tab.set_cell(at,h - 1,cell) at+=1 if @row_ended at = 0 yat+=1 end @cursor+=1 end true end |
#render_cell(v, d) ⇒ Object
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/lib/coopy/csv.rb', line 53 def render_cell(v,d) return "NULL" if d == nil str = v.to_s(d) need_quote = false if str.length > 0 need_quote = true if str[0] == " " || str[str.length - 1] == " " end if !need_quote _g1 = 0 _g = str.length while(_g1 < _g) i = _g1 _g1+=1 ch = str[i] if ch == "\"" || ch == "'" || ch == "\r" || ch == "\n" || ch == "\t" need_quote = true break end if ch == @delim[0] if @delim.length == 1 need_quote = true break end if i + @delim.length <= str.length match = true begin _g3 = 1 _g2 = @delim.length while(_g3 < _g2) j = _g3 _g3+=1 if str[i + j] != @delim[j] match = false break end end end if match need_quote = true break end end end end end result = "" result += "\"" if need_quote line_buf = "" begin _g11 = 0 _g4 = str.length while(_g11 < _g4) 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 result end |
#render_table(t) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/lib/coopy/csv.rb', line 30 def render_table(t) eol = @preferred_eol eol = "\r\n" if eol == nil result = "" txt = "" v = t.get_cell_view stream = ::Coopy::TableStream.new(t) w = stream.width while(stream.fetch) begin _g = 0 while(_g < w) x = _g _g+=1 txt += @delim if x > 0 txt += self.render_cell(v,stream.get_cell(x)) end end txt += eol end txt end |
#set_preferred_eol(eol) ⇒ Object
283 284 285 |
# File 'lib/lib/coopy/csv.rb', line 283 def set_preferred_eol(eol) @preferred_eol = eol end |