1 const char ssplit_rcs[] = "$Id: ssplit.c,v 1.2 2001/05/17 23:01:01 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/ssplit.c,v $
6 * Purpose : A function to split a string at specified deliminters.
8 * Copyright : Written by and Copyright (C) 2001 the SourceForge
9 * IJBSWA team. http://ijbswa.sourceforge.net
11 * Based on the Internet Junkbuster originally written
12 * by and Copyright (C) 1997 Anonymous Coders and
13 * Junkbusters Corporation. http://www.junkbusters.com
15 * This program is free software; you can redistribute it
16 * and/or modify it under the terms of the GNU General
17 * Public License as published by the Free Software
18 * Foundation; either version 2 of the License, or (at
19 * your option) any later version.
21 * This program is distributed in the hope that it will
22 * be useful, but WITHOUT ANY WARRANTY; without even the
23 * implied warranty of MERCHANTABILITY or FITNESS FOR A
24 * PARTICULAR PURPOSE. See the GNU General Public
25 * License for more details.
27 * The GNU General Public License should be included with
28 * this file. If not, you can view it at
29 * http://www.gnu.org/copyleft/gpl.html
30 * or write to the Free Software Foundation, Inc., 59
31 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 * Revision 1.2 2001/05/17 23:01:01 oes
36 * - Cleaned CRLF's from the sources and related files
38 * Revision 1.1.1.1 2001/05/15 13:59:04 oes
39 * Initial import of version 2.9.3 source tree
42 *********************************************************************/
57 const char ssplit_h_rcs[] = SSPLIT_H_VERSION;
59 /* Define this for lots of debugging information to stdout */
61 /* #define SSPLIT_VERBOSE 1 */
64 /*********************************************************************
68 * Description : Split a string using delimiters in `delim'. Results
72 * 1 : str = string to split. Will be split in place
73 * (i.e. do not free until you've finished with vec,
74 * previous contents will be trashed by the call).
75 * 2 : delim = array of delimiters (if NULL, uses " \t").
76 * 3 : vec[] = results vector (aka. array) [out]
77 * 4 : vec_len = number of usable slots in the vector (aka. array size)
78 * 5 : dont_save_empty_fields = zero if consecutive delimiters
79 * give a null output field(s), nonzero if they are just
80 * to be considered as single delimeter
81 * 6 : ignore_leading = nonzero to ignore leading field
84 * Returns : -1 => Error: vec_len is too small to hold all the
85 * data, or str == NULL.
86 * >=0 => the number of fields put in `vec'.
87 * On error, vec and str may still have been overwritten.
89 *********************************************************************/
90 int ssplit(char *str, const char *delim, char *vec[], int vec_len,
91 int dont_save_empty_fields, int ignore_leading)
93 unsigned char is_delim[256];
94 unsigned char char_type;
103 /* Build is_delim array */
105 memset(is_delim, '\0', sizeof(is_delim));
109 delim = " \t"; /* default field separators */
114 is_delim[(unsigned)(unsigned char)*delim++] = 1; /* separator */
117 is_delim[(unsigned)(unsigned char)'\0'] = 2; /* terminator */
118 is_delim[(unsigned)(unsigned char)'\n'] = 2; /* terminator */
125 /* skip leading separators */
126 while (is_delim[(unsigned)(unsigned char)*str] == 1)
132 /* first pointer is the beginning of string */
133 /* Check if we want to save this field */
134 if ( (!dont_save_empty_fields)
135 || (is_delim[(unsigned)(unsigned char)*str] == 0) )
138 * We want empty fields, or the first character in this
139 * field is not a delimiter or the end of string.
142 if (vec_count >= vec_len)
144 return(-1); /* overflow */
146 vec[vec_count++] = (char *) str;
149 while ((char_type = is_delim[(unsigned)(unsigned char)*str]) != 2)
153 /* the char is a separator */
155 /* null terminate the substring */
158 /* Check if we want to save this field */
159 if ( (!dont_save_empty_fields)
160 || (is_delim[(unsigned)(unsigned char)*str] == 0) )
163 * We want empty fields, or the first character in this
164 * field is not a delimiter or the end of string.
167 if (vec_count >= vec_len)
169 return(-1); /* overflow */
171 vec[vec_count++] = (char *) str;
179 *str = '\0'; /* null terminate the substring */
181 #ifdef SSPLIT_VERBOSE
184 printf("dump %d strings\n", vec_count);
185 for (i = 0; i < vec_count; i++)
187 printf("%d '%s'\n", i, vec[i]);
190 #endif /* def SSPLIT_VERBOSE */