| 1 | ##---------------------------------------------------------------------------## | ||
| 2 | ## File: | ||
| 3 | ## $Id: Encode.pm,v 1.2 2002/12/20 08:01:11 ehood Exp $ | ||
| 4 | ## Author: | ||
| 5 | ## Earl Hood earl@earlhood.com | ||
| 6 | ## Description: | ||
| 7 | ## POD after __END__. | ||
| 8 | ##---------------------------------------------------------------------------## | ||
| 9 | ## Copyright (C) 2002 Earl Hood, earl@earlhood.com | ||
| 10 | ## | ||
| 11 | ## This program is free software; you can redistribute it and/or modify | ||
| 12 | ## it under the terms of the GNU General Public License as published by | ||
| 13 | ## the Free Software Foundation; either version 2 of the License, or | ||
| 14 | ## (at your option) any later version. | ||
| 15 | ## | ||
| 16 | ## This program is distributed in the hope that it will be useful, | ||
| 17 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 18 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 19 | ## GNU General Public License for more details. | ||
| 20 | ## | ||
| 21 | ## You should have received a copy of the GNU General Public License | ||
| 22 | ## along with this program; if not, write to the Free Software | ||
| 23 | ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | ||
| 24 | ## 02111-1307, USA | ||
| 25 | ##---------------------------------------------------------------------------## | ||
| 26 | |||
| 27 | package MHonArc::Encode; | ||
| 28 | |||
| 29 | use strict; | ||
| 30 | |||
| 31 | our $VERSION = '2.6.24'; | ||
| 32 | |||
| 33 | BEGIN { | ||
| 34 | # If the Encode module is available, we use it, otherwise, we | ||
| 35 | # try to use Unicode::MapUTF8. | ||
| 36 | eval { require Encode; }; | ||
| 37 | if (!$@) { | ||
| 38 | *from_to = \&_encode_from_to; | ||
| 39 | } else { | ||
| 40 | require Unicode::MapUTF8; | ||
| 41 | *from_to = \&_unimap_from_to; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | ##---------------------------------------------------------------------------## | ||
| 46 | |||
| 47 | sub _encode_from_to { | ||
| 48 | my $text_r = shift; | ||
| 49 | my $from_enc = lc shift; | ||
| 50 | my $to_enc = lc shift; | ||
| 51 | |||
| 52 | return '' if $$text_r eq ''; | ||
| 53 | |||
| 54 | # Strip utf8 string flag if set | ||
| 55 | if (Encode::is_utf8($$text_r)) { | ||
| 56 | $$text_r = Encode::encode('utf8', $$text_r); | ||
| 57 | } | ||
| 58 | my $is_error = 0; | ||
| 59 | eval { | ||
| 60 | if (!defined(Encode::from_to($$text_r, $from_enc, $to_enc))) { | ||
| 61 | warn qq/Warning: MHonArc::Encode: Unable to convert /, | ||
| 62 | qq/"$from_enc" to "$to_enc"\n/; | ||
| 63 | $is_error = 1; | ||
| 64 | } | ||
| 65 | }; | ||
| 66 | if ($@) { | ||
| 67 | warn qq/Warning: $@\n/; | ||
| 68 | $is_error = 1; | ||
| 69 | } | ||
| 70 | $is_error ? undef : $to_enc; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub _unimap_from_to { | ||
| 74 | my $text_r = shift; | ||
| 75 | my $from_enc = lc shift; | ||
| 76 | my $to_enc = lc shift; | ||
| 77 | |||
| 78 | if (!Unicode::MapUTF8::utf8_supported_charset($from_enc)) { | ||
| 79 | warn qq/Warning: MHonArc::Encode "$from_enc" not supported\n/; | ||
| 80 | return undef; | ||
| 81 | } | ||
| 82 | if (!Unicode::MapUTF8::utf8_supported_charset($to_enc)) { | ||
| 83 | warn qq/Warning: MHonArc::Encode "$to_enc" not supported\n/; | ||
| 84 | return undef; | ||
| 85 | } | ||
| 86 | $$text_r = Unicode::MapUTF8::to_utf8( | ||
| 87 | {-string => $$text_r, -charset => $from_enc}); | ||
| 88 | $$text_r = Unicode::MapUTF8::from_utf8( | ||
| 89 | {-string => $$text_r, -charset => $to_enc}); | ||
| 90 | $to_enc; | ||
| 91 | } | ||
| 92 | |||
| 93 | ##---------------------------------------------------------------------------## | ||
| 94 | 1; | ||
| 95 | __END__ |