Module: Libcall::Fiddley::Utils
- Defined in:
- lib/libcall/fiddley.rb
Class Method Summary collapse
-
.array2str(type, array) ⇒ Object
Convert Ruby array of numbers to a binary string for the given type.
-
.array_pack_template(type) ⇒ Object
Pack template for array values of given base type.
-
.sizeof(type) ⇒ Object
Return size in bytes for a given type symbol.
-
.str2array(type, str) ⇒ Object
Convert binary string to Ruby array of the given type.
-
.to_fiddle_type(type) ⇒ Object
Convert a type symbol to a Fiddle type constant.
Class Method Details
.array2str(type, array) ⇒ Object
Convert Ruby array of numbers to a binary string for the given type
70 71 72 |
# File 'lib/libcall/fiddley.rb', line 70 def array2str(type, array) array.pack("#{array_pack_template(type)}*") end |
.array_pack_template(type) ⇒ Object
Pack template for array values of given base type.
63 64 65 66 67 |
# File 'lib/libcall/fiddley.rb', line 63 def array_pack_template(type) Libcall::TypeMap.pack_template(type) rescue StandardError => e raise Libcall::Error, "Unsupported array base type: #{type} (#{e.})" end |
.sizeof(type) ⇒ Object
Return size in bytes for a given type symbol.
49 50 51 52 53 |
# File 'lib/libcall/fiddley.rb', line 49 def sizeof(type) Libcall::TypeMap.sizeof(type) rescue StandardError => e raise Libcall::Error, "unknown type for sizeof: #{type} (#{e.})" end |
.str2array(type, str) ⇒ Object
Convert binary string to Ruby array of the given type
75 76 77 |
# File 'lib/libcall/fiddley.rb', line 75 def str2array(type, str) str.unpack("#{array_pack_template(type)}*") end |
.to_fiddle_type(type) ⇒ Object
Convert a type symbol to a Fiddle type constant.
56 57 58 59 60 |
# File 'lib/libcall/fiddley.rb', line 56 def to_fiddle_type(type) Libcall::TypeMap.to_fiddle_type(type) rescue StandardError => e raise Libcall::Error, "unknown type for to_fiddle_type: #{type} (#{e.})" end |