Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1020 B

  1. ;# Usage: &look(*FILEHANDLE,$key,$dict,$fold)
  2. ;# Sets file position in FILEHANDLE to be first line greater than or equal
  3. ;# (stringwise) to $key. Pass flags for dictionary order and case folding.
  4. sub look {
  5. local(*FH,$key,$dict,$fold) = @_;
  6. local($max,$min,$mid,$_);
  7. local($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
  8. $blksize,$blocks) = stat(FH);
  9. $blksize = 8192 unless $blksize;
  10. $key =~ s/[^\w\s]//g if $dict;
  11. $key = lc $key if $fold;
  12. $max = int($size / $blksize);
  13. while ($max - $min > 1) {
  14. $mid = int(($max + $min) / 2);
  15. seek(FH,$mid * $blksize,0);
  16. $_ = <FH> if $mid; # probably a partial line
  17. $_ = <FH>;
  18. chop;
  19. s/[^\w\s]//g if $dict;
  20. $_ = lc $_ if $fold;
  21. if ($_ lt $key) {
  22. $min = $mid;
  23. }
  24. else {
  25. $max = $mid;
  26. }
  27. }
  28. $min *= $blksize;
  29. seek(FH,$min,0);
  30. <FH> if $min;
  31. while (<FH>) {
  32. chop;
  33. s/[^\w\s]//g if $dict;
  34. $_ = lc $_ if $fold;
  35. last if $_ ge $key;
  36. $min = tell(FH);
  37. }
  38. seek(FH,$min,0);
  39. $min;
  40. }
  41. 1;