1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright (c) 1996, by Sun Microsystems, Inc.
24 * All rights reserved.
25 */
26
27 /*
28 * m_ord.c
29 *
30 * Copyright 1986, 1993 by Mortice Kern Systems Inc. All rights reserved.
31 *
32 * FUNCTIONS:
33 * m_ord(c)
34 * m_chr(i)
35 */
36
37 #ifdef M_RCSID
38 #ifndef lint
39 static char rcsID[] = "$Header: /rd/src/libc/mks/rcs/m_ord.c 1.13 1993/05/19 18:52:15 ant Exp $";
40 #endif
41 #endif
42
43 #include <mks.h>
44 #include <ctype.h>
45
46 /*
47 * Given an English alphabetic letter, return an ordinal number.
48 * If the character is not an English letter then return -1.
49 *
50 * This is most useful for handling the fact that POSIX.2 does not
51 * have a requirement that the English alpha letters be in sequential
52 * order in the character set. This would mean that the following is
53 * non-portable to non-ASCII machines:
54 *
55 * index = letter - 'a';
56 *
57 * The only solution possible was to have a mapping function to
58 * convert the alpha letters to their one (1) based ordinal value.
59 *
60 * index = m_ord(letter) - 1;
61 */
62
63 /*f
64 * m_ord(c) : convert character(case insensitive) to an an ordinal value.
65 * if c is an alphabetic character (A-Z,a-z), this returns
66 * a number between 1 and 26
67 */
68 int
m_ord(c)69 m_ord(c)
70 wint_t c;
71 {
72 /* note: this implementation is code set independent */
73 switch (towupper(c)) {
74 case 'A': return 1;
75 case 'B': return 2;
76 case 'C': return 3;
77 case 'D': return 4;
78 case 'E': return 5;
79 case 'F': return 6;
80 case 'G': return 7;
81 case 'H': return 8;
82 case 'I': return 9;
83 case 'J': return 10;
84 case 'K': return 11;
85 case 'L': return 12;
86 case 'M': return 13;
87 case 'N': return 14;
88 case 'O': return 15;
89 case 'P': return 16;
90 case 'Q': return 17;
91 case 'R': return 18;
92 case 'S': return 19;
93 case 'T': return 20;
94 case 'U': return 21;
95 case 'V': return 22;
96 case 'W': return 23;
97 case 'X': return 24;
98 case 'Y': return 25;
99 case 'Z': return 26;
100 default : return -1;
101 }
102 }
103
104 /*f
105 * m_chr(i) : convert an ordinal value to its corresponding character
106 * using the reverse mapping as m_ord().
107 * if i is a number between 1 and 26 it returns
108 * a character between A-Z
109 *
110 */
111 wint_t
m_chr(i)112 m_chr(i)
113 int i;
114 {
115 /* note: this implementation is code set independent */
116 switch (i) {
117 case 1: return 'A';
118 case 2: return 'B';
119 case 3: return 'C';
120 case 4: return 'D';
121 case 5: return 'E';
122 case 6: return 'F';
123 case 7: return 'G';
124 case 8: return 'H';
125 case 9: return 'I';
126 case 10: return 'J';
127 case 11: return 'K';
128 case 12: return 'L';
129 case 13: return 'M';
130 case 14: return 'N';
131 case 15: return 'O';
132 case 16: return 'P';
133 case 17: return 'Q';
134 case 18: return 'R';
135 case 19: return 'S';
136 case 20: return 'T';
137 case 21: return 'U';
138 case 22: return 'V';
139 case 23: return 'W';
140 case 24: return 'X';
141 case 25: return 'Y';
142 case 26: return 'Z';
143 default : return -1;
144 }
145 }
146
147 #ifdef TEST
main()148 main()
149 {
150 int A,Z,a,z;
151
152 A = m_ord('A');
153 Z = m_ord('Z');
154 a = m_ord('a');
155 z = m_ord('z');
156 printf("ord(A) = %d, ord(Z) = %d, m_chr(A) = '%c', m_chr(Z) = '%c'\n",
157 A, Z, m_chr(A), m_chr(Z));
158 printf("ord(a) = %d, ord(z) = %d, m_chr(a) = '%c', m_chr(z) = '%c'\n",
159 a, z, m_chr(a), m_chr(z));
160
161 printf("ord(0x100) = %d, ord(0) = %d\n", m_ord(0x100), m_ord(0));
162 printf("chr(0x100) = %d, chr(0) = %d\n", m_chr(0x100), m_chr(0));
163 }
164 #endif /*TEST*/
165