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