Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 1 | import argparse |
| 2 | import re |
Damien George | 1976bae | 2014-01-24 22:22:00 +0000 | [diff] [blame] | 3 | |
| 4 | # codepoint2name is different in Python 2 to Python 3 |
| 5 | import platform |
| 6 | if platform.python_version_tuple()[0] == '2': |
| 7 | from htmlentitydefs import codepoint2name |
| 8 | elif platform.python_version_tuple()[0] == '3': |
| 9 | from html.entities import codepoint2name |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 10 | |
| 11 | # this must match the equivalent function in qstr.c |
| 12 | def compute_hash(qstr): |
| 13 | hash = 0 |
| 14 | for char in qstr: |
| 15 | hash += ord(char) |
| 16 | return hash & 0xffff |
| 17 | |
| 18 | def do_work(infiles): |
| 19 | # read the qstrs in from the input files |
Paul Sokolovsky | ab5d082 | 2014-01-24 00:22:00 +0200 | [diff] [blame] | 20 | qstrs = {} |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 21 | for infile in infiles: |
| 22 | with open(infile, 'rt') as f: |
| 23 | line_number = 0 |
| 24 | for line in f: |
| 25 | line_number += 1 |
| 26 | line = line.strip() |
| 27 | |
| 28 | # ignore blank lines and comments |
| 29 | if len(line) == 0 or line.startswith('//'): |
| 30 | continue |
| 31 | |
| 32 | # verify line is of the correct form |
Paul Sokolovsky | ab5d082 | 2014-01-24 00:22:00 +0200 | [diff] [blame] | 33 | match = re.match(r'Q\((.+)\)$', line) |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 34 | if not match: |
| 35 | print('({}:{}) bad qstr format, got {}'.format(infile, line_number, line)) |
| 36 | return False |
| 37 | |
| 38 | # get the qstr value |
| 39 | qstr = match.group(1) |
Paul Sokolovsky | ab5d082 | 2014-01-24 00:22:00 +0200 | [diff] [blame] | 40 | ident = re.sub(r'[^A-Za-z0-9_]', lambda s: "_" + codepoint2name[ord(s.group(0))] + "_", qstr) |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 41 | |
| 42 | # don't add duplicates |
Paul Sokolovsky | ab5d082 | 2014-01-24 00:22:00 +0200 | [diff] [blame] | 43 | if ident in qstrs: |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 44 | continue |
| 45 | |
Damien George | 1976bae | 2014-01-24 22:22:00 +0000 | [diff] [blame] | 46 | # add the qstr to the list, with order number to retain original order in file |
| 47 | qstrs[ident] = (len(qstrs), ident, qstr) |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 48 | |
| 49 | # process the qstrs, printing out the generated C header file |
| 50 | print('// This file was automatically generated by makeqstrdata.py') |
Dave Hylands | 7a996b1 | 2014-01-21 15:28:27 -0800 | [diff] [blame] | 51 | print('') |
Damien George | 1976bae | 2014-01-24 22:22:00 +0000 | [diff] [blame] | 52 | for order, ident, qstr in sorted(qstrs.values(), key=lambda x: x[0]): |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 53 | qhash = compute_hash(qstr) |
| 54 | qlen = len(qstr) |
Paul Sokolovsky | ab5d082 | 2014-01-24 00:22:00 +0200 | [diff] [blame] | 55 | 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 George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 56 | |
| 57 | return True |
| 58 | |
| 59 | def main(): |
| 60 | arg_parser = argparse.ArgumentParser(description='Process raw qstr file and output qstr data with length, hash and data bytes') |
| 61 | arg_parser.add_argument('files', nargs='+', help='input file(s)') |
| 62 | args = arg_parser.parse_args() |
| 63 | |
| 64 | result = do_work(args.files) |
| 65 | if not result: |
| 66 | print('exiting with error code') |
| 67 | exit(1) |
| 68 | |
| 69 | if __name__ == "__main__": |
| 70 | main() |