|
1 # Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 # All rights reserved. |
|
3 # This component and the accompanying materials are made available |
|
4 # under the terms of "Eclipse Public License v1.0" |
|
5 # which accompanies this distribution, and is available |
|
6 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 # |
|
8 # Initial Contributors: |
|
9 # Nokia Corporation - initial contribution. |
|
10 # |
|
11 # Contributors: |
|
12 # |
|
13 # Description: |
|
14 # stringtable.pl - Makes a string table .cpp and .h file from a .st file |
|
15 # This tool takes a valid .st file as an input parameter and converts it into a .h and a .cpp file |
|
16 # |
|
17 # |
|
18 |
|
19 use strict; |
|
20 |
|
21 # Removes the extension from the filename |
|
22 $ARGV[0] =~ m/(.*)\..*/; |
|
23 my $root = $1; |
|
24 $root =~ m/(.*[\\\/])([^\\\/]*)$/; |
|
25 |
|
26 my $path_to_file = ("$1"); |
|
27 my $filename = lc("$2"); |
|
28 my $header_to_include = lc("$filename.h"); |
|
29 |
|
30 # Open the input filename |
|
31 open (IN, "<$ARGV[0]" ) or die ("Error: $ARGV[0] $!"); |
|
32 |
|
33 # Parse header comments |
|
34 my $input; |
|
35 my @comments; |
|
36 while ($input = <IN>) { |
|
37 # Comments starting with '#' not to be included in header file |
|
38 next if ($input =~ /^\s*\#/); |
|
39 |
|
40 chomp($input); |
|
41 |
|
42 # Save comments containing '!' or stop looking if something else is found |
|
43 if ($input =~ /\!(.*)/) { |
|
44 push(@comments, "$1\n"); |
|
45 } else { |
|
46 last; |
|
47 } |
|
48 } |
|
49 |
|
50 my $folding; |
|
51 # Check that line is of the right format and get the table name. |
|
52 $input =~ m/(f*stringtable) ([A-Za-z0-9_]+)/ or die ("Error: $ARGV[0] is not a stringtable file"); |
|
53 my $name = $2; |
|
54 if ($1 eq "stringtable") { |
|
55 $folding = "ETrue"; |
|
56 } else { |
|
57 $folding = "EFalse"; |
|
58 } |
|
59 |
|
60 # Open the output filenames |
|
61 open (CPP, ">"."$path_to_file"."$filename".".cpp" ) or die ("Error: $ARGV[0] Can't open cpp file"); |
|
62 open (HEADER, ">"."$path_to_file"."$filename".".h" ) or die ("Error: $ARGV[0] Can't open header file"); |
|
63 |
|
64 my $sourcename = $ARGV[0]; |
|
65 $sourcename =~ s/^(.*epoc32)/epoc32/i; |
|
66 |
|
67 # Output the preambles |
|
68 print CPP <<EOD; |
|
69 // Autogenerated from $sourcename by the stringtable tool - Do not edit |
|
70 #include <e32std.h> |
|
71 #include <stringpool.h> |
|
72 #include <stringtablesupport.h> |
|
73 #include "$header_to_include" |
|
74 #ifdef _DEBUG |
|
75 #undef _DEBUG |
|
76 #endif |
|
77 |
|
78 EOD |
|
79 |
|
80 print HEADER <<EOD; |
|
81 // Autogenerated from $sourcename by the stringtable tool - Do not edit |
|
82 |
|
83 #ifndef STRINGTABLE_$name |
|
84 #define STRINGTABLE_$name |
|
85 |
|
86 #include <stringpool.h> |
|
87 |
|
88 struct TStringTable; |
|
89 |
|
90 EOD |
|
91 |
|
92 # If there are no header comments, use the default comment, otherwise, add the header comments |
|
93 if (@comments == 0) { |
|
94 print HEADER "/** A String table */\n"; |
|
95 } else { |
|
96 print HEADER @comments; |
|
97 } |
|
98 |
|
99 print HEADER <<EOD; |
|
100 class $name |
|
101 { |
|
102 public: |
|
103 enum TStrings |
|
104 { |
|
105 EOD |
|
106 |
|
107 my $count = 0; |
|
108 my $outputcomma = 0; |
|
109 my @commentlines; |
|
110 |
|
111 #Now process file |
|
112 until (eof IN) { |
|
113 # Treat lines with a '#' as the first character as comments and skip them, ignoring 0 or more whitespaces before it. |
|
114 # If # is not the first character, then treat them as part of the string. |
|
115 do |
|
116 { |
|
117 $input = readline (*IN); |
|
118 chomp($input); |
|
119 } while ($input =~ m/^\s*\#/); |
|
120 |
|
121 if ($input =~ m/\!(.*)/) { |
|
122 # Save lines that start with '!' to print later |
|
123 push(@commentlines, "\t\t$1\n"); |
|
124 } else { |
|
125 # Parse the line |
|
126 $input =~ m/([A-Za-z0-9]+)\s*(.*)/ or die ("Error: $ARGV[0] badly formed at \"$input\""); |
|
127 |
|
128 # If a comma needs to be printed, print it |
|
129 if ($outputcomma == 1) { |
|
130 print HEADER ",\n"; |
|
131 $outputcomma = 0; |
|
132 } |
|
133 # Print pending comments and clear the comments array |
|
134 if (@commentlines > 0) { |
|
135 print HEADER @commentlines; |
|
136 @commentlines = ''; |
|
137 } |
|
138 |
|
139 # Form a version of the string that can go in a comment. This |
|
140 # can't contain /* or */ |
|
141 my $enum = $1; |
|
142 my $string = $2; |
|
143 $string =~ s/\s+$//; # remove any trailing whitespace |
|
144 my $comment = $string; |
|
145 $comment =~ s|/\*|/ \*|g; |
|
146 $comment =~ s|\*/|\* /|g; |
|
147 |
|
148 # Increment the count |
|
149 $count++; |
|
150 print HEADER "\t\t/** $comment */\n"; |
|
151 print HEADER "\t\t$enum"; |
|
152 print CPP "_STLIT8(K$count, \"$string\");\n"; |
|
153 $outputcomma = 1; |
|
154 } |
|
155 } |
|
156 |
|
157 # Print closing comments |
|
158 if (@commentlines > 0) { |
|
159 print HEADER "\n"; |
|
160 print HEADER @commentlines; |
|
161 @commentlines = ''; |
|
162 } else { |
|
163 print HEADER "\n"; |
|
164 } |
|
165 |
|
166 #Output intermediate boilerplate in the cpp |
|
167 print CPP <<EOD; |
|
168 |
|
169 // Intermediate |
|
170 const void * const KStringPointers[] = |
|
171 { |
|
172 EOD |
|
173 |
|
174 # And the end of the headers |
|
175 print HEADER <<EOD; |
|
176 }; |
|
177 static const TStringTable Table; |
|
178 }; |
|
179 |
|
180 #endif // STRINGTABLE_$name |
|
181 |
|
182 EOD |
|
183 |
|
184 #Output the table of pointers to the cpp file |
|
185 my $total = $count; |
|
186 |
|
187 for ($count = 1; $count <= $total ; $count++) { |
|
188 if ($count > 1) { |
|
189 print CPP ",\n"; |
|
190 } |
|
191 print CPP "\t(const void*)&K$count"; |
|
192 }; |
|
193 |
|
194 #The last of the boilerplate |
|
195 print CPP <<EOD; |
|
196 |
|
197 }; |
|
198 |
|
199 const TStringTable ${name}::Table = {$total, KStringPointers, $folding}; |
|
200 EOD |