1 /******************************************************************************
2 ** Filename: name2char.c
3 ** Purpose: Routines to convert between classes and class names.
4 ** Author: Dan Johnson
5 ** History: Fri Feb 23 08:03:09 1990, DSJ, Created.
6 **
7 ** Licensed under the Apache License, Version 2.0 (the "License");
8 ** you may not use this file except in compliance with the License.
9 ** You may obtain a copy of the License at
10 ** http://www.apache.org/licenses/LICENSE-2.0
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 ******************************************************************************/
17 /**----------------------------------------------------------------------------
18 Include Files and Type Defines
19 ----------------------------------------------------------------------------**/
20 #include "name2char.h"
21 #include "matchdefs.h"
22 #include "danerror.h"
23 #include <string.h>
24
25 #define ILLEGALCHARNAME 6001
26
27 /**----------------------------------------------------------------------------
28 Global Data Definitions and Declarations
29 ----------------------------------------------------------------------------**/
30 /* character ID (ascii code) to character name mapping */
31 static const char *NameList[]={
32 "!bang",
33 "\"doubleq",
34 "#hash",
35 "$dollar",
36 "%percent",
37 "&and",
38 "'quote",
39 "(lround",
40 ")rround",
41 "*asterisk",
42 "+plus",
43 ",comma",
44 "-minus",
45 ".dot",
46 "/slash",
47 ":colon",
48 ";semic",
49 "<less",
50 "=equal",
51 ">greater",
52 "?question",
53 "@at",
54 "[lsquare",
55 "\\backsl",
56 "]rsquare",
57 "^uparr",
58 "_unders",
59 "`grave",
60 "{lbrace",
61 "|bar",
62 "}rbrace",
63 "~tilde",
64 "AcA",
65 "BcB",
66 "CcC",
67 "DcD",
68 "EcE",
69 "FcF",
70 "GcG",
71 "HcH",
72 "IcI",
73 "JcJ",
74 "KcK",
75 "LcL",
76 "McM",
77 "NcN",
78 "OcO",
79 "PcP",
80 "QcQ",
81 "RcR",
82 "ScS",
83 "TcT",
84 "UcU",
85 "VcV",
86 "WcW",
87 "XcX",
88 "YcY",
89 "ZcZ",
90 NULL
91 };
92
93
94 /**----------------------------------------------------------------------------
95 Public Code
96 ----------------------------------------------------------------------------**/
97 /*---------------------------------------------------------------------------*/
NameToChar(char CharName[])98 CLASS_ID NameToChar (
99 char CharName[])
100
101 /*
102 ** Parameters:
103 ** CharName character name to convert to a character
104 ** Globals:
105 ** NameList lookup table for name to char mapping
106 ** Operation:
107 ** This routine converts the specified character name to
108 ** an ascii character.
109 ** Return: Ascii character that corresponds to the character name.
110 ** Exceptions: ILLEGALCHARNAME
111 ** History: Sat Aug 26 12:26:54 1989, DSJ, Created.
112 */
113
114 {
115 int i;
116
117 // look for name in table and return character if found
118 for ( i = 0; NameList[i] != NULL; i++ )
119 if ( strcmp (CharName, &NameList[i][1]) == 0)
120 return (NameList[i][0]);
121 if ( strlen (CharName) == 1 )
122 return (CharName[0]); //name is not in table but is a single character
123 else //illegal character
124 {
125 DoError (ILLEGALCHARNAME, "Illegal character name");
126 return 0;
127 }
128 } /* NameToChar */
129
130 /*---------------------------------------------------------------------------*/
CharToName(CLASS_ID Char,char CharName[])131 void CharToName (
132 CLASS_ID Char,
133 char CharName[])
134
135 /*
136 ** Parameters:
137 ** Char character to map to a character name
138 ** CharName string to copy character name into
139 ** Globals:
140 ** NameList lookup table for char to name mapping
141 ** Operation:
142 ** This routine converts the specified ascii character to a
143 ** character name. This is convenient for representing
144 ** characters which might have special meaning to operating
145 ** system shells or other programs (e.g. "*?&><" etc.).
146 ** Return: none
147 ** Exceptions: none
148 ** History: Sat Aug 26 12:51:02 1989, DSJ, Created.
149 */
150
151 {
152 int i;
153
154 /* look for character in table and return a copy of its name if found */
155 for ( i = 0; NameList[i] != NULL; i++ )
156 if ( Char == NameList[i][0] )
157 {
158 strcpy ( CharName, &NameList[i][1] );
159 return;
160 }
161
162 /* if the character is not in the table, then use it as the name */
163 CharName[0] = Char;
164 CharName[1] = 0;
165
166 } /* CharToName */
167