1 const char ssplit_rcs[] = "$Id: ssplit.c,v 1.1 2001/05/13 21:57:07 administrator Exp $";
2 /*********************************************************************
4 * File : $Source: /home/administrator/cvs/ijb/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.
36 *********************************************************************/
51 const char ssplit_h_rcs[] = SSPLIT_H_VERSION;
53 /* Define this for lots of debugging information to stdout */
54 /* #define SSPLIT_VERBOSE */
57 /*********************************************************************
61 * Description : Debugging routine to spit info on stdout. Not very
62 * useful to the non-console based IJB compiles.
65 * 1 : v = an array of strings
66 * 2 : n = number of strings in `v' to dump to stdout
70 *********************************************************************/
71 static void print(char **v, int n)
74 printf("dump %d strings\n", n);
77 printf("%d '%s'\n", i, v[i]);
81 #endif /* def SSPLIT_VERBOSE */
84 /*********************************************************************
88 * Description : Split a string using deliminters in `c'. Results go
92 * 1 : s = string to split
93 * 2 : c = array of delimiters
94 * 3 : v[] = results vector (aka. array)
95 * 4 : n = number of usable slots in the vector (aka. array size)
96 * 5 : m = consecutive delimiters means multiple fields?
97 * 6 : l = ignore leading field separators?
99 * Returns : -1 => failure, else the number of fields put in `v'.
101 *********************************************************************/
102 int ssplit(char *s, char *c, char *v[], int n, int m, int l)
118 memset(t, '\0', sizeof(t));
120 p = (unsigned char *) c;
124 p = (unsigned char *) " \t"; /* default field separators */
129 t[*p++] = 1; /* separator */
132 t['\0'] = 2; /* terminator */
133 t['\n'] = 2; /* terminator */
135 p = (unsigned char *) s;
137 if (l)/* are we to skip leading separators ? */
139 while ((b = t[*p]) != 2)
151 x = (char **) zalloc((xsize) * sizeof(char *));
153 x[xi++] = (char *) p; /* first pointer is the beginning of string */
155 /* first pass: save pointers to the field separators */
156 while ((b = t[*p]) != 2)
158 if (b == 1) /* if the char is a separator ... */
160 *p++ = '\0'; /* null terminate the substring */
164 /* get another chunk */
165 int new_xsize = xsize + 256;
166 char **new_x = (char **)zalloc((new_xsize) * sizeof(char *));
168 for (i=0; i < xsize; i++)
177 x[xi++] = (char *) p; /* save pointer to beginning of next string */
184 *p = '\0'; /* null terminate the substring */
187 #ifdef SSPLIT_VERBOSE
190 print(x, xi); /* debugging */
192 #endif /* def SSPLIT_VERBOSE */
195 /* second pass: copy the relevant pointers to the output vector */
197 for (i=0 ; i < xi; i++)
201 /* there are NO null fields */
214 return(-1); /* overflow */
219 #ifdef SSPLIT_VERBOSE
222 print(v, vi); /* debugging */
224 #endif /* def SSPLIT_VERBOSE */