Class: RRD::Reader

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

Defined Under Namespace

Classes: Unival

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Reader

Returns a new instance of Reader.



28
29
30
31
32
# File 'lib/rrd/reader.rb', line 28

def initialize(filename)
  @file = ::File.open(filename, "rb")
  @endianess = :little
  @alignment = 4
end

Instance Attribute Details

#alignmentObject

Returns the value of attribute alignment.



26
27
28
# File 'lib/rrd/reader.rb', line 26

def alignment
  @alignment
end

#endianessObject

Returns the value of attribute endianess.



26
27
28
# File 'lib/rrd/reader.rb', line 26

def endianess
  @endianess
end

#fileObject (readonly)

Returns the value of attribute file.



24
25
26
# File 'lib/rrd/reader.rb', line 24

def file
  @file
end

Instance Method Details

#alignObject



48
49
50
51
# File 'lib/rrd/reader.rb', line 48

def align
  skip = (alignment - (file.pos % alignment)) % alignment
  file.read(skip) if skip
end

#closeObject



69
70
71
# File 'lib/rrd/reader.rb', line 69

def close
  @file.close if @file
end

#posObject



44
45
46
# File 'lib/rrd/reader.rb', line 44

def pos
  file.pos
end

#read(size, symbol = "a*", options = {}) ⇒ Object



53
54
55
56
57
# File 'lib/rrd/reader.rb', line 53

def read(size, symbol = "a*", options = {})
  align if options[:align]

  file.read(size).unpack(endian(symbol))
end

#read_string(size, symbol = "a*") ⇒ Object



59
60
61
# File 'lib/rrd/reader.rb', line 59

def read_string(size, symbol = "a*")
  read(size, symbol).first
end

#read_uniparams(size) ⇒ Object



63
64
65
66
67
# File 'lib/rrd/reader.rb', line 63

def read_uniparams(size)
  size.times.map do |i|
    Unival.new(read(8, "a*"), endianess)
  end
end

#seek(position, mode = :absolute) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/rrd/reader.rb', line 34

def seek(position, mode = :absolute)
  modes = {
    absolute: IO::SEEK_SET,
    eof: IO::SEEK_END,
    relative: IO::SEEK_CUR
  }

  file.seek(position.to_i, modes[mode] || raise("Unknown seek mode '#{mode}'"))
end