blob: e60f0004401aaef8860bae1dc3bb71e05e8d95fb [file] [log] [blame]
Dave Hylands0308f962014-03-10 00:07:35 -07001from __future__ import print_function
2
Damien George55baff42014-01-21 21:40:13 +00003import argparse
4import re
Damien Georgefdf0da52014-03-08 15:03:25 +00005import sys
Damien George1976bae2014-01-24 22:22:00 +00006
7# codepoint2name is different in Python 2 to Python 3
8import platform
9if platform.python_version_tuple()[0] == '2':
10 from htmlentitydefs import codepoint2name
11elif platform.python_version_tuple()[0] == '3':
12 from html.entities import codepoint2name
Damien George55baff42014-01-21 21:40:13 +000013
Damien Georgea71c83a2014-02-15 11:34:50 +000014# add some custom names to map characters that aren't in HTML
15codepoint2name[ord('.')] = 'dot'
Paul Sokolovsky4aee1192014-02-18 00:06:37 +020016codepoint2name[ord(':')] = 'colon'
17codepoint2name[ord('/')] = 'slash'
Damien Georgea71c83a2014-02-15 11:34:50 +000018
Damien George55baff42014-01-21 21:40:13 +000019# this must match the equivalent function in qstr.c
20def compute_hash(qstr):
Damien George6e628c42014-03-25 15:27:15 +000021 hash = 5381
Damien George55baff42014-01-21 21:40:13 +000022 for char in qstr:
Damien George6e628c42014-03-25 15:27:15 +000023 hash = (hash * 33) ^ ord(char)
Damien George55baff42014-01-21 21:40:13 +000024 return hash & 0xffff
25
26def do_work(infiles):
27 # read the qstrs in from the input files
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020028 qstrs = {}
Damien George55baff42014-01-21 21:40:13 +000029 for infile in infiles:
30 with open(infile, 'rt') as f:
31 line_number = 0
Paul Sokolovskyacb133d2014-04-10 03:38:42 +030032 conditional = None
Damien George55baff42014-01-21 21:40:13 +000033 for line in f:
34 line_number += 1
35 line = line.strip()
36
37 # ignore blank lines and comments
38 if len(line) == 0 or line.startswith('//'):
39 continue
40
Paul Sokolovskyacb133d2014-04-10 03:38:42 +030041 if line[0] == '#':
42 if conditional == "<endif>":
43 assert line == "#endif"
44 conditional = None
45 else:
46 assert conditional is None
47 conditional = line
48 continue
49
50 if conditional == "<endif>":
51 assert False, "#endif expected before '%s'" % line
52
Damien George55baff42014-01-21 21:40:13 +000053 # verify line is of the correct form
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020054 match = re.match(r'Q\((.+)\)$', line)
Damien George55baff42014-01-21 21:40:13 +000055 if not match:
Damien Georgefdf0da52014-03-08 15:03:25 +000056 print('({}:{}) bad qstr format, got {}'.format(infile, line_number, line), file=sys.stderr)
Damien George55baff42014-01-21 21:40:13 +000057 return False
58
59 # get the qstr value
60 qstr = match.group(1)
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020061 ident = re.sub(r'[^A-Za-z0-9_]', lambda s: "_" + codepoint2name[ord(s.group(0))] + "_", qstr)
Damien George55baff42014-01-21 21:40:13 +000062
63 # don't add duplicates
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020064 if ident in qstrs:
Damien George55baff42014-01-21 21:40:13 +000065 continue
66
Damien George1976bae2014-01-24 22:22:00 +000067 # add the qstr to the list, with order number to retain original order in file
Paul Sokolovskyacb133d2014-04-10 03:38:42 +030068 qstrs[ident] = (len(qstrs), ident, qstr, conditional)
69 if conditional is not None:
70 conditional = "<endif>"
Damien George55baff42014-01-21 21:40:13 +000071
72 # process the qstrs, printing out the generated C header file
73 print('// This file was automatically generated by makeqstrdata.py')
Dave Hylands7a996b12014-01-21 15:28:27 -080074 print('')
Paul Sokolovskyacb133d2014-04-10 03:38:42 +030075 for order, ident, qstr, conditional in sorted(qstrs.values(), key=lambda x: x[0]):
Damien George55baff42014-01-21 21:40:13 +000076 qhash = compute_hash(qstr)
77 qlen = len(qstr)
Paul Sokolovskyacb133d2014-04-10 03:38:42 +030078 if conditional:
79 print(conditional)
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020080 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))
Paul Sokolovskyacb133d2014-04-10 03:38:42 +030081 if conditional:
82 print('#endif')
Damien George55baff42014-01-21 21:40:13 +000083
84 return True
85
86def main():
87 arg_parser = argparse.ArgumentParser(description='Process raw qstr file and output qstr data with length, hash and data bytes')
88 arg_parser.add_argument('files', nargs='+', help='input file(s)')
89 args = arg_parser.parse_args()
90
91 result = do_work(args.files)
92 if not result:
Damien Georgefdf0da52014-03-08 15:03:25 +000093 print('exiting with error code', file=sys.stderr)
Damien George55baff42014-01-21 21:40:13 +000094 exit(1)
95
96if __name__ == "__main__":
97 main()