HDB/Encode.pm

filter: Statementstmt 5.0% 9 / 180 Branchbran 0.0% 0 / 106 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 13.3% 2 / 15 Podpod 92.3% 12 / 13 totaltotal 7.3% 23 / 314 CC 58 SCAR 40.6
CC
58
Cov
3%
Packed_SIZE_HASH
90.0
Packed_SIZE_ARRAY
90.0
Pack_HASH
72.0
CRAP 58.0
« HDB.pm ↑ Summary HDB/CORE.pm »
1 #############################################################################
2 ## Name: Encode.pm
3 ## Purpose: HDB::Encode - Common things for HDB modules.
4 ## Author: Graciliano M. P.
5 ## Modified by:
6 ## Created: 15/01/2003
7 ## RCS-ID:
8 ## Copyright: (c) 2002 Graciliano M. P.
9 ## Licence: This program is free software; you can redistribute it and/or
10 ## modify it under the same terms as Perl itself
11 #############################################################################
12  
13 package HDB::Encode ;
14  
15 1 ▶use strict qw(vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶no warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 our $VERSION = '1.0' ;
19  
20 ########
21 # VARS #
22 ########
23  
24 1 my %VER = (
25 PACKED_HASH => '1.0' ,
26 PACKED_ARRAY => '1.0' ,
27 ) ;
28
29 ########
30 # PACK #
31 ########
32  
33 0 ▶sub Pack {
Subroutine Pack: not called CC=3 SCAR=24.8
Pod documented
34 0 ▶ if ( ref($_[0]) eq 'HASH' ) { return &Pack_HASH($_[0]) ;}
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH") 0 0 0
35 0 ▶ if ( ref($_[0]) eq 'ARRAY' ) { return &Pack_ARRAY($_[0]) ;}
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "ARRAY") 0 0 0
36 }
37  
38 #############
39 # PACK_HASH #
40 #############
41  
42 0 ▶sub Pack_HASH {
Subroutine Pack_HASH: not called CC=8 SCAR=42.8
Pod documented
43 0 my ( $hash ) = @_ ;
44
45 0 ▶ if (ref($hash) ne 'HASH') { return( undef ) ;}
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash ne "HASH") 0 0 0
46
47 0 my ($pack_init,$pack) ;
48
49 0 ▶ $pack_init = "%HDB_PACKED_HASH%[$VER{PACKED_HASH}]{0}:" if !$_[1] ;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 0 0 0
50
51 0 foreach my $key ( keys %$hash ) {
52 0 my ($blk,$tp,$value) ;
53 0 ▶ if ( ref( $$hash{$key} ) eq 'HASH' ) { $tp = 1 ; $value = &Pack_HASH( $$hash{$key} , 1 ) }
Branch: 3 branches
BranchTrueFalseTotal
if (ref $hash->{$key} eq 'HASH') { } 0 0 0
elsif (ref $hash->{$key} eq 'ARRAY') { } 0 0 0
elsif (UNIVERSAL::isa($hash->{$key}, 'UNIVERSAL')) { } 0 0 0
54 0 elsif ( ref( $$hash{$key} ) eq 'ARRAY' ) { $tp = 2 ; $value = &Pack_ARRAY( $$hash{$key} , 1 ) ;}
55 0 elsif ( UNIVERSAL::isa($$hash{$key} ,'UNIVERSAL') ) { next ;} ## ignore objects.
56 0 else { $tp = 0 ; $value = $$hash{$key} ;}
57
58 0 $blk .= $tp ;
59 0 $blk .= length($key) . ":" ;
60 0 $blk .= $key ;
61 0 $blk .= length($value) . ":" ;
62 0 $blk .= $value ;
63
64 0 $pack .= $blk ;
65 }
66
67 0 ▶ if ( !$_[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[1]) 0 0 0
68 0 my $sz = length($pack) ;
69 0 $pack_init =~ s/\{0}/\{$sz}/s ;
70 }
71
72 0 return( $pack_init . $pack ) ;
73 }
74  
75 ##############
76 # PACK_ARRAY #
77 ##############
78  
79 0 ▶sub Pack_ARRAY {
Subroutine Pack_ARRAY: not called CC=8 SCAR=42.8
Pod documented
80 0 my ( $array ) = @_ ;
81
82 0 ▶ if (ref($array) ne 'ARRAY') { return( undef ) ;}
Branch: 1 branch
BranchTrueFalseTotal
if (ref $array ne "ARRAY") 0 0 0
83
84 0 my ($pack_init,$pack) ;
85
86 0 ▶ $pack_init = "%HDB_PACKED_ARRAY%[$VER{PACKED_ARRAY}]{0}:" if !$_[1] ;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 0 0 0
87
88 0 foreach my $array_i ( @$array ) {
89 0 my ($blk,$tp,$value) ;
90 0 ▶ if ( ref( $array_i ) eq 'HASH' ) { $tp = 1 ; $value = &Pack_HASH( $array_i , 1 ) ;}
Branch: 3 branches
BranchTrueFalseTotal
if (ref $array_i eq 'HASH') { } 0 0 0
elsif (ref $array_i eq 'ARRAY') { } 0 0 0
elsif (UNIVERSAL::isa($array_i, 'UNIVERSAL')) { } 0 0 0
91 0 elsif ( ref( $array_i ) eq 'ARRAY' ) { $tp = 2 ; $value = &Pack_ARRAY( $array_i , 1 ) ;}
92 0 elsif ( UNIVERSAL::isa($array_i ,'UNIVERSAL') ) { next ;} ## ignore objects.
93 0 else { $tp = 0 ; $value = $array_i ;}
94
95 0 $blk .= $tp ;
96 0 $blk .= length($value) . ":" ;
97 0 $blk .= $value ;
98
99 0 $pack .= $blk ;
100 }
101
102 0 ▶ if ( !$_[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[1]) 0 0 0
103 0 my $sz = length($pack) ;
104 0 $pack_init =~ s/\{0}/\{$sz}/s ;
105 }
106
107 0 return( $pack_init . $pack ) ;
108 }
109  
110 ##########
111 # UNPACK #
112 ##########
113  
114 0 ▶sub UnPack {
Subroutine UnPack: not called CC=3 SCAR=24.8
Pod documented
115 0 ▶ if ( &Is_Packed_HASH($_[0]) ) { return &UnPack_HASH($_[0]) ;}
Branch: 1 branch
BranchTrueFalseTotal
if (&Is_Packed_HASH($_[0])) 0 0 0
116 0 ▶ if ( &Is_Packed_ARRAY($_[0]) ) { return &UnPack_ARRAY($_[0]) ;}
Branch: 1 branch
BranchTrueFalseTotal
if (&Is_Packed_ARRAY($_[0])) 0 0 0
117 0 return( $_[0] ) ;
118 }
119  
120 ###############
121 # UNPACK_HASH #
122 ###############
123  
124 0 ▶sub UnPack_HASH {
Subroutine UnPack_HASH: not called CC=8 SCAR=42.8
Pod documented
125 0 my %hash ;
126
127 0 my $pos = 0 ;
128
129 0 ▶ if ( !$_[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[1]) 0 0 0
130 0 ▶ if ( !&Is_Packed_HASH($_[0]) ) { return() ;}
Branch: 2 branches
BranchTrueFalseTotal
if (not &Is_Packed_HASH($_[0])) { } 0 0 0
elsif (not &Check_Pack_Size($_[0])) { } 0 0 0
131 0 elsif ( !&Check_Pack_Size($_[0]) ) { return("SIZE_ERROR: $_[0]") ;}
132 0 else { $pos = index($_[0],':') + 1 ;}
133 }
134
135 0 my $lng = length($_[0]) ;
136
137 0 ▶ while( $pos < $lng ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $lng) 0 0 0
138 0 my $tp = substr($_[0],$pos++,1) ;
139 0 my $key = &blk_read($_[0],$pos) ;
140 0 my $val = &blk_read($_[0],$pos) ;
141
142 0 ▶ if ($tp == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tp == 1) { } 0 0 0
elsif ($tp == 2) { } 0 0 0
143 0 my %val = &UnPack_HASH($val,1) ;
144 0 $val = \%val ;
145 }
146 elsif ($tp == 2) {
147 0 my @val = &UnPack_ARRAY($val,1) ;
148 0 $val = \@val ;
149 }
150
151 0 $hash{$key} = $val ;
152 }
153  
154 0 ▶ if ( wantarray ) { return( %hash ) ;}
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
155 0 else { return( \%hash ) ;}
156 }
157  
158 ################
159 # UNPACK_ARRAY #
160 ################
161  
162 0 ▶sub UnPack_ARRAY {
Subroutine UnPack_ARRAY: not called CC=8 SCAR=42.8
Pod documented
163 0 my @array ;
164
165 0 my $pos = 0 ;
166
167 0 ▶ if ( !$_[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[1]) 0 0 0
168 0 ▶ if ( !&Is_Packed_ARRAY($_[0]) ) { return() ;}
Branch: 2 branches
BranchTrueFalseTotal
if (not &Is_Packed_ARRAY($_[0])) { } 0 0 0
elsif (not &Check_Pack_Size($_[0])) { } 0 0 0
169 0 elsif ( !&Check_Pack_Size($_[0]) ) { return("SIZE_ERROR: $_[0]") ;}
170 0 else { $pos = index($_[0],':') + 1 ;}
171 }
172
173 0 my $lng = length($_[0]) ;
174
175 0 ▶ while( $pos < $lng ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $lng) 0 0 0
176 0 my $tp = substr($_[0],$pos++,1) ;
177 0 my $val = &blk_read($_[0],$pos) ;
178
179 0 ▶ if ($tp == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tp == 1) { } 0 0 0
elsif ($tp == 2) { } 0 0 0
180 0 my %val = &UnPack_HASH($val,1) ;
181 0 $val = \%val ;
182 }
183 elsif ($tp == 2) {
184 0 my @val = &UnPack_ARRAY($val,1) ;
185 0 $val = \@val ;
186 }
187
188 0 push(@array , $val) ;
189 }
190
191 0 ▶ if ( wantarray ) { return( @array ) ;}
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
192 0 else { return( \@array ) ;}
193 }
194  
195 ############
196 # BLK_READ #
197 ############
198  
199 0 ▶sub blk_read {
Subroutine blk_read: not called CC=4 SCAR=30.0
Pod undocumented
200 0 my ( undef , undef , $lng ) = @_ ;
201
202 0 ▶ if (!$lng) { $lng = length( $_[0] ) ;}
Branch: 1 branch
BranchTrueFalseTotal
unless ($lng) 0 0 0
203
204 0 my ($s,$sz) ;
205
206 0 ▶ while( $_[1] <= $lng ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] <= $lng) 0 0 0
207 0 $s = substr( $_[0] , $_[1] , 1) ;
208 0 $_[1]++ ;
209 0 ▶ if ($s eq ':') { last ;}
Branch: 1 branch
BranchTrueFalseTotal
if ($s eq ":") 0 0 0
210 0 $sz .= $s ;
211 }
212
213 0 my $blk = substr( $_[0] , $_[1] , $sz) ;
214
215 0 $_[1] += $sz ;
216  
217 0 return( $blk ) ;
218 }
219  
220 ##################
221 # IS_PACKED_HASH #
222 ##################
223  
224 0 ▶sub Is_Packed_HASH {
Subroutine Is_Packed_HASH: not called CC=2 SCAR=17.9
Pod documented
225 0 ▶ if ( $_[0] =~ /^\s*\%HDB_PACKED_HASH%\[[\d\.]+]\{\d+}:/ ) { return( 1 ) ;}
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\s*\%HDB_PACKED_HASH%\[[\d\.]+]\{\d+}:/) 0 0 0
226 0 return( undef ) ;
227 }
228  
229 ###################
230 # IS_PACKED_ARRAY #
231 ###################
232  
233 0 ▶sub Is_Packed_ARRAY {
Subroutine Is_Packed_ARRAY: not called CC=2 SCAR=17.9
Pod documented
234 0 ▶ if ( $_[0] =~ /^\s*\%HDB_PACKED_ARRAY%\[[\d\.]+]\{\d+}:/ ) { return( 1 ) ;}
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\s*\%HDB_PACKED_ARRAY%\[[\d\.]+]\{\d+}:/) 0 0 0
235 0 return( undef ) ;
236 }
237  
238 ###################
239 # CHECK_PACK_SIZE #
240 ###################
241  
242 0 ▶sub Check_Pack_Size {
Subroutine Check_Pack_Size: not called CC=3 SCAR=24.8
Pod documented
243 0 ▶ if ( $_[0] =~ /^(\s*\%HDB_PACKED_(?:HASH|ARRAY)%\[[\d\.]+]\{)(\d+)(}:)/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^(\s*\%HDB_PACKED_(?:HASH|ARRAY)%\[[\d\.]+]\{)(\d+)(}:)/s) 0 0 0
244 0 my $lng = length($1) + length($3) ;
245 0 my $sz = $2 ;
246 0 $lng += length($sz) + $sz ;
247 0 ▶ if ( length($_[0]) == $lng ) { return( 1 ) ;}
Branch: 1 branch
BranchTrueFalseTotal
if (length $_[0] == $lng) 0 0 0
248 }
249 0 return( undef ) ;
250 }
251  
252 ###############
253 # PACKED_SIZE #
254 ###############
255  
256 0 ▶sub Packed_SIZE {
Subroutine Packed_SIZE: not called CC=3 SCAR=24.8
Pod documented
257 0 my ( $ref ) = @_ ;
258 0 ▶ if ( ref($ref) eq 'HASH' ) { return &Packed_SIZE_HASH($ref) ;}
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref eq "HASH") 0 0 0
259 0 ▶ if ( ref($ref) eq 'ARRAY' ) { return &Packed_SIZE_ARRAY($ref) ;}
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref eq "ARRAY") 0 0 0
260 }
261  
262 ####################
263 # PACKED_SIZE_HASH #
264 ####################
265  
266 0 ▶sub Packed_SIZE_HASH {
Subroutine Packed_SIZE_HASH: not called CC=9 SCAR=45.0
Pod documented
267 0 my ( $hash ) = @_ ;
268
269 0 ▶ if (ref($hash) ne 'HASH') { return( undef ) ;}
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash ne "HASH") 0 0 0
270
271 0 my ($pack_init,$size) ;
272
273 0 ▶ $pack_init = "%HDB_PACKED_HASH%[$VER{PACKED_HASH}]{0}:" if !$_[1] ;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 0 0 0
274
275 0 foreach my $key ( keys %$hash ) {
276 0 my ($blk_sz,$value_sz) ;
277 0 ▶ if ( ref( $$hash{$key} ) eq 'HASH' ) { $value_sz = &Packed_SIZE_HASH( $$hash{$key} , 1 ) }
Branch: 3 branches
BranchTrueFalseTotal
if (ref $hash->{$key} eq 'HASH') { } 0 0 0
elsif (ref $hash->{$key} eq 'ARRAY') { } 0 0 0
elsif (UNIVERSAL::isa($hash->{$key}, 'UNIVERSAL')) { } 0 0 0
278 0 elsif ( ref( $$hash{$key} ) eq 'ARRAY' ) { $value_sz = &Packed_SIZE_ARRAY( $$hash{$key} , 1 ) ;}
279 0 elsif ( UNIVERSAL::isa($$hash{$key} ,'UNIVERSAL') ) { next ;} ## ignore objects.
280 0 else { $value_sz = length( $$hash{$key} ) ;}
281
282 0 $blk_sz += 1 ;
283 0 $blk_sz += length(length($key)) + 1 ;
284 0 $blk_sz += length($key) ;
285 0 $blk_sz += length($value_sz) + 1 ;
286 0 $blk_sz += $value_sz ;
287
288 0 $size += $blk_sz ;
289 }
290
291 0 ▶ if ( !$_[1] ) { $pack_init =~ s/\{0}/\{$size}/s ;}
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[1]) 0 0 0
292
293 0 ▶ $size += length($pack_init) if !$_[1] ;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 0 0 0
294
295 0 return( $size ) ;
296 }
297  
298 #####################
299 # PACKED_SIZE_ARRAY #
300 #####################
301  
302 0 ▶sub Packed_SIZE_ARRAY {
Subroutine Packed_SIZE_ARRAY: not called CC=9 SCAR=45.0
Pod documented
303 0 my ( $array ) = @_ ;
304
305 0 ▶ if (ref($array) ne 'ARRAY') { return( undef ) ;}
Branch: 1 branch
BranchTrueFalseTotal
if (ref $array ne "ARRAY") 0 0 0
306
307 0 my ($pack_init,$size) ;
308
309 0 ▶ $pack_init = "%HDB_PACKED_ARRAY%[$VER{PACKED_ARRAY}]{0}:" if !$_[1] ;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 0 0 0
310
311 0 foreach my $array_i ( @$array ) {
312 0 my ($blk_sz,$value_sz) ;
313 0 ▶ if ( ref( $array_i ) eq 'HASH' ) { $value_sz = &Packed_SIZE_HASH( $array_i , 1 ) ;}
Branch: 3 branches
BranchTrueFalseTotal
if (ref $array_i eq 'HASH') { } 0 0 0
elsif (ref $array_i eq 'ARRAY') { } 0 0 0
elsif (UNIVERSAL::isa($array_i, 'UNIVERSAL')) { } 0 0 0
314 0 elsif ( ref( $array_i ) eq 'ARRAY' ) { $value_sz = &Packed_SIZE_ARRAY( $array_i , 1 ) ;}
315 0 elsif ( UNIVERSAL::isa($array_i ,'UNIVERSAL') ) { next ;} ## ignore objects.
316 0 else { $value_sz = length($array_i) ;}
317
318 0 $blk_sz += 1 ;
319 0 $blk_sz += length($value_sz) + 1 ;
320 0 $blk_sz += $value_sz ;
321
322 0 $size += $blk_sz ;
323 }
324
325 0 ▶ if ( !$_[1] ) { $pack_init =~ s/\{0}/\{$size}/s ;}
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[1]) 0 0 0
326
327 0 ▶ $size += length($pack_init) if !$_[1] ;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 0 0 0
328
329 0 return( $size ) ;
330 }
331  
332 #######
333 # END #
334 #######
335  
336 # 0 => key & val || line
337 # 1 => hash ref
338 # 2 => array ref
339  
340 1 1;
341  
342 __END__
« HDB.pm ↑ Summary HDB/CORE.pm »