Class: Reader

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

Constant Summary collapse

@@unpack_formats =
{
  unsigned_int: {
    1 => { not_applicable: 'C' },
    2 => { big_endian: 'n', little_endian: 'v' },
    4 => { big_endian: 'N', little_endian: 'V' }
  },
   signed_int: {
    1 => { not_applicable: 'c' }
  },
   ieee_single_precision_float: {
    4 => { big_endian: 'g', little_endian: 'e' }
  },
   ieee_double_precision_float: {
    8 => { big_endian: 'G', little_endian: 'E' }
  }
 }

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Reader

Returns a new instance of Reader.



40
41
42
# File 'lib/data_inspect.rb', line 40

def initialize(filename)
  @file = File.open(filename, 'rb')
end

Instance Method Details

#atEOS?Boolean

If the file is at the end of stream.

Returns:

  • (Boolean)


88
89
90
# File 'lib/data_inspect.rb', line 88

def atEOS?
  @file.eof?
end

#each_byteObject



44
45
46
47
48
49
50
# File 'lib/data_inspect.rb', line 44

def each_byte
  offset = 0
  @file.each_byte do |byte|
    yield byte, offset
    offset += 1
  end
end

#next_byte(size = 1) ⇒ Object

optional parameter size is the number of bytes to read. returns a string, representing the binary data.



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

def next_byte(size=1)
  @file.read(size)
end

#next_ieee_double_precision_float(byte_order) ⇒ Object

Next IEEE double-precision floating point number (8 bytes)



81
82
83
84
85
# File 'lib/data_inspect.rb', line 81

def next_ieee_double_precision_float(byte_order)
  size = 8
  unpack_format = format(:ieee_double_precision_float, size, byte_order)
  unpack(size, unpack_format)
end

#next_ieee_single_precision_float(byte_order) ⇒ Object

Next IEEE single-precision floating point number (4 bytes)



72
73
74
75
76
# File 'lib/data_inspect.rb', line 72

def next_ieee_single_precision_float(byte_order)
  size = 4
  unpack_format = format(:ieee_single_precision_float, size, byte_order)
  unpack(size, unpack_format)
end

#next_signed_int(size, byte_order) ⇒ Object

Ruby doesn’t do this for us automatically so we have to add our own code (UnsignedToSignedInteger class).



65
66
67
68
# File 'lib/data_inspect.rb', line 65

def next_signed_int(size, byte_order)
  unsigned_value = next_unsigned_int(size, byte_order)
  UnsignedToSignedInteger.toSignedInteger(unsigned_value, size)
end

#next_unsigned_int(size = 1, byte_order = :not_applicable) ⇒ Object



58
59
60
61
# File 'lib/data_inspect.rb', line 58

def next_unsigned_int(size=1, byte_order=:not_applicable)
  unpack_format = format(:unsigned_int, size, byte_order)
  unpack(size, unpack_format)
end

#seek(offset) ⇒ Object

Seek to a given offset in the file.



93
94
95
# File 'lib/data_inspect.rb', line 93

def seek(offset)
  @file.seek(offset)
end