| 1 | package File::Format::RIFF; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use base File::Format::RIFF::Container; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | our $VERSION = '0.14'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | sub new | ||||||||||||||||||||||||||||
| 12 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 1 | my ( $proto, $type, $data ) = @_; | |||||||||||||||||||||||||||
| 14 | 1 | return $proto->SUPER::new( $type, 'RIFF', $data ); | |||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | sub read | ||||||||||||||||||||||||||||
| 19 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=8 SCAR=22.2
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 2 | my ( $proto ) = shift; | |||||||||||||||||||||||||||
| 21 | 2 | my ( $fh ) = shift; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | my ( $filesize ); | |||||||||||||||||||||||||||
| 24 | 2 | ▶ | if ( @_ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | { | ||||||||||||||||||||||||||||
| 26 | 1 | ( $filesize ) = @_; | |||||||||||||||||||||||||||
| 27 | 1 | ▶ | $filesize = 0+$filesize if ( defined $filesize ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | } else { | ||||||||||||||||||||||||||||
| 29 | 1 | $filesize = ( stat( $fh ) )[ 7 ]; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 2 | ▶ | croak 'Bad file: too small' if ( defined $filesize and $filesize < 12 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $filesize and $filesize < 12
MC/DC0%0/2
defined $filesize and $filesize < 12
defined $filesize
$filesize < 12
Truth table33%1/3
A and B
A= defined $filesizeB= $filesize < 12
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 2 | binmode( $fh ); | |||||||||||||||||||||||||||
| 34 | 2 | my ( $id ) = $proto->_read_fourcc( $fh ); | |||||||||||||||||||||||||||
| 35 | 2 | ▶ | croak "Bad file ($id)" unless ( $id eq 'RIFF' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2 | my ( $self ) = $proto->SUPER::read( 'RIFF', $fh ); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 2 | ▶ | croak "Bad file: expected $filesize bytes, got " . $self->total_size | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $filesize and $filesize != $self->total_size
MC/DC0%0/2
defined $filesize and $filesize != $self->total_size
defined $filesize
$filesize != $self->total_size
Truth table33%1/3
A and B
A= defined $filesizeB= $filesize != $self->total_size
| |||||||||||||||||||||||||||||
| 40 | if ( defined $filesize and $filesize != $self->total_size ); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 2 | return $self; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 3 | 1; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =pod | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =head1 NAME | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | File::Format::RIFF - Resource Interchange File Format/RIFF files | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | use File::Format::RIFF; | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | open( IN, 'file' ) or die "Could not open file: $!"; | ||||||||||||||||||||||||||||
| 60 | my ( $riff1 ) = File::Format::RIFF->read( \*IN ); | ||||||||||||||||||||||||||||
| 61 | close( IN ); | ||||||||||||||||||||||||||||
| 62 | $riff1->dump; | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | my ( $riff2 ) = new File::Format::RIFF( 'TYPE' ); | ||||||||||||||||||||||||||||
| 65 | foreach my $chunk ( $riff1->data ) | ||||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||
| 67 | next if ( $chunk->id eq 'LIST' ); | ||||||||||||||||||||||||||||
| 68 | $riff2->addChunk( $chunk->id, $chunk->data ); | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | open( OUT, ">otherfile" ) or die "Could not open file: $!"; | ||||||||||||||||||||||||||||
| 71 | $riff2->write( \*OUT ); | ||||||||||||||||||||||||||||
| 72 | close( OUT ); | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | C<File::Format::RIFF> provides an implementation of the Resource Interchange | ||||||||||||||||||||||||||||
| 77 | File Format. You can read, manipulate, and write RIFF files. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =head1 CONSTRUCTORS | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =over 4 | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =item $riff = new File::Format::RIFF( $type, $data ); | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | Creates a new File::Format::RIFF object. C<$type> is a four character code | ||||||||||||||||||||||||||||
| 86 | that identifies the type of this particular RIFF file. Certain types are | ||||||||||||||||||||||||||||
| 87 | defined to have a format, specifying which chunks must appear (e.g., WAVE | ||||||||||||||||||||||||||||
| 88 | files). If C<$type> is not specified, it defaults to C<' '> (four | ||||||||||||||||||||||||||||
| 89 | spaces). C<$data> must be an array reference containing some number of RIFF | ||||||||||||||||||||||||||||
| 90 | lists and/or RIFF chunks. If C<$data> is C<undef> or not specified, then | ||||||||||||||||||||||||||||
| 91 | the new RIFF object is initialized empty. | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =item $riff = File::Format::RIFF->read( $fh, $filesize ); | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | Reads and parses an existing RIFF file from the given filehandle C<$fh>. An | ||||||||||||||||||||||||||||
| 96 | exception will be thrown if the file is not a valid RIFF file. C<$filesize> | ||||||||||||||||||||||||||||
| 97 | controls one aspect of the file format checking -- if C<$filesize> is not | ||||||||||||||||||||||||||||
| 98 | specified, then C<stat> will be called on C<$fh> to determine how much data | ||||||||||||||||||||||||||||
| 99 | to expect. You may explicitly specify how much data to expect by passing | ||||||||||||||||||||||||||||
| 100 | in that value as C<$filesize>. In either case, the amount of data read will | ||||||||||||||||||||||||||||
| 101 | be checked to make sure it matches the amount expected. Otherwise, it will | ||||||||||||||||||||||||||||
| 102 | throw an exception. If you do not wish it to make this check, pass in C<undef> | ||||||||||||||||||||||||||||
| 103 | for C<$filesize>. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | Please note, if you wish to read an "in memory" filehandle, such as by doing | ||||||||||||||||||||||||||||
| 106 | this: C<open( $fh, 'E<lt>', \$variable )>, you may do so, but you must pass in | ||||||||||||||||||||||||||||
| 107 | C<length( $variable )> for C<$filesize>, because filehandles opened this way | ||||||||||||||||||||||||||||
| 108 | to do not support the C<stat> call. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | You may also use sockets for C<$fh>. But if you do, you must either specify | ||||||||||||||||||||||||||||
| 111 | the amount of data expected by passing in a value for C<$filesize>, or if you | ||||||||||||||||||||||||||||
| 112 | do not know ahead of time how much data to expect, you must pass in C<undef> | ||||||||||||||||||||||||||||
| 113 | for C<$filesize>. (An example of using sockets to read and write RIFF files | ||||||||||||||||||||||||||||
| 114 | is available with your File::Format::RIFF distribution, at eq/socketpair.pl.) | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =back | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head1 METHODS | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =over 4 | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =item $riff->read( $fh, $filesize ); | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | The C<read> constructor may also be used as a method. If used in this | ||||||||||||||||||||||||||||
| 125 | manner, then all existing data contained in C<$riff> will be discarded, and | ||||||||||||||||||||||||||||
| 126 | replaced by the contents read from C<$fh>. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =item $riff->write( $fh ); | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | Outputs a properly-formatted RIFF file to the given filehandle C<$fh>. | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =back | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =over 4 | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =item L<File::Format::RIFF::Container> | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | C<File::Format::RIFF> inherits from C<File::Format::RIFF::Container>, so all | ||||||||||||||||||||||||||||
| 141 | methods available for Containers can be used on RIFF objects. A Container | ||||||||||||||||||||||||||||
| 142 | essentially contains an array of RIFF lists and/or RIFF chunks. See | ||||||||||||||||||||||||||||
| 143 | the L<File::Format::RIFF::Container> page for more information. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =item L<File::Format::RIFF::List> | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =item L<File::Format::RIFF::Chunk> | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =back | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | The author monitors the CPAN forum at: | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | L<http://www.cpanforum.com/dist/File-Format-RIFF> | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | Paul Sturm E<lt>I<sturm@branewave.com>E<gt> | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head1 WEBSITE | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | L<http://branewave.com/perl> | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Copyright (c) 2005 Paul Sturm. All rights reserved. This program is free | ||||||||||||||||||||||||||||
| 168 | software; you can redistribute it and/or modify it under the same terms | ||||||||||||||||||||||||||||
| 169 | as Perl itself. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | I would love to hear about my software being used; send me an email! | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =cut | ||||||||||||||||||||||||||||