class BinTools::Writer

8 16 32 64 C S L Q - unsigned c s l q - unsigned < LE > BE A - binary string z - null terminated H - hex string

Public Class Methods

new(fn) click to toggle source
# File lib/bin_tools/writer.rb, line 12
def initialize fn
  @f = File.new fn, "wb"
end
open(fn, &block) click to toggle source
# File lib/bin_tools/writer.rb, line 16
def self.open fn, &block
  f = self.new fn
  block.call f
  f.close
end

Public Instance Methods

close() click to toggle source
# File lib/bin_tools/writer.rb, line 120
def close
  @f.close
end
seek(pos) click to toggle source
# File lib/bin_tools/writer.rb, line 22
def seek pos
  @f.seek pos
end
tell() click to toggle source
# File lib/bin_tools/writer.rb, line 26
def tell
  @f.tell
end
write_bin(str) click to toggle source
# File lib/bin_tools/writer.rb, line 80
def write_bin str
  @f.write str
end
write_binswap(str) click to toggle source
# 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) click to toggle source
# File lib/bin_tools/writer.rb, line 43
def write_bool val
  write_byte val ? 1 : 0
end
write_byte(val) click to toggle source
# File lib/bin_tools/writer.rb, line 39
def write_byte val
  @f.write [val].pack("C")
end
write_f32_le(val) click to toggle source
# File lib/bin_tools/writer.rb, line 71
def write_f32_le val
  @f.write [val].pack("e")
end
write_s16_le(val) click to toggle source
# File lib/bin_tools/writer.rb, line 51
def write_s16_le val
  @f.write [val].pack("s<")
end
write_s32_le(val) click to toggle source
# File lib/bin_tools/writer.rb, line 63
def write_s32_le val
  @f.write [val].pack("l<")
end
write_str(str) click to toggle source
# File lib/bin_tools/writer.rb, line 30
def write_str str
  @f.write str
end
write_str_varlen(str) click to toggle source
# 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) click to toggle source
# File lib/bin_tools/writer.rb, line 55
def write_u16_be val
  @f.write [val].pack("S>")
end
write_u16_le(val) click to toggle source
# File lib/bin_tools/writer.rb, line 47
def write_u16_le val
  @f.write [val].pack("S<")
end
write_u24_be(val) click to toggle source
# 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) click to toggle source
# File lib/bin_tools/writer.rb, line 67
def write_u32_be val
  @f.write [val].pack("L>")
end
write_u32_le(val) click to toggle source
# File lib/bin_tools/writer.rb, line 59
def write_u32_le val
  @f.write [val].pack("L<")
end
write_varlen_be(val) click to toggle source
# 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) click to toggle source
# 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