Aard

 view release on metacpan or  search on metacpan

lib/Aard.pm  view on Meta::CPAN

sub count         { shift->{index_count} }

sub meta                          { shift->{meta} }
sub article_count                 { shift->meta->{article_count} }
sub article_count_is_volume_total { shift->meta->{article_count_is_volume_total} }
sub index_language                { shift->meta->{index_language} }
sub article_language              { shift->meta->{article_language} }
sub title                         { shift->meta->{title} }
sub version                       { shift->meta->{version} }
sub description                   { shift->meta->{description} }
sub copyright                     { shift->meta->{copyright} }
sub license                       { shift->meta->{license} }
sub source                        { shift->meta->{source} }

sub key {
	my ($self, $index) = @_;
	unless (exists $self->{key}{$index}) {
		my $part = $self->read_at($self->{index2_offset} + $self->index1($index)->[0], 2);
		my $len = unpack 'S>', $part;
		read $self->{fh}, $self->{key}{$index}, $len;
	}
	$self->{key}{$index}
}

sub article {
	my ($self, $index) = @_;
	unless (exists $self->{article}{$index}) {
		my $part = $self->read_at($self->{article_offset} + $self->index1($index)->[1], 4);
		my $len = unpack 'L>', $part;
		read $self->{fh}, $part, $len;
		$self->{article}{$index} = decompress $part
	}
	$self->{article}{$index}
}

sub new {
	my ($self, $file) = @_;
	open my $fh, '<', $file or die $!;
	binmode $fh;
	my %header;
	for (@{HEADER_SPEC()}) {
		read $fh, my $part, $_->[2];
		$header{$_->[0]} = unpack $_->[1], $part;
	}

	die 'Not a recognized aarddict dictionary file' if $header{signature} ne 'aard';
	die 'Unknown file format version' if $header{version} != 1;

	read $fh, my $meta, $header{meta_length};
	$meta = decode_json decompress $meta;

	my %obj = (
		%header,
		fh => $fh,
		meta => $meta,
		index_format => ($header{index1_item_format} eq '>LL' ? 'L>L>' : 'L>Q>'),
		index_length => ($header{index1_item_format} eq '>LL' ? 8 : 12),
	);
	$obj{index1_offset} = $header_length + $obj{meta_length};
	$obj{index2_offset} = $obj{index1_offset} + $obj{index_count} * $obj{index_length};
	bless \%obj, $self
}

1;
__END__

=head1 NAME

Aard - Read aarddict dictionaries

=head1 SYNOPSIS

  use Aard;
  my $dict = Aard->new('something.aar');
  printf "This dictionary (volume %d of %d) has %d entries\n", $dict->volume, $dict->total_volumes, $dict->count;
  printf "The tenth entry's key: %s\n", $dict->key(9);
  printf "The tenth entry's value: %s\n", $dict->article(9);

=head1 DESCRIPTION

Aard is a module for reading files in the Aard Dictionary format (.aar). A dictionary is an array of I<(key, article)> pairs, with some associated metadata.

=over

=item B<new>(I<filename>)

Creates a new Aard object for the given file.

=item B<fh>

Returns the open filehandle to the dictionary.

=item B<count>

Returns the number of entries in this dictionary.

=item B<key>(I<index>)

Returns the key of the I<index>th element. This method caches the keys.

=item B<article>(I<index>)

Returns the article of the I<index>th element. This method caches the articles.

=item B<uuid>

Returns the UUID of this dictionary as a binary string. This is a value shared by all volumes of the same dictionary.

=item B<uuid_string>

Returns the UUID of this dictionary as a human-readable string. This is a value shared by all volumes of the same dictionary.

=item B<volume>

Returns the volume number of this file.

=item B<total_volumes>

Returns the total number of volumes for this dictionary.

=item B<meta>



( run in 0.568 second using v1.01-cache-2.11-cpan-5a3173703d6 )