1 const char ssplit_rcs[] = "$Id: ssplit.c,v 1.1.1.1 2001/05/15 13:59:04 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.1.1.1 2001/05/15 13:59:04 oes
36 * Initial import of version 2.9.3 source tree
39 *********************************************************************/
54 const char ssplit_h_rcs[] = SSPLIT_H_VERSION;
56 /* Define this for lots of debugging information to stdout */
57 /* #define SSPLIT_VERBOSE */
60 /*********************************************************************
64 * Description : Debugging routine to spit info on stdout. Not very
65 * useful to the non-console based IJB compiles.
68 * 1 : v = an array of strings
69 * 2 : n = number of strings in `v' to dump to stdout
73 *********************************************************************/
74 static void print(char **v, int n)
77 printf("dump %d strings\n", n);
80 printf("%d '%s'\n", i, v[i]);
84 #endif /* def SSPLIT_VERBOSE */
87 /*********************************************************************
91 * Description : Split a string using deliminters in `c'. Results go
95 * 1 : s = string to split
96 * 2 : c = array of delimiters
97 * 3 : v[] = results vector (aka. array)
98 * 4 : n = number of usable slots in the vector (aka. array size)
99 * 5 : m = consecutive delimiters means multiple fields?
100 * 6 : l = ignore leading field separators?
102 * Returns : -1 => failure, else the number of fields put in `v'.
104 *********************************************************************/
105 int ssplit(char *s, char *c, char *v[], int n, int m, int l)
121 memset(t, '\0', sizeof(t));
123 p = (unsigned char *) c;
127 p = (unsigned char *) " \t"; /* default field separators */
132 t[*p++] = 1; /* separator */
135 t['\0'] = 2; /* terminator */
136 t['\n'] = 2; /* terminator */
138 p = (unsigned char *) s;
140 if (l)/* are we to skip leading separators ? */
142 while ((b = t[*p]) != 2)
154 x = (char **) zalloc((xsize) * sizeof(char *));
156 x[xi++] = (char *) p; /* first pointer is the beginning of string */
158 /* first pass: save pointers to the field separators */
159 while ((b = t[*p]) != 2)
161 if (b == 1) /* if the char is a separator ... */
163 *p++ = '\0'; /* null terminate the substring */
167 /* get another chunk */
168 int new_xsize = xsize + 256;
169 char **new_x = (char **)zalloc((new_xsize) * sizeof(char *));
171 for (i=0; i < xsize; i++)
180 x[xi++] = (char *) p; /* save pointer to beginning of next string */
187 *p = '\0'; /* null terminate the substring */
190 #ifdef SSPLIT_VERBOSE
193 print(x, xi); /* debugging */
195 #endif /* def SSPLIT_VERBOSE */
198 /* second pass: copy the relevant pointers to the output vector */
200 for (i=0 ; i < xi; i++)
204 /* there are NO null fields */
217 return(-1); /* overflow */
222 #ifdef SSPLIT_VERBOSE
225 print(v, vi); /* debugging */
227 #endif /* def SSPLIT_VERBOSE */