Fix an amazing number of typos & malformed sentences reported by Detlef
[python/dscho.git] / Tools / scripts / fixheader.py
blob774b907e4fdd0d4f2b15f170ca8181899f7d87a9
1 #! /usr/bin/env python
3 # Add some standard cpp magic to a header file
5 import sys
6 import string
8 def main():
9 args = sys.argv[1:]
10 for file in args:
11 process(file)
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, '*/'
49 main()