Image/ExifTool/TagInfoXML.pm untested

filter: Statementstmt 0.0% 0 / 464 Branchbran 0.0% 0 / 296 Conditioncond 0.0% 0 / 233 MC/DCmcdc 0.0% 0 / 142 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 1163 CC 149 SCAR 80.1
CC
149
Cov
0%
BuildLangModules
4290.0
Write
3540.0
WriteLangModule
272.0
CRAP 3007.4
1 #------------------------------------------------------------------------------
2 # File: TagInfoXML.pm
3 #
4 # Description: Read/write tag information XML database
5 #
6 # Revisions: 2009/01/28 - P. Harvey Created
7 #------------------------------------------------------------------------------
8  
9 package Image::ExifTool::TagInfoXML;
10  
11 use strict;
12 require Exporter;
13  
14 use vars qw($VERSION @ISA $makeMissing);
15 use Image::ExifTool qw(:Utils :Vars);
16 use Image::ExifTool::XMP;
17  
18 $VERSION = '1.38';
19 @ISA = qw(Exporter);
20  
21 # set this to a language code to generate Lang module with 'MISSING' entries
22 $makeMissing = '';
23  
24 sub LoadLangModules($;$);
25 sub WriteLangModule($$;$);
26 sub NumbersFirst;
27  
28 # names for acknowledgements in the POD documentation
29 my %credits = (
30 cs => 'Jens Duttke and Petr MichE<aacute>lek',
31 de => 'Jens Duttke, Herbert Kauer and Jobi',
32 es => 'Jens Duttke, Santiago del BrE<iacute>o GonzE<aacute>lez and Emilio Sancha',
33 fi => 'Jens Duttke and Jarkko ME<auml>kineva',
34 fr => 'Jens Duttke, Bernard Guillotin, Jean Glasser, Jean Piquemal, Harry Nizard, Alphonse Philippe and Philippe Bonnaure (GraphicConverter)',
35 it => 'Jens Duttke, Ferdinando Agovino, Emilio Dati and Michele Locati',
36 ja => 'Jens Duttke and Kazunari Nishina',
37 ko => 'Jens Duttke and Jeong Beom Kim',
38 nl => 'Jens Duttke, Peter Moonen, Herman Beld and Peter van der Laan',
39 pl => 'Jens Duttke, Przemyslaw Sulek and Kacper Perschke',
40 ru => 'Jens Duttke, Sergey Shemetov, Dmitry Yerokhin, Anton Sukhinov and Alexander',
41 sk => 'Peter Bagin',
42 sv => 'Jens Duttke and BjE<ouml>rn SE<ouml>derstrE<ouml>m',
43 'tr' => 'Jens Duttke, Hasan Yildirim and Cihan Ulusoy',
44 zh_cn => 'Jens Duttke and Haibing Zhong',
45 zh_tw => 'Jens Duttke and MikeF',
46 );
47  
48 # translate country codes to language codes
49 my %translateLang = (
50 ch_s => 'zh_cn',
51 ch_cn => 'zh_cn',
52 ch_tw => 'zh_tw',
53 cz => 'cs',
54 jp => 'ja',
55 kr => 'ko',
56 se => 'sv',
57 );
58  
59 my $numbersFirst = 1; # set to -1 to sort numbers last, or 2 to put negative numbers last
60 my $caseInsensitive; # used internally by sort routine
61  
62 # write groups that don't represent real family 1 group names
63 my %fakeWriteGroup = (
64 Comment => 1, # (JPEG Comment)
65 colr => 1, # (Jpeg2000 'colr' box)
66 );
67  
68 #------------------------------------------------------------------------------
69 # Utility to print tag information database as an XML list
70 # Inputs: 0) output file name (undef to send to console),
71 # 1) group name (may be undef), 2) options hash ('Flags','NoDesc','Lang')
72 # Returns: true on success
73 sub Write(;$$%)
74 {
75 local ($_, *PTIFILE);
76 my ($file, $group, %opts) = @_;
77 my $et = Image::ExifTool->new;
78 my ($fp, $tableName, %langInfo, @langs, $defaultLang, @groups);
79 @groups = split ':', $group if $group;
80  
81 Image::ExifTool::LoadAllTables(); # first load all our tables
82 unless ($opts{NoDesc}) {
83 $defaultLang = $Image::ExifTool::defaultLang;
84 LoadLangModules(\%langInfo, $opts{Lang}); # load necessary Lang modules
85 if ($opts{Lang}) {
86 @langs = grep /^$opts{Lang}$/i, keys %langInfo;
87 } else {
88 @langs = sort keys %langInfo;
89 }
90 }
91 if (defined $file) {
92 open PTIFILE, ">$file" or return 0;
93 $fp = \*PTIFILE;
94 } else {
95 $fp = \*STDOUT;
96 }
97 print $fp "<?xml version='1.0' encoding='UTF-8'?>\n";
98 print $fp "<!-- Generated by Image::ExifTool $Image::ExifTool::VERSION -->\n";
99 print $fp "<taginfo>\n\n";
100  
101 # loop through all tables and save tag names to %allTags hash
102 foreach $tableName (sort keys %allTables) {
103 my $table = GetTagTable($tableName);
104 my $grps = $$table{GROUPS};
105 my ($tagID, $didTag);
106 # sort in same order as tag name documentation
107 $caseInsensitive = ($tableName =~ /::XMP::/);
108 # get list of languages defining elements in this table
109 my $isBinary = ($$table{PROCESS_PROC} and
110 $$table{PROCESS_PROC} eq \&Image::ExifTool::ProcessBinaryData);
111 # generate flattened tag names for structure fields if this is an XMP table
112 if ($$table{GROUPS} and $$table{GROUPS}{0} eq 'XMP') {
113 Image::ExifTool::XMP::AddFlattenedTags($table);
114 }
115 $numbersFirst = 2;
116 $numbersFirst = -1 if $$table{VARS} and $$table{VARS}{ALPHA_FIRST};
117 my @keys = sort NumbersFirst TagTableKeys($table);
118 $numbersFirst = 1;
119 # loop through all tag ID's in this table
120 foreach $tagID (@keys) {
121 my @infoArray = GetTagInfoList($table, $tagID);
122 my $xmlID = Image::ExifTool::XMP::FullEscapeXML($tagID);
123 # get a list of languages defining elements for this ID
124 my ($index, $fam);
125 PTILoop: for ($index=0; $index<@infoArray; ++$index) {
126 my $tagInfo = $infoArray[$index];
127 # don't list subdirectories unless they are writable
128 next unless $$tagInfo{Writable} or not $$tagInfo{SubDirectory};
129 next if $$tagInfo{Hidden};
130 if (@groups) {
131 my @tg = $et->GetGroup($tagInfo);
132 foreach $group (@groups) {
133 next PTILoop unless grep /^$group$/i, @tg;
134 }
135 }
136 unless ($didTag) {
137 my $tname = $$table{SHORT_NAME};
138 print $fp "<table name='${tname}' g0='$$grps{0}' g1='$$grps{1}' g2='$$grps{2}'>\n";
139 unless ($opts{NoDesc}) {
140 # print table description
141 my $desc = $$table{TABLE_DESC};
142 unless ($desc) {
143 ($desc = $tname) =~ s/::Main$//;
144 $desc =~ s/::/ /g;
145 }
146 # print alternate language descriptions
147 print $fp " <desc lang='en'>$desc</desc>\n";
148 foreach (@langs) {
149 $desc = $langInfo{$_}{$tableName} or next;
150 $desc = Image::ExifTool::XMP::EscapeXML($desc);
151 print $fp " <desc lang='${_}'>$desc</desc>\n";
152 }
153 }
154 $didTag = 1;
155 }
156 my $name = $$tagInfo{Name};
157 my $ind = @infoArray > 1 ? " index='${index}'" : '';
158 my $format = $$tagInfo{Writable} || $$table{WRITABLE};
159 my $writable = $format ? 'true' : 'false';
160 # check our conversions to make sure we can really write this tag
161 if ($writable eq 'true') {
162 $writable = 'false' if defined $$tagInfo{Writable} and not $$tagInfo{Writable};
163 foreach ('PrintConv','ValueConv') {
164 next unless $$tagInfo{$_};
165 next if $$tagInfo{$_ . 'Inv'};
166 next if ref($$tagInfo{$_}) =~ /^(HASH|ARRAY)$/;
167 next if $$tagInfo{WriteAlso};
168 $writable = 'false';
169 last;
170 }
171 }
172 $format = $$tagInfo{Format} || $$table{FORMAT} if not $format or $format eq '1';
173 $format = 'struct' if $$tagInfo{Struct};
174 if (defined $format) {
175 $format =~ s/\[.*\$.*\]//; # remove expressions from format
176 $format = 'undef' if $format eq '2'; # (special case)
177 } elsif ($isBinary) {
178 $format = 'int8u';
179 } else {
180 $format = '?';
181 }
182 my $count = '';
183 if ($format =~ s/\[.*?(\d*)\]$//) {
184 $count = " count='${1}'" if length $1;
185 } elsif ($$tagInfo{Count} and $$tagInfo{Count} > 1) {
186 $count = " count='$$tagInfo{Count}'";
187 }
188 my @groups = $et->GetGroup($tagInfo);
189 my $writeGroup = $$tagInfo{WriteGroup} || $$table{WRITE_GROUP};
190 # use common write group for group 1 (unless fake)
191 $groups[1] = $writeGroup if $writeGroup and not $fakeWriteGroup{$writeGroup};
192 # add group names if different from table defaults
193 my $grp = '';
194 for ($fam=0; $fam<3; ++$fam) {
195 $grp .= " g$fam='$groups[$fam]'" if $groups[$fam] ne $$grps{$fam};
196 }
197 # add flags if necessary
198 if ($opts{Flags}) {
199 my @flags;
200 foreach (qw(Avoid Binary List Mandatory Unknown)) {
201 push @flags, $_ if $$tagInfo{$_};
202 }
203 push @flags, $$tagInfo{List} if $$tagInfo{List} and $$tagInfo{List} =~ /^(Alt|Bag|Seq)$/;
204 push @flags, 'Flattened' if defined $$tagInfo{Flat};
205 push @flags, 'Unsafe' if $$tagInfo{Protected} and $$tagInfo{Protected} & 0x01;
206 push @flags, 'Protected' if $$tagInfo{Protected} and $$tagInfo{Protected} & 0x02;
207 push @flags, 'Permanent' if $$tagInfo{Permanent} or
208 ($groups[0] eq 'MakerNotes' and not defined $$tagInfo{Permanent});
209 $grp = " flags='" . join(',', sort @flags) . "'$grp" if @flags;
210 # add parent structure tag ID
211 $grp .= " struct='$$tagInfo{ParentTagInfo}{TagID}'" if $$tagInfo{ParentTagInfo};
212 }
213 print $fp " <tag id='${xmlID}' name='${name}'$ind type='${format}'$count writable='${writable}'$grp";
214 if ($opts{NoDesc}) {
215 # short output format
216 print $fp "/>\n"; # empty tag element
217 next; # no descriptions or values
218 } else {
219 print $fp ">";
220 }
221 my $desc = $$tagInfo{Description};
222 $desc = Image::ExifTool::MakeDescription($name) unless defined $desc;
223 # add alternate language descriptions and get references
224 # to alternate language PrintConv hashes
225 my $altDescr = '';
226 my %langConv;
227 foreach (@langs) {
228 my $ld = $langInfo{$_}{$name} or next;
229 if (ref $ld) {
230 $langConv{$_} = $$ld{PrintConv};
231 $ld = $$ld{Description} or next;
232 }
233 # ignore descriptions that are the same as the default language
234 next if $ld eq $desc;
235 $ld = Image::ExifTool::XMP::EscapeXML($ld);
236 $altDescr .= "\n <desc lang='${_}'>$ld</desc>";
237 }
238 # print tag descriptions
239 $desc = Image::ExifTool::XMP::EscapeXML($desc);
240 unless ($opts{Lang} and $altDescr) {
241 print $fp "\n <desc lang='${defaultLang}'>$desc</desc>";
242 }
243 print $fp "$altDescr\n";
244 for (my $i=0; ; ++$i) {
245 my $conv = $$tagInfo{PrintConv};
246 # (hidden = '' disables PrintConv in -listx output)
247 last if defined $$tagInfo{Hidden} and $$tagInfo{Hidden} eq '';
248 my $idx = '';
249 if (ref $conv eq 'ARRAY') {
250 last unless $i < @$conv;
251 $conv = $$conv[$i];
252 $idx = " index='${i}'";
253 } else {
254 last if $i;
255 }
256 next unless ref $conv eq 'HASH';
257 # make a list of available alternate languages
258 my @langConv = sort keys %langConv;
259 print $fp " <values$idx>\n";
260 my $key;
261 $caseInsensitive = 0;
262 # add bitmask values to main lookup
263 if ($$conv{BITMASK}) {
264 foreach $key (keys %{$$conv{BITMASK}}) {
265 my $mask = "Bit$key";
266 next if not $mask or $$conv{$mask};
267 $$conv{$mask} = $$conv{BITMASK}{$key};
268 }
269 }
270 foreach $key (sort NumbersFirst keys %$conv) {
271 next if $key eq 'BITMASK' or $key eq 'OTHER' or $key eq 'Notes';
272 my $val = $$conv{$key};
273 my $xmlVal = Image::ExifTool::XMP::EscapeXML($val);
274 my $xmlKey = Image::ExifTool::XMP::FullEscapeXML($key);
275 print $fp " <key id='${xmlKey}'>\n";
276 # add alternate language values
277 my $altConv = '';
278 foreach (@langConv) {
279 my $lv = $langConv{$_};
280 # handle indexed PrintConv entries
281 $lv = $$lv[$i] or next if ref $lv eq 'ARRAY';
282 $lv = $$lv{$val};
283 # ignore values that are missing or same as default
284 next unless defined $lv and $lv ne $val;
285 $lv = Image::ExifTool::XMP::EscapeXML($lv);
286 $altConv .= " <val lang='${_}'>$lv</val>\n";
287 }
288 unless ($opts{Lang} and $altConv) {
289 print $fp " <val lang='${defaultLang}'>$xmlVal</val>\n"
290 }
291 print $fp "$altConv </key>\n";
292 }
293 print $fp " </values>\n";
294 }
295 print $fp " </tag>\n";
296 }
297 }
298 print $fp "</table>\n\n" if $didTag;
299 }
300 my $success = 1;
301 print $fp "</taginfo>\n" or $success = 0;
302 close $fp or $success = 0 if defined $file;
303 return $success;
304 }
305  
306 #------------------------------------------------------------------------------
307 # Escape backslash and quote in string
308 # Inputs: string
309 # Returns: escaped string
310 sub EscapePerl
311 {
312 my $str = shift;
313 $str =~ s/\\/\\\\/g;
314 $str =~ s/'/\\'/g;
315 return $str;
316 }
317  
318 #------------------------------------------------------------------------------
319 # Generate Lang modules from input tag info XML database
320 # Inputs: 0) XML filename, 1) update flags:
321 # 0x01 = preserve version numbers
322 # 0x02 = update all modules, even if they didn't change
323 # 0x04 = update from scratch, ignoring existing definitions
324 # 0x08 = override existing different descriptions and values
325 # Returns: Count of updated Lang modules, or -1 on error
326 # Notes: Must be run from the directory containing 'lib'
327 sub BuildLangModules($;$)
328 {
329 local ($_, *XFILE);
330 my ($file, $updateFlag) = @_;
331 my ($table, $tableName, $id, $index, $valIndex, $name, $key, $lang, $defDesc);
332 my (%langInfo, %different, %changed, $overrideDifferent);
333  
334 Image::ExifTool::LoadAllTables(); # first load all our tables
335 # generate our flattened tags
336 foreach $tableName (sort keys %allTables) {
337 my $table = GetTagTable($tableName);
338 next unless $$table{GROUPS} and $$table{GROUPS}{0} eq 'XMP';
339 Image::ExifTool::XMP::AddFlattenedTags($table);
340 }
341 LoadLangModules(\%langInfo); # load all existing Lang modules
342 $updateFlag = 0 unless $updateFlag;
343 %langInfo = () if $updateFlag & 0x04;
344 $overrideDifferent = 1 if $updateFlag & 0x08;
345  
346 if (defined $file) {
347 open XFILE, $file or return -1;
348 while (<XFILE>) {
349 next unless /^\s*<(\/?)(\w+)/;
350 my $tok = $2;
351 if ($1) {
352 # close appropriate entities
353 if ($tok eq 'tag') {
354 undef $id;
355 undef $index;
356 undef $name;
357 undef $defDesc;
358 } elsif ($tok eq 'values') {
359 undef $key;
360 undef $valIndex;
361 } elsif ($tok eq 'table') {
362 undef $table;
363 undef $id;
364 }
365 next;
366 }
367 if ($tok eq 'table') {
368 /^\s*<table name='([^']+)'[ >]/ or warn('Bad table'), next;
369 $tableName = "Image::ExifTool::$1";
370 # ignore userdefined tables
371 next if $tableName =~ /^Image::ExifTool::UserDefined/;
372 $table = Image::ExifTool::GetTagTable($tableName);
373 $table or warn("Unknown tag table $tableName\n");
374 next;
375 }
376 next unless defined $table;
377 if ($tok eq 'tag') {
378 /^\s*<tag id='([^']*)' name='([^']+)'( index='(\d+)')?[ >]/ or warn('Bad tag'), next;
379 $id = Image::ExifTool::XMP::FullUnescapeXML($1);
380 $name = $2;
381 $index = $4;
382 # convert hex ID's unless ID_FMT is something other than 'hex'
383 # (for string ID's that look like hex)
384 if ($id =~ /^0x[\da-fA-F]+$/ and (not defined $$table{VARS} or
385 not defined $$table{VARS}{ID_FMT} or $$table{VARS}{ID_FMT} eq 'hex'))
386 {
387 $id = hex($id);
388 }
389 next;
390 }
391 if ($tok eq 'values') {
392 /^\s*<values index='([^']*)'>/ or next;
393 $valIndex = $1;
394 } elsif ($tok eq 'key') {
395 defined $id or warn('No ID'), next;
396 /^\s*<key id='([^']*)'>/ or warn('Bad key'), next;
397 $key = Image::ExifTool::XMP::FullUnescapeXML($1);
398 $key = hex($key) if $key =~ /^0x[\da-fA-F]+$/; # convert hex keys
399 } elsif ($tok eq 'val' or $tok eq 'desc') {
400 /^\s*<$tok( lang='([-\w]+?)')?>(.*)<\/$tok>/ or warn("Bad $tok"), next;
401 $tok eq 'desc' and defined $key and warn('Out of order "desc"'), next;
402 my $lang = $2 or next; # looking only for alternate languages
403 $lang =~ tr/-A-Z/_a-z/;
404 # use standard ISO 639-1 language codes
405 $lang = $translateLang{$lang} if $translateLang{$lang};
406 my $tval = Image::ExifTool::XMP::UnescapeXML($3);
407 my $val = ucfirst $tval;
408 $val = $tval if $tval =~ /^(cRAW|iTun)/; # special-case non-capitalized values
409 my $cap = ($tval ne $val);
410 if ($makeMissing) {
411 if ($lang eq 'en') {
412 $lang = $makeMissing;
413 $val = 'MISSING';
414 undef $cap;
415 }
416 } elsif ($val eq 'MISSING') {
417 next; # ignore "MISSING" entries
418 }
419 my $isDefault = ($lang eq $Image::ExifTool::defaultLang);
420 unless ($langInfo{$lang} or $isDefault) {
421 print "Creating new language $lang\n";
422 $langInfo{$lang} = { };
423 }
424 defined $name or $name = '<unknown>';
425 unless (defined $id) {
426 next if $isDefault;
427 # this is a table description
428 next if $langInfo{$lang}{$tableName} and
429 $langInfo{$lang}{$tableName} eq $val;
430 $langInfo{$lang}{$tableName} = $val;
431 $changed{$lang} = 1;
432 warn("Capitalized '${lang}' val for $name: $val\n") if $cap;
433 next;
434 }
435 my @infoArray = GetTagInfoList($table, $id);
436  
437 # this will fail for UserDefined tags and tags without ID's
438 @infoArray or warn("Error loading tag for $tableName ID='${id}'\n"), next;
439 my ($tagInfo, $langInfo);
440 if (defined $index) {
441 $tagInfo = $infoArray[$index];
442 $tagInfo or warn('Invalid index'), next;
443 } else {
444 @infoArray > 1 and warn('Missing index'), next;
445 $tagInfo = $infoArray[0];
446 }
447 my $tagName = $$tagInfo{Name};
448 if ($isDefault) {
449 unless ($$tagInfo{Description}) {
450 $$tagInfo{Description} = Image::ExifTool::MakeDescription($tagName);
451 }
452 $defDesc = $$tagInfo{Description};
453 $langInfo = $tagInfo;
454 } else {
455 $langInfo = $langInfo{$lang}{$tagName};
456 if (not defined $langInfo) {
457 $langInfo = $langInfo{$lang}{$tagName} = { };
458 } elsif (not ref $langInfo) {
459 $langInfo = $langInfo{$lang}{$tagName} = { Description => $langInfo };
460 }
461 }
462 # save new value in langInfo record
463 if ($tok eq 'desc') {
464 my $oldVal = $$langInfo{Description};
465 next if defined $oldVal and $oldVal eq $val;
466 if ($makeMissing) {
467 next if defined $oldVal and $val eq 'MISSING';
468 } elsif (defined $oldVal) {
469 my $t = "$lang $tagName";
470 unless (defined $different{$t} and $different{$t} eq $val) {
471 my $a = defined $different{$t} ? 'ANOTHER ' : '';
472 warn "${a}Different '${lang}' desc for $tagName: $val (was $$langInfo{Description})\n";
473 next if defined $different{$t}; # don't change back again
474 $different{$t} = $val;
475 }
476 next unless $overrideDifferent;
477 }
478 next if $isDefault;
479 if (defined $defDesc and $defDesc eq $val) {
480 delete $$langInfo{Description}; # delete if same as default language
481 } else {
482 $$langInfo{Description} = $val;
483 }
484 } else {
485 defined $key or warn("No key for $$tagInfo{Name}"), next;
486 my $printConv = $$tagInfo{PrintConv};
487 if (ref $printConv eq 'ARRAY') {
488 defined $valIndex or warn('No value index'), next;
489 $printConv = $$printConv[$valIndex];
490 }
491 ref $printConv eq 'HASH' or warn('No PrintConv'), next;
492 my $convVal = $$printConv{$key};
493 unless (defined $convVal) {
494 if ($$printConv{BITMASK} and $key =~ /^\d+$/) {
495 my $i;
496 for ($i=0; $i<64; ++$i) {
497 my $mask = (0x01 << $i) or last;
498 next unless $key == $mask;
499 $convVal = $$printConv{BITMASK}{$i};
500 }
501 }
502 warn("Missing PrintConv entry for $tableName $$tagInfo{Name} $key\n") and next unless defined $convVal;
503 }
504 if ($cap and $convVal =~ /^[a-z]/) {
505 $val = lcfirst $val; # change back to lower case
506 undef $cap;
507 }
508 my $lc = $$langInfo{PrintConv};
509 $lc or $lc = $$langInfo{PrintConv} = { };
510 $lc = $printConv if ref $lc eq 'ARRAY'; #(default lang only)
511 my $oldVal = $$lc{$convVal};
512 next if defined $oldVal and $oldVal eq $val;
513 if ($makeMissing) {
514 next if defined $oldVal and $val eq 'MISSING';
515 } elsif (defined $oldVal and (not $isDefault or not $val=~/^\d+$/)) {
516 my $t = "$lang $tagName $convVal";
517 unless (defined $different{$t} and $different{$t} eq $val) {
518 my $a = defined $different{$t} ? 'ANOTHER ' : '';
519 warn "${a}Different '${lang}' val for $tagName '${convVal}': $val (was $oldVal)\n";
520 next if defined $different{$t}; # don't change back again
521 $different{$t} = $val;
522 }
523 next unless $overrideDifferent;
524 }
525 next if $isDefault;
526 warn("Capitalized '${lang}' val for $tagName: $tval\n") if $cap;
527 $$lc{$convVal} = $val;
528 }
529 $changed{$lang} = 1;
530 }
531 }
532 close XFILE;
533 }
534 # rewrite all changed Lang modules
535 my $rtnVal = 0;
536 foreach $lang ($updateFlag & 0x02 ? @Image::ExifTool::langs : sort keys %changed) {
537 next if $lang eq $Image::ExifTool::defaultLang;
538 ++$rtnVal;
539 # write this module (only increment version number if not forced)
540 WriteLangModule($lang, $langInfo{$lang}, not $updateFlag & 0x01) or $rtnVal = -1, last;
541 }
542 return $rtnVal;
543 }
544  
545 #------------------------------------------------------------------------------
546 # Write Lang module
547 # Inputs: 0) language string, 1) langInfo lookup reference, 2) flag to increment version
548 # Returns: true on success
549 sub WriteLangModule($$;$)
550 {
551 local ($_, *XOUT);
552 my ($lang, $langTags, $newVersion) = @_;
553 my $err;
554 -e "lib/Image/ExifTool" or die "Must run from directory containing 'lib'\n";
555 my $out = "lib/Image/ExifTool/Lang/$lang.pm";
556 my $tmp = "$out.tmp";
557 open XOUT, ">$tmp" or die "Error creating $tmp\n";
558 my $ver = "Image::ExifTool::Lang::${lang}::VERSION";
559 no strict 'refs';
560 if ($$ver) {
561 $ver = $$ver;
562 $ver = int($ver * 100 + 1.5) / 100 if $newVersion;
563 } else {
564 $ver = 1.0;
565 }
566 $ver = sprintf('%.2f', $ver);
567 use strict 'refs';
568 my $langName = $Image::ExifTool::langName{$lang} || $lang;
569 $langName =~ s/\s*\(.*//;
570 print XOUT <<HEADER;
571 #------------------------------------------------------------------------------
572 # File: $lang.pm
573 #
574 # Description: ExifTool $langName language translations
575 #
576 # Notes: This file generated automatically by Image::ExifTool::TagInfoXML
577 #------------------------------------------------------------------------------
578  
579 package Image::ExifTool::Lang::$lang;
580  
581 use strict;
582 use vars qw(\$VERSION);
583  
584 \$VERSION = '${ver}';
585  
586 HEADER
587 print XOUT "\%Image::ExifTool::Lang::${lang}::Translate = (\n";
588 # loop through all tag and table names
589 my $tag;
590 foreach $tag (sort keys %$langTags) {
591 my $desc = $$langTags{$tag};
592 my $conv;
593 if (ref $desc) {
594 $conv = $$desc{PrintConv};
595 $desc = $$desc{Description};
596 # remove description if not necessary
597 # (not strictly correct -- should test against tag description, not name)
598 undef $desc if $desc and $desc eq $tag;
599 # remove unnecessary value translations
600 if ($conv) {
601 my @keys = keys %$conv;
602 foreach (@keys) {
603 delete $$conv{$_} if $_ eq $$conv{$_};
604 }
605 undef $conv unless %$conv;
606 }
607 }
608 if (defined $desc) {
609 $desc = EscapePerl($desc);
610 } else {
611 next unless $conv;
612 }
613 print XOUT " '${tag}' => ";
614 unless ($conv) {
615 print XOUT "'${desc}',\n";
616 next;
617 }
618 print XOUT "{\n";
619 print XOUT " Description => '${desc}',\n" if defined $desc;
620 if ($conv) {
621 print XOUT " PrintConv => {\n";
622 foreach (sort keys %$conv) {
623 my $str = EscapePerl($_);
624 my $val = EscapePerl($$conv{$_});
625 print XOUT " '${str}' => '${val}',\n";
626 }
627 print XOUT " },\n";
628 }
629 print XOUT " },\n";
630 }
631 # generate acknowledgements for this language
632 my $ack;
633 if ($credits{$lang}) {
634 $ack = "Thanks to $credits{$lang} for providing this translation.";
635 $ack =~ s/(.{1,76})( +|$)/$1\n/sg; # wrap text to 76 columns
636 $ack = "~head1 ACKNOWLEDGEMENTS\n\n$ack\n";
637 } else {
638 $ack = '';
639 }
640 my $footer = <<FOOTER;
641 );
642  
643 1; # end
644  
645 __END__