Class: CsvReader::BufferIO

Inherits:
Object
  • Object
show all
Defined in:
lib/csvreader/buffer.rb

Overview

todo: find a better name - why? why not? is really just for reading (keep io?)

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ BufferIO

Returns a new instance of BufferIO.



5
6
7
8
9
# File 'lib/csvreader/buffer.rb', line 5

def initialize( data )
  # create the IO object we will read from

  @io = data.is_a?(String) ? StringIO.new(data) : data
  @buf = [] ## last (buffer) chars (used for peek)

end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


11
# File 'lib/csvreader/buffer.rb', line 11

def eof?()   @buf.size == 0 && @io.eof?;  end

#getcObject



13
14
15
16
17
18
19
# File 'lib/csvreader/buffer.rb', line 13

def getc
  if @buf.size > 0
    @buf.shift  ## get first char from buffer

  else
    @io.getc
  end
end

#peekObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/csvreader/buffer.rb', line 30

def peek
   ## todo/fix:

   ## use Hexadecimal code: 1A, U+001A for eof char - why? why not?

  if @buf.size == 0 && @io.eof?
    puts "peek - hitting eof!!!"
    ## return eof char(s) - exits? is \0 ?? double check

    return "\0"
  end

  if @buf.size == 0
     c = @io.getc
     @buf.push( c )
     ## puts "peek - fill buffer >#{c}< (#{c.ord})"

  end

  @buf.first
end

#ungetc(c) ⇒ Object



22
23
24
25
26
27
# File 'lib/csvreader/buffer.rb', line 22

def ungetc( c )
  ## add upfront as first char in buffer

  ##   last in/first out queue!!!!

  @buf.unshift( c )
  ## puts "ungetc - >#{c} (#{c.ord})< => >#{@buf}<"

end