module GRCommons::Fiddley::Utils

NOTE: GR.rb supports 2.5 +. Unpack 1 does not work under 2.3.

Constants

SIZET_FORMAT

assumes short = 16bit, int = 32bit, long long = 64bit

SIZET_TYPE

Public Instance Methods

array2str(type, arr) click to toggle source

added

# File lib/gr_commons/fiddley.rb, line 99
def array2str(type, arr)
  case type
  when :char, :int8
    arr.pack('c*')
  when :uchar, :uint8
    arr.pack('C*')
  when :short, :int16
    arr.pack('s*')
  when :ushort, :uint16
    arr.pack('S*')
  when :int32
    arr.pack('l*')
  when :uint32
    arr.pack('L*')
  when :int
    arr.pack('i!*')
  when :uint
    arr.pack('I!*')
  when :bool
    [arr ? 1 : 0].pack('i!*')
  when :long
    arr.pack('l!*')
  when :ulong
    arr.pack('L!*')
  when :long_long, :int64
    arr.pack('q*')
  when :ulong_long, :uint64
    arr.pack('Q*')
  when :size_t
    arr.pack(SIZET_FORMAT)
  when :float
    arr.pack('f*')
  when :double
    arr.pack('d*')
  when :string, :pointer
    arr.pack('p*')
  else
    raise "unknown type #{type}"
  end
end
str2array(type, str) click to toggle source

added

# File lib/gr_commons/fiddley.rb, line 55
def str2array(type, str)
  case type
  when :char, :int8
    str.unpack('c*')
  when :uchar, :uint8
    str.unpack('C*')
  when :short, :int16
    str.unpack('s*')
  when :ushort, :uint16
    str.unpack('S*')
  when :int32
    str.unpack('l*')
  when :uint32
    str.unpack('L*')
  when :int
    str.unpack('i!*')
  when :uint
    str.unpack('I!*')
  when :bool
    str.unpack('i!*') != 0
  when :long
    str.unpack('l!*')
  when :ulong
    str.unpack('L!*')
  when :long_long, :int64
    str.unpack('q*')
  when :ulong_long, :uint64
    str.unpack('Q*')
  when :size_t
    str.unpack(SIZET_FORMAT)
  when :float
    str.unpack('f*')
  when :double
    str.unpack('d*')
  when :string, :pointer
    str.unpack('p*')
  else
    raise "unknown type #{type}"
  end
end
type2size(type) click to toggle source
# File lib/gr_commons/fiddley.rb, line 25
def type2size(type)
  case type
  when :char, :uchar, :int8, :uint8
    Fiddle::SIZEOF_CHAR
  when :short, :ushort, :int16, :uint16
    Fiddle::SIZEOF_SHORT
  when :int, :uint, :int32, :uint32, :bool
    Fiddle::SIZEOF_INT
  when :long, :ulong
    Fiddle::SIZEOF_LONG
  when :int64, :uint64, :long_long, :ulong_long
    Fiddle::SIZEOF_LONG_LONG
  when :float
    Fiddle::SIZEOF_FLOAT
  when :double
    Fiddle::SIZEOF_DOUBLE
  when :size_t
    Fiddle::SIZEOF_SIZE_T
  when :string, :pointer
    Fiddle::SIZEOF_VOIDP
  else
    raise "unknown type #{type}"
  end
end
type2type(type) click to toggle source

`type2str` is not used in GR.rb, so deleted.

# File lib/gr_commons/fiddley.rb, line 142
def type2type(type)
  case type
  when :char, :int8
    Fiddle::TYPE_CHAR
  when :uchar, :uint8
    -Fiddle::TYPE_CHAR
  when :short, :int16
    Fiddle::TYPE_SHORT
  when :ushort, :uint16
    -Fiddle::TYPE_SHORT
  when :int, :int32
    Fiddle::TYPE_INT
  when :uint, :uint32
    -Fiddle::TYPE_INT
  when :bool
    Fiddle::TYPE_INT
  when :long
    Fiddle::TYPE_LONG
  when :ulong
    -Fiddle::TYPE_LONG
  when :long_long, :int64
    Fiddle::TYPE_LONG_LONG
  when :ulong_long, :uint64
    -Fiddle::TYPE_LONG_LONG
  when :float
    Fiddle::TYPE_FLOAT
  when :double
    Fiddle::TYPE_DOUBLE
  when :size_t
    Fiddle::TYPE_SIZE_T
  when :string, :pointer
    Fiddle::TYPE_VOIDP
  when :void
    Fiddle::TYPE_VOID
  else
    raise "unknown type #{type}"
  end
end