MaTirelire/Types.pm

filter: Statementstmt 9.9% 16 / 161 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 41.6% 5 / 12 Podpod 0.0% 0 / 7 totaltotal 6.9% 21 / 301 CC 58 SCAR 50.6
CC
58
Cov
6%
new_RecordWithFullName
306.0
dump
210.0
new_RecordChildOf
132.0
CRAP 158.2
« MaTirelire/AccountsV1.pm ↑ Summary MaTirelire/CGICLI.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::MaTirelire::Types;
12  
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use Palm::BlockPack;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use Palm::MaTirelire::DBItemId;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use base qw(Palm::MaTirelire::DBItemId);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 our $VERSION = '1.0';
22  
23  
24 # Each record
25 1 my $RECORD_BLOCK = Palm::BlockPack->new
26 (UInt32 => [
27 [ 'type_id:8' => 0 ],
28 [ 'parent_id:8' => 0xff ],
29 [ 'child_id:8' => 0xff ],
30 [ 'brother_id:8' => 0xff ],
31 ],
32 UInt32 => [
33 [ 'sign_depend:2' => 3 ],
34 [ 'folded:1' => 0 ],
35 [ 'reserved:*' => 0 ],
36 ],
37 'Z24' => [ 'only_in_account' => '' ],
38 'Z*' => [ 'name' => '' ],
39 );
40  
41  
42 sub import
43 1 ▶{
Subroutine import: called CC=1 SCAR=0.0
44 1 &Palm::PDB::RegisterPDBHandlers(__PACKAGE__, [ "MaT2", 'Type' ]);
45 }
46  
47  
48 sub meta_infos ($)
49 0 ▶{
Subroutine meta_infos: not called CC=1 SCAR=6.9
Pod undocumented
50 0 return { name => "MaTi-Types",
51 type => "Type",
52 record_block => $RECORD_BLOCK,
53 id_field => 'type_id',
54 unfiled_name => 'Unfiled',
55 num => (1 << 8),
56 };
57 }
58  
59  
60 sub full_name
61 0 ▶{
Subroutine full_name: not called CC=6 SCAR=37.4
Pod undocumented
62 0 my($self, $id) = @_;
63  
64 # "Unfiled" case
65 0 my($num, $unfiled_name) = @{$self->meta_infos}{qw(num unfiled_name)};
66 0 ▶ return $unfiled_name if $id == $num - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $id == $num - 1 0 0 0
67  
68 0 my $cache = $self->build_cache_id;
69  
70 0 my $rec = $cache->[$id];
71  
72 0 ▶ return undef unless defined $rec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rec 0 0 0
73  
74 0 my $full_name = $rec->{name};
75  
76 0 ▶ while (defined $rec and $rec->{parent_id} != 0xff)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rec and $rec->{"parent_id"} != 255) 0 0 0
Condition0%0/3
defined $rec and $rec->{"parent_id"} != 255
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rec and $rec->{"parent_id"} != 255
defined $rec $rec->{"parent_id"} != 255
Truth table0%0/3
A and B
A= defined $recB= $rec->{"parent_id"} != 255
ABresult
0X0
100
111
77 {
78 0 $rec = $cache->[$rec->{parent_id}];
79  
80 0 ▶ substr($full_name, 0, 0) = (defined($rec) ? $rec->{name} : '?') . '/';
Branch: 1 branch
BranchTrueFalseTotal
defined $rec ? : 0 0 0
81 }
82  
83 0 return $full_name;
84 }
85  
86  
87 sub find_by_full_name ($$)
88 0 ▶{
Subroutine find_by_full_name: not called CC=9 SCAR=45.0
Pod undocumented
89 0 my($self, $full_name) = @_;
90  
91 my($id_field, $unfiled_name)
92 0 = @{$self->meta_infos}{qw(id_field unfiled_name)};
93  
94 0 ▶ return wantarray ? () : undef if $full_name eq $unfiled_name;
Branch: 2 branches
BranchTrueFalseTotal
if $full_name eq $unfiled_name 0 0 0
wantarray ? : 0 0 0
95  
96 0 my $parent_id = 0xff;
97 0 my $rec;
98  
99 0 type: foreach my $sub_type (split('/', $full_name))
100 {
101 0 ▶ $sub_type = '<empty>' if $sub_type =~ /^\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $sub_type =~ /^\s*\z/ 0 0 0
102  
103 0 foreach my $cur_rec (@{$self->{records}})
104 {
105 0 ▶ if ($cur_rec->{parent_id} == $parent_id
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_rec->{"parent_id"} == $parent_id and $cur_rec->{"name"} eq $sub_type) 0 0 0
Condition0%0/3
$cur_rec->{"parent_id"} == $parent_id and $cur_rec->{"name"} eq $sub_type
!ll&&!rl&&r
000
MC/DC0%0/2
$cur_rec->{"parent_id"} == $parent_id and $cur_rec->{"name"} eq $sub_type
$cur_rec->{"parent_id"} == $parent_id $cur_rec->{"name"} eq $sub_type
Truth table0%0/3
A and B
A= $cur_rec->{"parent_id"} == $parent_idB= $cur_rec->{"name"} eq $sub_type
ABresult
0X0
100
111
106 and $cur_rec->{name} eq $sub_type)
107 {
108 0 $parent_id = $cur_rec->{type_id};
109 0 $rec = $cur_rec;
110 0 next type;
111 }
112 }
113  
114 # Not found...
115 0 ▶ return wantarray ? () : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
116 }
117  
118 0 return $rec;
119 }
120  
121  
122 sub replaceAutoID ($$)
123 0 ▶{
Subroutine replaceAutoID: not called CC=6 SCAR=37.4
Pod undocumented
124 0 my($self, $rec) = @_;
125  
126 0 my $new_id = $self->get_first_free_id;
127 0 ▶ return undef unless defined $new_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_id 0 0 0
128  
129 0 my $old_id = $rec->{type_id};
130 0 $rec->{type_id} = $new_id;
131  
132 0 foreach $rec (@{$self->{records}})
133 {
134 0 ▶ $rec->{parent_id} = $new_id if $rec->{parent_id} == $old_id;
Branch: 1 branch
BranchTrueFalseTotal
if $rec->{"parent_id"} == $old_id 0 0 0
135 0 ▶ $rec->{child_id} = $new_id if $rec->{child_id} == $old_id;
Branch: 1 branch
BranchTrueFalseTotal
if $rec->{"child_id"} == $old_id 0 0 0
136 0 ▶ $rec->{brother_id} = $new_id if $rec->{brother_id} == $old_id;
Branch: 1 branch
BranchTrueFalseTotal
if $rec->{"brother_id"} == $old_id 0 0 0
137 }
138  
139 0 return $new_id;
140 }
141  
142  
143 sub new_RecordWithFullName ($$;$)
144 0 ▶{
Subroutine new_RecordWithFullName: not called CC=17 SCAR=57.2
Pod undocumented
145 0 my($self, $full_name, $final_id) = @_;
146  
147 my($id_field, $unfiled_name)
148 0 = @{$self->meta_infos}{qw(id_field unfiled_name)};
149  
150 0 ▶ return undef if $full_name eq $unfiled_name;
Branch: 1 branch
BranchTrueFalseTotal
if $full_name eq $unfiled_name 0 0 0
151  
152 0 my $rec;
153  
154 # This final ID already "auto created" ?
155 0 ▶ if (defined $final_id
Branch: 1 branch
BranchTrueFalseTotal
if (defined $final_id and defined($rec = $self->build_cache_id->[$final_id])) 0 0 0
Condition0%0/3
defined $final_id and defined($rec = $self->build_cache_id->[$final_id])
!ll&&!rl&&r
000
MC/DC0%0/2
defined $final_id and defined($rec = $self->build_cache_id->[$final_id])
defined $final_id defined($rec = $self->build_cache_id->[$final_id])
Truth table0%0/3
A and B
A= defined $final_idB= defined($rec = $self->build_cache_id->[$final_id])
ABresult
0X0
100
111
156 and defined($rec = $self->build_cache_id->[$final_id]))
157 {
158 0 ▶ if ($rec->{auto_id})
Branch: 1 branch
BranchTrueFalseTotal
if ($rec->{'auto_id'}) { } 0 0 0
159 {
160 0 ▶ return undef unless defined $self->replaceAutoID($rec);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->replaceAutoID($rec) 0 0 0
161 }
162 else
163 {
164 # This ID already exists but is not an auto one!
165 # XXX
166 }
167 }
168  
169 0 my $parent_id = 0xff;
170 0 my @sub_types = split('/', $full_name);
171  
172 # No more than 10 depth levels...
173 0 ▶ return undef if @sub_types > 10;
Branch: 1 branch
BranchTrueFalseTotal
if @sub_types > 10 0 0 0
174  
175 0 type: for (;;)
176 {
177 0 my $sub_type = shift @sub_types;
178 0 ▶ last unless defined $sub_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sub_type 0 0 0
179  
180 0 ▶ $sub_type = '<empty>' if $sub_type =~ /^\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $sub_type =~ /^\s*\z/ 0 0 0
181  
182 0 foreach my $cur_rec (@{$self->{records}})
183 {
184 0 ▶ if ($cur_rec->{parent_id} == $parent_id
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_rec->{"parent_id"} == $parent_id and $cur_rec->{"name"} eq $sub_type) 0 0 0
Condition0%0/3
$cur_rec->{"parent_id"} == $parent_id and $cur_rec->{"name"} eq $sub_type
!ll&&!rl&&r
000
MC/DC0%0/2
$cur_rec->{"parent_id"} == $parent_id and $cur_rec->{"name"} eq $sub_type
$cur_rec->{"parent_id"} == $parent_id $cur_rec->{"name"} eq $sub_type
Truth table0%0/3
A and B
A= $cur_rec->{"parent_id"} == $parent_idB= $cur_rec->{"name"} eq $sub_type
ABresult
0X0
100
111
185 and $cur_rec->{name} eq $sub_type)
186 {
187 0 $parent_id = $cur_rec->{type_id};
188  
189 0 ▶ return $cur_rec if @sub_types == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @sub_types == 0 0 0 0
190 0 next type;
191 }
192 }
193  
194 0 unshift(@sub_types, $sub_type);
195  
196 # Not found, create this sub type and all behind it
197 0 ▶ for (my $sub_idx = 0; $sub_idx < @sub_types; $sub_idx++)
Branch: 1 branch
BranchTrueFalseTotal
if ($sub_idx < @sub_types) 0 0 0
198 {
199 0 $sub_type = $sub_types[$sub_idx];
200  
201 0 ▶ $sub_type = '<empty>' if $sub_type =~ /^\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $sub_type =~ /^\s*\z/ 0 0 0
202  
203 0 ▶ $rec = $self->new_RecordChildOf($parent_id,
Branch: 1 branch
BranchTrueFalseTotal
$sub_idx == @sub_types - 1 ? : 0 0 0
204 $sub_idx == @sub_types - 1
205 ? $final_id : undef);
206 0 ▶ last unless defined $rec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rec 0 0 0
207  
208 0 $rec->{name} = $sub_type;
209 0 $parent_id = $rec->{type_id};
210 }
211  
212 0 last;
213 }
214  
215 # This type already exists, returns it (undef if not enough free IDs)
216 0 return $rec;
217 }
218  
219  
220 sub new_RecordChildOf ($$;$)
221 0 ▶{
Subroutine new_RecordChildOf: not called CC=11 SCAR=48.8
Pod undocumented
222 0 my($self, $parent_id, $final_id) = @_;
223  
224 0 my $rec;
225  
226 my $id;
227 0 my $auto_id;
228 0 ▶ if (defined($final_id))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $final_id) { } 0 0 0
229 {
230 0 $id = $final_id;
231  
232 0 $rec = $self->build_cache_id->[$id];
233 0 ▶ if (defined $rec)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rec) 0 0 0
234 {
235 0 ▶ if ($rec->{auto_id})
Branch: 1 branch
BranchTrueFalseTotal
if ($rec->{'auto_id'}) { } 0 0 0
236 {
237 0 ▶ return undef unless defined $self->replaceAutoID($rec);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->replaceAutoID($rec) 0 0 0
238 }
239 else
240 {
241 # This ID already exists but is not an auto one!
242 # XXX
243 }
244 }
245 }
246 else
247 {
248 0 $id = $self->get_first_free_id;
249 0 $auto_id = 1;
250 }
251  
252 0 ▶ return undef unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
253  
254 0 $rec = $self->new_Record;
255  
256 0 $rec->{type_id} = $id;
257 0 ▶ $rec->{auto_id} = 1 if $auto_id;
Branch: 1 branch
BranchTrueFalseTotal
if $auto_id 0 0 0
258  
259 # Insert the type somewhere in the top level
260 0 ▶ if ($parent_id == 0xff)
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_id == 255) { } 0 0 0
261 {
262 # Nothing to do if no record is present (no brother)
263 0 foreach my $rec_any (@{$self->{records}})
264 {
265 # Toplevel record
266 0 ▶ if ($rec_any->{parent_id} == 0xff)
Branch: 1 branch
BranchTrueFalseTotal
if ($rec_any->{"parent_id"} == 255) 0 0 0
267 {
268 0 $rec->{brother_id} = $rec_any->{brother_id};
269 0 $rec_any->{brother_id} = $id;
270 0 last;
271 }
272 }
273 }
274 else
275 {
276 0 my $parent_rec = $self->get_id($parent_id);
277 0 ▶ return undef unless defined $parent_rec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parent_rec 0 0 0
278  
279 # Add the type as the first child of parent
280 0 $rec->{parent_id} = $parent_id;
281 0 $rec->{brother_id} = $parent_rec->{child_id};
282  
283 0 $parent_rec->{child_id} = $id;
284 }
285  
286 0 $self->append_Record($rec);
287  
288 0 return $rec;
289 }
290  
291  
292 sub dump
293 0 ▶{
Subroutine dump: not called CC=14 SCAR=53.5
Pod undocumented
294 0 my $self = shift;
295  
296 0 ▶ return if @{$self->{records}} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"records"};} == 0 0 0 0
297  
298 0 my $first_id = 0xff;
299 0 my $rec;
300 0 my($index, $loops);
301  
302 0 $loops = 0xff;
303  
304 # Search the first type
305 0 ▶ for ($index = 0; $index < @{$self->{records}}; $index++)
Branch: 1 branch
BranchTrueFalseTotal
if ($index < @{$self->{"records"};}) 0 0 0
306 {
307 0 $rec = $self->{records}[$index];
308  
309 0 ▶ if ($rec->{parent_id} == 0xff
Branch: 1 branch
BranchTrueFalseTotal
if ($rec->{"parent_id"} == 255 and $first_id == 255 || $rec->{"brother_id"} == $first_id) 0 0 0
Condition0%0/6
$rec->{"parent_id"} == 255 and $first_id == 255 || $rec->{"brother_id"} == $first_id
!ll&&!rl&&r
000
$first_id == 255 or $rec->{"brother_id"} == $first_id
l!l&&r!l&&!r
000
MC/DC0%0/3
$rec->{"parent_id"} == 255 and $first_id == 255 || $rec->{"brother_id"} == $first_id
$rec->{"parent_id"} == 255 $first_id == 255 $rec->{"brother_id"} == $first_id
Truth table0%0/4
A and B or C
A= $rec->{"parent_id"} == 255B= $first_id == 255C= $rec->{"brother_id"} == $first_id
ABCresult
0XX0
1000
11X1
1011
310 and ($first_id == 0xff or $rec->{brother_id} == $first_id))
311 {
312 0 $first_id = $rec->{type_id};
313 0 $index = -1;
314  
315 # Par sécurité... XXX
316 0 ▶ if (--$loops == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (--$loops == 0) 0 0 0
317 {
318 0 die "Types first ID loop detected...\n";
319 0 last;
320 }
321 }
322 }
323  
324 0 my $ref_cache = $self->build_cache_id;
325  
326 0 my $depth_glyphs = '';
327 0 my $depth = 1;
328 0 $rec = $ref_cache->[$first_id];
329 0 my @types;
330 my $id;
331  
332 0 for (;;)
333 {
334 push(@types,
335 {
336 depth => $depth,
337 depth_glyphs => $depth_glyphs . ($rec->{brother_id} == 0xff
338 ? '+-' : '|-'),
339 type_id => $rec->{type_id},
340 name => $rec->{name},
341 0 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
$rec->{'brother_id'} == 255 ? : 0 0 0
342  
343 # Type has a child
344 0 $id = $rec->{child_id};
345 0 ▶ if ($id != 0xff)
Branch: 1 branch
BranchTrueFalseTotal
if ($id != 255) 0 0 0
346 {
347 0 ▶ $depth_glyphs .= $rec->{brother_id} == 0xff ? ' ' : '| ';
Branch: 1 branch
BranchTrueFalseTotal
$rec->{'brother_id'} == 255 ? : 0 0 0
348 0 $depth++;
349  
350 0 goto load_and_continue;
351 }
352  
353 # Else type has a brother
354 brother:
355 0 $id = $rec->{brother_id};
356 0 ▶ goto load_and_continue if $id != 0xff;
Branch: 1 branch
BranchTrueFalseTotal
if $id != 255 0 0 0
357  
358 # Else, if the type has a parent => go to his brother OR his parent
359 0 $id = $rec->{parent_id};
360 0 ▶ if ($id != 0xff)
Branch: 1 branch
BranchTrueFalseTotal
if ($id != 255) 0 0 0
361 {
362 0 substr($depth_glyphs, -2) = '';
363 0 $depth--;
364  
365 0 $rec = $ref_cache->[$id];
366  
367 0 goto brother;
368 }
369  
370 # Else that's all folk...
371 0 last;
372  
373 0 load_and_continue:
374 $rec = $ref_cache->[$id];
375 }
376  
377 0 ▶ if (@types != @{$self->{records}})
Branch: 1 branch
BranchTrueFalseTotal
if (@types != @{$self->{"records"};}) 0 0 0
378 {
379 die("Not all types are chained, only ", scalar(@types), " on ",
380 0 scalar(@{$self->{records}}), "\n");
381 }
382  
383 0 ▶ return @types if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
384  
385 return join("\n",
386 0 map { "$_->{depth_glyphs} $_->{name} ($_->{type_id})" }
387 @types);
388 }
389  
390  
391 1 1;
392 __END__
« MaTirelire/AccountsV1.pm ↑ Summary MaTirelire/CGICLI.pm »