# ode.py
import
os
odeSourceDir
=
"C:\\PHYSICS\\ode-0.11.1\\include\\ode\\"
# typical format 'ODE_API dGeomID dBodyGetFirstGeom (dBodyID b);'
dllfunlist
=
[]
# Our list of functions, params and return values which
# we'll save as we go along
def
EquivalentArg(
p
):
# Using IntPtr for c# for pointers etc
p
=
p.replace(
"dJointID",
"IntPtr/*dJointID*/")
p
=
p.replace(
"dWorldID",
"IntPtr/*dWorldID*/")
p
=
p.replace(
"dBodyID",
"IntPtr/*dBodyID*/"
)
p
=
p.replace(
"dGeomID",
"IntPtr/*dGeomID*/"
)
p
=
p.replace(
"dSpaceID",
"IntPtr/*dSpaceID*/"
)
p
=
p.replace(
"dReal",
"float/*dReal*/"
)
p
=
p.replace(
"dQuaternion",
"
float[]/*dQuaternion*/"
)
p
=
p.replace(
"dBodyID",
"IntPtr/*dBodyID*/"
)
p
=
p.replace(
"unsigned int",
"UInt32"
)
p
=
p.strip()
# remove any spaces
return
p
fileList
=
os.listdir(odeSourceDir)
for
fileName
in
fileList:
if
len(fileName)
<
3:
continue
if
not
fileName[-2:]
==
".h":
continue
fp
=
open(odeSourceDir
+
fileName)
s
=
fp.read()
lines
=
s.split("\n")
fp.close()
for
i
in
range(0,len(lines)):
line
=
lines[i]
if
line[:7]
==
"ODE_API":
closeB
=
line.find(")")
while
closeB
<
0:
line
=
line
+
lines[i+1]
# parameters on multiple lines
closeB
=
line.find(")")
i
=
i
+
1
openB
=
line.find("(")
if
openB<0
or
closeB<0:
# BUG - multiple lines
1/0
lcomment
=
line.find("/*");
rcomment
=
line.find("*/")
while
(lcomment>=0
and
rcomment>=0):
line
=
line[:lcomment]
+
line[rcomment+2:]
lcomment
=
line.find("/*");
rcomment
=
line.find("*/")
comment
=
line.find("//")
if
(comment>=0):
line
=
line[:comment]
line
=
line.strip()
line
=
line.replace("
*",
"* ")
line
=
line.replace("
*",
"*")
line
=
line.replace("const",
"")
# ignore consts
line
=
line.replace("ODE_API","").strip()
openB
=
line.find("(")
closeB
=
line.find(")")
params
=
line[openB:closeB+1]
front
=
line[:openB].strip()
funcNameLeft
=
front.rfind("
")
funcName
=
line[funcNameLeft:openB]
returnarg
=
line[:funcNameLeft].strip()
returnarg
=
EquivalentArg(
returnarg
)
params
=
params.replace("(",
"")
params
=
params.replace(")",
"")
params
=
params.replace("\t",
"")
paramArgs
=
params.split(",")
# tidy up the args
outParamArgs
=
[]
for
p
in
paramArgs:
p
=
EquivalentArg(
p
)
outParamArgs.append(
p
)
#
store them in a list for use later
dllfunlist.append(
[returnarg,
funcName,
outParamArgs]
)
# SPIT OUT THE LIST OF FUNCTION
# redirect it to a txt file so you can use it..e.g. ode.py > myfile.txt
print
"using System.Runtime.InteropServices; // DLL support\n\n"
print
"public class Ode_c\n{"
print
'\tconst string dllName = "C:\\PHYSICS\\ode-0.11.1\\lib\\DebugSingleDLL\\ode_singled.dll";\n\n'
for
func
in
dllfunlist:
returnarg
=
func[0]
funcname
=
func[1]
params
=
func[2]
out
=
""
out
+=
"\t//[DllImport(dllName, CallingConvention=CallingConvention.Cdecl)]\n"
out
+=
"\t//public static extern "
out
+=
returnarg
+
"
"
out
+=
funcname
out
+=
"("
for
i
in
range(0,len(params)):
out
+=
params[i].strip()
if
(i<len(params)-1):
out
+=
","
out
+=
");"
out
+=
"\n"
print
out
print
"} // End Odedll_c" |