summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuilhem Moulin <guilhem.moulin@ens-lyon.org>2012-03-02 09:23:22 +0100
committerGuilhem Moulin <guilhem.moulin@ens-lyon.org>2012-03-02 09:23:30 +0100
commit8aac36c6e2d16c7122eb1b18febba4b5b9374323 (patch)
treeee3fa75c6b19fe05f8f9ae1e04211a180eec5603
parenta18fb4c2f3638d60cb16e498612db490430a5c0e (diff)
strings from DBI are lacking the UTF-8 flag
-rwxr-xr-xvideoadd.pl18
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