• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2
3import sys
4import re
5
6header = '''// This file was extracted from the TCG Published
7// Trusted Platform Module Library
8// Part 4: Supporting Routines
9// Family "2.0"
10// Level 00 Revision 01.16
11// October 30, 2014
12
13'''
14
15head_spaces = re.compile('^\s*[0-9]+\s{0,4}')
16sec_num_in_comments = re.compile('^//\s+([A-D0-9]\.([0-9]\.?)+)')
17source_lines = open(sys.argv[1], 'r').read().splitlines()
18
19def postprocess_lines(buffer):
20    # get rid of heading line numbers and spaces.
21    postproc_buffer = []
22    for big_line in buffer:
23        big_line = head_spaces.sub('', big_line)
24        for line in big_line.splitlines():
25            m = sec_num_in_comments.match(line)
26            if m:
27                line = line.replace(m.groups()[0], '')
28            postproc_buffer.append(line)
29
30    return header + '\n'.join(postproc_buffer) + '\n'
31
32text = []
33for line in source_lines:
34    text.append(line)
35    if line == '' and text[-2].startswith('') and text[-5] == '':
36        text = text[:-5]
37
38func_file = None
39file_name = ''
40prev_num = 0
41line_buffer = []
42output_buffer = []
43for line in text:
44    f = re.match('^\s*[A-D0-9]+\.([0-9]+|([0-9]+\.)+)\s+(\S+\.[ch])$', line)
45    if not f:
46        f = re.match('^\s*[A-D0-9]+\.([0-9]+|([0-9]+\.)+)\s+[^\(]+\((\S+\.[ch])\)$', line)
47    if f:
48        file_name = f.groups(0)[2]
49        continue
50
51    num = re.match('^\s{0,3}([0-9]+)\s', line + ' ')
52    if num:
53        line_num = int(num.groups(0)[0])
54        if line_num == 1:
55            # this is the first line of a file
56            if func_file:
57                func_file.write(postprocess_lines(output_buffer))
58                func_file.close()
59            if file_name:
60                func_file = open('%s' % file_name, 'w')
61            output_buffer = [line,]
62            prev_num = 1
63            line_buffer = []
64            continue
65        if line_num == prev_num + 1:
66            if line_buffer:
67                output_buffer.append('\n'.join(line_buffer))
68                line_buffer = []
69            output_buffer.append(line)
70            prev_num = line_num
71        continue
72    if not '//' in line:
73        line = '//' + line
74    line_buffer.append(line)
75
76if func_file:
77    func_file.write(postprocess_lines(output_buffer))
78    func_file.close()
79