Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / swig / gen-swig-bug-fix
1 #!/usr/bin/env python
2 #
3 # Copyright 2004 Free Software Foundation, Inc.
4
5 # This file is part of GNU Radio
6
7 # GNU Radio is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2, or (at your option)
10 # any later version.
11
12 # GNU Radio is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with GNU Radio; see the file COPYING.  If not, write to
19 # the Free Software Foundation, Inc., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
21
22
23 import sys
24 import re
25
26 def write_header (f):
27     f.write ('''/* -*- c++ -*- */
28 /*
29  * Copyright 2004 Free Software Foundation, Inc.
30  * 
31  * This file is part of GNU Radio
32  * 
33  * GNU Radio is free software; you can redistribute it and/or modify
34  * it under the terms of the GNU General Public License as published by
35  * the Free Software Foundation; either version 2, or (at your option)
36  * any later version.
37  * 
38  * GNU Radio is distributed in the hope that it will be useful,
39  * but WITHOUT ANY WARRANTY; without even the implied warranty of
40  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
41  * GNU General Public License for more details.
42  * 
43  * You should have received a copy of the GNU General Public License
44  * along with GNU Radio; see the file COPYING.  If not, write to
45  * the Free Software Foundation, Inc., 51 Franklin Street,
46  * Boston, MA 02110-1301, USA.
47  */
48
49 #ifndef INCLUDED_GNURADIO_SWIG_BUG_WORKAROUND_H
50 #define INCLUDED_GNURADIO_SWIG_BUG_WORKAROUND_H
51
52 /*
53  * This include files works around a bug in SWIG 1.3.21 and 22
54  * where it fails to emit these declarations when doing
55  * %import "gnuradio.i"
56  */
57
58 ''')
59     
60 def write_trailer (f):
61     f.write ('''
62 #endif /* INCLUDED_GNURADIO_SWIG_BUG_WORKAROUND_H */
63 ''')
64
65 def doit (input, output):
66     re_RULES_BEGIN = re.compile ('RULES \(BEGIN\)')
67     re_RULES_END = re.compile ('RULES \(END\)')
68     re_RETURN = re.compile ('^\s*return')
69     re_NOT_ID = re.compile ('[^a-zA-Z0-9_]')
70     words = {}
71     
72     write_header (output)
73     for line in input:
74         if re_RULES_BEGIN.search (line):
75             break
76
77     for line in input:
78         if re_RULES_END.search (line):
79             break
80         if not re_RETURN.match (line):
81             continue
82         line = re_NOT_ID.sub (' ', line)
83         line = re.sub (' +', ' ', line)
84         for w in line.split (' '):
85             words[w] = 1
86
87     for w in ('', 'return', 'void', 'x'):
88         del words[w]
89     
90     wl = words.keys()
91     wl.sort ()
92     for w in wl:
93         output.write ('class ' + w + ';\n')
94
95     write_trailer (output)
96     
97
98 def main ():
99     if len (sys.argv) != 3:
100         sys.stderr.write ("usage: %s gnuradio_swig_python.cc gnuradio_swig_bug_workaround.h\n"
101                           % (sys.argv[0],))
102         sys.exit (1)
103     input_filename = sys.argv[1]
104     output_filename = sys.argv[2]
105     input = open (input_filename, "r")
106     output = open (output_filename, "w")
107     doit (input, output)
108
109 if __name__ == '__main__':
110     main ()
111