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