00001 00002 /*-------------------------------------------------------------------------*/ 00003 /** 00004 @file strlib.c 00005 @author N. Devillard 00006 @date Jan 2001 00007 @version $Revision: 1.8 $ 00008 @brief Various string handling routines to complement the C lib. 00009 00010 This modules adds a few complementary string routines usually missing 00011 in the standard C library. 00012 */ 00013 /*--------------------------------------------------------------------------*/ 00014 00015 /* 00016 $Id: strlib.c,v 1.8 2002/12/12 10:29:16 ndevilla Exp $ 00017 $Author: ndevilla $ 00018 $Date: 2002/12/12 10:29:16 $ 00019 $Revision: 1.8 $ 00020 */ 00021 00022 /*--------------------------------------------------------------------------- 00023 Includes 00024 ---------------------------------------------------------------------------*/ 00025 00026 #include <string.h> 00027 #include <ctype.h> 00028 00029 #include "strlib.h" 00030 00031 /*--------------------------------------------------------------------------- 00032 Defines 00033 ---------------------------------------------------------------------------*/ 00034 #define ASCIILINESZ 1024 00035 00036 /*--------------------------------------------------------------------------- 00037 Function codes 00038 ---------------------------------------------------------------------------*/ 00039 00040 00041 /*-------------------------------------------------------------------------*/ 00042 /** 00043 @brief Convert a string to lowercase. 00044 @param s String to convert. 00045 @return ptr to statically allocated string. 00046 00047 This function returns a pointer to a statically allocated string 00048 containing a lowercased version of the input string. Do not free 00049 or modify the returned string! Since the returned string is statically 00050 allocated, it will be modified at each function call (not re-entrant). 00051 */ 00052 /*--------------------------------------------------------------------------*/ 00053 00054 char * strlwc(const char * s) 00055 { 00056 static char l[ASCIILINESZ+1]; 00057 int i ; 00058 00059 if (s==NULL) return NULL ; 00060 memset(l, 0, ASCIILINESZ+1); 00061 i=0 ; 00062 while (s[i] && i<ASCIILINESZ) { 00063 l[i] = (char)tolower((int)s[i]); 00064 i++ ; 00065 } 00066 l[ASCIILINESZ]=(char)0; 00067 return l ; 00068 } 00069 00070 00071 00072 /*-------------------------------------------------------------------------*/ 00073 /** 00074 @brief Convert a string to uppercase. 00075 @param s String to convert. 00076 @return ptr to statically allocated string. 00077 00078 This function returns a pointer to a statically allocated string 00079 containing an uppercased version of the input string. Do not free 00080 or modify the returned string! Since the returned string is statically 00081 allocated, it will be modified at each function call (not re-entrant). 00082 */ 00083 /*--------------------------------------------------------------------------*/ 00084 00085 char * strupc(char * s) 00086 { 00087 static char l[ASCIILINESZ+1]; 00088 int i ; 00089 00090 if (s==NULL) return NULL ; 00091 memset(l, 0, ASCIILINESZ+1); 00092 i=0 ; 00093 while (s[i] && i<ASCIILINESZ) { 00094 l[i] = (char)toupper((int)s[i]); 00095 i++ ; 00096 } 00097 l[ASCIILINESZ]=(char)0; 00098 return l ; 00099 } 00100 00101 00102 00103 /*-------------------------------------------------------------------------*/ 00104 /** 00105 @brief Skip blanks until the first non-blank character. 00106 @param s String to parse. 00107 @return Pointer to char inside given string. 00108 00109 This function returns a pointer to the first non-blank character in the 00110 given string. 00111 */ 00112 /*--------------------------------------------------------------------------*/ 00113 00114 char * strskp(char * s) 00115 { 00116 char * skip = s; 00117 if (s==NULL) return NULL ; 00118 while (isspace((int)*skip) && *skip) skip++; 00119 return skip ; 00120 } 00121 00122 00123 00124 /*-------------------------------------------------------------------------*/ 00125 /** 00126 @brief Remove blanks at the end of a string. 00127 @param s String to parse. 00128 @return ptr to statically allocated string. 00129 00130 This function returns a pointer to a statically allocated string, 00131 which is identical to the input string, except that all blank 00132 characters at the end of the string have been removed. 00133 Do not free or modify the returned string! Since the returned string 00134 is statically allocated, it will be modified at each function call 00135 (not re-entrant). 00136 */ 00137 /*--------------------------------------------------------------------------*/ 00138 00139 char * strcrop(char * s) 00140 { 00141 static char l[ASCIILINESZ+1]; 00142 char * last ; 00143 00144 if (s==NULL) return NULL ; 00145 memset(l, 0, ASCIILINESZ+1); 00146 strcpy(l, s); 00147 last = l + strlen(l); 00148 while (last > l) { 00149 if (!isspace((int)*(last-1))) 00150 break ; 00151 last -- ; 00152 } 00153 *last = (char)0; 00154 return l ; 00155 } 00156 00157 00158 00159 /*-------------------------------------------------------------------------*/ 00160 /** 00161 @brief Remove blanks at the beginning and the end of a string. 00162 @param s String to parse. 00163 @return ptr to statically allocated string. 00164 00165 This function returns a pointer to a statically allocated string, 00166 which is identical to the input string, except that all blank 00167 characters at the end and the beg. of the string have been removed. 00168 Do not free or modify the returned string! Since the returned string 00169 is statically allocated, it will be modified at each function call 00170 (not re-entrant). 00171 */ 00172 /*--------------------------------------------------------------------------*/ 00173 char * strstrip(char * s) 00174 { 00175 static char l[ASCIILINESZ+1]; 00176 char * last ; 00177 00178 if (s==NULL) return NULL ; 00179 00180 while (isspace((int)*s) && *s) s++; 00181 00182 memset(l, 0, ASCIILINESZ+1); 00183 strcpy(l, s); 00184 last = l + strlen(l); 00185 while (last > l) { 00186 if (!isspace((int)*(last-1))) 00187 break ; 00188 last -- ; 00189 } 00190 *last = (char)0; 00191 00192 return (char*)l ; 00193 } 00194 00195 /* Test code */ 00196 #ifdef TEST 00197 int main(int argc, char * argv[]) 00198 { 00199 char * str ; 00200 00201 str = "\t\tI'm a lumberkack and I'm OK " ; 00202 printf("lowercase: [%s]\n", strlwc(str)); 00203 printf("uppercase: [%s]\n", strupc(str)); 00204 printf("skipped : [%s]\n", strskp(str)); 00205 printf("cropped : [%s]\n", strcrop(str)); 00206 printf("stripped : [%s]\n", strstrip(str)); 00207 00208 return 0 ; 00209 } 00210 #endif 00211 /* vim: set ts=4 et sw=4 tw=75 */