RIFF/Container.pm

filter: Statementstmt 91.1% 93 / 102 Branchbran 70.5% 24 / 34 Conditioncond 41.6% 5 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 95.8% 23 / 24 Podpod 94.1% 16 / 17 totaltotal 82.2% 162 / 197 CC 22 SCAR 14.8
CC
22
Cov
82%
dump
20.0
splice
6.7
_read_data
4.0
CRAP 4.4
« RIFF.pm ↑ Summary RIFF/List.pm »
1 package File::Format::RIFF::Container;
2 3 ▶use base File::Format::RIFF::Chunk;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4  
5 3 our $VERSION = '0.08';
6  
7  
8 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Format::RIFF::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 sub new
13 4 ▶{
Subroutine new: called CC=2 SCAR=6.9
Pod documented
14 4 my ( $proto, $type, $id, $data ) = @_;
15 4 my ( $self ) = $proto->SUPER::new( $id, $data );
16 4 ▶ $self->type( defined $type ? $type : ' ' );
Branch: 1 branch
BranchTrueFalseTotal
defined $type ? : 2 2 4
17 4 return $self;
18 }
19  
20  
21 sub type
22 16 ▶{
Subroutine type: called CC=3 SCAR=11.0
Pod documented
23 16 my ( $self ) = shift;
24 16 ▶ return $self->{type} unless ( @_ );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 9 7 16
25 7 my ( $type ) = shift;
26 7 ▶ croak "Length of type must be 4" unless ( length( $type ) == 4 );
Branch: 1 branch
BranchTrueFalseTotal
unless length $type == 4 0 7 7
27 7 $self->{type} = $type;
28 }
29  
30  
31 sub id
32 8 ▶{
Subroutine id: called CC=3 SCAR=11.4
Pod documented
33 8 my ( $self ) = shift;
34 8 ▶ croak "Cannot set id of $self->{id} chunk" if ( @_ and exists $self->{id} );
Branch: 1 branch
BranchTrueFalseTotal
if @_ and exists $self->{"id"} 0 8 8
Condition66%2/3
@_ and exists $self->{"id"}
!ll&&!rl&&r
260
MC/DC0%0/2
@_ and exists $self->{"id"}
@_ exists $self->{"id"}
Truth table66%2/3
A and B
A= @_B= exists $self->{"id"}
ABresult
0X0
100
111
35 8 return $self->SUPER::id( @_ );
36 }
37  
38  
39 sub read
40 2 ▶{
Subroutine read: called CC=2 SCAR=6.9
Pod undocumented
41 2 my ( $proto ) = shift;
42 2 ▶ delete $proto->{id} if ( ref( $proto ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $proto 1 1 2
43 2 return $proto->SUPER::read( @_ );
44 }
45  
46  
47 sub total_size
48 9 ▶{
Subroutine total_size: called CC=1 SCAR=0.0
Pod documented
49 9 my ( $self ) = @_;
50 9 return $self->SUPER::total_size + 4;
51 }
52  
53  
54 sub data
55 7 ▶{
Subroutine data: called CC=4 SCAR=13.9
Pod documented
56 7 my ( $self ) = shift;
57 7 ▶ return @{ $self->{data} } unless ( @_ );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 2 5 7
58 5 my ( $data ) = @_;
59 5 ▶ $data = [ ] unless ( defined $data and ref( $data ) eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data and ref $data eq "ARRAY" 2 3 5
Condition66%2/3
defined $data and ref $data eq "ARRAY"
!ll&&!rl&&r
203
MC/DC50%1/2
defined $data and ref $data eq "ARRAY"
defined $data ref $data eq "ARRAY"
Truth table66%2/3
A and B
A= defined $dataB= ref $data eq "ARRAY"
ABresult
0X0
100
111
60 5 $self->{data} = [ ];
61 5 $self->push( @$data );
62 }
63  
64  
65 sub numChunks
66 6 ▶{
Subroutine numChunks: called CC=1 SCAR=0.0
Pod documented
67 6 my ( $self ) = @_;
68 6 return scalar( @{ $self->{data} } );
69 }
70  
71  
72 sub size
73 15 ▶{
Subroutine size: called CC=1 SCAR=0.0
Pod documented
74 15 my ( $self ) = @_;
75 15 my ( $sz ) = 0;
76 15 map { $sz += $_->total_size } @{ $self->{data} };
77 15 return $sz;
78 }
79  
80  
81 sub splice
82 17 ▶{
Subroutine splice: called CC=6 SCAR=19.0
Pod documented
83 17 my ( $self, $offset, $length, @elts ) = @_;
84 17 ▶ map { croak "Can only add Chunk or List elements"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ and $_->isa("File::Format::RIFF::Chunk") 0 15 15
Condition33%1/3
ref $_ and $_->isa("File::Format::RIFF::Chunk")
!ll&&!rl&&r
0015
MC/DC0%0/2
ref $_ and $_->isa("File::Format::RIFF::Chunk")
ref $_ $_->isa("File::Format::RIFF::Chunk")
Truth table33%1/3
A and B
A= ref $_B= $_->isa("File::Format::RIFF::Chunk")
ABresult
0X0
100
111
85 unless ( ref( $_ ) and $_->isa( 'File::Format::RIFF::Chunk' ) ) } @elts;
86 return ( @_ > 3 )
87 12 ▶ ? splice( @{ $self->{data} }, $offset, $length, @elts )
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 ? : 4 1 5
88 : ( @_ == 3 )
89 4 ▶ ? splice( @{ $self->{data} }, $offset, $length )
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 1 0 1
90 : ( @_ == 2 )
91 1 ? splice( @{ $self->{data} }, $offset )
92 0 ▶ : splice( @{ $self->{data} } );
Branch: 1 branch
BranchTrueFalseTotal
@_ > 3 ? : 12 5 17
93 }
94  
95  
96 sub push
97 12 ▶{
Subroutine push: called CC=1 SCAR=0.0
Pod documented
98 12 my ( $self, @elts ) = @_;
99 12 return $self->splice( scalar( @{ $self->{data} } ), 0, @elts );
100 }
101  
102  
103 sub pop
104 1 ▶{
Subroutine pop: called CC=1 SCAR=0.0
Pod documented
105 1 my ( $self ) = @_;
106 1 return $self->splice( -1 );
107 }
108  
109  
110 sub unshift
111 1 ▶{
Subroutine unshift: called CC=1 SCAR=0.0
Pod documented
112 1 my ( $self, @elts ) = @_;
113 1 return $self->splice( 0, 0, @elts );
114 }
115  
116  
117 sub at
118 14 ▶{
Subroutine at: called CC=2 SCAR=6.9
Pod documented
119 14 my ( $self ) = shift;
120 14 my ( $i ) = shift;
121 14 ▶ return $self->splice( $i, 1, shift ) if ( @_ );
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 13 14
122 13 return $self->{data}->[ $i ];
123 }
124  
125  
126 sub addChunk
127 3 ▶{
Subroutine addChunk: called CC=1 SCAR=0.0
Pod documented
128 3 my ( $self ) = shift;
129 3 my ( $chk ) = new File::Format::RIFF::Chunk( @_ );
130 3 $self->push( $chk );
131 3 return $chk;
132 }
133  
134  
135 sub addList
136 1 ▶{
Subroutine addList: called CC=1 SCAR=0.0
Pod documented
137 1 my ( $self ) = shift;
138 1 my ( $ctr ) = new File::Format::RIFF::List( @_ );
139 1 $self->push( $ctr );
140 1 return $ctr;
141 }
142  
143  
144 sub _read_header
145 2 ▶{
Subroutine _read_header: called CC=1 SCAR=0.0
146 2 my ( $self, $fh ) = @_;
147 2 $self->SUPER::_read_header( $fh );
148 2 $self->{size} -= 4;
149 2 $self->{type} = $self->_read_fourcc( $fh );
150 }
151  
152  
153 sub _write_header
154 3 ▶{
Subroutine _write_header: called CC=1 SCAR=0.0
155 3 my ( $self, $fh ) = @_;
156 3 $self->_write_fourcc( $fh, $self->{id} );
157 3 $self->_write_size( $fh, $self->size + 4 );
158 3 $self->_write_fourcc( $fh, $self->{type} );
159 }
160  
161  
162 sub _read_data
163 2 ▶{
Subroutine _read_data: called CC=4 SCAR=14.0
164 2 my ( $self, $fh ) = @_;
165 2 my ( $to_read ) = $self->{size};
166 2 $self->{data} = [ ];
167 2 ▶ while ( $to_read )
Branch: 1 branch
BranchTrueFalseTotal
if ($to_read) 2 2 4
168 {
169 2 my ( $id ) = $self->_read_fourcc( $fh );
170 2 ▶ croak "Embedded RIFF chunks not allowed" if ( $id eq 'RIFF' );
Branch: 1 branch
BranchTrueFalseTotal
if $id eq "RIFF" 0 2 2
171 2 ▶ my ( $subchunk ) = ( $id eq 'LIST' )
Branch: 1 branch
BranchTrueFalseTotal
$id eq 'LIST' ? : 0 2 2
172 ? File::Format::RIFF::List->read( $fh )
173 : File::Format::RIFF::Chunk->read( $id, $fh );
174 2 $to_read -= $subchunk->total_size;
175 2 $self->push( $subchunk );
176 }
177 }
178  
179  
180 sub _write_data
181 3 ▶{
Subroutine _write_data: called CC=1 SCAR=0.0
182 3 my ( $self, $fh ) = @_;
183 3 map { $_->write( $fh ) } @{ $self->{data} };
184 }
185  
186  
187 sub dump
188 0 ▶{
Subroutine dump: not called CC=4 SCAR=30.0
Pod documented
189 0 my ( $self, $max, $indent ) = @_;
190 0 ▶ $max = 64 unless ( defined $max );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max 0 0 0
191 0 ▶ $indent = 0 unless ( defined $indent and $indent > 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $indent and $indent > 0 0 0 0
Condition0%0/3
defined $indent and $indent > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $indent and $indent > 0
defined $indent $indent > 0
Truth table0%0/3
A and B
A= defined $indentB= $indent > 0
ABresult
0X0
100
111
192 0 print join( '', "\t" x $indent ), 'id: ', $self->id, ' (',
193 $self->type, ') size: ', $self->size, ' (', $self->total_size, ")\n";
194  
195 0 ++ $indent;
196 0 map { $_->dump( $max, $indent ) } @{ $self->{data} };
197 }
198  
199  
200 sub shift
201 1 ▶{
Subroutine shift: called CC=1 SCAR=0.0
Pod documented
202 1 my ( $self ) = @_;
203 1 return $self->splice( 0, 1 );
204 }
205  
206  
207 3 1;
208  
209  
210 =pod
211  
212 =head1 NAME
213  
214 File::Format::RIFF::Container - RIFF Container (Lists and RIFFs)
215  
216 =head1 SYNOPSIS
217  
218 You should not instantiate a C<File::Format::RIFF::Container> directly;
219 instead, you should instantiate one of its subclasses: either a
220 L<File::Format::RIFF> object, or a L<File::Format::RIFF::List> object.
221  
222 =head1 DESCRIPTION
223  
224 C<File::Format::RIFF::Container> is a base class for both RIFF objects
225 and RIFF lists. It is, essentially, an array of other RIFF lists and/or
226 RIFF chunks, and you can add, change, delete, and read them.
227  
228 =head1 METHODS
229  
230 =over 4
231  
232 =item $type = $container->type;
233  
234 Returns the type of C<$container>.
235  
236 =item $container->type( $type );
237  
238 Sets the type of C<$container>. C<$type> must be a four character code, which
239 represents what data will be found in C<$container>.
240  
241 =item $id = $container->id;
242  
243 Returns the id of C<$container>. C<$container> must be either a RIFF object
244 or a List object, so C<$id> will be 'RIFF' or 'LIST', respectively.
245  
246 =item @data = $container->data;
247  
248 Returns the RIFF chunks and/or RIFF lists contained by C<$container>.
249  
250 =item $container->data( $data );
251  
252 Clears out any existing RIFF chunks contained by C<$container> and replaces
253 them with C<$data>. C<$data> must be an array reference containing some
254 number of RIFF lists and/or RIFF chunks.
255  
256 =item $numChunks = $container->numChunks;
257  
258 Returns the number of RIFF lists and/or RIFF chunks contained by
259 C<$container>.
260  
261 =item $size = $container->size;
262  
263 Returns the size (in bytes) of C<$container>'s data, when written to a file.
264  
265 =item $total_size = $container->total_size;
266  
267 Returns the total size (in bytes) that C<$container> will take up when
268 written out to a file. Total size is the size of the data, plus 12 bytes
269 for the header.
270  
271 =item @replaced = $self->splice( $offset, $length, @list );
272  
273 =item $container->push( @chunks );
274  
275 =item $chunk = $container->pop;
276  
277 =item $container->unshift( @chunks );
278  
279 =item $chunk = $container->shift;
280  
281 C<splice>, C<push>, C<pop>, C<unshift>, and C<shift> operate analogously
282 to the same-named functions in core perl, acting on C<$container>'s array
283 of RIFF lists and/or RIFF chunks. All items added must be RIFF lists or
284 RIFF chunks.
285  
286 =item $chunk = $container->at( $i );
287  
288 Returns the RIFF list or RIFF chunk at the C<$i>th position in
289 C<$container>'s array.
290  
291 =item $container->at( $i, $chunk );
292  
293 Sets the C<$i>th position in C<$container>'s array to C<$chunk>, replacing
294 the previous item. C<$chunk> must be a RIFF list or a RIFF chunk.
295  
296 =item $newChunk = $container->addChunk( $id, $data );
297  
298 Creates a new RIFF chunk object with the given C<$id> and C<$data>, appending
299 it to C<$container>'s array. Returns the just-created RIFF chunk.
300  
301 =item $newList = $container->addList( $type, $data );
302  
303 Creates a new List object with the given C<$type> and C<$data>, appending
304 it to C<$container>'s array. Returns the just-created RIFF list.
305  
306 =item $container->dump( $max );
307  
308 Prints a string representation of C<$container> to STDOUT, recursively
309 printing contained items. If a RIFF chunk's data is larger than C<$max> bytes,
310 prints '[...]' instead of the actual data. If C<$max> is not specified or
311 C<undef>, it defaults to 64.
312  
313 A RIFF chunk is rendered as:
314  
315 id: E<lt>idE<gt> size: E<lt>sizeE<gt> (E<lt>total sizeE<gt>): E<lt>dataE<gt>
316  
317 A RIFF container is rendered as:
318  
319 id: E<lt>idE<gt> (E<lt>typeE<gt>) size: E<lt>sizeE<gt> (E<lt>total sizeE<gt>)
320  
321 Items contained in the RIFF list are recursively printed on subsequent lines,
322 and are indented in one additional tab level.
323  
324 =back
325  
326 =head1 SEE ALSO
327  
328 =over 4
329  
330 =item L<File::Format::RIFF>
331  
332 =item L<File::Format::RIFF::List>
333  
334 =back
335  
336 =head1 AUTHOR
337  
338 Paul Sturm E<lt>I<sturm@branewave.com>E<gt>
339  
340 =cut
« RIFF.pm ↑ Summary RIFF/List.pm »