Class: RRD::Reader
- Inherits:
-
Object
- Object
- RRD::Reader
- Defined in:
- lib/rrd/reader.rb
Defined Under Namespace
Classes: Unival
Instance Attribute Summary collapse
-
#alignment ⇒ Object
Returns the value of attribute alignment.
-
#endianess ⇒ Object
Returns the value of attribute endianess.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Instance Method Summary collapse
- #align ⇒ Object
- #close ⇒ Object
-
#initialize(filename) ⇒ Reader
constructor
A new instance of Reader.
- #pos ⇒ Object
- #read(size, symbol = "a*", options = {}) ⇒ Object
- #read_string(size, symbol = "a*") ⇒ Object
- #read_uniparams(size) ⇒ Object
- #seek(position, mode = :absolute) ⇒ Object
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
#alignment ⇒ Object
Returns the value of attribute alignment.
26 27 28 |
# File 'lib/rrd/reader.rb', line 26 def alignment @alignment end |
#endianess ⇒ Object
Returns the value of attribute endianess.
26 27 28 |
# File 'lib/rrd/reader.rb', line 26 def endianess @endianess end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
24 25 26 |
# File 'lib/rrd/reader.rb', line 24 def file @file end |
Instance Method Details
#align ⇒ Object
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 |
#close ⇒ Object
69 70 71 |
# File 'lib/rrd/reader.rb', line 69 def close @file.close if @file end |
#pos ⇒ Object
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*", = {}) align if [: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 |