home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnurecod.zip / charname.c < prev    next >
C/C++ Source or Header  |  1993-12-22  |  2KB  |  91 lines

  1. /* Conversion of files between different charsets and usages.
  2.    Copyright (C) 1993 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, but
  11.    WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.    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.  
  20. #include "recode.h"
  21.  
  22. #include "charname.h"
  23.  
  24. /*--------------------------------------------------------------------.
  25. | Return a statically allocated full charname associated with a given |
  26. | SYMBOL, or NULL if not found.                          |
  27. `--------------------------------------------------------------------*/
  28.  
  29. char *
  30. symbol_to_charname (const char *symbol)
  31. {
  32.   static char result[MAX_CHARNAME_LENGTH + 1];
  33.   int first;
  34.   int last;
  35.   int middle;
  36.   int value;
  37.   const char *in;
  38.   char *out;
  39.   const char *cursor;
  40.  
  41.   /* Find the symbol by binary searching the charname table.  */
  42.  
  43.   first = 0;
  44.   last = NUMBER_OF_CHARNAMES;
  45.   while (first < last)
  46.     {
  47.       middle = (first + last) / 2;
  48.       value = strcmp (charname[middle].symbol, symbol);
  49.       if (value < 0)
  50.     first = middle + 1;
  51.       else if (value > 0)
  52.     last = middle;
  53.       else
  54.     break;
  55.     }
  56.  
  57.   /* If the symbol has not been found, return the NULL string.  */
  58.  
  59.   if (first >= last)
  60.     return NULL;
  61.  
  62.   /* Else, construct the resulting charname.  */
  63.  
  64.   out = NULL;
  65.   for (in = charname[middle].crypted; *in; in++)
  66.     {
  67.  
  68.       /* Decrypt the next word.  */
  69.  
  70.       value = *(const unsigned char *) in - 1;
  71.       if (value >= NUMBER_OF_SINGLES)
  72.     value = (NUMBER_OF_SINGLES + 255 * (value - NUMBER_OF_SINGLES)
  73.          + *(const unsigned char *) ++in - 1);
  74.  
  75.       /* Copy it.  */
  76.  
  77.       if (out)
  78.     *out++ = ' ';
  79.       else
  80.     out = result;
  81.  
  82.       for (cursor = word[value]; *cursor; cursor++)
  83.     *out++ = *cursor;
  84.     }
  85.  
  86.   /* Return the result.  */
  87.  
  88.   *out = '\0';
  89.   return result;
  90. }
  91.