Module: Fiddley::Utils
- Included in:
- Function, Library, MemoryPointer, Struct, Struct
- Defined in:
- lib/fiddley/utils.rb
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
- .str2value(type, str) ⇒ Object
- .type2offset_size(type) ⇒ Object
- .type2size(type) ⇒ Object
- .type2str(type) ⇒ Object
- .type2type(type) ⇒ Object
- .value2str(type, value) ⇒ Object
Class Method Details
.str2value(type, str) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/fiddley/utils.rb', line 71 module_function def str2value(type, str) case type when :char, :int8 str.unpack1('c') when :uchar, :uint8 str.unpack1('C') when :short, :int16 str.unpack1('s') when :ushort, :uint16 str.unpack1('S') when :int32 str.unpack1('l') when :uint32 str.unpack1('L') when :int str.unpack1('i!') when :uint str.unpack1('I!') when :bool str.unpack1('i!') != 0 when :long str.unpack1('l!') when :ulong str.unpack1('L!') when :long_long, :int64 str.unpack1('q') when :ulong_long, :uint64 str.unpack1('Q') when :size_t str.unpack1(SIZET_FORMAT) when :float str.unpack1('f') when :double str.unpack1('d') when :string, :pointer str.unpack1('p') else raise RuntimeError, "unknown type #{type}" end end |
.type2offset_size(type) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/fiddley/utils.rb', line 46 module_function def type2offset_size(type) case type when :char, :uchar, :int8, :uint8 Fiddle::ALIGN_CHAR when :short, :ushort, :int16, :uint16 Fiddle::ALIGN_SHORT when :int, :uint, :int32, :uint32, :bool Fiddle::ALIGN_INT when :long, :ulong Fiddle::ALIGN_LONG when :int64, :uint64, :long_long, :ulong_long Fiddle::ALIGN_LONG_LONG when :float Fiddle::ALIGN_FLOAT when :double Fiddle::ALIGN_DOUBLE when :size_t Fiddle::ALIGN_SIZE_T when :string, :pointer Fiddle::ALIGN_VOIDP else raise RuntimeError, "unknown type #{type}" end end |
.type2size(type) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/fiddley/utils.rb', line 21 module_function 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 RuntimeError, "unknown type #{type}" end end |
.type2str(type) ⇒ Object
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 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/fiddley/utils.rb', line 153 module_function def type2str(type) case type when :char, :int8 "char" when :uchar, :uint8 "unsigned char" when :short, :int16 "short" when :ushort, :uint16 "unsigned short" when :int, :int32 "int" when :uint, :uint32 "unsigned int" when :bool "int" when :long "long" when :ulong "unsigned long" when :long_long, :int64 "long long" when :ulong_long, :uint64 "unsigned long long" when :size_t SIZET_TYPE when :float "float" when :double "double" when :string, :pointer "void *" else if @enums && @enums.find{|e| type == e.tag} "int" else type.to_s end end end |
.type2type(type) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/fiddley/utils.rb', line 194 module_function 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 RuntimeError, "unknown type #{type}" end end |
.value2str(type, value) ⇒ Object
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 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/fiddley/utils.rb', line 112 module_function def value2str(type, value) case type when :char, :int8 [value].pack('c') when :uchar, :uint8 [value].pack('C') when :short, :int16 [value].pack('s') when :ushort, :uint16 [value].pack('S') when :int32 [value].pack('l') when :uint32 [value].pack('L') when :int [value].pack('i!') when :uint [value].pack('I!') when :bool [value ? 1 : 0].pack('i!') when :long [value].pack('l!') when :ulong [value].pack('L!') when :long_long, :int64 [value].pack('q') when :ulong_long, :uint64 [value].pack('Q') when :size_t [value].pack(SIZET_FORMAT) when :float [value].pack('f') when :double [value].pack('d') when :string, :pointer [value].pack('p') else raise RuntimeError, "unknown type #{type}" end end |