Class: BinTools::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/bin_tools/writer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fn) ⇒ Writer

Returns a new instance of Writer.



12
13
14
# File 'lib/bin_tools/writer.rb', line 12

def initialize fn
  @f = File.new fn, "wb"
end

Class Method Details

.open(fn, &block) ⇒ Object



16
17
18
19
20
# File 'lib/bin_tools/writer.rb', line 16

def self.open fn, &block
  f = self.new fn
  block.call f
  f.close
end

Instance Method Details

#closeObject



120
121
122
# File 'lib/bin_tools/writer.rb', line 120

def close
  @f.close
end

#seek(pos) ⇒ Object



22
23
24
# File 'lib/bin_tools/writer.rb', line 22

def seek pos
  @f.seek pos
end

#tellObject



26
27
28
# File 'lib/bin_tools/writer.rb', line 26

def tell
  @f.tell
end

#write_bin(str) ⇒ Object



80
81
82
# File 'lib/bin_tools/writer.rb', line 80

def write_bin str
  @f.write str
end

#write_binswap(str) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/bin_tools/writer.rb', line 108

def write_binswap str
  togo = str.size
  pos = 0
  while togo > 0
    r = @rom[pos..(pos+3)].unpack('L>')
    d = r.pack('L<')
    write_bin d
    pos += 4
    togo -= 4
  end
end

#write_bool(val) ⇒ Object



43
44
45
# File 'lib/bin_tools/writer.rb', line 43

def write_bool val
  write_byte val ? 1 : 0
end

#write_byte(val) ⇒ Object



39
40
41
# File 'lib/bin_tools/writer.rb', line 39

def write_byte val
  @f.write [val].pack("C")
end

#write_f32_le(val) ⇒ Object



71
72
73
# File 'lib/bin_tools/writer.rb', line 71

def write_f32_le val
  @f.write [val].pack("e")
end

#write_s16_le(val) ⇒ Object



51
52
53
# File 'lib/bin_tools/writer.rb', line 51

def write_s16_le val
  @f.write [val].pack("s<")
end

#write_s32_le(val) ⇒ Object



63
64
65
# File 'lib/bin_tools/writer.rb', line 63

def write_s32_le val
  @f.write [val].pack("l<")
end

#write_str(str) ⇒ Object



30
31
32
# File 'lib/bin_tools/writer.rb', line 30

def write_str str
  @f.write str
end

#write_str_varlen(str) ⇒ Object



34
35
36
37
# File 'lib/bin_tools/writer.rb', line 34

def write_str_varlen str
  write_varlen_le str.size
  @f.write str
end

#write_u16_be(val) ⇒ Object



55
56
57
# File 'lib/bin_tools/writer.rb', line 55

def write_u16_be val
  @f.write [val].pack("S>")
end

#write_u16_le(val) ⇒ Object



47
48
49
# File 'lib/bin_tools/writer.rb', line 47

def write_u16_le val
  @f.write [val].pack("S<")
end

#write_u24_be(val) ⇒ Object



75
76
77
78
# File 'lib/bin_tools/writer.rb', line 75

def write_u24_be val
  write_byte val >> 16
  write_u16_be val & 0xFFFF
end

#write_u32_be(val) ⇒ Object



67
68
69
# File 'lib/bin_tools/writer.rb', line 67

def write_u32_be val
  @f.write [val].pack("L>")
end

#write_u32_le(val) ⇒ Object



59
60
61
# File 'lib/bin_tools/writer.rb', line 59

def write_u32_le val
  @f.write [val].pack("L<")
end

#write_varlen_be(val) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bin_tools/writer.rb', line 84

def write_varlen_be val
  out = [val & 0x7F]
  val = val >> 7
  while val > 0
      out << (val & 0x7f) + 0x80
      val = val >> 7
  end
  out.reverse.each do |x|
    write_byte x
  end
end

#write_varlen_le(val) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/bin_tools/writer.rb', line 96

def write_varlen_le val
  out = [val & 0x7F]
  val = val >> 7
  while val > 0
      out << (val & 0x7f)
      val = val >> 7
  end
  out.each_with_index do |x, i|
    write_byte(x + ((i == out.size - 1) ? 0 : 0x80))
  end
end