]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/charray.c
ITS#6254
[openldap] / libraries / libldap / charray.c
index fefe1d916604a2c7362047ce8ec7047420607276..6692428182765c39507db52a996e58a38f717c59 100644 (file)
@@ -1,8 +1,18 @@
-/*
- * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
- */
 /* charray.c - routines for dealing with char * arrays */
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2009 The OpenLDAP Foundation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
+ */
 
 #include "portable.h"
 
@@ -16,7 +26,7 @@
 int
 ldap_charray_add(
     char       ***a,
-    char       *s
+    const char *s
 )
 {
        int     n;
@@ -24,17 +34,27 @@ ldap_charray_add(
        if ( *a == NULL ) {
                *a = (char **) LDAP_MALLOC( 2 * sizeof(char *) );
                n = 0;
+
+               if( *a == NULL ) {
+                       return -1;
+               }
+
        } else {
+               char **new;
+
                for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
                        ;       /* NULL */
                }
 
-               *a = (char **) LDAP_REALLOC( (char *) *a,
+               new = (char **) LDAP_REALLOC( (char *) *a,
                    (n + 2) * sizeof(char *) );
-       }
 
-       if( *a == NULL ) {
-               return -1;
+               if( new == NULL ) {
+                       /* caller is required to call ldap_charray_free(*a) */
+                       return -1;
+               }
+
+               *a = new;
        }
 
        (*a)[n] = LDAP_STRDUP(s);
@@ -66,8 +86,9 @@ ldap_charray_merge(
 
        aa = (char **) LDAP_REALLOC( (char *) *a, (n + nn + 1) * sizeof(char *) );
 
-       if( aa == NULL )
+       if( aa == NULL ) {
                return -1;
+       }
 
        *a = aa;
 
@@ -108,18 +129,20 @@ ldap_charray_free( char **a )
 int
 ldap_charray_inlist(
     char       **a,
-    char       *s
+    const char *s
 )
 {
        int     i;
 
-       for ( i = 0; a[i] != NULL; i++ ) {
+       if( a == NULL ) return 0;
+
+       for ( i=0; a[i] != NULL; i++ ) {
                if ( strcasecmp( s, a[i] ) == 0 ) {
-                       return( 1 );
+                       return 1;
                }
        }
 
-       return( 0 );
+       return 0;
 }
 
 char **
@@ -154,24 +177,24 @@ ldap_charray_dup( char **a )
 }
 
 char **
-ldap_str2charray( char *str, char *brkstr )
+ldap_str2charray( const char *str_in, const char *brkstr )
 {
        char    **res;
-       char    *s;
+       char    *str, *s;
        char    *lasts;
        int     i;
 
        /* protect the input string from strtok */
-       str = LDAP_STRDUP( str );
+       str = LDAP_STRDUP( str_in );
        if( str == NULL ) {
                return NULL;
        }
 
        i = 1;
-       for ( s = str; *s; s++ ) {
-               if ( strchr( brkstr, *s ) != NULL ) {
-                       i++;
-               }
+       for ( s = str; ; LDAP_UTF8_INCR(s) ) {
+               s = ldap_utf8_strpbrk( s, brkstr );
+               if ( !s ) break;
+               i++;
        }
 
        res = (char **) LDAP_MALLOC( (i + 1) * sizeof(char *) );
@@ -183,9 +206,9 @@ ldap_str2charray( char *str, char *brkstr )
 
        i = 0;
 
-       for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
+       for ( s = ldap_utf8_strtok( str, brkstr, &lasts );
                s != NULL;
-               s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
+               s = ldap_utf8_strtok( NULL, brkstr, &lasts ) )
        {
                res[i] = LDAP_STRDUP( s );
 
@@ -206,3 +229,47 @@ ldap_str2charray( char *str, char *brkstr )
        LDAP_FREE( str );
        return( res );
 }
+
+char * ldap_charray2str( char **a, const char *sep )
+{
+       char *s, **v, *p;
+       int len;
+       int slen;
+
+       if( sep == NULL ) sep = " ";
+
+       slen = strlen( sep );
+       len = 0;
+
+       for ( v = a; *v != NULL; v++ ) {
+               len += strlen( *v ) + slen;
+       }
+
+       if ( len == 0 ) {
+               return NULL;
+       }
+
+       /* trim extra sep len */
+       len -= slen;
+
+       s = LDAP_MALLOC ( len + 1 );
+
+       if ( s == NULL ) {
+               return NULL;    
+       }
+
+       p = s;
+       for ( v = a; *v != NULL; v++ ) {
+               if ( v != a ) {
+                       strncpy( p, sep, slen );
+                       p += slen;
+               }
+
+               len = strlen( *v );
+               strncpy( p, *v, len );
+               p += len;
+       }
+
+       *p = '\0';
+       return s;
+}