blob: f231a5d8617f6ed0a105c44e6c94494850fb0e55 [file] [log] [blame]
Damien George55baff42014-01-21 21:40:13 +00001import argparse
2import re
Damien George1976bae2014-01-24 22:22:00 +00003
4# codepoint2name is different in Python 2 to Python 3
5import platform
6if platform.python_version_tuple()[0] == '2':
7 from htmlentitydefs import codepoint2name
8elif platform.python_version_tuple()[0] == '3':
9 from html.entities import codepoint2name
Damien George55baff42014-01-21 21:40:13 +000010
Damien Georgea71c83a2014-02-15 11:34:50 +000011# add some custom names to map characters that aren't in HTML
12codepoint2name[ord('.')] = 'dot'
13
Damien George55baff42014-01-21 21:40:13 +000014# this must match the equivalent function in qstr.c
15def compute_hash(qstr):
16 hash = 0
17 for char in qstr:
18 hash += ord(char)
19 return hash & 0xffff
20
21def do_work(infiles):
22 # read the qstrs in from the input files
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020023 qstrs = {}
Damien George55baff42014-01-21 21:40:13 +000024 for infile in infiles:
25 with open(infile, 'rt') as f:
26 line_number = 0
27 for line in f:
28 line_number += 1
29 line = line.strip()
30
31 # ignore blank lines and comments
32 if len(line) == 0 or line.startswith('//'):
33 continue
34
35 # verify line is of the correct form
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020036 match = re.match(r'Q\((.+)\)$', line)
Damien George55baff42014-01-21 21:40:13 +000037 if not match:
38 print('({}:{}) bad qstr format, got {}'.format(infile, line_number, line))
39 return False
40
41 # get the qstr value
42 qstr = match.group(1)
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020043 ident = re.sub(r'[^A-Za-z0-9_]', lambda s: "_" + codepoint2name[ord(s.group(0))] + "_", qstr)
Damien George55baff42014-01-21 21:40:13 +000044
45 # don't add duplicates
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020046 if ident in qstrs:
Damien George55baff42014-01-21 21:40:13 +000047 continue
48
Damien George1976bae2014-01-24 22:22:00 +000049 # add the qstr to the list, with order number to retain original order in file
50 qstrs[ident] = (len(qstrs), ident, qstr)
Damien George55baff42014-01-21 21:40:13 +000051
52 # process the qstrs, printing out the generated C header file
53 print('// This file was automatically generated by makeqstrdata.py')
Dave Hylands7a996b12014-01-21 15:28:27 -080054 print('')
Damien George1976bae2014-01-24 22:22:00 +000055 for order, ident, qstr in sorted(qstrs.values(), key=lambda x: x[0]):
Damien George55baff42014-01-21 21:40:13 +000056 qhash = compute_hash(qstr)
57 qlen = len(qstr)
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020058 print('Q({}, (const byte*)"\\x{:02x}\\x{:02x}\\x{:02x}\\x{:02x}" "{}")'.format(ident, qhash & 0xff, (qhash >> 8) & 0xff, qlen & 0xff, (qlen >> 8) & 0xff, qstr))
Damien George55baff42014-01-21 21:40:13 +000059
60 return True
61
62def main():
63 arg_parser = argparse.ArgumentParser(description='Process raw qstr file and output qstr data with length, hash and data bytes')
64 arg_parser.add_argument('files', nargs='+', help='input file(s)')
65 args = arg_parser.parse_args()
66
67 result = do_work(args.files)
68 if not result:
69 print('exiting with error code')
70 exit(1)
71
72if __name__ == "__main__":
73 main()