Back to index

lightning-sunbird  0.9+nobinonly
charray.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 /* charray.c - routines for dealing with char * arrays */
00038 
00039 
00040 #include "ldap-int.h" 
00041  
00042 /*
00043  * Add s at the end of the array of strings *a.
00044  * Return 0 for success, -1 for failure.
00045  */
00046 int
00047 LDAP_CALL
00048 ldap_charray_add(
00049     char      ***a,
00050     char      *s
00051 )
00052 {
00053        int    n;
00054 
00055        if ( *a == NULL ) {
00056               *a = (char **)NSLDAPI_MALLOC( 2 * sizeof(char *) );
00057               if ( *a == NULL ) {
00058                      return -1;
00059               }
00060               n = 0;
00061        } else {
00062               for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
00063                      ;      /* NULL */
00064               }
00065 
00066               *a = (char **)NSLDAPI_REALLOC( (char *) *a,
00067                   (n + 2) * sizeof(char *) );
00068               if ( *a == NULL ) {
00069                      return -1;
00070               }
00071        }
00072 
00073        (*a)[n++] = s;
00074        (*a)[n] = NULL;
00075        return 0;
00076 }
00077 
00078 /*
00079  * Add array of strings s at the end of the array of strings *a.
00080  * Return 0 for success, -1 for failure.
00081  */
00082 int
00083 LDAP_CALL
00084 ldap_charray_merge(
00085     char      ***a,
00086     char      **s
00087 )
00088 {
00089        int    i, n, nn;
00090 
00091        if ( (s == NULL) || (s[0] == NULL) )
00092            return 0;
00093 
00094        for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
00095               ;      /* NULL */
00096        }
00097        for ( nn = 0; s[nn] != NULL; nn++ ) {
00098               ;      /* NULL */
00099        }
00100 
00101        *a = (char **)NSLDAPI_REALLOC( (char *) *a,
00102            (n + nn + 1) * sizeof(char *) );
00103        if ( *a == NULL ) {
00104               return -1;
00105        }
00106 
00107        for ( i = 0; i < nn; i++ ) {
00108               (*a)[n + i] = s[i];
00109        }
00110        (*a)[n + nn] = NULL;
00111        return 0;
00112 }
00113 
00114 void
00115 LDAP_CALL
00116 ldap_charray_free( char **array )
00117 {
00118        char   **a;
00119 
00120        if ( array == NULL ) {
00121               return;
00122        }
00123 
00124        for ( a = array; *a != NULL; a++ ) {
00125               if ( *a != NULL ) {
00126                      NSLDAPI_FREE( *a );
00127               }
00128        }
00129        NSLDAPI_FREE( (char *) array );
00130 }
00131 
00132 int
00133 LDAP_CALL
00134 ldap_charray_inlist(
00135     char      **a,
00136     char      *s
00137 )
00138 {
00139        int    i;
00140 
00141        if ( a == NULL )
00142               return( 0 );
00143 
00144        for ( i = 0; a[i] != NULL; i++ ) {
00145               if ( strcasecmp( s, a[i] ) == 0 ) {
00146                      return( 1 );
00147               }
00148        }
00149 
00150        return( 0 );
00151 }
00152 
00153 /*
00154  * Duplicate the array of strings a, return NULL upon any memory failure.
00155  */
00156 char **
00157 LDAP_CALL
00158 ldap_charray_dup( char **a )
00159 {
00160        int    i;
00161        char   **new;
00162 
00163        for ( i = 0; a[i] != NULL; i++ )
00164               ;      /* NULL */
00165 
00166        new = (char **)NSLDAPI_MALLOC( (i + 1) * sizeof(char *) );
00167        if ( new == NULL ) {
00168               return NULL;
00169        }
00170 
00171        for ( i = 0; a[i] != NULL; i++ ) {
00172               new[i] = nsldapi_strdup( a[i] );
00173               if ( new[i] == NULL ) {
00174                      int    j;
00175 
00176                      for ( j = 0; j < i; j++ )
00177                          NSLDAPI_FREE( new[j] );
00178                      NSLDAPI_FREE( new );
00179                      return NULL;
00180               }
00181        }
00182        new[i] = NULL;
00183 
00184        return( new );
00185 }
00186 
00187 /*
00188  * Tokenize the string str, return NULL upon any memory failure.
00189  * XXX: on many platforms this function is not thread safe because it
00190  *     uses strtok().
00191  */
00192 char **
00193 LDAP_CALL
00194 ldap_str2charray( char *str, char *brkstr )
00195      /* This implementation fails if brkstr contains multibyte characters.
00196         But it works OK if str is UTF-8 and brkstr is 7-bit ASCII.
00197       */
00198 {
00199        char   **res;
00200        char   *s;
00201        int    i;
00202 
00203        i = 1;
00204        for ( s = str; *s; s++ ) {
00205               if ( strchr( brkstr, *s ) != NULL ) {
00206                      i++;
00207               }
00208        }
00209 
00210        res = (char **)NSLDAPI_MALLOC( (i + 1) * sizeof(char *) );
00211        if ( res == NULL ) {
00212               return NULL;
00213        }
00214        i = 0;
00215        for ( s = strtok( str, brkstr ); s != NULL; s = strtok( NULL,
00216            brkstr ) ) {
00217               res[i++] = nsldapi_strdup( s );
00218               if ( res[i - 1] == NULL ) {
00219                      int    j;
00220 
00221                      for ( j = 0; j < (i - 1); j++ )
00222                          NSLDAPI_FREE( res[j] );
00223                      NSLDAPI_FREE( res );
00224                      return NULL;
00225               }
00226        }
00227        res[i] = NULL;
00228 
00229        return( res );
00230 }
00231 
00232 int
00233 LDAP_CALL
00234 ldap_charray_position( char **a, char *s )
00235 {
00236        int     i;
00237 
00238        for ( i = 0; a[i] != NULL; i++ ) {
00239               if ( strcasecmp( s, a[i] ) == 0 ) {
00240                      return( i );
00241               }
00242        }
00243 
00244        return( -1 );
00245 }