Module: GRCommons::Fiddley::Utils

Included in:
Function, MemoryPointer
Defined in:
lib/gr_commons/fiddley.rb

Overview

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

Constant Summary collapse

SIZET_FORMAT =

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

Fiddle::SIZEOF_VOIDP == Fiddle::SIZEOF_LONG ? 'l!' : 'q'
SIZET_TYPE =
Fiddle::SIZEOF_VOIDP == Fiddle::SIZEOF_LONG ? 'unsigned long' : 'unsigned long long'

Class Method Summary collapse

Class Method Details

.array2str(type, arr) ⇒ Object

added



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 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) ⇒ Object

added



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/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) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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) ⇒ Object

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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# 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