Remove two pointless casts in ssplit()
[privoxy.git] / ssplit.c
1 const char ssplit_rcs[] = "$Id: ssplit.c,v 1.16 2012/07/23 12:44:17 fabiankeil Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/ssplit.c,v $
5  *
6  * Purpose     :  A function to split a string at specified delimiters.
7  *
8  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
9  *                Privoxy team. http://www.privoxy.org/
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  *********************************************************************/
34
35
36 #include "config.h"
37
38 #include <string.h>
39 #include <stdlib.h>
40
41 #include "ssplit.h"
42 #include "miscutil.h"
43
44 const char ssplit_h_rcs[] = SSPLIT_H_VERSION;
45
46
47 /*********************************************************************
48  *
49  * Function    :  ssplit
50  *
51  * Description :  Split a string using delimiters in `delim'.  Results
52  *                go into `vec'.
53  *
54  * Parameters  :
55  *          1  :  str = string to split.  Will be split in place
56  *                (i.e. do not free until you've finished with vec,
57  *                previous contents will be trashed by the call).
58  *          2  :  delim = array of delimiters (if NULL, uses " \t").
59  *          3  :  vec[] = results vector (aka. array) [out]
60  *          4  :  vec_len = number of usable slots in the vector (aka. array size)
61  *
62  * Returns     :  -1 => Error: vec_len is too small to hold all the
63  *                      data, or str == NULL.
64  *                >=0 => the number of fields put in `vec'.
65  *                On error, vec and str may still have been overwritten.
66  *
67  *********************************************************************/
68 int ssplit(char *str, const char *delim, char *vec[], size_t vec_len)
69 {
70    unsigned char is_delim[256];
71    unsigned char char_type;
72    int vec_count = 0;
73
74    if (!str)
75    {
76       return(-1);
77    }
78
79
80    /* Build is_delim array */
81
82    memset(is_delim, '\0', sizeof(is_delim));
83
84    if (!delim)
85    {
86       delim = " \t";  /* default field separators */
87    }
88
89    while (*delim)
90    {
91       is_delim[(unsigned)(unsigned char)*delim++] = 1;   /* separator  */
92    }
93
94    is_delim[(unsigned)(unsigned char)'\0'] = 2;   /* terminator */
95    is_delim[(unsigned)(unsigned char)'\n'] = 2;   /* terminator */
96
97
98    /* Parse string */
99
100    /* Skip leading separators. XXX: Why do they matter? */
101    while (is_delim[(unsigned)(unsigned char)*str] == 1)
102    {
103       str++;
104    }
105
106    /* The first pointer is the beginning of string */
107    if (is_delim[(unsigned)(unsigned char)*str] == 0)
108    {
109       /*
110        * The first character in this field is not a
111        * delimiter or the end of string, so save it.
112        */
113       if (vec_count >= vec_len)
114       {
115          return(-1); /* overflow */
116       }
117       vec[vec_count++] = str;
118    }
119
120    while ((char_type = is_delim[(unsigned)(unsigned char)*str]) != 2)
121    {
122       if (char_type == 1)
123       {
124          /* the char is a separator */
125
126          /* null terminate the substring */
127          *str++ = '\0';
128
129          /* Check if we want to save this field */
130          if (is_delim[(unsigned)(unsigned char)*str] == 0)
131          {
132             /*
133              * The first character in this field is not a
134              * delimiter or the end of string. So save it.
135              */
136             if (vec_count >= vec_len)
137             {
138                return(-1); /* overflow */
139             }
140             vec[vec_count++] = str;
141          }
142       }
143       else
144       {
145          str++;
146       }
147    }
148    /* null terminate the substring */
149    *str = '\0';
150
151    return(vec_count);
152 }
153
154
155 /*
156   Local Variables:
157   tab-width: 3
158   end:
159 */