blib/lib/Audio/Cuefile/Parser.pm

filter: Statementstmt 33.8% 23 / 68 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 7 / 14 Podpod 50.0% 1 / 2 totaltotal 26.5% 31 / 117 CC 18 SCAR 35.4
CC
18
Cov
23%
_parse_tracks
72.0
_parse_header
56.0
new
20.0
CRAP 34.6
↑ Summary
1 package Audio::Cuefile::Parser;
2  
3 =head1 NAME
4  
5 Audio::Cuefile::Parser
6  
7 =head1 VERSION
8  
9 Version 0.02
10  
11 =cut
12  
13 2 our $VERSION = '0.02';
14  
15 =head1 SYNOPSIS
16  
17 Class to parse a cuefile and access the chewy, nougat centre.
18 Returns Audio::Cuefile::Parser::Track objects.
19  
20 =head1 USAGE
21  
22 use Audio::Cuefile::Parser;
23  
24 my $filename = 'filename.cue';
25  
26 my $cue = Audio::Cuefile::Parser->new($filename);
27  
28 my ($audio_file, $cd_performer, $cd_title) =
29 ($cue->file, $cue->performer, $cue->title);
30  
31 foreach my $track ($cue->tracks) {
32  
33 my ($position, $index, $performer, $title) =
34 ($track->position, $track->index, $track->performer, $track->title);
35  
36 print "$position $index $performer $title";
37 }
38  
39 =cut
40  
41 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 2 ▶use Class::Struct qw/struct/;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 # Class specifications
49 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 struct 'Audio::Cuefile::Parser' => {
51 cuedata => '$',
52 cuefile => '$',
53 file => '$',
54 performer => '$',
55 title => '$',
56 _tracks => '@',
57 };
58  
59 2 struct 'Audio::Cuefile::Parser::Track' => {
60 index => '$',
61 performer => '$',
62 position => '$',
63 title => '$',
64 };
65 }
66  
67 {
68 # Over-ride Class::Struct's constructor so
69 # we can install some custom subs
70 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 0 ▶ sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod undocumented
73 0 ▶ my $class = shift or croak 'usage: '.__PACKAGE__.'->new($filename)';
Branch: 1 branch
BranchTrueFalseTotal
my $class = shift() or croak("usage: Audio::Cuefile::Parser->new(\$filename)") 0 0 0
74 0 ▶ my $cuefile = shift or croak 'no cue file specified';
Branch: 1 branch
BranchTrueFalseTotal
my $cuefile = shift() or croak("no cue file specified") 0 0 0
75 0 ▶ -e $cuefile or croak "$cuefile does not exist";
Branch: 1 branch
BranchTrueFalseTotal
-e $cuefile or croak("$cuefile does not exist") 0 0 0
76  
77 0 my $self = bless {}, $class;
78  
79 0 $self->cuefile($cuefile);
80  
81 0 $self->_loadcue;
82 0 $self->_parse;
83  
84 0 return $self;
85 }
86 }
87  
88 # Load .cue file's contents into memory
89 2 ▶sub _loadcue {
Subroutine _loadcue: not called CC=1 SCAR=4.1
90 0 my $self = shift;
91 0 my $cuefile = $self->cuefile;
92  
93 0 my $data = join "",
94 IO::File->new($cuefile, 'r')->getlines;
95  
96 0 $self->cuedata($data);
97 }
98  
99 # Parse text and dispatch headers and data into
100 # their respective methods
101 0 ▶sub _parse {
Subroutine _parse: not called CC=2 SCAR=17.9
102 0 my $self = shift;
103  
104 0 ▶ my $data = $self->cuedata or return;
Branch: 1 branch
BranchTrueFalseTotal
my $data = $self->cuedata or return 0 0 0
105  
106 0 my ($header, $tracks) = (
107 $data =~ m{
108 \A # start of string
109 (.*?) # capture all header text
110 (^ \s* TRACK .*) # capture all tracklist text
111 \z # end of string
112 }xms
113 );
114  
115 0 $self->_parse_header($header);
116 0 $self->_parse_tracks($tracks);
117 }
118  
119 # Process each <keyword> <value> pair and dispatch
120 # value to object mutator
121 0 ▶sub _parse_header {
Subroutine _parse_header: not called CC=7 SCAR=40.3
122 0 my ($self, $header) = @_;
123  
124 0 ▶ $header or return;
Branch: 1 branch
BranchTrueFalseTotal
$header or return 0 0 0
125  
126 0 my @lines = split /\r*\n/, $header;
127  
128  
129 LINE:
130 0 foreach my $line (@lines) {
131 0 _strip_spaces($line);
132  
133 0 ▶ $line =~ m/\S/ or next LINE;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /\S/ or next LINE 0 0 0
134  
135 0 my ($keyword, $data) = (
136 $line =~ m/
137 \A # anchor at string beginning
138 (\w+) # capture keyword (e.g. FILE, PERFORMER, TITLE)
139 \s+ ['"]? # optional quotes
140 (.*?) # capture all text as keyword's value
141 (?: # non-capture cluster
142 ['"] # quote, followed by
143 (?:
144 \s+ # spacing, followed by
145 \w+ # word (e.g. MP3, WAVE)
146 )? # make cluster optional
147 )?
148 \z # anchor at line end
149 /xms
150 );
151  
152 0 ▶ ($keyword && $data) or next LINE;
Branch: 1 branch
BranchTrueFalseTotal
$keyword and $data or next LINE 0 0 0
Condition0%0/3
$keyword and $data
!ll&&!rl&&r
000
MC/DC0%0/2
$keyword and $data
$keyword $data
Truth table0%0/3
A and B
A= $keywordB= $data
ABresult
0X0
100
111
153  
154 0 $keyword = lc $keyword;
155  
156 0 my %ISKEYWORD = map { $_ => 1 } qw/file performer title/;
157  
158 0 ▶ if ( $ISKEYWORD{$keyword} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ISKEYWORD{$keyword}) 0 0 0
159 # print "\$self->$keyword($data)\n";
160 0 $self->$keyword($data);
161 }
162 }
163 }
164  
165 # Walk through the track data, line by line,
166 # creating track objects and populating them
167 # as we go
168 0 ▶sub _parse_tracks {
Subroutine _parse_tracks: not called CC=8 SCAR=42.8
169 0 my ($self, $tracks) = @_;
170  
171 0 ▶ $tracks or return;
Branch: 1 branch
BranchTrueFalseTotal
$tracks or return 0 0 0
172  
173 0 my @lines = split /\r*\n/, $tracks;
174  
175 0 my @tracks;
176  
177 0 foreach my $line (@lines) {
178 0 _strip_spaces($line);
179  
180 # TRACK 01
181 # TRACK 02 AUDIO
182 0 ▶ $line =~ /\A TRACK \s+ (\d+) .* \z/xms
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /\A TRACK \s+ (\d+) .* \z/msx and push @tracks, "Audio::Cuefile::Parser::Track"->new("position", $1) 0 0 0
183 and push @tracks, Audio::Cuefile::Parser::Track->new(position => $1);
184  
185 0 ▶ next unless @tracks;
Branch: 1 branch
BranchTrueFalseTotal
unless @tracks 0 0 0
186  
187 # TITLE Track Name
188 # TITLE "Track Name"
189 # TITLE 'Track Name'
190 0 ▶ $line =~ /\A TITLE \s+ ['"]? (.*?) ['"]? \z/xms
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /\A TITLE \s+ ['"]? (.*?) ['"]? \z/msx and $tracks[-1]->title($1) 0 0 0
191 and $tracks[-1]->title($1);
192  
193 # PERFORMER Artist Name
194 # PERFORMER "Artist Name"
195 # PERFORMER 'Artist Name'
196 0 ▶ $line =~ /\A PERFORMER \s+ ['"]? (.*?) ['"]? \z/xms
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /\A PERFORMER \s+ ['"]? (.*?) ['"]? \z/msx and $tracks[-1]->performer($1) 0 0 0
197 and $tracks[-1]->performer($1);
198  
199 # INDEX 01 06:32:20
200 0 ▶ $line =~ /\A INDEX \s+ (?: \d+ \s+) ([\d:]+) \z/xms
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /\A INDEX \s+ (?: \d+ \s+) ([\d:]+) \z/msx and $tracks[-1]->index($1) 0 0 0
201 and $tracks[-1]->index($1);
202 }
203  
204 # Store them for safe keeping
205 0 $self->_tracks(\@tracks);
206 }
207  
208 0 ▶sub tracks {
Subroutine tracks: not called CC=1 SCAR=6.9
Pod documented
209 0 @{ shift->_tracks };
210 }
211  
212 # strip leading and trailing whitespace from input string
213 0 ▶sub _strip_spaces {
Subroutine _strip_spaces: not called CC=1 SCAR=6.9
214 0 $_[0] =~ s/
215 (?:
216 \A \s+
217 |
218 \s+ \z
219 )
220 //xms;
221 }
222  
223 =head1 CUEFILE METHODS
224  
225 =head2 $cue->tracks
226  
227 Returns a list of Audio::Cuefile::Parser::Track objects.
228  
229 =head2 $cue->file
230  
231 Returns the filename associated with the FILE keyword from
232 the .cue's headers (i.e. the audio file that the .cue file
233 is describing).
234  
235 =head2 $cue->performer
236  
237 The audio file's performer.
238  
239 =head2 $cue->title
240  
241 The title of the audio file.
242  
243 =head1 TRACK METHODS
244  
245 =head2 $track->index
246  
247 Timestamp that signifies the track's beginning.
248  
249 =head2 $track->performer
250  
251 The track's performer.
252  
253 =head2 $track->position
254  
255 The track's position in the audio file.
256  
257 =head2 $track->title
258  
259 Track title.
260  
261 =cut
262  
263 =head1 AUTHOR
264  
265 Matt Koscica <matt.koscica@gmail.com>
266  
267 =head1 BUGS
268  
269 Probably a few, the regexes are very simple.
270  
271 Please report any bugs or feature requests to
272 C<bug-audio-cuefile-parser@rt.cpan.org>, or through the web interface at
273 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Audio-Cuefile-Parser>.
274 I will be notified, and then you'll automatically be notified of progress on
275 your bug as I make changes.
276  
277 =head1 COPYRIGHT & LICENSE
278  
279 Copyright 2005-2010 Matt Koscica, all rights reserved.
280  
281 This program is free software; you can redistribute it and/or modify it
282 under the same terms as Perl itself.
283  
284 =cut
285  
286 2 1; # End of Audio::Cuefile::Parser
↑ Summary