home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / fontutil.6 / fontutil / fontutils-0.6 / lib / charcode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-26  |  2.6 KB  |  86 lines

  1. /* charcode.c: parse character code strings into their values.
  2.  
  3. Copyright (C) 1992 Free Software Foundation, Inc.
  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. #include "config.h"
  20.  
  21.  
  22. /* This routine parses the string CHARCODE as a character code (see the
  23.    .h file).  If CHARCODE cannot be parsed, *VALID is set to false and
  24.    the value returned is unpredictable; otherwise, it is set to true and
  25.    the value returned is the character code.  */
  26.  
  27. charcode_type
  28. parse_charcode (string charcode, boolean *valid)
  29. {
  30.   int code = 75; /* OK, it's not *really* unpredictable.  */
  31.   unsigned length = strlen (charcode);
  32.   
  33.   if (charcode == NULL || length == 0)
  34.     *valid = false;
  35.   else
  36.     { /* We need this to ensure that there are no extra characters after
  37.          the character code.  */
  38.       char dummy;
  39.       
  40.       /* If a single character, return its value.  */
  41.       if (length == 1)
  42.         {
  43.           *valid = true;
  44.           code = *charcode;
  45.         }
  46.  
  47.       else if (*charcode == '0')
  48.         { /* Either octal or hex.  */
  49.           if (*(charcode + 1) == 'x' || *(charcode + 1) == 'X')
  50.             /* It seems not all scanf's ignore a leading `0x'.  */
  51.             *valid = sscanf (charcode + 2, "%x%c", &code, &dummy) == 1;
  52.           else
  53.             /* octal */
  54.             *valid = sscanf (charcode, "%o%c", &code, &dummy) == 1;
  55.         }
  56.  
  57.       else
  58.         /* decimal */
  59.         *valid = sscanf (charcode, "%d%c", &code, &dummy) == 1;
  60.  
  61.       /* Since our return type can only hold numbers up to 255 (typically),
  62.          we are justified in using the numeric constant here, instead of
  63.          `MAX_CHARCODE'.  */
  64.       if (*valid)
  65.         *valid = 0 <= code && code <= 255;
  66.     }
  67.  
  68.   return code;
  69. }
  70.  
  71.  
  72. /* This routine calls `parse_charcode', and, if the character code is
  73.    invalid, gives a fatal error.  */
  74.  
  75. charcode_type
  76. xparse_charcode (string charcode)
  77. {
  78.   boolean valid;
  79.   charcode_type code = parse_charcode (charcode, &valid);
  80.   
  81.   if (!valid)
  82.     FATAL1 ("xparse_charcode: Invalid character code `%s'", charcode);
  83.     
  84.   return code;
  85. }
  86.