Add a inflateEnd() call to free up the stream in the re-init block.
[obnox/wireshark/wip.git] / epan / ws_strsplit.c
1 /* ws_strsplit.c
2  * String Split utility function
3  * Code borrowed from GTK2 to override the GTK1 version of g_strsplit, which is
4  * known to be buggy.
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
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.
16  *
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.
21  *
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.
25  */
26
27 #if GLIB_MAJOR_VERSION < 2
28 #include <glib.h>
29 #include <string.h>
30
31 gchar** ws_strsplit ( const gchar *string,
32                       const gchar *delimiter,
33                       gint max_tokens)
34 {
35   GSList *string_list = NULL, *slist;
36   gchar **str_array, *s;
37   guint n = 0;
38   const gchar *remainder;
39
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);
43
44   if (max_tokens < 1)
45     max_tokens = G_MAXINT;
46
47   remainder = string;
48   s = strstr (remainder, delimiter);
49   if (s) {
50     gsize delimiter_len = strlen (delimiter);
51
52     while (--max_tokens && s) {
53       gsize len;
54       gchar *new_string;
55
56       len = s - remainder;
57       new_string = g_new (gchar, len + 1);
58       strncpy (new_string, remainder, len);
59       new_string[len] = 0;
60       string_list = g_slist_prepend (string_list, new_string);
61       n++;
62       remainder = s + delimiter_len;
63       s = strstr (remainder, delimiter);
64     }
65   }
66   if (*string) {
67     n++;
68     string_list = g_slist_prepend (string_list, g_strdup (remainder));
69   }
70
71   str_array = g_new (gchar*, n + 1);
72
73   str_array[n--] = NULL;
74   for (slist = string_list; slist; slist = slist->next)
75     str_array[n--] = slist->data;
76
77   g_slist_free (string_list);
78
79   return str_array;
80 }
81
82 #endif /* GLIB_MAJOR_VERSION */