Class: Arcana::Cursor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buf) ⇒ Cursor

Returns a new instance of Cursor.



7
8
9
10
# File 'lib/arcana.rb', line 7

def initialize(buf)
  @buf = buf
  @base = @offset = 0
end

Instance Attribute Details

#bufObject (readonly)

Returns the value of attribute buf.



5
6
7
# File 'lib/arcana.rb', line 5

def buf
  @buf
end

#offsetObject (readonly)

Returns the value of attribute offset.



5
6
7
# File 'lib/arcana.rb', line 5

def offset
  @offset
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/arcana.rb', line 12

def eof?
  @offset >= @buf.size
end

#inspectObject



54
55
56
# File 'lib/arcana.rb', line 54

def inspect
  "#<#{self.class} offset=#{@offset}>"
end

#mark_baseObject



26
27
28
29
# File 'lib/arcana.rb', line 26

def mark_base
  @base += @offset
  @offset = 0
end

#peek(n) ⇒ Object



22
23
24
# File 'lib/arcana.rb', line 22

def peek(n)
  @buf[@offset, n]
end

#read(n) ⇒ Object



16
17
18
19
20
# File 'lib/arcana.rb', line 16

def read(n)
  ret = peek(n)
  seek_relative(n)
  ret
end

#restoreObject



47
48
49
50
51
52
# File 'lib/arcana.rb', line 47

def restore
  prev = @offset, @base
  yield
ensure
  @offset, @base = prev
end

#seek_absolute(offset) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/arcana.rb', line 31

def seek_absolute(offset)
  if offset < 0
    @offset = @buf.size + offset
  else
    @offset = offset
  end
end

#seek_pos(offset) ⇒ Object



39
40
41
# File 'lib/arcana.rb', line 39

def seek_pos(offset)
  seek_absolute(@base + offset)
end

#seek_relative(offset) ⇒ Object



43
44
45
# File 'lib/arcana.rb', line 43

def seek_relative(offset)
  @offset += offset
end