MaTirelire/AccountsV1.pm

filter: Statementstmt 19.8% 30 / 151 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 50.0% 8 / 16 Podpod 75.0% 6 / 8 totaltotal 17.4% 44 / 252 CC 37 SCAR 59.0
CC
37
Cov
14%
validRecords
702.0
PackRecord
72.0
ParseRecord
30.0
CRAP 365.3
« MaTirelire/AccountsV2.pm ↑ Summary MaTirelire/Types.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::AccountsV1;
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;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Palm::StdAppInfo();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use base qw(Palm::MaTirelire Palm::StdAppInfo);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 our $VERSION = '1.0';
23  
24  
25 1 ▶use constant UNKNOWN_MODE => ((1 << 5) - 1);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use constant UNKNOWN_TYPE => ((1 << 6) - 1);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 my $TRANS_BLOCK = Palm::BlockPack->new
29 ('DateType' => [ 'date_' => 'now' ],
30 'TimeType' => [ 'time_' => 'now' ],
31  
32 '-N' => [ 'amount' => 0 ],
33  
34 UInt32 => [
35 [ 'checked:1' => 0 ],
36 'repeat:1',
37 [ 'mode:5' => UNKNOWN_MODE ],
38 [ 'type:6' => UNKNOWN_TYPE ],
39 'check_num:1',
40 'xfer:1',
41 [ 'marked:1' => 0 ],
42 [ 'alarm:1' => 0 ],
43 'xfer_cat:1',
44 'value_date:1',
45 [ 'reserved:*' => 0 ],
46 ],
47 );
48  
49 1 my $TRANS_CHECKNUM_BLOCK = Palm::BlockPack->new(N => 'check_num');
50  
51 1 my $TRANS_VALUEDATE_BLOCK = Palm::BlockPack->new(DateType => '');
52  
53 1 my $TRANS_REPEAT_BLOCK = Palm::BlockPack->new
54 (UInt16 => [
55 'repeat_type:2',
56 'repeat_freq:6',
57 'reserved:*',
58 ],
59 skip => [ 2 => "\xff" ], # End date is not used in M1 and must be -1
60 );
61  
62 1 my $TRANS_XFER_BLOCK = Palm::BlockPack->new(N => 'xfer');
63  
64 1 my $TRANS_DESCRIPTION_BLOCK = Palm::BlockPack->new
65 ('Z*' => [ 'description' => '' ]);
66  
67  
68 sub import
69 1 ▶{
Subroutine import: called CC=1 SCAR=0.0
70 1 &Palm::PDB::RegisterPDBHandlers(__PACKAGE__, [ "MaTi", 'Data' ]);
71 }
72  
73  
74 sub new
75 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
76 0 my $classname = shift;
77 0 my $self = $classname->SUPER::new(@_);
78 # Create a generic PDB. No need to rebless it,
79 # though.
80  
81 # Creator for V1 is not the same
82 0 $self->{creator} = "MaTi";
83  
84 0 $self->{name} = "MaTirelire Data"; # Default
85 0 $self->{type} = "Data";
86  
87 # Add the standard AppInfo block stuff
88 0 &Palm::StdAppInfo::seed_StdAppInfo($self->{appinfo});
89  
90 0 return $self;
91 }
92  
93  
94 sub ParseAppInfoBlock
95 0 ▶{
Subroutine ParseAppInfoBlock: not called CC=1 SCAR=6.9
Pod documented
96 0 my $self = shift;
97 0 my $data = shift;
98 0 my $appinfo = {};
99 0 my $std_len;
100  
101 # Get the standard parts of the AppInfo block
102 0 $std_len = &Palm::StdAppInfo::parse_StdAppInfo($appinfo, $data);
103  
104 0 return $appinfo;
105 }
106  
107  
108 sub PackAppInfoBlock
109 0 ▶{
Subroutine PackAppInfoBlock: not called CC=1 SCAR=6.9
Pod documented
110 0 my $self = shift;
111 0 my $retval;
112  
113 # Pack the AppInfo block
114 0 $retval = &Palm::StdAppInfo::pack_StdAppInfo($self->{appinfo});
115  
116 0 return $retval;
117 }
118  
119  
120 sub new_Record
121 0 ▶{
Subroutine new_Record: not called CC=1 SCAR=6.9
Pod documented
122 0 my $classname = shift;
123 0 my $retval = $classname->SUPER::new_Record(@_);
124  
125 0 $TRANS_BLOCK->init_block($retval);
126 0 $TRANS_DESCRIPTION_BLOCK->init_block($retval);
127  
128 0 return $retval;
129 }
130  
131  
132 sub ParseRecord
133 0 ▶{
Subroutine ParseRecord: not called CC=5 SCAR=34.0
Pod documented
134 0 my $self = shift;
135 0 my %record = @_;
136  
137 0 delete $record{offset}; # This is useless
138 0 my $data = delete $record{data};
139  
140 0 $record{size} = length $data; # Used in validRecords method
141  
142 0 $TRANS_BLOCK->unpack_block(\$data, \%record, 1);
143  
144 # Cheque number
145 0 ▶ if (delete $record{check_num})
Branch: 1 branch
BranchTrueFalseTotal
if (delete $record{"check_num"}) 0 0 0
146 {
147 0 $TRANS_CHECKNUM_BLOCK->unpack_block(\$data, \%record, 1);
148 }
149  
150 # Value date
151 0 ▶ if (delete $record{value_date})
Branch: 1 branch
BranchTrueFalseTotal
if (delete $record{"value_date"}) 0 0 0
152 {
153 0 $record{value_date} = {};
154 0 $TRANS_VALUEDATE_BLOCK->unpack_block(\$data, $record{value_date}, 1);
155 }
156  
157 # Repetition
158 0 ▶ if (delete $record{repeat})
Branch: 1 branch
BranchTrueFalseTotal
if (delete $record{"repeat"}) 0 0 0
159 {
160 0 $record{repeat} = {};
161 0 $TRANS_REPEAT_BLOCK->unpack_block(\$data, $record{repeat}, 1);
162 }
163  
164 # Transfer
165 0 ▶ if (delete $record{xfer})
Branch: 1 branch
BranchTrueFalseTotal
if (delete $record{'xfer'}) { } 0 0 0
166 {
167 0 $TRANS_XFER_BLOCK->unpack_block(\$data, \%record, 1);
168 }
169 else
170 {
171 0 delete $record{xfer_cat};
172 }
173  
174 0 $TRANS_DESCRIPTION_BLOCK->unpack_block(\$data, \%record);
175  
176 #if (length($data) > 0)
177 #{
178 # use Data::Dumper;
179 #
180 # print Dumper(\%record);
181 # print Dumper($data);
182 #}
183  
184 0 return \%record;
185 }
186  
187  
188 sub PackRecord
189 0 ▶{
Subroutine PackRecord: not called CC=8 SCAR=42.8
Pod documented
190 0 my $self = shift;
191 0 my $record = shift;
192 0 my $pack;
193  
194 # Small check...
195 0 ▶ if ($record->{xfer_cat})
Branch: 1 branch
BranchTrueFalseTotal
if ($record->{"xfer_cat"}) 0 0 0
196 {
197 0 ▶ if (not defined $record->{xfer} or $record->{xfer} >= 16)
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $record->{"xfer"} or $record->{"xfer"} >= 16) 0 0 0
Condition0%0/3
not defined $record->{"xfer"} or $record->{"xfer"} >= 16
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $record->{"xfer"} or $record->{"xfer"} >= 16
not defined $record->{"xfer"} $record->{"xfer"} >= 16
Truth table0%0/3
not(A) or B
A= not defined $record->{"xfer"}B= $record->{"xfer"} >= 16
ABresult
1X1
011
000
198 0 { delete $record->{xfer_cat} }
199 }
200  
201 0 $pack = $TRANS_BLOCK->pack_block($record);
202  
203 # Cheque number
204 0 ▶ if ($record->{check_num})
Branch: 1 branch
BranchTrueFalseTotal
if ($record->{"check_num"}) 0 0 0
205 {
206 0 $pack .= $TRANS_CHECKNUM_BLOCK->pack_block($record);
207 }
208  
209 # Value date
210 0 ▶ if ($record->{value_date})
Branch: 1 branch
BranchTrueFalseTotal
if ($record->{"value_date"}) 0 0 0
211 {
212 0 $pack .= $TRANS_VALUEDATE_BLOCK->pack_block($record->{value_date});
213 }
214  
215 # Repetition
216 0 ▶ if ($record->{repeat})
Branch: 1 branch
BranchTrueFalseTotal
if ($record->{"repeat"}) 0 0 0
217 {
218 0 $pack .= $TRANS_REPEAT_BLOCK->pack_block($record->{repeat});
219 }
220  
221 # Transfer
222 0 ▶ if ($record->{xfer})
Branch: 1 branch
BranchTrueFalseTotal
if ($record->{"xfer"}) 0 0 0
223 {
224 0 $pack .= $TRANS_XFER_BLOCK->pack_block($record);
225 }
226  
227 0 $pack .= $TRANS_DESCRIPTION_BLOCK->pack_block($record);
228  
229 0 return $pack;
230 }
231  
232  
233 sub sortRecords
234 0 ▶{
Subroutine sortRecords: not called CC=1 SCAR=6.9
Pod undocumented
235 0 my $self = shift;
236  
237 0 @{$self->{records}} = sort
238 {
239 # Pack date and time on an 31 bits width integer...
240  
241 # 11 bits: 30 .. 20
242 (($a->{date_year} << 20)
243 # 4 bits: 19 .. 16
244 | ($a->{date_month} << 16)
245 # 5 bits: 15 .. 11
246 | ($a->{date_day} << 11)
247 # 5 bits: 10 .. 6
248 | ($a->{time_hour} << 6)
249 # 6 bits: 5 .. 0 11 bits: 30 .. 20
250 | $a->{time_min}) <=> (($b->{date_year} << 20)
251 # 4 bits: 19 .. 16
252 | ($b->{date_month} << 16)
253 # 5 bits: 15 .. 11
254 | ($b->{date_day} << 11)
255 # 5 bits: 10 .. 6
256 | ($b->{time_hour} << 6)
257 # 6 bits: 5 .. 0
258 | $b->{time_min})
259 0 }
260 0 @{$self->{records}};
261 }
262  
263  
264 #
265 # Returns a list (number of deleted records, number of errors corrected)
266 sub validRecords ($;$)
267 0 ▶{
Subroutine validRecords: not called CC=26 SCAR=65.5
Pod undocumented
268 0 my($self, $verbose) = @_;
269  
270 # $verbose can be a reference on a filehandle
271 0 ▶ $verbose = \*STDOUT if $verbose && not ref $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose and not ref $verbose 0 0 0
Condition0%0/3
$verbose and not ref $verbose
!ll&&!rl&&r
000
MC/DC0%0/2
$verbose and not ref $verbose
$verbose not ref $verbose
Truth table0%0/3
A and not(B)
A= $verboseB= not ref $verbose
ABresult
0X0
100
111
272  
273 0 my $deleted_records = 0;
274 0 my $errors_found = 0;
275  
276 0 my @to_del;
277 my %ids;
278 0 my $index = 0;
279  
280 0 foreach my $rec (@{$self->{records}})
281 {
282 0 ▶ if ($rec->{size} == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($rec->{'size'} == 0) { } 0 0 0
283 {
284 0 ▶ print $verbose ("Record #$index (cat=$rec->{category}) "
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
285 . "UniqueID $rec->{id}\n"
286 . "**** empty => deleted\n")
287 if $verbose;
288  
289 0 push(@to_del, $index);
290 }
291 else
292 {
293 0 $ids{$rec->{id}} = 1;
294 }
295 0 $index++;
296 }
297  
298 0 ▶ if (@to_del)
Branch: 1 branch
BranchTrueFalseTotal
if (@to_del) 0 0 0
299 {
300 0 $deleted_records = @to_del;
301  
302 0 foreach my $idx (reverse @to_del)
303 {
304 0 splice @{$self->{records}}, $idx, 1;
305 }
306 }
307  
308 0 my %links;
309 0 $index = 0;
310  
311 0 foreach my $rec (@{$self->{records}})
312 {
313 0 my @err_msg;
314  
315 # Repeat
316 0 ▶ if ($rec->{repeat})
Branch: 1 branch
BranchTrueFalseTotal
if ($rec->{"repeat"}) 0 0 0
317 {
318 0 ▶ if ($rec->{repeat}{repeat_freq} == 0
Branch: 1 branch
BranchTrueFalseTotal
if ($rec->{"repeat"}{"repeat_freq"} == 0 or $rec->{"repeat"}{"repeat_type"} > 2 or $rec->{"repeat"}{"reserved"} != 0) 0 0 0
Condition0%0/6
$rec->{"repeat"}{"repeat_freq"} == 0 or $rec->{"repeat"}{"repeat_type"} > 2 or $rec->{"repeat"}{"reserved"} != 0
l!l&&r!l&&!r
000
$rec->{"repeat"}{"repeat_freq"} == 0 or $rec->{"repeat"}{"repeat_type"} > 2
l!l&&r!l&&!r
000
MC/DC0%0/3
$rec->{"repeat"}{"repeat_freq"} == 0 or $rec->{"repeat"}{"repeat_type"} > 2 or $rec->{"repeat"}{"reserved"} != 0
$rec->{"repeat"}{"repeat_freq"} == 0 $rec->{"repeat"}{"repeat_type"} > 2 $rec->{"repeat"}{"reserved"} != 0
Truth table0%0/4
A or B or C
A= $rec->{"repeat"}{"repeat_freq"} == 0B= $rec->{"repeat"}{"repeat_type"} > 2C= $rec->{"repeat"}{"reserved"} != 0
ABCresult
1XX1
01X1
0011
0000
319 or $rec->{repeat}{repeat_type} > 2
320 or $rec->{repeat}{reserved} != 0)
321 {
322 0 push(@err_msg, "deleted repeat option");
323 0 delete $rec->{repeat};
324 }
325 }
326  
327 # Xfer
328 0 ▶ if (exists $rec->{xfer})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rec->{"xfer"}) 0 0 0
329 {
330 0 my $error = 0;
331  
332 0 ▶ if ($rec->{xfer_cat})
Branch: 1 branch
BranchTrueFalseTotal
if ($rec->{'xfer_cat'}) { } 0 0 0
333 {
334 0 ▶ if ($rec->{xfer} >= 16)
Branch: 1 branch
BranchTrueFalseTotal
if ($rec->{"xfer"} >= 16) 0 0 0
335 {
336 0 push(@err_msg, "invalid account (xfer) link");
337  
338 0 $error = 1;
339 }
340 }
341 else
342 {
343 0 ▶ if (exists $ids{$rec->{xfer}})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ids{$rec->{'xfer'}}) { } 0 0 0
344 {
345 0 $links{$rec->{id}} = $rec->{xfer};
346 }
347 else
348 {
349 0 push(@err_msg, "invalid transaction (xfer) link");
350  
351 0 $error = 1;
352 }
353 }
354  
355 0 ▶ if ($error)
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
356 {
357 0 delete @$rec{qw(xfer xfer_cat)};
358 0 push(@err_msg, "deleted transfer option");
359 }
360 }
361  
362 # No account (not possible ?)
363 0 ▶ if ($rec->{category} eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($rec->{"category"} eq "") 0 0 0
364 {
365 0 $rec->{attributes} = { dirty => 1 };
366 0 push(@err_msg, "not associated to an account");
367 }
368  
369 0 ▶ if (@err_msg)
Branch: 1 branch
BranchTrueFalseTotal
if (@err_msg) 0 0 0
370 {
371 0 ▶ if ($verbose)
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
372 {
373 print $verbose
374 ("Record #$index (account=$rec->{category}) "
375 . "UniqueID $rec->{id}\n"
376 . "$rec->{date_year}/$rec->{date_month}/$rec->{date_day} "
377 . "$rec->{time_min}:$rec->{time_hour} "
378 0 . "amount = ", $rec->{amount} / 100, "\n",
379 " ");
380  
381 0 print $verbose join("\n ", @err_msg), "\n";
382 }
383  
384 0 $rec->{attributes}{Dirty} = 1;
385  
386 0 $errors_found++;
387 }
388 }
389  
390 0 ▶ while (my($id, $link) = each %links)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %links) 0 0 0
391 {
392 0 ▶ if (not exists $links{$link})
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $links{$link}) { } 0 0 0
elsif ($links{$link} != $id) { } 0 0 0
393 {
394 0 ▶ print $verbose ("**** Xfer: $id => $link but $link is not linked,",
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
395 " corrected.\n")
396 if $verbose;
397  
398 0 my $rec = $self->findRecordByID($link);
399 0 $rec->{xfer} = $id;
400 0 delete $rec->{xfer_cat};
401  
402 0 $rec->{attributes}{Dirty} = 1;
403 }
404 elsif ($links{$link} != $id)
405 {
406 0 ▶ print $verbose
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
407 "**** Xfer: $id => $link but $link => $links{$link}\n"
408 if $verbose;
409 }
410 }
411  
412 0 return ($deleted_records, $errors_found);
413 }
414  
415 1 1;
416 __END__
« MaTirelire/AccountsV2.pm ↑ Summary MaTirelire/Types.pm »