media/webrtc/trunk/build/escape_unicode.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/webrtc/trunk/build/escape_unicode.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,56 @@
     1.4 +#!/usr/bin/env python
     1.5 +# Copyright (c) 2011 The Chromium Authors. All rights reserved.
     1.6 +# Use of this source code is governed by a BSD-style license that can be
     1.7 +# found in the LICENSE file.
     1.8 +
     1.9 +"""Convert any unicode characters found in the input file to C literals."""
    1.10 +
    1.11 +import codecs
    1.12 +import optparse
    1.13 +import os
    1.14 +import sys
    1.15 +
    1.16 +
    1.17 +def main(argv):
    1.18 +  parser = optparse.OptionParser()
    1.19 +  usage = 'Usage: %prog -o <output_dir> <input_file>'
    1.20 +  parser.set_usage(usage)
    1.21 +  parser.add_option('-o', dest='output_dir')
    1.22 +
    1.23 +  options, arglist = parser.parse_args(argv)
    1.24 +
    1.25 +  if not options.output_dir:
    1.26 +    print "output_dir required"
    1.27 +    return 1
    1.28 +
    1.29 +  if len(arglist) != 2:
    1.30 +    print "input_file required"
    1.31 +    return 1
    1.32 +
    1.33 +  in_filename = arglist[1]
    1.34 +
    1.35 +  if not in_filename.endswith('.utf8'):
    1.36 +    print "input_file should end in .utf8"
    1.37 +    return 1
    1.38 +
    1.39 +  out_filename = os.path.join(options.output_dir, os.path.basename(
    1.40 +      os.path.splitext(in_filename)[0]))
    1.41 +
    1.42 +  WriteEscapedFile(in_filename, out_filename)
    1.43 +  return 0
    1.44 +
    1.45 +
    1.46 +def WriteEscapedFile(in_filename, out_filename):
    1.47 +  input_data = codecs.open(in_filename, 'r', 'utf8').read()
    1.48 +  with codecs.open(out_filename, 'w', 'ascii') as out_file:
    1.49 +    for i, char in enumerate(input_data):
    1.50 +      if ord(char) > 127:
    1.51 +        out_file.write(repr(char.encode('utf8'))[1:-1])
    1.52 +        if input_data[i + 1:i + 2] in '0123456789abcdefABCDEF':
    1.53 +          out_file.write('""')
    1.54 +      else:
    1.55 +        out_file.write(char.encode('ascii'))
    1.56 +
    1.57 +
    1.58 +if __name__ == '__main__':
    1.59 +  sys.exit(main(sys.argv))

mercurial