transliterate

Transliteration engine
git clone git://lumidify.org/transliterate.git
Log | Files | Refs | README | LICENSE

commit 3f5ea2bc94c0f4e1786fb7029db9dd2dbcc23473
parent a5d4813bdbc63d04449cbc2032a4db845876caf4
Author: lumidify <nobody@lumidify.org>
Date:   Wed,  8 Apr 2020 10:40:58 +0200

Fix opening of relative paths

Diffstat:
Mtransliterate.pl | 33++++++++++++---------------------
1 file changed, 12 insertions(+), 21 deletions(-)

diff --git a/transliterate.pl b/transliterate.pl @@ -241,9 +241,8 @@ sub prompt_unknown_word { $window->show_all; Gtk2->main; - if ($stop) { - die "Processing stopped at line $cur_lineno"; - } + die "Processing stopped at line $cur_lineno\n" if $stop; + if (!$action) { # This action isn't explicitly handled, but at least nothing # breaks when the window is closed without selecting an action @@ -451,7 +450,7 @@ sub prompt_choose_word { $window->show_all; $accept->hide; Gtk2->main; - die "Processing stopped at line $cur_lineno" if $stop; + die "Processing stopped at line $cur_lineno\n" if $stop; } my $ID = 0; @@ -520,13 +519,14 @@ sub parse_config { # if the path is relative, find its absolute location based # on the location of the config file sub open_file_rel_abs { - my ($filename, $config_file) = @_; + my ($filename, $config_file, $mode) = @_; + $mode //= "<"; if (!file_name_is_absolute $filename) { my $config_dir = dirname $config_file; $filename = rel2abs($filename, $config_dir); } my $fh; - if (!open $fh, "<", $filename) { + if (!open $fh, $mode, $filename) { warn "Can't open file \"$filename\"!\n"; return; } @@ -911,21 +911,15 @@ sub handle_unknown_word_action { return 3 if $action->[1] eq "wholeline"; $config->{"ignore_words"}->{$action->[2]} = ""; if ($action->[1] eq "permanent") { - my $fh; - if (!open($fh, ">>", $config->{"ignore"})) { - warn "ERROR: Can't open ignore file for appending.\n"; - return 1; - } + my $fh = open_file_rel_abs $config->{"ignore"}, $args->{"config"}, ">>"; + return 1 if !$fh; print($fh $action->[2] . "\n"); close($fh); } elsif ($action->[1] eq "run") { # Print to error file if ignore isn't permanent return 0 if ($args->{"errors"} eq ""); - my $fh; - if (!open($fh, ">>", $args->{"errors"})) { - warn "ERROR: Can't open error file \"$args->{errors}\".\n"; - return 0; - } + my $fh = open_file_rel_abs $args->{"errors"}, $args->{"config"}, ">>"; + return 0 if !$fh; print($fh $action->[2] . "\n"); close($fh); } @@ -939,11 +933,8 @@ sub handle_unknown_word_action { my $reverted = exists $config->{"reverted_tables"}->{$table_path}; my $word_abs = $reverted ? $action->[2] : $action->[1]; my $replace_word_abs = $reverted ? $action->[1] : $action->[2]; - my $fh; - if (!open($fh, ">>", $table_path)) { - warn "ERROR: Can't open table file \"$table_path\" for appending.\n"; - return 1; - } + my $fh = open_file_rel_abs $table_path, $args->{"config"}, ">>"; + return 1 if !$fh; print($fh $word_abs . $config->{tablesep} . $replace_word_abs . "\n"); close($fh); # loop over all table ids that are impacted by this file