base/include/rtai_nam2num.h

Go to the documentation of this file.
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 &copy; 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 /** 00038 * Convert a 6 characters string to an unsigned long. 00039 * 00040 * Converts a 6 characters string name containing an alpha numeric 00041 * identifier to its corresponding unsigned long identifier. 00042 * 00043 * @param name is the name to be converted. 00044 * 00045 * Allowed characters are: 00046 * - english letters (no difference between upper and lower case); 00047 * - decimal digits; 00048 * - underscore (_) and another character of your choice. The latter will be 00049 * always converted back to a $ by num2nam(). 00050 * 00051 * @return the unsigned long associated with @a name. 00052 */ 00053 static inline unsigned long nam2num (const char *name) 00054 { 00055 unsigned long retval = 0; 00056 int c, i; 00057 00058 for (i = 0; i < 6; i++) { 00059 if (!(c = name[i])) 00060 break; 00061 if (c >= 'a' && c <= 'z') { 00062 c += (11 - 'a'); 00063 } else if (c >= 'A' && c <= 'Z') { 00064 c += (11 - 'A'); 00065 } else if (c >= '0' && c <= '9') { 00066 c -= ('0' - 1); 00067 } else { 00068 c = c == '_' ? 37 : 38; 00069 } 00070 retval = retval*39 + c; 00071 } 00072 if (i > 0) 00073 return retval + 2; 00074 else 00075 return 0xFFFFFFFF; 00076 } 00077 00078 /** 00079 * Convert an unsigned long to a 6 characters string. 00080 * 00081 * @param num is the unsigned long identifier whose alphanumeric name string has 00082 * to be evaluated; 00083 * 00084 * @param name is a pointer to a 6 characters buffer where the identifier will 00085 * be returned. 00086 */ 00087 static inline void num2nam (unsigned long num, char *name) 00088 { 00089 int c, i, k, q; 00090 if (num == 0xFFFFFFFF) { 00091 name[0] = 0; 00092 return; 00093 } 00094 i = 5; 00095 num -= 2; 00096 while (num && i >= 0) { 00097 q = num/39; 00098 c = num - q*39; 00099 num = q; 00100 if ( c < 37) { 00101 name[i--] = c > 10 ? c + 'A' - 11 : c + '0' - 1; 00102 } else { 00103 name[i--] = c == 37 ? '_' : '$'; 00104 } 00105 } 00106 for (k = 0; i < 5; k++) { 00107 name[k] = name[++i]; 00108 } 00109 name[k] = 0; 00110 } 00111 00112 #endif /* !_RTAI_NAM2NUM_H */

Generated on Thu Nov 20 11:49:49 2008 for RTAI API by doxygen 1.3.8