2 * String Split utility function
3 * Code borrowed from GTK2 to override the GTK1 version of g_strsplit, which is
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #if GLIB_MAJOR_VERSION < 2
31 gchar** ws_strsplit ( const gchar *string,
32 const gchar *delimiter,
35 GSList *string_list = NULL, *slist;
36 gchar **str_array, *s;
38 const gchar *remainder;
40 g_return_val_if_fail (string != NULL, NULL);
41 g_return_val_if_fail (delimiter != NULL, NULL);
42 g_return_val_if_fail (delimiter[0] != '\0', NULL);
45 max_tokens = G_MAXINT;
48 s = strstr (remainder, delimiter);
50 gsize delimiter_len = strlen (delimiter);
52 while (--max_tokens && s) {
57 new_string = g_new (gchar, len + 1);
58 strncpy (new_string, remainder, len);
60 string_list = g_slist_prepend (string_list, new_string);
62 remainder = s + delimiter_len;
63 s = strstr (remainder, delimiter);
68 string_list = g_slist_prepend (string_list, g_strdup (remainder));
71 str_array = g_new (gchar*, n + 1);
73 str_array[n--] = NULL;
74 for (slist = string_list; slist; slist = slist->next)
75 str_array[n--] = slist->data;
77 g_slist_free (string_list);
82 #endif /* GLIB_MAJOR_VERSION */