Class: DataInspect::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.



42
43
44
# File 'lib/data_inspect.rb', line 42

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)


90
91
92
# File 'lib/data_inspect.rb', line 90

def atEOS?
  @file.eof?
end

#each_byteObject



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

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.



56
57
58
# File 'lib/data_inspect.rb', line 56

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)



83
84
85
86
87
# File 'lib/data_inspect.rb', line 83

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)



74
75
76
77
78
# File 'lib/data_inspect.rb', line 74

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).



67
68
69
70
# File 'lib/data_inspect.rb', line 67

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



60
61
62
63
# File 'lib/data_inspect.rb', line 60

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.



95
96
97
# File 'lib/data_inspect.rb', line 95

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