From fc43959180e0ab748ba68fa4834e6913f2aba74e Mon Sep 17 00:00:00 2001 From: Christopher James Lahey Date: Fri, 7 Jul 2000 18:29:26 +0000 Subject: Added e_strsplit to work around a bug in g_strsplit. 2000-07-07 Christopher James Lahey * e-util.c, e-util.h: Added e_strsplit to work around a bug in g_strsplit. svn path=/trunk/; revision=3953 --- e-util/ChangeLog | 5 +++++ e-util/e-util.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ e-util/e-util.c-8611 | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ e-util/e-util.h | 4 ++++ e-util/e-util.h-29002 | 4 ++++ 5 files changed, 121 insertions(+) (limited to 'e-util') diff --git a/e-util/ChangeLog b/e-util/ChangeLog index 4a19bb7207..582155a13f 100644 --- a/e-util/ChangeLog +++ b/e-util/ChangeLog @@ -1,3 +1,8 @@ +2000-07-07 Christopher James Lahey + + * e-util.c, e-util.h: Added e_strsplit to work around a bug in + g_strsplit. + 2000-07-07 Christopher James Lahey * e-iterator.c, e-iterator.h, e-list-iterator.c, diff --git a/e-util/e-util.c b/e-util/e-util.c index 6cb261410d..3dea9282c3 100644 --- a/e-util/e-util.c +++ b/e-util/e-util.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "e-util.h" @@ -251,3 +252,56 @@ e_marshal_BOOL__OBJECT_DOUBLE_DOUBLE_BOOL (GtkObject * object, GTK_VALUE_BOOL (args[3]), func_data); } + +gchar** +e_strsplit (const gchar *string, + const gchar *delimiter, + gint max_tokens) +{ + GSList *string_list = NULL, *slist; + gchar **str_array, *s; + guint i, n = 1; + + g_return_val_if_fail (string != NULL, NULL); + g_return_val_if_fail (delimiter != NULL, NULL); + + if (max_tokens < 1) + max_tokens = G_MAXINT; + + s = strstr (string, delimiter); + if (s) + { + guint delimiter_len = strlen (delimiter); + + do + { + guint len; + gchar *new_string; + + len = s - string; + new_string = g_new (gchar, len + 1); + strncpy (new_string, string, len); + new_string[len] = 0; + string_list = g_slist_prepend (string_list, new_string); + n++; + string = s + delimiter_len; + s = strstr (string, delimiter); + } + while (--max_tokens && s); + } + + n++; + string_list = g_slist_prepend (string_list, g_strdup (string)); + + str_array = g_new (gchar*, n); + + i = n - 1; + + str_array[i--] = NULL; + for (slist = string_list; slist; slist = slist->next) + str_array[i--] = slist->data; + + g_slist_free (string_list); + + return str_array; +} diff --git a/e-util/e-util.c-8611 b/e-util/e-util.c-8611 index 6cb261410d..3dea9282c3 100644 --- a/e-util/e-util.c-8611 +++ b/e-util/e-util.c-8611 @@ -26,6 +26,7 @@ #include #include #include +#include #include "e-util.h" @@ -251,3 +252,56 @@ e_marshal_BOOL__OBJECT_DOUBLE_DOUBLE_BOOL (GtkObject * object, GTK_VALUE_BOOL (args[3]), func_data); } + +gchar** +e_strsplit (const gchar *string, + const gchar *delimiter, + gint max_tokens) +{ + GSList *string_list = NULL, *slist; + gchar **str_array, *s; + guint i, n = 1; + + g_return_val_if_fail (string != NULL, NULL); + g_return_val_if_fail (delimiter != NULL, NULL); + + if (max_tokens < 1) + max_tokens = G_MAXINT; + + s = strstr (string, delimiter); + if (s) + { + guint delimiter_len = strlen (delimiter); + + do + { + guint len; + gchar *new_string; + + len = s - string; + new_string = g_new (gchar, len + 1); + strncpy (new_string, string, len); + new_string[len] = 0; + string_list = g_slist_prepend (string_list, new_string); + n++; + string = s + delimiter_len; + s = strstr (string, delimiter); + } + while (--max_tokens && s); + } + + n++; + string_list = g_slist_prepend (string_list, g_strdup (string)); + + str_array = g_new (gchar*, n); + + i = n - 1; + + str_array[i--] = NULL; + for (slist = string_list; slist; slist = slist->next) + str_array[i--] = slist->data; + + g_slist_free (string_list); + + return str_array; +} diff --git a/e-util/e-util.h b/e-util/e-util.h index 3df503f2d9..ab5325806b 100644 --- a/e-util/e-util.h +++ b/e-util/e-util.h @@ -42,6 +42,10 @@ void e_free_string_list (GList *list); char *e_read_file (const char *filename); gint e_write_file(const char *filename, const char *data, int flags); +gchar **e_strsplit (const gchar *string, + const gchar *delimiter, + gint max_tokens); + void e_marshal_INT__INT_INT_POINTER (GtkObject * object, GtkSignalFunc func, gpointer func_data, GtkArg * args); diff --git a/e-util/e-util.h-29002 b/e-util/e-util.h-29002 index 3df503f2d9..ab5325806b 100644 --- a/e-util/e-util.h-29002 +++ b/e-util/e-util.h-29002 @@ -42,6 +42,10 @@ void e_free_string_list (GList *list); char *e_read_file (const char *filename); gint e_write_file(const char *filename, const char *data, int flags); +gchar **e_strsplit (const gchar *string, + const gchar *delimiter, + gint max_tokens); + void e_marshal_INT__INT_INT_POINTER (GtkObject * object, GtkSignalFunc func, gpointer func_data, GtkArg * args); -- cgit v1.2.3