whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 3 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 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. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 7 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 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. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 11 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 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. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 18 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 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. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 22 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 24 | * Mountain View, CA 94043, or: |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 25 | * |
| 26 | * http://www.sgi.com |
| 27 | * |
| 28 | * For further information regarding this notice, see: |
| 29 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 30 | * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 31 | */ |
| 32 | /********************************************************** |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 33 | * |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 34 | * OS Testing - Silicon Graphics, Inc. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 35 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 36 | * FUNCTION NAME : string_to_tokens |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 37 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 38 | * FUNCTION TITLE : Break a string into its tokens |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 39 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 40 | * SYNOPSIS: |
| 41 | * |
| 42 | * int string_to_tokens(arg_string, arg_array, array_size, separator) |
| 43 | * char *arg_string; |
| 44 | * char *arg_array[]; |
| 45 | * int array_size; |
| 46 | * char *separator; |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 47 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 48 | * AUTHOR : Richard Logan |
| 49 | * |
| 50 | * DATE : 10/94 |
| 51 | * |
| 52 | * INITIAL RELEASE : UNICOS 7.0 |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 53 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 54 | * DESCRIPTION |
| 55 | * This function parses the string 'arg_string', placing pointers to |
| 56 | * the 'separator' separated tokens into the elements of 'arg_array'. |
| 57 | * The array is terminated with a null pointer. |
| 58 | * 'arg_array' must contains at least 'array_size' elements. |
| 59 | * Only the first 'array_size' minus one tokens will be placed into |
| 60 | * 'arg_array'. If there are more than 'array_size'-1 tokens, the rest are |
| 61 | * ignored by this routine. |
| 62 | * |
| 63 | * RETURN VALUE |
| 64 | * This function returns the number of 'separator' separated tokens that |
| 65 | * were found in 'arg_string'. |
| 66 | * If 'arg_array' or 'separator' is NULL or 'array_size' is less than 2, -1 is returned. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 67 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 68 | * WARNING |
| 69 | * This function uses strtok() to parse 'arg_string', and thus |
| 70 | * physically alters 'arg_string' by placing null characters where the |
| 71 | * separators originally were. |
| 72 | * |
| 73 | * |
| 74 | *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/ |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 75 | #include <stdio.h> |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 76 | #include <string.h> /* for string functions */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 77 | #include "string_to_tokens.h" |
| 78 | |
| 79 | int |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 80 | string_to_tokens(char *arg_string, char *arg_array[], int array_size, |
| 81 | char *separator) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 82 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 83 | int num_toks = 0; /* number of tokens found */ |
| 84 | char *strtok(); |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 85 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 86 | if (arg_array == NULL || array_size <= 1 || separator == NULL) |
| 87 | return -1; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 88 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 89 | /* |
| 90 | * Use strtok() to parse 'arg_string', placing pointers to the |
| 91 | * individual tokens into the elements of 'arg_array'. |
| 92 | */ |
| 93 | if ((arg_array[num_toks] = strtok(arg_string, separator)) == NULL) { |
| 94 | return 0; |
| 95 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 96 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 97 | for (num_toks = 1; num_toks < array_size; num_toks++) { |
| 98 | if ((arg_array[num_toks] = strtok(NULL, separator)) == NULL) |
| 99 | break; |
| 100 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 101 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 102 | if (num_toks == array_size) |
| 103 | arg_array[num_toks] = NULL; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 104 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 105 | /* |
| 106 | * Return the number of tokens that were found in 'arg_string'. |
| 107 | */ |
| 108 | return (num_toks); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 109 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 110 | } /* end of string_to_tokens */ |