xref: /aosp_15_r20/external/ltp/pan/splitstr.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or modify it
5*49cdfc7eSAndroid Build Coastguard Worker  * under the terms of version 2 of the GNU General Public License as
6*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation.
7*49cdfc7eSAndroid Build Coastguard Worker  *
8*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful, but
9*49cdfc7eSAndroid Build Coastguard Worker  * WITHOUT ANY WARRANTY; without even the implied warranty of
10*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Further, this software is distributed without any warranty that it is
13*49cdfc7eSAndroid Build Coastguard Worker  * free of the rightful claim of any third person regarding infringement
14*49cdfc7eSAndroid Build Coastguard Worker  * or the like.  Any license provided herein, whether implied or
15*49cdfc7eSAndroid Build Coastguard Worker  * otherwise, applies only to this software file.  Patent licenses, if
16*49cdfc7eSAndroid Build Coastguard Worker  * any, provided herein do not apply to combinations of this program with
17*49cdfc7eSAndroid Build Coastguard Worker  * other software, or any other product whatsoever.
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License along
20*49cdfc7eSAndroid Build Coastguard Worker  * with this program; if not, write the Free Software Foundation, Inc.,
21*49cdfc7eSAndroid Build Coastguard Worker  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22*49cdfc7eSAndroid Build Coastguard Worker  *
23*49cdfc7eSAndroid Build Coastguard Worker  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24*49cdfc7eSAndroid Build Coastguard Worker  * Mountain View, CA  94043, or:
25*49cdfc7eSAndroid Build Coastguard Worker  *
26*49cdfc7eSAndroid Build Coastguard Worker  * http://www.sgi.com
27*49cdfc7eSAndroid Build Coastguard Worker  *
28*49cdfc7eSAndroid Build Coastguard Worker  * For further information regarding this notice, see:
29*49cdfc7eSAndroid Build Coastguard Worker  *
30*49cdfc7eSAndroid Build Coastguard Worker  * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31*49cdfc7eSAndroid Build Coastguard Worker  *
32*49cdfc7eSAndroid Build Coastguard Worker  */
33*49cdfc7eSAndroid Build Coastguard Worker /* $Id: splitstr.c,v 1.2 2000/09/21 20:42:31 nstraz Exp $ */
34*49cdfc7eSAndroid Build Coastguard Worker /*
35*49cdfc7eSAndroid Build Coastguard Worker  * Synopsis
36*49cdfc7eSAndroid Build Coastguard Worker  *
37*49cdfc7eSAndroid Build Coastguard Worker  * const char **splitstr(const char *str, const char *separator, int *argcount)
38*49cdfc7eSAndroid Build Coastguard Worker  *
39*49cdfc7eSAndroid Build Coastguard Worker  * Description
40*49cdfc7eSAndroid Build Coastguard Worker  * This function splits a string (str) into components that are separated by
41*49cdfc7eSAndroid Build Coastguard Worker  * one or more of the characters in the (separator) string.  An array of
42*49cdfc7eSAndroid Build Coastguard Worker  * strings is returned, along with argcount being set to the number of strings
43*49cdfc7eSAndroid Build Coastguard Worker  * found.  Argcount can be NULL.  There will always be a NULL element in the
44*49cdfc7eSAndroid Build Coastguard Worker  * array after the last valid element.  If an error occurs, NULL will be
45*49cdfc7eSAndroid Build Coastguard Worker  * returned and argcount will be set to zero.
46*49cdfc7eSAndroid Build Coastguard Worker  *
47*49cdfc7eSAndroid Build Coastguard Worker  * To rid yourself of the memory allocated for splitstr(), pass the return
48*49cdfc7eSAndroid Build Coastguard Worker  * value from splitstr() unmodified to splitstr_free():
49*49cdfc7eSAndroid Build Coastguard Worker  *
50*49cdfc7eSAndroid Build Coastguard Worker  * void splitstr_free( const char ** return_from_splitstr );
51*49cdfc7eSAndroid Build Coastguard Worker  *
52*49cdfc7eSAndroid Build Coastguard Worker  */
53*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
54*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
55*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>		/* for string functions */
56*49cdfc7eSAndroid Build Coastguard Worker #ifdef UNIT_TEST
57*49cdfc7eSAndroid Build Coastguard Worker #include <assert.h>
58*49cdfc7eSAndroid Build Coastguard Worker #endif /* UNIT_TEST */
59*49cdfc7eSAndroid Build Coastguard Worker #include "splitstr.h"
60*49cdfc7eSAndroid Build Coastguard Worker 
splitstr(const char * str,const char * separator,int * argcount)61*49cdfc7eSAndroid Build Coastguard Worker const char **splitstr(const char *str, const char *separator, int *argcount)
62*49cdfc7eSAndroid Build Coastguard Worker {
63*49cdfc7eSAndroid Build Coastguard Worker 	char *arg_string = NULL, **arg_array = NULL, *cur_tok = NULL;
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 	int num_toks = 0, max_toks = 20, i;
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	/*
68*49cdfc7eSAndroid Build Coastguard Worker 	 * In most recoverable errors, if argcount is not NULL,
69*49cdfc7eSAndroid Build Coastguard Worker 	 * set argcount to 0. Then return NULL.
70*49cdfc7eSAndroid Build Coastguard Worker 	 */
71*49cdfc7eSAndroid Build Coastguard Worker 	if (str == NULL) {
72*49cdfc7eSAndroid Build Coastguard Worker 		if (argcount != NULL)
73*49cdfc7eSAndroid Build Coastguard Worker 			*argcount = 0;
74*49cdfc7eSAndroid Build Coastguard Worker 		return (NULL);
75*49cdfc7eSAndroid Build Coastguard Worker 	}
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker 	/*
78*49cdfc7eSAndroid Build Coastguard Worker 	 * set aside temporary space to work on the string.
79*49cdfc7eSAndroid Build Coastguard Worker 	 */
80*49cdfc7eSAndroid Build Coastguard Worker 	arg_string = strdup(str);
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 	if (arg_string == NULL) {
83*49cdfc7eSAndroid Build Coastguard Worker 		if (argcount != NULL)
84*49cdfc7eSAndroid Build Coastguard Worker 			*argcount = 0;
85*49cdfc7eSAndroid Build Coastguard Worker 		return (NULL);
86*49cdfc7eSAndroid Build Coastguard Worker 	}
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	/*
89*49cdfc7eSAndroid Build Coastguard Worker 	 * set aside an initial char ** array for string array.
90*49cdfc7eSAndroid Build Coastguard Worker 	 */
91*49cdfc7eSAndroid Build Coastguard Worker 	arg_array = malloc(sizeof(char *) * max_toks);
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 	if (arg_array == NULL) {
94*49cdfc7eSAndroid Build Coastguard Worker 		if (argcount != NULL)
95*49cdfc7eSAndroid Build Coastguard Worker 			*argcount = 0;
96*49cdfc7eSAndroid Build Coastguard Worker 		free(arg_string);
97*49cdfc7eSAndroid Build Coastguard Worker 		return (NULL);
98*49cdfc7eSAndroid Build Coastguard Worker 	}
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker 	if (separator == NULL)
101*49cdfc7eSAndroid Build Coastguard Worker 		separator = " \t";
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 	/*
104*49cdfc7eSAndroid Build Coastguard Worker 	 * Use strtok() to parse 'arg_string', placing pointers to the
105*49cdfc7eSAndroid Build Coastguard Worker 	 * individual tokens into the elements of 'arg_array'.  Expand
106*49cdfc7eSAndroid Build Coastguard Worker 	 * 'arg_array' if necessary.
107*49cdfc7eSAndroid Build Coastguard Worker 	 */
108*49cdfc7eSAndroid Build Coastguard Worker 	cur_tok = strtok(arg_string, separator);
109*49cdfc7eSAndroid Build Coastguard Worker 	while (cur_tok != NULL) {
110*49cdfc7eSAndroid Build Coastguard Worker 		arg_array[num_toks++] = cur_tok;
111*49cdfc7eSAndroid Build Coastguard Worker 		cur_tok = strtok(NULL, separator);
112*49cdfc7eSAndroid Build Coastguard Worker 		if (num_toks == max_toks) {
113*49cdfc7eSAndroid Build Coastguard Worker 			max_toks += 20;
114*49cdfc7eSAndroid Build Coastguard Worker 			arg_array =
115*49cdfc7eSAndroid Build Coastguard Worker 			    (char **)realloc((void *)arg_array,
116*49cdfc7eSAndroid Build Coastguard Worker 					     sizeof(char *) * max_toks);
117*49cdfc7eSAndroid Build Coastguard Worker 			if (arg_array == NULL) {
118*49cdfc7eSAndroid Build Coastguard Worker 				fprintf(stderr, "realloc: New memory allocation failed \n");
119*49cdfc7eSAndroid Build Coastguard Worker 				free(arg_string);
120*49cdfc7eSAndroid Build Coastguard Worker 				exit(1);
121*49cdfc7eSAndroid Build Coastguard Worker 			}
122*49cdfc7eSAndroid Build Coastguard Worker 		}
123*49cdfc7eSAndroid Build Coastguard Worker 	}
124*49cdfc7eSAndroid Build Coastguard Worker 	arg_array[num_toks] = NULL;
125*49cdfc7eSAndroid Build Coastguard Worker 
126*49cdfc7eSAndroid Build Coastguard Worker 	/*
127*49cdfc7eSAndroid Build Coastguard Worker 	 * If there are any spaces left in our array, make them NULL
128*49cdfc7eSAndroid Build Coastguard Worker 	 */
129*49cdfc7eSAndroid Build Coastguard Worker 	for (i = num_toks + 1; i < max_toks; i++)
130*49cdfc7eSAndroid Build Coastguard Worker 		arg_array[i] = NULL;
131*49cdfc7eSAndroid Build Coastguard Worker 
132*49cdfc7eSAndroid Build Coastguard Worker 	/* This seems nice, but since memory is allocated on a page basis, this
133*49cdfc7eSAndroid Build Coastguard Worker 	 * isn't really helpful:
134*49cdfc7eSAndroid Build Coastguard Worker 	 * arg_array = (char **)realloc((void *)arg_array, sizeof(char *)*num_toks+1 );*/
135*49cdfc7eSAndroid Build Coastguard Worker 
136*49cdfc7eSAndroid Build Coastguard Worker 	if (argcount != NULL)
137*49cdfc7eSAndroid Build Coastguard Worker 		*argcount = num_toks;
138*49cdfc7eSAndroid Build Coastguard Worker 
139*49cdfc7eSAndroid Build Coastguard Worker 	/*
140*49cdfc7eSAndroid Build Coastguard Worker 	 * Return the argument array.
141*49cdfc7eSAndroid Build Coastguard Worker 	 */
142*49cdfc7eSAndroid Build Coastguard Worker 	return ((const char **)arg_array);
143*49cdfc7eSAndroid Build Coastguard Worker }
144*49cdfc7eSAndroid Build Coastguard Worker 
145*49cdfc7eSAndroid Build Coastguard Worker /*
146*49cdfc7eSAndroid Build Coastguard Worker  * splitster_free( const char ** )
147*49cdfc7eSAndroid Build Coastguard Worker  *
148*49cdfc7eSAndroid Build Coastguard Worker  * This takes the return value from splitster() and free()s memory
149*49cdfc7eSAndroid Build Coastguard Worker  * allocated by splitster.  Assuming: ret=splitster(...), this
150*49cdfc7eSAndroid Build Coastguard Worker  * requires that ret and *ret returned from splitster() have not
151*49cdfc7eSAndroid Build Coastguard Worker  * been modified.
152*49cdfc7eSAndroid Build Coastguard Worker  */
splitstr_free(const char ** p_return)153*49cdfc7eSAndroid Build Coastguard Worker void splitstr_free(const char **p_return)
154*49cdfc7eSAndroid Build Coastguard Worker {
155*49cdfc7eSAndroid Build Coastguard Worker 	if (*p_return != NULL)
156*49cdfc7eSAndroid Build Coastguard Worker 		free((char *)*p_return);
157*49cdfc7eSAndroid Build Coastguard Worker 	if (p_return != NULL)
158*49cdfc7eSAndroid Build Coastguard Worker 		free((char **)p_return);
159*49cdfc7eSAndroid Build Coastguard Worker }
160*49cdfc7eSAndroid Build Coastguard Worker 
161*49cdfc7eSAndroid Build Coastguard Worker #ifdef UNIT_TEST
162*49cdfc7eSAndroid Build Coastguard Worker 
main()163*49cdfc7eSAndroid Build Coastguard Worker int main()
164*49cdfc7eSAndroid Build Coastguard Worker {
165*49cdfc7eSAndroid Build Coastguard Worker 	int i, y, test_size = 1000, size_ret;
166*49cdfc7eSAndroid Build Coastguard Worker 	char test_str[32768];
167*49cdfc7eSAndroid Build Coastguard Worker 	char buf[16];
168*49cdfc7eSAndroid Build Coastguard Worker 	char *test_str_array[test_size];
169*49cdfc7eSAndroid Build Coastguard Worker 	const char **ret;
170*49cdfc7eSAndroid Build Coastguard Worker 
171*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < test_size; i++) {
172*49cdfc7eSAndroid Build Coastguard Worker 		snprintf(buf, 16, "arg%d", i);
173*49cdfc7eSAndroid Build Coastguard Worker 		test_str_array[i] = strdup(buf);
174*49cdfc7eSAndroid Build Coastguard Worker 	}
175*49cdfc7eSAndroid Build Coastguard Worker 
176*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < test_size; i++) {
177*49cdfc7eSAndroid Build Coastguard Worker 		test_str[0] = '\0';
178*49cdfc7eSAndroid Build Coastguard Worker 		for (y = 0; y < i; y++) {
179*49cdfc7eSAndroid Build Coastguard Worker 			snprintf(buf, 16, "arg%d ", y);
180*49cdfc7eSAndroid Build Coastguard Worker 			strncat(test_str, buf, 16);
181*49cdfc7eSAndroid Build Coastguard Worker 		}
182*49cdfc7eSAndroid Build Coastguard Worker 		ret = splitstr(test_str, NULL, &size_ret);
183*49cdfc7eSAndroid Build Coastguard Worker 		assert(size_ret == i);
184*49cdfc7eSAndroid Build Coastguard Worker 		for (y = 0; y < i; y++)
185*49cdfc7eSAndroid Build Coastguard Worker 			assert(strcmp(ret[y], test_str_array[y]) == 0);
186*49cdfc7eSAndroid Build Coastguard Worker 
187*49cdfc7eSAndroid Build Coastguard Worker 		splitstr_free(ret);
188*49cdfc7eSAndroid Build Coastguard Worker 	}
189*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
190*49cdfc7eSAndroid Build Coastguard Worker }
191*49cdfc7eSAndroid Build Coastguard Worker 
192*49cdfc7eSAndroid Build Coastguard Worker #endif
193