RIFF/Chunk.pm

filter: Statementstmt 90.3% 75 / 83 Branchbran 57.1% 16 / 28 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 95.6% 22 / 23 Podpod 75.0% 6 / 8 totaltotal 76.4% 120 / 157 CC 18 SCAR 20.7
CC
18
Cov
77%
dump
42.0
_file_read
3.1
id
3.0
CRAP 7.9
↑ Summary RIFF.pm »
1 package File::Format::RIFF::Chunk;
2  
3  
4 3 our $VERSION = '0.09';
5  
6  
7 3 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 3 ▶use vars qw/ $PACKFMT /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶BEGIN { $PACKFMT = 'V' }
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 sub new
16 15 ▶{
Subroutine new: called CC=2 SCAR=6.9
Pod documented
17 15 my ( $proto, $id, $data ) = @_;
18 15 my ( $self ) = $proto->_new;
19 15 ▶ $self->id( defined $id ? $id : ' ' );
Branch: 1 branch
BranchTrueFalseTotal
defined $id ? : 14 1 15
20 15 $self->data( $data );
21 15 return $self;
22 }
23  
24  
25 sub _new
26 18 ▶{
Subroutine _new: called CC=2 SCAR=7.6
27 18 my ( $proto ) = @_;
28 18 ▶ my ( $class ) = ref( $proto ) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0180
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
29 18 return bless { }, $class;
30 }
31  
32  
33 sub write
34 6 ▶{
Subroutine write: called CC=1 SCAR=0.0
Pod undocumented
35 6 my ( $self, $fh ) = @_;
36 6 $self->_write_header( $fh );
37 6 $self->_write_data( $fh );
38 }
39  
40  
41 sub id
42 30 ▶{
Subroutine id: called CC=3 SCAR=11.0
Pod documented
43 30 my ( $self ) = shift;
44 30 ▶ return $self->{id} unless ( @_ );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 10 20 30
45 20 my ( $id ) = shift;
46 20 ▶ croak "Length of id must be 4" unless ( length( $id ) == 4 );
Branch: 1 branch
BranchTrueFalseTotal
unless length $id == 4 0 20 20
47 20 $self->{id} = $id;
48 }
49  
50  
51 sub data
52 26 ▶{
Subroutine data: called CC=3 SCAR=11.0
Pod documented
53 26 my ( $self ) = shift;
54 26 ▶ return $self->{data} unless ( @_ );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 13 13 26
55 13 my ( $data ) = shift;
56 13 ▶ $data = '' unless ( defined $data );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 1 12 13
57 13 $self->{size} = length( $self->{data} = $data );
58 }
59  
60  
61 sub size
62 23 ▶{
Subroutine size: called CC=1 SCAR=0.0
Pod documented
63 23 my ( $self ) = @_;
64 23 return $self->{size};
65 }
66  
67  
68 sub total_size
69 28 ▶{
Subroutine total_size: called CC=1 SCAR=0.0
Pod documented
70 28 my ( $self ) = @_;
71 28 my ( $sz ) = $self->size;
72 28 return 8 + $sz + ( $sz % 2 );
73 }
74  
75  
76 sub _read_header
77 4 ▶{
Subroutine _read_header: called CC=1 SCAR=0.0
78 4 my ( $self, $fh ) = @_;
79 4 $self->{size} = $self->_read_size( $fh );
80 }
81  
82  
83 sub _write_header
84 3 ▶{
Subroutine _write_header: called CC=1 SCAR=0.0
85 3 my ( $self, $fh ) = @_;
86 3 $self->_write_fourcc( $fh, $self->{id} );
87 3 $self->_write_size( $fh, $self->{size} );
88 }
89  
90  
91 sub _read_data
92 2 ▶{
Subroutine _read_data: called CC=2 SCAR=7.9
93 2 my ( $self, $fh ) = @_;
94 2 my ( $sz ) = $self->size;
95 2 $self->_file_read( $fh, \$self->{data}, $sz );
96 2 ▶ return unless ( $sz % 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless $sz % 2 2 0 2
97  
98 0 my ( $x );
99 0 $self->_file_read( $fh, \$x, 1 );
100 }
101  
102  
103 sub _write_data
104 3 ▶{
Subroutine _write_data: called CC=2 SCAR=7.1
105 3 my ( $self, $fh ) = @_;
106 3 $self->_file_write( $fh, $self->{data} );
107 3 ▶ $self->_file_write( $fh, "\0" ) if ( $self->{size} % 2 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"size"} % 2 0 3 3
108 }
109  
110  
111 sub _file_read
112 12 ▶{
Subroutine _file_read: called CC=3 SCAR=11.4
113 12 my ( $proto, $fh, $ref, $expect ) = @_;
114 12 my ( $got ) = read( $fh, $$ref, $expect );
115 12 ▶ croak "File read error: $!" unless ( defined $got );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $got 0 12 12
116 12 ▶ croak "File read error: expected $expect bytes, got $got"
Branch: 1 branch
BranchTrueFalseTotal
unless $got == $expect 0 12 12
117 unless ( $got == $expect );
118 }
119  
120  
121 sub _file_write
122 18 ▶{
Subroutine _file_write: called CC=2 SCAR=7.1
123 18 my ( $proto, $fh, @data ) = @_;
124 18 ▶ print { $fh } @data or croak 'Could not write to file';
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} @data or croak("Could not write to file") 18 0 18
125 }
126  
127  
128 sub _read_fourcc
129 6 ▶{
Subroutine _read_fourcc: called CC=1 SCAR=0.0
130 6 my ( $proto, $fh ) = @_;
131 6 my ( $fourcc );
132 6 $proto->_file_read( $fh, \$fourcc, 4 );
133 6 return $fourcc;
134 }
135  
136  
137 sub _write_fourcc
138 9 ▶{
Subroutine _write_fourcc: called CC=1 SCAR=0.0
139 9 my ( $self, $fh, $fourcc ) = @_;
140 9 $self->_file_write( $fh, $fourcc );
141 }
142  
143  
144 sub _read_size
145 4 ▶{
Subroutine _read_size: called CC=1 SCAR=0.0
146 4 my ( $proto, $fh ) = @_;
147 4 my ( $size );
148 4 $proto->_file_read( $fh, \$size, 4 );
149 4 return unpack( $PACKFMT, $size );
150 }
151  
152  
153 sub _write_size
154 6 ▶{
Subroutine _write_size: called CC=1 SCAR=0.0
155 6 my ( $self, $fh, $size ) = @_;
156 6 $self->_file_write( $fh, pack( $PACKFMT, $size ) );
157 }
158  
159  
160 sub dump
161 0 ▶{
Subroutine dump: not called CC=6 SCAR=37.4
Pod documented
162 0 my ( $self, $max, $indent ) = @_;
163 0 ▶ $max = 64 unless ( defined $max );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max 0 0 0
164 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
165 0 print join( '', "\t" x $indent ), 'id: ', $self->id,
166 ' size: ', $self->size, ' (', $self->total_size, '): ';
167 0 ▶ ( $max and $self->size > $max ) ? print '[...]' : print $self->{data};
Branch: 1 branch
BranchTrueFalseTotal
$max && $self->size > $max ? : 0 0 0
Condition0%0/3
$max and $self->size > $max
!ll&&!rl&&r
000
MC/DC0%0/2
$max and $self->size > $max
$max $self->size > $max
Truth table0%0/3
A and B
A= $maxB= $self->size > $max
ABresult
0X0
100
111
168 0 print "\n";
169 }
170  
171  
172 sub read
173 4 ▶{
Subroutine read: called CC=2 SCAR=6.9
Pod undocumented
174 4 my ( $proto, $id, $fh ) = @_;
175 4 ▶ my ( $self ) = ref( $proto ) ? $proto : $proto->_new;
Branch: 1 branch
BranchTrueFalseTotal
ref $proto ? : 1 3 4
176 4 $self->id( $id );
177 4 $self->_read_header( $fh );
178 4 $self->_read_data( $fh );
179 4 return $self;
180 }
181  
182  
183 3 1;
184  
185  
186 =pod
187  
188 =head1 NAME
189  
190 File::Format::RIFF::Chunk - a single RIFF chunk
191  
192 =head1 SYNOPSIS
193  
194 use File::Format::RIFF;
195  
196 my ( $chunk ) = new File::Format::RIFF::Chunk;
197 $chunk->id( 'stuf' );
198 $chunk->data( 'here is some stuff' );
199  
200 ... some $container ...
201  
202 $container->push( $chunk );
203  
204 =head1 DESCRIPTION
205  
206 A C<File::Format::RIFF::Chunk> is a single chunk of data in a RIFF file. It
207 has an identifier and one piece of scalar data. The id must be a
208 four character code, and the data can be any piece of scalar data you wish
209 to store, in any format (it is treated as opaque binary data, so you must
210 interpret it yourself).
211  
212 =head1 CONSTRUCTOR
213  
214 =over 4
215  
216 =item $chunk = new File::Format::RIFF::Chunk( $id, $data );
217  
218 Creates a new File::Format::RIFF::Chunk object. C<$id> is a four character
219 code that identifies the type of data. If C<$id> is not specified, it
220 defaults to C<' '> (four spaces). C<$data> is a scalar, treated as
221 opaque binary data. If C<$data> is C<undef> or not specified, it defaults
222 to ''.
223  
224 =back
225  
226 =head1 METHODS
227  
228 =over 4
229  
230 =item $id = $chunk->id;
231  
232 Returns the id of C<$chunk>.
233  
234 =item $chunk->id( 'abcd' );
235  
236 Sets the id of C<$chunk>. C<$id> must be a four character code that
237 identifies the type of data.
238  
239 =item $data = $chunk->data;
240  
241 Returns the data of C<$chunk>.
242  
243 =item $chunk->data( $data );
244  
245 Sets the data of C<$chunk>. C<$data> is treated as a piece of opaque
246 binary data, not modified or interpreted in any way.
247  
248 =item $size = $chunk->size;
249  
250 Returns the size (in bytes) of C<$chunk>'s data.
251  
252 =item $total_size = $chunk->total_size;
253  
254 Returns the total size (in bytes) that C<$chunk> will take up when written
255 out to a file. Total size is the size of the data, plus 8 bytes for the
256 header, plus 1 alignment byte if the data has an odd number of bytes (so that
257 the RIFF chunks will be word-aligned on disk).
258  
259 =item $chunk->dump( $max );
260  
261 Prints a string representation of C<$chunk> to STDOUT. If the data is
262 larger than C<$max> bytes, prints '[...]' instead of the actual data. If
263 C<$max> is not specified or C<undef>, it defaults to 64.
264  
265 A RIFF chunk is rendered as:
266  
267 id: E<lt>idE<gt> size: E<lt>sizeE<gt> (E<lt>total sizeE<gt>): E<lt>dataE<gt>
268  
269 =back
270  
271 =head1 AUTHOR
272  
273 Paul Sturm E<lt>I<sturm@branewave.com>E<gt>
274  
275 =cut
↑ Summary RIFF.pm »