| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/env python |
| 2 # |
| 3 # Copyright 2015 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 |
| 8 import optparse |
| 9 import textwrap |
| 10 |
| 11 TEMPLATE = """\ |
| 12 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 13 // Use of this source code is governed by a BSD-style license that can be |
| 14 // found in the LICENSE file. |
| 15 |
| 16 // This file was generated from .js source files by GYP. If you |
| 17 // want to make changes to this file you should either change the |
| 18 // javascript source files or the GYP script. |
| 19 |
| 20 namespace %(namespace)s { |
| 21 |
| 22 const char %(var_name)s[] = { |
| 23 %(c_array)s |
| 24 }; |
| 25 |
| 26 } // namespace %(namespace)s |
| 27 """ |
| 28 |
| 29 |
| 30 def ToCArray(byte_sequence): |
| 31 result = [] |
| 32 for chr in byte_sequence: |
| 33 result.append(str(ord(chr))) |
| 34 joined = ", ".join(result) |
| 35 return textwrap.fill(joined, 80) |
| 36 |
| 37 |
| 38 def JS2C(source, target, namespace, var_name): |
| 39 input = open(source, "r") |
| 40 source_bytes = input.read() |
| 41 |
| 42 template_params = { |
| 43 "namespace": namespace, |
| 44 "var_name": var_name, |
| 45 "c_array": ToCArray(source_bytes) |
| 46 } |
| 47 |
| 48 output = open(target, "wb") # don't output CRLF on Windows |
| 49 output.write(TEMPLATE % template_params) |
| 50 output.close() |
| 51 |
| 52 |
| 53 parser = optparse.OptionParser() |
| 54 parser.set_usage("""js2c source.js out.cpp namespace var_name |
| 55 sources.js: input JS file. |
| 56 out.cpp: C++ code to be generated. |
| 57 namespace: namespace the array will be declared in. |
| 58 varName: the name of the variable for the array. |
| 59 """) |
| 60 (options, args) = parser.parse_args() |
| 61 |
| 62 JS2C(args[0], args[1], args[2], args[3]) |
| OLD | NEW |