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
|
#!/usr/bin/ruby
# DLL ̃_Ci~bN[hpc[
# Ps NX
# Qs dll
# Rsڈȍ~ vg^Cv
# ȃt@C source
# ruby dllutl.rb < source
# ƂĂÅ dll _Ci~bN[hĂ
# \[X쐬ĂBA܂蕡GȊvg^Cvɂ
# ΉB () ݁B
#
# KpFPublic Domain
#
# Version 0.1p
#
# Daisuke Aoki <dai@y7.net>
def arg_ana(args)
list = []
temp = ""
if args.index(",")==nil
temp = args
else
temp = args.split(/,/)
end
for i in temp
if i == "void"
return []
end
if i =~ /\s*(.*)\s+(\S+)\s*/
rest = $2
if rest =~ /(\s*\**\s*\**\s*\**\s*\**\s*)(.*)/
list << $2
else
return nil
end
else
return nil
end
end
return list
end
class_name = "default_name"
dll_name = "default_name.dll"
class_name = gets.chomp
dll_name = gets.chomp
funclist = []
while gets
# if $_ =~ /(\s*[^\s]\s+\**)([^\(]+)(\(.*\))\;\s*/
# if $_ =~ /(\s*\S+\s+)([^\(]+)(\(.*\))\;\s*/
if $_ =~ /^#.*/
next
end
rest = $_
part_prev = ""
part_func = ""
part_post = ""
if rest =~ /\s*(.*)\s*(\(.*?\))\s*;/
rest = $1
part_post << $2
if rest =~ /\s*(.*)\s+(\S+)\s*/
part_prev << $1
rest = $2
if rest =~ /(\s*\**\s*\**\s*\**\s*\**\s*)(.*)/
part_prev << $1
part_func << $2
# printf("<%s><%s><%s>\n",part_prev,part_func,part_post)
end
end
end
if part_func!=""
funclist << [part_prev,part_func,part_post]
end
end
print "/***************************************************************\n"
printf " name: %s dll: %s \n",class_name,dll_name
print "***************************************************************/\n"
print "\n"
printf "extern int load_%s(void);\n",class_name
printf "extern void free_%s(void);\n",class_name
print "\n"
for i in funclist
printf "typedef %s(*type_%s)%s;\n",i[0],i[1],i[2]
end
printf "\nstatic struct %s_ {\n",class_name
for i in funclist
printf "\t type_%s %s;\n",i[1],i[1]
end
printf "} %s;\n\n",class_name
printf "static volatile HANDLE h_%s = NULL;\n\n",class_name
printf "void free_%s(void)
{
\tif(h_%s){
\t\tFreeLibrary(h_%s);
\t\th_%s = NULL;
\t}
}
",class_name,class_name,class_name,class_name
printf "int load_%s(void)
{
\tif(!h_%s){
\t\th_%s = LoadLibrary(\"%s\");
\t\tif(!h_%s) return -1;
\t}
",class_name,class_name,class_name,dll_name,class_name
for i in funclist
printf "\t%s.%s = (type_%s)GetProcAddress(h_%s,\"%s\");\n",class_name,i[1],i[1],class_name,i[1]
printf "\tif(!%s.%s){ free_%s(); return -1; }\n",class_name,i[1],class_name
end
printf "\treturn 0;\n}\n\n"
for i in funclist
printf "%s %s%s
{
\tif(h_%s){
",i[0],i[1],i[2],class_name
if i[0] =~ /\s*void\s*/
printf "\t\t%s.%s(",class_name,i[1]
else
printf "\t\treturn %s.%s(",class_name,i[1]
end
args = ""
if i[2] =~ /\((.*)\)/
args = $1.strip
end
arglist = arg_ana(args)
if arglist == nil
print "\n@@@ BAD @@@\n"
exit
end
num = 0
for j in arglist
if j != "void"
print "," if num!=0
print j
num += 1
end
end
print ");\n"
if i[0] =~ /\s*void\s*/
printf "\t}\n"
else
printf "\t}\n\treturn (%s)0;\n",i[0]
end
printf "}\n\n"
end
print "/***************************************************************/\n"
|