Back to index

openldap  2.4.31
charray.c
Go to the documentation of this file.
00001 /* charray.c - routines for dealing with char * arrays */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 
00017 #include "portable.h"
00018 
00019 #include <stdio.h>
00020 
00021 #include <ac/string.h>
00022 #include <ac/socket.h>
00023 
00024 #include "ldap-int.h"
00025 
00026 int
00027 ldap_charray_add(
00028     char      ***a,
00029     const char *s
00030 )
00031 {
00032        int    n;
00033 
00034        if ( *a == NULL ) {
00035               *a = (char **) LDAP_MALLOC( 2 * sizeof(char *) );
00036               n = 0;
00037 
00038               if( *a == NULL ) {
00039                      return -1;
00040               }
00041 
00042        } else {
00043               char **new;
00044 
00045               for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
00046                      ;      /* NULL */
00047               }
00048 
00049               new = (char **) LDAP_REALLOC( (char *) *a,
00050                   (n + 2) * sizeof(char *) );
00051 
00052               if( new == NULL ) {
00053                      /* caller is required to call ldap_charray_free(*a) */
00054                      return -1;
00055               }
00056 
00057               *a = new;
00058        }
00059 
00060        (*a)[n] = LDAP_STRDUP(s);
00061 
00062        if( (*a)[n] == NULL ) {
00063               return 1;
00064        }
00065 
00066        (*a)[++n] = NULL;
00067 
00068        return 0;
00069 }
00070 
00071 int
00072 ldap_charray_merge(
00073     char      ***a,
00074     char      **s
00075 )
00076 {
00077        int    i, n, nn;
00078        char **aa;
00079 
00080        for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
00081               ;      /* NULL */
00082        }
00083        for ( nn = 0; s[nn] != NULL; nn++ ) {
00084               ;      /* NULL */
00085        }
00086 
00087        aa = (char **) LDAP_REALLOC( (char *) *a, (n + nn + 1) * sizeof(char *) );
00088 
00089        if( aa == NULL ) {
00090               return -1;
00091        }
00092 
00093        *a = aa;
00094 
00095        for ( i = 0; i < nn; i++ ) {
00096               (*a)[n + i] = LDAP_STRDUP(s[i]);
00097 
00098               if( (*a)[n + i] == NULL ) {
00099                      for( --i ; i >= 0 ; i-- ) {
00100                             LDAP_FREE( (*a)[n + i] );
00101                             (*a)[n + i] = NULL;
00102                      }
00103                      return -1;
00104               }
00105        }
00106 
00107        (*a)[n + nn] = NULL;
00108        return 0;
00109 }
00110 
00111 void
00112 ldap_charray_free( char **a )
00113 {
00114        char   **p;
00115 
00116        if ( a == NULL ) {
00117               return;
00118        }
00119 
00120        for ( p = a; *p != NULL; p++ ) {
00121               if ( *p != NULL ) {
00122                      LDAP_FREE( *p );
00123               }
00124        }
00125 
00126        LDAP_FREE( (char *) a );
00127 }
00128 
00129 int
00130 ldap_charray_inlist(
00131     char      **a,
00132     const char *s
00133 )
00134 {
00135        int    i;
00136 
00137        if( a == NULL ) return 0;
00138 
00139        for ( i=0; a[i] != NULL; i++ ) {
00140               if ( strcasecmp( s, a[i] ) == 0 ) {
00141                      return 1;
00142               }
00143        }
00144 
00145        return 0;
00146 }
00147 
00148 char **
00149 ldap_charray_dup( char **a )
00150 {
00151        int    i;
00152        char   **new;
00153 
00154        for ( i = 0; a[i] != NULL; i++ )
00155               ;      /* NULL */
00156 
00157        new = (char **) LDAP_MALLOC( (i + 1) * sizeof(char *) );
00158 
00159        if( new == NULL ) {
00160               return NULL;
00161        }
00162 
00163        for ( i = 0; a[i] != NULL; i++ ) {
00164               new[i] = LDAP_STRDUP( a[i] );
00165 
00166               if( new[i] == NULL ) {
00167                      for( --i ; i >= 0 ; i-- ) {
00168                             LDAP_FREE( new[i] );
00169                      }
00170                      LDAP_FREE( new );
00171                      return NULL;
00172               }
00173        }
00174        new[i] = NULL;
00175 
00176        return( new );
00177 }
00178 
00179 char **
00180 ldap_str2charray( const char *str_in, const char *brkstr )
00181 {
00182        char   **res;
00183        char   *str, *s;
00184        char   *lasts;
00185        int    i;
00186 
00187        /* protect the input string from strtok */
00188        str = LDAP_STRDUP( str_in );
00189        if( str == NULL ) {
00190               return NULL;
00191        }
00192 
00193        i = 1;
00194        for ( s = str; ; LDAP_UTF8_INCR(s) ) {
00195               s = ldap_utf8_strpbrk( s, brkstr );
00196               if ( !s ) break;
00197               i++;
00198        }
00199 
00200        res = (char **) LDAP_MALLOC( (i + 1) * sizeof(char *) );
00201 
00202        if( res == NULL ) {
00203               LDAP_FREE( str );
00204               return NULL;
00205        }
00206 
00207        i = 0;
00208 
00209        for ( s = ldap_utf8_strtok( str, brkstr, &lasts );
00210               s != NULL;
00211               s = ldap_utf8_strtok( NULL, brkstr, &lasts ) )
00212        {
00213               res[i] = LDAP_STRDUP( s );
00214 
00215               if(res[i] == NULL) {
00216                      for( --i ; i >= 0 ; i-- ) {
00217                             LDAP_FREE( res[i] );
00218                      }
00219                      LDAP_FREE( res );
00220                      LDAP_FREE( str );
00221                      return NULL;
00222               }
00223 
00224               i++;
00225        }
00226 
00227        res[i] = NULL;
00228 
00229        LDAP_FREE( str );
00230        return( res );
00231 }
00232 
00233 char * ldap_charray2str( char **a, const char *sep )
00234 {
00235        char *s, **v, *p;
00236        int len;
00237        int slen;
00238 
00239        if( sep == NULL ) sep = " ";
00240 
00241        slen = strlen( sep );
00242        len = 0;
00243 
00244        for ( v = a; *v != NULL; v++ ) {
00245               len += strlen( *v ) + slen;
00246        }
00247 
00248        if ( len == 0 ) {
00249               return NULL;
00250        }
00251 
00252        /* trim extra sep len */
00253        len -= slen;
00254 
00255        s = LDAP_MALLOC ( len + 1 );
00256 
00257        if ( s == NULL ) {
00258               return NULL;  
00259        }
00260 
00261        p = s;
00262        for ( v = a; *v != NULL; v++ ) {
00263               if ( v != a ) {
00264                      strncpy( p, sep, slen );
00265                      p += slen;
00266               }
00267 
00268               len = strlen( *v );
00269               strncpy( p, *v, len );
00270               p += len;
00271        }
00272 
00273        *p = '\0';
00274        return s;
00275 }