2 /*-------------------------------------------------------------------------*/
7 @version $Revision: 1.9 $
8 @brief Various string handling routines to complement the C lib.
10 This modules adds a few complementary string routines usually missing
11 in the standard C library.
13 /*--------------------------------------------------------------------------*/
16 $Id: strlib.c,v 1.9 2006-09-27 11:04:11 ndevilla Exp $
18 $Date: 2006-09-27 11:04:11 $
22 /*---------------------------------------------------------------------------
24 ---------------------------------------------------------------------------*/
31 /*---------------------------------------------------------------------------
33 ---------------------------------------------------------------------------*/
34 #define ASCIILINESZ 1024
36 /*---------------------------------------------------------------------------
38 ---------------------------------------------------------------------------*/
41 /*-------------------------------------------------------------------------*/
43 @brief Convert a string to lowercase.
44 @param s String to convert.
45 @return ptr to statically allocated string.
47 This function returns a pointer to a statically allocated string
48 containing a lowercased version of the input string. Do not free
49 or modify the returned string! Since the returned string is statically
50 allocated, it will be modified at each function call (not re-entrant).
52 /*--------------------------------------------------------------------------*/
54 char * strlwc(const char * s
)
56 static char l
[ASCIILINESZ
+1];
59 if (s
==NULL
) return NULL
;
60 memset(l
, 0, ASCIILINESZ
+1);
62 while (s
[i
] && i
<ASCIILINESZ
) {
63 l
[i
] = (char)tolower((int)s
[i
]);
66 l
[ASCIILINESZ
]=(char)0;
72 /*-------------------------------------------------------------------------*/
74 @brief Convert a string to uppercase.
75 @param s String to convert.
76 @return ptr to statically allocated string.
78 This function returns a pointer to a statically allocated string
79 containing an uppercased version of the input string. Do not free
80 or modify the returned string! Since the returned string is statically
81 allocated, it will be modified at each function call (not re-entrant).
83 /*--------------------------------------------------------------------------*/
85 char * strupc(char * s
)
87 static char l
[ASCIILINESZ
+1];
90 if (s
==NULL
) return NULL
;
91 memset(l
, 0, ASCIILINESZ
+1);
93 while (s
[i
] && i
<ASCIILINESZ
) {
94 l
[i
] = (char)toupper((int)s
[i
]);
97 l
[ASCIILINESZ
]=(char)0;
103 /*-------------------------------------------------------------------------*/
105 @brief Skip blanks until the first non-blank character.
106 @param s String to parse.
107 @return Pointer to char inside given string.
109 This function returns a pointer to the first non-blank character in the
112 /*--------------------------------------------------------------------------*/
114 char * strskp(char * s
)
117 if (s
==NULL
) return NULL
;
118 while (isspace((int)*skip
) && *skip
) skip
++;
124 /*-------------------------------------------------------------------------*/
126 @brief Remove blanks at the end of a string.
127 @param s String to parse.
128 @return ptr to statically allocated string.
130 This function returns a pointer to a statically allocated string,
131 which is identical to the input string, except that all blank
132 characters at the end of the string have been removed.
133 Do not free or modify the returned string! Since the returned string
134 is statically allocated, it will be modified at each function call
137 /*--------------------------------------------------------------------------*/
139 char * strcrop(char * s
)
141 static char l
[ASCIILINESZ
+1];
144 if (s
==NULL
) return NULL
;
145 memset(l
, 0, ASCIILINESZ
+1);
147 last
= l
+ strlen(l
);
149 if (!isspace((int)*(last
-1)))
159 /*-------------------------------------------------------------------------*/
161 @brief Remove blanks at the beginning and the end of a string.
162 @param s String to parse.
163 @return ptr to statically allocated string.
165 This function returns a pointer to a statically allocated string,
166 which is identical to the input string, except that all blank
167 characters at the end and the beg. of the string have been removed.
168 Do not free or modify the returned string! Since the returned string
169 is statically allocated, it will be modified at each function call
172 /*--------------------------------------------------------------------------*/
173 char * strstrip(char * s
)
175 static char l
[ASCIILINESZ
+1];
178 if (s
==NULL
) return NULL
;
180 while (isspace((int)*s
) && *s
) s
++;
182 memset(l
, 0, ASCIILINESZ
+1);
184 last
= l
+ strlen(l
);
186 if (!isspace((int)*(last
-1)))
197 int main(int argc
, char * argv
[])
201 str
= "\t\tI'm a lumberkack and I'm OK " ;
202 printf("lowercase: [%s]\n", strlwc(str
));
203 printf("uppercase: [%s]\n", strupc(str
));
204 printf("skipped : [%s]\n", strskp(str
));
205 printf("cropped : [%s]\n", strcrop(str
));
206 printf("stripped : [%s]\n", strstrip(str
));
211 /* vim: set ts=4 et sw=4 tw=75 */