blob: 4706323a82060856c038e31bfda7b39657661d2b [file] [log] [blame]
nstrazf307d5f2000-09-14 21:54:44 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 *
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.
7 *
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.
11 *
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.
18 *
19 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080020 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
nstrazf307d5f2000-09-14 21:54:44 +000022 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 *
32 */
nstrazcd87d682000-09-21 20:42:31 +000033/* $Id: splitstr.c,v 1.2 2000/09/21 20:42:31 nstraz Exp $ */
nstrazf307d5f2000-09-14 21:54:44 +000034/*
nstrazf307d5f2000-09-14 21:54:44 +000035 * Synopsis
36 *
nstrazcd87d682000-09-21 20:42:31 +000037 * const char **splitstr(const char *str, const char *separator, int *argcount)
nstrazf307d5f2000-09-14 21:54:44 +000038 *
39 * Description
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
nstrazcd87d682000-09-21 20:42:31 +000043 * 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.
nstrazf307d5f2000-09-14 21:54:44 +000046 *
nstrazcd87d682000-09-21 20:42:31 +000047 * To rid yourself of the memory allocated for splitstr(), pass the return
48 * value from splitstr() unmodified to splitstr_free():
nstrazf307d5f2000-09-14 21:54:44 +000049 *
nstrazcd87d682000-09-21 20:42:31 +000050 * void splitstr_free( const char ** return_from_splitstr );
51 *
52 */
nstrazf307d5f2000-09-14 21:54:44 +000053#include <stdio.h>
Garrett Cooper6ea8c5b2011-02-23 00:14:59 -080054#include <stdlib.h>
Wanlong Gao354ebb42012-12-07 10:10:04 +080055#include <string.h> /* for string functions */
nstrazcd87d682000-09-21 20:42:31 +000056#ifdef UNIT_TEST
57#include <assert.h>
58#endif /* UNIT_TEST */
59#include "splitstr.h"
nstrazf307d5f2000-09-14 21:54:44 +000060
Wanlong Gao354ebb42012-12-07 10:10:04 +080061const char **splitstr(const char *str, const char *separator, int *argcount)
nstrazf307d5f2000-09-14 21:54:44 +000062{
Wanlong Gao354ebb42012-12-07 10:10:04 +080063 char *arg_string = NULL, **arg_array = NULL, *cur_tok = NULL;
nstrazf307d5f2000-09-14 21:54:44 +000064
Wanlong Gao354ebb42012-12-07 10:10:04 +080065 int num_toks = 0, max_toks = 20, i;
nstrazcd87d682000-09-21 20:42:31 +000066
Wanlong Gao354ebb42012-12-07 10:10:04 +080067 /*
68 * In most recoverable errors, if argcount is not NULL,
69 * set argcount to 0. Then return NULL.
70 */
71 if (str == NULL) {
72 if (argcount != NULL)
73 *argcount = 0;
74 return (NULL);
75 }
nstrazcd87d682000-09-21 20:42:31 +000076
Wanlong Gao354ebb42012-12-07 10:10:04 +080077 /*
78 * set aside temporary space to work on the string.
79 */
80 arg_string = strdup(str);
nstrazcd87d682000-09-21 20:42:31 +000081
Wanlong Gao354ebb42012-12-07 10:10:04 +080082 if (arg_string == NULL) {
83 if (argcount != NULL)
84 *argcount = 0;
85 return (NULL);
86 }
nstrazcd87d682000-09-21 20:42:31 +000087
Wanlong Gao354ebb42012-12-07 10:10:04 +080088 /*
89 * set aside an initial char ** array for string array.
90 */
91 arg_array = (char **)malloc(sizeof(char *) * max_toks);
nstrazcd87d682000-09-21 20:42:31 +000092
Wanlong Gao354ebb42012-12-07 10:10:04 +080093 if (arg_array == NULL) {
94 if (argcount != NULL)
95 *argcount = 0;
96 return (NULL);
97 }
nstrazf307d5f2000-09-14 21:54:44 +000098
Wanlong Gao354ebb42012-12-07 10:10:04 +080099 if (separator == NULL)
100 separator = " \t";
nstrazf307d5f2000-09-14 21:54:44 +0000101
Wanlong Gao354ebb42012-12-07 10:10:04 +0800102 /*
103 * Use strtok() to parse 'arg_string', placing pointers to the
104 * individual tokens into the elements of 'arg_array'. Expand
105 * 'arg_array' if necessary.
106 */
107 cur_tok = strtok(arg_string, separator);
108 while (cur_tok != NULL) {
109 arg_array[num_toks++] = cur_tok;
110 cur_tok = strtok(NULL, separator);
111 if (num_toks == max_toks) {
112 max_toks += 20;
113 arg_array =
114 (char **)realloc((void *)arg_array,
115 sizeof(char *) * max_toks);
116 }
117 }
118 arg_array[num_toks] = NULL;
nstrazf307d5f2000-09-14 21:54:44 +0000119
Wanlong Gao354ebb42012-12-07 10:10:04 +0800120 /*
121 * If there are any spaces left in our array, make them NULL
122 */
123 for (i = num_toks + 1; i < max_toks; i++)
124 arg_array[i] = NULL;
nstrazcd87d682000-09-21 20:42:31 +0000125
Wanlong Gao354ebb42012-12-07 10:10:04 +0800126 /* This seems nice, but since memory is allocated on a page basis, this
127 * isn't really helpful:
128 * arg_array = (char **)realloc((void *)arg_array, sizeof(char *)*num_toks+1 );*/
nstrazcd87d682000-09-21 20:42:31 +0000129
Wanlong Gao354ebb42012-12-07 10:10:04 +0800130 if (argcount != NULL)
131 *argcount = num_toks;
nstrazf307d5f2000-09-14 21:54:44 +0000132
Wanlong Gao354ebb42012-12-07 10:10:04 +0800133 /*
134 * Return the argument array.
135 */
136 return ((const char **)arg_array);
nstrazf307d5f2000-09-14 21:54:44 +0000137}
nstrazcd87d682000-09-21 20:42:31 +0000138
139/*
140 * splitster_free( const char ** )
141 *
142 * This takes the return value from splitster() and free()s memory
143 * allocated by splitster. Assuming: ret=splitster(...), this
144 * requires that ret and *ret returned from splitster() have not
145 * been modified.
146 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800147void splitstr_free(const char **p_return)
nstrazcd87d682000-09-21 20:42:31 +0000148{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800149 if (*p_return != NULL)
150 free((char *)*p_return);
151 if (p_return != NULL)
152 free((char **)p_return);
nstrazcd87d682000-09-21 20:42:31 +0000153}
154
155#ifdef UNIT_TEST
156
157int main()
158{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800159 int i, y, test_size = 1000, size_ret;
160 char test_str[32768];
161 char buf[16];
162 char *test_str_array[test_size];
163 const char **ret;
nstrazcd87d682000-09-21 20:42:31 +0000164
Wanlong Gao354ebb42012-12-07 10:10:04 +0800165 for (i = 0; i < test_size; i++) {
166 snprintf(buf, 16, "arg%d", i);
167 test_str_array[i] = strdup(buf);
168 }
nstrazcd87d682000-09-21 20:42:31 +0000169
Wanlong Gao354ebb42012-12-07 10:10:04 +0800170 for (i = 0; i < test_size; i++) {
171 test_str[0] = '\0';
172 for (y = 0; y < i; y++) {
173 snprintf(buf, 16, "arg%d ", y);
174 strncat(test_str, buf, 16);
175 }
176 ret = splitstr(test_str, NULL, &size_ret);
177 assert(size_ret == i);
178 for (y = 0; y < i; y++)
179 assert(strcmp(ret[y], test_str_array[y]) == 0);
nstrazcd87d682000-09-21 20:42:31 +0000180
Wanlong Gao354ebb42012-12-07 10:10:04 +0800181 splitstr_free(ret);
182 }
183 return 0;
nstrazcd87d682000-09-21 20:42:31 +0000184}
185
Garrett Cooper6ea8c5b2011-02-23 00:14:59 -0800186#endif