[WK2] Fix crash in create and update tiles.
[framework/web/webkit-efl.git] / Source / WebCore / make-file-arrays.py
1 #!/usr/bin/env python
2 # Copyright (C) 2012 Google Inc. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 #     * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 #     * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 #     * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 # Usage: make-file-arrays.py [--condition=condition-string] --out-h=<header-file-name> --out-cpp=<cpp-file-name> <input-file>...
31
32 import os.path
33 import re
34 import sys
35 from optparse import OptionParser
36
37
38 def make_variable_name_and_read(file_name):
39     result = re.match(r"([\w\d_]+)\.([\w\d_]+)", os.path.basename(file_name))
40     if not result:
41         print "Invalid input file name:", os.path.basename(file_name)
42         sys.exit(1)
43     variable_name = result.group(1)[0].lower() + result.group(1)[1:] + result.group(2).capitalize()
44     file = open(file_name, "rb")
45     content = file.read()
46     file.close()
47     return (variable_name, content)
48
49
50 def strip_whitespace_and_comments(file_name, content):
51     result = re.match(r".*\.([^.]+)", file_name)
52     if not result:
53         print "The file name has no extension:", file_name
54         sys.exit(1)
55     extension = result.group(1).lower()
56     multi_line_comment = re.compile(r"/\*.*?\*/", re.MULTILINE | re.DOTALL)
57     single_line_comment = re.compile(r"//.*$", re.MULTILINE)
58     repeating_space = re.compile(r"[ \t]+", re.MULTILINE)
59     leading_space = re.compile(r"^[ \t]+", re.MULTILINE)
60     trailing_space = re.compile(r"[ \t]+$", re.MULTILINE)
61     empty_line = re.compile(r"\n+")
62     if extension == "js":
63         content = multi_line_comment.sub("", content)
64         content = single_line_comment.sub("", content)
65         content = repeating_space.sub(" ", content)
66         content = leading_space.sub("", content)
67         content = trailing_space.sub("", content)
68         content = empty_line.sub("\n", content)
69     elif extension == "css":
70         content = multi_line_comment.sub("", content)
71         content = repeating_space.sub(" ", content)
72         content = leading_space.sub("", content)
73         content = trailing_space.sub("", content)
74         content = empty_line.sub("\n", content)
75     return content
76
77
78 def main():
79     parser = OptionParser()
80     parser.add_option("--out-h", dest="out_header")
81     parser.add_option("--out-cpp", dest="out_cpp")
82     parser.add_option("--condition", dest="flag")
83     (options, args) = parser.parse_args()
84     if len(args) < 1:
85         parser.error("Need one or more input files")
86     if not options.out_header:
87         parser.error("Need to specify --out-h=filename")
88     if not options.out_cpp:
89         parser.error("Need to specify --out-cpp=filename")
90
91     header_file = open(options.out_header, "w")
92     if options.flag:
93         header_file.write("#if " + options.flag + "\n")
94     header_file.write("namespace WebCore {\n")
95
96     cpp_file = open(options.out_cpp, "w")
97     cpp_file.write("#include \"config.h\"\n")
98     cpp_file.write("#include \"" + os.path.basename(options.out_header) + "\"\n")
99     if options.flag:
100         cpp_file.write("#if " + options.flag + "\n")
101     cpp_file.write("namespace WebCore {\n")
102
103     for file_name in args:
104         (variable_name, content) = make_variable_name_and_read(file_name)
105         content = strip_whitespace_and_comments(file_name, content)
106         size = len(content)
107         header_file.write("extern const char %s[%d];\n" % (variable_name, size))
108         cpp_file.write("const char %s[%d] = {\n" % (variable_name, size))
109         for index in range(size):
110             char_code = ord(content[index])
111             if char_code < 128:
112                 cpp_file.write("%d" % char_code)
113             else:
114                 cpp_file.write("'\\x%02x'" % char_code)
115             cpp_file.write("," if index != len(content) - 1 else "};\n")
116             if index % 20 == 19:
117                 cpp_file.write("\n")
118         cpp_file.write("\n")
119
120     header_file.write("}\n")
121     if options.flag:
122         header_file.write("#endif\n")
123     header_file.close()
124
125     cpp_file.write("}\n")
126     if options.flag:
127         cpp_file.write("#endif\n")
128     cpp_file.close()
129
130
131 if __name__ == "__main__":
132     main()