2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
33 /* $Id: splitstr.c,v 1.2 2000/09/21 20:42:31 nstraz Exp $ */
37 * const char **splitstr(const char *str, const char *separator, int *argcount)
40 * This function splits a string (str) into components that are separated by
41 * one or more of the characters in the (separator) string. An array of
42 * strings is returned, along with argcount being set to the number of strings
43 * found. Argcount can be NULL. There will always be a NULL element in the
44 * array after the last valid element. If an error occurs, NULL will be
45 * returned and argcount will be set to zero.
47 * To rid yourself of the memory allocated for splitstr(), pass the return
48 * value from splitstr() unmodified to splitstr_free():
50 * void splitstr_free( const char ** return_from_splitstr );
55 #include <string.h> /* for string functions */
58 #endif /* UNIT_TEST */
62 splitstr(const char *str
, const char *separator
, int *argcount
)
64 char *arg_string
=NULL
,
73 * In most recoverable errors, if argcount is not NULL,
74 * set argcount to 0. Then return NULL.
78 if ( argcount
!= NULL
)
84 * set aside temporary space to work on the string.
86 arg_string
= strdup( str
);
88 if ( arg_string
== NULL
)
90 if ( argcount
!= NULL
)
96 * set aside an initial char ** array for string array.
98 arg_array
= (char **)malloc( sizeof(char *) * max_toks
);
100 if ( arg_array
== NULL
)
102 if ( argcount
!= NULL
)
111 * Use strtok() to parse 'arg_string', placing pointers to the
112 * individual tokens into the elements of 'arg_array'. Expand
113 * 'arg_array' if necessary.
115 cur_tok
= strtok(arg_string
, separator
);
116 while ( cur_tok
!= NULL
)
118 arg_array
[num_toks
++] = cur_tok
;
119 cur_tok
= strtok(NULL
, separator
);
120 if ( num_toks
== max_toks
)
123 arg_array
= (char **)realloc((void *)arg_array
, sizeof(char *)*max_toks
);
126 arg_array
[num_toks
] = NULL
;
129 * If there are any spaces left in our array, make them NULL
131 for(i
=num_toks
+1;i
<max_toks
;i
++)
134 /* This seems nice, but since memory is allocated on a page basis, this
135 * isn't really helpful:
136 * arg_array = (char **)realloc((void *)arg_array, sizeof(char *)*num_toks+1 );*/
138 if ( argcount
!= NULL
)
139 *argcount
= num_toks
;
142 * Return the argument array.
144 return((const char **)arg_array
);
148 * splitster_free( const char ** )
150 * This takes the return value from splitster() and free()s memory
151 * allocated by splitster. Assuming: ret=splitster(...), this
152 * requires that ret and *ret returned from splitster() have not
155 void splitstr_free( const char **p_return
)
157 if ( *p_return
!= NULL
)
158 free( (char *)*p_return
);
159 if ( p_return
!= NULL
)
160 free( (char **)p_return
);
167 int i
,y
,test_size
=1000,size_ret
;
168 char test_str
[32768];
170 char *test_str_array
[test_size
];
173 for(i
=0;i
<test_size
;i
++)
175 snprintf(buf
,16,"arg%d",i
);
176 test_str_array
[i
] = strdup(buf
);
179 for(i
=0;i
<test_size
;i
++)
184 snprintf(buf
,16,"arg%d ",y
);
185 strncat(test_str
,buf
,16);
187 ret
= splitstr(test_str
,NULL
,&size_ret
);
188 assert(size_ret
== i
);
190 assert( strcmp(ret
[y
],test_str_array
[y
])==0 );