| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
Debconf::Encoding - Character encoding support for debconf |
| 6 |
|
| 7 |
=head1 DESCRIPTION |
| 8 |
|
| 9 |
This module provides facilities to convert between character encodings for |
| 10 |
debconf, as well as other functions to operate on characters. |
| 11 |
|
| 12 |
Debconf uses glibc's character encoding converter via Text::Iconv instead |
| 13 |
of perl's internal Encoding conversion library because I'm not really sure |
| 14 |
if perls encoding is 100% the same. There could be round-trip errors |
| 15 |
between iconv's encodings and perl's, conceivably. |
| 16 |
|
| 17 |
$Debconf::Encoding::charmap holds the user's charmap. |
| 18 |
|
| 19 |
Debconf::Encoding::convert() takes a charmap and a string encoded in that |
| 20 |
charmap, and converts it to the user's charmap. |
| 21 |
|
| 22 |
Debconf::Encoding::wrap is a word-wrapping function, with the same interface |
| 23 |
as the one in Text::Wrap (except it doesn't gratuitously unexpand tabs). |
| 24 |
If Text::WrapI18N is available, it will be used for proper wrapping of |
| 25 |
multibyte encodings, combining and fullwidth characters, and languages that |
| 26 |
do not use whitespace between words. |
| 27 |
|
| 28 |
$Debconf::Encoding::columns is used to set the number of columns text is |
| 29 |
wrapped to by Debconf::Encoding::wrap |
| 30 |
|
| 31 |
Debconf::Encoding::width returns the number of columns required to display |
| 32 |
the given string. If available, Text::CharWidth is used to determine the |
| 33 |
width, to support combining and fullwidth characters. |
| 34 |
|
| 35 |
Any of the above can be exported, this module uses the exporter. |
| 36 |
|
| 37 |
=cut |
| 38 |
|
| 39 |
package Debconf::Encoding; |
| 40 |
|
| 41 |
use strict; |
| 42 |
use warnings; |
| 43 |
|
| 44 |
our $charmap; |
| 45 |
BEGIN { |
| 46 |
no warnings; |
| 47 |
eval q{ use Text::Iconv }; |
| 48 |
use warnings; |
| 49 |
if (! $@) { |
| 50 |
# I18N::Langinfo is not even in Debian as I write this, so |
| 51 |
# I will use something that is to get the charmap. |
| 52 |
$charmap = `locale charmap`; |
| 53 |
chomp $charmap; |
| 54 |
} |
| 55 |
|
| 56 |
no warnings; |
| 57 |
## no critic (BuiltinFunctions::ProhibitStringyEval) |
| 58 |
eval q{ use Text::WrapI18N; use Text::CharWidth }; |
| 59 |
## use critic |
| 60 |
use warnings; |
| 61 |
# mblen has been known to get busted and return large numbers when |
| 62 |
# the wrong version of perl is installed. Avoid an infinite loop |
| 63 |
# in Text::WrapI18n in this case. |
| 64 |
if (! $@ && Text::CharWidth::mblen("a") == 1) { |
| 65 |
# Set up wrap and width functions to point to functions |
| 66 |
# from the modules. |
| 67 |
*wrap = *Text::WrapI18N::wrap; |
| 68 |
*columns = *Text::WrapI18N::columns; |
| 69 |
*width = *Text::CharWidth::mbswidth; |
| 70 |
} |
| 71 |
else { |
| 72 |
# Use Text::Wrap for wrapping, but unexpand tabs. |
| 73 |
require Text::Wrap; |
| 74 |
require Text::Tabs; |
| 75 |
sub _wrap { return Text::Tabs::expand(Text::Wrap::wrap(@_)) } |
| 76 |
*wrap = *_wrap; |
| 77 |
*columns = *Text::Wrap::columns; |
| 78 |
# Cannot just use *CORE::length; perl is too dumb. |
| 79 |
sub _dumbwidth { length shift } |
| 80 |
*width = *_dumbwidth; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
use base qw(Exporter); |
| 85 |
our @EXPORT_OK=qw(wrap $columns width convert $charmap to_Unicode); |
| 86 |
|
| 87 |
my $converter; |
| 88 |
my $old_input_charmap; |
| 89 |
sub convert { |
| 90 |
my $input_charmap = shift; |
| 91 |
my $string = shift; |
| 92 |
|
| 93 |
return unless defined $charmap; |
| 94 |
|
| 95 |
# The converter object is cached. |
| 96 |
if (! defined $old_input_charmap || |
| 97 |
$input_charmap ne $old_input_charmap) { |
| 98 |
$converter = Text::Iconv->new($input_charmap, $charmap); |
| 99 |
$old_input_charmap = $input_charmap; |
| 100 |
} |
| 101 |
return $converter->convert($string); |
| 102 |
} |
| 103 |
|
| 104 |
my $unicode_conv; |
| 105 |
sub to_Unicode { |
| 106 |
my $string = shift; |
| 107 |
my $result; |
| 108 |
|
| 109 |
return $string if utf8::is_utf8($string); |
| 110 |
if (!defined $unicode_conv) { |
| 111 |
$unicode_conv = Text::Iconv->new($charmap, "UTF-8"); |
| 112 |
} |
| 113 |
$result = $unicode_conv->convert($string); |
| 114 |
utf8::decode($result); |
| 115 |
return $result; |
| 116 |
} |
| 117 |
|
| 118 |
=head1 AUTHOR |
| 119 |
|
| 120 |
Joey Hess <[email protected]> |
| 121 |
|
| 122 |
=cut |
| 123 |
|
| 124 |
1 |