aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/farmhash/split.py
blob: 083eae63f61a69b4094d39885b2399bf924fed2a (plain) (blame)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python 
 
 
END_PREFIX = '}  // namespace ' 
TEST_PREFIX = '#if FARMHASHSELFTEST' 
 
 
def main(): 
    with open('farmhash.cc', 'rb') as input_file: 
        lines = input_file.readlines() 
 
    namespace_out = None 
 
    prev_name = None 
 
    output = open('common.h', 'wb') 
    output.write('#pragma once\n\n') 
 
    def write_common(): 
        output.write('#include "common.h"\n\n') 
 
    def write_include(include): 
        if include: 
            output.write('namespace {\n') 
            output.write('    ' + '#include "{}"\n'.format(include)) 
            output.write('}\n\n') 
 
    write_test = False 
 
    for line in lines: 
 
        if line.startswith(TEST_PREFIX): 
            write_test = True 
            output.close() 
            output = open('test.cc', 'wb') 
            write_common() 
            write_include('farmhash_iface.h') 
 
        if write_test: 
            output.write(line) 
            continue 
 
        elif line.startswith('namespace '): 
            namespace = line.split(' ')[1] 
 
            if namespace.startswith('farmhash'): 
                filename = namespace + '.cc' 
 
                output.close() 
                output = open(filename, 'wb') 
                write_common() 
                write_include(prev_name) 
 
                output.write(line) 
                prev_name = filename 
 
            else: 
                if prev_name is not None: 
                    output.close() 
                    output = open('farmhash_iface.cc', 'wb') 
                    write_common() 
                    write_include(prev_name) 
                    prev_name = None 
 
                output.write(line) 
 
        else: 
            output.write(line) 
 
    output.close() 
 
 
if __name__ == '__main__': 
    main()