1 #pragma ident "%Z%%M% %I% %E% SMI"
4 * The contents of this file are subject to the Netscape Public
5 * License Version 1.1 (the "License"); you may not use this file
6 * except in compliance with the License. You may obtain a copy of
7 * the License at http://www.mozilla.org/NPL/
9 * Software distributed under the License is distributed on an "AS
10 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11 * implied. See the License for the specific language governing
12 * rights and limitations under the License.
14 * The Original Code is Mozilla Communicator client code, released
17 * The Initial Developer of the Original Code is Netscape
18 * Communications Corporation. Portions created by Netscape are
19 * Copyright (C) 1998-1999 Netscape Communications Corporation. All
24 /* charray.c - routines for dealing with char * arrays */
30 * Add s at the end of the array of strings *a.
31 * Return 0 for success, -1 for failure.
43 *a
= (char **)NSLDAPI_MALLOC( 2 * sizeof(char *) );
49 for ( n
= 0; *a
!= NULL
&& (*a
)[n
] != NULL
; n
++ ) {
53 *a
= (char **)NSLDAPI_REALLOC( (char *) *a
,
54 (n
+ 2) * sizeof(char *) );
66 * Add array of strings s at the end of the array of strings *a.
67 * Return 0 for success, -1 for failure.
78 if ( (s
== NULL
) || (s
[0] == NULL
) )
81 for ( n
= 0; *a
!= NULL
&& (*a
)[n
] != NULL
; n
++ ) {
84 for ( nn
= 0; s
[nn
] != NULL
; nn
++ ) {
88 *a
= (char **)NSLDAPI_REALLOC( (char *) *a
,
89 (n
+ nn
+ 1) * sizeof(char *) );
94 for ( i
= 0; i
< nn
; i
++ ) {
103 ldap_charray_free( char **array
)
107 if ( array
== NULL
) {
111 for ( a
= array
; *a
!= NULL
; a
++ ) {
116 NSLDAPI_FREE( (char *) array
);
131 for ( i
= 0; a
[i
] != NULL
; i
++ ) {
132 if ( strcasecmp( s
, a
[i
] ) == 0 ) {
141 * Duplicate the array of strings a, return NULL upon any memory failure.
145 ldap_charray_dup( char **a
)
150 for ( i
= 0; a
[i
] != NULL
; i
++ )
153 new = (char **)NSLDAPI_MALLOC( (i
+ 1) * sizeof(char *) );
158 for ( i
= 0; a
[i
] != NULL
; i
++ ) {
159 new[i
] = nsldapi_strdup( a
[i
] );
160 if ( new[i
] == NULL
) {
163 for ( j
= 0; j
< i
; j
++ )
164 NSLDAPI_FREE( new[j
] );
175 * Tokenize the string str, return NULL upon any memory failure.
176 * XXX: on many platforms this function is not thread safe because it
181 ldap_str2charray( char *str
, char *brkstr
)
182 /* This implementation fails if brkstr contains multibyte characters.
183 But it works OK if str is UTF-8 and brkstr is 7-bit ASCII.
191 for ( s
= str
; *s
; s
++ ) {
192 if ( strchr( brkstr
, *s
) != NULL
) {
197 res
= (char **)NSLDAPI_MALLOC( (i
+ 1) * sizeof(char *) );
202 for ( s
= strtok( str
, brkstr
); s
!= NULL
; s
= strtok( NULL
,
204 res
[i
++] = nsldapi_strdup( s
);
205 if ( res
[i
- 1] == NULL
) {
208 for ( j
= 0; j
< (i
- 1); j
++ )
209 NSLDAPI_FREE( res
[j
] );
221 ldap_charray_position( char **a
, char *s
)
225 for ( i
= 0; a
[i
] != NULL
; i
++ ) {
226 if ( strcasecmp( s
, a
[i
] ) == 0 ) {