line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
|
|
|
|
# |
21
|
|
|
|
|
|
|
# Notes: Calls the normal file i/o routines unless SeekTest() fails, in |
22
|
|
|
|
|
|
|
# which case the file is buffered in memory to allow random access. |
23
|
|
|
|
|
|
|
# SeekTest() is called automatically when the object is created |
24
|
|
|
|
|
|
|
# unless specified. |
25
|
|
|
|
|
|
|
# |
26
|
|
|
|
|
|
|
# May also be used for string i/o (just pass a scalar reference) |
27
|
|
|
|
|
|
|
# |
28
|
|
|
|
|
|
|
# Legal: Copyright (c) 2003-2022 Phil Harvey (philharvey66 at gmail.com) |
29
|
|
|
|
|
|
|
# This library is free software; you can redistribute it and/or |
30
|
|
|
|
|
|
|
# modify it under the same terms as Perl itself. |
31
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
package File::RandomAccess; |
34
|
|
|
|
|
|
|
|
35
|
105
|
|
|
105
|
|
676
|
use strict; |
|
105
|
|
|
|
|
209
|
|
|
105
|
|
|
|
|
5268
|
|
36
|
|
|
|
|
|
|
require 5.002; |
37
|
|
|
|
|
|
|
require Exporter; |
38
|
|
|
|
|
|
|
|
39
|
105
|
|
|
105
|
|
687
|
use vars qw($VERSION @ISA @EXPORT_OK); |
|
105
|
|
|
|
|
210
|
|
|
105
|
|
|
|
|
222115
|
|
40
|
|
|
|
|
|
|
$VERSION = '1.11'; |
41
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub Read($$$); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# constants |
46
|
|
|
|
|
|
|
my $CHUNK_SIZE = 8192; # size of chunks to read from file (must be power of 2) |
47
|
|
|
|
|
|
|
my $SKIP_SIZE = 65536; # size to skip when fast-forwarding over sequential data |
48
|
|
|
|
|
|
|
my $SLURP_CHUNKS = 16; # read this many chunks at a time when slurping |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
51
|
|
|
|
|
|
|
# Create new RandomAccess object |
52
|
|
|
|
|
|
|
# Inputs: 0) reference to RandomAccess object or RandomAccess class name |
53
|
|
|
|
|
|
|
# 1) file reference or scalar reference |
54
|
|
|
|
|
|
|
# 2) flag set if file is already random access (disables automatic SeekTest) |
55
|
|
|
|
|
|
|
sub new($$;$) |
56
|
|
|
|
|
|
|
{ |
57
|
1497
|
|
|
1497
|
1
|
4670
|
my ($that, $filePt, $isRandom) = @_; |
58
|
1497
|
|
33
|
|
|
6500
|
my $class = ref($that) || $that; |
59
|
1497
|
|
|
|
|
2521
|
my $self; |
60
|
|
|
|
|
|
|
|
61
|
1497
|
100
|
|
|
|
4615
|
if (ref $filePt eq 'SCALAR') { |
62
|
|
|
|
|
|
|
# string i/o |
63
|
808
|
|
|
|
|
3917
|
$self = { |
64
|
|
|
|
|
|
|
BUFF_PT => $filePt, |
65
|
|
|
|
|
|
|
BASE => 0, |
66
|
|
|
|
|
|
|
POS => 0, |
67
|
|
|
|
|
|
|
LEN => length($$filePt), |
68
|
|
|
|
|
|
|
TESTED => -1, |
69
|
|
|
|
|
|
|
}; |
70
|
808
|
|
|
|
|
1779
|
bless $self, $class; |
71
|
|
|
|
|
|
|
} else { |
72
|
|
|
|
|
|
|
# file i/o |
73
|
689
|
|
|
|
|
1805
|
my $buff = ''; |
74
|
689
|
|
|
|
|
5189
|
$self = { |
75
|
|
|
|
|
|
|
FILE_PT => $filePt, # file pointer |
76
|
|
|
|
|
|
|
BUFF_PT => \$buff, # reference to file data |
77
|
|
|
|
|
|
|
BASE => 0, # location of start of buffer in file |
78
|
|
|
|
|
|
|
POS => 0, # current position in buffer |
79
|
|
|
|
|
|
|
LEN => 0, # length of data in buffer |
80
|
|
|
|
|
|
|
TESTED => 0, # 0=untested, 1=passed, -1=failed (requires buffering) |
81
|
|
|
|
|
|
|
}; |
82
|
689
|
|
|
|
|
1895
|
bless $self, $class; |
83
|
689
|
100
|
|
|
|
3163
|
$self->SeekTest() unless $isRandom; |
84
|
|
|
|
|
|
|
} |
85
|
1497
|
|
|
|
|
4937
|
return $self; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
89
|
|
|
|
|
|
|
# Enable DEBUG code |
90
|
|
|
|
|
|
|
# Inputs: 0) reference to RandomAccess object |
91
|
|
|
|
|
|
|
sub Debug($) |
92
|
|
|
|
|
|
|
{ |
93
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
94
|
0
|
|
|
|
|
0
|
$self->{DEBUG} = { }; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
98
|
|
|
|
|
|
|
# Perform seek test and turn on buffering if necessary |
99
|
|
|
|
|
|
|
# Inputs: 0) reference to RandomAccess object |
100
|
|
|
|
|
|
|
# Returns: 1 if seek test passed (ie. no buffering required) |
101
|
|
|
|
|
|
|
# Notes: Must be done before any other i/o |
102
|
|
|
|
|
|
|
sub SeekTest($) |
103
|
|
|
|
|
|
|
{ |
104
|
715
|
|
|
715
|
1
|
1740
|
my $self = shift; |
105
|
715
|
100
|
|
|
|
3355
|
unless ($self->{TESTED}) { |
106
|
688
|
|
|
|
|
1643
|
my $fp = $self->{FILE_PT}; |
107
|
688
|
50
|
33
|
|
|
13973
|
if (seek($fp, 1, 1) and seek($fp, -1, 1)) { |
108
|
688
|
|
|
|
|
2654
|
$self->{TESTED} = 1; # test passed |
109
|
|
|
|
|
|
|
} else { |
110
|
0
|
|
|
|
|
0
|
$self->{TESTED} = -1; # test failed (requires buffering) |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
} |
113
|
715
|
100
|
|
|
|
3126
|
return $self->{TESTED} == 1 ? 1 : 0; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
117
|
|
|
|
|
|
|
# Get current position in file |
118
|
|
|
|
|
|
|
# Inputs: 0) reference to RandomAccess object |
119
|
|
|
|
|
|
|
# Returns: current position in file |
120
|
|
|
|
|
|
|
sub Tell($) |
121
|
|
|
|
|
|
|
{ |
122
|
5781
|
|
|
5781
|
1
|
10337
|
my $self = shift; |
123
|
5781
|
|
|
|
|
9077
|
my $rtnVal; |
124
|
5781
|
100
|
|
|
|
12752
|
if ($self->{TESTED} < 0) { |
125
|
765
|
|
|
|
|
1404
|
$rtnVal = $self->{POS} + $self->{BASE}; |
126
|
|
|
|
|
|
|
} else { |
127
|
5016
|
|
|
|
|
10867
|
$rtnVal = tell($self->{FILE_PT}); |
128
|
|
|
|
|
|
|
} |
129
|
5781
|
|
|
|
|
14977
|
return $rtnVal; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
133
|
|
|
|
|
|
|
# Seek to position in file |
134
|
|
|
|
|
|
|
# Inputs: 0) reference to RandomAccess object |
135
|
|
|
|
|
|
|
# 1) position, 2) whence (0 or undef=from start, 1=from cur pos, 2=from end) |
136
|
|
|
|
|
|
|
# Returns: 1 on success |
137
|
|
|
|
|
|
|
# Notes: When buffered, this doesn't quite behave like seek() since it will return |
138
|
|
|
|
|
|
|
# success even if you seek outside the limits of the file. However if you |
139
|
|
|
|
|
|
|
# do this, you will get an error on your next Read(). |
140
|
|
|
|
|
|
|
sub Seek($$;$) |
141
|
|
|
|
|
|
|
{ |
142
|
7993
|
|
|
7993
|
1
|
17491
|
my ($self, $num, $whence) = @_; |
143
|
7993
|
100
|
|
|
|
17637
|
$whence = 0 unless defined $whence; |
144
|
7993
|
|
|
|
|
11198
|
my $rtnVal; |
145
|
7993
|
100
|
|
|
|
16940
|
if ($self->{TESTED} < 0) { |
146
|
1261
|
|
|
|
|
2010
|
my $newPos; |
147
|
1261
|
100
|
33
|
|
|
3309
|
if ($whence == 0) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
148
|
894
|
|
|
|
|
1578
|
$newPos = $num - $self->{BASE}; # from start of file |
149
|
|
|
|
|
|
|
} elsif ($whence == 1) { |
150
|
275
|
|
|
|
|
536
|
$newPos = $num + $self->{POS}; # relative to current position |
151
|
|
|
|
|
|
|
} elsif ($self->{NoBuffer} and $self->{FILE_PT}) { |
152
|
0
|
|
|
|
|
0
|
$newPos = -1; # (can't seek relative to end if no buffering) |
153
|
|
|
|
|
|
|
} else { |
154
|
92
|
|
|
|
|
467
|
$self->Slurp(); # read whole file into buffer |
155
|
92
|
|
|
|
|
249
|
$newPos = $num + $self->{LEN}; # relative to end of file |
156
|
|
|
|
|
|
|
} |
157
|
1261
|
100
|
|
|
|
2656
|
if ($newPos >= 0) { |
158
|
1248
|
|
|
|
|
1983
|
$self->{POS} = $newPos; |
159
|
1248
|
|
|
|
|
2034
|
$rtnVal = 1; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
} else { |
162
|
6732
|
|
|
|
|
81741
|
$rtnVal = seek($self->{FILE_PT}, $num, $whence); |
163
|
|
|
|
|
|
|
} |
164
|
7993
|
|
|
|
|
49504
|
return $rtnVal; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
168
|
|
|
|
|
|
|
# Read from the file |
169
|
|
|
|
|
|
|
# Inputs: 0) reference to RandomAccess object, 1) buffer, 2) bytes to read |
170
|
|
|
|
|
|
|
# Returns: Number of bytes read |
171
|
|
|
|
|
|
|
sub Read($$$) |
172
|
|
|
|
|
|
|
{ |
173
|
26659
|
|
|
26659
|
1
|
41889
|
my $self = shift; |
174
|
26659
|
|
|
|
|
36997
|
my $len = $_[1]; |
175
|
26659
|
|
|
|
|
35426
|
my $rtnVal; |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
# protect against reading too much at once |
178
|
|
|
|
|
|
|
# (also from dying with a "Negative length" error) |
179
|
26659
|
50
|
|
|
|
53440
|
if ($len & 0xf8000000) { |
180
|
0
|
0
|
|
|
|
0
|
return 0 if $len < 0; |
181
|
|
|
|
|
|
|
# read in smaller blocks because Windows attempts to pre-allocate |
182
|
|
|
|
|
|
|
# memory for the full size, which can lead to an out-of-memory error |
183
|
0
|
|
|
|
|
0
|
my $maxLen = 0x4000000; # (MUST be less than bitmask in "if" above) |
184
|
0
|
|
|
|
|
0
|
my $num = Read($self, $_[0], $maxLen); |
185
|
0
|
0
|
|
|
|
0
|
return $num if $num < $maxLen; |
186
|
0
|
|
|
|
|
0
|
for (;;) { |
187
|
0
|
|
|
|
|
0
|
$len -= $maxLen; |
188
|
0
|
0
|
|
|
|
0
|
last if $len <= 0; |
189
|
0
|
0
|
|
|
|
0
|
my $l = $len < $maxLen ? $len : $maxLen; |
190
|
0
|
|
|
|
|
0
|
my $buff; |
191
|
0
|
|
|
|
|
0
|
my $n = Read($self, $buff, $l); |
192
|
0
|
0
|
|
|
|
0
|
last unless $n; |
193
|
0
|
|
|
|
|
0
|
$_[0] .= $buff; |
194
|
0
|
|
|
|
|
0
|
$num += $n; |
195
|
0
|
0
|
|
|
|
0
|
last if $n < $l; |
196
|
|
|
|
|
|
|
} |
197
|
0
|
|
|
|
|
0
|
return $num; |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
# read through our buffer if necessary |
200
|
26659
|
100
|
|
|
|
52245
|
if ($self->{TESTED} < 0) { |
201
|
|
|
|
|
|
|
# purge old data before reading in NoBuffer mode |
202
|
7649
|
50
|
0
|
|
|
15028
|
$self->Purge() or return 0 if $self->{NoBuffer}; |
203
|
7649
|
|
|
|
|
10212
|
my $buff; |
204
|
7649
|
|
|
|
|
11525
|
my $newPos = $self->{POS} + $len; |
205
|
|
|
|
|
|
|
# number of bytes to read from file |
206
|
7649
|
|
|
|
|
11723
|
my $num = $newPos - $self->{LEN}; |
207
|
7649
|
50
|
66
|
|
|
14552
|
if ($num > 0 and $self->{FILE_PT}) { |
208
|
|
|
|
|
|
|
# read data from file in multiples of $CHUNK_SIZE |
209
|
0
|
|
|
|
|
0
|
$num = (($num - 1) | ($CHUNK_SIZE - 1)) + 1; |
210
|
0
|
|
|
|
|
0
|
$num = read($self->{FILE_PT}, $buff, $num); |
211
|
0
|
0
|
|
|
|
0
|
if ($num) { |
212
|
0
|
|
|
|
|
0
|
${$self->{BUFF_PT}} .= $buff; |
|
0
|
|
|
|
|
0
|
|
213
|
0
|
|
|
|
|
0
|
$self->{LEN} += $num; |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
# number of bytes left in data buffer |
217
|
7649
|
|
|
|
|
12088
|
$num = $self->{LEN} - $self->{POS}; |
218
|
7649
|
100
|
|
|
|
13338
|
if ($len <= $num) { |
|
|
100
|
|
|
|
|
|
219
|
7190
|
|
|
|
|
10039
|
$rtnVal = $len; |
220
|
|
|
|
|
|
|
} elsif ($num <= 0) { |
221
|
361
|
|
|
|
|
635
|
$_[0] = ''; |
222
|
361
|
|
|
|
|
1099
|
return 0; |
223
|
|
|
|
|
|
|
} else { |
224
|
98
|
|
|
|
|
188
|
$rtnVal = $num; |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
# return data from our buffer |
227
|
7288
|
|
|
|
|
9205
|
$_[0] = substr(${$self->{BUFF_PT}}, $self->{POS}, $rtnVal); |
|
7288
|
|
|
|
|
17964
|
|
228
|
7288
|
|
|
|
|
12598
|
$self->{POS} += $rtnVal; |
229
|
|
|
|
|
|
|
} else { |
230
|
|
|
|
|
|
|
# read directly from file |
231
|
19010
|
100
|
|
|
|
39209
|
$_[0] = '' unless defined $_[0]; |
232
|
19010
|
|
100
|
|
|
132035
|
$rtnVal = read($self->{FILE_PT}, $_[0], $len) || 0; |
233
|
|
|
|
|
|
|
} |
234
|
26298
|
50
|
|
|
|
57662
|
if ($self->{DEBUG}) { |
235
|
0
|
|
|
|
|
0
|
my $pos = $self->Tell() - $rtnVal; |
236
|
0
|
0
|
0
|
|
|
0
|
unless ($self->{DEBUG}->{$pos} and $self->{DEBUG}->{$pos} > $rtnVal) { |
237
|
0
|
|
|
|
|
0
|
$self->{DEBUG}->{$pos} = $rtnVal; |
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
} |
240
|
26298
|
|
|
|
|
89581
|
return $rtnVal; |
241
|
|
|
|
|
|
|
} |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
244
|
|
|
|
|
|
|
# Read a line from file (end of line is $/) |
245
|
|
|
|
|
|
|
# Inputs: 0) reference to RandomAccess object, 1) buffer |
246
|
|
|
|
|
|
|
# Returns: Number of bytes read |
247
|
|
|
|
|
|
|
sub ReadLine($$) |
248
|
|
|
|
|
|
|
{ |
249
|
6601
|
|
|
6601
|
1
|
11767
|
my $self = shift; |
250
|
6601
|
|
|
|
|
9287
|
my $rtnVal; |
251
|
6601
|
|
|
|
|
11421
|
my $fp = $self->{FILE_PT}; |
252
|
|
|
|
|
|
|
|
253
|
6601
|
100
|
|
|
|
13299
|
if ($self->{TESTED} < 0) { |
254
|
123
|
|
|
|
|
233
|
my ($num, $buff); |
255
|
123
|
50
|
0
|
|
|
336
|
$self->Purge() or return 0 if $self->{NoBuffer}; |
256
|
123
|
|
|
|
|
249
|
my $pos = $self->{POS}; |
257
|
123
|
50
|
|
|
|
262
|
if ($fp) { |
258
|
|
|
|
|
|
|
# make sure we have some data after the current position |
259
|
0
|
|
|
|
|
0
|
while ($self->{LEN} <= $pos) { |
260
|
0
|
|
|
|
|
0
|
$num = read($fp, $buff, $CHUNK_SIZE); |
261
|
0
|
0
|
|
|
|
0
|
return 0 unless $num; |
262
|
0
|
|
|
|
|
0
|
${$self->{BUFF_PT}} .= $buff; |
|
0
|
|
|
|
|
0
|
|
263
|
0
|
|
|
|
|
0
|
$self->{LEN} += $num; |
264
|
|
|
|
|
|
|
} |
265
|
|
|
|
|
|
|
# scan and read until we find the EOL (or hit EOF) |
266
|
0
|
|
|
|
|
0
|
for (;;) { |
267
|
0
|
|
|
|
|
0
|
$pos = index(${$self->{BUFF_PT}}, $/, $pos); |
|
0
|
|
|
|
|
0
|
|
268
|
0
|
0
|
|
|
|
0
|
if ($pos >= 0) { |
269
|
0
|
|
|
|
|
0
|
$pos += length($/); |
270
|
0
|
|
|
|
|
0
|
last; |
271
|
|
|
|
|
|
|
} |
272
|
0
|
|
|
|
|
0
|
$pos = $self->{LEN}; # have scanned to end of buffer |
273
|
0
|
0
|
|
|
|
0
|
$num = read($fp, $buff, $CHUNK_SIZE) or last; |
274
|
0
|
|
|
|
|
0
|
${$self->{BUFF_PT}} .= $buff; |
|
0
|
|
|
|
|
0
|
|
275
|
0
|
|
|
|
|
0
|
$self->{LEN} += $num; |
276
|
|
|
|
|
|
|
} |
277
|
|
|
|
|
|
|
} else { |
278
|
|
|
|
|
|
|
# string i/o |
279
|
123
|
|
|
|
|
204
|
$pos = index(${$self->{BUFF_PT}}, $/, $pos); |
|
123
|
|
|
|
|
1199
|
|
280
|
123
|
100
|
|
|
|
342
|
if ($pos < 0) { |
281
|
18
|
|
|
|
|
35
|
$pos = $self->{LEN}; |
282
|
18
|
50
|
|
|
|
67
|
$self->{POS} = $pos if $self->{POS} > $pos; |
283
|
|
|
|
|
|
|
} else { |
284
|
105
|
|
|
|
|
239
|
$pos += length($/); |
285
|
|
|
|
|
|
|
} |
286
|
|
|
|
|
|
|
} |
287
|
|
|
|
|
|
|
# read the line from our buffer |
288
|
123
|
|
|
|
|
224
|
$rtnVal = $pos - $self->{POS}; |
289
|
123
|
|
|
|
|
191
|
$_[0] = substr(${$self->{BUFF_PT}}, $self->{POS}, $rtnVal); |
|
123
|
|
|
|
|
370
|
|
290
|
123
|
|
|
|
|
255
|
$self->{POS} = $pos; |
291
|
|
|
|
|
|
|
} else { |
292
|
6478
|
|
|
|
|
27811
|
$_[0] = <$fp>; |
293
|
6478
|
100
|
|
|
|
13212
|
if (defined $_[0]) { |
294
|
6458
|
|
|
|
|
9757
|
$rtnVal = length($_[0]); |
295
|
|
|
|
|
|
|
} else { |
296
|
20
|
|
|
|
|
58
|
$rtnVal = 0; |
297
|
|
|
|
|
|
|
} |
298
|
|
|
|
|
|
|
} |
299
|
6601
|
50
|
|
|
|
14115
|
if ($self->{DEBUG}) { |
300
|
0
|
|
|
|
|
0
|
my $pos = $self->Tell() - $rtnVal; |
301
|
0
|
0
|
0
|
|
|
0
|
unless ($self->{DEBUG}->{$pos} and $self->{DEBUG}->{$pos} > $rtnVal) { |
302
|
0
|
|
|
|
|
0
|
$self->{DEBUG}->{$pos} = $rtnVal; |
303
|
|
|
|
|
|
|
} |
304
|
|
|
|
|
|
|
} |
305
|
6601
|
|
|
|
|
20417
|
return $rtnVal; |
306
|
|
|
|
|
|
|
} |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
309
|
|
|
|
|
|
|
# Read whole file into buffer (without changing read pointer) |
310
|
|
|
|
|
|
|
# Inputs: 0) reference to RandomAccess object |
311
|
|
|
|
|
|
|
sub Slurp($) |
312
|
|
|
|
|
|
|
{ |
313
|
92
|
|
|
92
|
1
|
213
|
my $self = shift; |
314
|
92
|
|
50
|
|
|
303
|
my $fp = $self->{FILE_PT} || return; |
315
|
|
|
|
|
|
|
# read whole file into buffer (in large chunks) |
316
|
0
|
|
|
|
|
0
|
my ($buff, $num); |
317
|
0
|
|
|
|
|
0
|
while (($num = read($fp, $buff, $CHUNK_SIZE * $SLURP_CHUNKS)) != 0) { |
318
|
0
|
|
|
|
|
0
|
${$self->{BUFF_PT}} .= $buff; |
|
0
|
|
|
|
|
0
|
|
319
|
0
|
|
|
|
|
0
|
$self->{LEN} += $num; |
320
|
|
|
|
|
|
|
} |
321
|
|
|
|
|
|
|
} |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
324
|
|
|
|
|
|
|
# Purge internal buffer [internal use only] |
325
|
|
|
|
|
|
|
# Inputs: 0) reference to RandomAccess object |
326
|
|
|
|
|
|
|
# Returns: 1 on success, or 0 if current buffer position is negative |
327
|
|
|
|
|
|
|
# Notes: This is called only in NoBuffer mode |
328
|
|
|
|
|
|
|
sub Purge($) |
329
|
|
|
|
|
|
|
{ |
330
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
331
|
0
|
0
|
|
|
|
0
|
return 1 unless $self->{FILE_PT}; |
332
|
0
|
0
|
|
|
|
0
|
return 0 if $self->{POS} < 0; # error if we can't read from here |
333
|
0
|
0
|
|
|
|
0
|
if ($self->{POS} > $CHUNK_SIZE) { |
334
|
0
|
|
|
|
|
0
|
my $purge = $self->{POS} - ($self->{POS} % $CHUNK_SIZE); |
335
|
0
|
0
|
|
|
|
0
|
if ($purge >= $self->{LEN}) { |
|
|
0
|
|
|
|
|
|
336
|
|
|
|
|
|
|
# read up to current position in 64k chunks, discarding as we go |
337
|
0
|
|
|
|
|
0
|
while ($self->{POS} > $self->{LEN}) { |
338
|
0
|
|
|
|
|
0
|
$self->{BASE} += $self->{LEN}; |
339
|
0
|
|
|
|
|
0
|
$self->{POS} -= $self->{LEN}; |
340
|
0
|
|
|
|
|
0
|
${$self->{BUFF_PT}} = ''; |
|
0
|
|
|
|
|
0
|
|
341
|
0
|
|
|
|
|
0
|
$self->{LEN} = read($self->{FILE_PT}, ${$self->{BUFF_PT}}, $SKIP_SIZE); |
|
0
|
|
|
|
|
0
|
|
342
|
0
|
0
|
|
|
|
0
|
last if $self->{LEN} < $SKIP_SIZE; |
343
|
|
|
|
|
|
|
} |
344
|
|
|
|
|
|
|
} elsif ($purge > 0) { |
345
|
0
|
|
|
|
|
0
|
${$self->{BUFF_PT}} = substr ${$self->{BUFF_PT}}, $purge; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
346
|
0
|
|
|
|
|
0
|
$self->{BASE} += $purge; |
347
|
0
|
|
|
|
|
0
|
$self->{POS} -= $purge; |
348
|
0
|
|
|
|
|
0
|
$self->{LEN} -= $purge; |
349
|
|
|
|
|
|
|
} |
350
|
|
|
|
|
|
|
} |
351
|
0
|
|
|
|
|
0
|
return 1; |
352
|
|
|
|
|
|
|
} |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
355
|
|
|
|
|
|
|
# Set binary mode |
356
|
|
|
|
|
|
|
# Inputs: 0) reference to RandomAccess object |
357
|
|
|
|
|
|
|
sub BinMode($) |
358
|
|
|
|
|
|
|
{ |
359
|
751
|
|
|
751
|
1
|
1894
|
my $self = shift; |
360
|
751
|
100
|
|
|
|
4554
|
binmode($self->{FILE_PT}) if $self->{FILE_PT}; |
361
|
|
|
|
|
|
|
} |
362
|
|
|
|
|
|
|
|
363
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
364
|
|
|
|
|
|
|
# Close the file and free the buffer |
365
|
|
|
|
|
|
|
# Inputs: 0) reference to RandomAccess object |
366
|
|
|
|
|
|
|
sub Close($) |
367
|
|
|
|
|
|
|
{ |
368
|
483
|
|
|
483
|
1
|
1387
|
my $self = shift; |
369
|
|
|
|
|
|
|
|
370
|
483
|
50
|
|
|
|
2090
|
if ($self->{DEBUG}) { |
371
|
0
|
|
|
|
|
0
|
local $_; |
372
|
0
|
0
|
|
|
|
0
|
if ($self->Seek(0,2)) { |
373
|
0
|
|
|
|
|
0
|
$self->{DEBUG}->{$self->Tell()} = 0; # set EOF marker |
374
|
0
|
|
|
|
|
0
|
my $last; |
375
|
0
|
|
|
|
|
0
|
my $tot = 0; |
376
|
0
|
|
|
|
|
0
|
my $bad = 0; |
377
|
0
|
|
|
|
|
0
|
foreach (sort { $a <=> $b } keys %{$self->{DEBUG}}) { |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
378
|
0
|
|
|
|
|
0
|
my $pos = $_; |
379
|
0
|
|
|
|
|
0
|
my $len = $self->{DEBUG}->{$_}; |
380
|
0
|
0
|
0
|
|
|
0
|
if (defined $last and $last < $pos) { |
381
|
0
|
|
|
|
|
0
|
my $bytes = $pos - $last; |
382
|
0
|
|
|
|
|
0
|
$tot += $bytes; |
383
|
0
|
|
|
|
|
0
|
$self->Seek($last); |
384
|
0
|
|
|
|
|
0
|
my $buff; |
385
|
0
|
|
|
|
|
0
|
$self->Read($buff, $bytes); |
386
|
0
|
|
|
|
|
0
|
my $warn = ''; |
387
|
0
|
0
|
|
|
|
0
|
if ($buff =~ /[^\0]/) { |
388
|
0
|
|
|
|
|
0
|
$bad += ($pos - $last); |
389
|
0
|
|
|
|
|
0
|
$warn = ' - NON-ZERO!'; |
390
|
|
|
|
|
|
|
} |
391
|
0
|
|
|
|
|
0
|
printf "0x%.8x - 0x%.8x (%d bytes)$warn\n", $last, $pos, $bytes; |
392
|
|
|
|
|
|
|
} |
393
|
0
|
|
|
|
|
0
|
my $cur = $pos + $len; |
394
|
0
|
0
|
0
|
|
|
0
|
$last = $cur unless defined $last and $last > $cur; |
395
|
|
|
|
|
|
|
} |
396
|
0
|
|
|
|
|
0
|
print "$tot bytes missed"; |
397
|
0
|
0
|
|
|
|
0
|
$bad and print ", $bad non-zero!"; |
398
|
0
|
|
|
|
|
0
|
print "\n"; |
399
|
|
|
|
|
|
|
} else { |
400
|
0
|
|
|
|
|
0
|
warn "File::RandomAccess DEBUG not working (file already closed?)\n"; |
401
|
|
|
|
|
|
|
} |
402
|
0
|
|
|
|
|
0
|
delete $self->{DEBUG}; |
403
|
|
|
|
|
|
|
} |
404
|
|
|
|
|
|
|
# close the file |
405
|
483
|
100
|
|
|
|
2128
|
if ($self->{FILE_PT}) { |
406
|
481
|
|
|
|
|
10739
|
close($self->{FILE_PT}); |
407
|
481
|
|
|
|
|
2447
|
delete $self->{FILE_PT}; |
408
|
|
|
|
|
|
|
} |
409
|
|
|
|
|
|
|
# reset the buffer |
410
|
483
|
|
|
|
|
1826
|
my $emptyBuff = ''; |
411
|
483
|
|
|
|
|
1869
|
$self->{BUFF_PT} = \$emptyBuff; |
412
|
483
|
|
|
|
|
1226
|
$self->{BASE} = 0; |
413
|
483
|
|
|
|
|
1204
|
$self->{LEN} = 0; |
414
|
483
|
|
|
|
|
1731
|
$self->{POS} = 0; |
415
|
|
|
|
|
|
|
} |
416
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
418
|
|
|
|
|
|
|
1; # end |