xref: /netbsd-src/external/bsd/openldap/dist/libraries/libldap/string.c (revision 466a16a118933bd295a8a104f095714fadf9cf68)
1 /* $OpenLDAP: pkg/ldap/libraries/libldap/string.c,v 1.23.2.3 2008/02/11 23:26:41 kurt Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2008 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 
16 /*
17  * Locale-specific 1-byte character versions
18  * See utf-8.c for UTF-8 versions
19  */
20 
21 #include "portable.h"
22 
23 #include <ac/stdlib.h>
24 #include <ac/string.h>
25 #include <ac/time.h>
26 #include <ac/ctype.h>
27 
28 #include "ldap-int.h"
29 
30 
31 #if defined ( HAVE_STRSPN )
32 #define int_strspn strspn
33 #else
34 static int int_strspn( const char *str, const char *delim )
35 {
36 	int pos;
37 	const char *p=delim;
38 
39 	for( pos=0; (*str) ; pos++,str++) {
40 		if (*str!=*p) {
41 			for( p=delim; (*p) ; p++ ) {
42 				if (*str==*p) {
43 					break;
44 				}
45 		  	}
46 		}
47 
48 		if (*p=='\0') {
49 			return pos;
50 		}
51 	}
52 	return pos;
53 }
54 #endif
55 
56 #if defined( HAVE_STRPBRK )
57 #define int_strpbrk strpbrk
58 #else
59 static char *(int_strpbrk)( const char *str, const char *accept )
60 {
61 	const char *p;
62 
63 	for( ; (*str) ; str++ ) {
64 		for( p=accept; (*p) ; p++) {
65 			if (*str==*p) {
66 				return str;
67 			}
68 		}
69 	}
70 
71 	return NULL;
72 }
73 #endif
74 
75 char *(ldap_pvt_strtok)( char *str, const char *delim, char **pos )
76 {
77 	char *p;
78 
79 	if (pos==NULL) {
80 		return NULL;
81 	}
82 
83 	if (str==NULL) {
84 		if (*pos==NULL) {
85 			return NULL;
86 		}
87 
88 		str=*pos;
89 	}
90 
91 	/* skip any initial delimiters */
92 	str += int_strspn( str, delim );
93 	if (*str == '\0') {
94 		return NULL;
95 	}
96 
97 	p = int_strpbrk( str, delim );
98 	if (p==NULL) {
99 		*pos = NULL;
100 
101 	} else {
102 		*p ='\0';
103 		*pos = p+1;
104 	}
105 
106 	return str;
107 }
108 
109 char *
110 ldap_pvt_str2upper( char *str )
111 {
112 	char    *s;
113 
114 	/* to upper */
115 	if ( str ) {
116 		for ( s = str; *s; s++ ) {
117 			*s = TOUPPER( (unsigned char) *s );
118 		}
119 	}
120 
121 	return( str );
122 }
123 
124 struct berval *
125 ldap_pvt_str2upperbv( char *str, struct berval *bv )
126 {
127 	char    *s = NULL;
128 
129 	assert( bv != NULL );
130 
131 	/* to upper */
132 	if ( str ) {
133 		for ( s = str; *s; s++ ) {
134 			*s = TOUPPER( (unsigned char) *s );
135 		}
136 	}
137 
138 	bv->bv_val = str;
139 	bv->bv_len = (ber_len_t)(s - str);
140 
141 	return( bv );
142 }
143 
144 char *
145 ldap_pvt_str2lower( char *str )
146 {
147 	char    *s;
148 
149 	/* to lower */
150 	if ( str ) {
151 		for ( s = str; *s; s++ ) {
152 			*s = TOLOWER( (unsigned char) *s );
153 		}
154 	}
155 
156 	return( str );
157 }
158 
159 struct berval *
160 ldap_pvt_str2lowerbv( char *str, struct berval *bv )
161 {
162 	char    *s = NULL;
163 
164 	assert( bv != NULL );
165 
166 	/* to lower */
167 	if ( str ) {
168 		for ( s = str; *s; s++ ) {
169 			*s = TOLOWER( (unsigned char) *s );
170 		}
171 	}
172 
173 	bv->bv_val = str;
174 	bv->bv_len = (ber_len_t)(s - str);
175 
176 	return( bv );
177 }
178