forked from beyondgrep/ack2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Basic.pm
167 lines (114 loc) · 3.21 KB
/
Basic.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package App::Ack::Resource::Basic;
=head1 App::Ack::Resource::Basic
=cut
use warnings;
use strict;
use base 'App::Ack::Resource';
=head1 METHODS
=head2 new( $filename )
Opens the file specified by I<$filename> and returns a filehandle and
a flag that says whether it could be binary.
If there's a failure, it throws a warning and returns an empty list.
=cut
sub new {
my $class = shift;
my $filename = shift;
my $self = bless {
filename => $filename,
fh => undef,
opened => undef,
}, $class;
if ( $self->{filename} eq '-' ) {
$self->{fh} = *STDIN;
}
else {
if ( !open( $self->{fh}, '<', $self->{filename} ) && $App::Ack::report_bad_filenames ) {
App::Ack::warn( "$self->{filename}: $!" );
return;
}
}
return $self;
}
=head2 $res->name()
Returns the name of the resource.
=cut
sub name {
my $self = shift;
return $self->{filename};
}
=head2 $res->needs_line_scan( \%opts )
API: Tells if the resource needs a line-by-line scan. This is a big
optimization because if you can tell from the outset that the pattern
is not found in the resource at all, then there's no need to do the
line-by-line iteration. If in doubt, return true.
Base: Slurp up an entire file up to 100K, see if there are any
matches in it, and if so, let us know so we can iterate over it
directly. If it's bigger than 100K or the match is inverted, we
have to do the line-by-line, too.
=cut
sub needs_line_scan {
my $self = shift;
my $opt = shift;
return 1 if $opt->{v};
my $size = -s $self->{fh};
if ( $size == 0 ) {
return 0;
}
elsif ( $size > 100_000 ) {
return 1;
}
my $buffer;
my $rc = sysread( $self->{fh}, $buffer, $size );
if ( !defined($rc) && $App::Ack::report_bad_filenames ) {
App::Ack::warn( "$self->{filename}: $!" );
return 1;
}
return 0 unless $rc && ( $rc == $size );
my $regex = $opt->{regex};
return $buffer =~ /$regex/m;
}
=head2 $res->reset()
Resets the resource back to the beginning. This is only called if
C<needs_line_scan()> is true, but not always if C<needs_line_scan()>
is true.
=cut
sub reset {
my $self = shift;
if( !seek( $self->{fh}, 0, 0 ) && $App::Ack::report_bad_filenames ) {
App::Ack::warn( "$self->{filename}: $!" );
}
return;
}
=head2 $res->next_text()
API: Gets the next line of text from the resource. Returns true
if there is one, or false if not.
Sets C<$_> with the line of text, and C<$.> for the ID number of
the text. This basically emulates a call to C<< <$fh> >>.
=cut
sub next_text {
if ( defined ($_ = readline $_[0]->{fh}) ) {
$. = ++$_[0]->{line};
s/[\r\n]+$//; # chomp may not handle this
$_ .= "\n"; # add back newline (XXX make it native)
return 1;
}
return;
}
=head2 $res->close()
API: Close the resource.
=cut
sub close {
my $self = shift;
if ( !close($self->{fh}) && $App::Ack::report_bad_filenames ) {
App::Ack::warn( $self->name() . ": $!" );
}
return;
}
=head2 $res->clone()
API: Clone this resource.
=cut
sub clone {
my ( $self ) = @_;
return __PACKAGE__->new($self->name);
}
1;