Class: Baikal::Cursor

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

Overview

Represents an offset into a byte pool, permitting traversing the byte pool in a linear manner.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool, offset = 0) ⇒ Cursor

Creates a cursor of the given pool, initialising it to point at its given offset. If offset is not given, defaults to zero, that is to say, the byte pool’s beginning.



19
20
21
22
23
24
25
26
# File 'lib/baikal/cursor.rb', line 19

def initialize pool, offset = 0
  raise 'Type mismatch' unless pool.is_a? Baikal::Pool
  raise 'Type mismatch' unless offset.is_a? Integer
  super()
  @pool = pool
  @offset = offset
  return
end

Instance Attribute Details

#offsetObject

The cursor’s current offset into the underlying byte pool.



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

def offset
  @offset
end

Instance Method Details

#eof?Boolean

Returns true if the cursor has passed the last byte currently in the underlying pool or false otherwise.

Returns:

  • (Boolean)


291
292
293
# File 'lib/baikal/cursor.rb', line 291

def eof?
  return @offset >= @pool.size
end

#parse_blob(size) ⇒ Object

Retrieves a specified number of bytes from the underlying pool’s current position and advances the cursor by the same number.

Error if the blob referred to by this cursor and this byte count lies outside the boundaries of the underlying pool, even partially.



254
255
256
257
258
259
260
# File 'lib/baikal/cursor.rb', line 254

def parse_blob size
  raise 'Type mismatch' unless size.is_a? Integer
  raise 'Invalid blob size' unless size >= 0
  blob = @pool.get_blob(@offset, size)
  @offset += size
  return blob
end

#parse_signed_byteObject

Retrieves a signed byte from the underlying pool’s current position and advances the cursor by one byte.

Error if the byte referred to by this cursor lies outside the boundaries of the underlying pool.



90
91
92
93
94
# File 'lib/baikal/cursor.rb', line 90

def parse_signed_byte
  value = @pool.get_signed_byte(@offset)
  @offset += 1
  return value
end

#parse_signed_octaObject

Retrieves a signed octabyte from the underlying pool’s current position using the pool’s currently selected byte order and advances the cursor by eight bytes.

Error if the octa referred to by this cursor lies outside the boundaries of the underlying pool, even partially.



132
133
134
135
136
# File 'lib/baikal/cursor.rb', line 132

def parse_signed_octa
  value = @pool.get_signed_octa(@offset)
  @offset += 8
  return value
end

#parse_signed_tetraObject

Retrieves a signed tetrabyte from the underlying pool’s current position using the pool’s currently selected byte order and advances the cursor by four bytes.

Error if the tetra referred to by this cursor lies outside the boundaries of the underlying pool, even partially.



118
119
120
121
122
# File 'lib/baikal/cursor.rb', line 118

def parse_signed_tetra
  value = @pool.get_signed_tetra(@offset)
  @offset += 4
  return value
end

#parse_signed_wydeObject

Retrieves a signed wyde from the underlying pool’s current position using the pool’s currently selected byte order and advances the cursor by two bytes.

Error if the wyde referred to by this cursor lies outside the boundaries of the underlying pool, even partially.



104
105
106
107
108
# File 'lib/baikal/cursor.rb', line 104

def parse_signed_wyde
  value = @pool.get_signed_wyde(@offset)
  @offset += 2
  return value
end

#parse_unsigned_byteObject

Retrieves an unsigned byte from the underlying pool’s current position and advances the cursor by one byte.

Error if the byte referred to by this cursor lies outside the boundaries of the underlying pool.



35
36
37
38
39
# File 'lib/baikal/cursor.rb', line 35

def parse_unsigned_byte
  value = @pool.get_unsigned_byte(@offset)
  @offset += 1
  return value
end

#parse_unsigned_integer(size) ⇒ Object

Retrieves an unsigned integer of the given size (which must be 1, 2, 4 or 8) from the underlying pool’s current position using the pool’s currently selected byte order and advances the cursor by the integer’s size.

Error if the integer of this size referred to by this cursor lies outside the boundaries of the underlying pool, even partially.



241
242
243
244
245
# File 'lib/baikal/cursor.rb', line 241

def parse_unsigned_integer size
  value = @pool.get_unsigned_integer(size, @offset)
  @offset += size
  return value
end

#parse_unsigned_octaObject

Retrieves an unsigned octabyte from the underlying pool’s current position using the pool’s currently selected byte order and advances the cursor by eight bytes.

Error if the octa referred to by this cursor lies outside the boundaries of the underlying pool, even partially.



77
78
79
80
81
# File 'lib/baikal/cursor.rb', line 77

def parse_unsigned_octa
  value = @pool.get_unsigned_octa(@offset)
  @offset += 8
  return value
end

#parse_unsigned_tetraObject

