File: FFindex.pm

package info (click to toggle)
ffindex 0.9.9.9-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 488 kB
  • sloc: ansic: 2,382; makefile: 163; perl: 86; python: 56; sh: 21; ruby: 6
file content (94 lines) | stat: -rw-r--r-- 1,909 bytes parent folder | download | duplicates (3)
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
#
# A simple perl native Module interfacing FFindex
# written by Andreas Hauser <andreas-source@creative-memory.de
#

package FFindex;

use Sys::Mmap;

sub new
{
    my $class = shift;
    my $self = {
        ffdata_filename   => shift,
        ffindex_filename  => shift,
        mode              => shift,
        ffdata            => undef,
        ffdata_file       => undef,
        ffindex_file      => undef,
        ffindex           => [],
        ffindex_hash      => {},
        n_entries         => []
    };

    bless $self, $class;

    #new Sys::Mmap $self->{ffdata}, 0, $self->{ffdata_filename} or die $!;
    open($self->{ffdata_file}, "<", $self->{ffdata_filename});
    mmap($self->{ffdata}, 0, PROT_READ, MAP_SHARED, $self->{ffdata_file}) or die $!;
    open($self->{ffindex_file}, "<", $self->{ffindex_filename});

    my $fh = $self->{ffindex_file};
    my $i = 0;
    while( my $line = <$fh>)
    {   
      chomp $line;
      my ($name, $offset, $len) = split(/\t/, $line);
      $self->{ffindex}[$i] = [$name, $offset, $len];
      $self->{ffindex_hash}{$name} = $self->{ffindex}[$i];
      $i++;
    }
    $self->{n_entries} = $i;

    return $self;
}


sub get_num_entries()
{
  my( $self ) = @_;
  return $self->{n_entries};
}


sub get_entry_by_index()
{
  my( $self, $index ) = @_;
  return $self->{ffindex}[$index];
}


sub get_entry_by_name()
{
  my( $self, $qname ) = @_;
  return $self->{ffindex_hash}{$qname};
}


sub get_data_by_index()
{
  my( $self, $index ) = @_;
  my ($name, $offset, $len) = @{$self->{ffindex}[$index]};
  return substr($self->{ffdata}, $offset, $len - 1);
}


sub get_data_by_name()
{
  my( $self, $qname ) = @_;
  my ($name, $offset, $len) = @{$self->{ffindex_hash}{$qname}};
  return substr($self->{ffdata}, $offset, $len - 1);
}



sub DESTROY
{
  close($self->{ffdata_file});
  close($self->{ffindex_file});
  $self->{ffindex} = undef;
}


1;