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 *********************************************************************/
44 /*********************************************************************
48 * Description : Split a string using delimiters in `delim'. Results
52 * 1 : str = string to split. Will be split in place
53 * (i.e. do not free until you've finished with vec,
54 * previous contents will be trashed by the call).
55 * 2 : delim = array of delimiters (if NULL, uses " \t").
56 * 3 : vec[] = results vector (aka. array) [out]
57 * 4 : vec_len = number of usable slots in the vector (aka. array size)
59 * Returns : -1 => Error: vec_len is too small to hold all the
60 * data, or str == NULL.
61 * >=0 => the number of fields put in `vec'.
62 * On error, vec and str may still have been overwritten.
64 *********************************************************************/
65 int ssplit(char *str, const char *delim, char *vec[], size_t vec_len)
67 unsigned char is_delim[256];
68 unsigned char char_type;
83 /* Build is_delim array */
85 memset(is_delim, '\0', sizeof(is_delim));
89 delim = " \t"; /* default field separators */
94 is_delim[(unsigned)(unsigned char)*delim++] = SEPARATOR;
97 is_delim[(unsigned)(unsigned char)'\0'] = TERMINATOR;
98 is_delim[(unsigned)(unsigned char)'\n'] = TERMINATOR;
103 /* Skip leading separators. XXX: Why do they matter? */
104 while (is_delim[(unsigned)(unsigned char)*str] == SEPARATOR)
109 /* The first pointer is the beginning of string */
110 if (is_delim[(unsigned)(unsigned char)*str] == WANTED)
113 * The first character in this field is not a
114 * delimiter or the end of string, so save it.
116 if (vec_count >= vec_len)
118 return(-1); /* overflow */
120 vec[vec_count++] = str;
123 while ((char_type = is_delim[(unsigned)(unsigned char)*str]) != TERMINATOR)
125 if (char_type == SEPARATOR)
127 /* the char is a separator */
129 /* null terminate the substring */
132 /* Check if we want to save this field */
133 if (is_delim[(unsigned)(unsigned char)*str] == WANTED)
136 * The first character in this field is not a
137 * delimiter or the end of string. So save it.
139 if (vec_count >= vec_len)
141 return(-1); /* overflow */
143 vec[vec_count++] = str;
151 /* null terminate the substring */