NFB/Element.pm

filter: Statementstmt 61.7% 42 / 68 Branchbran 27.2% 6 / 22 Conditioncond 56.2% 9 / 16 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 71.4% 10 / 14 Podpod 100% 8 / 8 totaltotal 56.9% 78 / 137 CC 19 SCAR 18.1
CC
19
Cov
54%
pretty_print
12.0
timestamp
12.0
type
9.3
CRAP 6.1
↑ Summary NFB.pm »
1 package Nokia::File::NFB::Element;
2  
3 ## Create and Write a Nokia NFB file element.
4 ## Robert Price - http://www.robertprice.co.uk/
5  
6 3 ▶use 5.00503;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 $VERSION = '0.01';
15  
16  
17 ## new()
18 ## the object creation method
19 ## INPUT: type - the type of the entry (1 = file, 2 = directory)
20 ## name - the name of the entry
21 ## time - the timestamp of the entry
22 ## data - the binary data
23 ## OUTPUT: blessed hash representing the object
24 8 ▶sub new {
Subroutine new: called CC=6 SCAR=18.0
Pod documented
25 8 my $class = shift;
26 8 my $params = shift;
27 my $self = {
28 'type' => $params->{'type'} || '',
29 'name' => $params->{'name'} || '',
30 'timestamp' => $params->{'time'} || CORE::time, ## default to now if no time is given.
31 8 ▶ 'data' => $params->{'data'} || '',
Condition75%6/8
$params->{'type'} || ''
l!l
80
$params->{'name'} || ''
l!l
80
$params->{'time'} || time
l!l
26
$params->{'data'} || ''
l!l
44
MC/DC0%0/1
$params->{'type'} || ''
$params->{'type'}
MC/DC0%0/1
$params->{'name'} || ''
$params->{'name'}
MC/DC100%1/1
$params->{'time'} || time
$params->{'time'}
MC/DC100%1/1
$params->{'data'} || ''
$params->{'data'}
Truth table50%1/2
A
A= $params->{'type'}
Aresult
11
00
Truth table50%1/2
A
A= $params->{'name'}
Aresult
11
00
Truth table100%2/2
A
A= $params->{'time'}
Aresult
11
00
Truth table100%2/2
A
A= $params->{'data'}
Aresult
11
00
32 };
33 8 ▶ $self->{'size'} = length($self->{'data'}) || 0;
Condition100%2/2
length $self->{'data'} || 0
l!l
44
MC/DC100%1/1
length $self->{'data'} || 0
length $self->{'data'}
Truth table100%2/2
A
A= length $self->{'data'}
Aresult
11
00
34 8 bless $self, $class;
35 8 return $self;
36 }
37  
38  
39 #use overload '""' => \&pretty_print;
40 ## pretty_print()
41 ## display the contents of the object nicely
42 ## OUTPUT: string containing a version of the object suitable
43 ## for printing.
44 0 ▶sub pretty_print {
Subroutine pretty_print: not called CC=3 SCAR=24.8
Pod documented
45 0 my $self = shift;
46 0 my $string = "------------------------------------------------\n";
47 0 $string .= ' NAME: ' . $self->{'name'} . "\n";
48 0 $string .= ' TYPE: ' . $self->{'type'} . "\n";
49 0 ▶ if ($self->{'type'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} == 1) 0 0 0
50 0 $string .= 'SIZE: ' . $self->{'size'} . "\n";
51 0 ▶ if ($self->{'time'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"time"}) 0 0 0
52 0 $string .= 'TIME: ' . $self->{'time'} . ' - ' . localtime($self->{'time'}) . "\n";
53 }
54 }
55 0 $string .= "------------------------------------------------\n";
56 0 return $string;
57 }
58  
59  
60 ## type()
61 ## get or set the entry type for the element.
62 ## INPUT: type - the type (1 = file, 2 = directory) (optional)
63 ## OUTPUT: the type
64 4 ▶sub type {
Subroutine type: called CC=4 SCAR=22.3
Pod documented
65 4 my $self = shift;
66 4 ▶ if ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) 0 4 4
67 0 my $type = shift;
68 0 ▶ croak("Unknown type $type\nCan only be 1 or 2\n") unless ($type == 1 || $type ==2);
Branch: 1 branch
BranchTrueFalseTotal
unless $type == 1 or $type == 2 0 0 0
Condition0%0/3
$type == 1 or $type == 2
l!l&&r!l&&!r
000
MC/DC0%0/2
$type == 1 or $type == 2
$type == 1 $type == 2
Truth table0%0/3
A or B
A= $type == 1B= $type == 2
ABresult
1X1
011
000
69 0 $self->{'type'} = $type;
70 }
71 4 return $self->{'type'};
72 }
73  
74  
75 ## timestamp()
76 ## get or set the timestamp for the element.
77 ## INPUT: timestamp - creation time in seconds since epoch (optional)
78 ## OUTPUT: timestamp - the timestmap of the element.
79 0 ▶sub timestamp {
Subroutine timestamp: not called CC=3 SCAR=24.8
Pod documented
80 0 my $self = shift;
81 0 ▶ if ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) 0 0 0
82 0 my $time = shift;
83 0 ▶ croak("Unknown time format, must be seconds since epoch\n") unless ($time =~ /^\d+$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $time =~ /^\d+$/ 0 0 0
84 0 $self->{'time'} = $time;
85 }
86 0 return $self->{'time'};
87 }
88  
89  
90 ## name()
91 ## get or set the filename of the element.
92 ## INPUT: name - the name of the file / directory (optional).
93 ## OUTPUT: name - the name of the file / directory.
94 4 ▶sub name {
Subroutine name: called CC=2 SCAR=7.1
Pod documented
95 4 my $self = shift;
96 4 ▶ $self->{'name'} = $_[0] if ($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 0 4 4
97 4 return $self->{'name'};
98 }
99  
100  
101 ## data()
102 ## get or set the data in the element.
103 ## INPUT: data - the data of the file.
104 ## OUTPUT: data - the data of the file.
105 0 ▶sub data {
Subroutine data: not called CC=2 SCAR=17.9
Pod documented
106 0 my $self = shift;
107 0 ▶ if ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) 0 0 0
108 0 $self->{'data'} = $_[0];
109 0 $self->{'size'} = length($self->{'data'});
110 }
111 0 return $self->{'data'};
112 }
113  
114  
115 ## size()
116 ## get the size of the data.
117 ## OUTPUT: size - the size of the data.
118 0 ▶sub size {
Subroutine size: not called CC=1 SCAR=6.9
Pod documented
119 0 my $self = shift;
120 0 return $self->{'size'};
121 }
122  
123  
124 ## binary
125 ## return the object in a binary format suitable for insertion
126 ## in a NFB file.
127 ## OUTPUT: binary - the binary representation of the data.
128 2 ▶sub binary {
Subroutine binary: called CC=5 SCAR=16.5
Pod documented
129 2 my $self = shift;
130 2 my $binfile;
131  
132 croak("Need at least a name and type to generate binary element\n")
133 2 ▶ unless(($self->{'type'}) && ($self->{'name'}));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"type"} and $self->{"name"} 0 2 2
Condition33%1/3
$self->{"type"} and $self->{"name"}
!ll&&!rl&&r
002
MC/DC0%0/2
$self->{"type"} and $self->{"name"}
$self->{"type"} $self->{"name"}
Truth table33%1/3
A and B
A= $self->{"type"}B= $self->{"name"}
ABresult
0X0
100
111
134  
135 2 $binfile .= pack('V',$self->{'type'});
136 2 $binfile .= pack('V', length($self->{'name'}));
137 2 $binfile .= encode('UCS-2LE', $self->{'name'});
138
139 2 ▶ if ($self->{'type'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} == 1) 1 1 2
140 1 $binfile .= pack('V', length($self->{'data'}));
141 1 $binfile .= $self->{'data'};
142 1 ▶ $binfile .= pack('V', ($self->{'time'} ? $self->{'time'} : CORE::time()));
Branch: 1 branch
BranchTrueFalseTotal
$self->{'time'} ? : 0 1 1
143 }
144 2 return $binfile;
145 }
146  
147 3 1;
148 __END__
↑ Summary NFB.pm »