s3: Fix vfs_xattr_tdb.c
[Samba/gebeck_regimport.git] / lib / iniparser / src / strlib.c
blobf0d85aea580246991be353affe2657af49ce5d7b
2 /*-------------------------------------------------------------------------*/
3 /**
4 @file strlib.c
5 @author N. Devillard
6 @date Jan 2001
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 $
17 $Author: ndevilla $
18 $Date: 2006-09-27 11:04:11 $
19 $Revision: 1.9 $
22 /*---------------------------------------------------------------------------
23 Includes
24 ---------------------------------------------------------------------------*/
26 #include <string.h>
27 #include <ctype.h>
29 #include "strlib.h"
31 /*---------------------------------------------------------------------------
32 Defines
33 ---------------------------------------------------------------------------*/
34 #define ASCIILINESZ 1024
36 /*---------------------------------------------------------------------------
37 Function codes
38 ---------------------------------------------------------------------------*/
41 /*-------------------------------------------------------------------------*/
42 /**
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];
57 int i ;
59 if (s==NULL) return NULL ;
60 memset(l, 0, ASCIILINESZ+1);
61 i=0 ;
62 while (s[i] && i<ASCIILINESZ) {
63 l[i] = (char)tolower((int)s[i]);
64 i++ ;
66 l[ASCIILINESZ]=(char)0;
67 return l ;
72 /*-------------------------------------------------------------------------*/
73 /**
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];
88 int i ;
90 if (s==NULL) return NULL ;
91 memset(l, 0, ASCIILINESZ+1);
92 i=0 ;
93 while (s[i] && i<ASCIILINESZ) {
94 l[i] = (char)toupper((int)s[i]);
95 i++ ;
97 l[ASCIILINESZ]=(char)0;
98 return l ;
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
110 given string.
112 /*--------------------------------------------------------------------------*/
114 char * strskp(char * s)
116 char * skip = s;
117 if (s==NULL) return NULL ;
118 while (isspace((int)*skip) && *skip) skip++;
119 return 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
135 (not re-entrant).
137 /*--------------------------------------------------------------------------*/
139 char * strcrop(char * s)
141 static char l[ASCIILINESZ+1];
142 char * last ;
144 if (s==NULL) return NULL ;
145 memset(l, 0, ASCIILINESZ+1);
146 strcpy(l, s);
147 last = l + strlen(l);
148 while (last > l) {
149 if (!isspace((int)*(last-1)))
150 break ;
151 last -- ;
153 *last = (char)0;
154 return l ;
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
170 (not re-entrant).
172 /*--------------------------------------------------------------------------*/
173 char * strstrip(char * s)
175 static char l[ASCIILINESZ+1];
176 char * last ;
178 if (s==NULL) return NULL ;
180 while (isspace((int)*s) && *s) s++;
182 memset(l, 0, ASCIILINESZ+1);
183 strcpy(l, s);
184 last = l + strlen(l);
185 while (last > l) {
186 if (!isspace((int)*(last-1)))
187 break ;
188 last -- ;
190 *last = (char)0;
192 return (char*)l ;
195 /* Test code */
196 #ifdef TEST
197 int main(int argc, char * argv[])
199 char * str ;
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));
208 return 0 ;
210 #endif
211 /* vim: set ts=4 et sw=4 tw=75 */