1
0
Fork 0
forked from len0rd/rockbox

Implement a --delete option for langtoo for easy deletion of lang phrases

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@26556 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Nils Wallménius 2010-06-04 20:40:49 +00:00
parent 3704f1f71a
commit b91fa04250

View file

@ -53,6 +53,12 @@ Usage langtool [--inplace] --options langfile1 [langfile2 ...]
Example: Example:
langtool --changetarget --from e200 --to e200,c200 --id LANG_ON dansk.lang langtool --changetarget --from e200 --to e200,c200 --id LANG_ON dansk.lang
--delete --id ID_1,ID_2 --id ID_3 langfile
Delete a number of ids. THIS WILL BREAK BACKWARDS COMPATIBILITY. Use with
extreme caution.
Example: langtool --delete --id LANG_LEFT,LANG_RIGHT english.lang
--inplace --inplace
Perform file operations in-place, instead of outputting the result to Perform file operations in-place, instead of outputting the result to
@ -77,6 +83,7 @@ my $changesource = '';
my $changeid = ''; my $changeid = '';
my $changetarget = ''; my $changetarget = '';
my $changedesc = ''; my $changedesc = '';
my $delete = '';
my $inplace = ''; my $inplace = '';
my $help = ''; my $help = '';
# Parameters # Parameters
@ -91,6 +98,7 @@ GetOptions(
'changeid' => \$changeid, 'changeid' => \$changeid,
'changetarget' => \$changetarget, 'changetarget' => \$changetarget,
'changedesc' => \$changedesc, 'changedesc' => \$changedesc,
'delete' => \$delete,
'help' => \$help, 'help' => \$help,
'inplace' => \$inplace, 'inplace' => \$inplace,
@ -110,8 +118,8 @@ if ($help) {
exit(); exit();
} }
# More than one option set (or none) # More than one option set (or none)
elsif (($deprecate + $changesource + $changeid + $changetarget + $changedesc) != 1) { elsif (($deprecate + $changesource + $changeid + $changetarget + $changedesc +$delete) != 1) {
error("Exactly one of --deprecate, --changesource, --changeid, --changetarget,\n--changedesc must be used."); error("Exactly one of --deprecate, --changesource, --changeid, --changetarget,\n--changedesc, --delete must be used.");
} }
# Do changeid, but either from or to is empty # Do changeid, but either from or to is empty
elsif ($changeid and ($from eq "" or $to eq "")) { elsif ($changeid and ($from eq "" or $to eq "")) {
@ -129,6 +137,10 @@ elsif ($changetarget and ($from eq "" or $to eq "")) {
elsif ($deprecate and $numids < 1) { elsif ($deprecate and $numids < 1) {
error("--deprecate used, but no IDs specified"); error("--deprecate used, but no IDs specified");
} }
# Do delete, but no ids set
elsif ($delete and $numids < 1) {
error("--delete used, but no IDs specified");
}
# Do changesource, but either target or to not set # Do changesource, but either target or to not set
elsif ($changesource and ($s_target eq "" or $to eq "")) { elsif ($changesource and ($s_target eq "" or $to eq "")) {
error("--changesource used, but --target or --to not set"); error("--changesource used, but --target or --to not set");
@ -159,7 +171,7 @@ foreach my $file (@ARGV) {
my $target = ""; my $target = "";
my $string = ""; my $string = "";
my $open = 0; my $open = 0;
my $output = ""; my @output;
for (<LANGFILE>) { for (<LANGFILE>) {
my $line = $_; my $line = $_;
@ -209,6 +221,16 @@ foreach my $file (@ARGV) {
} }
} }
} }
elsif ($delete) {
if ($id ne "" and grep(/^$id$/, @ids)) {
if ($location eq "phrase" and $line =~ /id:/) {
# Kluge to nuke the <phrase> line
pop(@output);
}
# Set the whole phrase to empty string.
$line = "";
}
}
elsif ($changetarget) { elsif ($changetarget) {
# Change target if set and it's the same as $from # 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) { if ($id ne "" and grep(/^$id$/, @ids) and $location =~ /source|dest|voice/ and $target eq $from) {
@ -234,17 +256,16 @@ foreach my $file (@ARGV) {
print("This should never happen.\n"); print("This should never happen.\n");
exit(3); exit(3);
} }
if ($inplace) {
$output .= $line; push(@output, $line);
}
else {
print($line);
}
} }
close(LANGFILE); close(LANGFILE);
if ($inplace) { if ($inplace) {
open(LANGFILE, ">", $file) or die(sprintf("Couldn't open file for writing: %s\n", $file)); open(LANGFILE, ">", $file) or die(sprintf("Couldn't open file for writing: %s\n", $file));
print(LANGFILE $output); print(LANGFILE @output);
close(LANGFILE); close(LANGFILE);
} }
else {
print(@output);
}
} }