| 1 | package D64::Disk::Image::File; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | D64::Disk::Image::File - File I/O portion of Perl interface to Per Olofsson's "diskimage.c", an ANSI C library for manipulating Commodore disk images | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | use D64::Disk::Image qw(:all); | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | # Load an image from disk: | ||||||||||||||||||||
| 12 | my $d64 = D64::Disk::Image->load_image('disk.d64'); | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | # Write data to file: | ||||||||||||||||||||
| 15 | my $rawname = $d64->rawname_from_name('testfile'); | ||||||||||||||||||||
| 16 | my $prg = $d64->open($rawname, T_PRG, F_WRITE); | ||||||||||||||||||||
| 17 | my $counter = $prg->write($buffer); | ||||||||||||||||||||
| 18 | $prg->close(); | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | # Read data from file: | ||||||||||||||||||||
| 21 | my $rawname = $d64->rawname_from_name('testfile'); | ||||||||||||||||||||
| 22 | my $prg = $d64->open($rawname, T_PRG, F_READ); | ||||||||||||||||||||
| 23 | my ($counter, $buffer) = $prg->read(); | ||||||||||||||||||||
| 24 | $prg->close(); | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | # Write the image to disk: | ||||||||||||||||||||
| 27 | $d64->free_image(); | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | =head1 DESCRIPTION | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | Per Olofsson's "diskimage.c" is an ANSI C library for manipulating Commodore disk images. In Perl the following operations are implemented via D64::Disk::Image::File package: | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | =over | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | =item * | ||||||||||||||||||||
| 36 | Open file ('$' reads directory) | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | =item * | ||||||||||||||||||||
| 39 | Read file | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | =item * | ||||||||||||||||||||
| 42 | Write file | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | =item * | ||||||||||||||||||||
| 45 | Close file | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | =back | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | =head1 METHODS | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 4 | ▶ | use bytes; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 54 | 4 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 55 | 4 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 4 | ▶ | use constant MAXIMUM_FILE_LENGTH => &D64::Disk::Image::D81_SIZE; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | # Open mode constants: | ||||||||||||||||||||
| 60 | 4 | ▶ | use constant F_READ => 'rb'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 61 | 4 | ▶ | use constant F_WRITE => 'wb'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 4 | ▶ | use base qw( Exporter ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 64 | 4 | our %EXPORT_TAGS = (); | |||||||||||||||||||
| 65 | 4 | $EXPORT_TAGS{'modes'} = [ qw(&F_READ &F_WRITE) ]; | |||||||||||||||||||
| 66 | 4 | $EXPORT_TAGS{'all'} = [ @{$EXPORT_TAGS{'modes'}} ]; | |||||||||||||||||||
| 67 | 4 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||
| 68 | 4 | our @EXPORT = qw(); | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 4 | our $VERSION = '0.05'; | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 4 | ▶ | use Carp qw/carp croak verbose/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | =head2 new | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | Create new D64::Disk::Image::File object and open file on a disk image: | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | my $imageFile = D64::Disk::Image::File->new($diskImage, $rawname, $fileType, $mode); | ||||||||||||||||||||
| 79 | my $imageFile = D64::Disk::Image::File->open($diskImage, $rawname, $fileType, $mode); | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | Opens a file for reading or writing. Mode should be either F_READ (for reading) or F_WRITE (for writing). If '$' is given instead of the raw filename, the directory will be read. Consult D64::Disk::Image module documentation for the list of available file types. All parameters are mandatory. | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 20 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 86 | 20 | my $this = shift; | |||||||||||||||||||
| 87 | 20 | my $diskImage = shift; | |||||||||||||||||||
| 88 | 20 | my $rawname = shift; | |||||||||||||||||||
| 89 | 20 | my $fileType = shift; | |||||||||||||||||||
| 90 | 20 | my $mode = shift; | |||||||||||||||||||
| 91 | 20 | ▶ | $diskImage = $diskImage->{'DISK_IMAGE'} unless ref $diskImage eq 'DiskImagePtr'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 20 | ▶ | my $class = ref($this) || $this; | ||||||||||||||||||
|
Condition33%1/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table33%1/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||
| 93 | 20 | my $self = {}; | |||||||||||||||||||
| 94 | 20 | bless $self, $class; | |||||||||||||||||||
| 95 | 20 | my $imageFile = di_open($diskImage, $rawname, $fileType, $mode); | |||||||||||||||||||
| 96 | 20 | my $name = D64::Disk::Image->name_from_rawname($rawname); | |||||||||||||||||||
| 97 | 20 | ▶ | croak "Failed to open image file '${name}' in '${mode}' mode" unless defined $imageFile; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | 17 | $self->{'IMAGE_FILE'} = $imageFile; | |||||||||||||||||||
| 99 | 17 | ▶ | $self->{'WRITE_CALLED'} = $mode eq F_READ ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | 17 | return $self; | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 4 | *open = \&new; | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | =head2 close | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | Close a file (each opened file needs to be subsequently closed to avoid memory leaks): | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | $imageFile->close(); | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | =cut | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 16 | ▶ | sub close { | ||||||||||||||||||
|
Subroutine
close: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 114 | 16 | my $self = shift; | |||||||||||||||||||
| 115 | 16 | my $imageFile = $self->{'IMAGE_FILE'}; | |||||||||||||||||||
| 116 | # Make sure there is no file without any content ever created (which would | ||||||||||||||||||||
| 117 | # result in uninitialized values of track/sector for this file in diskdir): | ||||||||||||||||||||
| 118 | 16 | ▶ | $self->write(chr 0x00) unless $self->{'WRITE_CALLED'}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 119 | 16 | di_close($imageFile); | |||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =head2 read | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | Read data from file opened in F_READ mode: | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | my ($counter, $buffer) = $imageFile->read($maxlength); | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | Reads $maxlength bytes of data into the buffer. Returns the number of bytes actually read, and the buffer with succeeding bytes of data. | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | =cut | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 3 | ▶ | sub read { | ||||||||||||||||||
|
Subroutine
read: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 133 | 3 | my $self = shift; | |||||||||||||||||||
| 134 | 3 | ▶ | my $maxlength = shift || &MAXIMUM_FILE_LENGTH; | ||||||||||||||||||
|
Condition66%2/3
shift() || &MAXIMUM_FILE_LENGTH
MC/DC0%0/2
shift() || &MAXIMUM_FILE_LENGTH
shift()
&MAXIMUM_FILE_LENGTH
Truth table66%2/3
A || B
A= shift()B= &MAXIMUM_FILE_LENGTH
| |||||||||||||||||||||
| 135 | 3 | my $imageFile = $self->{'IMAGE_FILE'}; | |||||||||||||||||||
| 136 | 3 | my ($counter, $buffer) = di_read($imageFile, $maxlength); | |||||||||||||||||||
| 137 | 3 | return ($counter, $buffer); | |||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =head2 write | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | Write data to file opened in F_WRITE mode: | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | my $counter = $imageFile->write($buffer, $length); | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | Writes $length bytes of data from the $buffer. Returns the number of bytes actually written. | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | =cut | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 12 | ▶ | sub write { | ||||||||||||||||||
|
Subroutine
write: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 151 | 12 | my $self = shift; | |||||||||||||||||||
| 152 | 12 | my $buffer = shift; | |||||||||||||||||||
| 153 | 12 | ▶ | my $length = shift || length $buffer; | ||||||||||||||||||
|
Condition66%2/3
shift() || length $buffer
MC/DC0%0/2
shift() || length $buffer
shift()
length $buffer
Truth table66%2/3
A || B
A= shift()B= length $buffer
| |||||||||||||||||||||
| 154 | 12 | my $imageFile = $self->{'IMAGE_FILE'}; | |||||||||||||||||||
| 155 | 12 | my $counter = di_write($imageFile, $buffer, $length); | |||||||||||||||||||
| 156 | 12 | $self->{'WRITE_CALLED'} = 1; | |||||||||||||||||||
| 157 | 12 | return $counter; | |||||||||||||||||||
| 158 | } | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | =head1 BUGS | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | There are no known bugs at the moment. Please report any bugs or feature requests. | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | =head1 EXPORT | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | C<D64::Disk::Image::File> exports nothing by default. | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | You may request the import of open mode constants (C<F_READ>, and C<F_WRITE>). Both these constants can be explicitly imported from C<D64::Disk::Image::File> by using it with ":modes" tag. All constants can be explicitly imported from C<D64::Disk::Image::File> by using it with ":all" tag. | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | =head1 SEE ALSO | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | L<D64::Disk::Image> | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =head1 AUTHOR | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | Pawel Krol, E<lt>pawelkrol@cpan.orgE<gt>. | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =head1 VERSION | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | Version 0.05 (2018-12-01) | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | diskimage.c is released under a slightly modified BSD license. | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | Copyright (c) 2003-2006, Per Olofsson | ||||||||||||||||||||
| 187 | All rights reserved. | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | ||||||||||||||||||||
| 192 | - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | diskimage.c website: L<https://paradroid.automac.se/diskimage/> | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | =cut | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | 4 | 1; | |||||||||||||||||||