class Coopy::Csv
Attributes
cursor[RW]
delim[RW]
discovered_eol[RW]
has_structure[RW]
preferred_eol[RW]
row_ended[RW]
Public Class Methods
new(delim = ",",eol = nil)
click to toggle source
# 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
Public Instance Methods
get_discovered_eol()
click to toggle source
# File lib/lib/coopy/csv.rb, line 279 def get_discovered_eol @discovered_eol end
make_table(txt)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/lib/coopy/csv.rb, line 283 def set_preferred_eol(eol) @preferred_eol = eol end
Protected Instance Methods
parse_cell_part(txt)
click to toggle source
# File lib/lib/coopy/csv.rb, line 182 def parse_cell_part(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 if ch == (@delim[0].ord rescue nil) break if @delim.length == 1 if i + @delim.length <= txt.length match = true begin _g3 = 1 _g2 = @delim.length while(_g3 < _g2) j = _g3 _g3+=1 if txt[i + j] != @delim[j] match = false break end end end if match last_processed += @delim.length - 1 break end end end if ch == 13 || ch == 10 ch2 = (txt[i + 1].ord rescue nil) if ch2 != nil if ch2 != ch if ch2 == 13 || ch2 == 10 @discovered_eol = _hx_str([ch].pack("U")) + _hx_str([ch2].pack("U")) if @discovered_eol == nil last_processed+=1 end end end @discovered_eol = [ch].pack("U") if @discovered_eol == nil @row_ended = true break end if ch == 34 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 result end