class GTP::Reader

Constants

INTEGER_SIZE

Attributes

file[RW]
offset[RW]

Public Class Methods

new(file) click to toggle source
# File lib/GTP/io/reader.rb, line 7
def initialize(file)
  @file = File.open(file, "r")
  @offset = 0
end

Public Instance Methods

increment_offset(delta) click to toggle source
# File lib/GTP/io/reader.rb, line 23
def increment_offset(delta)
  self.offset = self.offset + delta
end
read_byte() click to toggle source
# File lib/GTP/io/reader.rb, line 46
def read_byte

  result = IO.binread(self.file, 1, self.offset).bytes.to_a

  increment_offset 1

  raise "Byte is greather than expected" if result.size > 1

  result.first.to_i
end
read_default_string(amount_to_read=1) click to toggle source
# File lib/GTP/io/reader.rb, line 27
def read_default_string(amount_to_read=1)
  skip_useless_bytes
  read_next_string amount_to_read
end
read_first_string_and_unpack() click to toggle source
# File lib/GTP/io/reader.rb, line 38
def read_first_string_and_unpack
  read_next_string.unpack("L")
end
read_int_string() click to toggle source
# File lib/GTP/io/reader.rb, line 42
def read_int_string
  read_next_string INTEGER_SIZE
end
read_integer() click to toggle source
# File lib/GTP/io/reader.rb, line 17
def read_integer
  result = IO.binread(self.file, INTEGER_SIZE, self.offset).unpack('L').first
  increment_offset INTEGER_SIZE
  result
end
read_next_chunk_with_offset(size, offset) click to toggle source
# File lib/GTP/io/reader.rb, line 12
def read_next_chunk_with_offset(size, offset)
  increment_offset size
  IO.binread(self.file, size, offset)
end
read_string() click to toggle source
# File lib/GTP/io/reader.rb, line 32
def read_string
  value = read_next_string
  skip_bytes value.length
  value
end
skip_integer() click to toggle source
# File lib/GTP/io/reader.rb, line 57
def skip_integer
  increment_offset INTEGER_SIZE
end

Protected Instance Methods

read_next_string(amount_to_read=1) click to toggle source
# File lib/GTP/io/reader.rb, line 70
def read_next_string(amount_to_read=1)
  size = IO.binread(self.file, amount_to_read, self.offset).bytes.to_a.first.to_i
  increment_offset amount_to_read
  value = IO.binread(self.file, size, self.offset).strip
  increment_offset size
  value
end
skip_bytes(size) click to toggle source
# File lib/GTP/io/reader.rb, line 66
def skip_bytes(size)
  increment_offset 30-size
end
skip_useless_bytes() click to toggle source
# File lib/GTP/io/reader.rb, line 62
def skip_useless_bytes
  increment_offset INTEGER_SIZE
end