2 # Copyright (c) 2011 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Convert any unicode characters found in the input file to C literals."""
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')
20 options
, arglist
= parser
.parse_args(argv
)
22 if not options
.output_dir
:
23 print "output_dir required"
27 print "input_file required"
30 in_filename
= arglist
[1]
32 if not in_filename
.endswith('.utf8'):
33 print "input_file should end in .utf8"
36 out_filename
= os
.path
.join(options
.output_dir
, os
.path
.basename(
37 os
.path
.splitext(in_filename
)[0]))
39 WriteEscapedFile(in_filename
, out_filename
)
43 def WriteEscapedFile(in_filename
, out_filename
):
44 input_data
= codecs
.open(in_filename
, 'r', 'utf8').read()
45 with codecs
.open(out_filename
, 'w', 'ascii') as out_file
:
46 for i
, char
in enumerate(input_data
):
48 out_file
.write(repr(char
.encode('utf8'))[1:-1])
49 if input_data
[i
+ 1:i
+ 2] in '0123456789abcdefABCDEF':
52 out_file
.write(char
.encode('ascii'))
55 if __name__
== '__main__':
56 sys
.exit(main(sys
.argv
))