File Coverage

blib/lib/File/Format/RIFF.pm
Criterion Covered Total %
statement 25 25 100.0
branch 6 10 60.0
condition 2 6 33.3
subroutine 5 5 100.0
pod 2 2 100.0
total 40 48 83.3


line stmt bran cond sub pod time code
1             package File::Format::RIFF;
2 3     3   23676 use base File::Format::RIFF::Container;
  3         5  
  3         8619  
3              
4              
5 3     3   101 use 5.006;
  3         10  
  3         135  
6 3     3   16 use Carp;
  3         6  
  3         1157  
7              
8             our $VERSION = '0.14';
9              
10              
11             sub new
12             {
13 1     1 1 2 my ( $proto, $type, $data ) = @_;
14 1         8 return $proto->SUPER::new( $type, 'RIFF', $data );
15             }
16              
17              
18             sub read
19             {
20 2     2 1 57 my ( $proto ) = shift;
21 2         6 my ( $fh ) = shift;
22              
23 2         2 my ( $filesize );
24 2 100       6 if ( @_ )
25             {
26 1         2 ( $filesize ) = @_;
27 1 50       4 $filesize = 0+$filesize if ( defined $filesize );
28             } else {
29 1         13 $filesize = ( stat( $fh ) )[ 7 ];
30             }
31 2 50 33     14 croak 'Bad file: too small' if ( defined $filesize and $filesize < 12 );
32              
33 2         6 binmode( $fh );
34 2         15 my ( $id ) = $proto->_read_fourcc( $fh );
35 2 50       8 croak "Bad file ($id)" unless ( $id eq 'RIFF' );
36              
37 2         13 my ( $self ) = $proto->SUPER::read( 'RIFF', $fh );
38              
39 2 50 33     12 croak "Bad file: expected $filesize bytes, got " . $self->total_size
40             if ( defined $filesize and $filesize != $self->total_size );
41              
42 2         5 return $self;
43             }
44              
45              
46             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 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 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 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
103             for C<$filesize>.
104              
105             Please note, if you wish to read an "in memory" filehandle, such as by doing
106             this: C', \$variable )>, you may do so, but you must pass in
107             C for C<$filesize>, because filehandles opened this way
108             to do not support the C 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
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 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
139              
140             C inherits from C, 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 page for more information.
144              
145             =item L
146              
147             =item L
148              
149             =back
150              
151             =head1 SUPPORT
152              
153             The author monitors the CPAN forum at:
154              
155             L
156              
157             =head1 AUTHOR
158              
159             Paul Sturm EIE
160              
161             =head1 WEBSITE
162              
163             L
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