1 /* charray.c - routines for dealing with char * arrays */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
13 #include <ac/socket.h>
26 *a = (char **) ch_malloc( 2 * sizeof(char *) );
29 for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
33 *a = (char **) ch_realloc( (char *) *a,
34 (n + 2) * sizeof(char *) );
37 (*a)[n++] = ch_strdup(s);
51 *a = (char **) ch_malloc( 2 * sizeof(char *) );
54 for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
58 *a = (char **) ch_realloc( (char *) *a,
59 (n + 2) * sizeof(char *) );
62 (*a)[n] = (char *) ch_malloc( ( l + 1 ) * sizeof( char ) );
63 strncpy( (*a)[n], s, l );
76 for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
79 for ( nn = 0; s[nn] != NULL; nn++ ) {
83 *a = (char **) ch_realloc( (char *) *a, (n + nn + 1) * sizeof(char *) );
85 for ( i = 0; i < nn; i++ ) {
86 (*a)[n + i] = ch_strdup(s[i]);
92 charray_free( char **array )
96 if ( array == NULL ) {
100 for ( a = array; *a != NULL; a++ ) {
105 free( (char *) array );
116 if( a == NULL ) return 0;
118 for ( i = 0; a[i] != NULL; i++ ) {
119 if ( strcasecmp( s, a[i] ) == 0 ) {
135 if( a == NULL ) return 0;
137 for ( i = 0; a[i] != NULL; i++ ) {
138 if ( a[i]->bv_len != s->bv_len) continue;
139 if ( strcasecmp( s->bv_val, a[i]->bv_val ) == 0 ) {
148 charray_dup( char **a )
153 for ( i = 0; a[i] != NULL; i++ )
156 new = (char **) ch_malloc( (i + 1) * sizeof(char *) );
158 for ( i = 0; a[i] != NULL; i++ ) {
159 new[i] = ch_strdup( a[i] );
168 str2charray( const char *str_in, const char *brkstr )
176 /* protect the input string from strtok */
177 str = ch_strdup( str_in );
180 for ( s = str; *s; s++ ) {
181 if ( strchr( brkstr, *s ) != NULL ) {
186 res = (char **) ch_malloc( (i + 1) * sizeof(char *) );
189 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
191 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
193 res[i++] = ch_strdup( s );
202 /* Convert a delimited string into an array of bervals; Add on
203 * to an existing array if it was given.
206 str2bvec( struct berval **vec, const char *in, const char *brkstr )
214 /* protect the input string from strtok */
215 str = ch_strdup( in );
217 for (old = 0; vec && vec[old]; old++);
220 for ( s = str; *s; s++ ) {
221 if ( strchr( brkstr, *s ) != NULL ) {
227 res = (struct berval **) ch_realloc( vec, (old + i + 1) * sizeof(struct berval *) );
230 res = (struct berval **) ch_malloc( (i + 1) * sizeof(struct berval *) );
235 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
237 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
239 vec[i++] = ber_bvstrdup( s );
250 charray_strcmp( const char **a1, const char **a2 )
252 for ( ; a1[0] && a2[0]; a1++, a2++ ) {
253 if ( strcmp( a1[0], a2[0] ) ) {
258 if ( ! ( a1[0] && a2[0] ) ) {
267 charray_strcasecmp( const char **a1, const char **a2 )
269 for ( ; a1[0] && a2[0]; a1++, a2++ ) {
270 if ( strcasecmp( a1[0], a2[0] ) ) {
275 if ( ! ( a1[0] && a2[0] ) ) {
282 /* strcopy is like strcpy except it returns a pointer to the trailing NUL of
283 * the result string. This allows fast construction of catenated strings
284 * without the overhead of strlen/strcat.
295 while (*a++ = *b++) ;
308 *a = (struct berval *) ch_malloc( 2 * sizeof(struct berval) );
311 for ( n = 0; *a != NULL && (*a)[n].bv_val != NULL; n++ ) {
315 *a = (struct berval *) ch_realloc( (char *) *a,
316 (n + 2) * sizeof(struct berval) );
319 ber_dupbv( (*a)+n, bv );
321 (*a)[n].bv_val = NULL;
331 for (i=0; a[i].bv_val; i++)