Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | #!/usr/bin/env python |
michael@0 | 2 | # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
michael@0 | 3 | # Use of this source code is governed by a BSD-style license that can be |
michael@0 | 4 | # found in the LICENSE file. |
michael@0 | 5 | |
michael@0 | 6 | """Convert any unicode characters found in the input file to C literals.""" |
michael@0 | 7 | |
michael@0 | 8 | import codecs |
michael@0 | 9 | import optparse |
michael@0 | 10 | import os |
michael@0 | 11 | import sys |
michael@0 | 12 | |
michael@0 | 13 | |
michael@0 | 14 | def main(argv): |
michael@0 | 15 | parser = optparse.OptionParser() |
michael@0 | 16 | usage = 'Usage: %prog -o <output_dir> <input_file>' |
michael@0 | 17 | parser.set_usage(usage) |
michael@0 | 18 | parser.add_option('-o', dest='output_dir') |
michael@0 | 19 | |
michael@0 | 20 | options, arglist = parser.parse_args(argv) |
michael@0 | 21 | |
michael@0 | 22 | if not options.output_dir: |
michael@0 | 23 | print "output_dir required" |
michael@0 | 24 | return 1 |
michael@0 | 25 | |
michael@0 | 26 | if len(arglist) != 2: |
michael@0 | 27 | print "input_file required" |
michael@0 | 28 | return 1 |
michael@0 | 29 | |
michael@0 | 30 | in_filename = arglist[1] |
michael@0 | 31 | |
michael@0 | 32 | if not in_filename.endswith('.utf8'): |
michael@0 | 33 | print "input_file should end in .utf8" |
michael@0 | 34 | return 1 |
michael@0 | 35 | |
michael@0 | 36 | out_filename = os.path.join(options.output_dir, os.path.basename( |
michael@0 | 37 | os.path.splitext(in_filename)[0])) |
michael@0 | 38 | |
michael@0 | 39 | WriteEscapedFile(in_filename, out_filename) |
michael@0 | 40 | return 0 |
michael@0 | 41 | |
michael@0 | 42 | |
michael@0 | 43 | def WriteEscapedFile(in_filename, out_filename): |
michael@0 | 44 | input_data = codecs.open(in_filename, 'r', 'utf8').read() |
michael@0 | 45 | with codecs.open(out_filename, 'w', 'ascii') as out_file: |
michael@0 | 46 | for i, char in enumerate(input_data): |
michael@0 | 47 | if ord(char) > 127: |
michael@0 | 48 | out_file.write(repr(char.encode('utf8'))[1:-1]) |
michael@0 | 49 | if input_data[i + 1:i + 2] in '0123456789abcdefABCDEF': |
michael@0 | 50 | out_file.write('""') |
michael@0 | 51 | else: |
michael@0 | 52 | out_file.write(char.encode('ascii')) |
michael@0 | 53 | |
michael@0 | 54 | |
michael@0 | 55 | if __name__ == '__main__': |
michael@0 | 56 | sys.exit(main(sys.argv)) |