-
Notifications
You must be signed in to change notification settings - Fork 7
/
FileIndex.pm
112 lines (91 loc) · 2.78 KB
/
FileIndex.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package FileIndex;
use vars qw($AUTOLOAD @ISA);
use Exporter;
use Carp;
use strict;
use FileHandle;
@ISA = qw(Exporter);
my $suffix = ".FileIndex";
my %fields = (
filename => undef,
verbose => undef,
filehandle => undef,
index => undef
);
sub new {
my $obj = shift;
my $filename = shift;
my $verbose = @_ ? shift : 1;
my $separator = @_ ? shift : $/;
my $pkg = ref($obj) || $obj;
my $self = { _permitted=>\%fields, %fields };
bless $self, $pkg;
$self->filename($filename);
$self->verbose($verbose);
$self->filehandle(new FileHandle);
$self->filehandle->open("< $filename") or confess ref($self).": couldn't open $filename for reading: $!";
$self->filehandle->input_record_separator($separator);
$self->buildindex($filename);
$self;
}
sub DESTROY {
my ($self) = @_;
$self->filehandle->close;
}
sub AUTOLOAD {
my $self = shift;
my $type = ref($self) || confess "AUTOLOAD: object unknown: $self";
my $name = $AUTOLOAD;
# don't propagate DESTROY messages...
$name =~ /::DESTROY/ && return;
$name =~ s/.*://; # get only the bit we want
unless (exists $self->{'_permitted'}->{$name} ) { confess "$type: can't access $name" }
if (@_) { return $self->{$name} = shift }
else { return $self->{$name} }
}
sub buildindex {
my ($self,$filename) = @_;
unless (-r $filename) { confess ref($self).": can't read $filename" }
my $indexfilename = "$filename$suffix";
my @fstat;
my @istat;
local *INDEX;
if (-e $indexfilename and @fstat=stat($filename), @istat=stat($indexfilename), $istat[9] > $fstat[9]) {
my $indexdata;
open INDEX, $indexfilename or confess ref($self).": couldn't open $indexfilename for reading: $!";
read INDEX, $indexdata, $istat[7];
close INDEX;
$self->index([unpack("I*",$indexdata)]);
} else {
carp ref($self).": building index for $filename" if $self->verbose;
my ($n,$fpos,@index);
$n = $fpos = 0;
while ($fpos = $self->filehandle->tell, $_ = $self->filehandle->getline) { push @index, $fpos }
open INDEX, ">$indexfilename" or confess ref($self).": couldn't open $indexfilename for writing: $!";
print INDEX pack("I*",@index);
close INDEX;
$self->index(\@index);
}
}
# data access methods
sub lines {
my ($self) = @_;
scalar(@{$self->index});
}
sub getline {
my ($self,@n) = @_;
my @result;
my $index = $self->index;
my $n;
foreach $n (@n) {
if ($n < 0 || $n >= @$index) {
carp ref($self).": tried to access line $n of ".$self->filename." (only ".$self->lines." lines long)" if $self->verbose;
push @result, undef;
} else {
$self->filehandle->seek($index->[$n],0);
push @result, $self->filehandle->getline;
}
}
@n==1 ? shift(@result) : @result; # return scalar if possible
}
1;