nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 20 | * with this program; if not, write the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 22 | * |
| 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 | */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 33 | /* $Id: splitstr.c,v 1.2 2000/09/21 20:42:31 nstraz Exp $ */ |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 34 | /* |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 35 | * Synopsis |
| 36 | * |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 37 | * const char **splitstr(const char *str, const char *separator, int *argcount) |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 38 | * |
| 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 |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 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. |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 46 | * |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 47 | * To rid yourself of the memory allocated for splitstr(), pass the return |
| 48 | * value from splitstr() unmodified to splitstr_free(): |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 49 | * |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 50 | * void splitstr_free( const char ** return_from_splitstr ); |
| 51 | * |
| 52 | */ |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 53 | #include <stdio.h> |
Garrett Cooper | 6ea8c5b | 2011-02-23 00:14:59 -0800 | [diff] [blame] | 54 | #include <stdlib.h> |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 55 | #include <string.h> /* for string functions */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 56 | #ifdef UNIT_TEST |
| 57 | #include <assert.h> |
| 58 | #endif /* UNIT_TEST */ |
| 59 | #include "splitstr.h" |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 60 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 61 | const char **splitstr(const char *str, const char *separator, int *argcount) |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 62 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 63 | char *arg_string = NULL, **arg_array = NULL, *cur_tok = NULL; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 64 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 65 | int num_toks = 0, max_toks = 20, i; |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 66 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 67 | /* |
| 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 | } |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 76 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 77 | /* |
| 78 | * set aside temporary space to work on the string. |
| 79 | */ |
| 80 | arg_string = strdup(str); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 81 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 82 | if (arg_string == NULL) { |
| 83 | if (argcount != NULL) |
| 84 | *argcount = 0; |
| 85 | return (NULL); |
| 86 | } |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 87 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 88 | /* |
| 89 | * set aside an initial char ** array for string array. |
| 90 | */ |
| 91 | arg_array = (char **)malloc(sizeof(char *) * max_toks); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 92 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 93 | if (arg_array == NULL) { |
| 94 | if (argcount != NULL) |
| 95 | *argcount = 0; |
| 96 | return (NULL); |
| 97 | } |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 98 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 99 | if (separator == NULL) |
| 100 | separator = " \t"; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 101 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 102 | /* |
| 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; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 119 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 120 | /* |
| 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; |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 125 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 126 | /* 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 );*/ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 129 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 130 | if (argcount != NULL) |
| 131 | *argcount = num_toks; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 132 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 133 | /* |
| 134 | * Return the argument array. |
| 135 | */ |
| 136 | return ((const char **)arg_array); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 137 | } |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 138 | |
| 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 147 | void splitstr_free(const char **p_return) |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 148 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 149 | if (*p_return != NULL) |
| 150 | free((char *)*p_return); |
| 151 | if (p_return != NULL) |
| 152 | free((char **)p_return); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | #ifdef UNIT_TEST |
| 156 | |
| 157 | int main() |
| 158 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 159 | 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; |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 164 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 165 | for (i = 0; i < test_size; i++) { |
| 166 | snprintf(buf, 16, "arg%d", i); |
| 167 | test_str_array[i] = strdup(buf); |
| 168 | } |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 169 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 170 | 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); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 180 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 181 | splitstr_free(ret); |
| 182 | } |
| 183 | return 0; |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Garrett Cooper | 6ea8c5b | 2011-02-23 00:14:59 -0800 | [diff] [blame] | 186 | #endif |