| 1 | package Text::Hunspell::FFI; | ||||||||||
| 2 | |||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 10 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 10 | ▶ | use 5.020; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 10 | ▶ | use FFI::Platypus 1.00; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 10 | ▶ | use FFI::CheckLib 0.27 (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 10 | ▶ | use experimental qw( postderef ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | # ABSTRACT: Perl FFI interface to the Hunspell library | ||||||||||
| 11 | 10 | our $VERSION = '0.04'; # VERSION | |||||||||
| 12 | |||||||||||
| 13 | sub _lib | ||||||||||
| 14 | 11 | ▶ | { | ||||||||
|
Subroutine
_lib: called CC=3 SCAR=11.2
| |||||||||||
| 15 | 11 | ▶ | my @args = (lib => '*', verify => sub { $_[0] =~ /hunspell/ }, symbol => "Hunspell_create"); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 11 | my @libs = FFI::CheckLib::find_lib @args; | |||||||||
| 17 | 11 | ▶ | my($first) = @libs | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | ? @libs | ||||||||||
| 19 | : FFI::CheckLib::find_lib @args, alien => 'Alien::Hunspell'; | ||||||||||
| 20 | 11 | ▶ | die "unable to find libs" unless $first; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 11 | $first; | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 10 | my $ffi = FFI::Platypus->new( | |||||||||
| 25 | api => 1, | ||||||||||
| 26 | lib => _lib(), | ||||||||||
| 27 | ); | ||||||||||
| 28 | |||||||||||
| 29 | $ffi->attach(['Hunspell_create'=>'new'] => ['string','string'] => 'opaque', sub | ||||||||||
| 30 | 8 | ▶ | { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 8 | my($xsub, $class, $aff, $dic) = @_; | |||||||||
| 32 | 8 | my $ptr = $xsub->($aff, $dic); | |||||||||
| 33 | 8 | bless \$ptr, $class; | |||||||||
| 34 | 10 | }); | |||||||||
| 35 | |||||||||||
| 36 | |||||||||||
| 37 | $ffi->attach(['Hunspell_destroy'=>'DESTROY'] => ['opaque'] => 'void', sub | ||||||||||
| 38 | 8 | ▶ | { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 8 | my($xsub, $self) = @_; | |||||||||
| 40 | 8 | $xsub->($$self); | |||||||||
| 41 | 10 | }); | |||||||||
| 42 | |||||||||||
| 43 | 10 | foreach my $try (qw( Hunspell_add_dic _ZN8Hunspell7add_dicEPKcS1_ )) | |||||||||
| 44 | { | ||||||||||
| 45 | 10 | eval { | |||||||||
| 46 | $ffi->attach([$try=>'add_dic'] => ['opaque','string'] => 'void', sub | ||||||||||
| 47 | 1 | ▶ | { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 1 | my($xsub, $self, $dpath) = @_; | |||||||||
| 49 | 1 | $xsub->($$self, $dpath); | |||||||||
| 50 | 10 | }); | |||||||||
| 51 | }; | ||||||||||
| 52 | 10 | ▶ | last unless $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 10 | ▶ | unless(__PACKAGE__->can('add_dic')) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | { | ||||||||||
| 57 | # TODO: fallback on Perl implementation ? | ||||||||||
| 58 | 0 | die "unable to find add_dic"; | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | $ffi->attach(['Hunspell_spell'=>'check'] => ['opaque','string'] => 'int', sub | ||||||||||
| 62 | 9 | ▶ | { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 63 | 9 | my($xsub, $self, $word) = @_; | |||||||||
| 64 | 9 | $xsub->($$self, $word); | |||||||||
| 65 | 10 | }); | |||||||||
| 66 | |||||||||||
| 67 | 10 | $ffi->attach(['Hunspell_free_list',=>'_free_list'] => ['opaque','opaque*','int'] => 'void'); | |||||||||
| 68 | |||||||||||
| 69 | sub _string_array_and_word | ||||||||||
| 70 | 2 | ▶ | { | ||||||||
|
Subroutine
_string_array_and_word: called CC=4 SCAR=14.0
| |||||||||||
| 71 | 2 | my($xsub, $self, $word) = @_; | |||||||||
| 72 | 2 | my $ptr; | |||||||||
| 73 | 2 | my $count = $xsub->($$self, \$ptr, $word); | |||||||||
| 74 | 2 | ▶ | my @result = defined $ptr ? map { $ffi->cast('opaque','string',$_) } $ffi->cast('opaque',"opaque[$count]", $ptr)->@* : (); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 2 | ▶ | _free_list($self, $ptr, $count) if defined $ptr; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 2 | ▶ | wantarray ? @result : $result[0]; ## no critic (Freenode::Wantarray) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 10 | $ffi->attach(['Hunspell_suggest'=>'suggest'] => ['opaque','opaque*','string'] => 'int', \&_string_array_and_word); | |||||||||
| 80 | 10 | $ffi->attach(['Hunspell_analyze'=>'analyze'] => ['opaque','opaque*','string'] => 'int', \&_string_array_and_word); | |||||||||
| 81 | |||||||||||
| 82 | 16 | ▶ | $ffi->attach(['Hunspell_generate'=>'generate'] => ['opaque','opaque*','string','string'] => 'int', sub { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.3
| |||||||||||
| 83 | 16 | my($xsub, $self, $word, $word2) = @_; | |||||||||
| 84 | 16 | my $ptr; | |||||||||
| 85 | 16 | my $count = $xsub->($$self, \$ptr, $word, $word2); | |||||||||
| 86 | 16 | ▶ | my @result = defined $ptr ? map { $ffi->cast('opaque','string',$_) } $ffi->cast('opaque',"opaque[$count]", $ptr)->@* : (); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 16 | ▶ | _free_list($self, $ptr, $count) if $ptr; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 16 | ▶ | wantarray ? @result : $result[0]; ## no critic (Freenode::Wantarray) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 10 | }); | |||||||||
| 90 | |||||||||||
| 91 | $ffi->attach(['Hunspell_generate2'=>'generate2'] => ['opaque','opaque*','string','string[]','int'] => 'int', sub | ||||||||||
| 92 | 1 | ▶ | { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.2
| |||||||||||
| 93 | 1 | my($xsub, $self, $word, $suggestions) = @_; | |||||||||
| 94 | 1 | my $n = scalar @$suggestions; | |||||||||
| 95 | 1 | my $ptr; | |||||||||
| 96 | 1 | my $count = $xsub->($$self, \$ptr, $word, [@$suggestions], 1); | |||||||||
| 97 | 1 | ▶ | my @result = defined $ptr ? map { $ffi->cast('opaque','string',$_) } $ffi->cast('opaque',"opaque[$count]", $ptr)->@* : (); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 1 | ▶ | _free_list($self, $ptr, $count) if defined $ptr; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 1 | ▶ | wantarray ? @result : $result[0]; ## no critic (Freenode::Wantarray) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 10 | }); | |||||||||
| 101 | |||||||||||
| 102 | 10 | 1; | |||||||||
| 103 | |||||||||||
| 104 | __END__ | ||||||||||