Guest User

Untitled

a guest
Jan 19th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. # usage: build_index(*DATA_HANDLE, *INDEX_HANDLE)
  2. sub build_index {
  3. my $data_file = shift;
  4. my $index_file = shift;
  5. my $offset = 0;
  6.  
  7. while (<$data_file>) {
  8. print $index_file pack("N", $offset);
  9. $offset = tell($data_file);
  10. }
  11. }
  12.  
  13. # usage: line_with_index(*DATA_HANDLE, *INDEX_HANDLE, $LINE_NUMBER)
  14. # returns line or undef if LINE_NUMBER was out of range
  15. sub line_with_index {
  16. my $data_file = shift;
  17. my $index_file = shift;
  18. my $line_number = shift;
  19.  
  20. my $size; # size of an index entry
  21. my $i_offset; # offset into the index of the entry
  22. my $entry; # index entry
  23. my $d_offset; # offset into the data file
  24.  
  25. $size = length(pack("N", 0));
  26. $i_offset = $size * ($line_number-1);
  27. seek($index_file, $i_offset, 0) or return;
  28. read($index_file, $entry, $size);
  29. $d_offset = unpack("N", $entry);
  30. seek($data_file, $d_offset, 0);
  31. return scalar(<$data_file>);
  32. }
  33.  
  34. use Config qw( %Config );
  35. my $off_t = $Config{lseeksize} > $Config{ivsize} ? 'F' : 'j';
  36.  
  37. ...
  38. print $index_file pack($off_t, $offset);
  39. ...
Add Comment
Please, Sign In to add comment