]> git.sur5r.net Git - openldap/blob - servers/slapd/charray.c
Move experimental Back-BDB2 to Attic
[openldap] / servers / slapd / charray.c
1 /* charray.c - routines for dealing with char * arrays */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/string.h>
13 #include <ac/socket.h>
14
15 #include "slap.h"
16
17 void
18 charray_add(
19     char        ***a,
20     const char  *s
21 )
22 {
23         int     n;
24
25         if ( *a == NULL ) {
26                 *a = (char **) ch_malloc( 2 * sizeof(char *) );
27                 n = 0;
28         } else {
29                 for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
30                         ;       /* NULL */
31                 }
32
33                 *a = (char **) ch_realloc( (char *) *a,
34                     (n + 2) * sizeof(char *) );
35         }
36
37         (*a)[n++] = ch_strdup(s);
38         (*a)[n] = NULL;
39 }
40
41 void
42 charray_merge(
43     char        ***a,
44     char        **s
45 )
46 {
47         int     i, n, nn;
48
49         for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
50                 ;       /* NULL */
51         }
52         for ( nn = 0; s[nn] != NULL; nn++ ) {
53                 ;       /* NULL */
54         }
55
56         *a = (char **) ch_realloc( (char *) *a, (n + nn + 1) * sizeof(char *) );
57
58         for ( i = 0; i < nn; i++ ) {
59                 (*a)[n + i] = ch_strdup(s[i]);
60         }
61         (*a)[n + nn] = NULL;
62 }
63
64 void
65 charray_free( char **array )
66 {
67         char    **a;
68
69         if ( array == NULL ) {
70                 return;
71         }
72
73         for ( a = array; *a != NULL; a++ ) {
74                 if ( *a != NULL ) {
75                         free( *a );
76                 }
77         }
78         free( (char *) array );
79 }
80
81 int
82 charray_inlist(
83     char        **a,
84     const char  *s
85 )
86 {
87         int     i;
88
89         if( a == NULL ) return 0;
90
91         for ( i = 0; a[i] != NULL; i++ ) {
92                 if ( strcasecmp( s, a[i] ) == 0 ) {
93                         return( 1 );
94                 }
95         }
96
97         return( 0 );
98 }
99
100 char **
101 charray_dup( char **a )
102 {
103         int     i;
104         char    **new;
105
106         for ( i = 0; a[i] != NULL; i++ )
107                 ;       /* NULL */
108
109         new = (char **) ch_malloc( (i + 1) * sizeof(char *) );
110
111         for ( i = 0; a[i] != NULL; i++ ) {
112                 new[i] = ch_strdup( a[i] );
113         }
114         new[i] = NULL;
115
116         return( new );
117 }
118
119
120 char **
121 str2charray( const char *str_in, const char *brkstr )
122 {
123         char    *str;
124         char    **res;
125         char    *s;
126         char    *lasts;
127         int     i;
128
129         /* protect the input string from strtok */
130         str = ch_strdup( str_in );
131
132         i = 1;
133         for ( s = str; *s; s++ ) {
134                 if ( strchr( brkstr, *s ) != NULL ) {
135                         i++;
136                 }
137         }
138
139         res = (char **) ch_malloc( (i + 1) * sizeof(char *) );
140         i = 0;
141
142         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
143                 s != NULL;
144                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
145         {
146                 res[i++] = ch_strdup( s );
147         }
148
149         res[i] = NULL;
150
151         free( str );
152         return( res );
153 }