From 387dc95134e859b029ff686b005bf5a9649f294d Mon Sep 17 00:00:00 2001 From: Jonas Häggqvist Date: Mon, 15 Dec 2008 17:12:42 +0000 Subject: Add an --inplace switch that modifies files directly, which should make this tool more useful. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@19445 a1c6a512-1295-4272-9138-f99709370657 --- tools/langtool.pl | 169 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 97 insertions(+), 72 deletions(-) diff --git a/tools/langtool.pl b/tools/langtool.pl index 79fedf5a0e..fe0c25bd39 100755 --- a/tools/langtool.pl +++ b/tools/langtool.pl @@ -17,7 +17,7 @@ sub usage { print < \$changetarget, 'changedesc' => \$changedesc, 'help' => \$help, + 'inplace' => \$inplace, 'ids=s' => \@ids, 'from=s' => \$from, @@ -104,6 +113,8 @@ if ( ($deprecate and $numids < 1) or # Do changesource, but either target or to not set ($changesource and ($s_target eq "" or $to eq "")) + or # More than one file passed, but inplace isn't set + ($numfiles > 1 and not $inplace) ) { usage(); exit(1); @@ -121,87 +132,101 @@ if ($changesource and not $to =~ /none|deprecated/) { $to = sprintf('"%s"', $to); } -open(LANGFILE, $ARGV[0]); -my $id = ""; -my $desc = ""; -my $location = ""; -my $target = ""; -my $string = ""; -my $open = 0; - -for () { - my $line = $_; - - if ($line =~ /^\s*<(\/?)([^>]+)>\s*$/) { - my $tag = $2; - $open = $1 eq "/" ? 0 : 1; - if ($open) { - $location = $tag; - ($target, $string) = ("", ""); - } - if ($open and $tag eq "phrase") { - $id = ""; - } - if (not $open) { - $location = ""; +foreach my $file (@ARGV) { + print(STDERR "$file\n"); + open(LANGFILE, $file) or die(sprintf("Couldn't open file for reading: %s", $file)); + my $id = ""; + my $desc = ""; + my $location = ""; + my $target = ""; + my $string = ""; + my $open = 0; + my $output = ""; + + for () { + my $line = $_; + + ### Set up values when a tag starts or ends ### + if ($line =~ /^\s*<(\/?)([^>]+)>\s*$/) { + my $tag = $2; + $open = $1 eq "/" ? 0 : 1; + if ($open) { + $location = $tag; + ($target, $string) = ("", ""); + } + if ($open and $tag eq "phrase") { + $id = ""; + } + if (not $open) { + $location = ""; + } } - } - elsif ($line =~ /^\s*([^:]*?)\s*:\s*(.*?)\s*$/) { - my ($key, $val) = ($1, $2); - if ($location =~ /source|dest|voice/) { - ($target, $string) = ($key, $val); + ### Set up values when a key: value pair is found ### + elsif ($line =~ /^\s*([^:]*?)\s*:\s*(.*?)\s*$/) { + my ($key, $val) = ($1, $2); + if ($location =~ /source|dest|voice/) { + ($target, $string) = ($key, $val); + } + if ($key eq "id") { + $id = $val; + } + elsif ($key eq "desc") { + $desc = $val; + } } - if ($key eq "id") { - $id = $val; + + if ($deprecate) { + if ($id ne "" and grep(/$id/, @ids)) { + # Set desc + $line =~ s/\s*desc:.*/ desc: deprecated/; + # Set user + $line =~ s/\s*user:.*/ user:/; + # Print an empty target line after opening tag (target isn't set) + if ($location =~ /source|dest|voice/ and $target eq "") { + $line .= " *: none\n"; + } + # Do not print target: string lines + elsif ($location =~ /source|dest|voice/ and $target ne "") { + $line = ""; + } + } } - elsif ($key eq "desc") { - $desc = $val; + elsif ($changetarget) { + # Change target if set and it's the same as $from + if ($id ne "" and grep(/$id/, @ids) and $location =~ /source|dest|voice/ and $target eq $from) { + $line =~ s/$from/$to/; + } } - } - - if ($deprecate) { - if ($id ne "" and grep(/$id/, @ids)) { - # Set desc - $line =~ s/\s*desc:.*/ desc: deprecated/; - # Set user - $line =~ s/\s*user:.*/ user:/; - # Print an empty target line after opening tag (target isn't set) - if ($location =~ /source|dest|voice/ and $target eq "") { - $line .= " *: none\n"; + elsif ($changesource) { + # Change string if $target is set and matches $s_target + if ($id ne "" and grep(/$id/, @ids) and $target eq $s_target and $location eq "source") { + $line =~ s/$string/$to/; } - # Do not print target: string lines - elsif ($location =~ /source|dest|voice/ and $target ne "") { - $line = ""; + } + elsif ($changedesc) { + # Simply change the desc line if the id matches + if ($id ne "" and grep(/$id/, @ids)) { + $line =~ s/\s*desc:.*/ desc: $to/; } } - print($line); - } - elsif ($changetarget) { - # Change target if set and it's the same as $from - if ($id ne "" and grep(/$id/, @ids) and $location =~ /source|dest|voice/ and $target eq $from) { - $line =~ s/$from/$to/; + elsif ($changeid) { + $line =~ s/^\s*id:\s*$from.*$/ id: $to/; } - print($line); - } - elsif ($changesource) { - # Change string if $target is set and matches $s_target - if ($id ne "" and grep(/$id/, @ids) and $target eq $s_target and $location eq "source") { - $line =~ s/$string/$to/; + else { + print("This should never happen.\n"); + exit(3); } - print($line); - } - elsif ($changedesc) { - # Simply change the desc line if the id matches - if ($id ne "" and grep(/$id/, @ids)) { - $line =~ s/\s*desc:.*/ desc: $to/; + if ($inplace) { + $output .= $line; + } + else { + print($line); } - print($line); - } - elsif ($changeid) { - $line =~ s/^\s*id:\s*$from.*$/ id: $to/; - print($line); } - else { - print("wut wut\n"); + close(LANGFILE); + if ($inplace) { + open(LANGFILE, ">", $file) or die(sprintf("Couldn't open file for writing: %s\n", $file)); + print(LANGFILE $output); + close(LANGFILE); } } -- cgit v1.2.3