Lingua-StarDict-Gen

 view release on metacpan or  search on metacpan

lib/Lingua/StarDict/Gen.pm  view on Meta::CPAN

package Lingua::StarDict::Gen;

use warnings;
use strict;
use Data::Dumper;
use locale;
use Encode;
use utf8;
use File::Spec::Functions;
#use POSIX qw(locale_h);
#setlocale(LC_ALL,"C");

$Data::Dumper::Indent=1;
$Data::Dumper::Terse=1;

our $VERSION = '0.10';

my $nome; my %dic; 

sub carregaDic { &loadDict; }

sub loadDict {
  my %opt =(type=> "default");
  local $/;
  if(ref($_[0]) eq "HASH") {%opt = (%opt , %{shift(@_)}) } ;

  if ($opt{type} eq "default"){ $/ = "\n"; }
  if ($opt{type} eq "term")   { $/ = "";   }

  my $file = shift;
  my %dic;
  open IN,"<$file" or die "Can load $file\n";
  while (<IN>) {
      chomp;
      if (m!^%enc(oding)?\s+([a-zA-Z0-9-]+)!) {
         binmode IN, ":$2";
         next
      } elsif ($opt{type} eq "term") {
        $opt{lang} = $1 if(!$opt{lang} &&  m((\w+)));

        my $inf={};
        my @ls = split (/\n(?=\S)/,$_);  
        for (@ls){
          if(/(\w+)\s+(.*)/s){ push( @{$inf->{$1}}, split (/\s*[;,]\s*/,$2));} 
        }
        for(@{$inf->{$opt{lang}}}){ 
          $dic{$_} = $inf;
        }
      } elsif ($opt{type} eq "default" && /(.*?)\s*\{\s*(.*?)\s*\}/) {
        my @palavras = split (/\s*;\s*/,$2);  
        $dic{$1} = [@palavras];
      }
  }
  close IN;
  \%dic
}

sub mostraDic { &showDict; }

sub showDict {
    $nome = shift;
    %dic = %{$nome};
    for my $chave (sort (keys %dic)) {
        for (@{$dic{$chave}}) {
            print "$chave -> $_\n";
        }
    }
}

sub escreveDic { &writeDict; }


sub writeDict {
    my $hash= shift;
    my $dic = shift;
    my $dirpath=shift;
    my $d ;  ## install dic directory
    my $s='/';
    if(    $^O eq "linux")  {$d= "/usr/share/stardict/dic/" }
    elsif( $^O eq "darwin") {$d= "/opt/gtk/share/stardict/dic/" }
    elsif( $^O eq "MSWin32"){$d= "$ENV{ProgramFiles}\\stardict\\dic\\";$s="\\"}
    $dirpath ||= "";
    $dirpath ||= $d if -d $d;
    $dirpath ||= "/usr/local/share/stardict/dic/" if -d "/usr/local/share/stardict/dic/";
    my $finalpath= catfile($dirpath,$dic);
    unless(-d $finalpath){
      mkdir($finalpath,0755) or die "Cant create directory $finalpath\n";
    }



( run in 0.544 second using v1.01-cache-2.11-cpan-d80b1682f3f )