00001 /** 00002 * @ingroup shm 00003 * @ingroup lxrt 00004 * @ingroup tasklets 00005 * @file 00006 * 00007 * Conversion between characters strings and unsigned long identifiers. 00008 * 00009 * Convert a 6 characters string to un unsigned long, and vice versa, to be used 00010 * as an identifier for RTAI services symmetrically available in user and kernel 00011 * space, e.g. @ref shm "shared memory" and @ref lxrt "LXRT and LXRT-INFORMED". 00012 * 00013 * @author Paolo Mantegazza 00014 * 00015 * @note Copyright © 1999-2003 Paolo Mantegazza <mantegazza@aero.polimi.it> 00016 * 00017 * This program is free software; you can redistribute it and/or 00018 * modify it under the terms of the GNU General Public License as 00019 * published by the Free Software Foundation; either version 2 of the 00020 * License, or (at your option) any later version. 00021 * 00022 * This program is distributed in the hope that it will be useful, 00023 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00025 * GNU General Public License for more details. 00026 * 00027 * You should have received a copy of the GNU General Public License 00028 * along with this program; if not, write to the Free Software 00029 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00030 */ 00031 00032 #ifndef _RTAI_NAM2NUM_H 00033 #define _RTAI_NAM2NUM_H 00034 00035 #include <rtai_types.h> 00036 00037 #ifdef __KERNEL__ 00038 00039 #define NAM2NUM_PROTO(type, name, arglist) static inline type name arglist 00040 00041 #else 00042 00043 #define NAM2NUM_PROTO RTAI_PROTO 00044 00045 #endif 00046 00047 /** 00048 * Convert a 6 characters string to an unsigned long. 00049 * 00050 * Converts a 6 characters string name containing an alpha numeric 00051 * identifier to its corresponding unsigned long identifier. 00052 * 00053 * @param name is the name to be converted. 00054 * 00055 * Allowed characters are: 00056 * - english letters (no difference between upper and lower case); 00057 * - decimal digits; 00058 * - underscore (_) and another character of your choice. The latter will be 00059 * always converted back to a $ by num2nam(). 00060 * 00061 * @return the unsigned long associated with @a name. 00062 */ 00063 NAM2NUM_PROTO(unsigned long, nam2num, (const char *name)) 00064 { 00065 unsigned long retval = 0; 00066 int c, i; 00067 00068 for (i = 0; i < 6; i++) { 00069 if (!(c = name[i])) 00070 break; 00071 if (c >= 'a' && c <= 'z') { 00072 c += (11 - 'a'); 00073 } else if (c >= 'A' && c <= 'Z') { 00074 c += (11 - 'A'); 00075 } else if (c >= '0' && c <= '9') { 00076 c -= ('0' - 1); 00077 } else { 00078 c = c == '_' ? 37 : 38; 00079 } 00080 retval = retval*39 + c; 00081 } 00082 if (i > 0) 00083 return retval + 2; 00084 else 00085 return 0xFFFFFFFF; 00086 } 00087 00088 /** 00089 * Convert an unsigned long to a 6 characters string. 00090 * 00091 * @param num is the unsigned long identifier whose alphanumeric name string has 00092 * to be evaluated; 00093 * 00094 * @param name is a pointer to a 6 characters buffer where the identifier will 00095 * be returned. 00096 */ 00097 NAM2NUM_PROTO(void, num2nam, (unsigned long num, char *name)) 00098 { 00099 int c, i, k, q; 00100 if (num == 0xFFFFFFFF) { 00101 name[0] = 0; 00102 return; 00103 } 00104 i = 5; 00105 num -= 2; 00106 while (num && i >= 0) { 00107 q = num/39; 00108 c = num - q*39; 00109 num = q; 00110 if ( c < 37) { 00111 name[i--] = c > 10 ? c + 'A' - 11 : c + '0' - 1; 00112 } else { 00113 name[i--] = c == 37 ? '_' : '$'; 00114 } 00115 } 00116 for (k = 0; i < 5; k++) { 00117 name[k] = name[++i]; 00118 } 00119 name[k] = 0; 00120 } 00121 00122 #endif /* !_RTAI_NAM2NUM_H */