@@ -23,7 +23,7 @@ def get_atom_name(name):
23
23
name = os .path .basename (name )
24
24
return name .upper ()
25
25
26
- def write_atom_literal (out , name , contents , lang ):
26
+ def write_atom_literal (out , name , contents , lang , utf8 ):
27
27
# Escape the contents of the file so it can be stored as a literal.
28
28
contents = contents .replace ("\\ " , "\\ \\ " )
29
29
contents = contents .replace ("\f " , "\\ f" )
@@ -33,7 +33,10 @@ def write_atom_literal(out, name, contents, lang):
33
33
contents = contents .replace ('"' , '\\ "' )
34
34
35
35
if "cc" == lang or "hh" == lang :
36
+ if utf8 :
36
37
line_format = " L\" {}\" ,\n "
38
+ else :
39
+ line_format = " \" {}\" ,\n "
37
40
elif "java" == lang :
38
41
line_format = " .append\(\" {}\" )\n "
39
42
else :
@@ -42,7 +45,9 @@ def write_atom_literal(out, name, contents, lang):
42
45
name = get_atom_name (name )
43
46
44
47
if "cc" == lang or "hh" == lang :
45
- out .write ("const wchar_t* const %s[] = {\n " % name )
48
+ string_type = "std::wstring" if utf8 else "std::string"
49
+ char_type = "wchar_t" if utf8 else "char"
50
+ out .write ("const %s* const %s[] = {\n " % (char_type , name ))
46
51
elif "java" == lang :
47
52
out .write (" %s(new StringBuilder()\n " % name )
48
53
else :
@@ -67,32 +72,32 @@ def write_atom_literal(out, name, contents, lang):
67
72
elif "java" == lang :
68
73
out .write (" .toString()),\n " )
69
74
70
- def generate_header (file_name , out , js_map , just_declare ):
75
+ def generate_header (file_name , out , js_map , just_declare , utf8 ):
71
76
define_guard = "WEBDRIVER_%s" % os .path .basename (file_name .upper ()).replace ("." , "_" )
72
-
77
+ include_stddef = " \n #include <stddef.h> // For wchar_t." if utf8 else ""
73
78
out .write ("""%s
74
79
75
80
/* AUTO GENERATED - DO NOT EDIT BY HAND */
76
81
#ifndef %s
77
82
#define %s
78
-
79
- #include <stddef.h> // For wchar_t.
83
+ %s
80
84
#include <string> // For std::(w)string.
81
85
82
86
namespace webdriver {
83
87
namespace atoms {
84
88
85
- """ % (_copyright , define_guard , define_guard ))
89
+ """ % (_copyright , define_guard , define_guard , include_stddef ))
86
90
91
+ string_type = "std::wstring" if utf8 else "std::string"
92
+ char_type = "wchar_t" if utf8 else "char"
93
+
87
94
for (name , file ) in js_map .items ():
88
95
if just_declare :
89
- out .write ("extern const wchat_t * const %s[];\n " % name .upper ())
96
+ out .write ("extern const %s * const %s[];\n " % ( char_type , name .upper () ))
90
97
else :
91
98
contents = open (file , "r" ).read ()
92
- write_atom_literal (out , name , contents , "hh" )
99
+ write_atom_literal (out , name , contents , "hh" , utf8 )
93
100
94
- string_type = "std::wstring"
95
- char_type = "wchar_t"
96
101
out .write ("""
97
102
static inline %s asString(const %s* const atom[]) {
98
103
%s source;
@@ -108,7 +113,7 @@ def generate_header(file_name, out, js_map, just_declare):
108
113
#endif // %s
109
114
""" % (string_type , char_type , string_type , define_guard ))
110
115
111
- def generate_cc_source (out , js_map ):
116
+ def generate_cc_source (out , js_map , utf8 ):
112
117
out .write ("""%s
113
118
114
119
/* AUTO GENERATED - DO NOT EDIT BY HAND */
@@ -123,7 +128,7 @@ def generate_cc_source(out, js_map):
123
128
124
129
for (name , file ) in js_map .items ():
125
130
contents = open (file , "r" ).read ()
126
- write_atom_literal (out , name , contents , "cc" )
131
+ write_atom_literal (out , name , contents , "cc" , utf8 )
127
132
128
133
out .write ("""
129
134
} // namespace atoms
@@ -148,7 +153,7 @@ def generate_java_source(file_name, out, preamble, js_map):
148
153
149
154
for (name , file ) in js_map .items ():
150
155
contents = open (file , "r" ).read ()
151
- write_atom_literal (out , name , contents , "java" )
156
+ write_atom_literal (out , name , contents , "java" , True )
152
157
153
158
out .write ("""
154
159
;
@@ -173,18 +178,19 @@ def main(argv=[]):
173
178
lang = argv [1 ]
174
179
file_name = argv [2 ]
175
180
preamble = argv [3 ]
181
+ utf8 = (argv [4 ] == "true" )
176
182
177
183
js_map = {}
178
- for i in range (4 , len (argv ), 2 ):
184
+ for i in range (5 , len (argv ), 2 ):
179
185
js_map [argv [i ]] = argv [i + 1 ]
180
186
181
187
with open (file_name , "w" ) as out :
182
188
if "cc" == lang :
183
- generate_cc_source (out , js_map )
189
+ generate_cc_source (out , js_map , utf8 )
184
190
elif "hdecl" == lang :
185
- generate_header (file_name , out , js_map , True )
191
+ generate_header (file_name , out , js_map , True , utf8 )
186
192
elif "hh" == lang :
187
- generate_header (file_name , out , js_map , False )
193
+ generate_header (file_name , out , js_map , False , utf8 )
188
194
elif "java" == lang :
189
195
generate_java_source (file_name , out , preamble , js_map )
190
196
else :
0 commit comments