whr | b973f2b | 2000-05-05 19:34:50 +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 |
| 20 | * with this program; if not, write the Free Software Foundation, Inc., 59 |
| 21 | * Temple Place - Suite 330, Boston MA 02111-1307, USA. |
| 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 | /********************************************************** |
| 33 | * |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 34 | * OS Testing - Silicon Graphics, Inc. |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 35 | * |
| 36 | * FUNCTION NAME : string_to_tokens |
| 37 | * |
| 38 | * FUNCTION TITLE : Break a string into its tokens |
| 39 | * |
| 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; |
| 47 | * |
| 48 | * AUTHOR : Richard Logan |
| 49 | * |
| 50 | * DATE : 10/94 |
| 51 | * |
| 52 | * INITIAL RELEASE : UNICOS 7.0 |
| 53 | * |
| 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. |
| 67 | * |
| 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 | *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/ |
| 75 | #include <stdio.h> |
| 76 | #include <string.h> /* for string functions */ |
| 77 | #include "string_to_tokens.h" |
| 78 | |
| 79 | int |
| 80 | string_to_tokens(char *arg_string, char *arg_array[], int array_size, char *separator) |
| 81 | { |
| 82 | int num_toks = 0; /* number of tokens found */ |
| 83 | char *strtok(); |
| 84 | |
| 85 | if ( arg_array == NULL || array_size <= 1 || separator == NULL ) |
| 86 | return -1; |
| 87 | |
| 88 | /* |
| 89 | * Use strtok() to parse 'arg_string', placing pointers to the |
| 90 | * individual tokens into the elements of 'arg_array'. |
| 91 | */ |
| 92 | if ( (arg_array[num_toks] = strtok(arg_string, separator)) == NULL ) { |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | for (num_toks=1;num_toks<array_size; num_toks++) { |
| 97 | if ( (arg_array[num_toks] = strtok(NULL, separator)) == NULL ) |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | if ( num_toks == array_size ) |
| 102 | arg_array[num_toks] = NULL; |
| 103 | |
| 104 | /* |
| 105 | * Return the number of tokens that were found in 'arg_string'. |
| 106 | */ |
| 107 | return(num_toks); |
| 108 | |
| 109 | } /* end of string_to_tokens */ |