forked from len0rd/rockbox
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
This commit is contained in:
parent
db1ea97fe8
commit
387dc95134
1 changed files with 99 additions and 74 deletions
|
@ -17,7 +17,7 @@
|
||||||
|
|
||||||
sub usage {
|
sub usage {
|
||||||
print <<MOO
|
print <<MOO
|
||||||
Usage langtool --options langfile
|
Usage langtool [--inplace] --options langfile1 [langfile2 ...]
|
||||||
|
|
||||||
For all actions, the modified langfile will be output on stdout. When doing
|
For all actions, the modified langfile will be output on stdout. When doing
|
||||||
stuff to english.lang, you should almost always apply the same change to all
|
stuff to english.lang, you should almost always apply the same change to all
|
||||||
|
@ -52,6 +52,13 @@ Usage langtool --options langfile
|
||||||
Change the target for the specified id from one value to another
|
Change the target for the specified id from one value to another
|
||||||
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
|
||||||
|
|
||||||
|
--inplace
|
||||||
|
|
||||||
|
Perform file operations in-place, instead of outputting the result to
|
||||||
|
stdout. With this option set, you can specify multiple langfiles for
|
||||||
|
all commands.
|
||||||
|
Example: langtool --deprecate --id LANG_ASK --inplace *.lang
|
||||||
MOO
|
MOO
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,6 +71,7 @@ my $changesource = '';
|
||||||
my $changeid = '';
|
my $changeid = '';
|
||||||
my $changetarget = '';
|
my $changetarget = '';
|
||||||
my $changedesc = '';
|
my $changedesc = '';
|
||||||
|
my $inplace = '';
|
||||||
my $help = '';
|
my $help = '';
|
||||||
# Parameters
|
# Parameters
|
||||||
my @ids = ();
|
my @ids = ();
|
||||||
|
@ -78,6 +86,7 @@ GetOptions(
|
||||||
'changetarget' => \$changetarget,
|
'changetarget' => \$changetarget,
|
||||||
'changedesc' => \$changedesc,
|
'changedesc' => \$changedesc,
|
||||||
'help' => \$help,
|
'help' => \$help,
|
||||||
|
'inplace' => \$inplace,
|
||||||
|
|
||||||
'ids=s' => \@ids,
|
'ids=s' => \@ids,
|
||||||
'from=s' => \$from,
|
'from=s' => \$from,
|
||||||
|
@ -104,6 +113,8 @@ if (
|
||||||
($deprecate and $numids < 1)
|
($deprecate and $numids < 1)
|
||||||
or # Do changesource, but either target or to not set
|
or # Do changesource, but either target or to not set
|
||||||
($changesource and ($s_target eq "" or $to eq ""))
|
($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();
|
usage();
|
||||||
exit(1);
|
exit(1);
|
||||||
|
@ -121,87 +132,101 @@ if ($changesource and not $to =~ /none|deprecated/) {
|
||||||
$to = sprintf('"%s"', $to);
|
$to = sprintf('"%s"', $to);
|
||||||
}
|
}
|
||||||
|
|
||||||
open(LANGFILE, $ARGV[0]);
|
foreach my $file (@ARGV) {
|
||||||
my $id = "";
|
print(STDERR "$file\n");
|
||||||
my $desc = "";
|
open(LANGFILE, $file) or die(sprintf("Couldn't open file for reading: %s", $file));
|
||||||
my $location = "";
|
my $id = "";
|
||||||
my $target = "";
|
my $desc = "";
|
||||||
my $string = "";
|
my $location = "";
|
||||||
my $open = 0;
|
my $target = "";
|
||||||
|
my $string = "";
|
||||||
|
my $open = 0;
|
||||||
|
my $output = "";
|
||||||
|
|
||||||
for (<LANGFILE>) {
|
for (<LANGFILE>) {
|
||||||
my $line = $_;
|
my $line = $_;
|
||||||
|
|
||||||
if ($line =~ /^\s*<(\/?)([^>]+)>\s*$/) {
|
### Set up values when a tag starts or ends ###
|
||||||
my $tag = $2;
|
if ($line =~ /^\s*<(\/?)([^>]+)>\s*$/) {
|
||||||
$open = $1 eq "/" ? 0 : 1;
|
my $tag = $2;
|
||||||
if ($open) {
|
$open = $1 eq "/" ? 0 : 1;
|
||||||
$location = $tag;
|
if ($open) {
|
||||||
($target, $string) = ("", "");
|
$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);
|
|
||||||
}
|
|
||||||
if ($key eq "id") {
|
|
||||||
$id = $val;
|
|
||||||
}
|
|
||||||
elsif ($key eq "desc") {
|
|
||||||
$desc = $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
|
if ($open and $tag eq "phrase") {
|
||||||
elsif ($location =~ /source|dest|voice/ and $target ne "") {
|
$id = "";
|
||||||
$line = "";
|
}
|
||||||
|
if (not $open) {
|
||||||
|
$location = "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
print($line);
|
### Set up values when a key: value pair is found ###
|
||||||
}
|
elsif ($line =~ /^\s*([^:]*?)\s*:\s*(.*?)\s*$/) {
|
||||||
elsif ($changetarget) {
|
my ($key, $val) = ($1, $2);
|
||||||
# Change target if set and it's the same as $from
|
if ($location =~ /source|dest|voice/) {
|
||||||
if ($id ne "" and grep(/$id/, @ids) and $location =~ /source|dest|voice/ and $target eq $from) {
|
($target, $string) = ($key, $val);
|
||||||
$line =~ s/$from/$to/;
|
}
|
||||||
|
if ($key eq "id") {
|
||||||
|
$id = $val;
|
||||||
|
}
|
||||||
|
elsif ($key eq "desc") {
|
||||||
|
$desc = $val;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
print($line);
|
|
||||||
}
|
if ($deprecate) {
|
||||||
elsif ($changesource) {
|
if ($id ne "" and grep(/$id/, @ids)) {
|
||||||
# Change string if $target is set and matches $s_target
|
# Set desc
|
||||||
if ($id ne "" and grep(/$id/, @ids) and $target eq $s_target and $location eq "source") {
|
$line =~ s/\s*desc:.*/ desc: deprecated/;
|
||||||
$line =~ s/$string/$to/;
|
# 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 = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
print($line);
|
elsif ($changetarget) {
|
||||||
}
|
# Change target if set and it's the same as $from
|
||||||
elsif ($changedesc) {
|
if ($id ne "" and grep(/$id/, @ids) and $location =~ /source|dest|voice/ and $target eq $from) {
|
||||||
# Simply change the desc line if the id matches
|
$line =~ s/$from/$to/;
|
||||||
if ($id ne "" and grep(/$id/, @ids)) {
|
}
|
||||||
$line =~ s/\s*desc:.*/ desc: $to/;
|
}
|
||||||
|
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/;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
elsif ($changedesc) {
|
||||||
|
# Simply change the desc line if the id matches
|
||||||
|
if ($id ne "" and grep(/$id/, @ids)) {
|
||||||
|
$line =~ s/\s*desc:.*/ desc: $to/;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
elsif ($changeid) {
|
||||||
|
$line =~ s/^\s*id:\s*$from.*$/ id: $to/;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
print("This should never happen.\n");
|
||||||
|
exit(3);
|
||||||
|
}
|
||||||
|
if ($inplace) {
|
||||||
|
$output .= $line;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
print($line);
|
||||||
}
|
}
|
||||||
print($line);
|
|
||||||
}
|
}
|
||||||
elsif ($changeid) {
|
close(LANGFILE);
|
||||||
$line =~ s/^\s*id:\s*$from.*$/ id: $to/;
|
if ($inplace) {
|
||||||
print($line);
|
open(LANGFILE, ">", $file) or die(sprintf("Couldn't open file for writing: %s\n", $file));
|
||||||
}
|
print(LANGFILE $output);
|
||||||
else {
|
close(LANGFILE);
|
||||||
print("wut wut\n");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue