1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 95 96 97 98 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 139 140 141 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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 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 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
# -*- ruby -*-
require 'dl'
module DL
class Types
TYPES = [
# FORMAT:
# ["alias name",
# "type name", encoding_method, decoding_method, for function prototypes
# "type name", encoding_method, decoding_method] for structures (not implemented)
# for Windows
["DWORD", "unsigned long", nil, nil,
"unsigned long", nil, nil],
["PDWORD", "unsigned long *", nil, nil,
"unsigned long *", nil, nil],
["WORD", "unsigned short", nil, nil,
"unsigned short", nil, nil],
["PWORD", "unsigned int *", nil, nil,
"unsigned int *", nil, nil],
["BYTE", "unsigned char", nil, nil,
"unsigned char", nil, nil],
["PBYTE", "unsigned char *", nil, nil,
"unsigned char *", nil, nil],
["BOOL", "ibool", nil, nil,
"ibool", nil, nil],
["ATOM", "int", nil, nil,
"int", nil, nil],
["BYTE", "unsigned char", nil, nil,
"unsigned char", nil, nil],
["PBYTE", "unsigned char *", nil, nil,
"unsigned char *", nil, nil],
["UINT", "unsigned int", nil, nil,
"unsigned int", nil, nil],
["ULONG", "unsigned long", nil, nil,
"unsigned long", nil, nil],
["UCHAR", "unsigned char", nil, nil,
"unsigned char", nil, nil],
["HANDLE", "unsigned long", nil, nil,
"unsigned long", nil, nil],
["PHANDLE","void*", nil, nil,
"void*", nil, nil],
["PVOID", "void*", nil, nil,
"void*", nil, nil],
["LPCSTR", "char*", nil, nil,
"char*", nil, nil],
["HDC", "unsigned int", nil, nil,
"unsigned int", nil, nil],
["HWND", "unsigned int", nil, nil,
"unsigned int", nil, nil],
# Others
["uint", "unsigned int", nil, nil,
"unsigned int", nil, nil],
["u_int", "unsigned int", nil, nil,
"unsigned int", nil, nil],
["ulong", "unsigned long", nil, nil,
"unsigned long", nil, nil],
["u_long", "unsigned long", nil, nil,
"unsigned long", nil, nil],
# DL::Importable primitive types
["ibool",
"I",
proc{|v| v ? 1 : 0},
proc{|v| (v != 0) ? true : false},
"I",
proc{|v| v ? 1 : 0 },
proc{|v| (v != 0) ? true : false} ],
["cbool",
"C",
proc{|v| v ? 1 : 0},
proc{|v| (v != 0) ? true : false},
"C",
proc{|v,len| v ? 1 : 0},
proc{|v,len| (v != 0) ? true : false}],
["lbool",
"L",
proc{|v| v ? 1 : 0},
proc{|v| (v != 0) ? true : false},
"L",
proc{|v,len| v ? 1 : 0},
proc{|v,len| (v != 0) ? true : false}],
["unsigned char",
"C",
proc{|v| [v].pack("C").unpack("c")[0]},
proc{|v| [v].pack("c").unpack("C")[0]},
"C",
proc{|v| [v].pack("C").unpack("c")[0]},
proc{|v| [v].pack("c").unpack("C")[0]}],
["unsigned short",
"H",
proc{|v| [v].pack("S").unpack("s")[0]},
proc{|v| [v].pack("s").unpack("S")[0]},
"H",
proc{|v| [v].pack("S").unpack("s")[0]},
proc{|v| [v].pack("s").unpack("S")[0]}],
["unsigned int",
"I",
proc{|v| [v].pack("I").unpack("i")[0]},
proc{|v| [v].pack("i").unpack("I")[0]},
"I",
proc{|v| [v].pack("I").unpack("i")[0]},
proc{|v| [v].pack("i").unpack("I")[0]}],
["unsigned long",
"L",
proc{|v| [v].pack("L").unpack("l")[0]},
proc{|v| [v].pack("l").unpack("L")[0]},
"L",
proc{|v| [v].pack("L").unpack("l")[0]},
proc{|v| [v].pack("l").unpack("L")[0]}],
["unsigned char ref",
"c",
proc{|v| [v].pack("C").unpack("c")[0]},
proc{|v| [v].pack("c").unpack("C")[0]},
nil, nil, nil],
["unsigned int ref",
"i",
proc{|v| [v].pack("I").unpack("i")[0]},
proc{|v| [v].pack("i").unpack("I")[0]},
nil, nil, nil],
["unsigned long ref",
"l",
proc{|v| [v].pack("L").unpack("l")[0]},
proc{|v| [v].pack("l").unpack("L")[0]},
nil, nil, nil],
["char ref", "c", nil, nil,
nil, nil, nil],
["short ref", "h", nil, nil,
nil, nil, nil],
["int ref", "i", nil, nil,
nil, nil, nil],
["long ref", "l", nil, nil,
nil, nil, nil],
["float ref", "f", nil, nil,
nil, nil, nil],
["double ref","d", nil, nil,
nil, nil, nil],
["char", "C", nil, nil,
"C", nil, nil],
["short", "H", nil, nil,
"H", nil, nil],
["int", "I", nil, nil,
"I", nil, nil],
["long", "L", nil, nil,
"L", nil, nil],
["float", "F", nil, nil,
"F", nil, nil],
["double", "D", nil, nil,
"D", nil, nil],
[/^char\s*\*$/,"s",nil, nil,
"S",nil, nil],
[/^const char\s*\*$/,"S",nil, nil,
"S",nil, nil],
[/^.+\*$/, "P", nil, nil,
"P", nil, nil],
[/^.+\[\]$/, "a", nil, nil,
"a", nil, nil],
["void", "0", nil, nil,
nil, nil, nil],
]
def initialize
init_types()
end
def typealias(ty1, ty2, enc=nil, dec=nil, ty3=nil, senc=nil, sdec=nil)
@TYDEFS.unshift([ty1, ty2, enc, dec, ty3, senc, sdec])
end
def init_types
@TYDEFS = TYPES.dup
end
def encode_argument_type(alias_type)
proc_encode = nil
proc_decode = nil
@TYDEFS.each{|aty,ty,enc,dec,_,_,_|
if( (aty.is_a?(Regexp) && (aty =~ alias_type)) || (aty == alias_type) )
alias_type = alias_type.gsub(aty,ty) if ty
alias_type.strip! if alias_type
if( proc_encode )
if( enc )
conv1 = proc_encode
proc_encode = proc{|v| enc.call(conv1.call(v))}
end
else
if( enc )
proc_encode = enc
end
end
if( proc_decode )
if( dec )
conv2 = proc_decode
proc_decode = proc{|v| dec.call(conv2.call(v))}
end
else
if( dec )
proc_decode = dec
end
end
end
}
return [alias_type, proc_encode, proc_decode]
end
def encode_return_type(ty)
ty, enc, dec = encode_argument_type(ty)
return [ty, enc, dec]
end
def encode_struct_type(alias_type)
proc_encode = nil
proc_decode = nil
@TYDEFS.each{|aty,_,_,_,ty,enc,dec|
if( (aty.is_a?(Regexp) && (aty =~ alias_type)) || (aty == alias_type) )
alias_type = alias_type.gsub(aty,ty) if ty
alias_type.strip! if alias_type
if( proc_encode )
if( enc )
conv1 = proc_encode
proc_encode = proc{|v| enc.call(conv1.call(v))}
end
else
if( enc )
proc_encode = enc
end
end
if( proc_decode )
if( dec )
conv2 = proc_decode
proc_decode = proc{|v| dec.call(conv2.call(v))}
end
else
if( dec )
proc_decode = dec
end
end
end
}
return [alias_type, proc_encode, proc_decode]
end
end # end of Types
end
|