FieldPack.pm

filter: Statementstmt 13.9% 13 / 93 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 17.6% 3 / 17 Podpod 0.0% 0 / 12 totaltotal 7.6% 16 / 208 CC 39 SCAR 39.4
CC
39
Cov
8%
__unpack_fields
90.0
unpack_DateType
90.0
__pack_fields
72.0
CRAP 51.3
« MaTirelire/CGICLI.pm ↑ Summary MaTirelire/DBItemId.pm »
1 =encoding iso-8859-1
2  
3 =cut
4  
5 #
6 # Copyright (C) 2005, Maxime Soulé
7 # You may distribute this file under the terms of the Artistic
8 # License, as specified in the README file.
9 #
10  
11 package Palm::FieldPack;
12  
13 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 our @EXPORT = qw(pack_fields_to_UInt8 unpack_UInt8_to_fields
18 pack_fields_to_UInt16 unpack_UInt16_to_fields
19 pack_fields_to_UInt32 unpack_UInt32_to_fields
20 pack_DateType unpack_DateType
21 pack_TimeType unpack_TimeType
22 pack_double unpack_double);
23  
24 2 our $VERSION = '1.0';
25  
26  
27 2 my $big_endian;
28 BEGIN
29 2 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 if (length(pack('d', 0)) != 8)
31 {
32 die "Can't manage double values in this database, abort.\n";
33 }
34  
35 2 if (pack('I', 0x12345678) eq pack('N', 0x12345678))
36 {
37 $big_endian = 1;
38 }
39 elsif (pack('I', 0x12345678) eq pack('V', 0x12345678))
40 {
41 2 $big_endian = 0;
42 }
43 else
44 {
45 die "Can't guess indianess of this host, abort\n";
46 }
47 }
48  
49  
50 #
51 # Arguments are :
52 # nbits, [hash,] packed_val, "name1:bitfld1_width", "name2:bitfld2_width",etc
53 # "nameX:bitfield_widths" are in the same order than in the C struct
54 # Returns values for each bitfield in the same order
55 sub __unpack_fields
56 0 ▶{
Subroutine __unpack_fields: not called CC=9 SCAR=45.0
57 0 my $cur_shift = shift; # Bit width...
58 0 my($ref_hash, $uint);
59 0 my @result;
60  
61 0 $ref_hash = shift;
62 0 ▶ if (ref($ref_hash) eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref_hash eq 'HASH') { } 0 0 0
63 {
64 0 $uint = shift;
65 }
66 else
67 {
68 0 $uint = $ref_hash;
69 0 $ref_hash = undef;
70 }
71  
72 0 ▶ $uint = unpack($cur_shift == 32 ? 'N' : ($cur_shift == 16 ? 'n' : 'C'),
Branch: 2 branches
BranchTrueFalseTotal
$cur_shift == 32 ? : 0 0 0
$cur_shift == 16 ? : 0 0 0
73 $uint);
74  
75 0 foreach my $width_str (@_)
76 {
77 0 my($name, $width) = split(':', $width_str);
78  
79 0 ▶ $width = $cur_shift if $width eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $width eq "*" 0 0 0
80  
81 0 $cur_shift -= $width;
82  
83 0 my $value = ($uint >> $cur_shift) & ((1 << $width) - 1);
84  
85 0 ▶ $ref_hash->{$name} = $value if defined $ref_hash;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ref_hash 0 0 0
86 0 push(@result, $value);
87 }
88  
89 0 ▶ return unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 0 0
90  
91 0 ▶ return wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
92 }
93  
94  
95 #
96 # Arguments are : bits, hash, "name1:bitfld1_width", "name1:bitfld2_width", etc
97 # Returns the packed value
98 sub __pack_fields
99 0 ▶{
Subroutine __pack_fields: not called CC=8 SCAR=42.8
100 0 my $uint = 0;
101 0 my $cur_shift = shift;
102 0 my $ref_hash = shift;
103  
104 0 ▶ my $pack_char = $cur_shift == 32 ? 'N' : ($cur_shift == 16 ? 'n' : 'C');
Branch: 2 branches
BranchTrueFalseTotal
$cur_shift == 32 ? : 0 0 0
$cur_shift == 16 ? : 0 0 0
105  
106 0 foreach my $width_str (@_)
107 {
108 0 my($name, $width) = split(':', $width_str);
109  
110 0 ▶ $width = $cur_shift if $width eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $width eq "*" 0 0 0
111  
112 0 $cur_shift -= $width;
113  
114 0 my $val = $ref_hash->{$name};
115  
116 0 ▶ if (defined $val)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 0 0 0
117 {
118 # Référence => consider 1
119 0 ▶ if (ref $val)
Branch: 2 branches
BranchTrueFalseTotal
if (ref $val) { } 0 0 0
elsif ($width == 1) { } 0 0 0
120 {
121 0 $val = 1;
122 }
123 # Boolean value
124 elsif ($width == 1)
125 {
126 0 $val = ($val != 0);
127 }
128 }
129 else
130 {
131 0 $val = 0;
132 }
133  
134 0 $uint |= ($val & ((1 << $width) - 1)) << $cur_shift;
135 }
136  
137 0 return pack($pack_char, $uint);
138 }
139  
140  
141 sub pack_double ($)
142 0 ▶{
Subroutine pack_double: not called CC=2 SCAR=17.9
Pod undocumented
143 0 my $pack = pack('d', shift);
144  
145 0 ▶ return $pack if $big_endian;
Branch: 1 branch
BranchTrueFalseTotal
if $big_endian 0 0 0
146  
147 0 return scalar reverse $pack;
148 }
149  
150  
151 sub unpack_double ($)
152 0 ▶{
Subroutine unpack_double: not called CC=2 SCAR=17.9
Pod undocumented
153 0 my $pack = shift;
154  
155 0 ▶ $pack = scalar reverse $pack unless $big_endian;
Branch: 1 branch
BranchTrueFalseTotal
unless $big_endian 0 0 0
156  
157 0 return unpack('d', $pack);
158 }
159  
160  
161 #
162 # Arguments are : hash, "name1:bitfld1_width", "name1:bitfld2_width", etc
163 # in the same order than in the C struct
164 # Returns the packed value
165 sub pack_fields_to_UInt32
166 0 ▶{
Subroutine pack_fields_to_UInt32: not called CC=1 SCAR=6.9
Pod undocumented
167 0 return __pack_fields(32, @_);
168 }
169  
170  
171 #
172 # Arguments are : hash, "name1:bitfld1_width", "name1:bitfld2_width", etc
173 # in the same order than in the C struct
174 # Returns the packed value
175 sub pack_fields_to_UInt16
176 0 ▶{
Subroutine pack_fields_to_UInt16: not called CC=1 SCAR=6.9
Pod undocumented
177 0 return __pack_fields(16, @_);
178 }
179  
180  
181 #
182 # Arguments are : hash, "name1:bitfld1_width", "name1:bitfld2_width", etc
183 # in the same order than in the C struct
184 # Returns the packed value
185 sub pack_fields_to_UInt8
186 0 ▶{
Subroutine pack_fields_to_UInt8: not called CC=1 SCAR=6.9
Pod undocumented
187 0 return __pack_fields(8, @_);
188 }
189  
190  
191 #
192 # Arguments are :
193 # [hash,] packed_val, "name1:bitfld1_width", "name2:bitfld2_width",etc
194 # in the same order than in the C struct
195 # Returns values for each bitfield in the same order
196 sub unpack_UInt8_to_fields
197 0 ▶{
Subroutine unpack_UInt8_to_fields: not called CC=1 SCAR=6.9
Pod undocumented
198 0 return __unpack_fields(8, @_);
199 }
200  
201  
202 #
203 # Arguments are :
204 # [hash,] packed_val, "name1:bitfld1_width", "name2:bitfld2_width",etc
205 # in the same order than in the C struct
206 # Returns values for each bitfield in the same order
207 sub unpack_UInt16_to_fields
208 0 ▶{
Subroutine unpack_UInt16_to_fields: not called CC=1 SCAR=6.9
Pod undocumented
209 0 return __unpack_fields(16, @_);
210 }
211  
212  
213 #
214 # Arguments are :
215 # [hash,] packed_val, "name1:bitfld1_width", "name2:bitfld2_width",etc
216 # in the same order than in the C struct
217 # Returns values for each bitfield in the same order
218 sub unpack_UInt32_to_fields
219 0 ▶{
Subroutine unpack_UInt32_to_fields: not called CC=1 SCAR=6.9
Pod undocumented
220 0 return __unpack_fields(32, @_);
221 }
222  
223  
224 #
225 # Arguments are : hash [, "prefix_"]
226 sub pack_DateType
227 0 ▶{
Subroutine pack_DateType: not called CC=6 SCAR=37.4
Pod undocumented
228 0 my($ref_hash, $prefix) = @_;
229  
230 0 ▶ $prefix = '' unless defined $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix 0 0 0
231  
232 # Sometimes we want the date to be totaly zeroed, don't add 1904
233 # in these cases
234 0 ▶ if (defined $ref_hash->{"${prefix}year"}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$ref_hash{"${prefix}year"} and $$ref_hash{"${prefix}year"} > 0) 0 0 0
Condition0%0/3
defined $$ref_hash{"${prefix}year"} and $$ref_hash{"${prefix}year"} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $$ref_hash{"${prefix}year"} and $$ref_hash{"${prefix}year"} > 0
defined $$ref_hash{"${prefix}year"} $$ref_hash{"${prefix}year"} > 0
Truth table0%0/3
A and B
A= defined $$ref_hash{"${prefix}year"}B= $$ref_hash{"${prefix}year"} > 0
ABresult
0X0
100
111
235 and $ref_hash->{"${prefix}year"} > 0)
236 {
237 0 $ref_hash->{"${prefix}year"} -= 1904;
238 }
239  
240 0 my $pack = pack_fields_to_UInt16($ref_hash,
241 "${prefix}year:7",
242 "${prefix}month:4",
243 "${prefix}day:5");
244  
245 # Sometimes we want the date to be totaly zeroed, don't add 1904
246 # in these cases
247 0 ▶ if (defined $ref_hash->{"${prefix}year"}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$ref_hash{"${prefix}year"} and $$ref_hash{"${prefix}year"} > 0) 0 0 0
Condition0%0/3
defined $$ref_hash{"${prefix}year"} and $$ref_hash{"${prefix}year"} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $$ref_hash{"${prefix}year"} and $$ref_hash{"${prefix}year"} > 0
defined $$ref_hash{"${prefix}year"} $$ref_hash{"${prefix}year"} > 0
Truth table0%0/3
A and B
A= defined $$ref_hash{"${prefix}year"}B= $$ref_hash{"${prefix}year"} > 0
ABresult
0X0
100
111
248 and $ref_hash->{"${prefix}year"} > 0)
249 {
250 0 $ref_hash->{"${prefix}year"} += 1904;
251 }
252  
253 0 return $pack;
254 }
255  
256  
257 #
258 # Arguments are : [hash,] packed_val [, "prefix_" (only if hash is present)]
259 sub unpack_DateType
260 0 ▶{
Subroutine unpack_DateType: not called CC=9 SCAR=45.0
Pod undocumented
261 0 my $ref_hash = $_[0];
262 0 ▶ my $prefix = @_ == 3 ? pop : '';
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 ? : 0 0 0
263 0 my @result;
264  
265 0 @result = unpack_UInt16_to_fields(@_,
266 "${prefix}year:7",
267 "${prefix}month:4",
268 "${prefix}day:5");
269  
270 # Sometimes we want the date to be totaly zeroed, don't add 1904
271 # in these cases
272 0 ▶ if (ref($ref_hash) eq 'HASH' and $ref_hash->{"${prefix}year"} > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref_hash eq "HASH" and $$ref_hash{"${prefix}year"} > 0) 0 0 0
Condition0%0/3
ref $ref_hash eq "HASH" and $$ref_hash{"${prefix}year"} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
ref $ref_hash eq "HASH" and $$ref_hash{"${prefix}year"} > 0
ref $ref_hash eq "HASH" $$ref_hash{"${prefix}year"} > 0
Truth table0%0/3
A and B
A= ref $ref_hash eq "HASH"B= $$ref_hash{"${prefix}year"} > 0
ABresult
0X0
100
111
273 {
274 0 $ref_hash->{"${prefix}year"} += 1904;
275 }
276  
277 0 ▶ return unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 0 0
278  
279 # Sometimes we want the date to be totaly zeroed, don't add 1904
280 # in these cases
281 0 ▶ if ($result[0] != 0 or $result[1] != 0 or $result[2] != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($result[0] != 0 or $result[1] != 0 or $result[2] != 0) 0 0 0
Condition0%0/6
$result[0] != 0 or $result[1] != 0 or $result[2] != 0
l!l&&r!l&&!r
000
$result[0] != 0 or $result[1] != 0
l!l&&r!l&&!r
000
MC/DC0%0/3
$result[0] != 0 or $result[1] != 0 or $result[2] != 0
$result[0] != 0 $result[1] != 0 $result[2] != 0
Truth table0%0/4
A or B or C
A= $result[0] != 0B= $result[1] != 0C= $result[2] != 0
ABCresult
1XX1
01X1
0011
0000
282 {
283 0 $result[0] += 1904;
284 }
285  
286 0 ▶ return wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
287 }
288  
289  
290 #
291 # Arguments are : hash [, "prefix_"]
292 sub pack_TimeType
293 0 ▶{
Subroutine pack_TimeType: not called CC=4 SCAR=30.0
Pod undocumented
294 0 my($ref_hash, $prefix) = @_;
295  
296 0 ▶ $prefix = '' unless defined $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix 0 0 0
297  
298 0 ▶ return pack('CC', map { defined($_) ? (ref($_) ? 1 : $_) : 0 }
Branch: 2 branches
BranchTrueFalseTotal
defined $_ ? : 0 0 0
ref $_ ? : 0 0 0
299 0 @$ref_hash{"${prefix}hour", "${prefix}min"});
300 }
301  
302  
303 #
304 # Arguments are : [hash,] packed_val [, "prefix_"]
305 sub unpack_TimeType
306 0 ▶{
Subroutine unpack_TimeType: not called CC=6 SCAR=37.4
Pod undocumented
307 0 my($ref_hash, $packed_value, $prefix);
308 0 my @result;
309  
310 0 $ref_hash = shift;
311  
312 0 ▶ if (ref($ref_hash) eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref_hash eq 'HASH') { } 0 0 0
313 {
314 0 ($packed_value, $prefix) = @_;
315  
316 0 ▶ $prefix = '' unless defined $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix 0 0 0
317 }
318 else
319 {
320 0 $packed_value = $ref_hash;
321 0 $ref_hash = undef;
322 }
323  
324 0 @result = unpack('CC', $packed_value);
325  
326 0 ▶ @$ref_hash{"${prefix}hour", "${prefix}min"} = @result if defined $ref_hash;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ref_hash 0 0 0
327  
328 0 ▶ return unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 0 0
329  
330 0 ▶ return wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
331 }
332  
333 2 1;
334 __END__
« MaTirelire/CGICLI.pm ↑ Summary MaTirelire/DBItemId.pm »