blib/lib/D64/Disk/Layout/Sector.pm

filter: Statementstmt 98.8% 246 / 249 Branchbran 99.1% 123 / 124 Conditioncond 79.4% 31 / 39 MC/DCmcdc 65.2% 15 / 23 Subroutinesub 96.5% 28 / 29 Podpod 85.7% 12 / 14 totaltotal 95.1% 455 / 478 CC 76 SCAR 18.9
CC
76
Cov
97%
_validate_data
13.0
ts_link
11.0
file_data
10.0
CRAP 6.6
↑ Summary Sector.xs »
1 package D64::Disk::Layout::Sector;
2  
3 =head1 NAME
4  
5 D64::Disk::Layout::Sector - An abstraction layer over physical sector data of various Commodore disk image formats
6  
7 =head1 SYNOPSIS
8  
9 use D64::Disk::Layout::Sector;
10  
11 # Create a new disk sector object instance:
12 my $object = D64::Disk::Layout::Sector->new(data => $data, track => $track, sector => $sector);
13 my $object = D64::Disk::Layout::Sector->new(data => \@data, track => $track, sector => $sector);
14  
15 # Fetch sector data as a scalar of 256 bytes:
16 my $data = $object->data();
17 # Fetch sector data as an array of 256 bytes:
18 my @data = $object->data();
19  
20 # Update sector providing 256 bytes of scalar data:
21 $object->data($data);
22 # Update sector given array with 256 bytes of data:
23 $object->data(@data);
24 $object->data(\@data);
25  
26 # Fetch the actual file contents from sector data as a scalar of allocated number of bytes:
27 my $file_data = $object->file_data();
28 # Fetch the actual file contents from sector data as an array of allocated number of bytes:
29 my @file_data = $object->file_data();
30  
31 # Update the actual file contents providing number of scalar data bytes to allocate:
32 $object->file_data($file_data);
33 # Update the actual file contents given array with number of bytes of data to allocate:
34 $object->file_data(@file_data);
35 $object->file_data(\@file_data);
36  
37 # Get/set track location of the object data in the actual disk image:
38 my $track = $object->track();
39 $object->track($track);
40  
41 # Get/set sector location of the object data in the actual disk image:
42 my $sector = $object->sector();
43 $object->sector($sector);
44  
45 # Check if first two bytes of data point to the next chunk of data in a chain:
46 my $is_valid_ts_link = $object->is_valid_ts_link();
47  
48 # Get/set track and sector link values to the next chunk of data in a chain:
49 my ($track, $sector) = $object->ts_link();
50 $object->ts_link($track, $sector);
51  
52 # Check if first two bytes of data indicate index of the last allocated byte:
53 my $is_last_in_chain = $object->is_last_in_chain();
54  
55 # Get/set index of the last allocated byte within the sector data:
56 my $alloc_size = $object->alloc_size();
57 $object->alloc_size($alloc_size);
58  
59 # Check if sector object is empty:
60 my $is_empty = $object->empty();
61  
62 # Set/clear boolean flag marking sector object as empty:
63 $object->empty($empty);
64  
65 # Wipe out an entire sector data, and mark it as empty:
66 $object->clean();
67  
68 # Print out formatted disk sector data:
69 $object->print();
70  
71 =head1 DESCRIPTION
72  
73 C<D64::Disk::Layout::Sector> provides a helper class for C<D64::Disk::Layout> module and defines an abstraction layer over physical sector data of various Commodore disk image formats, enabling users to access and modify disk sector data in an object oriented way without the hassle of worrying about the meaning of individual bits and bytes, describing their function in a disk image layout. The whole family of C<D64::Disk::Layout> modules has been implemented in pure Perl as an alternative to Per Olofsson's "diskimage.c" library originally written in an ANSI C.
74  
75 =head1 METHODS
76  
77 =cut
78  
79 6 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 6 our $VERSION = '0.02';
85  
86 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 6 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 6 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
89 6 ▶use Text::Convert::PETSCII qw/:convert/;
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 6 require XSLoader;
92 6 XSLoader::load(__PACKAGE__, $VERSION);
93  
94 # Data offset constants:
95 6 Readonly our $I_TS_POINTER_TRACK => 0x00;
96 6 Readonly our $I_TS_POINTER_SECTOR => 0x01;
97 6 Readonly our $I_ALLOC_SIZE => 0x01;
98 6 Readonly our $I_SECTOR_DATA => 0x02;
99  
100 6 Readonly our $SECTOR_DATA_SIZE => 0x0100;
101  
102 =head2 new
103  
104 Create an instance of a C<D64::Disk::Layout::Sector> class as an empty disk sector:
105  
106 my $object = D64::Disk::Layout::Sector->new();
107  
108 Create an instance of a C<D64::Disk::Layout::Sector> class providing 256 bytes of data retrieved from a disk image:
109  
110 my $object = D64::Disk::Layout::Sector->new(data => $data, track => $track, sector => $sector);
111 my $object = D64::Disk::Layout::Sector->new(data => \@data, track => $track, sector => $sector);
112  
113 C<$track> and C<$sector> values are expected to be single bytes, an exception will be thrown when non-byte or non-numeric or non-scalar value is provided (please note that a default value of C<undef> is internally translated into the value of C<0x00>). For more information about C<$data> and C<@data> validation, see the C<data> section below.
114  
115 =cut
116  
117 167 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
118 167 my ($this, %args) = @_;
119 167 ▶ my $class = ref ($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
01670
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
120 167 my $object = $class->_init();
121 167 my $self = bless $object, $class;
122  
123 167 ▶ if (%args) {
Branch: 1 branch
BranchTrueFalseTotal
if (%args) 126 41 167
124 126 ▶ unless (defined $args{data}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"data"}) 2 124 126
125 2 die sprintf q{Unable to initialize sector data: undefined value of data (%d bytes expected)}, $SECTOR_DATA_SIZE;
126 }
127 124 ▶ unless (defined $args{track}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"track"}) 1 123 124
128 1 die q{Unable to initialize track property: undefined value of track (numeric value expected)};
129 }
130 123 ▶ unless (defined $args{sector}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"sector"}) 1 122 123
131 1 die q{Unable to initialize sector property: undefined value of sector (numeric value expected)};
132 }
133  
134 122 $self->data($args{data});
135 113 $self->track($args{track});
136 109 $self->sector($args{sector});
137 }
138  
139 147 return $self;
140 }
141  
142 168 ▶sub _init {
Subroutine _init: called CC=1 SCAR=0.0
143 168 my ($this) = @_;
144 168 my @data = map { chr 0x00 } (0x01 .. $SECTOR_DATA_SIZE);
145 168 my %object = (
146 data => \@data,
147 track => 0,
148 sector => 0,
149 is_empty => 1,
150 );
151 168 return \%object;
152 }
153  
154 1666 ▶sub _object_property {
Subroutine _object_property: called CC=3 SCAR=11.0
155 1666 my ($self, $name, $value) = @_;
156  
157 1666 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 548 1118 1666
158 548 ▶ $self->{$name} = ref $value ? dclone $value : $value;
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 159 389 548
159 }
160  
161 1666 return $self->{$name};
162 }
163  
164 34059 ▶sub _is_valid_byte_value {
Subroutine _is_valid_byte_value: called CC=5 SCAR=16.2
165 34059 my ($self, $byte_value) = @_;
166  
167 34059 ▶ if (defined $byte_value && length ($byte_value) == 1 && ord ($byte_value) >= 0x00 && ord ($byte_value) <= 0xff) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $byte_value and length $byte_value == 1 and ord $byte_value >= 0 and ord $byte_value <= 255) 34051 8 34059
Condition77%7/9
defined $byte_value and length $byte_value == 1 and ord $byte_value >= 0 and ord $byte_value <= 255
!ll&&!rl&&r
8034051
defined $byte_value and length $byte_value == 1 and ord $byte_value >= 0
!ll&&!rl&&r
8034051
defined $byte_value and length $byte_value == 1
!ll&&!rl&&r
1734051
MC/DC50%2/4
defined $byte_value and length $byte_value == 1 and ord $byte_value >= 0 and ord $byte_value <= 255
defined $byte_value length $byte_value == 1 ord $byte_value >= 0 ord $byte_value <= 255
Truth table60%3/5
A and B and C and D
A= defined $byte_valueB= length $byte_value == 1C= ord $byte_value >= 0D= ord $byte_value <= 255
ABCDresult
0XXX0
10XX0
110X0
11100
11111
168 34051 return 1;
169 }
170  
171 8 return 0;
172 }
173  
174 279 ▶sub _is_valid_number_value {
Subroutine _is_valid_number_value: called CC=4 SCAR=13.9
175 279 my ($self, $number_value) = @_;
176  
177 279 ▶ if ($self->is_int($number_value) && $number_value >= 0x00 && $number_value <= 0xff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_int($number_value) and $number_value >= 0 and $number_value <= 255) 234 45 279
Condition83%5/6
$self->is_int($number_value) and $number_value >= 0 and $number_value <= 255
!ll&&!rl&&r
3114234
$self->is_int($number_value) and $number_value >= 0
!ll&&!rl&&r
310248
MC/DC66%2/3
$self->is_int($number_value) and $number_value >= 0 and $number_value <= 255
$self->is_int($number_value) $number_value >= 0 $number_value <= 255
Truth table75%3/4
A and B and C
A= $self->is_int($number_value)B= $number_value >= 0C= $number_value <= 255
ABCresult
0XX0
10X0
1100
1111
178 234 return 1;
179 }
180  
181 45 return 0;
182 }
183  
184 =head2 data
185  
186 Fetch sector data as a scalar of 256 bytes:
187  
188 my $data = $object->data();
189  
190 Fetch sector data as an array of 256 bytes:
191  
192 my @data = $object->data();
193  
194 Update sector providing 256 bytes of scalar data retrieved from a disk image:
195  
196 $object->data($data);
197  
198 C<$data> value is expected to be a scalar of 256 bytes in length, an exception will be thrown when non-scalar value or a scalar which does not have a length of 256 bytes or a scalar which contains wide non-byte character is provided.
199  
200 Update sector given array with 256 bytes of data retrieved from a disk image:
201  
202 $object->data(@data);
203 $object->data(\@data);
204  
205 C<@data> value is expected to be an array of 256 bytes in size, an exception will be thrown when non-array or an array with any other number of elements or an array with non-scalar byte values is provided.
206  
207 =cut
208  
209 180 ▶sub data {
Subroutine data: called CC=6 SCAR=17.9
Pod documented
210 180 my ($self, @args) = @_;
211  
212 180 my $data = $self->_validate_data(args => \@args, min_size => $SECTOR_DATA_SIZE, max_size => $SECTOR_DATA_SIZE, what => 'sector');
213  
214 152 ▶ if (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) 124 28 152
215 124 $self->_object_property('data', $data);
216  
217 # When data is set, object is no longer empty (unless it's filled with zeroes)
218 124 my $is_valid_ts_link = $self->is_valid_ts_link();
219 124 my $alloc_size = $self->alloc_size();
220 124 ▶ unless ($is_valid_ts_link || $alloc_size != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_valid_ts_link or $alloc_size != 0) { } 120 4 124
Condition100%3/3
$is_valid_ts_link or $alloc_size != 0
l!l&&r!l&&!r
92284
MC/DC100%2/2
$is_valid_ts_link or $alloc_size != 0
$is_valid_ts_link $alloc_size != 0
Truth table100%3/3
A or B
A= $is_valid_ts_linkB= $alloc_size != 0
ABresult
1X1
011
000
221 4 $self->empty(1);
222 }
223 else {
224 120 $self->empty(0);
225 }
226 }
227  
228 152 ▶ return unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 124 28 152
229  
230 28 $data = $self->_object_property('data');
231  
232 28 ▶ return wantarray ? @{$data} : join '', @{$data};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 13 15 28
233 }
234  
235 220 ▶sub _validate_data {
Subroutine _validate_data: called CC=13 SCAR=25.6
236 220 my ($self, %args) = @_;
237  
238 220 my @args = @{$args{args}};
239  
240 220 ▶ return unless scalar @args > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @args > 0 28 192 220
241  
242 # Convert arrayref parameter to an array:
243 192 ▶ if (scalar @args == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args == 1) 182 10 192
244 182 my ($arg) = @args;
245 182 ▶ if (ref $arg eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq "ARRAY") 149 33 182
246 149 @args = @{$arg};
247 }
248 }
249  
250 192 my $what = $args{what};
251 192 my $min_size = $args{min_size};
252 192 my $max_size = $args{max_size};
253  
254 # Convert scalar parameter to an array:
255 192 ▶ if (scalar @args == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args == 1) 34 158 192
256 34 my ($arg) = @args;
257 34 ▶ unless (ref $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg) { } 3 31 34
258 6 ▶ no bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
259 31 ▶ if (length ($arg) < $min_size || length ($arg) > $max_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $arg < $min_size or length $arg > $max_size) 8 23 31
Condition100%3/3
length $arg < $min_size or length $arg > $max_size
l!l&&r!l&&!r
6223
MC/DC100%2/2
length $arg < $min_size or length $arg > $max_size
length $arg < $min_size length $arg > $max_size
Truth table100%3/3
A or B
A= length $arg < $min_sizeB= length $arg > $max_size
ABresult
1X1
011
000
260 8 die sprintf q{Unable to set %s data: Invalid length of data}, $what;
261 }
262 23 @args = split //, $arg;
263 }
264 else {
265 3 die sprintf q{Unable to set %s data: Invalid arguments given}, $what;
266 }
267 }
268  
269 181 ▶ unless (scalar (@args) < $min_size || scalar (@args) > $max_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args < $min_size or scalar @args > $max_size) { } 11 170 181
Condition100%3/3
scalar @args < $min_size or scalar @args > $max_size
l!l&&r!l&&!r
74170
MC/DC100%2/2
scalar @args < $min_size or scalar @args > $max_size
scalar @args < $min_size scalar @args > $max_size
Truth table100%3/3
A or B
A= scalar @args < $min_sizeB= scalar @args > $max_size
ABresult
1X1
011
000
270 170 ▶ for (my $i = 0; $i < @args; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @args) 34063 158 34221
271 34063 my $byte_value = $args[$i];
272 34063 ▶ if (ref $byte_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $byte_value) 4 34059 34063
273 4 die sprintf q{Unable to set %s data: Invalid data type at offset %d (%s)}, $what, $i, ref $args[$i];
274 }
275 34059 ▶ unless ($self->_is_valid_byte_value($byte_value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_valid_byte_value($byte_value)) 8 34051 34059
276 8 die sprintf q{Unable to set %s data: Invalid byte value at offset %d (%s)}, $what, $i, $self->_dump($byte_value);
277 }
278 }
279 }
280 else {
281 11 die sprintf q{Unable to set %s data: Invalid amount of data}, $what;
282 }
283  
284 158 my @data = @args;
285  
286 158 return \@data;
287 }
288  
289 =head2 file_data
290  
291 Fetch the actual file contents from sector data as a scalar of allocated number of bytes:
292  
293 my $file_data = $object->file_data();
294  
295 Fetch the actual file contents from sector data as an array of allocated number of bytes:
296  
297 my @file_data = $object->file_data();
298  
299 Update the actual file contents providing number of scalar data bytes to allocate:
300  
301 $object->file_data($file_data, set_alloc_size => $set_alloc_size);
302  
303 C<$file_data> value is expected to be a scalar of between 0 and 254 bytes in length, an exception will be thrown when non-scalar value or a scalar which does not have a length between 0 and 254 bytes or a scalar which contains wide non-byte character is provided. C<$set_alloc_size> input parameter defaults to C<0>. That means every file data assignment modifies only certain data bytes. This may or may not be a desired behaviour. If C<$file_data> contains 254 bytes of data, it is likely that the first two bytes of sector data should still point to the next chunk of data in a chain and thus remain unchanged. If C<$set_alloc_size> flag is set, this operation will additionally mark sector object as the last sector in chain and calculate the last allocated byte within sector data based on the number of bytes provided in C<$file_data> value. This value will then be assigned to the C<alloc_size> object property.
304  
305 Update the actual file contents given array with number of bytes of data to allocate:
306  
307 $object->file_data(\@file_data, set_alloc_size => $set_alloc_size);
308  
309 C<@file_data> value is expected to be an array of between 0 and 254 bytes in size, an exception will be thrown when non-array or an array with any other number of elements not in between 0 and 254 or an array with non-scalar byte values is provided. C<$set_alloc_size> input parameter defaults to C<0>. The same remarks apply here as the ones desribed in a paragraph above.
310  
311 =cut
312  
313 56 ▶sub file_data {
Subroutine file_data: called CC=10 SCAR=23.0
Pod documented
314 56 my ($self, $data, %args) = @_;
315  
316 56 ▶ $args{set_alloc_size} = 0 if not exists $args{set_alloc_size};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"set_alloc_size"} 36 20 56
317  
318 56 ▶ my $file_data = $self->_validate_data(args => [$data], min_size => 0, max_size => 254, what => 'file') if defined $data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 40 16 56
319  
320 50 ▶ if (defined $file_data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file_data) 34 16 50
321 34 my $file_data_size = scalar @{$file_data};
322  
323 34 my $data = $self->_object_property('data');
324 34 splice @{$data}, $I_SECTOR_DATA, $file_data_size, @{$file_data};
325  
326 34 ▶ if ($args{set_alloc_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"set_alloc_size"}) 12 22 34
327 12 $data->[$I_TS_POINTER_TRACK] = chr 0x00;
328 12 ▶ $data->[$I_ALLOC_SIZE] = chr ($file_data_size + ($file_data_size > 0x00 ? 0x01 : 0x00));
Branch: 1 branch
BranchTrueFalseTotal
$file_data_size > 0 ? : 9 3 12
329 }
330  
331 34 $self->_object_property('data', $data);
332  
333 # When data is set, object is no longer empty (unless it's filled with zeroes)
334 34 my $is_valid_ts_link = $self->is_valid_ts_link();
335 34 my $alloc_size = $self->alloc_size();
336 34 ▶ unless ($is_valid_ts_link || $alloc_size != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_valid_ts_link or $alloc_size != 0) { } 23 11 34
Condition100%3/3
$is_valid_ts_link or $alloc_size != 0
l!l&&r!l&&!r
14911
MC/DC100%2/2
$is_valid_ts_link or $alloc_size != 0
$is_valid_ts_link $alloc_size != 0
Truth table100%3/3
A or B
A= $is_valid_ts_linkB= $alloc_size != 0
ABresult
1X1
011
000
337 11 $self->empty(1);
338 }
339 else {
340 23 $self->empty(0);
341 }
342 }
343  
344 50 ▶ return unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 34 16 50
345  
346 16 $data = $self->_object_property('data');
347  
348 16 my @file_data = @{$data};
349 16 my $alloc_size = $self->alloc_size();
350  
351 16 splice @file_data, $alloc_size + 1;
352 16 splice @file_data, 0, $I_SECTOR_DATA;
353  
354 16 ▶ return wantarray ? @file_data : join '', @file_data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 10 16
355 }
356  
357 =head2 track
358  
359 Get track location of sector data in the actual disk image:
360  
361 my $track = $object->track();
362  
363 Set track location of sector data in the actual disk image:
364  
365 $object->track($track);
366  
367 C<$track> value is expected to be a single byte, an exception will be thrown when non-byte or non-numeric or non-scalar value is provided.
368  
369 =cut
370  
371 125 ▶sub track {
Subroutine track: called CC=5 SCAR=16.1
Pod documented
372 125 my ($self, $track) = @_;
373  
374 125 ▶ if (defined $track) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $track) 118 7 125
375 118 ▶ if (ref $track) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $track) 2 116 118
376 2 die sprintf q{Invalid type (%s) of track location of sector data (single byte expected)}, $self->_dump($track);
377 }
378 116 ▶ unless ($self->_is_valid_number_value($track)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_valid_number_value($track)) 4 112 116
379 4 die sprintf q{Invalid value (%s) of track location of sector data (single byte expected)}, $self->_dump($track);
380 }
381 112 ▶ if ($track == 0x00) {
Branch: 1 branch
BranchTrueFalseTotal
if ($track == 0) 1 111 112
382 1 die sprintf q{Illegal value (0) of track location of sector data (track 0 does not exist)};
383 }
384 111 $track = $self->_object_property('track', $track);
385 }
386  
387 118 $track = $self->_object_property('track');
388  
389 118 return $track;
390 }
391  
392 =head2 sector
393  
394 Get sector location of sector data in the actual disk image:
395  
396 my $sector = $object->sector();
397  
398 Set sector location of sector data in the actual disk image:
399  
400 $object->sector($sector);
401  
402 C<$sector> value is expected to be a single byte, an exception will be thrown when non-byte or non-numeric or non-scalar value is provided.
403  
404 =cut
405  
406 121 ▶sub sector {
Subroutine sector: called CC=4 SCAR=13.9
Pod documented
407 121 my ($self, $sector) = @_;
408  
409 121 ▶ if (defined $sector) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sector) 114 7 121
410 114 ▶ if (ref $sector) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sector) 2 112 114
411 2 die sprintf q{Invalid type (%s) of sector location of sector data (single byte expected)}, $self->_dump($sector);
412 }
413 112 ▶ unless ($self->_is_valid_number_value($sector)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_valid_number_value($sector)) 4 108 112
414 4 die sprintf q{Invalid value (%s) of sector location of sector data (single byte expected)}, $self->_dump($sector);
415 }
416 108 $sector = $self->_object_property('sector', $sector);
417 }
418  
419 115 $sector = $self->_object_property('sector');
420  
421 115 return $sector;
422 }
423  
424 =head2 is_valid_ts_link
425  
426 Check if first two bytes of data point to the next chunk of data in a chain:
427  
428 my $is_valid_ts_link = $object->is_valid_ts_link();
429  
430 =cut
431  
432 378 ▶sub is_valid_ts_link {
Subroutine is_valid_ts_link: called CC=2 SCAR=6.9
Pod documented
433 378 my ($self) = @_;
434  
435 378 my $data = $self->_object_property('data');
436  
437 378 my $ts_pointer_track = ord $data->[$I_TS_POINTER_TRACK];
438  
439 378 ▶ return $ts_pointer_track == 0x00 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$ts_pointer_track == 0 ? : 139 239 378
440 }
441  
442 =head2 ts_link
443  
444 Get track and sector link values to the next chunk of data in a chain:
445  
446 my ($track, $sector) = $object->ts_link();
447  
448 Track and sector values will be returned if first two bytes of data point to the next chunk of data in a chain, indicating this sector is in a link chain. When two first bytes of data indicate an index of the last allocated byte, an undefined value will be returned. An undefined value indicates that this is the last sector in a chain (and C<alloc_size> can be used to fetch index of the last allocated byte within sector data).
449  
450 Set track and sector link values to the next chunk of data in a chain:
451  
452 $object->ts_link($track, $sector);
453  
454 Setting track/sector link includes sector in a chain and adds link to the next sector of data, at the same time allocating an entire sector for storing file data.
455  
456 =cut
457  
458 24 ▶sub ts_link {
Subroutine ts_link: called CC=11 SCAR=24.0
Pod documented
459 24 my ($self, $track, $sector) = @_;
460  
461 24 my $data = $self->_object_property('data');
462  
463 24 ▶ if (defined $track || defined $sector) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $track or defined $sector) 12 12 24
Condition100%3/3
defined $track or defined $sector
l!l&&r!l&&!r
11112
MC/DC100%2/2
defined $track or defined $sector
defined $track defined $sector
Truth table100%3/3
A or B
A= defined $trackB= defined $sector
ABresult
1X1
011
000
464 12 ▶ unless (defined $track) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $track) 1 11 12
465 1 die sprintf q{Undefined value of track location for the next chunk of data in a chain (single byte expected)}, $self->_dump($track);
466 }
467 11 ▶ if (ref $track) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $track) 1 10 11
468 1 die sprintf q{Invalid type (%s) of track location for the next chunk of data in a chain (single byte expected)}, $self->_dump($track);
469 }
470 10 ▶ unless ($self->_is_valid_number_value($track)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_valid_number_value($track)) 2 8 10
471 2 die sprintf q{Invalid value (%s) of track location for the next chunk of data in a chain (single byte expected)}, $self->_dump($track);
472 }
473 8 ▶ if ($track == 0x00) {
Branch: 1 branch
BranchTrueFalseTotal
if ($track == 0) 1 7 8
474 1 die sprintf q{Illegal value (0) of track location for the next chunk of data in a chain (track 0 does not exist)};
475 }
476  
477 7 ▶ unless (defined $sector) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sector) 1 6 7
478 1 die sprintf q{Undefined value of sector location for the next chunk of data in a chain (single byte expected)}, $self->_dump($sector);
479 }
480 6 ▶ if (ref $sector) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sector) 1 5 6
481 1 die sprintf q{Invalid type (%s) of sector location for the next chunk of data in a chain (single byte expected)}, $self->_dump($sector);
482 }
483 5 ▶ unless ($self->_is_valid_number_value($sector)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_valid_number_value($sector)) 2 3 5
484 2 die sprintf q{Invalid value (%s) of sector location for the next chunk of data in a chain (single byte expected)}, $self->_dump($sector);
485 }
486  
487 3 $data->[$I_TS_POINTER_TRACK] = chr $track;
488 3 $data->[$I_TS_POINTER_SECTOR] = chr $sector;
489  
490 # Once valid track and sector link values are set, sector can no longer be considered empty:
491 3 $self->empty(0);
492 }
493  
494 15 ▶ return unless $self->is_valid_ts_link();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid_ts_link 4 11 15
495  
496 11 $track = ord $data->[$I_TS_POINTER_TRACK];
497 11 $sector = ord $data->[$I_TS_POINTER_SECTOR];
498  
499 11 return ($track, $sector);
500 }
501  
502 6 *ts_pointer = \&ts_link;
503  
504 =head2 is_last_in_chain
505  
506 Check if two first bytes of data indicate an index of the last allocated byte, meaning this is the last sector in a chain:
507  
508 my $is_last_in_chain = $object->is_last_in_chain();
509  
510 Note that C<alloc_size> method will always correctly return index of the last allocated byte within the sector data (even if first two bytes of data contain track and sector link values to the next chunk of data in a chain).
511  
512 =cut
513  
514 14 ▶sub is_last_in_chain {
Subroutine is_last_in_chain: called CC=2 SCAR=6.9
Pod documented
515 14 my ($self) = @_;
516  
517 14 my $data = $self->_object_property('data');
518  
519 14 my $ts_pointer_track = ord $data->[$I_TS_POINTER_TRACK];
520  
521 14 ▶ return $ts_pointer_track == 0x00 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ts_pointer_track == 0 ? : 11 3 14
522 }
523  
524 =head2 alloc_size
525  
526 Get index of the last allocated byte within the sector data:
527  
528 my $alloc_size = $object->alloc_size();
529  
530 Index of the last valid (loaded) file byte will be returned when this is the last sector in a chain. When C<0xff> value is returned, this sector may be included in a link chain (if that is the case, C<ts_link> can be used to fetch track and sector link values to the next chunk of data in a chain).
531  
532 Set index of the last allocated byte within the sector data:
533  
534 $object->alloc_size($alloc_size);
535  
536 Setting index of the last allocated byte marks sector as the last one in a chain.
537  
538 =cut
539  
540 194 ▶sub alloc_size {
Subroutine alloc_size: called CC=5 SCAR=16.1
Pod documented
541 194 my ($self, $alloc_size) = @_;
542  
543 194 my $data = $self->_object_property('data');
544  
545 194 ▶ if (defined $alloc_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $alloc_size) 6 188 194
546 6 ▶ if (ref $alloc_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $alloc_size) 1 5 6
547 1 die sprintf q{Invalid index type (%s) of the last allocated byte within the sector data (single byte expected)}, $self->_dump($alloc_size);
548 }
549 5 ▶ unless ($self->_is_valid_number_value($alloc_size)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_valid_number_value($alloc_size)) 2 3 5
550 2 die sprintf q{Invalid index value (%s) of the last allocated byte within the sector data (single byte expected)}, $self->_dump($alloc_size);
551 }
552 3 $data->[$I_ALLOC_SIZE] = chr $alloc_size;
553 3 $data->[$I_TS_POINTER_TRACK] = chr 0x00;
554 }
555  
556 191 ▶ return 0xff if $self->is_valid_ts_link();
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_valid_ts_link 116 75 191
557  
558 75 $alloc_size = ord $data->[$I_ALLOC_SIZE];
559  
560 75 return $alloc_size;
561 }
562  
563 =head2 empty
564  
565 Check if sector object is empty:
566  
567 my $is_empty = $object->empty();
568  
569 Set boolean flag to mark sector object as empty:
570  
571 $object->empty(1);
572  
573 Clear boolean flag to mark sector object as non-empty:
574  
575 $object->empty(0);
576  
577 =cut
578  
579 195 ▶sub empty {
Subroutine empty: called CC=4 SCAR=13.9
Pod documented
580 195 my ($self, $is_empty) = @_;
581  
582 195 ▶ if (defined $is_empty) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $is_empty) 168 27 195
583 168 ▶ if (ref $is_empty) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $is_empty) 1 167 168
584 1 die q{Invalid "empty" flag};
585 }
586 167 ▶ $self->_object_property('is_empty', $is_empty ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$is_empty ? : 19 148 167
587 }
588  
589 194 $is_empty = $self->_object_property('is_empty');
590  
591 194 return $is_empty;
592 }
593  
594 =head2 clean
595  
596 Wipe out an entire sector data, and mark it as empty:
597  
598 $object->clean();
599  
600 =cut
601  
602 1 ▶sub clean {
Subroutine clean: called CC=2 SCAR=6.9
Pod documented
603 1 my ($self) = @_;
604  
605 1 my $clean_object = $self->_init();
606  
607 1 ▶ while (my ($property, $value) = each %{$clean_object}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$clean_object;}) 4 1 5
608 4 $self->_object_property($property, $value);
609 }
610  
611 1 return;
612 }
613  
614 =head2 print
615  
616 Print out formatted disk sector data:
617  
618 $object->print(fh => $fh);
619  
620 C<$fh> defaults to the standard output.
621  
622 =cut
623  
624 3 ▶sub print {
Subroutine print: called CC=8 SCAR=20.8
Pod documented
625 3 my ($self, %args) = @_;
626  
627 3 my $fh = $args{fh};
628  
629 3 ▶ $fh ||= *STDOUT;
Condition33%1/3
$fh ||= *STDOUT
l!l&&r!l&&!r
300
MC/DC0%0/2
$fh ||= *STDOUT
$fh *STDOUT
Truth table33%1/3
A ||= B
A= $fhB= *STDOUT
ABresult
1X1
011
000
630 3 $fh->binmode(':bytes');
631  
632 3 my $stdout = select $fh;
633  
634 3 my $data = $self->_object_property('data');
635  
636 3 print q{ };
637 3 ▶ for (my $col = 0x00; $col < 0x10; $col++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col < 16) 48 3 51
638 48 printf q{%02X }, $col;
639 }
640 3 print qq{\n} . q{ } . '-' x 47 . qq{\n};
641 3 ▶ for (my $row = 0x00; $row < 0x100; $row += 0x10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < 256) 48 3 51
642 48 printf q{%02X: }, $row;
643 48 ▶ for (my $col = 0x00; $col < 0x10; $col++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col < 16) 768 48 816
644 768 my $val = ord $data->[$row + $col];
645 768 printf q{%02X }, $val;
646 }
647 48 ▶ for (my $col = 0x00; $col < 0x10; $col++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col < 16) 768 48 816
648 768 my $val = ord $data->[$row + $col];
649 768 ▶ if ($val >= 0x20 and $val <= 0x7f) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val >= 32 and $val <= 127) { } 1 767 768
Condition66%2/3
$val >= 32 and $val <= 127
!ll&&!rl&&r
76701
MC/DC50%1/2
$val >= 32 and $val <= 127
$val >= 32 $val <= 127
Truth table66%2/3
A and B
A= $val >= 32B= $val <= 127
ABresult
0X0
100
111
650 1 $val = ord petscii_to_ascii chr $val;
651 }
652 else {
653 767 $val = ord '?';
654 }
655 768 printf q{%c}, $val;
656 }
657 48 printf qq{\n};
658 }
659  
660 3 select $stdout;
661  
662 3 return;
663 }
664  
665 31 ▶sub _dump {
Subroutine _dump: called CC=2 SCAR=7.4
666 31 my ($self, $value) = @_;
667  
668 31 ▶ if ($self->_is_valid_number_value($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_valid_number_value($value)) 0 31 31
669 0 return sprintf q{$%02x}, $value;
670 }
671  
672 31 my $dump = Data::Dumper->new([$value])->Indent(0)->Terse(1)->Deepcopy(1)->Sortkeys(1)->Dump();
673  
674 31 return $dump;
675 }
676  
677 279 ▶sub is_int {
Subroutine is_int: called CC=1 SCAR=0.0
Pod undocumented
678 279 my ($this, $var) = @_;
679  
680 279 return _is_int($var);
681 }
682  
683 0 ▶sub is_str {
Subroutine is_str: not called CC=1 SCAR=6.9
Pod undocumented
684 0 my ($this, $var) = @_;
685  
686 0 return _is_str($var);
687 }
688  
689 =head1 BUGS
690  
691 There are no known bugs at the moment. Please report any bugs or feature requests.
692  
693 =head1 EXPORT
694  
695 None. No method is exported into the caller's namespace neither by default nor explicitly.
696  
697 =head1 SEE ALSO
698  
699 L<D64::Disk::Image>, L<D64::Disk::Layout>.
700  
701 =head1 AUTHOR
702  
703 Pawel Krol, E<lt>pawelkrol@cpan.orgE<gt>.
704  
705 =head1 VERSION
706  
707 Version 0.02 (2013-02-10)
708  
709 =head1 COPYRIGHT AND LICENSE
710  
711 Copyright 2013 by Pawel Krol E<lt>pawelkrol@cpan.orgE<gt>.
712  
713 This library is free open source software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.
714  
715 PLEASE NOTE THAT IT COMES WITHOUT A WARRANTY OF ANY KIND!
716  
717 =cut
718  
719 6 1;
720  
721 __END__
↑ Summary Sector.xs »