forked from horndude77/leptonica-ruby-ffi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_header.rb
executable file
·77 lines (69 loc) · 1.66 KB
/
convert_header.rb
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
#!/usr/bin/env ruby
#
#This simple file is used to convert leptprotos.h (and possibly other header
#files) into a list of function signatures that ruby ffi can understand.
#
require 'ffi'
class Signature < Struct.new(:name, :return_type, :arguments)
def to_s
"[:#{name}, [#{arguments.map{|a|a.inspect}.join(', ')}], #{return_type.inspect}]"
end
end
class L_RB_TYPE < FFI::Union
layout :itype, :int64,
:utype, :uint64,
:ftype, :double,
:ptype, :pointer
end
TYPE_MAPPING = {
'l_uint8' => :uint8,
'l_uint16' => :uint16,
'l_int32' => :int32,
'l_uint32' => :uint32,
'l_uint64' => :uint64,
'l_float32' => :float,
'l_float64' => :double,
'void' => :void,
'char' => :char,
'size_t' => :uint64,
'L_TIMER' => :pointer,
'RB_TYPE' => L_RB_TYPE,
'alloc_fn' => :pointer,
'dealloc_fn' => :pointer
}
def get_type(s)
if(s =~ /\*/)
:pointer
else
TYPE_MAPPING[s.split(' ').first]
end
end
signatures = []
header_file = ARGV[0]
File.open(header_file, 'r') do |f|
f.each do |line|
if(line =~ /\w\w*\s*(.*);\s*$/)
if(line =~ /extern/)
line['extern'] = ''
end
if(line =~ /const/)
line['const'] = ''
end
if(line =~ /LEPT_DLL/)
line['LEPT_DLL'] = ''
end
first, mid, last = line.split(/[\(\)]/)
name = first.split(' ').last
return_type = get_type(first)
arguments = []
mid.strip!
if(mid.length > 0 && mid != 'void')
arguments = mid.split(',').map{|s|get_type(s)}
end
signatures << Signature.new(name, return_type, arguments)
end
end
end
print " [\n "
puts signatures.join(",\n ")
puts " ]"