]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/charray.c
Merge remote branch 'origin/mdb.master' into OPENLDAP_REL_ENG_2_4
[openldap] / libraries / libldap / charray.c
index 82d2feeb1ba264e09413991ad54c337addb7d220..163f4670b5d5e26744db82c2080b3c7efbe66064 100644 (file)
@@ -1,9 +1,18 @@
+/* charray.c - routines for dealing with char * arrays */
 /* $OpenLDAP$ */
-/*
- * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2012 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>.
  */
-/* charray.c - routines for dealing with char * arrays */
 
 #include "portable.h"
 
@@ -17,7 +26,7 @@
 int
 ldap_charray_add(
     char       ***a,
-    char       *s
+    const char *s
 )
 {
        int     n;
@@ -77,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;
 
@@ -119,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 **
@@ -179,10 +191,10 @@ ldap_str2charray( const char *str_in, const char *brkstr )
        }
 
        i = 1;
-       for ( s = str; *s; s++ ) {
-               if ( ldap_utf8_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 *) );
@@ -221,25 +233,26 @@ ldap_str2charray( const char *str_in, const char *brkstr )
 char * ldap_charray2str( char **a, const char *sep )
 {
        char *s, **v, *p;
-       int len = 0;
+       int len;
        int slen;
 
        if( sep == NULL ) sep = " ";
 
        slen = strlen( sep );
+       len = 0;
 
        for ( v = a; *v != NULL; v++ ) {
-               len += strlen( *v ) + slen; /* for a space */
+               len += strlen( *v ) + slen;
        }
 
        if ( len == 0 ) {
                return NULL;
        }
 
+       /* trim extra sep len */
        len -= slen;
-       len += 1; /* EOS */
 
-       s = LDAP_MALLOC ( len );
+       s = LDAP_MALLOC ( len + 1 );
 
        if ( s == NULL ) {
                return NULL;    
@@ -247,8 +260,6 @@ char * ldap_charray2str( char **a, const char *sep )
 
        p = s;
        for ( v = a; *v != NULL; v++ ) {
-               int len;
-
                if ( v != a ) {
                        strncpy( p, sep, slen );
                        p += slen;