Class: Achoo::System::CStruct

Inherits:
Object
  • Object
show all
Defined in:
lib/achoo/system/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.



8
9
10
11
# File 'lib/achoo/system/cstruct.rb', line 8

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

Class Attribute Details

.templateObject (readonly)

Returns the value of attribute template.



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

def template
  @template
end

Class Method Details

.bin_sizeObject



31
32
33
34
35
# File 'lib/achoo/system/cstruct.rb', line 31

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



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

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

.inherited(subclass) ⇒ Object



17
18
19
20
# File 'lib/achoo/system/cstruct.rb', line 17

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

.long(name) ⇒ Object



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

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

.quad(name) ⇒ Object



25
# File 'lib/achoo/system/cstruct.rb', line 25

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

.short(name) ⇒ Object



23
# File 'lib/achoo/system/cstruct.rb', line 23

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

.string(name, length) ⇒ Object



27
28
29
# File 'lib/achoo/system/cstruct.rb', line 27

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

Instance Method Details

#packObject



60
61
62
63
# File 'lib/achoo/system/cstruct.rb', line 60

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

#unpack(str) ⇒ Object



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

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