Commit | Line | Data |
a687059c |
1 | ;# Usage: &look(*FILEHANDLE,$key,$dict,$fold) |
a6d71656 |
2 | # |
3 | # This library is no longer being maintained, and is included for backward |
4 | # compatibility with Perl 4 programs which may require it. |
5 | # |
6 | # In particular, this should not be used as an example of modern Perl |
7 | # programming techniques. |
8 | # |
a687059c |
9 | ;# Sets file position in FILEHANDLE to be first line greater than or equal |
10 | ;# (stringwise) to $key. Pass flags for dictionary order and case folding. |
11 | |
12 | sub look { |
fe14fcc3 |
13 | local(*FH,$key,$dict,$fold) = @_; |
a687059c |
14 | local($max,$min,$mid,$_); |
15 | local($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, |
16 | $blksize,$blocks) = stat(FH); |
17 | $blksize = 8192 unless $blksize; |
18 | $key =~ s/[^\w\s]//g if $dict; |
55497cff |
19 | $key = lc $key if $fold; |
ac58e20f |
20 | $max = int($size / $blksize); |
21 | while ($max - $min > 1) { |
22 | $mid = int(($max + $min) / 2); |
23 | seek(FH,$mid * $blksize,0); |
24 | $_ = <FH> if $mid; # probably a partial line |
a687059c |
25 | $_ = <FH>; |
26 | chop; |
27 | s/[^\w\s]//g if $dict; |
55497cff |
28 | $_ = lc $_ if $fold; |
a687059c |
29 | if ($_ lt $key) { |
30 | $min = $mid; |
31 | } |
32 | else { |
33 | $max = $mid; |
34 | } |
35 | } |
ac58e20f |
36 | $min *= $blksize; |
a687059c |
37 | seek(FH,$min,0); |
ac58e20f |
38 | <FH> if $min; |
a687059c |
39 | while (<FH>) { |
40 | chop; |
41 | s/[^\w\s]//g if $dict; |
55497cff |
42 | $_ = lc $_ if $fold; |
a687059c |
43 | last if $_ ge $key; |
44 | $min = tell(FH); |
45 | } |
46 | seek(FH,$min,0); |
47 | $min; |
48 | } |
49 | |
50 | 1; |