| line | stmt | bran | cond | sub | pod | time | code | 
| 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 |  |  | 3 |  | 86 | use 5.00503; | 
|  | 3 |  |  |  |  | 9 |  | 
|  | 3 |  |  |  |  | 108 |  | 
| 7 | 3 |  |  | 3 |  | 15 | use strict; | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 77 |  | 
| 8 | 3 |  |  | 3 |  | 14 | use utf8; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 22 |  | 
| 9 | 3 |  |  | 3 |  | 94 | use vars qw($VERSION); | 
|  | 3 |  |  |  |  | 4 |  | 
|  | 3 |  |  |  |  | 157 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 3 |  |  | 3 |  | 14 | use Carp; | 
|  | 3 |  |  |  |  | 12 |  | 
|  | 3 |  |  |  |  | 210 |  | 
| 12 | 3 |  |  | 3 |  | 13 | use Encode qw(encode); | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 2662 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | $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 |  |  |  |  |  |  | sub new { | 
| 25 | 8 |  |  | 8 | 1 | 43 | my $class = shift; | 
| 26 | 8 |  |  |  |  | 13 | my $params = shift; | 
| 27 | 8 |  | 50 |  |  | 179 | my $self = { | 
|  |  |  | 50 |  |  |  |  | 
|  |  |  | 66 |  |  |  |  | 
|  |  |  | 100 |  |  |  |  | 
| 28 |  |  |  |  |  |  | 'type'		=> $params->{'type'} || '', | 
| 29 |  |  |  |  |  |  | 'name'		=> $params->{'name'} || '', | 
| 30 |  |  |  |  |  |  | 'timestamp'	=> $params->{'time'} || CORE::time,	## default to now if no time is given. | 
| 31 |  |  |  |  |  |  | 'data'		=> $params->{'data'} || '', | 
| 32 |  |  |  |  |  |  | }; | 
| 33 | 8 |  | 100 |  |  | 41 | $self->{'size'} = length($self->{'data'}) || 0; | 
| 34 | 8 |  |  |  |  | 27 | bless $self, $class; | 
| 35 | 8 |  |  |  |  | 23 | 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 |  |  |  |  |  |  | sub pretty_print { | 
| 45 | 0 |  |  | 0 | 1 | 0 | my $self = shift; | 
| 46 | 0 |  |  |  |  | 0 | my $string = "------------------------------------------------\n"; | 
| 47 | 0 |  |  |  |  | 0 | $string .= ' NAME: ' . $self->{'name'} . "\n"; | 
| 48 | 0 |  |  |  |  | 0 | $string .= ' TYPE: ' . $self->{'type'} . "\n"; | 
| 49 | 0 | 0 |  |  |  | 0 | if ($self->{'type'} == 1) { | 
| 50 | 0 |  |  |  |  | 0 | $string .= 'SIZE: ' . $self->{'size'} . "\n"; | 
| 51 | 0 | 0 |  |  |  | 0 | if ($self->{'time'}) { | 
| 52 | 0 |  |  |  |  | 0 | $string .= 'TIME: ' . $self->{'time'} . ' - ' . localtime($self->{'time'}) . "\n"; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  | } | 
| 55 | 0 |  |  |  |  | 0 | $string .= "------------------------------------------------\n"; | 
| 56 | 0 |  |  |  |  | 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 |  |  |  |  |  |  | sub type { | 
| 65 | 4 |  |  | 4 | 1 | 1749 | my $self = shift; | 
| 66 | 4 | 50 |  |  |  | 15 | if ($_[0]) { | 
| 67 | 0 |  |  |  |  | 0 | my $type = shift; | 
| 68 | 0 | 0 | 0 |  |  | 0 | croak("Unknown type $type\nCan only be 1 or 2\n")	unless ($type == 1 || $type ==2); | 
| 69 | 0 |  |  |  |  | 0 | $self->{'type'} = $type; | 
| 70 |  |  |  |  |  |  | } | 
| 71 | 4 |  |  |  |  | 24 | 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 |  |  |  |  |  |  | sub timestamp { | 
| 80 | 0 |  |  | 0 | 1 | 0 | my $self = shift; | 
| 81 | 0 | 0 |  |  |  | 0 | if ($_[0]) { | 
| 82 | 0 |  |  |  |  | 0 | my $time = shift; | 
| 83 | 0 | 0 |  |  |  | 0 | croak("Unknown time format, must be seconds since epoch\n")	unless ($time =~ /^\d+$/); | 
| 84 | 0 |  |  |  |  | 0 | $self->{'time'} = $time; | 
| 85 |  |  |  |  |  |  | } | 
| 86 | 0 |  |  |  |  | 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 |  |  |  |  |  |  | sub name { | 
| 95 | 4 |  |  | 4 | 1 | 7 | my $self = shift; | 
| 96 | 4 | 50 |  |  |  | 12 | $self->{'name'}	= $_[0]	if ($_[0]); | 
| 97 | 4 |  |  |  |  | 20 | 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 |  |  |  |  |  |  | sub data { | 
| 106 | 0 |  |  | 0 | 1 | 0 | my $self = shift; | 
| 107 | 0 | 0 |  |  |  | 0 | if ($_[0]) { | 
| 108 | 0 |  |  |  |  | 0 | $self->{'data'}	= $_[0]; | 
| 109 | 0 |  |  |  |  | 0 | $self->{'size'} = length($self->{'data'}); | 
| 110 |  |  |  |  |  |  | } | 
| 111 | 0 |  |  |  |  | 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 |  |  |  |  |  |  | sub size { | 
| 119 | 0 |  |  | 0 | 1 | 0 | my $self = shift; | 
| 120 | 0 |  |  |  |  | 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 |  |  |  |  |  |  | sub binary { | 
| 129 | 2 |  |  | 2 | 1 | 3 | my $self = shift; | 
| 130 | 2 |  |  |  |  | 1 | my $binfile; | 
| 131 |  |  |  |  |  |  |  | 
| 132 | 2 | 50 | 33 |  |  | 16 | croak("Need at least a name and type to generate binary element\n") | 
| 133 |  |  |  |  |  |  | unless(($self->{'type'}) && ($self->{'name'})); | 
| 134 |  |  |  |  |  |  |  | 
| 135 | 2 |  |  |  |  | 4 | $binfile .= pack('V',$self->{'type'}); | 
| 136 | 2 |  |  |  |  | 4 | $binfile .= pack('V', length($self->{'name'})); | 
| 137 | 2 |  |  |  |  | 5 | $binfile .= encode('UCS-2LE', $self->{'name'}); | 
| 138 |  |  |  |  |  |  |  | 
| 139 | 2 | 100 |  |  |  | 35 | if ($self->{'type'} == 1) { | 
| 140 | 1 |  |  |  |  | 3 | $binfile .= pack('V', length($self->{'data'})); | 
| 141 | 1 |  |  |  |  | 2 | $binfile .= $self->{'data'}; | 
| 142 | 1 | 50 |  |  |  | 5 | $binfile .= pack('V', ($self->{'time'} ? $self->{'time'} : CORE::time())); | 
| 143 |  |  |  |  |  |  | } | 
| 144 | 2 |  |  |  |  | 8 | return $binfile; | 
| 145 |  |  |  |  |  |  | } | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | 1; | 
| 148 |  |  |  |  |  |  | __END__ |