Retrieves an unsigned tetrabyte from the underlying pool’s current position using the pool’s currently selected byte order and advances the cursor by four bytes.

Error if the tetra referred to by this cursor lies outside the boundaries of the underlying pool, even partially.



63
64
65
66
67
# File 'lib/baikal/cursor.rb', line 63

def parse_unsigned_tetra
  value = @pool.get_unsigned_tetra(@offset)
  @offset += 4
  return value
end

#parse_unsigned_wydeObject

Retrieves an unsigned wyde from the underlying pool’s current position using the pool’s currently selected byte order and advances the cursor by two bytes.

Error if the wyde referred to by this cursor lies outside the boundaries of the underlying pool, even partially.



49
50
51
52
53
# File 'lib/baikal/cursor.rb', line 49

def parse_unsigned_wyde
  value = @pool.get_unsigned_wyde(@offset)
  @offset += 2
  return value
end

#peek_signed_byteObject

Retrieves a signed byte from the underlying pool’s current position. Does not affect the position.

Error if the byte referred to by this cursor lies outside the boundaries of the underlying pool.



192
193
194
# File 'lib/baikal/cursor.rb', line 192

def peek_signed_byte
  return @pool.get_signed_byte(@offset)
end

#peek_signed_octaObject

Retrieves a signed octabyte from the underlying pool’s current position using the pool’s currently selected byte order. Does not affect the position.

Error if the octa referred to by this cursor lies outside the boundaries of the underlying pool, even partially.



228
229
230
# File 'lib/baikal/cursor.rb', line 228

def peek_signed_octa
  return @pool.get_signed_octa(@offset)
end

#peek_signed_tetraObject

Retrieves a signed tetrabyte from the underlying pool’s current position using the pool’s currently selected byte order. Does not affect the position.

Error if the tetra referred to by this cursor lies outside the boundaries of the underlying pool, even partially.



216
217
218
# File 'lib/baikal/cursor.rb', line 216

def peek_signed_tetra
  return @pool.get_signed_tetra(@offset)
end

#peek_signed_wydeObject

Retrieves a signed wyde from the underlying pool’s current position using the pool’s currently selected byte order. Does not affect the position.

Error if the wyde referred to by this cursor lies outside the boundaries of the underlying pool, even partially.



204
205
206
# File 'lib/baikal/cursor.rb', line 204

def peek_signed_wyde
  return @pool.get_signed_wyde(@offset)
end

#peek_unsigned_byteObject

Retrieves an unsigned byte from the underlying pool’s current position. Does not affect the position.

Error if the byte referred to by this cursor lies outside the boundaries of the underlying pool.



145
146
147
# File 'lib/baikal/cursor.rb', line 145

def peek_unsigned_byte
  return @pool.get_unsigned_byte(@offset)
end

#peek_unsigned_octaObject

Retrieves an unsigned octabyte from the underlying pool’s current position using the pool’s currently selected byte order. Does not affect the position.

Error if the octa referred to by this cursor lies outside the boundaries of the underlying pool, even partially.



181
182
183
# File 'lib/baikal/cursor.rb', line 181

def peek_unsigned_octa
  return @pool.get_unsigned_octa(@offset)
end

#peek_unsigned_tetraObject

Retrieves an unsigned tetrabyte from the underlying pool’s current position using the pool’s currently selected byte order. Does not affect the position.

Error if the tetra referred to by this cursor lies outside the boundaries of the underlying pool, even partially.



169
170
171
# File 'lib/baikal/cursor.rb', line 169

def peek_unsigned_tetra
  return @pool.get_unsigned_tetra(@offset)
end

#peek_unsigned_wydeObject

Retrieves an unsigned wyde from the underlying pool’s current position using the pool’s currently selected byte order. Does not affect the position.

Error if the wyde referred to by this cursor lies outside the boundaries of the underlying pool, even partially.



157
158
159
# File 'lib/baikal/cursor.rb', line 157

def peek_unsigned_wyde
  return @pool.get_unsigned_wyde(@offset)
end

#skip(delta) ⇒ Object

Moves the cursor forwards by delta bytes, passing a part of the byte sequence without parsing.

Error if delta is negative.



268
269
270
271
272
273
# File 'lib/baikal/cursor.rb', line 268

def skip delta
  raise 'Type mismatch' unless delta.is_a? Integer
  raise 'Invalid byte count' if delta < 0
  @offset += delta
  return
end

#unskip(delta) ⇒ Object

Moves the cursor backwards by delta bytes.

Error if delta is negative.



280
281
282
283
284
285
# File 'lib/baikal/cursor.rb', line 280

def unskip delta
  raise 'Type mismatch' unless delta.is_a? Integer
  raise 'Invalid byte count' if delta < 0
  @offset -= delta
  return
end