Imported Upstream version 3.2.2
[debian/gnuradio] / pmt / src / lib / generate_unv.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2006 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 3, 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 """
24 Generate code for uniform numeric vectors
25 """
26
27 import re, os, os.path
28
29
30 unv_types = (
31     ('u8', 'uint8_t'),
32     ('s8', 'int8_t'),
33     ('u16', 'uint16_t'),
34     ('s16', 'int16_t'),
35     ('u32', 'uint32_t'),
36     ('s32', 'int32_t'),
37     ('u64', 'uint64_t'),
38     ('s64', 'int64_t'),
39     ('f32', 'float'),
40     ('f64', 'double'),
41     ('c32', 'std::complex<float>'),
42     ('c64', 'std::complex<double>')
43     )
44
45 header = """\
46 /* -*- c++ -*- */
47 /*
48  * Copyright 2006 Free Software Foundation, Inc.
49  * 
50  * This file is part of GNU Radio
51  * 
52  * GNU Radio is free software; you can redistribute it and/or modify
53  * it under the terms of the GNU General Public License as published by
54  * the Free Software Foundation; either version 3, or (at your option)
55  * any later version.
56  * 
57  * GNU Radio is distributed in the hope that it will be useful,
58  * but WITHOUT ANY WARRANTY; without even the implied warranty of
59  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
60  * GNU General Public License for more details.
61  * 
62  * You should have received a copy of the GNU General Public License
63  * along with GNU Radio; see the file COPYING.  If not, write to
64  * the Free Software Foundation, Inc., 51 Franklin Street,
65  * Boston, MA 02110-1301, USA.
66  */
67 """
68
69 guard_tail = """
70 #endif
71 """
72
73 includes = """
74 #ifdef HAVE_CONFIG_H
75 #include <config.h>
76 #endif
77 #include <vector>
78 #include <pmt.h>
79 #include "pmt_int.h"
80
81 """
82
83 qa_includes = """
84 #include <qa_pmt_unv.h>
85 #include <cppunit/TestAssert.h>
86 #include <pmt.h>
87 #include <stdio.h>
88
89 """
90
91
92 # set srcdir to the directory that contains Makefile.am
93 try:
94     srcdir = os.environ['srcdir']
95 except KeyError, e:
96     srcdir = "."
97 srcdir = srcdir + '/'
98
99
100 def open_src (name, mode):
101     global srcdir
102     return open(os.path.join (srcdir, name), mode)
103
104
105 def guard_name(filename):
106     return 'INCLUDED_' + re.sub('\.', '_', filename.upper())
107
108 def guard_head(filename):
109     guard = guard_name(filename)
110     return """
111 #ifndef %s
112 #define %s
113 """ % (guard, guard)
114
115
116 def do_substitution (d, input, out_file):
117     def repl (match_obj):
118         key = match_obj.group (1)
119         # print key
120         return d[key]
121     
122     out = re.sub (r"@([a-zA-Z0-9_]+)@", repl, input)
123     out_file.write (out)
124
125
126 def generate_h():
127     template = open_src('unv_template.h.t', 'r').read()
128     output_filename = 'pmt_unv_int.h'
129     output = open(output_filename, 'w')
130     output.write(header)
131     output.write(guard_head(output_filename))
132     for tag, typ in unv_types:
133         d = { 'TAG' : tag, 'TYPE' : typ }
134         do_substitution(d, template, output)
135     output.write(guard_tail)
136
137 def generate_cc():
138     template = open_src('unv_template.cc.t', 'r').read()
139     output = open('pmt_unv.cc', 'w')
140     output.write(header)
141     output.write(includes)
142     for tag, typ in unv_types:
143         d = { 'TAG' : tag, 'TYPE' : typ }
144         do_substitution(d, template, output)
145
146
147 def generate_qa_h():
148     output_filename = 'qa_pmt_unv.h'
149     output = open(output_filename, 'w')
150     output.write(header)
151     output.write(guard_head(output_filename))
152
153     output.write('''
154 #include <cppunit/extensions/HelperMacros.h>
155 #include <cppunit/TestCase.h>
156
157 class qa_pmt_unv : public CppUnit::TestCase {
158
159   CPPUNIT_TEST_SUITE(qa_pmt_unv);
160 ''')
161     for tag, typ in unv_types:
162         output.write('  CPPUNIT_TEST(test_%svector);\n' % (tag,))
163     output.write('''\
164   CPPUNIT_TEST_SUITE_END();
165
166  private:
167 ''')     
168     for tag, typ in unv_types:
169         output.write('  void test_%svector();\n' % (tag,))
170     output.write('};\n')
171     output.write(guard_tail)
172
173 def generate_qa_cc():
174     template = open_src('unv_qa_template.cc.t', 'r').read()
175     output = open('qa_pmt_unv.cc', 'w')
176     output.write(header)
177     output.write(qa_includes)
178     for tag, typ in unv_types:
179         d = { 'TAG' : tag, 'TYPE' : typ }
180         do_substitution(d, template, output)
181     
182
183 def main():
184     generate_h()
185     generate_cc()
186     generate_qa_h()
187     generate_qa_cc()
188
189 if __name__ == '__main__':
190     main()