summaryrefslogtreecommitdiff
path: root/lib/charcode.c
blob: 86158b5765b8bd26fa605910a7e580f865e5aa56 (plain)
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
/* charcode.c: parse character code strings into their values.

Copyright (C) 1992, 2011 Free Software Foundation, Inc.

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 3, 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.  */

#include "config.h"


/* This routine parses the string CHARCODE as a character code (see the
   .h file).  If CHARCODE cannot be parsed, *VALID is set to false and
   the value returned is unpredictable; otherwise, it is set to true and
   the value returned is the character code.  */

charcode_type
parse_charcode (string charcode, boolean *valid)
{
  int code = 75; /* OK, it's not *really* unpredictable.  */
  unsigned length = strlen (charcode);
  
  if (charcode == NULL || length == 0)
    *valid = false;
  else
    { /* We need this to ensure that there are no extra characters after
         the character code.  */
      char dummy;
      
      /* If a single character, return its value.  */
      if (length == 1)
        {
          *valid = true;
          code = *charcode;
        }

      else if (*charcode == '0')
        { /* Either octal or hex.  */
          if (*(charcode + 1) == 'x' || *(charcode + 1) == 'X')
            /* It seems not all scanf's ignore a leading `0x'.  */
            *valid = sscanf (charcode + 2, "%x%c", &code, &dummy) == 1;
          else
            /* octal */
            *valid = sscanf (charcode, "%o%c", &code, &dummy) == 1;
        }

      else
        /* decimal */
        *valid = sscanf (charcode, "%d%c", &code, &dummy) == 1;

      /* Since our return type can only hold numbers up to 255 (typically),
         we are justified in using the numeric constant here, instead of
         `MAX_CHARCODE'.  */
      if (*valid)
        *valid = 0 <= code && code <= 255;
    }

  return code;
}


/* This routine calls `parse_charcode', and, if the character code is
   invalid, gives a fatal error.  */

charcode_type
xparse_charcode (string charcode)
{
  boolean valid;
  charcode_type code = parse_charcode (charcode, &valid);
  
  if (!valid)
    FATAL1 ("xparse_charcode: Invalid character code `%s'", charcode);
    
  return code;
}