Class: Twiddler::Parser::V4Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/twiddler/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(raw, skipped = 0) ⇒ V4Parser

Returns a new instance of V4Parser.



14
15
16
17
18
19
20
21
22
# File 'lib/twiddler/parser.rb', line 14

def initialize(raw, skipped = 0)
  @data = raw
  @refs = Hash.new{|h,k| h[k] = [:missing_ref]}
  bytes_hex
  @index = 0
  consume("x" * skipped)
  bytes_hex
  @config = Config.new()
end

Instance Method Details

#bytes_hex(count = 8) ⇒ Object



36
37
38
# File 'lib/twiddler/parser.rb', line 36

def bytes_hex(count = 8)
  "0x%04x:#{' %02x'*count}" % ([@index] + @data.unpack("c" * count))
end

#consume(template) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/twiddler/parser.rb', line 28

def consume(template)
  old_len = @data.length
  values = @data.unpack(template + "a*")
  @data = values.pop
  @index += old_len - @data.length
  return values
end

#goObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/twiddler/parser.rb', line 40

def go()
  total_size = @data.length + 1
  @keyboard_chord_table, @mouse_chord_table, @multichar_table = *consume("vvv")

  @config.configs[:raw] = *consume("a#{@keyboard_chord_table - @index}")

  until @index >= @mouse_chord_table
    key1234, mod_or_ff, keyindex = consume("B16B8c")
    exit if @index > 685 
    if keyindex == 0 and 
      key1234 == "0000000000000000" and 
      mod_or_ff == "00000000"
      next
    end

    chord = Config::KeyChord.new
    chord.keydata = key1234

    if(mod_or_ff == "11111111")
      @refs[keyindex] = chord
    else
      chord.add_keystroke(mod_or_ff, keyindex)
    end

    @config.keyboard << chord
  end

  while @index < @multichar_table
    key1234, data = consume("B16B8")
    if key1234 == "0000000000000000" and 
      data == "00000000"
      next
    end

    chord_record = Config::MouseChord.new
    chord_record.keydata = key1234
    chord_record.data = data
    @config.mouse << chord_record
  end

  data_idx = 0
  until @data.size == 0 do
    data_size = *consume("v")

    chord_data = []
    ((data_size-1)/2).times do 
      mod, idx = *consume("B8c")
      next if idx == 0 #Not sure this is sufficient
      @refs[data_idx].add_keystroke(mod, idx)
    end
    data_idx += 1
  end

  return self
end

#parsedObject



24
25
26
# File 'lib/twiddler/parser.rb', line 24

def parsed
  return @config
end