Introduce another file-to-array script
authortkent@chromium.org <tkent@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 3 Apr 2012 07:14:51 +0000 (07:14 +0000)
committertkent@chromium.org <tkent@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 3 Apr 2012 07:14:51 +0000 (07:14 +0000)
https://bugs.webkit.org/show_bug.cgi?id=83001

Reviewed by Kentaro Hara.

make-file-arrays.py is a script to convert a binary file to a C++
char array. The differences from css/make-css-file-arrays.pl and
inspector/xxd.pl are:

- No whitespace stripping for CSS files
- We can make conditional arrays.
e.g. An array enclosed with #if ENABLE(...) - #endif.

This script will be used to implement a calendar picker for <input
type=date>.

* make-file-arrays.py: Added.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@112988 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/make-file-arrays.py [new file with mode: 0755]

index 209cf8e..7b89d05 100644 (file)
@@ -1,3 +1,23 @@
+2012-04-02  Kent Tamura  <tkent@chromium.org>
+
+        Introduce another file-to-array script
+        https://bugs.webkit.org/show_bug.cgi?id=83001
+
+        Reviewed by Kentaro Hara.
+
+        make-file-arrays.py is a script to convert a binary file to a C++
+        char array. The differences from css/make-css-file-arrays.pl and
+        inspector/xxd.pl are:
+
+        - No whitespace stripping for CSS files
+        - We can make conditional arrays.
+        e.g. An array enclosed with #if ENABLE(...) - #endif.
+
+        This script will be used to implement a calendar picker for <input
+        type=date>.
+
+        * make-file-arrays.py: Added.
+
 2012-04-02  Adam Barth  <abarth@webkit.org>
 
         Implement <iframe srcdoc>
diff --git a/Source/WebCore/make-file-arrays.py b/Source/WebCore/make-file-arrays.py
new file mode 100755 (executable)
index 0000000..49dd5dd
--- /dev/null
@@ -0,0 +1,99 @@
+#!/usr/bin/env python
+# Copyright (C) 2012 Google Inc. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+#     * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#     * Redistributions in binary form must reproduce the above
+# copyright notice, this list of conditions and the following disclaimer
+# in the documentation and/or other materials provided with the
+# distribution.
+#     * Neither the name of Google Inc. nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+# Usage: make-file-arrays.py [--condition=condition-string] --out-h=<header-file-name> --out-cpp=<cpp-file-name> <input-file>...
+
+import os.path
+import re
+import sys
+from optparse import OptionParser
+
+
+def make_variable_name_and_read(file_name):
+    result = re.match(r"([\w\d_]+)\.([\w\d_]+)", os.path.basename(file_name))
+    if not result:
+        print "Invalid input file name:", os.path.basename(file_name)
+        sys.exit(1)
+    variable_name = result.group(1)[0].lower() + result.group(1)[1:] + result.group(2).capitalize()
+    file = open(file_name, "rb")
+    content = file.read()
+    file.close()
+    return (variable_name, content)
+
+
+def main():
+    parser = OptionParser()
+    parser.add_option("--out-h", dest="out_header")
+    parser.add_option("--out-cpp", dest="out_cpp")
+    parser.add_option("--condition", dest="flag")
+    (options, args) = parser.parse_args()
+    if len(args) < 1:
+        parser.error("Need one or more input files")
+    if not options.out_header:
+        parser.error("Need to specify --out-h=filename")
+    if not options.out_cpp:
+        parser.error("Need to specify --out-cpp=filename")
+
+    header_file = open(options.out_header, "w")
+    if options.flag:
+        header_file.write("#if " + options.flag + "\n")
+    header_file.write("namespace WebCore {\n")
+
+    cpp_file = open(options.out_cpp, "w")
+    cpp_file.write("#include \"config.h\"\n")
+    cpp_file.write("#include \"" + os.path.basename(options.out_header) + "\"\n")
+    if options.flag:
+        cpp_file.write("#if " + options.flag + "\n")
+    cpp_file.write("namespace WebCore {\n")
+
+    for file_name in args:
+        (variable_name, content) = make_variable_name_and_read(file_name)
+        size = len(content)
+        header_file.write("extern const char %s[%d];\n" % (variable_name, size))
+        cpp_file.write("const char %s[%d] = {\n" % (variable_name, size))
+        for index in range(size):
+            cpp_file.write("%d" % ord(content[index]))
+            cpp_file.write("," if index != len(content) - 1 else "};\n")
+            if index % 20 == 19:
+                cpp_file.write("\n")
+        cpp_file.write("\n")
+
+    header_file.write("}\n")
+    if options.flag:
+        header_file.write("#endif\n")
+    header_file.close()
+
+    cpp_file.write("}\n")
+    if options.flag:
+        cpp_file.write("#endif\n")
+    cpp_file.close()
+
+
+if __name__ == "__main__":
+    main()