/[cvs]/nfo/perl/libs/Data/Mungle/Transform/Encode.pm
ViewVC logotype

Annotation of /nfo/perl/libs/Data/Mungle/Transform/Encode.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (hide annotations)
Tue May 13 07:42:59 2003 UTC (21 years, 1 month ago) by joko
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +15 -1 lines
+ sub decode_hex_nybbles: e.g. required to decode binary data from cellular phones

1 joko 1.3 ## -------------------------------------------------------------------------
2 joko 1.6 ## $Id: Encode.pm,v 1.5 2003/04/09 07:22:34 joko Exp $
3 joko 1.3 ## -------------------------------------------------------------------------
4     ## $Log: Encode.pm,v $
5 joko 1.6 ## Revision 1.5 2003/04/09 07:22:34 joko
6     ## childObj2string now inside Encode.pm, renamed to 'twingle_reference'
7     ##
8 joko 1.5 ## Revision 1.4 2003/03/27 15:17:07 joko
9     ## namespace fixes for Data::Mungle::*
10     ##
11 joko 1.4 ## Revision 1.3 2003/02/20 20:48:36 joko
12     ## renamed methods
13     ##
14 joko 1.3 ## Revision 1.2 2002/12/05 13:57:22 joko
15     ## + now able to export 'scalar2utf8'
16     ## + comments
17     ##
18     ## Revision 1.1 2002/11/29 04:49:20 joko
19     ## + initial check-in
20     ##
21     ## Revision 1.1 2002/10/10 03:26:00 cvsjoko
22     ## + new
23     ## -------------------------------------------------------------------------
24 joko 1.1
25    
26 joko 1.4 package Data::Mungle::Transform::Encode;
27 joko 1.1
28     use strict;
29     use warnings;
30    
31     require Exporter;
32     our @ISA = qw( Exporter );
33 joko 1.5 our @EXPORT_OK = qw(
34     &latin_to_utf8
35     &latin_to_utf8_scalar
36     &utf8_to_latin
37     &utf8_to_latin_scalar
38     &twingle_reference
39 joko 1.6 &decode_hex_nybbles
40 joko 1.5 );
41 joko 1.3
42 joko 1.1
43     use Unicode::String qw(utf8 latin1);
44    
45 joko 1.2 # TODO: refactor using Greg London's "Iterate" from CPAN
46 joko 1.3 sub latin_to_utf8 {
47 joko 1.1 my $vref = shift;
48     if (ref $vref eq 'HASH') {
49     foreach (keys %{$vref}) {
50     if ((ref $vref->{$_}) =~ m/ARRAY|HASH/) {
51 joko 1.3 latin_to_utf8($vref->{$_});
52 joko 1.1 } else {
53 joko 1.3 $vref->{$_} = latin_to_utf8_scalar($vref->{$_});
54 joko 1.1 }
55     }
56     } elsif (ref $vref eq 'ARRAY') {
57     foreach (@{$vref}) {
58     if (ref $_ ne 'SCALAR') {
59 joko 1.3 latin_to_utf8($_);
60 joko 1.1 } else {
61 joko 1.3 $_ = latin_to_utf8_scalar($_);
62 joko 1.1 }
63     }
64     }
65    
66     }
67    
68 joko 1.3 sub latin_to_utf8_scalar {
69 joko 1.1 my $scalar = shift;
70     if ($scalar) {
71     my $u = latin1($scalar);
72     return $u->utf8;
73     }
74     }
75    
76 joko 1.2 # TODO: refactor using Greg London's "Iterate" from CPAN
77 joko 1.3 sub utf8_to_latin {
78 joko 1.1 my $vref = shift;
79     if (ref $vref eq 'HASH') {
80     foreach (keys %{$vref}) {
81     if ((ref $vref->{$_}) =~ m/ARRAY|HASH/) {
82 joko 1.3 utf8_to_latin($vref->{$_});
83 joko 1.1 } else {
84 joko 1.3 $vref->{$_} = utf8_to_latin_scalar($vref->{$_});
85 joko 1.1 }
86     }
87     } elsif (ref $vref eq 'ARRAY') {
88     foreach (@{$vref}) {
89     if (ref $_ ne 'SCALAR') {
90 joko 1.3 utf8_to_latin($_);
91 joko 1.1 } else {
92 joko 1.3 $_ = utf8_to_latin_scalar($_);
93 joko 1.1 }
94     }
95     }
96    
97     }
98    
99 joko 1.3 sub utf8_to_latin_scalar {
100 joko 1.1 my $scalar = shift;
101     if ($scalar) {
102     my $u = utf8($scalar);
103     return $u->latin1;
104     }
105     }
106 joko 1.5
107    
108     # encodes object-references to serialized string representations
109     # made up of:
110     # - 'o_<classname>_<ref type>_<guid>'???
111     # - 'o_{guid}_{classname}'!!!
112    
113     # TODO: enhance further!
114     # make it possible to twingle OID-, GUID- and/or other references
115    
116     sub twingle_reference {
117     my $obj = shift;
118     my $option = shift;
119     my $string = 'n/a';
120    
121     #if ($option == 1) {
122     if ((my $classname = ref $obj) && (my $guid = $obj->{guid})) {
123     $string = join('_', 'o', $guid, $classname);
124     }
125     #}
126    
127     return $string;
128 joko 1.6 }
129    
130     sub decode_hex_nybbles {
131     my $data = shift;
132     my @buf;
133     for (my $i = 0; $i <= length($data); $i = $i + 2) {
134     my $nybble = substr($data, $i, 2);
135     push @buf, chr(hex($nybble));
136     }
137     return join('', @buf);
138 joko 1.5 }
139    
140 joko 1.1
141     1;
142 joko 1.3 __END__

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed