1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A/*
1N/A * The contents of this file are subject to the Netscape Public
1N/A * License Version 1.1 (the "License"); you may not use this file
1N/A * except in compliance with the License. You may obtain a copy of
1N/A * the License at http://www.mozilla.org/NPL/
1N/A *
1N/A * Software distributed under the License is distributed on an "AS
1N/A * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
1N/A * implied. See the License for the specific language governing
1N/A * rights and limitations under the License.
1N/A *
1N/A * The Original Code is Mozilla Communicator client code, released
1N/A * March 31, 1998.
1N/A *
1N/A * The Initial Developer of the Original Code is Netscape
1N/A * Communications Corporation. Portions created by Netscape are
1N/A * Copyright (C) 1998-1999 Netscape Communications Corporation. All
1N/A * Rights Reserved.
1N/A *
1N/A * Contributor(s):
1N/A */
1N/A/* charray.c - routines for dealing with char * arrays */
1N/A
1N/A
1N/A#include "ldap-int.h"
1N/A
1N/A/*
1N/A * Add s at the end of the array of strings *a.
1N/A * Return 0 for success, -1 for failure.
1N/A */
1N/Aint
1N/ALDAP_CALL
1N/Aldap_charray_add(
1N/A char ***a,
1N/A char *s
1N/A)
1N/A{
1N/A int n;
1N/A
1N/A if ( *a == NULL ) {
1N/A *a = (char **)NSLDAPI_MALLOC( 2 * sizeof(char *) );
1N/A if ( *a == NULL ) {
1N/A return -1;
1N/A }
1N/A n = 0;
1N/A } else {
1N/A for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
1N/A ; /* NULL */
1N/A }
1N/A
1N/A *a = (char **)NSLDAPI_REALLOC( (char *) *a,
1N/A (n + 2) * sizeof(char *) );
1N/A if ( *a == NULL ) {
1N/A return -1;
1N/A }
1N/A }
1N/A
1N/A (*a)[n++] = s;
1N/A (*a)[n] = NULL;
1N/A return 0;
1N/A}
1N/A
1N/A/*
1N/A * Add array of strings s at the end of the array of strings *a.
1N/A * Return 0 for success, -1 for failure.
1N/A */
1N/Aint
1N/ALDAP_CALL
1N/Aldap_charray_merge(
1N/A char ***a,
1N/A char **s
1N/A)
1N/A{
1N/A int i, n, nn;
1N/A
1N/A if ( (s == NULL) || (s[0] == NULL) )
1N/A return 0;
1N/A
1N/A for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
1N/A ; /* NULL */
1N/A }
1N/A for ( nn = 0; s[nn] != NULL; nn++ ) {
1N/A ; /* NULL */
1N/A }
1N/A
1N/A *a = (char **)NSLDAPI_REALLOC( (char *) *a,
1N/A (n + nn + 1) * sizeof(char *) );
1N/A if ( *a == NULL ) {
1N/A return -1;
1N/A }
1N/A
1N/A for ( i = 0; i < nn; i++ ) {
1N/A (*a)[n + i] = s[i];
1N/A }
1N/A (*a)[n + nn] = NULL;
1N/A return 0;
1N/A}
1N/A
1N/Avoid
1N/ALDAP_CALL
1N/Aldap_charray_free( char **array )
1N/A{
1N/A char **a;
1N/A
1N/A if ( array == NULL ) {
1N/A return;
1N/A }
1N/A
1N/A for ( a = array; *a != NULL; a++ ) {
1N/A if ( *a != NULL ) {
1N/A NSLDAPI_FREE( *a );
1N/A }
1N/A }
1N/A NSLDAPI_FREE( (char *) array );
1N/A}
1N/A
1N/Aint
1N/ALDAP_CALL
1N/Aldap_charray_inlist(
1N/A char **a,
1N/A char *s
1N/A)
1N/A{
1N/A int i;
1N/A
1N/A if ( a == NULL )
1N/A return( 0 );
1N/A
1N/A for ( i = 0; a[i] != NULL; i++ ) {
1N/A if ( strcasecmp( s, a[i] ) == 0 ) {
1N/A return( 1 );
1N/A }
1N/A }
1N/A
1N/A return( 0 );
1N/A}
1N/A
1N/A/*
1N/A * Duplicate the array of strings a, return NULL upon any memory failure.
1N/A */
1N/Achar **
1N/ALDAP_CALL
1N/Aldap_charray_dup( char **a )
1N/A{
1N/A int i;
1N/A char **new;
1N/A
1N/A for ( i = 0; a[i] != NULL; i++ )
1N/A ; /* NULL */
1N/A
1N/A new = (char **)NSLDAPI_MALLOC( (i + 1) * sizeof(char *) );
1N/A if ( new == NULL ) {
1N/A return NULL;
1N/A }
1N/A
1N/A for ( i = 0; a[i] != NULL; i++ ) {
1N/A new[i] = nsldapi_strdup( a[i] );
1N/A if ( new[i] == NULL ) {
1N/A int j;
1N/A
1N/A for ( j = 0; j < i; j++ )
1N/A NSLDAPI_FREE( new[j] );
1N/A NSLDAPI_FREE( new );
1N/A return NULL;
1N/A }
1N/A }
1N/A new[i] = NULL;
1N/A
1N/A return( new );
1N/A}
1N/A
1N/A/*
1N/A * Tokenize the string str, return NULL upon any memory failure.
1N/A * XXX: on many platforms this function is not thread safe because it
1N/A * uses strtok().
1N/A */
1N/Achar **
1N/ALDAP_CALL
1N/Aldap_str2charray( char *str, char *brkstr )
1N/A /* This implementation fails if brkstr contains multibyte characters.
1N/A But it works OK if str is UTF-8 and brkstr is 7-bit ASCII.
1N/A */
1N/A{
1N/A char **res;
1N/A char *s;
1N/A int i;
1N/A
1N/A i = 1;
1N/A for ( s = str; *s; s++ ) {
1N/A if ( strchr( brkstr, *s ) != NULL ) {
1N/A i++;
1N/A }
1N/A }
1N/A
1N/A res = (char **)NSLDAPI_MALLOC( (i + 1) * sizeof(char *) );
1N/A if ( res == NULL ) {
1N/A return NULL;
1N/A }
1N/A i = 0;
1N/A for ( s = strtok( str, brkstr ); s != NULL; s = strtok( NULL,
1N/A brkstr ) ) {
1N/A res[i++] = nsldapi_strdup( s );
1N/A if ( res[i - 1] == NULL ) {
1N/A int j;
1N/A
1N/A for ( j = 0; j < (i - 1); j++ )
1N/A NSLDAPI_FREE( res[j] );
1N/A NSLDAPI_FREE( res );
1N/A return NULL;
1N/A }
1N/A }
1N/A res[i] = NULL;
1N/A
1N/A return( res );
1N/A}
1N/A
1N/Aint
1N/ALDAP_CALL
1N/Aldap_charray_position( char **a, char *s )
1N/A{
1N/A int i;
1N/A
1N/A for ( i = 0; a[i] != NULL; i++ ) {
1N/A if ( strcasecmp( s, a[i] ) == 0 ) {
1N/A return( i );
1N/A }
1N/A }
1N/A
1N/A return( -1 );
1N/A}