blib/lib/CDDB/Fake.pm

filter: Statementstmt 94.8% 130 / 137 Branchbran 84.4% 49 / 58 Conditioncond 75.0% 6 / 8 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 79.3% 23 / 29 Podpod 93.3% 14 / 15 totaltotal 89.2% 225 / 252 CC 36 SCAR 18.5
CC
36
Cov
91%
new
17.1
as_cddb
6.0
_extd_append
3.0
CRAP 6.3
↑ Summary
1 # CDDB::Fake.pm -- CDDB File Faker
2 # RCS Info : $Id: CDDB-Fake.pm,v 1.5 2004/08/13 13:38:04 jv Exp $
3 # Author : Johan Vromans
4 # Created On : Tue Mar 25 22:38:32 2003
5 # Last Modified By: Johan Vromans
6 # Last Modified On: Fri Aug 13 15:28:55 2004
7 # Update Count : 139
8 # Status : Unknown, Use with caution!
9  
10 =head1 NAME
11  
12 CDDB::Fake - Fake CDDB entries if you have none
13  
14 =head1 SYNOPSIS
15  
16 use CDDB::Fake;
17 my $cddb = CDDB::Fake->new("music/Egg/Egg/.nocddb");
18 print "Artist: ", $cddb->artist, "\n";
19 foreach my $track ( $cddb->tracks ) {
20 print "Track ", $track->number, ": ", $track->title, "\n";
21 }
22  
23 =head1 DESCRIPTION
24  
25 Sometimes there's no CDDB file available for a piece of music. For
26 example, when you created a collection of tracks from other albums. In
27 this case, a text file containing the name of the artist / album,
28 followed by a mere list of the track titles can be used as a
29 fall-back.
30  
31 CDDB::Fake implements a part of the CDDB::File API based on manually
32 crafted fall-back files.
33  
34 I've adopted the convention to name files with CDDB data C<.cddb>, and
35 the fake data C<.nocddb>.
36  
37 For example, you can cut the results of a search at Gracenote
38 (cddb.com) and paste it into the file .nocddb. For example:
39  
40 Birelli Lagrene / Standards
41  
42 1. C'est Si Bon
43 2. Softly, As in a Morning Sunrise
44 3. Days of Wine and Roses
45 ...
46 12. Nuages
47  
48 The track titles may be optionally followed by trailing TABs (not
49 spaces) and a MM:SS time indicator (which may have a leading space if
50 it's M:SS).
51  
52 Extra track information can be passed on lines that follow the track
53 title. These lines must start with whitespace, and may not begin with
54 a number. Anything that follows the list of tracks is considered extra disc information. For example:
55  
56 Birelli Lagrene / Standards
57  
58 1. C'est Si Bon
59 Original version
60 2. Softly, As in a Morning Sunrise
61 Live recording.
62 Probably incomplete.
63 3. Days of Wine and Roses
64 ...
65 12. Nuages
66  
67 This album was recorded in the Olympia Studios in Paris.
68  
69 Multiple lines of additional info are concatenated with newlines
70 inbetween. However, if one of the lines contains C<\n> (that's
71 backslash-n), all lines are conctenated using a single whitespace, and
72 the C<\n>'s are turned into real newlines.
73  
74 A tool is included to generate a fake file from the names of the files
75 in the directory.
76  
77 B<WARNING:> CDDB::Fake implements only a part of the CDDB::File API.
78  
79 =cut
80  
81 package CDDB::Fake;
82  
83 4 $VERSION = "2.00";
84  
85 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
88  
89 =head1 METHODS
90  
91 =over 4
92  
93 =item new I<file>
94  
95 The new() package method takes the name of a file, and parses it. A
96 CDDB::Fake object is then created from the file data.
97  
98 =cut
99  
100 4 ▶sub new {
Subroutine new: called CC=17 SCAR=28.4
Pod documented
101 4 my ($pkg, $file) = @_;
102  
103 4 my $self = {
104 _title => "",
105 _artist => "",
106 _extd => "",
107 };
108  
109 4 my $fh;
110 4 ▶ if ( ref($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 4 0 4
111 # For testing.
112 4 $fh = $file;
113 }
114 else {
115 0 ▶ open($fh, $file) or croak("$file: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $file or croak("${file}: $!\n") 0 0 0
116 }
117 4 my $off = 150;
118 4 my $state = 0;
119 4 my $va = 0;
120 4 my $to;
121 4 ▶ while ( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 52 4 56
122 52 ▶ next unless /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\S/ 8 44 52
123 44 s/[\r\n]+$//;
124  
125 # State 0: Looking for artist/title.
126 44 ▶ if ( $state == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state == 0) 4 40 44
127 4 ▶ if ( /^\s*(.+)\s+\/\s+(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (m[^\s*(.+)\s+/\s+(.*)]) { } 2 2 4
128 2 $self->{_artist} = _deblank($1);
129 2 $self->{_title} = _deblank($2);
130 }
131 else {
132 # Eponymous.
133 2 $self->{_artist} = $self->{_title} = _deblank($_);
134 }
135 4 $va = lc($self->{_artist}) eq "various";
136 4 $state++;
137 4 next;
138 }
139  
140 # State 1: Processing tracks.
141 40 ▶ if ( $state == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state == 1) 38 2 40
142 38 ▶ if ( /^\s*(\d+)\.?\s+(.*)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^\s*(\d+)\.?\s+(.*)/) { } 24 14 38
elsif (defined $to and /^\s+(.+)/) { } 10 4 14
143 24 my $tn = 0 + $1;
144 24 my $tt = $2;
145 24 my $tl;
146 24 ▶ if ( $tt =~ /^(.*?)\t+ ?(\d+):(\d\d)\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tt =~ /^(.*?)\t+ ?(\d+):(\d\d)\s*$/) { } 12 12 24
147 12 $tt = _deblank($1);
148 12 $tl = 60 * $2 + $3;
149 12 $self->{_length} += $tl;
150 }
151 else {
152 12 $tt = _deblank($tt);
153 }
154 24 my $art = $self->{_artist};
155 24 ▶ if ( $va ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($va) 12 12 24
156 12 ▶ if ( $tt =~ /^(.+?):\s+(.*)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tt =~ /^(.+?):\s+(.*)/) { } 2 10 12
elsif ($tt =~ m[^(.+?)\s+/\s+(.*)]) { } 2 8 10
157 2 $art = _deblank($1);
158 2 $tt = _deblank($2);
159 }
160 elsif ( $tt =~ /^(.+?)\s+\/\s+(.*)/ ) {
161 2 $art = _deblank($1);
162 2 $tt = _deblank($2);
163 }
164 }
165 24 push(@{$self->{_tracks}},
166 $to = CDDB::Fake::Track->new($art, $tn, $tt,
167 $tl, $off, ""));
168 24 ▶ $off += 75 * $tl if $tl;
Branch: 1 branch
BranchTrueFalseTotal
if $tl 12 12 24
169 24 next;
170 }
171 ▶ elsif ( defined($to) && /^\s+(.+)/ ) {
Condition66%2/3
defined $to and /^\s+(.+)/
!ll&&!rl&&r
0410
MC/DC50%1/2
defined $to and /^\s+(.+)/
defined $to /^\s+(.+)/
Truth table66%2/3
A and B
A= defined $toB= /^\s+(.+)/
ABresult
0X0
100
111
172 10 $to->_extd_append($1);
173 10 next;
174 }
175 else {
176 4 $state++;
177 }
178 }
179  
180 # State 2: Remainder (ext info).
181 6 ▶ if ( $state == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state == 2) 4 2 6
182 4 $self->{_extd} = $_ . "\n";
183 4 $state++;
184 4 next;
185 }
186  
187 # State 3: Rest of ext info.
188 2 $self->{_extd} .= $_ . "\n";
189  
190 }
191  
192 4 $self->{_extd} = _newlines($self->{_extd});
193  
194 4 bless $self, $pkg;
195 }
196  
197 38 ▶sub _deblank {
Subroutine _deblank: called CC=2 SCAR=6.9
198 38 my $t = shift;
199 38 for ( $t ) {
200 38 s/^\s+//;
201 38 s/\s+$//;
202 38 s/\s+/ /g;
203 38 return $_;
204 }
205 }
206  
207 20 ▶sub _newlines {
Subroutine _newlines: called CC=3 SCAR=11.0
208 20 my $t = shift;
209 20 for ( $t ) {
210 20 ▶ return $_ unless /\\n/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\\n/ 16 4 20
211 4 s/\s\n\s/ /g;
212 4 s/\\n/\n/g;
213 4 return $_;
214 }
215 }
216  
217 =item artist
218  
219 Returns the name of the artist.
220  
221 =cut
222  
223 4 ▶sub artist {
Subroutine artist: called CC=1 SCAR=0.0
Pod documented
224 4 my ($self) = @_;
225 4 $self->{_artist};
226 }
227  
228 =item title
229  
230 Returns the name of the album.
231  
232 =cut
233  
234 4 ▶sub title {
Subroutine title: called CC=1 SCAR=0.0
Pod documented
235 4 my ($self) = @_;
236 4 $self->{_title};
237 }
238  
239 =item track_count
240  
241 Returns the number of tracks.
242  
243 =cut
244  
245 2 ▶sub track_count {
Subroutine track_count: called CC=1 SCAR=0.0
Pod documented
246 2 my ($self) = @_;
247 2 scalar(@{$self->{_tracks}});
248 }
249  
250 =item tracks
251  
252 Returns a list of track objects.
253  
254 =cut
255  
256 8 ▶sub tracks {
Subroutine tracks: called CC=1 SCAR=0.0
Pod documented
257 8 my ($self) = @_;
258 8 @{$self->{_tracks}};
259 }
260  
261 =item id
262  
263 =item all_ids
264  
265 Returns the (fake) id for this disc.
266  
267 =cut
268  
269 2 ▶sub id { "00000000" }
Subroutine id: called CC=1 SCAR=0.0
Pod documented
270  
271 0 ▶sub all_ids { ("00000000") }
Subroutine all_ids: not called CC=1 SCAR=6.9
Pod documented
272  
273 =item year
274  
275 =item genre
276  
277 =item submitted_by
278  
279 =item processed_by
280  
281 These methods return empty strings since the information is not
282 available in CDDB::Fake files.
283  
284 =cut
285  
286 0 ▶sub year { "" }
Subroutine year: not called CC=1 SCAR=6.9
Pod documented
287 0 ▶sub genre { "" }
Subroutine genre: not called CC=1 SCAR=6.9
Pod documented
288 0 ▶sub submitted_by { "" }
Subroutine submitted_by: not called CC=1 SCAR=6.9
Pod documented
289 0 ▶sub processed_by { "" }
Subroutine processed_by: not called CC=1 SCAR=6.9
Pod documented
290 0 ▶sub revision { 1 }
Subroutine revision: not called CC=1 SCAR=6.9
Pod undocumented
291  
292 =item length
293  
294 This method will return the accumulated length of all the tracks,
295 provided this information is present in the fake file.
296  
297 =cut
298  
299 4 ▶sub length {
Subroutine length: called CC=2 SCAR=6.9
Pod documented
300 4 my ($self) = @_;
301 4 ▶ $self->{_length} || 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_length"} or 0 3 1 4
302 }
303  
304 =item extd
305  
306 Returns the extended disc information, that is everything that follows
307 the list of tracks in the fake file.
308  
309 =cut
310  
311 4 ▶sub extd {
Subroutine extd: called CC=2 SCAR=7.2
Pod documented
312 4 my ($self) = @_;
313 4 ▶ $self->{_extd} || "";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_extd"} or "" 4 0 4
314 }
315  
316 =item as_cddb
317  
318 Returns the data in the format of a CDDB entry.
319  
320 =cut
321  
322 2 ▶sub as_cddb {
Subroutine as_cddb: called CC=6 SCAR=17.9
Pod documented
323 2 my ($self) = @_;
324  
325 2 my $ret = "";
326  
327 # Writing CDDB data requires some line breaking and such.
328 32 ▶ my $out = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
329 32 my ($tag, $desc) = @_;
330 32 $desc =~ s/\n/\\n/g;
331 32 $desc =~ s/\\n$//;
332 32 $tag .= "=";
333 32 $desc = $tag . $desc;
334 32 for ( ;; ) {
335 33 my $t = substr($desc,0,70,$tag);
336 33 $ret .= $t . "\n";
337 33 ▶ last if $desc eq $tag;
Branch: 1 branch
BranchTrueFalseTotal
if $desc eq $tag 32 1 33
338 }
339 2 };
340  
341 2 my @tracks = $self->tracks;
342  
343 # Header.
344 2 $ret = "# xmcd 2.0 CD database file\n" .
345 "# Copyright (C) 1996,2004 Johan Vromans\n" .
346 "#\n";
347  
348 2 ▶ if ( $self->length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->length) 1 1 2
349 1 $ret .= "# Track frame offsets:\n";
350 1 foreach ( @tracks ) {
351 6 $ret .= "#\t" . $_->offset . "\n";
352 }
353 1 $ret .= "#\n" .
354 "# Disc length: " . $self->length . " seconds\n" .
355 "#\n";
356 }
357  
358 2 $out->("DISCID", $self->id);
359 2 $out->("DTITLE", $self->artist . " / " . $self->title);
360  
361 2 my $i;
362 2 ▶ for ( $i=0; $i < @tracks; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @tracks) 12 2 14
363 12 ▶ $out->("TTITLE$i", $tracks[$i]->title)
Branch: 1 branch
BranchTrueFalseTotal
if defined $tracks[$i] 12 0 12
364 if defined $tracks[$i];
365 }
366  
367 2 $out->("EXTD", $self->extd);
368  
369 2 ▶ for ( $i=0; $i < @tracks; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @tracks) 12 2 14
370 12 $out->("EXTT$i", $tracks[$i]->extd);
371 }
372 2 $out->("PLAYORDER", "");
373 2 $ret;
374 }
375  
376 =back
377  
378 =cut
379  
380 package CDDB::Fake::Track;
381  
382 24 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
383 24 my ($pkg, $disc, $num, $tt, $len, $off, $extd) = @_;
384 24 bless [ $disc, $num, $tt, $len, $off, $extd ], $pkg;
385 }
386  
387 =pod
388  
389 Track objects provide the following methods:
390  
391 =over 4
392  
393 =item artist
394  
395 The artist, usually the same as the artist of the disc.
396  
397 =cut
398  
399 3 ▶sub artist { shift->[0] || ""}
Subroutine artist: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->[0] or "" 3 0 3
400  
401 =item number
402  
403 The track number, starting with 1.
404  
405 =cut
406  
407 5 ▶sub number { shift->[1] || 0 }
Subroutine number: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->[1] or 0 5 0 5
408  
409 =item title
410  
411 The track title.
412  
413 =cut
414  
415 17 ▶sub title { shift->[2] || ""}
Subroutine title: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->[2] or "" 17 0 17
416  
417 =item length
418  
419 The track length (in seconds).
420  
421 This will be zero unless a track length was specified in the fake info.
422  
423 =cut
424  
425 3 ▶sub length { shift->[3] || 0}
Subroutine length: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->[3] or 0 2 1 3
426  
427 =item offset
428  
429 The track offset.
430  
431 This will be bogus unless track offsets could be estimated using the
432 length information.
433  
434 =cut
435  
436 7 ▶sub offset { shift->[4] || 0 }
Subroutine offset: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->[4] or 0 7 0 7
437  
438 =item extd
439  
440 This extended track info, if present.
441  
442 =cut
443  
444 16 ▶sub extd {
Subroutine extd: called CC=2 SCAR=6.9
445 16 ▶ CDDB::Fake::_newlines(shift->[5] || "");
Condition100%2/2
+(shift())->[5] || ''
l!l
610
MC/DC100%1/1
+(shift())->[5] || ''
+(shift())->[5]
Truth table100%2/2
A
A= +(shift())->[5]
Aresult
11
00
446 }
447  
448 10 ▶sub _extd_append {
Subroutine _extd_append: called CC=3 SCAR=11.0
449 10 my ($self, $text) = @_;
450 10 ▶ if ( defined($self->[5]) && CORE::length($self->[5]) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->[5] and CORE::length $self->[5] > 0) { } 4 6 10
Condition66%2/3
defined $self->[5] and CORE::length $self->[5] > 0
!ll&&!rl&&r
064
MC/DC50%1/2
defined $self->[5] and CORE::length $self->[5] > 0
defined $self->[5] CORE::length $self->[5] > 0
Truth table66%2/3
A and B
A= defined $self->[5]B= CORE::length $self->[5] > 0
ABresult
0X0
100
111
451 4 $self->[5] .= " " . $text;
452 }
453 else {
454 6 $self->[5] = $text;
455 }
456 }
457  
458 =head1 EXAMPLES
459  
460 It is often handy to generalize the handling of real and fake files:
461  
462 use CDDB::File; # the real one
463 use CDDB::Fake; # the fake one
464 use Carp;
465  
466 # Return a CDDB::File object if a .cddb file is present, otherwise
467 # return a CDDB::Fake onkect from a .nocddb file, if present.
468  
469 sub cddb_info($) {
470 my $df = shift;
471 croak("cddb_info(dir)\n") unless -d $df;
472 return CDDB::File->new("$df/.cddb") if -s "$df/.cddb";
473 return CDDB::Fake->new("$df/.nocddb") if -s "$df/.nocddb";
474 undef;
475 }
476  
477 =head1 SEE ALSO
478  
479 L<CDDB::File>.
480  
481 =head1 AUTHOR
482  
483 Johan Vromans <jvromans@squirrel.nl>
484  
485 =head1 COPYRIGHT
486  
487 This programs is Copyright 2003,2004, Squirrel Consultancy.
488  
489 This program is free software; you can redistribute it and/or modify
490 it under the terms of the Perl Artistic License or the GNU General
491 Public License as published by the Free Software Foundation; either
492 version 2 of the License, or (at your option) any later version.
493  
494 =cut
495  
496 4 1;
497  
↑ Summary