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

filter: Statementstmt 97.0% 517 / 533 Branchbran 90.5% 201 / 222 Conditioncond 80.7% 88 / 109 MC/DCmcdc 63.4% 40 / 63 Subroutinesub 100% 52 / 52 Podpod 88.8% 16 / 18 totaltotal 91.6% 914 / 997 CC 152 SCAR 20.1
CC
152
Cov
93%
sectors
18.1
data
14.0
delete
11.0
CRAP 7.5
↑ Summary Dir.xs »
1 package D64::Disk::Layout::Dir;
2  
3 =head1 NAME
4  
5 D64::Disk::Layout::Dir - Handling entire Commodore (D64/D71/D81) disk image directories in pure Perl
6  
7 =head1 SYNOPSIS
8  
9 use D64::Disk::Layout::Dir;
10  
11 # Create an empty disk directory instance:
12 my $dir = D64::Disk::Layout::Dir->new();
13  
14 # Create a new disk directory instance providing 18 * 256 bytes of scalar data:
15 my $dir = D64::Disk::Layout::Dir->new(data => $data);
16  
17 # Fetch directory object data as a scalar of 18 * 256 bytes:
18 my $data = $dir->data();
19  
20 # Replace directory providing 18 * 256 bytes of scalar data:
21 $dir->data($data);
22  
23 # Fetch directory data as an array of up to 18 * 8 items:
24 my @items = $dir->items();
25  
26 # Replace directory providing an array of up to 18 * 8 items:
27 $dir->items(@items);
28  
29 # Get count of non-empty items stored in a disk directory:
30 my $num_items = $dir->num_items();
31  
32 # Fetch directory data as an array of 18 * sectors:
33 my @sectors = $dir->sectors();
34  
35 # Replace directory providing an array of 18 * sectors:
36 $dir->sectors(@sectors);
37  
38 # Fetch an item from a directory listing at any given position:
39 my $item = $dir->get(item => $index);
40  
41 # Fetch a list of items from a directory listing matching given PETSCII pattern:
42 my @items = $dir->get(pattern => $petscii_pattern);
43  
44 # Append an item to the end of directory listing, increasing number of files by one element:
45 $dir->push(item => $item);
46  
47 # Pop and return the last directory item, shortening a directory listing by one element:
48 my $item = $dir->pop();
49  
50 # Shift the first directory item, shortening a directory listing by one and moving everything down:
51 my $item = $dir->shift();
52  
53 # Prepend an item to the front of directory listing, and return the new number of elements:
54 my $num_items = $dir->unshift(item => $item);
55  
56 # Mark directory item designated by an offset as deleted:
57 my $num_deleted = $dir->delete(index => $index);
58  
59 # Wipe out directory item designated by an offset completely:
60 my $num_removed = $dir->remove(index => $index);
61  
62 # Add a new directory item to a directory listing:
63 my $is_success = $dir->add(item => $item);
64  
65 # Put an item to a directory listing at any given position:
66 my $is_success = $dir->put(item => $item, index => $index);
67  
68 # Print out formatted disk directory listing:
69 $dir->print();
70  
71 =head1 DESCRIPTION
72  
73 C<D64::Disk::Layout::Dir> provides a helper class for C<D64::Disk::Layout> module, enabling users to access and manipulate entire directories of D64/D71/D81 disk images in an object oriented way without the hassle of worrying about the meaning of individual bits and bytes describing each sector data on a disk directory track. 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 15 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 15 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 15 our $VERSION = '0.06';
85  
86 15 ▶use D64::Disk::Dir::Item qw(:types);
Subroutine BEGIN: called CC=1 SCAR=0.0
87 15 ▶use D64::Disk::Layout::Sector;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 15 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 15 ▶use List::MoreUtils qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
90 15 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 15 ▶use Text::Convert::PETSCII qw(:convert :validate);
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 15 require XSLoader;
94 15 XSLoader::load(__PACKAGE__, $VERSION);
95  
96 15 Readonly our $ITEMS_PER_SECTOR => 8;
97 15 Readonly our $TOTAL_SECTOR_COUNT => 18;
98  
99 15 Readonly our $ITEM_SIZE => $D64::Disk::Dir::Item::ITEM_SIZE;
100 15 Readonly our $SECTOR_DATA_SIZE => $D64::Disk::Layout::Sector::SECTOR_DATA_SIZE;
101  
102 # First directory track and sector:
103 15 Readonly our $DIRECTORY_FIRST_TRACK => 0x12;
104 15 Readonly our $DIRECTORY_FIRST_SECTOR => 0x01;
105  
106 15 Readonly our @TRACK_WRITE_ORDER => (
107 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
108 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
109 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
110 );
111 15 Readonly our @SECTOR_WRITE_ORDER => (
112 0x01, 0x04, 0x07, 0x0a, 0x0d, 0x10,
113 0x02, 0x05, 0x08, 0x0b, 0x0e, 0x11,
114 0x03, 0x06, 0x09, 0x0c, 0x0f, 0x12,
115 );
116  
117 15 Readonly our $MAX_ENTRIES => $TOTAL_SECTOR_COUNT * $ITEMS_PER_SECTOR;
118  
119 =head2 new
120  
121 Create an empty disk directory instance:
122  
123 my $dir = D64::Disk::Layout::Dir->new();
124  
125 Create a new disk directory instance providing 18 * 256 bytes of scalar data:
126  
127 my $dir = D64::Disk::Layout::Dir->new(data => $data);
128  
129 Create a new disk directory instance given array with 18 * 256 bytes of data:
130  
131 my $dir = D64::Disk::Layout::Dir->new(data => \@data);
132  
133 Alternatively setup source data structure required to initialize new object using 18 * sector objects:
134  
135 my @sectors = (
136 # It needs to be a list of D64::Disk::Layout::Sector objects:
137 D64::Disk::Layout::Sector->new(data => $sector1, track => 18, sector => 1),
138 D64::Disk::Layout::Sector->new(data => $sector2, track => 18, sector => 4),
139 D64::Disk::Layout::Sector->new(data => $sector3, track => 18, sector => 7),
140 # It needs to contain as many sectors as large directory may be:
141 ...
142 );
143  
144 Create a new disk directory instance providing source sector data:
145  
146 my $dir = D64::Disk::Layout::Dir->new(sectors => \@sectors);
147  
148 Directory object may also be initialized using the list of directory item objects:
149  
150 my @items = (
151 # It needs to be a list of D64::Disk::Dir::Item objects:
152 D64::Disk::Dir::Item->new($item1),
153 D64::Disk::Dir::Item->new($item2),
154 D64::Disk::Dir::Item->new($item3),
155 # Up to the maximum number of directory entries (18 * 8 = 144):
156 ...
157 );
158  
159 Create a new disk directory instance providing list of dir items:
160  
161 my $dir = D64::Disk::Layout::Dir->new(items => \@items);
162  
163 Individual directory items are stored, accessed and manipulated as C<D64::Disk::Dir::Item> objects.
164  
165 =cut
166  
167 258 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
168 258 my ($this) = CORE::shift;
169 258 ▶ my $class = ref ($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
02580
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
170 258 my $object = $class->_init();
171 258 my $self = bless $object, $class;
172 258 $self->_setup(@_);
173 242 return $self;
174 }
175  
176 455 ▶sub _init {
Subroutine _init: called CC=1 SCAR=0.0
177 455 my ($class) = @_;
178  
179 455 my @items = map { D64::Disk::Dir::Item->new() } (1 .. $ITEMS_PER_SECTOR * $TOTAL_SECTOR_COUNT);
180  
181 455 my $object = {
182 items => \@items,
183 sector_order => [@SECTOR_WRITE_ORDER],
184 track_order => [@TRACK_WRITE_ORDER],
185 };
186  
187 455 return $object;
188 }
189  
190 258 ▶sub _setup {
Subroutine _setup: called CC=4 SCAR=13.9
191 258 my ($self, %args) = @_;
192  
193 258 ▶ $self->data($args{data}) if exists $args{data};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"data"} 40 218 258
194 247 ▶ $self->items($args{items}) if exists $args{items};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"items"} 131 116 247
195 246 ▶ $self->sectors($args{sectors}) if exists $args{sectors};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"sectors"} 10 236 246
196  
197 242 return undef;
198 }
199  
200 55 ▶sub _validate_data {
Subroutine _validate_data: called CC=8 SCAR=20.8
201 55 my ($self, $data) = @_;
202  
203 55 my $expected_data_size = $TOTAL_SECTOR_COUNT * $SECTOR_DATA_SIZE;
204  
205 55 ▶ unless (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 1 54 55
206 1 die sprintf q{Unable to initialize disk directory: Undefined value of data (expected %d bytes)}, $expected_data_size;
207 }
208  
209 # Convert scalar data into an array:
210 54 ▶ unless (ref $data) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data) { } 40 14 54
if (do { ref $data ne "ARRAY" }) 1 39 40
211 15 ▶ no bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
212 14 $data = [ split //, $data ];
213 }
214 0 elsif (ref $data ne 'ARRAY') {
215 1 die sprintf q{Unable to initialize disk directory: Invalid arguments given (expected %d bytes)}, $expected_data_size;
216 }
217  
218 53 ▶ unless (scalar (@{$data}) == $expected_data_size) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$data;} == $expected_data_size) 6 47 53
219 6 die sprintf q{Unable to initialize disk directory: Invalid amount of data (got %d bytes, but required %d)}, scalar (@{$data}), $expected_data_size;
220 }
221  
222 47 ▶ for (my $i = 0; $i < @{$data}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$data;}) 202779 44 202823
223 202779 my $byte_value = $data->[$i];
224 202779 ▶ if (ref $byte_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $byte_value) 1 202778 202779
225 1 die sprintf q{Unable to initialize disk directory: Invalid data type at offset %d (%s)}, $i, ref $byte_value;
226 }
227 202778 ▶ unless ($self->_is_valid_byte_value($byte_value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_valid_byte_value($byte_value)) 2 202776 202778
228 2 die sprintf q{Unable to initialize disk directory: Invalid byte value at offset %d (%s)}, $i, $self->_dump($byte_value);
229 }
230 }
231  
232 44 return @{$data};
233 }
234  
235 60 ▶sub _validate_sectors {
Subroutine _validate_sectors: called CC=7 SCAR=19.9
236 60 my ($self, $sectors) = @_;
237  
238 60 my $expected_sectors_size = $TOTAL_SECTOR_COUNT;
239  
240 60 ▶ unless (scalar (@{$sectors}) == $expected_sectors_size) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$sectors;} == $expected_sectors_size) 3 57 60
241 3 die sprintf q{Unable to initialize disk directory: Invalid number of sectors (got %d sectors, but required %d)}, scalar (@{$sectors}), $expected_sectors_size;
242 }
243  
244 # Remove duplicate sectors (objects sharing the same track/sector position):
245 57 my $count_removed = $self->_remove_duplicate_sectors($sectors);
246  
247 57 ▶ unless (defined $sectors) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sectors) 0 57 57
248 0 die sprintf q{Unable to initialize disk directory: Undefined value of sectors (expected %d sectors)}, $expected_sectors_size;
249 }
250  
251 57 ▶ unless (ref $sectors eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $sectors eq "ARRAY") 0 57 57
252 0 die sprintf q{Unable to initialize disk directory: Invalid arguments given (expected %d sectors)}, $expected_sectors_size;
253 }
254  
255 57 ▶ unless (scalar (@{$sectors}) == $expected_sectors_size) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$sectors;} == $expected_sectors_size) 1 56 57
256 1 die sprintf q{Unable to initialize disk directory: Invalid number of sectors (got %d sectors, but required %d)}, scalar (@{$sectors}), $expected_sectors_size;
257 }
258  
259 56 ▶ for (my $i = 0; $i < @{$sectors}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$sectors;}) 1008 56 1064
260 1008 my $sector_value = $sectors->[$i];
261 1008 ▶ unless ($sector_value->isa('D64::Disk::Layout::Sector')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sector_value->isa("D64::Disk::Layout::Sector")) 0 1008 1008
262 0 die sprintf q{Unable to initialize disk directory: Invalid sector type at offset %d (%s)}, $i, ref $sector_value;
263 }
264 }
265  
266 56 return $sectors;
267 }
268  
269 57 ▶sub _remove_duplicate_sectors {
Subroutine _remove_duplicate_sectors: called CC=5 SCAR=16.1
270 57 my ($self, $sectors) = @_;
271  
272 57 my $count_removed = 0;
273  
274 57 ▶ for (my $i = 0; $i < @{$sectors}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$sectors;}) 1025 57 1082
275 1025 my $sector_object = $sectors->[$i];
276 1025 my $track = $sector_object->track();
277 1025 my $sector = $sector_object->sector();
278 1025 ▶ for (my $j = $i + 1; $j < @{$sectors}; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$sectors;}) 8716 1025 9741
279 8716 my $test_sector = $sectors->[$j];
280 8716 ▶ if ($test_sector->track() == $track && $test_sector->sector() == $sector) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test_sector->track == $track and $test_sector->sector == $sector) 1 8715 8716
Condition66%2/3
$test_sector->track == $track and $test_sector->sector == $sector
!ll&&!rl&&r
087151
MC/DC50%1/2
$test_sector->track == $track and $test_sector->sector == $sector
$test_sector->track == $track $test_sector->sector == $sector
Truth table66%2/3
A and B
A= $test_sector->track == $trackB= $test_sector->sector == $sector
ABresult
0X0
100
111
281 1 splice @{$sectors}, $j, 1;
282 1 $j--;
283 1 $count_removed++;
284 }
285 }
286 }
287  
288 57 return $count_removed;
289 }
290  
291 1080 ▶sub _find_sector {
Subroutine _find_sector: called CC=6 SCAR=19.3
292 1080 my ($self, $sectors, $track, $sector) = @_;
293  
294 1080 ▶ return unless defined $track && defined $sector;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $track and defined $sector 0 1080 1080
Condition33%1/3
defined $track and defined $sector
!ll&&!rl&&r
001080
MC/DC0%0/2
defined $track and defined $sector
defined $track defined $sector
Truth table33%1/3
A and B
A= defined $trackB= defined $sector
ABresult
0X0
100
111
295  
296 1080 for my $sector_object (@{$sectors}) {
297 9664 ▶ if ($sector_object->track() == $track && $sector_object->sector() == $sector) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sector_object->track == $track and $sector_object->sector == $sector) 1080 8584 9664
Condition66%2/3
$sector_object->track == $track and $sector_object->sector == $sector
!ll&&!rl&&r
085841080
MC/DC50%1/2
$sector_object->track == $track and $sector_object->sector == $sector
$sector_object->track == $track $sector_object->sector == $sector
Truth table66%2/3
A and B
A= $sector_object->track == $trackB= $sector_object->sector == $sector
ABresult
0X0
100
111
298 1080 return $sector_object;
299 }
300 }
301  
302 0 return undef;
303 }
304  
305 142 ▶sub _validate_items {
Subroutine _validate_items: called CC=7 SCAR=20.7
306 142 my ($self, $items) = @_;
307  
308 142 my $expected_items_size = $ITEMS_PER_SECTOR * $TOTAL_SECTOR_COUNT;
309  
310 142 ▶ unless (defined $items) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $items) 0 142 142
311 0 die sprintf q{Unable to initialize disk directory: Undefined value of items (expected up to %d items)}, $expected_items_size;
312 }
313  
314 142 ▶ unless (ref $items eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $items eq "ARRAY") 0 142 142
315 0 die sprintf q{Unable to initialize disk directory: Invalid arguments given (expected up to %d items)}, $expected_items_size;
316 }
317  
318 142 ▶ unless (scalar (@{$items}) <= $expected_items_size) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$items;} <= $expected_items_size) 1 141 142
319 1 die sprintf q{Unable to initialize disk directory: Invalid number of items (got %d items, but required up to %d)}, scalar (@{$items}), $expected_items_size;
320 }
321  
322 141 ▶ for (my $i = 0; $i < @{$items}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$items;}) 1671 141 1812
323 1671 my $item_value = $items->[$i];
324 1671 ▶ unless ($item_value->isa('D64::Disk::Dir::Item')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($item_value->isa("D64::Disk::Dir::Item")) 0 1671 1671
325 0 die sprintf q{Unable to initialize disk directory: Invalid item type at offset %d (%s)}, $i, ref $item_value;
326 }
327 1671 ▶ unless ($item_value->validate()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($item_value->validate) 0 1671 1671
328 0 die sprintf q{Unable to initialize disk directory: Invalid item value at offset %d (%s)}, $i, $self->_dump($item_value);
329 }
330 }
331  
332 141 return undef;
333 }
334  
335 =head2 data
336  
337 Fetch directory object data as a scalar of 18 * 256 bytes:
338  
339 my $data = $dir->data();
340  
341 Fetch directory object data as an array of 18 * 256 bytes:
342  
343 my @data = $dir->data();
344  
345 Replace directory providing 18 * 256 bytes of scalar data:
346  
347 $dir->data($data);
348  
349 Replace directory given array with 18 * 256 bytes of data:
350  
351 $dir->data(@data);
352 $dir->data(\@data);
353  
354 =cut
355  
356 155 ▶sub data {
Subroutine data: called CC=14 SCAR=26.4
Pod documented
357 155 my ($self, @args) = @_;
358  
359 155 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 55 100 155
360 55 my ($arg) = @args;
361 55 ▶ my $data = (scalar @args == 1) ? $arg : \@args;
Branch: 1 branch
BranchTrueFalseTotal
scalar @args == 1 ? : 50 5 55
362 55 my @data = $self->_validate_data($data);
363  
364 44 my $iter = $self->_get_order_from_data(\@data);
365 44 my ($track_order, $sector_order) = $self->_get_order($iter);
366  
367 ## TODO: Optimize code below by constructing directory "items" directly here!!!
368  
369 # Convert data into sectors and initialize object:
370 44 my @sectors;
371 44 ▶ while (my @sector_data = splice @data, 0, $SECTOR_DATA_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@sector_data) = splice(@data, 0, $D64::Disk::Layout::Dir::SECTOR_DATA_SIZE)) 792 44 836
372 792 my $track = CORE::shift @{$track_order};
373 792 my $sector = CORE::shift @{$sector_order};
374 792 my $sector_object = D64::Disk::Layout::Sector->new(data => \@sector_data, track => $track, sector => $sector);
375 792 CORE::push @sectors, $sector_object;
376 }
377 44 $self->sectors(@sectors);
378 }
379  
380 144 my $items = $self->{items};
381 144 my $num_items = $self->num_items();
382  
383 # Get directory object data as an array of bytes:
384 144 my @data;
385 144 ▶ for (my $i = 0; $i < @{$items}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$items;}) 20736 144 20880
386 20736 my @item_data = $items->[$i]->data();
387 20736 ▶ if ($i % $ITEMS_PER_SECTOR == 0 && ($i + $ITEMS_PER_SECTOR) < $num_items) {
Branch: 4 branches
BranchTrueFalseTotal
if ($i % $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR == 0 and $i + $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR < $num_items) { } 21 20715 20736
elsif ($i % $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR == 0 and $i + $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR >= $num_items and $i < $num_items) { } 118 20597 20715
elsif ($i == 0 and $num_items == 0) { } 26 20571 20597
elsif ($i % $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR == 0) { } 2427 18144 20571
Condition100%3/3
$i % $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR == 0 and $i + $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR < $num_items
!ll&&!rl&&r
18144257121
MC/DC100%2/2
$i % $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR == 0 and $i + $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR < $num_items
$i % $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR == 0 $i + $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR < $num_items
Truth table100%3/3
A and B
A= $i % $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR == 0B= $i + $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR < $num_items
ABresult
0X0
100
111
388 # Add information about the next directory track/sector data:
389 21 CORE::push @data, chr $self->{track_order}->[$i / $ITEMS_PER_SECTOR + 1];
390 21 CORE::push @data, chr $self->{sector_order}->[$i / $ITEMS_PER_SECTOR + 1];
391 }
392 ▶ elsif ($i % $ITEMS_PER_SECTOR == 0 && ($i + $ITEMS_PER_SECTOR) >= $num_items && $i < $num_items) {
Condition83%5/6
$i % $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR == 0 and $i + $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR >= $num_items and $i < $num_items
!ll&&!rl&&r
181442453118
$i % $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR == 0 and $i + $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR >= $num_items
!ll&&!rl&&r
1814402571
MC/DC66%2/3
$i % $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR == 0 and $i + $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR >= $num_items and $i < $num_items
$i % $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR == 0 $i + $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR >= $num_items $i < $num_items
Truth table75%3/4
A and B and C
A= $i % $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR == 0B= $i + $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR >= $num_itemsC= $i < $num_items
ABCresult
0XX0
10X0
1100
1111
393 118 CORE::push @data, chr (0x00), chr (0xff);
394 }
395 ▶ elsif ($i == 0 && $num_items == 0) {
Condition66%2/3
$i == 0 and $num_items == 0
!ll&&!rl&&r
20571026
MC/DC50%1/2
$i == 0 and $num_items == 0
$i == 0 $num_items == 0
Truth table66%2/3
A and B
A= $i == 0B= $num_items == 0
ABresult
0X0
100
111
396 26 CORE::push @data, chr (0x00), chr (0xff);
397 }
398 elsif ($i % $ITEMS_PER_SECTOR == 0) {
399 2427 CORE::push @data, chr (0x00), chr (0xff);
400 }
401 else {
402 18144 CORE::push @data, chr (0x00), chr (0x00);
403 }
404 20736 CORE::push @data, @item_data;
405 }
406  
407 144 ▶ return wantarray ? @data : join '', @data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 25 119 144
408 }
409  
410 44 ▶sub _get_order_from_data {
Subroutine _get_order_from_data: called CC=1 SCAR=0.0
411 44 my ($self, $data) = @_;
412  
413 44 my $i = 0;
414  
415 56 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
416 56 my $index = $SECTOR_DATA_SIZE * $i++;
417  
418 56 my $track = ord $data->[$index + 0];
419 56 my $sector = ord $data->[$index + 1];
420  
421 56 return ($track, $sector);
422 44 };
423 }
424  
425 100 ▶sub _get_order {
Subroutine _get_order: called CC=7 SCAR=19.5
426 100 my ($self, $next) = @_;
427  
428 100 my @track_order = @TRACK_WRITE_ORDER;
429 100 my @sector_order = @SECTOR_WRITE_ORDER;
430  
431 100 $sector_order[0] = _magic_to_int($DIRECTORY_FIRST_SECTOR);
432  
433 100 ▶ for (my $i = 0; $i < @sector_order; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @sector_order) 128 0 128
434 128 my ($track, $sector) = $next->();
435  
436 128 ▶ last if $track == 0x00;
Branch: 1 branch
BranchTrueFalseTotal
if $track == 0 100 28 128
437  
438 28 splice @track_order, $i + 1, 0, $track;
439 28 splice @sector_order, $i + 1, 0, $sector;
440 }
441  
442 # Remove duplicated track/sector order pairs:
443 100 ▶ for (my $i = 0; $i < @sector_order; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @sector_order) 1800 100 1900
444 1800 my $track = $track_order[$i];
445 1800 my $sector = $sector_order[$i];
446 1800 ▶ for (my $j = $i + 1; $j < @sector_order; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @sector_order) 15356 1800 17156
447 15356 ▶ if ($track_order[$j] == $track && $sector_order[$j] == $sector) {
Branch: 1 branch
BranchTrueFalseTotal
if ($track_order[$j] == $track and $sector_order[$j] == $sector) 28 15328 15356
Condition66%2/3
$track_order[$j] == $track and $sector_order[$j] == $sector
!ll&&!rl&&r
01532828
MC/DC50%1/2
$track_order[$j] == $track and $sector_order[$j] == $sector
$track_order[$j] == $track $sector_order[$j] == $sector
Truth table66%2/3
A and B
A= $track_order[$j] == $trackB= $sector_order[$j] == $sector
ABresult
0X0
100
111
448 28 splice @track_order, $j, 1;
449 28 splice @sector_order, $j, 1;
450 28 $j--;
451 }
452 }
453 }
454  
455 100 return (\@track_order, \@sector_order);
456 }
457  
458 =head2 items
459  
460 Fetch directory object data as an array of up to 18 * 8 items:
461  
462 my @items = $dir->items();
463  
464 This method returns only non-empty directory items.
465  
466 Replace entire directory providing an array of up to 18 * 8 items:
467  
468 $dir->items(@items);
469 $dir->items(\@items);
470  
471 An entire directory object data will be replaced when calling this method. This will happen even when number of items provided as an input parameter is less than the number of non-empty items stored in an object before method was invoked.
472  
473 =cut
474  
475 163 ▶sub items {
Subroutine items: called CC=6 SCAR=17.9
Pod documented
476 163 my ($self, @args) = @_;
477  
478 163 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 142 21 163
479 142 my ($arg) = @args;
480 142 ▶ my $items = (scalar @args == 1) ? (ref $arg ? $arg : [ $arg ]) : \@args;
Branch: 2 branches
BranchTrueFalseTotal
scalar @args == 1 ? : 136 6 142
ref $arg ? : 136 0 136
481 142 $self->_validate_items($items);
482  
483 141 my $object = $self->_init();
484 141 $self->{items} = $object->{items};
485 141 $self->{sector_order} = $object->{sector_order};
486 141 $self->{track_order} = $object->{track_order};
487  
488 141 my $i = 0;
489  
490 141 for my $item (@{$items}) {
491 1671 $self->{items}->[$i] = $item->clone();
492 1671 $i++;
493 }
494 }
495  
496 162 my $items = $self->{items};
497 162 my $num_items = $self->num_items();
498  
499 162 my @items;
500  
501 162 ▶ for (my $i = 0; $i < $num_items; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num_items) 765 162 927
502 765 CORE::push @items, $items->[$i]->clone();
503 }
504  
505 162 return @items;
506 }
507  
508 =head2 num_items
509  
510 Get count of non-empty items stored in a disk directory:
511  
512 my $num_items = $dir->num_items();
513  
514 =cut
515  
516 645 ▶sub num_items {
Subroutine num_items: called CC=3 SCAR=11.4
Pod documented
517 645 my ($self, @args) = @_;
518  
519 645 my $items = $self->{items};
520  
521 645 ▶ for (my $i = 0; $i < @{$items}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$items;}) 2854 0 2854
522 2854 my $item = $items->[$i];
523  
524 2854 ▶ return $i if $item->empty();
Branch: 1 branch
BranchTrueFalseTotal
if $item->empty 645 2209 2854
525 }
526  
527 0 return scalar @{$items};
528 }
529  
530 49 ▶sub _last_item_index {
Subroutine _last_item_index: called CC=1 SCAR=0.0
531 49 my ($self) = @_;
532  
533 49 my $num_items = $self->num_items();
534  
535 49 return $num_items - 1; # -1 .. ($ITEMS_PER_SECTOR * $TOTAL_SECTOR_COUNT - 1)
536 }
537  
538 =head2 sectors
539  
540 Fetch directory object data as an array of 18 * sector objects:
541  
542 my @sectors = $dir->sectors();
543  
544 Replace entire directory providing an array of 18 * sector objects:
545  
546 $dir->sectors(@sectors);
547 $dir->sectors(\@sectors);
548  
549 =cut
550  
551 72 ▶sub sectors {
Subroutine sectors: called CC=18 SCAR=28.9
Pod documented
552 72 my ($self, @args) = @_;
553  
554 72 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 60 12 72
555 60 my ($arg) = @args;
556 60 ▶ my $sectors = (scalar @args == 1) ? (ref $arg ? $arg : [ $arg ]) : \@args;
Branch: 2 branches
BranchTrueFalseTotal
scalar @args == 1 ? : 11 49 60
ref $arg ? : 11 0 11
557 60 $sectors = $self->_validate_sectors($sectors);
558  
559 56 my $object = $self->_init();
560 56 $self->{items} = $object->{items};
561  
562 56 my $iter = $self->_get_order_from_sectors($sectors);
563 56 my ($track_order, $sector_order) = $self->_get_order($iter);
564  
565 56 $self->{sector_order} = $sector_order;
566 56 $self->{track_order} = $track_order;
567  
568 56 my $sector = $sector_order->[0];
569 56 my $track = $track_order->[0];
570  
571 56 my $index = 0;
572 56 ▶ while (my $sector_object = $self->_find_sector($sectors, $track, $sector)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sector_object = $self->_find_sector($sectors, $track, $sector)) 1008 0 1008
573 1008 my @items = $self->_sector_to_items($sector_object);
574  
575 1008 splice @{$self->{items}}, $index * $ITEMS_PER_SECTOR, $ITEMS_PER_SECTOR, @items;
576  
577 1008 $index++;
578  
579 1008 $sector = $sector_order->[$index];
580 1008 $track = $track_order->[$index];
581  
582 1008 ▶ last unless defined $track && defined $sector;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $track and defined $sector 56 952 1008
Condition66%2/3
defined $track and defined $sector
!ll&&!rl&&r
560952
MC/DC50%1/2
defined $track and defined $sector
defined $track defined $sector
Truth table66%2/3
A and B
A= defined $trackB= defined $sector
ABresult
0X0
100
111
583 }
584 }
585  
586 68 my $items = $self->{items};
587 68 my $num_items = $self->num_items();
588  
589 # Get directory object data as an array of sectors:
590 68 my @sectors;
591 68 ▶ for (my $i = 0; $i < $TOTAL_SECTOR_COUNT; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $D64::Disk::Layout::Dir::TOTAL_SECTOR_COUNT) 1224 68 1292
592 1224 my $track = $self->{track_order}->[$i];
593 1224 my $sector = $self->{sector_order}->[$i];
594  
595 1224 my @data;
596 1224 ▶ for (my $j = 0; $j < $ITEMS_PER_SECTOR; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR) 9792 1224 11016
597 9792 my @item_data = $items->[$i * $ITEMS_PER_SECTOR + $j]->data();
598 9792 ▶ if ($j == 0 && ($i + 1) * $ITEMS_PER_SECTOR < $num_items) {
Branch: 4 branches
BranchTrueFalseTotal
if ($j == 0 and ($i + 1) * $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR < $num_items) { } 38 9754 9792
elsif ($j == 0 and ($i + 1) * $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR >= $num_items and $i * $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR < $num_items) { } 57 9697 9754
elsif ($i == 0 and $j == 0 and $num_items == 0) { } 11 9686 9697
elsif ($j == 0) { } 1118 8568 9686
Condition100%3/3
$j == 0 and ($i + 1) * $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR < $num_items
!ll&&!rl&&r
8568118638
MC/DC100%2/2
$j == 0 and ($i + 1) * $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR < $num_items
$j == 0 ($i + 1) * $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR < $num_items
Truth table100%3/3
A and B
A= $j == 0B= ($i + 1) * $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR < $num_items
ABresult
0X0
100
111
599 # Add information about the next directory track/sector data:
600 38 CORE::push @data, chr $self->{track_order}->[$i + 1];
601 38 CORE::push @data, chr $self->{sector_order}->[$i + 1];
602 }
603 ▶ elsif ($j == 0 && ($i + 1) * $ITEMS_PER_SECTOR >= $num_items && $i * $ITEMS_PER_SECTOR < $num_items) {
Condition83%5/6
$j == 0 and ($i + 1) * $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR >= $num_items and $i * $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR < $num_items
!ll&&!rl&&r
8568112957
$j == 0 and ($i + 1) * $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR >= $num_items
!ll&&!rl&&r
856801186
MC/DC66%2/3
$j == 0 and ($i + 1) * $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR >= $num_items and $i * $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR < $num_items
$j == 0 ($i + 1) * $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR >= $num_items $i * $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR < $num_items
Truth table75%3/4
A and B and C
A= $j == 0B= ($i + 1) * $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR >= $num_itemsC= $i * $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR < $num_items
ABCresult
0XX0
10X0
1100
1111
604 57 CORE::push @data, chr (0x00), chr (0xff);
605 }
606 ▶ elsif ($i == 0 && $j == 0 && $num_items == 0) {
Condition83%5/6
$i == 0 and $j == 0 and $num_items == 0
!ll&&!rl&&r
9686011
$i == 0 and $j == 0
!ll&&!rl&&r
921047611
MC/DC66%2/3
$i == 0 and $j == 0 and $num_items == 0
$i == 0 $j == 0 $num_items == 0
Truth table75%3/4
A and B and C
A= $i == 0B= $j == 0C= $num_items == 0
ABCresult
0XX0
10X0
1100
1111
607 11 CORE::push @data, chr (0x00), chr (0xff);
608 }
609 elsif ($j == 0) {
610 1118 CORE::push @data, chr (0x00), chr (0xff);
611 }
612 else {
613 8568 CORE::push @data, chr (0x00), chr (0x00);
614 }
615 9792 CORE::push @data, @item_data;
616 }
617  
618 1224 my $sector_object = D64::Disk::Layout::Sector->new(data => \@data, track => $track, sector => $sector);
619 1224 CORE::push @sectors, $sector_object;
620 }
621  
622 68 return @sectors;
623 }
624  
625 =head2 num_sectors
626  
627 Get total number of allocated sectors that can be used to store disk directory data:
628  
629 my $num_sectors = $dir->num_sectors(count => 'all');
630  
631 In the case of a C<D64> disk image format, the value of C<18> is always returned, as this is a standard number of sectors designated to store disk directory data.
632  
633 Get number of currently used sectors that are used to store actual disk directory data:
634  
635 my $num_sectors = $dir->num_sectors(count => 'used');
636  
637 In this case method call returns an integer value between C<0> and C<18> (total count of sectors used to store actual data), i.a. for an empty disk directory C<0> is returned, and for a disk directory filled with more than 136 files the value of C<18> will be retrieved.
638  
639 C<count> parameter defaults to C<all>.
640  
641 =cut
642  
643 5 ▶sub num_sectors {
Subroutine num_sectors: called CC=5 SCAR=16.4
Pod documented
644 5 my ($self, %args) = @_;
645  
646 5 ▶ my $mode = $args{'count'} || 'all';
Condition50%1/2
$args{'count'} || 'all'
l!l
50
MC/DC0%0/1
$args{'count'} || 'all'
$args{'count'}
Truth table50%1/2
A
A= $args{'count'}
Aresult
11
00
647  
648 5 ▶ if ($mode eq 'all') {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'all') { } 2 3 5
elsif ($mode eq 'used') { } 3 0 3
649 2 return $TOTAL_SECTOR_COUNT;
650 }
651 elsif ($mode eq 'used') {
652 3 my $last_item_index = $self->_last_item_index();
653  
654 3 ▶ while (++$last_item_index % 8) {};
Branch: 1 branch
BranchTrueFalseTotal
if (++$last_item_index % 8) 9 3 12
655  
656 3 return int ($last_item_index / 8);
657 }
658 else {
659 0 die sprintf q{Invalid value of "count" parameter: %s}, $mode;
660 }
661 }
662  
663 56 ▶sub _get_order_from_sectors {
Subroutine _get_order_from_sectors: called CC=1 SCAR=0.0
664 56 my ($self, $sectors) = @_;
665  
666 56 my $track = $DIRECTORY_FIRST_TRACK;
667 56 my $sector = $DIRECTORY_FIRST_SECTOR;
668  
669 72 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
670 72 my $sector_object = $self->_find_sector($sectors, $track, $sector);
671 72 ▶ return unless $sector_object;
Branch: 1 branch
BranchTrueFalseTotal
unless $sector_object 0 72 72
672  
673 72 my $sector_data = $sector_object->data();
674  
675 72 $track = ord substr $sector_data, 0, 1;
676 72 $sector = ord substr $sector_data, 1, 1;
677  
678 72 return ($track, $sector);
679 56 };
680 }
681  
682 1008 ▶sub _sector_to_items {
Subroutine _sector_to_items: called CC=2 SCAR=6.9
683 1008 my ($self, $sector_object) = @_;
684  
685 1008 my @data = $sector_object->data();
686  
687 1008 my @items;
688  
689 1008 ▶ for (my $i = 0; $i < $ITEMS_PER_SECTOR; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $D64::Disk::Layout::Dir::ITEMS_PER_SECTOR) 8064 1008 9072
690 8064 my $index = 2 + $i * ($ITEM_SIZE + 2);
691 8064 my @item_data = @data[$index .. $index + $ITEM_SIZE - 1];
692 8064 CORE::push @items, D64::Disk::Dir::Item->new(@item_data);
693 }
694  
695 1008 return @items;
696 }
697  
698 =head2 get
699  
700 Fetch an item from a directory listing at any given position:
701  
702 my $item = $dir->get(index => $index);
703  
704 C<$index> indicates an offset from the beginning of a directory listing, with count starting from C<0>. When C<$index> indicates an element beyond the number of non-empty items stored in a disk directory, an undefined value will be returned.
705  
706 Fetch a list of items from a directory listing matching given PETSCII pattern:
707  
708 use Text::Convert::PETSCII qw(:convert);
709  
710 my $pattern = ascii_to_petscii 'workstage*';
711  
712 my @items = $dir->get(pattern => $pattern);
713  
714 C<pattern> is expected to be any valid PETSCII text string. Such call to this method always returns B<all> items with filename matching given PETSCII pattern.
715  
716 =cut
717  
718 17 ▶sub get {
Subroutine get: called CC=9 SCAR=22.3
Pod documented
719 17 my ($self, %args) = @_;
720  
721 17 ▶ if (exists $args{index} && exists $args{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"index"} and exists $args{"pattern"}) 0 17 17
Condition66%2/3
exists $args{"index"} and exists $args{"pattern"}
!ll&&!rl&&r
6110
MC/DC0%0/2
exists $args{"index"} and exists $args{"pattern"}
exists $args{"index"} exists $args{"pattern"}
Truth table66%2/3
A and B
A= exists $args{"index"}B= exists $args{"pattern"}
ABresult
0X0
100
111
722 0 die q{Unable to fetch an item from a directory listing: ambiguous file index/matching pattern specified (you cannot specify both parameters at the same time)};
723 }
724  
725 17 ▶ unless (exists $args{index} || exists $args{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"index"} or exists $args{"pattern"}) 0 17 17
Condition66%2/3
exists $args{"index"} or exists $args{"pattern"}
l!l&&r!l&&!r
1160
MC/DC0%0/2
exists $args{"index"} or exists $args{"pattern"}
exists $args{"index"} exists $args{"pattern"}
Truth table66%2/3
A or B
A= exists $args{"index"}B= exists $args{"pattern"}
ABresult
1X1
011
000
726 0 die q{Unable to fetch an item from a directory listing: Missing index/pattern parameter (which element did you want to get?)};
727 }
728  
729 17 my $index = $args{index};
730 17 my $pattern = $args{pattern};
731  
732 17 ▶ if (exists $args{index}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'index'}) { } 11 6 17
733  
734 11 $self->_validate_index($index, 'get');
735  
736 6 my $num_items = $self->num_items();
737 6 my $items = $self->{items};
738  
739 6 ▶ if ($index < $num_items) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < $num_items) { } 4 2 6
740 4 return $items->[$index];
741 }
742 else {
743 2 return undef;
744 }
745 }
746 else {
747  
748 6 $self->_validate_pattern($pattern, 'get');
749  
750 6 my @items = $self->items();
751  
752 6 for my $item (@items) {
753 18 my $is_matched = $item->match_name($pattern);
754  
755 18 ▶ $item = undef unless $is_matched;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_matched 7 11 18
756 }
757  
758 6 return grep { defined } @items;
759 }
760 }
761  
762 63 ▶sub _validate_index {
Subroutine _validate_index: called CC=5 SCAR=16.1
763 63 my ($self, $index, $operation) = @_;
764  
765 63 my $items = $self->{items};
766 63 my $maximum_allowed_position = scalar (@{$items}) - 1;
767  
768 63 ▶ if (D64::Disk::Dir::Item->is_int($index) && $index >= 0x00 && $index <= $maximum_allowed_position) {
Branch: 1 branch
BranchTrueFalseTotal
if ("D64::Disk::Dir::Item"->is_int($index) and $index >= 0 and $index <= $maximum_allowed_position) 39 24 63
Condition100%6/6
"D64::Disk::Dir::Item"->is_int($index) and $index >= 0 and $index <= $maximum_allowed_position
!ll&&!rl&&r
19539
"D64::Disk::Dir::Item"->is_int($index) and $index >= 0
!ll&&!rl&&r
14544
MC/DC100%3/3
"D64::Disk::Dir::Item"->is_int($index) and $index >= 0 and $index <= $maximum_allowed_position
"D64::Disk::Dir::Item"->is_int($index) $index >= 0 $index <= $maximum_allowed_position
Truth table100%4/4
A and B and C
A= "D64::Disk::Dir::Item"->is_int($index)B= $index >= 0C= $index <= $maximum_allowed_position
ABCresult
0XX0
10X0
1100
1111
769 39 return undef;
770 }
771  
772 24 ▶ my $dumped_index = $self->_is_valid_number_value($index) ? $index : $self->_dump($index);
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_valid_number_value($index) ? : 5 19 24
773  
774 24 my %description = (
775 'add' => 'Unable to add an item to a directory listing',
776 'delete' => 'Unable to mark disk directory item as deleted',
777 'get' => 'Unable to fetch an item from a directory listing',
778 'put' => 'Unable to put an item to a directory listing',
779 'remove' => 'Unable to entirely remove directory item',
780 );
781  
782 24 die sprintf q{%s: Invalid index parameter (got "%s", but expected an integer between 0 and %d)}, $description{$operation}, $dumped_index, $maximum_allowed_position;
783 }
784  
785 56 ▶sub _validate_pattern {
Subroutine _validate_pattern: called CC=10 SCAR=23.0
786 56 my ($self, $pattern, $operation) = @_;
787  
788 56 ▶ if (defined ($pattern) && !ref ($pattern) && is_valid_petscii_string($pattern) && length ($pattern) > 0 && length ($pattern) <= 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pattern and not ref $pattern and is_valid_petscii_string($pattern) and length $pattern > 0 and length $pattern <= 16) 46 10 56
Condition100%12/12
defined $pattern and not ref $pattern and is_valid_petscii_string($pattern) and length $pattern > 0 and length $pattern <= 16
!ll&&!rl&&r
8246
defined $pattern and not ref $pattern and is_valid_petscii_string($pattern) and length $pattern > 0
!ll&&!rl&&r
6248
defined $pattern and not ref $pattern and is_valid_petscii_string($pattern)
!ll&&!rl&&r
4250
defined $pattern and not ref $pattern
!ll&&!rl&&r
2252
MC/DC100%5/5
defined $pattern and not ref $pattern and is_valid_petscii_string($pattern) and length $pattern > 0 and length $pattern <= 16
defined $pattern not ref $pattern is_valid_petscii_string($pattern) length $pattern > 0 length $pattern <= 16
Truth table100%6/6
A and not(B) and C and D and E
A= defined $patternB= not ref $patternC= is_valid_petscii_string($pattern)D= length $pattern > 0E= length $pattern <= 16
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
789 46 return undef;
790 }
791  
792 10 ▶ my $pattern_to_dump = ref ($pattern) ? $pattern :
Branch: 2 branches
BranchTrueFalseTotal
ref $pattern ? : 2 8 10
is_printable_petscii_string($pattern) ? : 4 4 8
793 is_printable_petscii_string($pattern) ? petscii_to_ascii($pattern) :
794 $pattern;
795  
796 10 ▶ my $dumped_pattern = !defined ($pattern) ? 'undef' :
Branch: 2 branches
BranchTrueFalseTotal
!defined($pattern) ? : 2 8 10
$self->_is_valid_number_value($pattern) ? : 0 8 8
797 $self->_is_valid_number_value($pattern) ? $pattern :
798 $self->_dump($pattern_to_dump);
799  
800 10 $dumped_pattern =~ s/^"(.*)"$/$1/;
801 10 $dumped_pattern =~ s/^'(.*)'$/$1/;
802  
803 10 my %description = (
804 'delete' => 'Unable to mark disk directory item as deleted',
805 'get' => 'Unable to fetch an item from a directory listing',
806 'remove' => 'Unable to entirely remove directory item',
807 );
808  
809 10 die sprintf q{%s: Invalid pattern parameter (got "%s", but expected a valid PETSCII text string)}, $description{$operation}, $dumped_pattern;
810 }
811  
812 60 ▶sub _validate_item_object {
Subroutine _validate_item_object: called CC=4 SCAR=13.9
813 60 my ($self, $item, $operation) = @_;
814  
815 60 my %description = (
816 'add' => 'Unable to add an item to a directory listing',
817 'prepended' => 'Failed to validate prepended directory item',
818 'pushed' => 'Failed to validate pushed directory item',
819 'put' => 'Unable to put an item to a directory listing',
820 );
821  
822 60 ▶ unless (defined $item) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $item) 2 58 60
823 2 die sprintf q{%s: Undefined item parameter (expected valid item object)}, $description{$operation};
824 }
825  
826 58 ▶ unless (ref $item && $item->isa('D64::Disk::Dir::Item')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $item and $item->isa("D64::Disk::Dir::Item")) 4 50 54
Condition66%2/3
ref $item and $item->isa("D64::Disk::Dir::Item")
!ll&&!rl&&r
4054
MC/DC50%1/2
ref $item and $item->isa("D64::Disk::Dir::Item")
ref $item $item->isa("D64::Disk::Dir::Item")
Truth table66%2/3
A and B
A= ref $itemB= $item->isa("D64::Disk::Dir::Item")
ABresult
0X0
100
111
827 4 die sprintf q{%s: Invalid item parameter (got "%s", but expected a valid item object)}, $description{$operation}, ref $item;
828 }
829  
830 50 return undef;
831 }
832  
833 =head2 push
834  
835 Append an item to the end of directory listing, increasing number of files by one element:
836  
837 $dir->push(item => $item);
838  
839 C<$item> is expected to be a valid C<D64::Disk::Dir::Item> object. This method will not work when number of non-empty items stored in a disk directory has already reached its maximum.
840  
841 =cut
842  
843 10 ▶sub push {
Subroutine push: called CC=2 SCAR=6.9
Pod documented
844 10 my ($self, %args) = @_;
845  
846 10 my $num_items = $self->num_items();
847 10 ▶ if ($num_items >= $MAX_ENTRIES) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_items >= $D64::Disk::Layout::Dir::MAX_ENTRIES) 1 9 10
848 1 warn sprintf q{Unable to push another item to a directory listing, maximum number of %d entries has been reached}, $MAX_ENTRIES;
849 }
850  
851 10 my $item = $args{item};
852 10 $self->_validate_item_object($item, 'pushed');
853  
854 7 my $last_item_index = $self->_last_item_index();
855  
856 7 $self->{items}->[$last_item_index + 1] = $item->clone();
857  
858 7 $num_items = $self->num_items();
859  
860 7 return $num_items;
861 }
862  
863 =head2 pop
864  
865 Pop and return the last non-empty directory item, shortening a directory listing by one element:
866  
867 my $item = $dir->pop();
868  
869 When there is at least one non-empty item stored in a disk directory, a C<D64::Disk::Dir::Item> object will be returned. Otherwise return value is undefined.
870  
871 =cut
872  
873 18 ▶sub pop {
Subroutine pop: called CC=2 SCAR=6.9
Pod documented
874 18 my ($self, %args) = @_;
875  
876 18 my $last_item_index = $self->_last_item_index();
877  
878 18 ▶ return if $last_item_index < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $last_item_index < 0 4 14 18
879  
880 14 my $item = $self->{items}->[$last_item_index];
881 14 $self->{items}->[$last_item_index] = D64::Disk::Dir::Item->new();
882  
883 14 return $item->clone();
884 }
885  
886 =head2 shift
887  
888 Shift the first directory item, shortening a directory listing by one and moving everything down:
889  
890 my $item = $dir->shift();
891  
892 When there is at least one non-empty item stored in a disk directory, a C<D64::Disk::Dir::Item> object will be returned. Otherwise return value is undefined.
893  
894 =cut
895  
896 21 ▶sub shift {
Subroutine shift: called CC=2 SCAR=6.9
Pod documented
897 21 my ($self, %args) = @_;
898  
899 21 my $last_item_index = $self->_last_item_index();
900  
901 21 ▶ return if $last_item_index < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $last_item_index < 0 4 17 21
902  
903 17 my $items = $self->{items};
904  
905 17 my $item = CORE::shift @{$items};
906 17 CORE::push @{$items}, D64::Disk::Dir::Item->new();
907  
908 17 return $item->clone();
909 }
910  
911 =head2 unshift
912  
913 Prepend an item to the front of directory listing, and return the new number of elements:
914  
915 my $num_items = $dir->unshift(item => $item);
916  
917 C<$item> is expected to be a valid C<D64::Disk::Dir::Item> object. This method will not work when number of non-empty items stored in a disk directory has already reached its maximum.
918  
919 =cut
920  
921 13 ▶sub unshift {
Subroutine unshift: called CC=2 SCAR=6.9
Pod documented
922 13 my ($self, %args) = @_;
923  
924 13 my $num_items = $self->num_items();
925 13 ▶ if ($num_items >= $MAX_ENTRIES) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_items >= $D64::Disk::Layout::Dir::MAX_ENTRIES) 1 12 13
926 1 warn sprintf q{Unable to prepend an item to the front of directory listing, maximum number of %d entries has been reached}, $MAX_ENTRIES;
927 }
928  
929 13 my $item = $args{item};
930 13 $self->_validate_item_object($item, 'prepended');
931  
932 10 my $items = $self->{items};
933 10 CORE::pop @{$items};
934 10 CORE::unshift @{$items}, $item->clone();
935  
936 10 $num_items = $self->num_items();
937  
938 10 return $num_items;
939 }
940  
941 =head2 delete
942  
943 Mark directory item designated by an offset as deleted:
944  
945 my $num_deleted = $dir->delete(index => $index);
946  
947 Mark directory item being the first one to match given PETSCII pattern as deleted:
948  
949 use Text::Convert::PETSCII qw(:convert);
950  
951 my $pattern = ascii_to_petscii 'workstage*';
952  
953 my $num_deleted = $dir->delete(pattern => $pattern, global => 0);
954  
955 Mark all directory items matching given PETSCII pattern as deleted:
956  
957 use Text::Convert::PETSCII qw(:convert);
958  
959 my $pattern = ascii_to_petscii 'workstage*';
960  
961 my $num_deleted = $dir->delete(pattern => $pattern, global => 1);
962  
963 C<pattern> is expected to be any valid PETSCII text string. C<global> parameter defaults to C<0>, hence deleting only a single file matching given criteria by default. When set to any C<true> value, it will trigger deletion of B<all> items with filename matching given PETSCII pattern.
964  
965 A call to this method always returns the number of successfully deleted items. When deleting an item designated by an offset of an already deleted directory item, such operation does not contribute to the count of successfully deleted items during such a particular method call. In other words, delete an item once, and you get it counted as a successfully deleted one, delete the same item again, and it will not be counted as a deleted one anymore. Of course an item remains delete in a directory listing, it just does not contribute to a value that is returned from this method's call.
966  
967 Note that this method does not remove an entry from directory layout, it only marks it as deleted. In order to wipe out an entry entirely, see description of L</remove> method.
968  
969 =cut
970  
971 35 ▶sub delete {
Subroutine delete: called CC=11 SCAR=24.0
Pod documented
972 35 my ($self, %args) = @_;
973  
974 35 ▶ if (exists $args{index} && exists $args{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"index"} and exists $args{"pattern"}) 1 34 35
Condition100%3/3
exists $args{"index"} and exists $args{"pattern"}
!ll&&!rl&&r
24101
MC/DC100%2/2
exists $args{"index"} and exists $args{"pattern"}
exists $args{"index"} exists $args{"pattern"}
Truth table100%3/3
A and B
A= exists $args{"index"}B= exists $args{"pattern"}
ABresult
0X0
100
111
975 1 die q{Unable to mark directory item as deleted: ambiguous deletion index/pattern specified (you cannot specify both parameters at the same time)};
976 }
977  
978 34 ▶ unless (exists $args{index} || exists $args{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"index"} or exists $args{"pattern"}) 1 33 34
Condition100%3/3
exists $args{"index"} or exists $args{"pattern"}
l!l&&r!l&&!r
10231
MC/DC100%2/2
exists $args{"index"} or exists $args{"pattern"}
exists $args{"index"} exists $args{"pattern"}
Truth table100%3/3
A or B
A= exists $args{"index"}B= exists $args{"pattern"}
ABresult
1X1
011
000
979 1 die q{Unable to mark directory item as deleted: Missing index/pattern parameter (which element did you want to delete?)};
980 }
981  
982 33 my $index = $args{index};
983 33 my $global = $args{global};
984 33 my $pattern = $args{pattern};
985  
986 33 my $num_items = $self->num_items();
987 33 my $items = $self->{items};
988  
989 33 ▶ if (exists $args{index}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'index'}) { } 10 23 33
990  
991 10 $self->_validate_index($index, 'delete');
992  
993 5 ▶ if ($index < $num_items) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < $num_items) { } 4 1 5
994 4 my $item = $items->[$index];
995 4 my $count = $self->_delete_item($item);
996 4 return $count;
997 }
998 else {
999 1 return 0;
1000 }
1001 }
1002 else {
1003  
1004 23 $self->_validate_pattern($pattern, 'delete');
1005  
1006 18 my $num_deleted = 0;
1007  
1008 18 ▶ for (my $i = 0; $i < $num_items; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num_items) 35 7 42
1009  
1010 35 my $item = $items->[$i];
1011  
1012 35 ▶ if ($item->match_name($pattern)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->match_name($pattern)) 25 10 35
1013  
1014 25 my $count = $self->_delete_item($item);
1015  
1016 25 $num_deleted += $count;
1017  
1018 # File got deleted and only one was requested to get deleted:
1019 25 ▶ last if $count and !$global;
Branch: 1 branch
BranchTrueFalseTotal
if $count and not $global 11 14 25
Condition100%3/3
$count and not $global
!ll&&!rl&&r
41011
MC/DC100%2/2
$count and not $global
$count not $global
Truth table100%3/3
A and not(B)
A= $countB= not $global
ABresult
0X0
100
111
1020 }
1021 }
1022  
1023 18 return $num_deleted;
1024 }
1025 }
1026  
1027 29 ▶sub _delete_item {
Subroutine _delete_item: called CC=3 SCAR=11.0
1028 29 my ($self, $item) = @_;
1029  
1030 29 my $was_closed = $item->closed();
1031 29 my $was_deleted = $item->type($T_DEL);
1032  
1033 29 my $is_closed = $item->closed(0);
1034 29 my $is_deleted = $item->type($T_DEL);
1035  
1036 29 ▶ if ($was_closed == $is_closed && $was_deleted == $is_deleted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($was_closed == $is_closed and $was_deleted == $is_deleted) 5 24 29
Condition66%2/3
$was_closed == $is_closed and $was_deleted == $is_deleted
!ll&&!rl&&r
2405
MC/DC50%1/2
$was_closed == $is_closed and $was_deleted == $is_deleted
$was_closed == $is_closed $was_deleted == $is_deleted
Truth table66%2/3
A and B
A= $was_closed == $is_closedB= $was_deleted == $is_deleted
ABresult
0X0
100
111
1037 5 return 0;
1038 }
1039  
1040 24 return 1;
1041 }
1042  
1043 =head2 remove
1044  
1045 Wipe out directory item designated by an offset entirely:
1046  
1047 my $num_removed = $dir->remove(index => $index);
1048  
1049 Wipe out directory item being the first one to match given PETSCII pattern entirely:
1050  
1051 use Text::Convert::PETSCII qw(:convert);
1052  
1053 my $pattern = ascii_to_petscii 'workstage*';
1054  
1055 my $num_removed = $dir->remove(pattern => $pattern, global => 0);
1056  
1057 Wipe out all directory items matching given PETSCII pattern entirely:
1058  
1059 use Text::Convert::PETSCII qw(:convert);
1060  
1061 my $pattern = ascii_to_petscii 'workstage*';
1062  
1063 my $num_removed = $dir->remove(pattern => $pattern, global => 1);
1064  
1065 C<pattern> is expected to be any valid PETSCII text string. C<global> parameter defaults to C<0>, hence removing only a single file matching given criteria by default. When set to any C<true> value, it will trigger removal of B<all> items with filename matching given PETSCII pattern.
1066  
1067 A call to this method always returns the number of successfully removed items.
1068  
1069 Note that this method removes an item from directory layout completely. It works a little bit like C<splice>, Perl's core method, removing a single element designated by an offset from an array of disk directory items, however it does not replace it with any new elements, it just shifts the remaining items, shortening a directory listing by one and moving everything from a given offset down. In order to safely mark given file as deleted without removing it from a directory listing, see description of L</delete> method.
1070  
1071 =cut
1072  
1073 39 ▶sub remove {
Subroutine remove: called CC=10 SCAR=23.0
Pod documented
1074 39 my ($self, %args) = @_;
1075  
1076 39 ▶ if (exists $args{index} && exists $args{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"index"} and exists $args{"pattern"}) 1 38 39
Condition100%3/3
exists $args{"index"} and exists $args{"pattern"}
!ll&&!rl&&r
28101
MC/DC100%2/2
exists $args{"index"} and exists $args{"pattern"}
exists $args{"index"} exists $args{"pattern"}
Truth table100%3/3
A and B
A= exists $args{"index"}B= exists $args{"pattern"}
ABresult
0X0
100
111
1077 1 die q{Unable to entirely remove directory item: ambiguous removal index/pattern specified (you cannot specify both parameters at the same time)};
1078 }
1079  
1080 38 ▶ unless (exists $args{index} || exists $args{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"index"} or exists $args{"pattern"}) 1 37 38
Condition100%3/3
exists $args{"index"} or exists $args{"pattern"}
l!l&&r!l&&!r
10271
MC/DC100%2/2
exists $args{"index"} or exists $args{"pattern"}
exists $args{"index"} exists $args{"pattern"}
Truth table100%3/3
A or B
A= exists $args{"index"}B= exists $args{"pattern"}
ABresult
1X1
011
000
1081 1 die q{Unable to entirely remove directory item: Missing index/pattern parameter (which element did you want to remove?)};
1082 }
1083  
1084 37 my $index = $args{index};
1085 37 my $global = $args{global};
1086 37 my $pattern = $args{pattern};
1087  
1088 37 my $num_items = $self->num_items();
1089 37 my $items = $self->{items};
1090  
1091 37 ▶ if (exists $args{index}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'index'}) { } 10 27 37
1092  
1093 10 $self->_validate_index($index, 'remove');
1094  
1095 5 ▶ if ($index < $num_items) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < $num_items) { } 4 1 5
1096 4 $self->_remove_item($index);
1097 4 return 1;
1098 }
1099 else {
1100 1 return 0;
1101 }
1102 }
1103 else {
1104  
1105 27 $self->_validate_pattern($pattern, 'remove');
1106  
1107 22 my $num_deleted = 0;
1108  
1109 22 ▶ for (my $i = 0; $i < $num_items; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num_items) 40 9 49
1110  
1111 40 my $item = $items->[$i];
1112  
1113 40 ▶ if ($item->match_name($pattern)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->match_name($pattern)) 24 16 40
1114  
1115 24 $self->_remove_item($i);
1116  
1117 24 $num_deleted += 1;
1118  
1119 # File got deleted and only one was requested to get deleted:
1120 24 ▶ last unless $global;
Branch: 1 branch
BranchTrueFalseTotal
unless $global 13 11 24
1121  
1122 11 $i--;
1123 11 $num_items--;
1124 }
1125 }
1126  
1127 22 return $num_deleted;
1128 }
1129 }
1130  
1131 28 ▶sub _remove_item {
Subroutine _remove_item: called CC=1 SCAR=0.0
1132 28 my ($self, $index) = @_;
1133  
1134 28 my $items = $self->{items};
1135  
1136 28 splice @{$items}, $index, 1;
1137  
1138 28 CORE::push @{$items}, D64::Disk::Dir::Item->new();
1139  
1140 28 return undef;
1141 }
1142  
1143 =head2 add
1144  
1145 Add a new directory item to a directory listing:
1146  
1147 my $is_success = $dir->add(item => $item);
1148  
1149 Add a new directory item designated by an offset:
1150  
1151 my $is_success = $dir->add(item => $item, index => $index);
1152  
1153 C<$item> is expected to be a valid C<D64::Disk::Dir::Item> object.
1154  
1155 A call to this method returns true on a successful addition of a new entry, and false otherwise. Addition of a new item may not be possible, for instance when a maximum number of allowed disk directory elements has already been reached.
1156  
1157 C<$index> indicates an offset from the beginning of a directory listing where a new item should be added, with count starting from C<0>. Note that this method will not only insert a new item into a disk directory, it will also shift the remaining items, extending a directory listing by one and moving everything from a given offset up. When C<$index> indicates an element beyond the number of non-empty items currently stored in a disk directory, subroutine will fail and an undefined value will be returned, because such operation would not make much sense (such added entry would not be obtainable from a directory listing anyway). It will also not work when number of non-empty items stored in a disk directory has already reached its maximum. Please note that this operation will not replace a "*", or "splat" file it encounters at a given offset, rather it will always it altogether with the remaining items, unlike C<add> method called without an C<index> parameter specified at all, which is described in the next paragraph.
1158  
1159 When C<$index> parameter is unspecified, the method behaves as follows. It finds the first empty slot in a directory listing (that is a first directory item with a "closed" flag unset), and writes given item at that exact position. It will however not work when there is no writable slot in a directory listing available at all. Please note that this operation may or may not write given item at the end of a directory listing, since it will replace any "*", or "splat" file it encounters earlier on its way. In most cases this is a desired behaviour, that is why it is always performed as a default action.
1160  
1161 =cut
1162  
1163 24 ▶sub add {
Subroutine add: called CC=6 SCAR=17.9
Pod documented
1164 24 my ($self, %args) = @_;
1165  
1166 24 ▶ unless (exists $args{item}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"item"}) 2 22 24
1167 2 die q{Unable to add an item to a directory listing: Missing item parameter (what element did you want to add?)};
1168 }
1169  
1170 22 my $index = $args{index};
1171 22 my $item = $args{item};
1172  
1173 22 $self->_validate_item_object($item, 'add');
1174  
1175 20 my $num_items = $self->num_items();
1176 20 my $items = $self->{items};
1177  
1178 20 ▶ unless (defined $index) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index) { } 12 8 20
1179 8 my $first_empty_slot = $self->_find_first_empty_slot();
1180  
1181 8 ▶ if (defined $first_empty_slot) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $first_empty_slot) 8 0 8
1182 8 splice @{$items}, $first_empty_slot, 0x01, $item->clone();
1183 8 return 1;
1184 }
1185 }
1186 else {
1187 12 $self->_validate_index($index, 'add');
1188  
1189 8 ▶ if ($num_items >= $MAX_ENTRIES) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_items >= $D64::Disk::Layout::Dir::MAX_ENTRIES) 1 7 8
1190 1 warn sprintf q{Unable to add another item to a directory listing, maximum number of %d entries has been reached}, $MAX_ENTRIES;
1191 }
1192  
1193 8 ▶ if ($index <= $num_items) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index <= $num_items) 7 1 8
1194 7 splice @{$items}, $index, 0x00, $item->clone();
1195 7 CORE::pop @{$items};
1196 7 return 1;
1197 }
1198 }
1199  
1200 1 return 0;
1201 }
1202  
1203 8 ▶sub _find_first_empty_slot {
Subroutine _find_first_empty_slot: called CC=3 SCAR=11.1
1204 8 my ($self) = @_;
1205  
1206 8 my $items = $self->{items};
1207  
1208 8 my $index = 0;
1209  
1210 8 ▶ while ($index < $MAX_ENTRIES) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < $D64::Disk::Layout::Dir::MAX_ENTRIES) 18 0 18
1211 18 my $item = $items->[$index];
1212 18 ▶ if ($item->writable()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->writable) 8 10 18
1213 8 return $index;
1214 }
1215 10 $index++;
1216 }
1217  
1218 0 return undef;
1219 }
1220  
1221 =head2 put
1222  
1223 Put an item to a directory listing at any given position:
1224  
1225 my $is_success = $dir->put(item => $item, index => $index);
1226  
1227 C<$item> is expected to be a valid C<D64::Disk::Dir::Item> object. A call to this method returns true on a successful put of a new entry, and false otherwise.
1228  
1229 C<$index> is a required parameter that indicates an offset from the beginning of a directory listing where a new item should be put, with count starting from C<0>. Note that this method does not just insert a new item into a disk directory, it rather replaces an existing item previously stored at a given offset. When C<$index> indicates an element beyond the number of non-empty items currently stored in a disk directory, subroutine will fail and an undefined value will be returned, because such operation would not make much sense (such added entry would not be obtainable from a directory listing anyway).
1230  
1231 =cut
1232  
1233 22 ▶sub put {
Subroutine put: called CC=4 SCAR=13.9
Pod documented
1234 22 my ($self, %args) = @_;
1235  
1236 22 ▶ unless (exists $args{index}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"index"}) 1 21 22
1237 1 die q{Unable to put an item to a directory listing: Missing index parameter (where did you want to put it?)};
1238 }
1239 21 ▶ unless (exists $args{item}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"item"}) 1 20 21
1240 1 die q{Unable to put an item to a directory listing: Missing item parameter (what did you want to put there?)};
1241 }
1242  
1243 20 my $index = $args{index};
1244 20 my $item = $args{item};
1245  
1246 20 $self->_validate_index($index, 'put');
1247 15 $self->_validate_item_object($item, 'put');
1248  
1249 13 my $num_items = $self->num_items();
1250 13 my $items = $self->{items};
1251  
1252 13 ▶ if ($index <= $num_items) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index <= $num_items) 12 1 13
1253 12 $items->[$index] = $item->clone();
1254 12 return 1;
1255 }
1256  
1257 1 return 0;
1258 }
1259  
1260 =head2 print
1261  
1262 Print out formatted disk directory listing:
1263  
1264 $dir->print(fh => $fh, as_petscii => $as_petscii);
1265  
1266 C<$fh> defaults to the standard output. C<as_petscii> defaults to false (meaning that ASCII characters will be printed out by default).
1267  
1268 A printout does not include header and number of blocks free lines, because information about disk title, disk ID and number of free sectors is stored in a Block Availability Map (see L<D64::Disk::BAM> for more details on how to access these bits of information).
1269  
1270 =cut
1271  
1272 5 ▶sub print {
Subroutine print: called CC=4 SCAR=13.9
Pod documented
1273 5 my ($self, %args) = @_;
1274  
1275 5 ▶ my $fh = $args{fh} || *STDOUT;
Condition33%1/3
$args{'fh'} || *STDOUT
l!l&&r!l&&!r
500
MC/DC0%0/2
$args{'fh'} || *STDOUT
$args{'fh'} *STDOUT
Truth table33%1/3
A || B
A= $args{'fh'}B= *STDOUT
ABresult
1X1
011
000
1276 5 ▶ my $as_petscii = $args{as_petscii} || 0;
Condition100%2/2
$args{'as_petscii'} || 0
l!l
23
MC/DC100%1/1
$args{'as_petscii'} || 0
$args{'as_petscii'}
Truth table100%2/2
A
A= $args{'as_petscii'}
Aresult
11
00
1277  
1278 5 $fh->binmode(':bytes');
1279 5 my $stdout = select $fh;
1280  
1281 5 my $items = $self->{items};
1282 5 my $num_items = $self->num_items();
1283  
1284 5 ▶ for (my $i = 0; $i < $num_items; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num_items) 30 5 35
1285 30 my $item = $items->[$i];
1286 30 $item->print(fh => $fh, as_petscii => $as_petscii);
1287 }
1288  
1289 5 select $stdout;
1290  
1291 5 return undef;
1292 }
1293  
1294 29 ▶sub is_numeric {
Subroutine is_numeric: called CC=1 SCAR=0.0
Pod undocumented
1295 29 my ($self, $var) = @_;
1296  
1297 29 my $is_numeric = _is_numeric($var);
1298  
1299 29 return $is_numeric;
1300 }
1301  
1302 416 ▶sub set_iok {
Subroutine set_iok: called CC=1 SCAR=0.0
Pod undocumented
1303 416 my ($self, $var) = @_;
1304  
1305 416 my $var_iok = _set_iok($var);
1306  
1307 416 return $var_iok;
1308 }
1309  
1310 202778 ▶sub _is_valid_byte_value {
Subroutine _is_valid_byte_value: called CC=4 SCAR=14.0
1311 202778 my ($self, $byte_value) = @_;
1312  
1313 202778 ▶ if (length ($byte_value) == 1 && ord ($byte_value) >= 0x00 && ord ($byte_value) <= 0xff) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $byte_value == 1 and ord $byte_value >= 0 and ord $byte_value <= 255) 202776 2 202778
Condition66%4/6
length $byte_value == 1 and ord $byte_value >= 0 and ord $byte_value <= 255
!ll&&!rl&&r
20202776
length $byte_value == 1 and ord $byte_value >= 0
!ll&&!rl&&r
20202776
MC/DC33%1/3
length $byte_value == 1 and ord $byte_value >= 0 and ord $byte_value <= 255
length $byte_value == 1 ord $byte_value >= 0 ord $byte_value <= 255
Truth table50%2/4
A and B and C
A= length $byte_value == 1B= ord $byte_value >= 0C= ord $byte_value <= 255
ABCresult
0XX0
10X0
1100
1111
1314 202776 return 1;
1315 }
1316  
1317 2 return 0;
1318 }
1319  
1320 61 ▶sub _is_valid_number_value {
Subroutine _is_valid_number_value: called CC=4 SCAR=13.9
1321 61 my ($self, $number_value) = @_;
1322  
1323 61 ▶ if (D64::Disk::Dir::Item->is_int($number_value) && $number_value >= 0x00 && $number_value <= 0xff) {
Branch: 1 branch
BranchTrueFalseTotal
if ("D64::Disk::Dir::Item"->is_int($number_value) and $number_value >= 0 and $number_value <= 255) 5 56 61
Condition100%6/6
"D64::Disk::Dir::Item"->is_int($number_value) and $number_value >= 0 and $number_value <= 255
!ll&&!rl&&r
5515
"D64::Disk::Dir::Item"->is_int($number_value) and $number_value >= 0
!ll&&!rl&&r
45106
MC/DC100%3/3
"D64::Disk::Dir::Item"->is_int($number_value) and $number_value >= 0 and $number_value <= 255
"D64::Disk::Dir::Item"->is_int($number_value) $number_value >= 0 $number_value <= 255
Truth table100%4/4
A and B and C
A= "D64::Disk::Dir::Item"->is_int($number_value)B= $number_value >= 0C= $number_value <= 255
ABCresult
0XX0
10X0
1100
1111
1324 5 return 1;
1325 }
1326  
1327 56 return 0;
1328 }
1329  
1330 29 ▶sub _dump {
Subroutine _dump: called CC=3 SCAR=11.2
1331 29 my ($self, $value) = @_;
1332  
1333 29 ▶ if ($self->_is_valid_number_value($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_valid_number_value($value)) 0 29 29
1334 0 return sprintf q{$%02x}, $value;
1335 }
1336  
1337 29 ▶ if ($self->is_numeric($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_numeric($value)) 11 18 29
1338 11 return sprintf q{%s}, $value;
1339 }
1340  
1341 18 my $dump = Data::Dumper->new([$value])->Indent(0)->Terse(1)->Deepcopy(1)->Sortkeys(1)->Dump();
1342  
1343 18 return $dump;
1344 }
1345  
1346 =head1 BUGS
1347  
1348 There are no known bugs at the moment. Please report any bugs or feature requests.
1349  
1350 =head1 EXPORT
1351  
1352 None. No method is exported into the caller's namespace neither by default nor explicitly.
1353  
1354 =head1 SEE ALSO
1355  
1356 L<D64::Disk::BAM>, L<D64::Disk::Dir::Item>, L<D64::Disk::Image>, L<D64::Disk::Layout>, L<D64::Disk::Layout::Sector>, L<D64::Disk::Status>.
1357  
1358 =head1 AUTHOR
1359  
1360 Pawel Krol, E<lt>pawelkrol@cpan.orgE<gt>.
1361  
1362 =head1 VERSION
1363  
1364 Version 0.06 (2021-01-18)
1365  
1366 =head1 COPYRIGHT AND LICENSE
1367  
1368 Copyright 2013-2021 by Pawel Krol E<lt>pawelkrol@cpan.orgE<gt>.
1369  
1370 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.
1371  
1372 PLEASE NOTE THAT IT COMES WITHOUT A WARRANTY OF ANY KIND!
1373  
1374 =cut
1375  
1376 15 1;
1377  
1378 __END__
↑ Summary Dir.xs »