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
diff --git a/tools/langtool.pl b/tools/langtool.pl
index 2e95931..a0e147c 100755
--- a/tools/langtool.pl
+++ b/tools/langtool.pl
@@ -53,6 +53,12 @@
     Example:
       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
 
     Perform file operations in-place, instead of outputting the result to
@@ -77,6 +83,7 @@
 my $changeid = '';
 my $changetarget = '';
 my $changedesc = '';
+my $delete = '';
 my $inplace = '';
 my $help = '';
 # Parameters
@@ -91,6 +98,7 @@
     'changeid'     => \$changeid,
     'changetarget' => \$changetarget,
     'changedesc'   => \$changedesc,
+    'delete'       => \$delete,
     'help'         => \$help,
     'inplace'      => \$inplace,
 
@@ -110,8 +118,8 @@
     exit();
 }
 # More than one option set (or none)
-elsif (($deprecate + $changesource + $changeid + $changetarget + $changedesc) != 1) {
-    error("Exactly one of --deprecate, --changesource, --changeid, --changetarget,\n--changedesc must be used.");
+elsif (($deprecate + $changesource + $changeid + $changetarget + $changedesc +$delete) != 1) {
+    error("Exactly one of --deprecate, --changesource, --changeid, --changetarget,\n--changedesc, --delete must be used.");
 }
 # Do changeid, but either from or to is empty
 elsif ($changeid and ($from eq "" or $to eq "")) {
@@ -129,6 +137,10 @@
 elsif ($deprecate and $numids < 1) {
     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
 elsif ($changesource and ($s_target eq "" or $to eq "")) {
     error("--changesource used, but --target or --to not set");
@@ -159,7 +171,7 @@
     my $target = "";
     my $string = "";
     my $open = 0;
-    my $output = "";
+    my @output;
 
     for (<LANGFILE>) {
         my $line = $_;
@@ -209,6 +221,16 @@
                 }
             }
         }
+        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) {
             # 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) {
@@ -234,17 +256,16 @@
             print("This should never happen.\n");
             exit(3);
         }
-        if ($inplace) {
-            $output .= $line;
-        }
-        else {
-            print($line);
-        }
+
+        push(@output, $line);
     }
     close(LANGFILE);
     if ($inplace) {
         open(LANGFILE, ">", $file) or die(sprintf("Couldn't open file for writing: %s\n", $file));
-        print(LANGFILE $output);
+        print(LANGFILE @output);
         close(LANGFILE);
     }
+    else {
+        print(@output);
+    }
 }