|
1 # |
|
2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Nokia Corporation - initial contribution. |
|
11 # |
|
12 # Contributors: |
|
13 # |
|
14 # Description: |
|
15 # |
|
16 # ============================================================================ |
|
17 # Name : loc_generator.pl |
|
18 # Part of : create loc files to \epoc32\s60\locfiles base on subfolders in the same folder |
|
19 # Origin : S60 build team,NMP |
|
20 # Created : Tue Sep 24 11:32:47 2006 |
|
21 # Version : 0.2 |
|
22 # Description: |
|
23 # Version 0.2 changes(VL): added "dirty hack" so that new string is added for all layers, |
|
24 # this need to be removed when S60locfiles is exporting localized .loc files to correct layers !!! |
|
25 # Also added fix to sub routine update_locs [if ($key eq "") {next}; # skip empty ones] |
|
26 |
|
27 |
|
28 use strict; |
|
29 use File::Basename; # for fileparse |
|
30 use Getopt::Long; |
|
31 use Data::Dumper; # debugging purposes |
|
32 |
|
33 # |
|
34 # Variables... |
|
35 # |
|
36 my $version = "0.2"; |
|
37 my $locfile_path; |
|
38 my $loc_sc_folder = "sc"; |
|
39 my $loc_01_folder = "01"; |
|
40 my @generated_files; |
|
41 |
|
42 my $input; |
|
43 my $dozip; |
|
44 my $noupdatelocfiles; |
|
45 my $force_generation; |
|
46 my $show_help; |
|
47 |
|
48 my $result = GetOptions('i:s' => \$input, 'zip:s' => \$dozip, 'noUpdate' => \$noupdatelocfiles, 'force' => \$force_generation , 'h' => \$show_help); |
|
49 |
|
50 if ($show_help) {help(); die "";}; |
|
51 |
|
52 if ($input && (!-d $input)){&error_msg("Invalid parameter '$input'!");} |
|
53 if ($input && $input !~ m/\\$/) {$input .= "\\";}; |
|
54 |
|
55 if (!$result){&error_msg("Required Arguments are missing or wrong");} |
|
56 # |
|
57 # Hello text |
|
58 # |
|
59 hello(); |
|
60 |
|
61 # paths to exported loc files |
|
62 my @exportedlocfilepaths = ("\\epoc32\\include\\platform\\loc", "\\epoc32\\include\\platform\\mw\\loc", "\\epoc32\\include\\platform\\app\\loc"); |
|
63 |
|
64 if ($input) { |
|
65 generate_locs($input); |
|
66 if ($dozip) {zipup_genfiles($input)}; |
|
67 } |
|
68 foreach $locfile_path (@exportedlocfilepaths) { |
|
69 generate_locs($locfile_path); |
|
70 } |
|
71 |
|
72 sub zipup_genfiles { |
|
73 my $folder = shift; |
|
74 unlink($dozip); |
|
75 print "zip.exe -j -m $dozip @{generated_files}\n"; |
|
76 system ("zip.exe -j -m $dozip @generated_files"); |
|
77 } |
|
78 |
|
79 # generate switch loc files |
|
80 sub generate_locs { |
|
81 my $folder = shift; |
|
82 my @files; |
|
83 my @languageids; |
|
84 @generated_files=(); |
|
85 |
|
86 # get language ids from folder names |
|
87 # use sc as reference |
|
88 if (-d ${folder}.${loc_sc_folder}) { |
|
89 opendir(SDIR, ${folder}.${loc_sc_folder}) or &error_msg("can not read ${folder}${loc_sc_folder}\n"); |
|
90 @files = grep !/^\.\.?$/ && /.loc$/i, readdir SDIR; |
|
91 closedir(SDIR); |
|
92 |
|
93 opendir(SDIR, $folder) or &error_msg("can not read $folder\n"); |
|
94 @languageids = grep !/^\.\.?$/ && /^\d+?$/i, readdir SDIR; |
|
95 closedir(SDIR); |
|
96 } |
|
97 # get language ids from zip file names. |
|
98 # use 01 as reference |
|
99 elsif (-f "${folder}${loc_01_folder}.zip") { |
|
100 open ZIP, "unzip -Z1 ${folder}${loc_01_folder}.zip |"; |
|
101 while (<ZIP>) |
|
102 { |
|
103 chomp; |
|
104 $_ =~ s/.*\///i; |
|
105 $_ =~ s/_${loc_01_folder}//i; |
|
106 |
|
107 push @files ,$_; |
|
108 } |
|
109 close ZIP; |
|
110 |
|
111 opendir(SDIR, $folder) or &error_msg("can not read $folder\n"); |
|
112 my @zipfiles = grep !/^\.\.?$/ && /^\d+?.zip$/i, readdir SDIR; |
|
113 closedir(SDIR); |
|
114 foreach (@zipfiles) { |
|
115 $_ =~ s/.zip//i; |
|
116 push @languageids, $_; |
|
117 } |
|
118 } |
|
119 |
|
120 # print Dumper( @languageids ); |
|
121 |
|
122 foreach my $loc_file (@files) { |
|
123 $loc_file =~ s/_${loc_01_folder}//i; |
|
124 if (!-e $folder.$loc_file || $force_generation) { |
|
125 push @generated_files, $folder.$loc_file."\n"; |
|
126 create_include_loc($folder, $loc_file, \@languageids); |
|
127 } |
|
128 if (!$noupdatelocfiles) { |
|
129 update_locs($folder, $loc_file, \@languageids); |
|
130 } |
|
131 } |
|
132 } |
|
133 |
|
134 # update exist localized loc files base on exported original loc. |
|
135 # create new files for all languages in case if loc is new one |
|
136 # update new defines from component exported loc to exist localized files |
|
137 sub update_locs { |
|
138 my $loc_path = shift; |
|
139 my $loc_file = shift; |
|
140 my $languageids = shift; |
|
141 my ($n, $ext) = split ('\.', $loc_file); |
|
142 |
|
143 if (!-f "${loc_path}${loc_sc_folder}\\${loc_file}"){return;}; |
|
144 |
|
145 my %defines_orig = read_loc("${loc_path}${loc_sc_folder}\\${loc_file}"); |
|
146 |
|
147 foreach my $languageID (@$languageids) { |
|
148 my @add; |
|
149 |
|
150 if (!-e "${loc_path}${languageID}\\${n}_${languageID}\.${ext}") { |
|
151 foreach my $key (keys %defines_orig) { |
|
152 push @add, $defines_orig{$key}."\n"; |
|
153 } |
|
154 } |
|
155 else { |
|
156 my %defines = read_loc("${loc_path}${languageID}\\${n}_${languageID}\.${ext}"); |
|
157 foreach my $key (keys %defines_orig) { |
|
158 if ($key eq "") {next}; # skip empty ones |
|
159 if (!exists $defines{$key}) { |
|
160 push @add, $defines_orig{$key}."\n"; |
|
161 } |
|
162 } |
|
163 |
|
164 } |
|
165 next if (!scalar @add); |
|
166 # Dirty quick hack to go trough all paths in this phase also |
|
167 foreach my $locfile_path_temp (@exportedlocfilepaths) { |
|
168 if (!-f "${locfile_path_temp}${languageID}\\${n}_${languageID}\.${ext}"){next;}; |
|
169 print "update ${locfile_path_temp}${languageID}\\${n}_${languageID}\.${ext} \n add lines\n @{add} \n"; |
|
170 open (OUT_LOC,">>${locfile_path_temp}${languageID}\\${n}_${languageID}\.${ext}") or die "Cannot open ${locfile_path_temp}${languageID}\\${n}_${languageID}\.${ext} for reading: $!"; |
|
171 print OUT_LOC "\n"; |
|
172 print OUT_LOC @add; |
|
173 close OUT_LOC; |
|
174 } |
|
175 |
|
176 } |
|
177 } |
|
178 |
|
179 # read .loc file and return defined strings |
|
180 sub read_loc { |
|
181 my $read_loc = shift; |
|
182 my %defines; |
|
183 |
|
184 open (IN,"$read_loc") or die "Cannot open ${read_loc} for reading: $!"; |
|
185 my @lines=<IN>; |
|
186 close IN; |
|
187 |
|
188 my $open_comment = 0; |
|
189 foreach (@lines) { |
|
190 chomp; |
|
191 if (m!^\s*/\*!) { |
|
192 $open_comment=1; |
|
193 if (m!\*/!) { |
|
194 $open_comment=0; |
|
195 } |
|
196 next; |
|
197 } |
|
198 if (m!\*/!) { |
|
199 $open_comment=0; |
|
200 next; |
|
201 } |
|
202 next if ($open_comment); |
|
203 next if (!m/^\s*?\#define/i); |
|
204 my $line = $_; |
|
205 |
|
206 $line =~ s/\"+/\"\#/; |
|
207 $_ =~ s/\s*?\#define\s+?//i; |
|
208 $_ =~ s/\s.*//i; |
|
209 |
|
210 $defines{$_}=$line; |
|
211 } |
|
212 |
|
213 return %defines; |
|
214 } |
|
215 |
|
216 |
|
217 # create switch loc file |
|
218 sub create_include_loc { |
|
219 my $loc_path = shift; |
|
220 my $loc_file = shift; |
|
221 my $languageids = shift; |
|
222 |
|
223 my ($n, $ext) = split ('\.', $loc_file); |
|
224 print "create file ${loc_path}${loc_file}\n"; |
|
225 open (OUT,">${loc_path}${loc_file}") or die "Cannot open ${loc_path}${loc_file} for writing: $!"; |
|
226 |
|
227 # my $first = 1; |
|
228 print OUT "\#if LANGUAGE_${loc_sc_folder}\n"; |
|
229 print OUT "\t\#include <${loc_sc_folder}\/${loc_file}\>\n"; |
|
230 |
|
231 foreach my $languageID (@$languageids) { |
|
232 # next if (!-f "${locfile_path}\\${languageID}\\${n}_${languageID}${ext}"); |
|
233 # if ($first) { |
|
234 # print OUT "\#if LANGUAGE_${languageID}\n"; |
|
235 # $first = 0; |
|
236 # } |
|
237 # else { |
|
238 print OUT "\#elif LANGUAGE_${languageID}\n"; |
|
239 # } |
|
240 print OUT "\t\#include <${languageID}\/${n}_${languageID}\.${ext}\>\n"; |
|
241 } |
|
242 |
|
243 # print OUT "\#else\n"; |
|
244 # print OUT "\t\#include <${loc_sc_folder}\\${loc_file}\>\n"; |
|
245 print OUT "\#endif\n"; |
|
246 |
|
247 |
|
248 close OUT; |
|
249 } |
|
250 |
|
251 sub datetime { |
|
252 my ($sec,$min,$hour,$day,$month,$year,$wbday,$yday,$isdst)=gmtime(); |
|
253 $month++; |
|
254 $year+=1900; |
|
255 |
|
256 return (sprintf("%04d.%02d.%02d %02d:%02d:%02d",$year,$month,$day,$hour,$min,$sec)); |
|
257 } |
|
258 |
|
259 sub hello { |
|
260 print "This is switch_loc_generator version ${version}\n"; |
|
261 print "\n"; |
|
262 } |
|
263 |
|
264 sub error_msg ($){ |
|
265 my($ErrorMsg); |
|
266 ($ErrorMsg)=@_; |
|
267 my $given_command=$0; |
|
268 $given_command =~ s/.*\\(\w+\.\w+)$/$1/; |
|
269 print ""; |
|
270 print "\n"; |
|
271 print "Error: $ErrorMsg \n\n"; |
|
272 help(); |
|
273 die "\n"; |
|
274 } |
|
275 |
|
276 sub help { |
|
277 my $given_command=$0; |
|
278 $given_command =~ s/.*\\(\w+\.\w+)$/$1/; |
|
279 |
|
280 print ""; |
|
281 print "\n"; |
|
282 hello(); |
|
283 print " Parameters:\n"; |
|
284 print " -i <path to localized loc zips> : custom path to generated switch loc files. Option for localisation team\n"; |
|
285 print " -zip <zip file name to zip up generated switch loc files> : path to zip up generated switch loc files. Option for localisation team\n"; |
|
286 print " -noUpdate : do not update localised loc files in epoc32 -folder\n"; |
|
287 print " -force : regenerate switch loc files even those are exists\n"; |
|
288 print " -h : print this info message\n"; |
|
289 print "Usage: \n$given_command -i <localised loc file folder>\n"; |
|
290 print "Example: \n$given_command -i \\s60\\misc\\release\\S60LocFiles\\data\n\n"; |
|
291 print "Example for localization team: \n$given_command -i ..\\data -zip ..\\data\\switch_loc.zip\n"; |
|
292 print "\n"; |
|
293 |
|
294 } |