File: fix_guards.py

package info (click to toggle)
rlvm 0.14-5.2
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 17,104 kB
  • sloc: cpp: 91,574; ansic: 39,346; perl: 768; sh: 320; python: 181; makefile: 8
file content (44 lines) | stat: -rw-r--r-- 883 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/python3

import sys
import os
import re

ifndef = re.compile('^\#ifndef')
define = re.compile('^\#define')
endif  = re.compile('^\#endif')

name = sys.argv[1]
bak_name = name + ".bak"

new_guard_name = name.replace('/', '_').replace('.', '_').upper()
new_guard_name += "_"

try:
  os.rename(name, bak_name)

  input = file(bak_name, 'r')
  output = file(name, 'w')

  for line in input:
    m = ifndef.search(line)
    if m is not None:
      output.write('#ifndef ' + new_guard_name + '\n')
      continue

    m = define.search(line)
    if m is not None:
      output.write('#define ' + new_guard_name + '\n')
      continue

    m = endif.search(line)
    if m is not None:
      output.write('#endif  // ' + new_guard_name + '\n')
      continue

    output.write(line)

  input.close()
  output.close()
except IOError:
  sys.stderr.write('Cannot open "%s"\n' % arg)