1 /*********************************************************************
3 * File : $Source: /cvsroot/ijbswa/current/ssplit.c,v $
5 * Purpose : A function to split a string at specified delimiters.
7 * Copyright : Written by and Copyright (C) 2001-2012 the
8 * Privoxy team. https://www.privoxy.org/
10 * Based on the Internet Junkbuster originally written
11 * by and Copyright (C) 1997 Anonymous Coders and
12 * Junkbusters Corporation. http://www.junkbusters.com
14 * This program is free software; you can redistribute it
15 * and/or modify it under the terms of the GNU General
16 * Public License as published by the Free Software
17 * Foundation; either version 2 of the License, or (at
18 * your option) any later version.
20 * This program is distributed in the hope that it will
21 * be useful, but WITHOUT ANY WARRANTY; without even the
22 * implied warranty of MERCHANTABILITY or FITNESS FOR A
23 * PARTICULAR PURPOSE. See the GNU General Public
24 * License for more details.
26 * The GNU General Public License should be included with
27 * this file. If not, you can view it at
28 * http://www.gnu.org/copyleft/gpl.html
29 * or write to the Free Software Foundation, Inc., 59
30 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 *********************************************************************/
45 /*********************************************************************
49 * Description : Split a string using delimiters in `delim'. Results
53 * 1 : str = string to split. Will be split in place
54 * (i.e. do not free until you've finished with vec,
55 * previous contents will be trashed by the call).
56 * 2 : delim = array of delimiters (if NULL, uses " \t").
57 * 3 : vec[] = results vector (aka. array) [out]
58 * 4 : vec_len = number of usable slots in the vector (aka. array size)
60 * Returns : -1 => Error: vec_len is too small to hold all the
61 * data, or str == NULL.
62 * >=0 => the number of fields put in `vec'.
63 * On error, vec and str may still have been overwritten.
65 *********************************************************************/
66 int ssplit(char *str, const char *delim, char *vec[], size_t vec_len)
68 unsigned char is_delim[256];
69 unsigned char char_type;
84 /* Build is_delim array */
86 memset(is_delim, '\0', sizeof(is_delim));
90 delim = " \t"; /* default field separators */
95 is_delim[(unsigned)(unsigned char)*delim++] = SEPARATOR;
98 is_delim[(unsigned)(unsigned char)'\0'] = TERMINATOR;
99 is_delim[(unsigned)(unsigned char)'\n'] = TERMINATOR;
104 /* Skip leading separators. XXX: Why do they matter? */
105 while (is_delim[(unsigned)(unsigned char)*str] == SEPARATOR)
110 /* The first pointer is the beginning of string */
111 if (is_delim[(unsigned)(unsigned char)*str] == WANTED)
114 * The first character in this field is not a
115 * delimiter or the end of string, so save it.
117 if (vec_count >= vec_len)
119 return(-1); /* overflow */
121 vec[vec_count++] = str;
124 while ((char_type = is_delim[(unsigned)(unsigned char)*str]) != TERMINATOR)
126 if (char_type == SEPARATOR)
128 /* the char is a separator */
130 /* null terminate the substring */
133 /* Check if we want to save this field */
134 if (is_delim[(unsigned)(unsigned char)*str] == WANTED)
137 * The first character in this field is not a
138 * delimiter or the end of string. So save it.
140 if (vec_count >= vec_len)
142 return(-1); /* overflow */
144 vec[vec_count++] = str;
152 /* null terminate the substring */
153 /* XXX: this shouldn't be necessary, so assert that it isn't. */
154 assert(*str == '\0');