Cosmetic fix: // -> block comment
[privoxy.git] / ssplit.c
1 const char ssplit_rcs[] = "$Id: ssplit.c,v 1.1.1.1 2001/05/15 13:59:04 oes Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/ssplit.c,v $
5  *
6  * Purpose     :  A function to split a string at specified deliminters.
7  *
8  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
9  *                IJBSWA team.  http://ijbswa.sourceforge.net
10  *
11  *                Based on the Internet Junkbuster originally written
12  *                by and Copyright (C) 1997 Anonymous Coders and 
13  *                Junkbusters Corporation.  http://www.junkbusters.com
14  *
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.
20  *
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.
26  *
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.
32  *
33  * Revisions   :
34  *    $Log: ssplit.c,v $
35  *    Revision 1.1.1.1  2001/05/15 13:59:04  oes
36  *    Initial import of version 2.9.3 source tree
37  *
38  *
39  *********************************************************************/
40 \f
41
42 #include "config.h"
43
44 #include <string.h>
45 #include <stdlib.h>
46
47 #ifdef _WIN32
48 #include <malloc.h>
49 #endif
50
51 #include "ssplit.h"
52 #include "miscutil.h"
53
54 const char ssplit_h_rcs[] = SSPLIT_H_VERSION;
55
56 /* Define this for lots of debugging information to stdout */
57 /* #define SSPLIT_VERBOSE */
58
59 #ifdef SSPLIT_VERBOSE
60 /*********************************************************************
61  *
62  * Function    :  print
63  *
64  * Description :  Debugging routine to spit info on stdout.  Not very
65  *                useful to the non-console based IJB compiles.
66  *
67  * Parameters  :
68  *          1  :  v = an array of strings
69  *          2  :  n = number of strings in `v' to dump to stdout
70  *
71  * Returns     :  N/A
72  *
73  *********************************************************************/
74 static void print(char **v, int n)
75 {
76    int i;
77    printf("dump %d strings\n", n);
78    for (i=0; i < n; i++)
79    {
80       printf("%d '%s'\n", i, v[i]);
81    }
82
83 }
84 #endif /* def SSPLIT_VERBOSE */
85
86
87 /*********************************************************************
88  *
89  * Function    :  ssplit
90  *
91  * Description :  Split a string using deliminters in `c'.  Results go
92  *                into `v'.
93  *
94  * Parameters  :
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?
101  *
102  * Returns     :  -1 => failure, else the number of fields put in `v'.
103  *
104  *********************************************************************/
105 int ssplit(char *s, char *c, char *v[], int n, int m, int l)
106 {
107    char t[256];
108    char **x = NULL;
109    int xsize = 0;
110    unsigned char *p, b;
111    int xi = 0;
112    int vi = 0;
113    int i;
114    int last_was_null;
115
116    if (!s)
117    {
118       return(-1);
119    }
120
121    memset(t, '\0', sizeof(t));
122
123    p = (unsigned char *) c;
124
125    if (!p)
126    {
127       p = (unsigned char *) " \t";  /* default field separators */
128    }
129
130    while (*p)
131    {
132       t[*p++] = 1;   /* separator  */
133    }
134
135    t['\0'] = 2;   /* terminator */
136    t['\n'] = 2;   /* terminator */
137
138    p = (unsigned char *) s;
139
140    if (l)/* are we to skip leading separators ? */
141    {
142       while ((b = t[*p]) != 2)
143       {
144          if (b != 1)
145          {
146             break;
147          }
148          p++;
149       }
150    }
151
152    xsize = 256;
153
154    x = (char **) zalloc((xsize) * sizeof(char *));
155
156    x[xi++] = (char *) p;   /* first pointer is the beginning of string */
157
158    /* first pass:  save pointers to the field separators */
159    while ((b = t[*p]) != 2)
160    {
161       if (b == 1)    /* if the char is a separator ... */
162       {
163          *p++ = '\0';      /* null terminate the substring */
164
165          if (xi == xsize)
166          {
167             /* get another chunk */
168             int new_xsize = xsize + 256;
169             char **new_x = (char **)zalloc((new_xsize) * sizeof(char *));
170
171             for (i=0; i < xsize; i++)
172             {
173                new_x[i] = x[i];
174             }
175
176             free(x);
177             xsize = new_xsize;
178             x     = new_x;
179          }
180          x[xi++] = (char *) p;   /* save pointer to beginning of next string */
181       }
182       else
183       {
184          p++;
185       }
186    }
187    *p = '\0';     /* null terminate the substring */
188
189
190 #ifdef SSPLIT_VERBOSE
191    if (DEBUG(HDR))
192    {
193       print(x, xi); /* debugging */
194    }
195 #endif /* def SSPLIT_VERBOSE */
196
197
198    /* second pass: copy the relevant pointers to the output vector */
199    last_was_null = 0;
200    for (i=0 ; i < xi; i++)
201    {
202       if (m)
203       {
204          /* there are NO null fields */
205          if (*x[i] == 0)
206          {
207             continue;
208          }
209       }
210       if (vi < n)
211       {
212          v[vi++] = x[i];
213       }
214       else
215       {
216          free(x);
217          return(-1); /* overflow */
218       }
219    }
220    free(x);
221
222 #ifdef SSPLIT_VERBOSE
223    if (DEBUG(HDR))
224    {
225       print(v, vi); /* debugging  */
226    }
227 #endif /* def SSPLIT_VERBOSE */
228
229    return(vi);
230
231 }
232
233
234 /*
235   Local Variables:
236   tab-width: 3
237   end:
238 */