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