ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-Knuddels/util/dic2bin
Revision: 1.5
Committed: Mon Jul 21 04:00:41 2008 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.4: +8 -3 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #!/opt/bin/perl
2    
3 root 1.2 # utility to convert the extracted dictionary to a perl hash for further use
4    
5 root 1.1 # use jcf-dump GroupChat.java and extract the longest constant string sans '"'
6 root 1.5 # (or jad GroupChat.java)
7 root 1.1
8     use Encode;
9     use Data::Dumper;
10    
11     local $/;
12    
13     binmode STDIN;
14     binmode STDOUT, ":utf-8";
15    
16     my $dictionary = <>;
17    
18     $dictionary =~ s%
19     \\
20     (?: \\
21 root 1.5 #| 0([0-9a-f]{2}) # jcf
22     | [0-7]{3} # jad
23 root 1.1 | u([0-9a-f]{4})
24     | ([rnt\x27\x22])
25     | (.)
26     )
27     %
28 root 1.5 if ($1) {
29     # chr oct "$2"; # jcf
30     chr oct "$2"; # jad
31     } elsif ($2) {
32     chr hex "$2";
33 root 1.1 } elsif ($3 eq "r") { "\015"
34     } elsif ($3 eq "n") { "\012"
35     } elsif ($3 eq "t") { "\011"
36     } elsif ($3 eq "'") { "'"
37     } elsif ($3 eq '"') { '"'
38     } elsif ($4) {
39     die "<<<$4>>>\n";
40     } else {
41     "\\";
42     }
43     %sgex;
44    
45     my ($idx, $bit) = (0, -32);
46    
47     while (length $dictionary) {
48     my $prefix = ord substr $dictionary, 0, 1, "";
49     $prefix = unpack "n", Encode::encode "iso-8859-1", substr $dictionary, 0, 2, "" if $prefix == 255;
50    
51     my ($len, $width) = (int $prefix / 21, $prefix % 21);
52     my $string = substr $dictionary, 0, $len + 1, "";
53    
54     # carry
55     while ($idx & 1) {
56     $idx >>= 1;
57     $bit--;
58     }
59    
60     $idx |= 1;
61     # widen
62     while ($bit < $width) {
63     $idx &= 0x7fffffff;
64     $idx <<= 1;
65     $bit++;
66     }
67    
68     my $code = reverse unpack "b$width", pack "V", $idx;
69     #$string =~ s/([^\x20-\x7e])/sprintf "\\x{%x}", ord $1/ge;
70    
71     $code{$code} = $string;
72     }
73    
74 root 1.4 "" eq delete $code{"0000000000"}
75     or die "FATAL";
76    
77 root 1.3 print "package Net::Knuddels::Dictionary;\n",
78     "\n",
79     "\$Net::Knuddels::Dictionary = ";
80    
81     print Data::Dumper->new([\%code])->Terse(1)->Useqq(0)->Quotekeys(1)->Sortkeys(0)->Dump;
82    
83     print ";\n1;\n";
84 root 1.1