Update to 2.0.0 tree from current Fremantle build
[opencv] / utils / extract_doublepointers.py
1 #! /usr/bin/env python
2 """
3 This script checks the OpenCV headers to find methods that take double pointers
4 to OpenCV data structures as in/out parameters.
5
6 These methods need a special treatment and therefore SWIG typemaps are generated.
7 """
8
9 import sys
10
11 def convert_name(str):
12     count = 0
13     if (str[0:2] == "_p"):
14         p,s = convert_name(str[2:])
15         return(('*' + p),s)
16     return ('',str[1:])
17
18
19 if (sys.argv.__len__() < 1):
20     sys.exit(1)
21
22 infile = open(sys.argv[1],'r')
23
24 lines = infile.readlines()
25
26 infile.close()
27
28 foundit = 0
29 mytypes = []
30
31 for line in lines:
32     if (foundit == 0):
33         if (line.find('TYPES TABLE (BEGIN') > -1):
34             foundit = 1
35     else:
36         if (line.find('TYPES TABLE (END)') > -1):
37             foundit = 0
38         else:
39             stuff = line.split()
40             if (stuff.__len__() >= 3):
41                 if (stuff[0] == "#define"):
42                     mytypes.append(stuff[1][8:])
43
44 print """
45 /*//////////////////////////////////////////////////////////////////////////////////////////////////
46 // This file was automatically generated from the extract_doublepointers.py script found in the 
47 // 'utils' subdirectory of the OpenCV distribution.  Run it on the .cpp file generated by swig to
48 // generate the double pointer typemaps
49 /////////////////////////////////////////////////////////////////////////////////////////////////M*/
50 """
51
52 for mytype in mytypes:
53     p,s = convert_name(mytype)
54     if (p.__len__() >= 2):
55         print '%typemap(in)',s,p,' (void * vptr, $*1_ltype buffer) {'
56         print '\tif ((SWIG_ConvertPtr($input, &vptr, $*1_descriptor, 1)) == -1){'
57         print '\t\tSWIG_fail;';
58         print '\t}'
59         print '\tbuffer = ($*1_ltype) vptr;'
60         print '\t$1=&buffer;'
61         print '}'
62         #rez = "" + s + " " + p + 'getPtrTo' + s + '( ' +  s +  ' ' + p[1:] + 'input)'
63         #sys.stdout.write(rez)
64         #sys.stdout.write('\n{\n\t' + s + ' ' + p + 'rez = new ' + s + p[1:] + '();')
65         #sys.stdout.write('\n\t*rez =input;\n\treturn(rez);\n}\n')
66         #sys.stdout.write(rez)
67         #sys.stdout.write(';\n')
68
69
70 #    else:
71 #        print '/* No conversions needed for type ', s, ' */'
72
73