2010-12-01 03:35:11 +01:00
|
|
|
#!/usr/bin/perl -CS
|
2004-06-08 10:13:17 +02:00
|
|
|
#
|
2010-12-01 03:35:11 +01:00
|
|
|
# Copyright 2004 - 2009 by Eric House (xwords@eehouse.org)
|
2004-06-10 05:27:40 +02:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
2004-06-08 10:13:17 +02:00
|
|
|
|
2004-06-10 05:27:40 +02:00
|
|
|
|
|
|
|
# Given a .pdb or .xwd file, print all the words in the DAWG to
|
|
|
|
# stdout.
|
2004-06-08 10:13:17 +02:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Fcntl;
|
2010-12-01 03:35:11 +01:00
|
|
|
use Encode 'from_to';
|
|
|
|
use Encode;
|
2004-06-08 10:13:17 +02:00
|
|
|
|
|
|
|
my $gInFile;
|
2004-10-15 06:01:22 +02:00
|
|
|
my $gDoRaw = 0;
|
2010-12-01 03:35:11 +01:00
|
|
|
my $gDoJSON = 0;
|
2004-06-08 10:13:17 +02:00
|
|
|
my $gFileType;
|
|
|
|
my $gNodeSize;
|
|
|
|
|
2009-03-29 20:13:09 +02:00
|
|
|
use Fcntl 'SEEK_CUR';
|
|
|
|
sub systell { sysseek($_[0], 0, SEEK_CUR) }
|
|
|
|
|
2004-06-08 10:13:17 +02:00
|
|
|
sub usage() {
|
|
|
|
print STDERR "USAGE: $0 "
|
2010-12-01 03:35:11 +01:00
|
|
|
. "[-raw | -json] "
|
2004-10-15 06:01:22 +02:00
|
|
|
. "-dict <xwdORpdb>"
|
2004-06-10 05:27:40 +02:00
|
|
|
. "\n"
|
|
|
|
. "\t(Takes a .pdb or .xwd and prints its words to stdout)\n";
|
|
|
|
exit 1;
|
2004-06-08 10:13:17 +02:00
|
|
|
}
|
|
|
|
|
2004-06-10 05:27:40 +02:00
|
|
|
sub parseARGV() {
|
|
|
|
|
2004-10-15 06:01:22 +02:00
|
|
|
while ( my $parm = shift(@ARGV) ) {
|
|
|
|
if ( $parm eq "-raw" ) {
|
|
|
|
$gDoRaw = 1;
|
2010-12-01 03:35:11 +01:00
|
|
|
} elsif ( $parm eq "-json" ) {
|
|
|
|
$gDoJSON = 1;
|
2004-10-15 06:01:22 +02:00
|
|
|
} elsif ( $parm eq "-dict" ) {
|
|
|
|
$gInFile = shift(@ARGV);
|
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
}
|
2004-06-08 10:13:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $gInFile =~ m|.xwd$| ) {
|
|
|
|
$gFileType = "xwd";
|
|
|
|
} elsif ( $gInFile =~ m|.pdb$| ) {
|
|
|
|
$gFileType = "pdb";
|
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
} # parseARGV
|
|
|
|
|
|
|
|
sub countSpecials($) {
|
2004-06-10 05:27:40 +02:00
|
|
|
my ( $facesRef ) = @_;
|
2004-06-08 10:13:17 +02:00
|
|
|
my $count = 0;
|
2004-06-10 05:27:40 +02:00
|
|
|
|
|
|
|
map { ++$count if ( ord($_) < 32 ); } @$facesRef;
|
2004-06-08 10:13:17 +02:00
|
|
|
return $count;
|
|
|
|
} # countSpecials
|
|
|
|
|
2004-06-10 05:27:40 +02:00
|
|
|
sub readXWDFaces($$$) {
|
2004-06-08 10:13:17 +02:00
|
|
|
my ( $fh, $facRef, $nSpecials ) = @_;
|
|
|
|
|
2010-12-01 03:35:11 +01:00
|
|
|
my ( $buf, $nRead, $nChars, $nBytes );
|
|
|
|
$nRead = sysread( $fh, $buf, 1 );
|
|
|
|
$nBytes = unpack( 'c', $buf );
|
|
|
|
printf STDERR "nBytes of faces: %d\n", $nBytes;
|
|
|
|
$nRead = sysread( $fh, $buf, 1 );
|
|
|
|
$nChars = unpack( 'c', $buf );
|
|
|
|
printf STDERR "nChars of faces: %d\n", $nChars;
|
|
|
|
|
|
|
|
binmode( $fh, ":encoding(utf8)" ) or die "binmode(:utf-8) failed\n";
|
|
|
|
sysread( $fh, $buf, $nChars );
|
|
|
|
length($buf) == $nChars or die "didn't read expected number of bytes\n";
|
|
|
|
binmode( $fh ) or die "binmode failed\n";
|
2004-06-08 10:13:17 +02:00
|
|
|
|
2010-12-01 03:35:11 +01:00
|
|
|
print STDERR "string now: $buf\n";
|
2004-06-08 10:13:17 +02:00
|
|
|
my @faces;
|
2010-12-01 03:35:11 +01:00
|
|
|
for ( my $ii = 0; $ii < $nChars; ++$ii ) {
|
|
|
|
my $chr = substr( $buf, $ii, 1 );
|
|
|
|
print STDERR "pushing $chr \n";
|
|
|
|
push( @faces, $chr );
|
2004-06-08 10:13:17 +02:00
|
|
|
}
|
|
|
|
|
2010-12-01 03:35:11 +01:00
|
|
|
printf STDERR "at 0x%x after reading faces\n", systell($fh);
|
|
|
|
|
2004-06-08 10:13:17 +02:00
|
|
|
${$nSpecials} = countSpecials( \@faces );
|
|
|
|
@{$facRef} = @faces;
|
2010-12-01 03:35:11 +01:00
|
|
|
printf STDERR "readXWDFaces=>%d\n", $nChars;
|
2004-06-08 10:13:17 +02:00
|
|
|
return $nChars;
|
2004-06-10 05:27:40 +02:00
|
|
|
} # readXWDFaces
|
2004-06-08 10:13:17 +02:00
|
|
|
|
2004-06-09 06:09:19 +02:00
|
|
|
sub skipBitmap($) {
|
2004-06-08 10:13:17 +02:00
|
|
|
my ( $fh ) = @_;
|
|
|
|
my $buf;
|
|
|
|
sysread( $fh, $buf, 1 );
|
|
|
|
my $nCols = unpack( 'C', $buf );
|
2004-06-09 06:09:19 +02:00
|
|
|
if ( $nCols > 0 ) {
|
|
|
|
sysread( $fh, $buf, 1 );
|
|
|
|
my $nRows = unpack( 'C', $buf );
|
|
|
|
my $nBytes = (($nRows * $nCols) + 7) / 8;
|
|
|
|
|
|
|
|
sysread( $fh, $buf, $nBytes );
|
|
|
|
}
|
2010-12-01 03:35:11 +01:00
|
|
|
printf STDERR "skipBitmap\n";
|
2004-06-09 06:09:19 +02:00
|
|
|
} # skipBitmap
|
2004-06-08 10:13:17 +02:00
|
|
|
|
|
|
|
sub getSpecials($$$) {
|
|
|
|
my ( $fh, $nSpecials, $specRef ) = @_;
|
|
|
|
|
|
|
|
my @specials;
|
|
|
|
for ( my $i = 0; $i < $nSpecials; ++$i ) {
|
|
|
|
my $buf;
|
|
|
|
sysread( $fh, $buf, 1 );
|
|
|
|
my $len = unpack( 'C', $buf );
|
|
|
|
sysread( $fh, $buf, $len );
|
|
|
|
push( @specials, $buf );
|
2004-06-09 06:09:19 +02:00
|
|
|
skipBitmap( $fh );
|
|
|
|
skipBitmap( $fh );
|
2004-06-08 10:13:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@{$specRef} = @specials;
|
|
|
|
} # getSpecials
|
|
|
|
|
2004-06-09 05:55:45 +02:00
|
|
|
sub readNodesToEnd($) {
|
|
|
|
my ( $fh ) = @_;
|
|
|
|
my @nodes;
|
|
|
|
my $count = 0;
|
|
|
|
my $offset = 4 - $gNodeSize;
|
|
|
|
my ( $buf, $nRead );
|
2004-06-08 10:13:17 +02:00
|
|
|
|
2004-06-09 05:55:45 +02:00
|
|
|
do {
|
|
|
|
$nRead = sysread( $fh, $buf, $gNodeSize, $offset );
|
|
|
|
$count += $nRead;
|
|
|
|
my $node = unpack( 'N', $buf );
|
|
|
|
push( @nodes, $node );
|
|
|
|
} while ( $nRead == $gNodeSize );
|
|
|
|
die "out of sync? nRead=$nRead, count=$count" if $nRead != 0;
|
|
|
|
|
|
|
|
return @nodes;
|
|
|
|
} # readNodesToEnd
|
2004-06-08 10:13:17 +02:00
|
|
|
|
2010-12-06 04:33:10 +01:00
|
|
|
sub nodeSizeFromFlags($$) {
|
|
|
|
my ( $fh, $flags ) = @_;
|
|
|
|
|
|
|
|
my $bitSet = $flags & 0x0008;
|
|
|
|
if ( 0 != $bitSet ){
|
|
|
|
$flags = $flags & ~0x0008;
|
|
|
|
# need to skip header
|
|
|
|
my $buf;
|
|
|
|
2 == sysread( $fh, $buf, 2 ) || die "couldn't read length of header";
|
|
|
|
my $len = unpack( "n", $buf );
|
|
|
|
$len == sysread( $fh, $buf, $len ) || die "couldn't read header bytes";
|
2010-12-06 16:23:22 +01:00
|
|
|
printf STDERR "skipped %d bytes of header\n", $len + 2;
|
2010-12-06 04:33:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $flags == 2 || $ flags == 4 ) {
|
2004-06-09 05:55:45 +02:00
|
|
|
return 3;
|
2010-12-01 03:35:11 +01:00
|
|
|
} elsif ( $flags == 5 ) {
|
2004-06-09 05:55:45 +02:00
|
|
|
return 4;
|
2004-06-08 10:13:17 +02:00
|
|
|
} else {
|
2004-06-10 05:27:40 +02:00
|
|
|
die "invalid dict flags $flags";
|
2004-06-08 10:13:17 +02:00
|
|
|
}
|
2004-06-09 05:55:45 +02:00
|
|
|
} # nodeSizeFromFlags
|
|
|
|
|
2004-06-09 06:09:19 +02:00
|
|
|
sub mergeSpecials($$) {
|
|
|
|
my ( $facesRef, $specialsRef ) = @_;
|
|
|
|
for ( my $i = 0; $i < @$facesRef; ++$i ) {
|
|
|
|
my $ref = ord($$facesRef[$i]);
|
|
|
|
if ( $ref < 32 ) {
|
|
|
|
$$facesRef[$i] = $$specialsRef[$ref];
|
2004-06-10 05:27:40 +02:00
|
|
|
#print STDERR "set $ref to $$specialsRef[$ref]\n";
|
2004-06-09 06:09:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-09 05:55:45 +02:00
|
|
|
sub prepXWD($$$$) {
|
2004-06-10 05:27:40 +02:00
|
|
|
my ( $fh, $facRef, $nodesRef, $startRef ) = @_;
|
2004-06-09 05:55:45 +02:00
|
|
|
|
2010-12-01 03:35:11 +01:00
|
|
|
printf STDERR "at 0x%x at start\n", systell($fh);
|
2004-06-09 05:55:45 +02:00
|
|
|
my $buf;
|
2004-06-10 05:27:40 +02:00
|
|
|
my $nRead = sysread( $fh, $buf, 2 );
|
2004-06-09 05:55:45 +02:00
|
|
|
my $flags = unpack( "n", $buf );
|
|
|
|
|
2010-12-06 04:33:10 +01:00
|
|
|
$gNodeSize = nodeSizeFromFlags( $fh, $flags );
|
2004-06-08 10:13:17 +02:00
|
|
|
|
|
|
|
my $nSpecials;
|
2004-06-10 05:27:40 +02:00
|
|
|
my $faceCount = readXWDFaces( $fh, $facRef, \$nSpecials );
|
2004-06-08 10:13:17 +02:00
|
|
|
|
2010-12-01 03:35:11 +01:00
|
|
|
printf STDERR "at 0x%x before header read\n", systell($fh);
|
2004-06-08 10:13:17 +02:00
|
|
|
# skip xloc header
|
2004-06-10 05:27:40 +02:00
|
|
|
$nRead = sysread( $fh, $buf, 2 );
|
2004-06-08 10:13:17 +02:00
|
|
|
|
|
|
|
# skip values info.
|
2010-12-01 03:35:11 +01:00
|
|
|
printf STDERR "at 0x%x before reading %d values\n", systell($fh), $faceCount;
|
2004-06-10 05:27:40 +02:00
|
|
|
sysread( $fh, $buf, $faceCount * 2 );
|
2010-12-01 03:35:11 +01:00
|
|
|
printf STDERR "at 0x%x after values read\n", systell($fh);
|
2004-06-08 10:13:17 +02:00
|
|
|
|
2010-12-01 03:35:11 +01:00
|
|
|
printf STDERR "at 0x%x before specials read\n", systell($fh);
|
2004-06-08 10:13:17 +02:00
|
|
|
my @specials;
|
2004-06-10 05:27:40 +02:00
|
|
|
getSpecials( $fh, $nSpecials, \@specials );
|
2004-06-09 06:09:19 +02:00
|
|
|
mergeSpecials( $facRef, \@specials );
|
2010-12-01 03:35:11 +01:00
|
|
|
printf STDERR "at 0x%x after specials read\n", systell($fh);
|
2004-06-08 10:13:17 +02:00
|
|
|
|
2010-12-01 03:35:11 +01:00
|
|
|
printf STDERR "at 0x%x before offset read\n", systell($fh);
|
2004-06-10 05:27:40 +02:00
|
|
|
sysread( $fh, $buf, 4 );
|
2004-06-08 10:13:17 +02:00
|
|
|
$$startRef = unpack( 'N', $buf );
|
2010-12-01 03:35:11 +01:00
|
|
|
print STDERR "startRef=$$startRef\n";
|
2004-06-08 10:13:17 +02:00
|
|
|
|
2004-06-10 05:27:40 +02:00
|
|
|
my @nodes = readNodesToEnd( $fh );
|
2004-06-08 10:13:17 +02:00
|
|
|
|
|
|
|
@$nodesRef = @nodes;
|
2010-12-01 03:35:11 +01:00
|
|
|
print STDERR "prepXWD done\n";
|
2004-06-08 10:13:17 +02:00
|
|
|
} # prepXWD
|
|
|
|
|
2004-06-10 05:27:40 +02:00
|
|
|
sub readPDBSpecials($$$$$) {
|
|
|
|
my ( $fh, $nChars, $nToRead, $nSpecials, $specRef ) = @_;
|
|
|
|
|
|
|
|
my ( $nRead, $buf );
|
|
|
|
|
|
|
|
# first skip counts and values, and xloc header
|
|
|
|
$nRead += sysread( $fh, $buf, ($nChars * 2) + 2 );
|
|
|
|
|
|
|
|
while ( $nSpecials-- ) {
|
|
|
|
$nRead += sysread( $fh, $buf, 8 ); # sizeof(Xloc_specialEntry)
|
|
|
|
my @chars = unpack( 'C8', $buf );
|
|
|
|
my $str;
|
|
|
|
foreach my $char (@chars) {
|
|
|
|
if ( $char == 0 ) { # null-terminated on palm
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
$str .= chr($char);
|
|
|
|
}
|
|
|
|
push( @$specRef, $str );
|
|
|
|
}
|
|
|
|
|
|
|
|
$nRead += sysread( $fh, $buf, $nToRead - $nRead ); # skip bitmaps
|
|
|
|
|
|
|
|
return $nRead;
|
|
|
|
} # readPDBSpecials
|
|
|
|
|
2004-06-09 05:55:45 +02:00
|
|
|
sub prepPDB($$$$) {
|
2004-06-10 05:27:40 +02:00
|
|
|
my ( $fh, $facRef, $nodesRef, $startRef ) = @_;
|
2004-06-09 05:55:45 +02:00
|
|
|
|
|
|
|
$$startRef = 0; # always for palm?
|
|
|
|
|
|
|
|
my $buf;
|
|
|
|
# skip header info
|
2004-06-10 05:27:40 +02:00
|
|
|
my $nRead = sysread( $fh, $buf, 76 );
|
|
|
|
$nRead += sysread( $fh, $buf, 2 );
|
2004-06-09 05:55:45 +02:00
|
|
|
my $nRecs = unpack( 'n', $buf );
|
|
|
|
|
|
|
|
my @offsets;
|
|
|
|
for ( my $i = 0; $i < $nRecs; ++$i ) {
|
2004-06-10 05:27:40 +02:00
|
|
|
$nRead += sysread( $fh, $buf, 4 );
|
2004-06-09 05:55:45 +02:00
|
|
|
push( @offsets, unpack( 'N', $buf ) );
|
2004-06-10 05:27:40 +02:00
|
|
|
$nRead += sysread( $fh, $buf, 4 ); # skip
|
2004-06-09 05:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
die "too far" if $nRead > $offsets[0];
|
|
|
|
while ( $nRead < $offsets[0] ) {
|
2004-06-10 05:27:40 +02:00
|
|
|
$nRead += sysread( $fh, $buf, 1 );
|
2004-06-09 05:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
my $facesOffset = $offsets[1];
|
|
|
|
my $nChars = ($offsets[2] - $facesOffset) / 2;
|
2004-06-10 05:27:40 +02:00
|
|
|
$nRead += sysread( $fh, $buf, $facesOffset - $nRead );
|
|
|
|
my @tmp = unpack( 'Nc6n', $buf );
|
2010-12-06 04:33:10 +01:00
|
|
|
$gNodeSize = nodeSizeFromFlags( 0, $tmp[7] );
|
2004-06-09 05:55:45 +02:00
|
|
|
|
|
|
|
my @faces;
|
|
|
|
for ( my $i = 0; $i < $nChars; ++$i ) {
|
2004-06-10 05:27:40 +02:00
|
|
|
$nRead += sysread( $fh, $buf, 2 );
|
2004-06-09 05:55:45 +02:00
|
|
|
push( @faces, chr(unpack( "n", $buf ) ) );
|
|
|
|
}
|
|
|
|
@{$facRef} = @faces;
|
|
|
|
|
|
|
|
die "out of sync: $nRead != $offsets[2]" if $nRead != $offsets[2];
|
|
|
|
|
2004-06-10 05:27:40 +02:00
|
|
|
my @specials;
|
|
|
|
$nRead += readPDBSpecials( $fh, $nChars, $offsets[3] - $nRead,
|
|
|
|
countSpecials($facRef), \@specials );
|
|
|
|
mergeSpecials( $facRef, \@specials );
|
2004-06-09 05:55:45 +02:00
|
|
|
|
|
|
|
die "out of sync" if $nRead != $offsets[3];
|
2004-06-10 05:27:40 +02:00
|
|
|
my @nodes = readNodesToEnd( $fh );
|
2004-06-09 05:55:45 +02:00
|
|
|
|
|
|
|
@$nodesRef = @nodes;
|
|
|
|
} # prepPDB
|
|
|
|
|
2004-06-08 10:13:17 +02:00
|
|
|
sub parseNode($$$$$) {
|
|
|
|
my ( $node, $chrIndex, $nextEdge, $accepting, $last ) = @_;
|
|
|
|
|
|
|
|
if ( $gNodeSize == 4 ) {
|
|
|
|
$$accepting = ($node & 0x00008000) != 0;
|
|
|
|
$$last = ($node & 0x00004000) != 0;
|
|
|
|
$$chrIndex = ($node & 0x00003f00) >> 8;
|
|
|
|
$$nextEdge = ($node >> 16) + (($node & 0x000000FF) << 16);
|
|
|
|
} elsif( $gNodeSize == 3 ) {
|
|
|
|
$$accepting = ($node & 0x00000080) != 0;
|
|
|
|
$$last = ($node & 0x00000040) != 0;
|
|
|
|
$$chrIndex = $node & 0x0000001f;
|
|
|
|
$$nextEdge = ($node >> 8) + (($node & 0x00000020) << 11);
|
|
|
|
}
|
|
|
|
|
|
|
|
# printf "%x: acpt=$$accepting; last=$$last; "
|
|
|
|
# . "next=$$nextEdge; ci=$$chrIndex\n", $node;
|
|
|
|
} # parseNode
|
|
|
|
|
2004-06-09 06:09:19 +02:00
|
|
|
sub printStr($$) {
|
|
|
|
my ( $strRef, $facesRef ) = @_;
|
2004-06-08 10:13:17 +02:00
|
|
|
|
2004-06-09 06:09:19 +02:00
|
|
|
print join( "", map {$$facesRef[$_]} @$strRef), "\n";
|
2004-06-08 10:13:17 +02:00
|
|
|
} # printStr
|
|
|
|
|
|
|
|
# Given an array of 4-byte nodes, a start index. and another array of
|
|
|
|
# two-byte faces, print out all of the words in the nodes array.
|
2004-06-10 05:27:40 +02:00
|
|
|
sub printDAWG($$$$) {
|
|
|
|
my ( $strRef, $arrRef, $start, $facesRef ) = @_;
|
2004-06-08 10:13:17 +02:00
|
|
|
|
2004-06-10 05:27:40 +02:00
|
|
|
die "infinite recursion???" if @$strRef > 15;
|
2004-06-08 10:13:17 +02:00
|
|
|
|
|
|
|
for ( ; ; ) {
|
|
|
|
my $node = $$arrRef[$start++];
|
|
|
|
my $nextEdge;
|
|
|
|
my $chrIndex;
|
|
|
|
my $accepting;
|
|
|
|
my $lastEdge;
|
|
|
|
|
|
|
|
parseNode( $node, \$chrIndex, \$nextEdge, \$accepting, \$lastEdge );
|
|
|
|
|
2004-06-10 05:27:40 +02:00
|
|
|
push( @$strRef, $chrIndex );
|
2004-06-09 05:55:45 +02:00
|
|
|
if ( $accepting ) {
|
2004-06-10 05:27:40 +02:00
|
|
|
printStr( $strRef, $facesRef );
|
2004-06-09 05:55:45 +02:00
|
|
|
}
|
2004-06-08 10:13:17 +02:00
|
|
|
|
|
|
|
if ( $nextEdge != 0 ) {
|
2004-06-10 05:27:40 +02:00
|
|
|
printDAWG( $strRef, $arrRef, $nextEdge, $facesRef );
|
2004-06-08 10:13:17 +02:00
|
|
|
}
|
|
|
|
|
2004-06-10 05:27:40 +02:00
|
|
|
pop( @$strRef );
|
2004-06-08 10:13:17 +02:00
|
|
|
|
|
|
|
if ( $lastEdge ) {
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
2004-06-10 05:27:40 +02:00
|
|
|
} # printDAWG
|
2004-06-08 10:13:17 +02:00
|
|
|
|
2004-10-15 06:01:22 +02:00
|
|
|
sub printNodes($$) {
|
|
|
|
my ( $nr, $fr ) = @_;
|
|
|
|
|
|
|
|
my $len = @$nr;
|
|
|
|
for ( my $i = 0; $i < $len; ++$i ) {
|
|
|
|
my $node = $$nr[$i];
|
|
|
|
|
|
|
|
my ( $chrIndex, $nextEdge, $accepting, $lastEdge );
|
|
|
|
parseNode( $node, \$chrIndex, \$nextEdge, \$accepting, \$lastEdge );
|
|
|
|
|
|
|
|
printf "%.8x: (%.8x) %2d(%s) %.8x ", $i, $node, $chrIndex,
|
|
|
|
$$fr[$chrIndex], $nextEdge;
|
|
|
|
print ($accepting? "A":"a");
|
|
|
|
print " ";
|
|
|
|
print ($lastEdge? "L":"l");
|
|
|
|
print "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-01 03:35:11 +01:00
|
|
|
sub printStartJson($) {
|
|
|
|
my ( $startIndex ) = @_;
|
|
|
|
printf( " start: 0x%.8x,\n", $startIndex );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub printCharsJson($) {
|
|
|
|
my ( $fr ) = @_;
|
|
|
|
print " chars: [ ";
|
|
|
|
foreach my $char (@$fr) {
|
|
|
|
print "\"$char\", "
|
|
|
|
}
|
|
|
|
print "],\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
sub printNodesJson($) {
|
|
|
|
my ( $nr ) = @_;
|
|
|
|
print " dawg: [\n";
|
|
|
|
|
|
|
|
my $len = @$nr;
|
|
|
|
my $newLine = 1;
|
|
|
|
for ( my $ii = 0; $ii < $len; ++$ii ) {
|
|
|
|
my $node = $$nr[$ii];
|
|
|
|
|
|
|
|
if ( $newLine ) {
|
|
|
|
printf( " /*%.6x*/ ", $ii );
|
|
|
|
$newLine = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf "0x%.8x, ", $node;
|
|
|
|
|
|
|
|
my ( $chrIndex, $nextEdge, $accepting, $lastEdge );
|
|
|
|
parseNode( $node, \$chrIndex, \$nextEdge, \$accepting, \$lastEdge );
|
|
|
|
if ( $lastEdge ) {
|
|
|
|
print "\n";
|
|
|
|
$newLine = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
print "\n ],\n"
|
|
|
|
}
|
|
|
|
|
2004-06-08 10:13:17 +02:00
|
|
|
#################################################################
|
|
|
|
# main
|
|
|
|
#################################################################
|
|
|
|
|
2010-12-01 03:35:11 +01:00
|
|
|
binmode( STDERR, ":encoding(utf8)" ) or die "binmode(:utf-8) failed\n";
|
2004-06-08 10:13:17 +02:00
|
|
|
|
2004-06-10 05:27:40 +02:00
|
|
|
parseARGV();
|
|
|
|
|
|
|
|
sysopen(INFILE, $gInFile, O_RDONLY) or die "couldn't open $gInFile: $!\n";;
|
|
|
|
binmode INFILE;
|
2004-06-08 10:13:17 +02:00
|
|
|
|
|
|
|
my @faces;
|
|
|
|
my @nodes;
|
|
|
|
my $startIndex;
|
|
|
|
|
|
|
|
if ( $gFileType eq "xwd" ){
|
2004-06-10 05:27:40 +02:00
|
|
|
prepXWD( *INFILE, \@faces, \@nodes, \$startIndex );
|
2004-06-08 10:13:17 +02:00
|
|
|
} elsif ( $gFileType eq "pdb" ) {
|
2004-06-10 05:27:40 +02:00
|
|
|
prepPDB( *INFILE, \@faces, \@nodes, \$startIndex );
|
2004-06-08 10:13:17 +02:00
|
|
|
}
|
2004-06-10 05:27:40 +02:00
|
|
|
close INFILE;
|
2004-06-08 10:13:17 +02:00
|
|
|
|
2004-06-10 05:27:40 +02:00
|
|
|
die "no nodes!!!" if 0 == @nodes;
|
2010-12-01 03:35:11 +01:00
|
|
|
|
2004-10-15 06:01:22 +02:00
|
|
|
if ( $gDoRaw ) {
|
|
|
|
printNodes( \@nodes, \@faces );
|
2010-12-01 03:35:11 +01:00
|
|
|
} elsif ( $gDoJSON ) {
|
|
|
|
print "dict = {\n";
|
|
|
|
printStartJson( $startIndex );
|
|
|
|
printCharsJson( \@faces );
|
|
|
|
printNodesJson( \@nodes );
|
|
|
|
print "}\n";
|
2004-10-15 06:01:22 +02:00
|
|
|
} else {
|
2010-12-01 03:35:11 +01:00
|
|
|
binmode( STDOUT, ":encoding(utf8)" ) or die "binmode(:utf-8) failed\n";
|
2004-10-15 06:01:22 +02:00
|
|
|
printDAWG( [], \@nodes, $startIndex, \@faces );
|
|
|
|
}
|
2004-06-09 06:09:19 +02:00
|
|
|
|
2004-06-10 05:27:40 +02:00
|
|
|
exit 0;
|