Class: ECCSV::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/eccsv/stream.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Stream

Returns a new instance of Stream.



5
6
7
8
9
10
11
12
# File 'lib/eccsv/stream.rb', line 5

def initialize(io)
  @io = io
  @line = 1
  @col = 1
  @pos = 0
  @inserts = Hash.new { |h, k| h[k] = {} }
  @deletions = Hash.new { |h, k| h[k] = {} }
end

Instance Attribute Details

#colObject (readonly)

Returns the value of attribute col.



3
4
5
# File 'lib/eccsv/stream.rb', line 3

def col
  @col
end

#lineObject (readonly)

Returns the value of attribute line.



3
4
5
# File 'lib/eccsv/stream.rb', line 3

def line
  @line
end

#posObject (readonly)

Returns the value of attribute pos.



3
4
5
# File 'lib/eccsv/stream.rb', line 3

def pos
  @pos
end

Instance Method Details

#delete(len, line, col) ⇒ Object



59
60
61
# File 'lib/eccsv/stream.rb', line 59

def delete(len, line, col)
  @deletions[line][col] = len
end

#eof?Boolean

Returns:



41
42
43
# File 'lib/eccsv/stream.rb', line 41

def eof?
  peek.nil?
end

#insert(str, line, col) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/eccsv/stream.rb', line 45

def insert(str, line, col)
  i = 0
  str.each_char do |c|
    @inserts[line][col+i] = c
    if c == "\n"
      line += 1
      col = 1
      i = 0
    else
      i += 1
    end
  end
end

#nextObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/eccsv/stream.rb', line 21

def next
  if defined? @buf
    val = @buf
    remove_instance_variable(:@buf)
  else
    val = getc
  end

  if val
    if val == "\n"
      @line += 1
      @col = 1
    else
      @col += 1
    end
    @pos += val.bytesize
  end
  val
end

#peekObject



14
15
16
17
18
19
# File 'lib/eccsv/stream.rb', line 14

def peek
  unless defined? @buf
    @buf = getc
  end
  @buf
end