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