File/RandomAccess.pm

filter: Statementstmt 49.1% 114 / 232 Branchbran 40.3% 50 / 124 Conditioncond 26.1% 11 / 42 MC/DCmcdc 4.1% 1 / 24 Subroutinesub 84.6% 11 / 13 Podpod 81.8% 9 / 11 totaltotal 43.9% 196 / 446 CC 77 SCAR 41.7
CC
77
Cov
44%
Read
122.3
Purge
90.0
ReadLine
75.8
CRAP 64.7
« Image/ExifTool/DV.pm ↑ Summary Image/ExifTool/Audible.pm »
1 #------------------------------------------------------------------------------
2 # File: RandomAccess.pm
3 #
4 # Description: Buffer to support random access reading of sequential file
5 #
6 # Revisions: 02/11/2004 - P. Harvey Created
7 # 02/20/2004 - P. Harvey Added flag to disable SeekTest in new()
8 # 11/18/2004 - P. Harvey Fixed bug with seek relative to end of file
9 # 01/02/2005 - P. Harvey Added DEBUG code
10 # 01/09/2006 - P. Harvey Fixed bug in ReadLine() when using
11 # multi-character EOL sequences
12 # 02/20/2006 - P. Harvey Fixed bug where seek past end of file could
13 # generate "substr outside string" warning
14 # 06/10/2006 - P. Harvey Decreased $CHUNK_SIZE from 64k to 8k
15 # 11/23/2006 - P. Harvey Limit reads to < 0x80000000 bytes
16 # 11/26/2008 - P. Harvey Fixed bug in ReadLine when reading from a
17 # scalar with a multi-character newline
18 # 01/24/2009 - PH Protect against reading too much at once
19 # 10/04/2018 - PH Added NoBuffer option
20 # 01/20/2024 - PH Set ERROR on file read error
21 #
22 # Notes: Calls the normal file i/o routines unless SeekTest() fails, in
23 # which case the file is buffered in memory to allow random access.
24 # SeekTest() is called automatically when the object is created
25 # unless specified.
26 #
27 # May also be used for string i/o (just pass a scalar reference)
28 #
29 # Sets internal ERROR member from $! if there is an error reading
30 # the file.
31 #
32 # Legal: Copyright (c) 2003-2026, Phil Harvey (philharvey66 at gmail.com)
33 # This library is free software; you can redistribute it and/or
34 # modify it under the same terms as Perl itself.
35 #------------------------------------------------------------------------------
36  
37 package File::RandomAccess;
38  
39 113 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 113 require 5.002;
41 113 require Exporter;
42  
43 113 ▶use vars qw($VERSION @ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 113 $VERSION = '1.13';
45 113 @ISA = qw(Exporter);
46  
47 sub Read($$$);
48  
49 # constants
50 113 my $CHUNK_SIZE = 8192; # size of chunks to read from file (must be power of 2)
51 113 my $SKIP_SIZE = 65536; # size to skip when fast-forwarding over sequential data
52 113 my $SLURP_CHUNKS = 16; # read this many chunks at a time when slurping
53  
54 #------------------------------------------------------------------------------
55 # Create new RandomAccess object
56 # Inputs: 0) reference to RandomAccess object or RandomAccess class name
57 # 1) file reference or scalar reference
58 # 2) flag set if file is already random access (disables automatic SeekTest)
59 sub new($$;$)
60 1751 ▶{
Subroutine new: called CC=4 SCAR=13.9
Pod documented
61 1751 my ($that, $filePt, $isRandom) = @_;
62 1751 ▶ my $class = ref($that) || $that;
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
017510
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
63 1751 my $self;
64  
65 1751 ▶ if (ref $filePt eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filePt eq 'SCALAR') { } 1020 731 1751
66 # string i/o
67 1020 $self = {
68 BUFF_PT => $filePt,
69 BASE => 0,
70 POS => 0,
71 LEN => length($$filePt),
72 TESTED => -1,
73 };
74 1020 bless $self, $class;
75 } else {
76 # file i/o
77 731 my $buff = '';
78 731 $self = {
79 FILE_PT => $filePt, # file pointer
80 BUFF_PT => \$buff, # reference to file data
81 BASE => 0, # location of start of buffer in file
82 POS => 0, # current position in buffer
83 LEN => 0, # length of data in buffer
84 TESTED => 0, # 0=untested, 1=passed, -1=failed (requires buffering)
85 };
86 731 bless $self, $class;
87 731 ▶ $self->SeekTest() unless $isRandom;
Branch: 1 branch
BranchTrueFalseTotal
unless $isRandom 515 216 731
88 }
89 1751 return $self;
90 }
91  
92 #------------------------------------------------------------------------------
93 # Enable DEBUG code
94 # Inputs: 0) reference to RandomAccess object
95 sub Debug($)
96 0 ▶{
Subroutine Debug: not called CC=1 SCAR=6.9
Pod undocumented
97 0 my $self = shift;
98 0 $self->{DEBUG} = { };
99 }
100  
101 #------------------------------------------------------------------------------
102 # Perform seek test and turn on buffering if necessary
103 # Inputs: 0) reference to RandomAccess object
104 # Returns: 1 if seek test passed (ie. no buffering required)
105 # Notes: Must be done before any other i/o
106 sub SeekTest($)
107 758 ▶{
Subroutine SeekTest: called CC=5 SCAR=16.8
Pod documented
108 758 my $self = shift;
109 758 ▶ unless ($self->{TESTED}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"TESTED"}) 730 28 758
110 730 my $fp = $self->{FILE_PT};
111 730 ▶ if (seek($fp, 1, 1) and seek($fp, -1, 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (seek $fp, 1, 1 and seek $fp, -1, 1) { } 730 0 730
Condition33%1/3
seek $fp, 1, 1 and seek $fp, -1, 1
!ll&&!rl&&r
00730
MC/DC0%0/2
seek $fp, 1, 1 and seek $fp, -1, 1
seek $fp, 1, 1 seek $fp, -1, 1
Truth table33%1/3
A and B
A= seek $fp, 1, 1B= seek $fp, -1, 1
ABresult
0X0
100
111
112 730 $self->{TESTED} = 1; # test passed
113 } else {
114 0 $self->{TESTED} = -1; # test failed (requires buffering)
115 }
116 }
117 758 ▶ return $self->{TESTED} == 1 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'TESTED'} == 1 ? : 730 28 758
118 }
119  
120 #------------------------------------------------------------------------------
121 # Get current position in file
122 # Inputs: 0) reference to RandomAccess object
123 # Returns: current position in file
124 sub Tell($)
125 9379 ▶{
Subroutine Tell: called CC=2 SCAR=6.9
Pod documented
126 9379 my $self = shift;
127 9379 my $rtnVal;
128 9379 ▶ if ($self->{TESTED} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'TESTED'} < 0) { } 3791 5588 9379
129 3791 $rtnVal = $self->{POS} + $self->{BASE};
130 } else {
131 5588 $rtnVal = tell($self->{FILE_PT});
132 }
133 9379 return $rtnVal;
134 }
135  
136 #------------------------------------------------------------------------------
137 # Seek to position in file
138 # Inputs: 0) reference to RandomAccess object
139 # 1) position, 2) whence (0 or undef=from start, 1=from cur pos, 2=from end)
140 # Returns: 1 on success
141 # Notes: When buffered, this doesn't quite behave like seek() since it will return
142 # success even if you seek outside the limits of the file. However if you
143 # do this, you will get an error on your next Read().
144 sub Seek($$;$)
145 8636 ▶{
Subroutine Seek: called CC=10 SCAR=23.7
Pod documented
146 8636 my ($self, $num, $whence) = @_;
147 8636 ▶ $whence = 0 unless defined $whence;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $whence 420 8216 8636
148 8636 my $rtnVal;
149 8636 ▶ if ($self->{TESTED} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'TESTED'} < 0) { } 1380 7256 8636
150 1380 my $newPos;
151 1380 ▶ if ($whence == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($whence == 0) { } 1052 328 1380
elsif ($whence == 1) { } 233 95 328
elsif ($self->{'NoBuffer'} and $self->{'FILE_PT'}) { } 0 95 95
152 1052 $newPos = $num - $self->{BASE}; # from start of file
153 } elsif ($whence == 1) {
154 233 $newPos = $num + $self->{POS}; # relative to current position
155 ▶ } elsif ($self->{NoBuffer} and $self->{FILE_PT}) {
Condition33%1/3
$self->{"NoBuffer"} and $self->{"FILE_PT"}
!ll&&!rl&&r
9500
MC/DC0%0/2
$self->{"NoBuffer"} and $self->{"FILE_PT"}
$self->{"NoBuffer"} $self->{"FILE_PT"}
Truth table33%1/3
A and B
A= $self->{"NoBuffer"}B= $self->{"FILE_PT"}
ABresult
0X0
100
111
156 0 $newPos = -1; # (can't seek relative to end if no buffering)
157 } else {
158 95 $self->Slurp(); # read whole file into buffer
159 95 $newPos = $num + $self->{LEN}; # relative to end of file
160 }
161 1380 ▶ if ($newPos >= 0 and
Branch: 1 branch
BranchTrueFalseTotal
if ($newPos >= 0 and !$self->{"NoBuffer"} || $newPos >= $self->{"POS"}) 1367 13 1380
Condition50%3/6
$newPos >= 0 and !$self->{"NoBuffer"} || $newPos >= $self->{"POS"}
!ll&&!rl&&r
1301367
not $self->{"NoBuffer"} or $newPos >= $self->{"POS"}
l!l&&r!l&&!r
136700
MC/DC33%1/3
$newPos >= 0 and !$self->{"NoBuffer"} || $newPos >= $self->{"POS"}
$newPos >= 0 not $self->{"NoBuffer"} $newPos >= $self->{"POS"}
Truth table50%2/4
A and not(B) or C
A= $newPos >= 0B= not $self->{"NoBuffer"}C= $newPos >= $self->{"POS"}
ABCresult
0XX0
1000
11X1
1011
162 # can't go backwards in unbuffered non-seekable file
163 (not $self->{NoBuffer} or $newPos >= $self->{POS}))
164 {
165 1367 $self->{POS} = $newPos;
166 1367 $rtnVal = 1;
167 }
168 } else {
169 7256 $rtnVal = seek($self->{FILE_PT}, $num, $whence);
170 }
171 8636 return $rtnVal;
172 }
173  
174 #------------------------------------------------------------------------------
175 # Read from the file
176 # Inputs: 0) reference to RandomAccess object, 1) buffer, 2) bytes to read
177 # Returns: Number of bytes read
178 sub Read($$$)
179 28926 ▶{
Subroutine Read: called CC=22 SCAR=48.1
Pod documented
180 28926 my $self = shift;
181 28926 my $len = $_[1];
182 28926 my $rtnVal;
183  
184 # protect against reading too much at once
185 # (also from dying with a "Negative length" error)
186 28926 ▶ if ($len & 0xf8000000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len & 4160749568) 0 28926 28926
187 0 ▶ return 0 if $len < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len < 0 0 0 0
188 # read in smaller blocks because Windows attempts to pre-allocate
189 # memory for the full size, which can lead to an out-of-memory error
190 0 my $maxLen = 0x4000000; # (MUST be less than bitmask in "if" above)
191 0 my $num = Read($self, $_[0], $maxLen);
192 0 ▶ return $num if $num < $maxLen;
Branch: 1 branch
BranchTrueFalseTotal
if $num < $maxLen 0 0 0
193 0 for (;;) {
194 0 $len -= $maxLen;
195 0 ▶ last if $len <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len <= 0 0 0 0
196 0 ▶ my $l = $len < $maxLen ? $len : $maxLen;
Branch: 1 branch
BranchTrueFalseTotal
$len < $maxLen ? : 0 0 0
197 0 my $buff;
198 0 my $n = Read($self, $buff, $l);
199 0 ▶ last unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 0 0
200 0 $_[0] .= $buff;
201 0 $num += $n;
202 0 ▶ last if $n < $l;
Branch: 1 branch
BranchTrueFalseTotal
if $n < $l 0 0 0
203 }
204 0 return $num;
205 }
206 # read through our buffer if necessary
207 28926 ▶ if ($self->{TESTED} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'TESTED'} < 0) { } 9187 19739 28926
208 # purge old data before reading in NoBuffer mode
209 9187 ▶ $self->Purge() or return 0 if $self->{NoBuffer};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"NoBuffer"} 0 9187 9187
Condition20%1/5
$self->{'NoBuffer'} && ($self->Purge || (return 0))
!ll&&!rl&&r
918700
$self->Purge or return 0
l!l
00
MC/DC0%0/2
$self->{'NoBuffer'} && ($self->Purge || (return 0))
$self->{'NoBuffer'} $self->Purge
Truth table33%1/3
A && B
A= $self->{'NoBuffer'}B= $self->Purge
ABresult
0X0
100
111
210 9187 my $buff;
211 9187 my $newPos = $self->{POS} + $len;
212 # number of bytes to read from file
213 9187 my $num = $newPos - $self->{LEN};
214 9187 ▶ if ($num > 0 and $self->{FILE_PT}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num > 0 and $self->{"FILE_PT"}) 0 9187 9187
Condition66%2/3
$num > 0 and $self->{"FILE_PT"}
!ll&&!rl&&r
85846030
MC/DC0%0/2
$num > 0 and $self->{"FILE_PT"}
$num > 0 $self->{"FILE_PT"}
Truth table66%2/3
A and B
A= $num > 0B= $self->{"FILE_PT"}
ABresult
0X0
100
111
215 # read data from file in multiples of $CHUNK_SIZE
216 0 $num = (($num - 1) | ($CHUNK_SIZE - 1)) + 1;
217 0 $num = read($self->{FILE_PT}, $buff, $num);
218 0 ▶ if ($num) {
Branch: 2 branches
BranchTrueFalseTotal
if ($num) { } 0 0 0
elsif (not defined $num) { } 0 0 0
219 0 ${$self->{BUFF_PT}} .= $buff;
220 0 $self->{LEN} += $num;
221 } elsif (not defined $num) {
222 0 $self->{ERROR} = $!;
223 }
224 }
225 # number of bytes left in data buffer
226 9187 $num = $self->{LEN} - $self->{POS};
227 9187 ▶ if ($len <= $num) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len <= $num) { } 8584 603 9187
elsif ($num <= 0) { } 489 114 603
228 8584 $rtnVal = $len;
229 } elsif ($num <= 0) {
230 489 $_[0] = '';
231 489 return 0;
232 } else {
233 114 $rtnVal = $num;
234 }
235 # return data from our buffer
236 8698 $_[0] = substr(${$self->{BUFF_PT}}, $self->{POS}, $rtnVal);
237 8698 $self->{POS} += $rtnVal;
238 } else {
239 # read directly from file
240 19739 ▶ $_[0] = '' unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 7398 12341 19739
241 19739 $rtnVal = read($self->{FILE_PT}, $_[0], $len);
242 19739 ▶ unless (defined $rtnVal) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rtnVal) 0 19739 19739
243 0 $self->{ERROR} = $!;
244 0 $rtnVal = 0;
245 }
246 }
247 28437 ▶ if ($self->{DEBUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DEBUG"}) 0 28437 28437
248 0 my $pos = $self->Tell() - $rtnVal;
249 0 ▶ unless ($self->{DEBUG}->{$pos} and $self->{DEBUG}->{$pos} > $rtnVal) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"DEBUG"}{$pos} and $self->{"DEBUG"}{$pos} > $rtnVal) 0 0 0
Condition0%0/3
$self->{"DEBUG"}{$pos} and $self->{"DEBUG"}{$pos} > $rtnVal
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"DEBUG"}{$pos} and $self->{"DEBUG"}{$pos} > $rtnVal
$self->{"DEBUG"}{$pos} $self->{"DEBUG"}{$pos} > $rtnVal
Truth table0%0/3
A and B
A= $self->{"DEBUG"}{$pos}B= $self->{"DEBUG"}{$pos} > $rtnVal
ABresult
0X0
100
111
250 0 $self->{DEBUG}->{$pos} = $rtnVal;
251 }
252 }
253 28437 return $rtnVal;
254 }
255  
256 #------------------------------------------------------------------------------
257 # Read a line from file (end of line is $/)
258 # Inputs: 0) reference to RandomAccess object, 1) buffer
259 # Returns: Number of bytes read
260 sub ReadLine($$)
261 6841 ▶{
Subroutine ReadLine: called CC=17 SCAR=43.3
Pod documented
262 6841 my $self = shift;
263 6841 my $rtnVal;
264 6841 my $fp = $self->{FILE_PT};
265  
266 6841 ▶ if ($self->{TESTED} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'TESTED'} < 0) { } 123 6718 6841
267 123 my ($num, $buff);
268 123 ▶ $self->Purge() or return 0 if $self->{NoBuffer};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"NoBuffer"} 0 123 123
Condition20%1/5
$self->{'NoBuffer'} && ($self->Purge || (return 0))
!ll&&!rl&&r
12300
$self->Purge or return 0
l!l
00
MC/DC0%0/2
$self->{'NoBuffer'} && ($self->Purge || (return 0))
$self->{'NoBuffer'} $self->Purge
Truth table33%1/3
A && B
A= $self->{'NoBuffer'}B= $self->Purge
ABresult
0X0
100
111
269 123 my $pos = $self->{POS};
270 123 ▶ if ($fp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fp) { } 0 123 123
271 # make sure we have some data after the current position
272 0 ▶ while ($self->{LEN} <= $pos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"LEN"} <= $pos) 0 0 0
273 0 $num = read($fp, $buff, $CHUNK_SIZE);
274 0 ▶ unless ($num) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($num) 0 0 0
275 0 ▶ defined $num or $self->{ERROR} = $!;
Branch: 1 branch
BranchTrueFalseTotal
defined $num or $self->{"ERROR"} = $! 0 0 0
276 0 return 0;
277 }
278 0 ${$self->{BUFF_PT}} .= $buff;
279 0 $self->{LEN} += $num;
280 }
281 # scan and read until we find the EOL (or hit EOF)
282 0 for (;;) {
283 0 $pos = index(${$self->{BUFF_PT}}, $/, $pos);
284 0 ▶ if ($pos >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos >= 0) 0 0 0
285 0 $pos += length($/);
286 0 last;
287 }
288 0 $pos = $self->{LEN}; # have scanned to end of buffer
289 0 $num = read($fp, $buff, $CHUNK_SIZE);
290 0 ▶ unless ($num) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($num) 0 0 0
291 0 ▶ defined $num or $self->{ERROR} = $!;
Branch: 1 branch
BranchTrueFalseTotal
defined $num or $self->{"ERROR"} = $! 0 0 0
292 0 last;
293 }
294 0 ${$self->{BUFF_PT}} .= $buff;
295 0 $self->{LEN} += $num;
296 }
297 } else {
298 # string i/o
299 123 $pos = index(${$self->{BUFF_PT}}, $/, $pos);
300 123 ▶ if ($pos < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < 0) { } 18 105 123
301 18 $pos = $self->{LEN};
302 18 ▶ $self->{POS} = $pos if $self->{POS} > $pos;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"POS"} > $pos 0 18 18
303 } else {
304 105 $pos += length($/);
305 }
306 }
307 # read the line from our buffer
308 123 $rtnVal = $pos - $self->{POS};
309 123 $_[0] = substr(${$self->{BUFF_PT}}, $self->{POS}, $rtnVal);
310 123 $self->{POS} = $pos;
311 } else {
312 6718 $_[0] = <$fp>;
313 6718 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 6696 22 6718
314 6696 $rtnVal = length($_[0]);
315 } else {
316 22 $rtnVal = 0;
317 }
318 }
319 6841 ▶ if ($self->{DEBUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DEBUG"}) 0 6841 6841
320 0 my $pos = $self->Tell() - $rtnVal;
321 0 ▶ unless ($self->{DEBUG}->{$pos} and $self->{DEBUG}->{$pos} > $rtnVal) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"DEBUG"}{$pos} and $self->{"DEBUG"}{$pos} > $rtnVal) 0 0 0
Condition0%0/3
$self->{"DEBUG"}{$pos} and $self->{"DEBUG"}{$pos} > $rtnVal
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"DEBUG"}{$pos} and $self->{"DEBUG"}{$pos} > $rtnVal
$self->{"DEBUG"}{$pos} $self->{"DEBUG"}{$pos} > $rtnVal
Truth table0%0/3
A and B
A= $self->{"DEBUG"}{$pos}B= $self->{"DEBUG"}{$pos} > $rtnVal
ABresult
0X0
100
111
322 0 $self->{DEBUG}->{$pos} = $rtnVal;
323 }
324 }
325 6841 return $rtnVal;
326 }
327  
328 #------------------------------------------------------------------------------
329 # Read whole file into buffer (without changing read pointer)
330 # Inputs: 0) reference to RandomAccess object
331 sub Slurp($)
332 95 ▶{
Subroutine Slurp: called CC=4 SCAR=25.6
Pod documented
333 95 my $self = shift;
334 95 ▶ my $fp = $self->{FILE_PT} || return;
Condition50%1/2
$self->{'FILE_PT'} || (return)
l!l
095
MC/DC0%0/1
$self->{'FILE_PT'} || (return)
$self->{'FILE_PT'}
Truth table50%1/2
A
A= $self->{'FILE_PT'}
Aresult
11
00
335 # read whole file into buffer (in large chunks)
336 0 my ($buff, $num);
337 0 for (;;) {
338 0 $num = read($fp, $buff, $CHUNK_SIZE * $SLURP_CHUNKS);
339 0 ▶ unless ($num) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($num) 0 0 0
340 0 ▶ defined $num or $self->{ERROR} = $!;
Branch: 1 branch
BranchTrueFalseTotal
defined $num or $self->{"ERROR"} = $! 0 0 0
341 0 last;
342 }
343 0 ${$self->{BUFF_PT}} .= $buff;
344 0 $self->{LEN} += $num;
345 }
346 }
347  
348 #------------------------------------------------------------------------------
349 # Purge internal buffer [internal use only]
350 # Inputs: 0) reference to RandomAccess object
351 # Returns: 1 on success, or 0 if current buffer position is negative
352 # Notes: This is called only in NoBuffer mode
353 sub Purge($)
354 0 ▶{
Subroutine Purge: not called CC=9 SCAR=45.0
Pod undocumented
355 0 my $self = shift;
356 0 ▶ return 1 unless $self->{FILE_PT};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"FILE_PT"} 0 0 0
357 0 ▶ return 0 if $self->{POS} < 0; # error if we can't read from here
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"POS"} < 0 0 0 0
358 0 ▶ if ($self->{POS} > $CHUNK_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"POS"} > $CHUNK_SIZE) 0 0 0
359 0 my $purge = $self->{POS} - ($self->{POS} % $CHUNK_SIZE);
360 0 ▶ if ($purge >= $self->{LEN}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($purge >= $self->{'LEN'}) { } 0 0 0
elsif ($purge > 0) { } 0 0 0
361 # read up to current position in 64k chunks, discarding as we go
362 0 ▶ while ($self->{POS} > $self->{LEN}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"POS"} > $self->{"LEN"}) 0 0 0
363 0 $self->{BASE} += $self->{LEN};
364 0 $self->{POS} -= $self->{LEN};
365 0 ${$self->{BUFF_PT}} = '';
366 0 $self->{LEN} = read($self->{FILE_PT}, ${$self->{BUFF_PT}}, $SKIP_SIZE);
367 0 ▶ if (not defined $self->{LEN}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"LEN"}) 0 0 0
368 0 $self->{ERROR} = $!;
369 0 last;
370 }
371 0 ▶ last if $self->{LEN} < $SKIP_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"LEN"} < $SKIP_SIZE 0 0 0
372 }
373 } elsif ($purge > 0) {
374 0 ${$self->{BUFF_PT}} = substr ${$self->{BUFF_PT}}, $purge;
375 0 $self->{BASE} += $purge;
376 0 $self->{POS} -= $purge;
377 0 $self->{LEN} -= $purge;
378 }
379 }
380 0 return 1;
381 }
382  
383 #------------------------------------------------------------------------------
384 # Set binary mode
385 # Inputs: 0) reference to RandomAccess object
386 sub BinMode($)
387 796 ▶{
Subroutine BinMode: called CC=2 SCAR=6.9
Pod documented
388 796 my $self = shift;
389 796 ▶ binmode($self->{FILE_PT}) if $self->{FILE_PT};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"FILE_PT"} 741 55 796
390 }
391  
392 #------------------------------------------------------------------------------
393 # Close the file and free the buffer
394 # Inputs: 0) reference to RandomAccess object
395 sub Close($)
396 516 ▶{
Subroutine Close: called CC=11 SCAR=42.2
Pod documented
397 516 my $self = shift;
398  
399 516 ▶ if ($self->{DEBUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DEBUG"}) 0 516 516
400 0 local $_;
401 0 ▶ if ($self->Seek(0,2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Seek(0, 2)) { } 0 0 0
402 0 $self->{DEBUG}->{$self->Tell()} = 0; # set EOF marker
403 0 my $last;
404 0 my $tot = 0;
405 0 my $bad = 0;
406 0 foreach (sort { $a <=> $b } keys %{$self->{DEBUG}}) {
407 0 my $pos = $_;
408 0 my $len = $self->{DEBUG}->{$_};
409 0 ▶ if (defined $last and $last < $pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last and $last < $pos) 0 0 0
Condition0%0/3
defined $last and $last < $pos
!ll&&!rl&&r
000
MC/DC0%0/2
defined $last and $last < $pos
defined $last $last < $pos
Truth table0%0/3
A and B
A= defined $lastB= $last < $pos
ABresult
0X0
100
111
410 0 my $bytes = $pos - $last;
411 0 $tot += $bytes;
412 0 $self->Seek($last);
413 0 my $buff;
414 0 $self->Read($buff, $bytes);
415 0 my $warn = '';
416 0 ▶ if ($buff =~ /[^\0]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buff =~ /[^\0]/) 0 0 0
417 0 $bad += ($pos - $last);
418 0 $warn = ' - NON-ZERO!';
419 }
420 0 printf "0x%.8x - 0x%.8x (%d bytes)$warn\n", $last, $pos, $bytes;
421 }
422 0 my $cur = $pos + $len;
423 0 ▶ $last = $cur unless defined $last and $last > $cur;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $last and $last > $cur 0 0 0
Condition0%0/3
defined $last and $last > $cur
!ll&&!rl&&r
000
MC/DC0%0/2
defined $last and $last > $cur
defined $last $last > $cur
Truth table0%0/3
A and B
A= defined $lastB= $last > $cur
ABresult
0X0
100
111
424 }
425 0 print "$tot bytes missed";
426 0 ▶ $bad and print ", $bad non-zero!";
Branch: 1 branch
BranchTrueFalseTotal
$bad and print ", $bad non-zero!" 0 0 0
427 0 print "\n";
428 } else {
429 0 warn "File::RandomAccess DEBUG not working (file already closed?)\n";
430 }
431 0 delete $self->{DEBUG};
432 }
433 # close the file
434 516 ▶ if ($self->{FILE_PT}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"FILE_PT"}) 514 2 516
435 514 close($self->{FILE_PT});
436 514 delete $self->{FILE_PT};
437 }
438 # reset the buffer
439 516 my $emptyBuff = '';
440 516 $self->{BUFF_PT} = \$emptyBuff;
441 516 $self->{BASE} = 0;
442 516 $self->{LEN} = 0;
443 516 $self->{POS} = 0;
444 }
445  
446 #------------------------------------------------------------------------------
447 113 1; # end
« Image/ExifTool/DV.pm ↑ Summary Image/ExifTool/Audible.pm »