File: charname.pl

package info (click to toggle)
recode 3.4.1-11
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,560 kB
  • ctags: 622
  • sloc: ansic: 10,572; perl: 339; makefile: 317; lisp: 243; sh: 173; lex: 165; awk: 127; sed: 10
file content (202 lines) | stat: -rw-r--r-- 5,088 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# Automatically derive charname.h from rfc1345.txt.
# Copyright (C) 1993, 1994 Free Software Foundation, Inc.
# Francois Pinard <pinard@iro.umontreal.ca>, 1993.

# 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA.

open (HDR, ">charname.h");

print HDR <<END_OF_TEXT;
/* DO NOT MODIFY THIS FILE!  It was generated by "charname.pl".  */

/* Conversion of files between different charsets and usages.
   Copyright (C) 1990, 1993 Free Software Foundation, Inc.
   Francois Pinard <pinard\@iro.umontreal.ca>, 1993.

   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, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
END_OF_TEXT

# Save a few definitions added after or independantly of RFC 1345.

$charname{"f2"} = "florin";
$max_length = 2;
$code{"florin"}++;

# Read the character comments.  Count words in charnames.

print STDERR "Reading...";

$_ = <>;
while ($_)
{
    chop;

    # Look ahead one line and merge it if it should.

    $next = <>;
    if ($next =~ /^              ( .*)/)
    {
	$_ .= $1;
	$next = <>;
    }

    # Separate fields and save needed information.

    if (/([^ ]+) +[0-9a-f]+ +(.*)/)
    {
	$charname{$1} = $2;
	if (length ($2) > $max_length)
	{
	    $max_length = length ($2);
	}
	foreach $word (split (/ /, $2))
	{
	    $code{$word}++;
	}
    }
    elsif (!/ +e000/)
    {
	print "What about <<", $_, ">>?\n";
    }

    # Prepare for next line.

    $_ = $next;
}

# Establish a mild compression scheme.  Words @word[0] to
# @word[$singles-1] will be represented by a single byte running from
# 1 to $singles.  All remaining words will be represented by two
# bytes, the first one running slowly from $singles+1 to 255, the
# second cycling faster from 1 to 255.

print STDERR "Sorting words...";

@word = sort descending keys %code;
$count = 0 + @word;
$singles = int ((255 * 255 - $count) / 254);

# Transmit a few values for further usage by the C code.

print STDERR "and charnames...";

@symbol = sort keys %charname;

printf HDR "\n#define NUMBER_OF_SINGLES %d\n", $singles;
printf HDR "\n#define MAX_CHARNAME_LENGTH %d\n", $max_length;
printf HDR "\n#define NUMBER_OF_CHARNAMES %d\n", (0 + @symbol);

# Establish a mild compression scheme (one or two bytes per word).

print STDERR "Writing words...";

print HDR "\n";
print HDR "static const char *const word[$count] =\n";
print HDR "  {\n";

$char1 = 1;
$char2 = 1;

for ($counter = 0; $counter < $singles; $counter++)
{
    $word = $word[$counter];
    $word =~ tr/A-Z/a-z/;
    printf HDR "    %-28s/* %0.3o */\n", "\"$word\",", $char1;
    $code{$word[$counter]} = $char1;
    $char1++;
}

for (; $counter < $count; $counter++)
{
    $word = $word[$counter];
    $word =~ tr/A-Z/a-z/;
    printf HDR "    %-28s/* %0.3o %0.3o */\n", "\"$word\",", $char1, $char2;
    $code{$word[$counter]} = 256 * $char1 + $char2;
    if ($char2 == 255)
    {
	$char1++;
	$char2 = 1;
    }
    else
    {
	$char2++;
    }
}
print HDR "  };\n";

# Print compressed charnames for all characters.

print STDERR "and charnames...";

print HDR "\n";
print HDR "struct charname\n";
print HDR "  {\n";
print HDR "    const char *symbol;\n";
print HDR "    const char *crypted;\n";
print HDR "  };\n";

print HDR "\n";
print HDR "static const struct charname charname[NUMBER_OF_CHARNAMES] =\n";
print HDR "  {\n";

foreach $symbol (@symbol)
{
    $string = $symbol;
    $string =~ s/([\"])/\\\1/g;
    print HDR "    {\"$string\", \"";
    foreach $word (split (' ', $charname{$symbol}))
    {
	$code = $code{$word};
	if ($code < 256)
	{
	    printf HDR "\\%0.3o", $code;
	}
	else
	{
	    printf HDR "\\%0.3o\\%0.3o", int ($code / 256), $code % 256;
	}
    }
    print HDR "\"},\n";
}

print HDR "  };\n";

print STDERR "done\n";

close HDR;
exit 0;

# Comparison routine for descending frequency sort.

sub descending
{
    local ($result);

    $result = $code{$b} - $code{$a};
    $result == 0 ? $a cmp $b : $result;
}