class ECCSV::Stream

Attributes

col[R]
line[R]
pos[R]

Public Class Methods

new(io) click to toggle source
# 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

Public Instance Methods

delete(len, line, col) click to toggle source
# File lib/eccsv/stream.rb, line 59
def delete(len, line, col)
  @deletions[line][col] = len
end
eof?() click to toggle source
# File lib/eccsv/stream.rb, line 41
def eof?
  peek.nil?
end
insert(str, line, col) click to toggle source
# 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
next() click to toggle source
# 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
peek() click to toggle source
# File lib/eccsv/stream.rb, line 14
def peek
  unless defined? @buf
    @buf = getc
  end
  @buf
end

Private Instance Methods

getc() click to toggle source
# File lib/eccsv/stream.rb, line 65
def getc
  if @deletions.has_key?(@line) && @deletions[@line].has_key?(@col)
    @io.seek(@deletions[@line][@col], IO::SEEK_CUR)
  end

  if @inserts.has_key?(@line) && @inserts[@line].has_key?(@col)
    @inserts[@line][@col]
  else
    @io.getc
  end
end