blib/lib/Uplug/Encoding.pm untested

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 119 CC 13 SCAR 35.6
CC
13
Cov
0%
convert
72.0
encode
12.0
encodeArray
12.0
CRAP 35.1
1 #-*-perl-*-
2 #####################################################################
3 # Copyright (C) 2004 Jörg Tiedemann <joerg@stp.ling.uu.se>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #
19 # $Author$
20 # $Id$
21  
22  
23 package Uplug::Encoding;
24  
25 # use lib '/home/staff/joerg/user_local/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi/';
26 # use Unicode::String;
27  
28 use strict;
29 use vars qw( @ISA @EXPORT @DefinedEncodings %EncodingSubs %EncodingAlias);
30 use vars qw( $DEFAULTENCODING );
31  
32 @ISA = qw( Exporter);
33 @EXPORT = qw( &convert &encodeArray );
34  
35 $DEFAULTENCODING='utf-8';
36  
37 @DefinedEncodings = ('ucs4','utf16','utf8','utf7','hex','iso-8859-1');
38 %EncodingAlias = ('utf-8' => 'utf8',
39 'latin1' => 'iso-8859-1',
40 'latin-1' => 'iso-8859-1',
41 'iso8859-1' => 'iso-8859-1',
42 'iso88591' => 'iso-8859-1'
43 );
44  
45  
46  
47 my $PerlVersion=$];
48 if ($PerlVersion<5.008){
49 # my $mod=qw( Unicode::String );
50 my $mod='Unicode/String.pm';
51 eval "require('$mod')";
52 if ($@){warn $@;exit;}
53 }
54 else{
55 my $mod=qw( Encode );
56 eval "require $mod";
57 if ($@){warn $@;exit;}
58 @DefinedEncodings=Encode->encodings(":all");
59 foreach (@DefinedEncodings){
60 $EncodingAlias{lc($_)}=$_;
61 }
62 }
63  
64  
65 %EncodingSubs = ('ucs4' => 'ucs4', # this is for perl < 5.8
66 'utf16' => 'utf16',
67 'utf8' => 'utf8',
68 'utf7' => 'utf7',
69 'hex' => 'hex',
70 'iso-8859-1' => 'latin1',
71 );
72  
73 1;
74  
75 sub convert{
76 my $data=shift;
77 my $from=shift;
78 my $to=shift;
79  
80 #---------
81 # perl >= 5.8 takes care of character encodings when opening files
82 # (using PerlIO ...)
83 if ($PerlVersion>=5.008){return $data}
84  
85 $from=~tr/A-Z/a-z/;
86 $to=~tr/A-Z/a-z/;
87 if (defined $EncodingAlias{$from}){$from=$EncodingAlias{$from};}
88 if (defined $EncodingAlias{$to}){$to=$EncodingAlias{$to};}
89 if ($from eq $to){return $data;}
90  
91 if (not grep (/^$from$/,@DefinedEncodings)){
92 # warn "# Uplug::Encoding: character encoding $from is not defined!";
93 return $data;
94 }
95 if (not grep (/^$to$/,@DefinedEncodings)){
96 # warn "# Uplug::Encoding: character encoding $to is not defined!";
97 return $data;
98 }
99  
100 # if ($PerlVersion<5.008){
101 my $DecodingSub=$EncodingSubs{$from};
102 my $EncodingSub=$EncodingSubs{$to};
103 my $u;
104 eval "\$u=Unicode::String::$DecodingSub(\$data);";
105 eval "\$data=\$u->$EncodingSub;";
106 if ($@){warn "# Uplug::Encoding: conversion failed!";}
107 return $data;
108 # }
109 # else{
110 ## Encode::from_to($data,$from,$to);
111 ## Encode::decode($from,$data,$from);
112 ## Encode::decode($to,$data);
113 # return Encode::encode($to,$data);
114 ## return $data;
115 # }
116 # return $data;
117 }
118  
119 #sub convertString{
120 # my $string=shift;
121 # my $from=shift;
122 # my $to=shift;
123 # if ($PerlVersion<5.008){return &convert($string,$from,$to);}
124 # return &decode($string,$to);
125 #}
126  
127 sub encode{
128 my $data=shift;
129 my $from=shift;
130 my $to=shift;
131 if ($from eq $to){return $data;}
132 if ($PerlVersion<5.008){
133 return &convert($data,$from,$to);
134 }
135 return Encode::encode($from,$data);
136 }
137  
138  
139  
140 sub decode{
141 my $data=shift;
142 my $to=shift;
143 my $from=shift;
144 if ($PerlVersion<5.008){
145 return &convert($data,$from,$to);
146 }
147 return Encode::decode($to,$data);
148 }
149  
150 ######################################################################
151  
152 sub encodeArray{
153 my ($arr,$oldCode,$newCode)=@_;
154 if (ref($arr) eq 'HASH'){
155 foreach (keys %{$arr}){
156 $arr->{$_}=&encode($arr->{$_},$oldCode,$newCode);
157 }
158 }
159 elsif (ref($arr) eq 'ARRAY'){
160 foreach (0..$#{$arr}){
161 $arr->[$_]=&encode($arr->[$_],$oldCode,$newCode);
162 }
163 }
164 }
165  
166 1;
167