| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Treex::PML::Backend::NTRED; | 
| 2 | 1 |  |  | 1 |  | 754 | use Treex::PML; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | use Storable qw(nfreeze thaw); | 
| 4 |  |  |  |  |  |  | use MIME::Base64; | 
| 5 |  |  |  |  |  |  | use Treex::PML::IO; | 
| 6 |  |  |  |  |  |  | use strict; | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | use vars qw($VERSION); | 
| 9 |  |  |  |  |  |  | BEGIN { | 
| 10 |  |  |  |  |  |  | $VERSION='2.21'; # version template | 
| 11 |  |  |  |  |  |  | } | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | use vars qw($ntred); | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | $ntred='ntred'; | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | sub test { | 
| 19 |  |  |  |  |  |  | my ($filename,$encoding)=@_; | 
| 20 |  |  |  |  |  |  | return $filename=~m(^ntred://); | 
| 21 |  |  |  |  |  |  | } | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub open_backend { | 
| 24 |  |  |  |  |  |  | my ($filename, $mode, $encoding)=@_; | 
| 25 |  |  |  |  |  |  | my $fh = undef; | 
| 26 |  |  |  |  |  |  | my $cmd = ""; | 
| 27 |  |  |  |  |  |  | return unless $filename=~m(^ntred://(.*)$); | 
| 28 |  |  |  |  |  |  | $filename=$1; | 
| 29 |  |  |  |  |  |  | $filename=~s/@/##/; | 
| 30 |  |  |  |  |  |  | if ($filename) { | 
| 31 |  |  |  |  |  |  | if ($mode eq 'w') { | 
| 32 |  |  |  |  |  |  | open($fh,'|-',$ntred, '-Q', '--upload-file', $filename) || die "Failed to start NTrEd Client '$ntred': $!\n"; | 
| 33 |  |  |  |  |  |  | } else { | 
| 34 |  |  |  |  |  |  | open($fh,'-|',$ntred, '-Q', '--dump-files', $filename) || die "Failed NTrEd Client '$ntred': $!\n"; | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  | return Treex::PML::IO::set_encoding($fh,$encoding); | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | sub close_backend { | 
| 41 |  |  |  |  |  |  | my ($fh)=@_; | 
| 42 |  |  |  |  |  |  | return $fh && $fh->close(); | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | sub read { | 
| 46 |  |  |  |  |  |  | my ($fd,$fs)=@_; | 
| 47 |  |  |  |  |  |  | my $data = do{{ local $/; <$fd>}}; | 
| 48 |  |  |  |  |  |  | unless (defined $data and length $data) { | 
| 49 |  |  |  |  |  |  | die "NTrEd Client returned no data\n"; | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  | my $fs_files = Storable::thaw(decode_base64($data)); | 
| 52 |  |  |  |  |  |  | undef $data; | 
| 53 |  |  |  |  |  |  | my $restore = $fs_files->[0]; | 
| 54 |  |  |  |  |  |  | if (ref($restore)) { | 
| 55 |  |  |  |  |  |  | my $api_version = $restore->[6]; | 
| 56 |  |  |  |  |  |  | if ($api_version ne $Treex::PML::API_VERSION) { | 
| 57 |  |  |  |  |  |  | warn "Warning: the binary content obtained via Treex::PML::Backend::NTRED from ".$fs->filename." is a dump of structures created by possibly incompatible Treex::PML API version $api_version (the current Treex::PML API version is $Treex::PML::API_VERSION)\n"; | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  | $fs->changeFS($restore->[0]); | 
| 60 |  |  |  |  |  |  | $fs->changeTrees(@{$restore->[1]}); | 
| 61 |  |  |  |  |  |  | $fs->changeTail(@{$restore->[2]}); | 
| 62 |  |  |  |  |  |  | $fs->[13]=$restore->[3]; # metaData | 
| 63 |  |  |  |  |  |  | $fs->changePatterns(@{$restore->[4]}); | 
| 64 |  |  |  |  |  |  | $fs->changeHint($restore->[5]); | 
| 65 |  |  |  |  |  |  | $fs->FS->renew_specials(); | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | sub write { | 
| 72 |  |  |  |  |  |  | my ($fh,$fsfile)=@_; | 
| 73 |  |  |  |  |  |  | my $dump= [$fsfile->FS, | 
| 74 |  |  |  |  |  |  | $fsfile->treeList, | 
| 75 |  |  |  |  |  |  | [$fsfile->tail], | 
| 76 |  |  |  |  |  |  | $fsfile->[13], # metaData | 
| 77 |  |  |  |  |  |  | [$fsfile->patterns], | 
| 78 |  |  |  |  |  |  | $fsfile->hint, | 
| 79 |  |  |  |  |  |  | $Treex::PML::API_VERSION | 
| 80 |  |  |  |  |  |  | # CAUTION: when adding to this list, don't forget to do the same | 
| 81 |  |  |  |  |  |  | # in btred DUMP request handler | 
| 82 |  |  |  |  |  |  | ]; | 
| 83 |  |  |  |  |  |  | eval { | 
| 84 |  |  |  |  |  |  | print $fh (encode_base64(Storable::nfreeze([$dump]))); | 
| 85 |  |  |  |  |  |  | print $fh ("\n"); | 
| 86 |  |  |  |  |  |  | }; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | 1; | 
| 90 |  |  |  |  |  |  | __END__ |