Class: Coopy::Csv

Inherits:
Object
  • Object
show all
Defined in:
lib/lib/coopy/csv.rb

Instance Method Summary collapse

Constructor Details

#initialize(delim = ",") ⇒ Csv

Returns a new instance of Csv.



7
8
9
10
11
12
13
14
15
# File 'lib/lib/coopy/csv.rb', line 7

def initialize(delim = ",")
  @cursor = 0
  @row_ended = false
  if delim == nil 
    @delim = ","
  else 
    @delim = delim
  end
end

Instance Method Details

#make_table(txt) ⇒ Object



140
141
142
143
144
# File 'lib/lib/coopy/csv.rb', line 140

def make_table(txt)
  tab = ::Coopy::SimpleTable.new(0,0)
  self.parse_table(txt,tab)
  tab
end

#parse_cell(txt) ⇒ Object



213
214
215
216
217
218
# File 'lib/lib/coopy/csv.rb', line 213

def parse_cell(txt)
  @cursor = 0
  @row_ended = false
  @has_structure = false
  self.parse_cell_part(txt)
end

#parse_table(txt, tab) ⇒ Object



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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/lib/coopy/csv.rb', line 90

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



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
# File 'lib/lib/coopy/csv.rb', line 47

def render_cell(v,d)
  return "NULL" if d == nil
  str = v.to_s(d)
  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
  result
end

#render_table(t) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lib/coopy/csv.rb', line 26

def render_table(t)
  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 += "\r\n"
  end
  txt
end