1 | n/a | #! /usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | # Add some standard cpp magic to a header file |
---|
4 | n/a | |
---|
5 | n/a | import sys |
---|
6 | n/a | |
---|
7 | n/a | def main(): |
---|
8 | n/a | args = sys.argv[1:] |
---|
9 | n/a | for filename in args: |
---|
10 | n/a | process(filename) |
---|
11 | n/a | |
---|
12 | n/a | def process(filename): |
---|
13 | n/a | try: |
---|
14 | n/a | f = open(filename, 'r') |
---|
15 | n/a | except IOError as msg: |
---|
16 | n/a | sys.stderr.write('%s: can\'t open: %s\n' % (filename, str(msg))) |
---|
17 | n/a | return |
---|
18 | n/a | data = f.read() |
---|
19 | n/a | f.close() |
---|
20 | n/a | if data[:2] != '/*': |
---|
21 | n/a | sys.stderr.write('%s does not begin with C comment\n' % filename) |
---|
22 | n/a | return |
---|
23 | n/a | try: |
---|
24 | n/a | f = open(filename, 'w') |
---|
25 | n/a | except IOError as msg: |
---|
26 | n/a | sys.stderr.write('%s: can\'t write: %s\n' % (filename, str(msg))) |
---|
27 | n/a | return |
---|
28 | n/a | sys.stderr.write('Processing %s ...\n' % filename) |
---|
29 | n/a | magic = 'Py_' |
---|
30 | n/a | for c in filename: |
---|
31 | n/a | if ord(c)<=0x80 and c.isalnum(): |
---|
32 | n/a | magic = magic + c.upper() |
---|
33 | n/a | else: magic = magic + '_' |
---|
34 | n/a | sys.stdout = f |
---|
35 | n/a | print('#ifndef', magic) |
---|
36 | n/a | print('#define', magic) |
---|
37 | n/a | print('#ifdef __cplusplus') |
---|
38 | n/a | print('extern "C" {') |
---|
39 | n/a | print('#endif') |
---|
40 | n/a | print() |
---|
41 | n/a | f.write(data) |
---|
42 | n/a | print() |
---|
43 | n/a | print('#ifdef __cplusplus') |
---|
44 | n/a | print('}') |
---|
45 | n/a | print('#endif') |
---|
46 | n/a | print('#endif /*', '!'+magic, '*/') |
---|
47 | n/a | |
---|
48 | n/a | if __name__ == '__main__': |
---|
49 | n/a | main() |
---|