ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Convert-CD/lib/Convert/CD.pm
Revision: 1.5
Committed: Tue Jun 10 14:53:22 2003 UTC (23 years, 3 months ago) by root
Branch: MAIN
Changes since 1.4: +79 -7 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 Convert::CD - import/export/represent various cd image file formats
4
5 =head1 SYNOPSIS
6
7 use Convert::CD;
8
9 # to be done
10
11 =head1 DESCRIPTION
12
13 Kidding?
14
15 =head2 Convert:CD METHODS
16
17 This class represents a full CD image, which can consist of one or more
18 files. While track layout and TOC data is stored in this class, to
19 read/write TOC files you need a Convert::CD::TOCFile.
20
21 =over 4
22
23 =cut
24
25 package Convert::CD;
26
27 $VERSION = 0.01;
28
29 use base Exporter;
30
31 =item new param => value...
32
33 Create a new Convert::CD object.
34
35 my $cd = new path => "/tmp/mycd.bin";
36
37 =cut
38
39 sub new {
40 my $class = shift;
41 my $self = bless {
42 track => [], # an array of tracks, track #1 is in index 0
43 @_,
44 }, $class;
45
46 $self;
47 }
48
49 =item $cd->append_track(param => value...)
50
51 Appends a new track to the image. Same parameters as Convert::CD::Track->new.
52
53 =cut
54
55 sub append_track {
56 my ($self, %arg) = @_;
57
58 push @{$self->{track}}, Convert::CD::Track->new (%arg);
59 }
60
61 =item $cd->as_string
62
63 Returns a multi-line, textual description of the CD Image.
64
65 =cut
66
67 sub as_string {
68 my ($self) = @_;
69 my $r;
70
71 $r .= "Image Path: $self->{path}\n";
72
73 for my $idx (1 .. @{$self->{track}}) {
74 $r .= sprintf "Track %02d: %s\n", $idx, $self->{track}[$idx - 1]->as_string;
75 }
76
77 $r;
78 }
79
80 =item $cd->track ([$idx])
81
82 Returns the track with the given index, or, if no index is given, all the
83 tracks of the CD (or the number of tracks in scalar context). The first
84 track is track #1.
85
86 =cut
87
88 sub track {
89 my ($self, $idx) = @_;
90
91 @_ > 1 ? $self->{track}[$idx - 1] : @{$self->{track}};
92 }
93
94 package Convert::CD::Track;
95
96 use POSIX qw(ceil);
97 use Carp;
98
99 =back
100
101 =head2 Convert::CD::Track METHODS
102
103 This class is used to repesent single track objects.
104
105 =over 4
106
107 =item new param => value...
108
109 Create a new track object.
110
111 Parameters:
112
113 path the file that contains the track
114 offset the offste within the file that contains the first sector
115 (may be negative)
116 rawsize the raw size (in bytes) of the track. (guessed if missing)
117 mode a mode string (MODE1, MODE2, AUDIO, CDI, CDG....)
118 datasize size of the data part of the sector (e.g. 2048). (def. secsize)
119 secsize size of a sector in disk image
120 index an array of index times (0 is pregap start, 1 is track start)
121
122 =cut
123
124 sub new {
125 my $class = shift;
126 my $self = bless {
127 @_,
128 }, $class;
129
130 $self->{rawsize} ||= (-s $self->{path}) - $self->{offset};
131 $self->{datasize} ||= $self->{secsize};
132
133 $self;
134 }
135
136 # create and cache a filehandle of the given mode
137 sub fh {
138 my ($self, $mode) = @_;
139
140 $self->{fh}{$mode} ||= do {
141 open my $fh, $mode, $self->{path}
142 or croak "$self->{path}: $!";
143 binmode $fh; # *seufz*
144 $fh;
145 };
146 }
147
148 =item $track->read ($offset, $length)
149
150 Reads and returns a part of the track. C<$offset> should be a multiple of
151 the datasize.
152
153 =cut
154
155 sub read {
156 my ($self, $offset, $length) = @_;
157 my $buf;
158
159 my $fh = $self->fh ("<");
160
161 $offset = $offset / $self->{secsize} * $self->{secsize};
162
163 while ($length > 0) {
164 sysseek $fh, $self->{offset} + $offset, 0
165 or croak "seek($self->{path}): $!";
166 sysread $fh, $buf, ($length < $self->{datasize} ? $length : $self->{datasize}), length $buf
167 or croak "read($self->{path}): $!";
168
169 $length -= $self->{datasize};
170 $offset += $self->{secsize};
171 }
172
173 $buf;
174 }
175
176 =item $track->size
177
178 Returns the (cooked) size of the track in bytes, i.e. logical size of the
179 track without any ECC etc. information.
180
181 =cut
182
183 sub size {
184 my ($self) = @_;
185
186 ceil ($self->{rawsize} / $self->{secsize}) * $self->{datasize};
187 }
188
189 =item $track->as_string
190
191 Returns a single-line, textual description of the track.
192
193 =cut
194
195 sub as_string {
196 my ($self) = @_;
197 my %data = %$self;
198
199 $data{index} = "<" . (join "|", @{$data{index}}) .">";
200
201 sprintf "%s %08x+%08x %s/%d (%s)",
202 delete @data{qw(path offset rawsize mode secsize)},
203 join " ", %data;
204 }
205
206 1;
207
208 =back
209
210 =head1 AUTHOR
211
212 Marc Lehmann <pcg@goof.com>
213
214 =cut
215