1 /* charray.c - routines for dealing with char * arrays */
2 /* $OpenLDAP: pkg/ldap/libraries/libldap/charray.c,v 1.16.2.3 2008/02/11 23:26:41 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
21 #include <ac/string.h>
22 #include <ac/socket.h>
35 *a
= (char **) LDAP_MALLOC( 2 * sizeof(char *) );
45 for ( n
= 0; *a
!= NULL
&& (*a
)[n
] != NULL
; n
++ ) {
49 new = (char **) LDAP_REALLOC( (char *) *a
,
50 (n
+ 2) * sizeof(char *) );
53 /* caller is required to call ldap_charray_free(*a) */
60 (*a
)[n
] = LDAP_STRDUP(s
);
62 if( (*a
)[n
] == NULL
) {
80 for ( n
= 0; *a
!= NULL
&& (*a
)[n
] != NULL
; n
++ ) {
83 for ( nn
= 0; s
[nn
] != NULL
; nn
++ ) {
87 aa
= (char **) LDAP_REALLOC( (char *) *a
, (n
+ nn
+ 1) * sizeof(char *) );
95 for ( i
= 0; i
< nn
; i
++ ) {
96 (*a
)[n
+ i
] = LDAP_STRDUP(s
[i
]);
98 if( (*a
)[n
+ i
] == NULL
) {
99 for( --i
; i
>= 0 ; i
-- ) {
100 LDAP_FREE( (*a
)[n
+ i
] );
112 ldap_charray_free( char **a
)
120 for ( p
= a
; *p
!= NULL
; p
++ ) {
126 LDAP_FREE( (char *) a
);
137 if( a
== NULL
) return 0;
139 for ( i
=0; a
[i
] != NULL
; i
++ ) {
140 if ( strcasecmp( s
, a
[i
] ) == 0 ) {
149 ldap_charray_dup( char **a
)
154 for ( i
= 0; a
[i
] != NULL
; i
++ )
157 new = (char **) LDAP_MALLOC( (i
+ 1) * sizeof(char *) );
163 for ( i
= 0; a
[i
] != NULL
; i
++ ) {
164 new[i
] = LDAP_STRDUP( a
[i
] );
166 if( new[i
] == NULL
) {
167 for( --i
; i
>= 0 ; i
-- ) {
180 ldap_str2charray( const char *str_in
, const char *brkstr
)
187 /* protect the input string from strtok */
188 str
= LDAP_STRDUP( str_in
);
194 for ( s
= str
; *s
; s
++ ) {
195 if ( ldap_utf8_strchr( brkstr
, s
) != NULL
) {
200 res
= (char **) LDAP_MALLOC( (i
+ 1) * sizeof(char *) );
209 for ( s
= ldap_utf8_strtok( str
, brkstr
, &lasts
);
211 s
= ldap_utf8_strtok( NULL
, brkstr
, &lasts
) )
213 res
[i
] = LDAP_STRDUP( s
);
216 for( --i
; i
>= 0 ; i
-- ) {
233 char * ldap_charray2str( char **a
, const char *sep
)
239 if( sep
== NULL
) sep
= " ";
241 slen
= strlen( sep
);
244 for ( v
= a
; *v
!= NULL
; v
++ ) {
245 len
+= strlen( *v
) + slen
;
252 /* trim extra sep len */
255 s
= LDAP_MALLOC ( len
+ 1 );
262 for ( v
= a
; *v
!= NULL
; v
++ ) {
264 strncpy( p
, sep
, slen
);
269 strncpy( p
, *v
, len
);