diff options
-rwxr-xr-x | videoadd.pl | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/videoadd.pl b/videoadd.pl index 765ed34..1c8b0db 100755 --- a/videoadd.pl +++ b/videoadd.pl @@ -641,7 +641,7 @@ sub mkcast { # Try to find a matching entry in the database. sub getfile { - my $crit = $_[0]; + my $crit = &iconv($_[0]); return $crit if defined $ignoredb_flag; my $SELECT = "SELECT id, imdbid, filename, title, year FROM $config{prefix}videodata @@ -672,7 +672,7 @@ sub getfile { my @options; foreach my $id (keys $res) { printf "%04d - ", $res->{$id}->{id} or die "Can't printf: $!"; - print &iconv($res->{$id}->{title}. " (" .$res->{$id}->{year}. ")\n") + print &iconv($res->{$id}->{title}. " (" .$res->{$id}->{year}. ")\n", 'utf8') or die "Can't print: $!"; push @options, sprintf ("%04d", $res->{$id}->{id}); } @@ -798,12 +798,22 @@ sub YNquestion { } # Convert to UTF-8 +# (Strings from the database are in UTF-8, but are lacking the UTF-8 flag) sub iconv { - my $string = $_[0]; + my ($string, $enc) = @_; return unless defined $string; unless (Encode::is_utf8($string)) { - $string = Encode::decode( 'latin1', my $copy = $string, 1 ); + my @encodings = qw/latin1 utf8/; + @encodings = $enc if defined $enc; + + my $decoded; + foreach my $enc (@encodings) { + $decoded = Encode::decode( $enc, $string); + next if $@ or not $decoded; + last if utf8::valid($decoded); + } + $string = $decoded; } # The UTF-8 flag should be on now |