#
# $Id: Saryer.pm,v 1.6 2000/12/18 05:10:08 knok Exp $
#
# Search::Saryer
#
# /* Copyright: LGPL */

package Search::Saryer;

=head1 NAME

Search::Saryer - a perl interface of libsary

=head1 SYNOPSIS

  my $saryer = Search::Saryer::new(filename->'search.txt');

  if (defined $saryer->search($pattern)) {
    while (defined ($line = saryer->get_next_line())) {
      print $line;
    }
  }

  # $saryer->destroy() # called automatically

=head1 DESCRIPTION

=head2 new

  $saryer->new('filename'->'file.txt')

Create saryer object.
If creation process was failed, it returns undef.

=head2 destroy

  $saryer->destroy()

Destruct saryer object. It is automatically called by destructor.
You don't need to call it implicity.

=head2 enable_cache

  $saryer->enable_cache()

Enable caching mechanism.

=head2 search

  $saryer->search($pattern)

Search by $pattern.

=head2 icase_search

  $saryer->icase_search($pattern)

Search with case-insensitive method.

=head2 get_next_line

  $saryer->get_next_line()

Get result of search/icase_search.

=head2 get_next_context_lines

  $saryer->get_next_context_lines($backward, $forward)

=head2 get_next_tagged_region

  $saryer->get_next_tagged_region($start_tag, $end_tag)

=head1 COPYRIGHT

LGPL

=cut

require Exporter;
require DynaLoader;

use Carp;

@ISA = qw(Exporter DynaLoader);

@EXPORT_OK = qw();
%EXPORT_TAGS = (all => [qw()]);

$VERSION = '0.10';

bootstrap Search::Saryer $VERSION;

sub new {
    my $self = {};
    my %args = @_;
    croak "No 'filename' argument" unless defined $args{'filename'};

    my $filename = $args{'filename'};

    $self->{_sary_ptr} = perl_saryer_new($filename);
    return undef unless defined $self->{_sary_ptr};
    bless $self;
    return $self;
}

sub destroy {
    my $self = shift;
    perl_saryer_destroy($self->{_sary_ptr}) if defined $self->{_sary_ptr};
    delete $self->{_sary_ptr};
}

sub DESTROY {
    my $self = shift;
    destroy($self);
}

sub enable_cache {
    my $self = shift;

    perl_saryer_enable_cache($self->{_sary_ptr});
}

sub search {
    my $self = shift;
    my $pattern = shift;

    perl_saryer_search($self->{_sary_ptr}, $pattern);
}

sub icase_search {
    my $self = shift;
    my $pattern = shift;

    perl_saryer_icase_search($self->{_sary_ptr}, $pattern);
}

sub get_next_line {
    my $self = shift;

    perl_saryer_get_next_line($self->{_sary_ptr});
}


sub get_next_context_lines {
    my $self = shift;
    my $backward = shift;
    my $forward = shift;

    perl_saryer_get_next_context_lines($self->{_sary_ptr}, $backward, $forward);
}

sub get_next_tagged_region {
    my $self = shift;
    my $start_tag = shift;
    my $end_tag = shift;

    perl_saryer_get_next_tagged_region($self->{_sary_ptr},
				       $start_tag, $end_tag);
}

sub sort_occurrences {
    my $self = shift;

    perl_saryer_sort_occurrences($self->{_sary_ptr});
}

1;
