home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / GNU / recode-3.4-MIHS / src / charname.pl < prev    next >
Encoding:
Text File  |  1994-01-06  |  5.0 KB  |  203 lines

  1. # Automatically derive charname.h from rfc1345.txt.
  2. # Copyright (C) 1993, 1994 Free Software Foundation, Inc.
  3. # Francois Pinard <pinard@iro.umontreal.ca>, 1993.
  4.  
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2, or (at your option)
  8. # any later version.
  9.  
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14.  
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. open (HDR, ">charname.h");
  20.  
  21. print HDR <<END_OF_TEXT;
  22. /* DO NOT MODIFY THIS FILE!  It was generated by "charname.pl".  */
  23.  
  24. /* Conversion of files between different charsets and usages.
  25.    Copyright (C) 1990, 1993 Free Software Foundation, Inc.
  26.    Francois Pinard <pinard@iro.umontreal.ca>, 1993.
  27.  
  28.    This program is free software; you can redistribute it and/or modify
  29.    it under the terms of the GNU General Public License as published by
  30.    the Free Software Foundation; either version 2, or (at your option)
  31.    any later version.
  32.  
  33.    This program is distributed in the hope that it will be useful, but
  34.    WITHOUT ANY WARRANTY; without even the implied warranty of
  35.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  36.    General Public License for more details.
  37.  
  38.    You should have received a copy of the GNU General Public License
  39.    along with this program; if not, write to the Free Software
  40.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  41. */
  42. END_OF_TEXT
  43.  
  44. # Save a few definitions added after or independantly of RFC 1345.
  45.  
  46. $charname{"f2"} = "florin";
  47. $max_length = 2;
  48. $code{"florin"}++;
  49.  
  50. # Read the character comments.  Count words in charnames.
  51.  
  52. print STDERR "Reading...";
  53.  
  54. $_ = <>;
  55. while ($_)
  56. {
  57.     chop;
  58.  
  59.     # Look ahead one line and merge it if it should.
  60.  
  61.     $next = <>;
  62.     if ($next =~ /^              ( .*)/)
  63.     {
  64.     $_ .= $1;
  65.     $next = <>;
  66.     }
  67.  
  68.     # Separate fields and save needed information.
  69.  
  70.     if (/([^ ]+) +[0-9a-f]+ +(.*)/)
  71.     {
  72.     $charname{$1} = $2;
  73.     if (length ($2) > $max_length)
  74.     {
  75.         $max_length = length ($2);
  76.     }
  77.     foreach $word (split (/ /, $2))
  78.     {
  79.         $code{$word}++;
  80.     }
  81.     }
  82.     elsif (!/ +e000/)
  83.     {
  84.     print "What about <<", $_, ">>?\n";
  85.     }
  86.  
  87.     # Prepare for next line.
  88.  
  89.     $_ = $next;
  90. }
  91.  
  92. # Establish a mild compression scheme.  Words @word[0] to
  93. # @word[$singles-1] will be represented by a single byte running from
  94. # 1 to $singles.  All remaining words will be represented by two
  95. # bytes, the first one running slowly from $singles+1 to 255, the
  96. # second cycling faster from 1 to 255.
  97.  
  98. print STDERR "Sorting words...";
  99.  
  100. @word = sort descending keys %code;
  101. $count = 0 + @word;
  102. $singles = int ((255 * 255 - $count) / 254);
  103.  
  104. # Transmit a few values for further usage by the C code.
  105.  
  106. print STDERR "and charnames...";
  107.  
  108. @symbol = sort keys %charname;
  109.  
  110. printf HDR "\n#define NUMBER_OF_SINGLES %d\n", $singles;
  111. printf HDR "\n#define MAX_CHARNAME_LENGTH %d\n", $max_length;
  112. printf HDR "\n#define NUMBER_OF_CHARNAMES %d\n", (0 + @symbol);
  113.  
  114. # Establish a mild compression scheme (one or two bytes per word).
  115.  
  116. print STDERR "Writing words...";
  117.  
  118. print HDR "\n";
  119. print HDR "static const char *const word[$count] =\n";
  120. print HDR "  {\n";
  121.  
  122. $char1 = 1;
  123. $char2 = 1;
  124.  
  125. for ($counter = 0; $counter < $singles; $counter++)
  126. {
  127.     $word = $word[$counter];
  128.     $word =~ tr/A-Z/a-z/;
  129.     printf HDR "    %-28s/* %0.3o */\n", "\"$word\",", $char1;
  130.     $code{$word[$counter]} = $char1;
  131.     $char1++;
  132. }
  133.  
  134. for (; $counter < $count; $counter++)
  135. {
  136.     $word = $word[$counter];
  137.     $word =~ tr/A-Z/a-z/;
  138.     printf HDR "    %-28s/* %0.3o %0.3o */\n", "\"$word\",", $char1, $char2;
  139.     $code{$word[$counter]} = 256 * $char1 + $char2;
  140.     if ($char2 == 255)
  141.     {
  142.     $char1++;
  143.     $char2 = 1;
  144.     }
  145.     else
  146.     {
  147.     $char2++;
  148.     }
  149. }
  150. print HDR "  };\n";
  151.  
  152. # Print compressed charnames for all characters.
  153.  
  154. print STDERR "and charnames...";
  155.  
  156. print HDR "\n";
  157. print HDR "struct charname\n";
  158. print HDR "  {\n";
  159. print HDR "    const char *symbol;\n";
  160. print HDR "    const char *crypted;\n";
  161. print HDR "  };\n";
  162.  
  163. print HDR "\n";
  164. print HDR "static const struct charname charname[NUMBER_OF_CHARNAMES] =\n";
  165. print HDR "  {\n";
  166.  
  167. foreach $symbol (@symbol)
  168. {
  169.     $string = $symbol;
  170.     $string =~ s/([\"])/\\\1/g;
  171.     print HDR "    {\"$string\", \"";
  172.     foreach $word (split (' ', $charname{$symbol}))
  173.     {
  174.     $code = $code{$word};
  175.     if ($code < 256)
  176.     {
  177.         printf HDR "\\%0.3o", $code;
  178.     }
  179.     else
  180.     {
  181.         printf HDR "\\%0.3o\\%0.3o", int ($code / 256), $code % 256;
  182.     }
  183.     }
  184.     print HDR "\"},\n";
  185. }
  186.  
  187. print HDR "  };\n";
  188.  
  189. print STDERR "done\n";
  190.  
  191. close HDR;
  192. exit 0;
  193.  
  194. # Comparison routine for descending frequency sort.
  195.  
  196. sub descending
  197. {
  198.     local ($result);
  199.  
  200.     $result = $code{$b} - $code{$a};
  201.     $result == 0 ? $a cmp $b : $result;
  202. }
  203.