Class: Achoo::Binary::CStruct

Inherits:
Object
  • Object
show all
Defined in:
lib/achoo/binary/cstruct.rb

Direct Known Subclasses

UTMPRecord

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes = nil) ⇒ CStruct

Returns a new instance of CStruct.



5
6
7
8
# File 'lib/achoo/binary/cstruct.rb', line 5

def initialize(bytes=nil)
  @values = []
  unpack(bytes) unless bytes.nil?
end

Class Attribute Details

.templateObject (readonly)

Returns the value of attribute template.



12
13
14
# File 'lib/achoo/binary/cstruct.rb', line 12

def template
  @template
end

Class Method Details

.bin_sizeObject



26
27
28
29
30
# File 'lib/achoo/binary/cstruct.rb', line 26

def bin_size
  @bin_size ||= template.split('').select {|c| c =~ /[[:alpha:]]/}.map do |c|
    c == 'A' ? '' : 0
  end.pack(template).length
end

.char(name) ⇒ Object



19
# File 'lib/achoo/binary/cstruct.rb', line 19

def char(name);  add_type(name, :char,  'c', 0); end

.inherited(subclass) ⇒ Object



14
15
16
17
# File 'lib/achoo/binary/cstruct.rb', line 14

def inherited(subclass)
  subclass.instance_variable_set(:@template, '') 
  subclass.instance_variable_set(:@count, 0)
end

.long(name) ⇒ Object



21
# File 'lib/achoo/binary/cstruct.rb', line 21

def long(name);  add_type(name, :long,  'l', 0); end

.quad(name) ⇒ Object



22
# File 'lib/achoo/binary/cstruct.rb', line 22

def quad(name);  add_type(name, :quad,  'q', 0); end

.short(name) ⇒ Object



20
# File 'lib/achoo/binary/cstruct.rb', line 20

def short(name); add_type(name, :short, 's', 0); end

.string(name, length) ⇒ Object



24
# File 'lib/achoo/binary/cstruct.rb', line 24

def string(name, length); add_type(name, :string, 'A', '', length); end

Instance Method Details

#packObject



55
56
57
58
# File 'lib/achoo/binary/cstruct.rb', line 55

def pack
  t = self.class.template.tr('A', 'a')
  @values.pack(t)
end

#unpack(str) ⇒ Object



51
52
53
# File 'lib/achoo/binary/cstruct.rb', line 51

def unpack(str)
  @values = str.unpack(self.class.template)
end