Class: Raiff::File

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ File

Instance Methods =====================================================



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/raiff/file.rb', line 12

def initialize(file)
  case (file)
  when IO
    @handle = file
  else
    @handle = File.open(file.to_s, 'r:BINARY')
  end

  @data = @handle.read
  @offset = 0
end

Instance Attribute Details

#offsetObject (readonly)

Properties ===========================================================



6
7
8
# File 'lib/raiff/file.rb', line 6

def offset
  @offset
end

Instance Method Details

#advance(count = 1) ⇒ Object



28
29
30
# File 'lib/raiff/file.rb', line 28

def advance(count = 1)
  @offset += count
end

#eof?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/raiff/file.rb', line 24

def eof?
  @offset >= @data.length
end

#peek(bytes) ⇒ Object



36
37
38
# File 'lib/raiff/file.rb', line 36

def peek(bytes)
  @data[@offset, bytes]
end

#read(bytes) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/raiff/file.rb', line 40

def read(bytes)
  data = @data[@offset, bytes]
  
  @offset += bytes
  
  data
end

#seek(offset) ⇒ Object



32
33
34
# File 'lib/raiff/file.rb', line 32

def seek(offset)
  @offset = offset
end

#unpack(format, length = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/raiff/file.rb', line 58

def unpack(format, length = nil)
  return if (@offset >= @data.length)

  part = @data[@offset, @data.length].unpack(format)
  advance(length || part.pack(format).length)
  part

rescue TypeError
  [ ]
end

#unpack_extended_floatObject



48
49
50
51
52
53
54
55
56
# File 'lib/raiff/file.rb', line 48

def unpack_extended_float
  extended = read(10).unpack('B80')[0]

  sign = extended[0, 1]
  exponent = extended[1, 15].to_i(2) - ((1 << 14) - 1)
  fraction = extended[16, 64].to_i(2)
  
  ((sign == '1') ? -1.0 : 1.0) * (fraction.to_f / ((1 << 63) - 1)) * (2 ** exponent)
end