imagehlp/tests: Use nameless unions/structs.
[wine.git] / libs / ldap / libldap / charray.c
blobcd83290c86dfe5efeb6720881a6dd7b833cf561f
1 /* charray.c - routines for dealing with char * arrays */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2022 The OpenLDAP Foundation.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 #include "portable.h"
19 #include <stdio.h>
21 #include <ac/string.h>
22 #include <ac/socket.h>
24 #include "ldap-int.h"
26 int
27 ldap_charray_add(
28 char ***a,
29 const char *s
32 int n;
34 if ( *a == NULL ) {
35 *a = (char **) LDAP_MALLOC( 2 * sizeof(char *) );
36 n = 0;
38 if( *a == NULL ) {
39 return -1;
42 } else {
43 char **new;
45 for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
46 ; /* NULL */
49 new = (char **) LDAP_REALLOC( (char *) *a,
50 (n + 2) * sizeof(char *) );
52 if( new == NULL ) {
53 /* caller is required to call ldap_charray_free(*a) */
54 return -1;
57 *a = new;
60 (*a)[n] = LDAP_STRDUP(s);
62 if( (*a)[n] == NULL ) {
63 return 1;
66 (*a)[++n] = NULL;
68 return 0;
71 int
72 ldap_charray_merge(
73 char ***a,
74 char **s
77 int i, n, nn;
78 char **aa;
80 for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
81 ; /* NULL */
83 for ( nn = 0; s[nn] != NULL; nn++ ) {
84 ; /* NULL */
87 aa = (char **) LDAP_REALLOC( (char *) *a, (n + nn + 1) * sizeof(char *) );
89 if( aa == NULL ) {
90 return -1;
93 *a = aa;
95 for ( i = 0; i < nn; i++ ) {
96 (*a)[n + i] = LDAP_STRDUP(s[i]);
98 if( (*a)[n + i] == NULL ) {
99 for( --i ; i >= 0 ; i-- ) {
100 LDAP_FREE( (*a)[n + i] );
101 (*a)[n + i] = NULL;
103 return -1;
107 (*a)[n + nn] = NULL;
108 return 0;
111 void
112 ldap_charray_free( char **a )
114 char **p;
116 if ( a == NULL ) {
117 return;
120 for ( p = a; *p != NULL; p++ ) {
121 if ( *p != NULL ) {
122 LDAP_FREE( *p );
126 LDAP_FREE( (char *) a );
130 ldap_charray_inlist(
131 char **a,
132 const char *s
135 int i;
137 if( a == NULL ) return 0;
139 for ( i=0; a[i] != NULL; i++ ) {
140 if ( strcasecmp( s, a[i] ) == 0 ) {
141 return 1;
145 return 0;
148 char **
149 ldap_charray_dup( char **a )
151 int i;
152 char **new;
154 for ( i = 0; a[i] != NULL; i++ )
155 ; /* NULL */
157 new = (char **) LDAP_MALLOC( (i + 1) * sizeof(char *) );
159 if( new == NULL ) {
160 return NULL;
163 for ( i = 0; a[i] != NULL; i++ ) {
164 new[i] = LDAP_STRDUP( a[i] );
166 if( new[i] == NULL ) {
167 for( --i ; i >= 0 ; i-- ) {
168 LDAP_FREE( new[i] );
170 LDAP_FREE( new );
171 return NULL;
174 new[i] = NULL;
176 return( new );
179 char **
180 ldap_str2charray( const char *str_in, const char *brkstr )
182 char **res;
183 char *str, *s;
184 char *lasts;
185 int i;
187 /* protect the input string from strtok */
188 str = LDAP_STRDUP( str_in );
189 if( str == NULL ) {
190 return NULL;
193 i = 1;
194 for ( s = str; ; LDAP_UTF8_INCR(s) ) {
195 s = ldap_utf8_strpbrk( s, brkstr );
196 if ( !s ) break;
197 i++;
200 res = (char **) LDAP_MALLOC( (i + 1) * sizeof(char *) );
202 if( res == NULL ) {
203 LDAP_FREE( str );
204 return NULL;
207 i = 0;
209 for ( s = ldap_utf8_strtok( str, brkstr, &lasts );
210 s != NULL;
211 s = ldap_utf8_strtok( NULL, brkstr, &lasts ) )
213 res[i] = LDAP_STRDUP( s );
215 if(res[i] == NULL) {
216 for( --i ; i >= 0 ; i-- ) {
217 LDAP_FREE( res[i] );
219 LDAP_FREE( res );
220 LDAP_FREE( str );
221 return NULL;
224 i++;
227 res[i] = NULL;
229 LDAP_FREE( str );
230 return( res );
233 char * ldap_charray2str( char **a, const char *sep )
235 char *s, **v, *p;
236 int len;
237 int slen;
239 if( sep == NULL ) sep = " ";
241 slen = strlen( sep );
242 len = 0;
244 for ( v = a; *v != NULL; v++ ) {
245 len += strlen( *v ) + slen;
248 if ( len == 0 ) {
249 return NULL;
252 /* trim extra sep len */
253 len -= slen;
255 s = LDAP_MALLOC ( len + 1 );
257 if ( s == NULL ) {
258 return NULL;
261 p = s;
262 for ( v = a; *v != NULL; v++ ) {
263 if ( v != a ) {
264 strncpy( p, sep, slen );
265 p += slen;
268 len = strlen( *v );
269 memcpy( p, *v, len );
270 p += len;
273 *p = '\0';
274 return s;