From e5c1f207c95aadfc0a9a670e2295649efbde1aba Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Thu, 7 Dec 2000 04:48:37 +0000 Subject: [PATCH] add g_strdupv test 2000-12-06 Havoc Pennington * tests/strfunc-test.c (main): add g_strdupv test * gstrfuncs.c (g_strdupv): Add a function to copy an array of strings --- ChangeLog | 7 +++++++ ChangeLog.pre-2-0 | 7 +++++++ ChangeLog.pre-2-10 | 7 +++++++ ChangeLog.pre-2-12 | 7 +++++++ ChangeLog.pre-2-2 | 7 +++++++ ChangeLog.pre-2-4 | 7 +++++++ ChangeLog.pre-2-6 | 7 +++++++ ChangeLog.pre-2-8 | 7 +++++++ glib/gstrfuncs.c | 39 +++++++++++++++++++++++++++++++++++++++ glib/gstrfuncs.h | 2 ++ gstrfuncs.c | 39 +++++++++++++++++++++++++++++++++++++++ gstrfuncs.h | 2 ++ tests/strfunc-test.c | 14 ++++++++++++-- 13 files changed, 150 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9218492..5783b8d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2000-12-06 Havoc Pennington + + * tests/strfunc-test.c (main): add g_strdupv test + + * gstrfuncs.c (g_strdupv): Add a function to copy + an array of strings + Tue Dec 5 12:23:04 2000 Owen Taylor * gmain.[hc]: Major change in API for creating sources diff --git a/ChangeLog.pre-2-0 b/ChangeLog.pre-2-0 index 9218492..5783b8d 100644 --- a/ChangeLog.pre-2-0 +++ b/ChangeLog.pre-2-0 @@ -1,3 +1,10 @@ +2000-12-06 Havoc Pennington + + * tests/strfunc-test.c (main): add g_strdupv test + + * gstrfuncs.c (g_strdupv): Add a function to copy + an array of strings + Tue Dec 5 12:23:04 2000 Owen Taylor * gmain.[hc]: Major change in API for creating sources diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 9218492..5783b8d 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,10 @@ +2000-12-06 Havoc Pennington + + * tests/strfunc-test.c (main): add g_strdupv test + + * gstrfuncs.c (g_strdupv): Add a function to copy + an array of strings + Tue Dec 5 12:23:04 2000 Owen Taylor * gmain.[hc]: Major change in API for creating sources diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 9218492..5783b8d 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,10 @@ +2000-12-06 Havoc Pennington + + * tests/strfunc-test.c (main): add g_strdupv test + + * gstrfuncs.c (g_strdupv): Add a function to copy + an array of strings + Tue Dec 5 12:23:04 2000 Owen Taylor * gmain.[hc]: Major change in API for creating sources diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2 index 9218492..5783b8d 100644 --- a/ChangeLog.pre-2-2 +++ b/ChangeLog.pre-2-2 @@ -1,3 +1,10 @@ +2000-12-06 Havoc Pennington + + * tests/strfunc-test.c (main): add g_strdupv test + + * gstrfuncs.c (g_strdupv): Add a function to copy + an array of strings + Tue Dec 5 12:23:04 2000 Owen Taylor * gmain.[hc]: Major change in API for creating sources diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index 9218492..5783b8d 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,10 @@ +2000-12-06 Havoc Pennington + + * tests/strfunc-test.c (main): add g_strdupv test + + * gstrfuncs.c (g_strdupv): Add a function to copy + an array of strings + Tue Dec 5 12:23:04 2000 Owen Taylor * gmain.[hc]: Major change in API for creating sources diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index 9218492..5783b8d 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,10 @@ +2000-12-06 Havoc Pennington + + * tests/strfunc-test.c (main): add g_strdupv test + + * gstrfuncs.c (g_strdupv): Add a function to copy + an array of strings + Tue Dec 5 12:23:04 2000 Owen Taylor * gmain.[hc]: Major change in API for creating sources diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index 9218492..5783b8d 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,10 @@ +2000-12-06 Havoc Pennington + + * tests/strfunc-test.c (main): add g_strdupv test + + * gstrfuncs.c (g_strdupv): Add a function to copy + an array of strings + Tue Dec 5 12:23:04 2000 Owen Taylor * gmain.[hc]: Major change in API for creating sources diff --git a/glib/gstrfuncs.c b/glib/gstrfuncs.c index 69e19b1..792278a 100644 --- a/glib/gstrfuncs.c +++ b/glib/gstrfuncs.c @@ -1325,6 +1325,45 @@ g_strfreev (gchar **str_array) } } +/** + * g_strdupv: + * @str_array: %NULL-terminated array of strings + * + * Copies %NULL-terminated array of strings. The copy is a deep copy; + * the new array should be freed by first freeing each string, then + * the array itself. g_strfreev() does this for you. If called + * on a %NULL value, g_strdupv() simply returns %NULL. + * + * Return value: a new %NULL-terminated array of strings + **/ +gchar** +g_strdupv (gchar **str_array) +{ + if (str_array) + { + gint i; + gchar **retval; + + i = 0; + while (str_array[i]) + ++i; + + retval = g_new (gchar*, i + 1); + + i = 0; + while (str_array[i]) + { + retval[i] = g_strdup (str_array[i]); + ++i; + } + retval[i] = NULL; + + return retval; + } + else + return NULL; +} + gchar* g_strjoinv (const gchar *separator, gchar **str_array) diff --git a/glib/gstrfuncs.h b/glib/gstrfuncs.h index 516c310..4d33272 100644 --- a/glib/gstrfuncs.h +++ b/glib/gstrfuncs.h @@ -109,6 +109,7 @@ gpointer g_memdup (gconstpointer mem, * g_strjoinv() concatenates all of str_array's strings, sliding in an * optional separator, the returned string is newly allocated. * g_strfreev() frees the array itself and all of its strings. + * g_strdupv() copies a NULL-terminated array of strings */ gchar** g_strsplit (const gchar *string, const gchar *delimiter, @@ -116,6 +117,7 @@ gchar** g_strsplit (const gchar *string, gchar* g_strjoinv (const gchar *separator, gchar **str_array); void g_strfreev (gchar **str_array); +gchar** g_strdupv (gchar **str_array); G_END_DECLS diff --git a/gstrfuncs.c b/gstrfuncs.c index 69e19b1..792278a 100644 --- a/gstrfuncs.c +++ b/gstrfuncs.c @@ -1325,6 +1325,45 @@ g_strfreev (gchar **str_array) } } +/** + * g_strdupv: + * @str_array: %NULL-terminated array of strings + * + * Copies %NULL-terminated array of strings. The copy is a deep copy; + * the new array should be freed by first freeing each string, then + * the array itself. g_strfreev() does this for you. If called + * on a %NULL value, g_strdupv() simply returns %NULL. + * + * Return value: a new %NULL-terminated array of strings + **/ +gchar** +g_strdupv (gchar **str_array) +{ + if (str_array) + { + gint i; + gchar **retval; + + i = 0; + while (str_array[i]) + ++i; + + retval = g_new (gchar*, i + 1); + + i = 0; + while (str_array[i]) + { + retval[i] = g_strdup (str_array[i]); + ++i; + } + retval[i] = NULL; + + return retval; + } + else + return NULL; +} + gchar* g_strjoinv (const gchar *separator, gchar **str_array) diff --git a/gstrfuncs.h b/gstrfuncs.h index 516c310..4d33272 100644 --- a/gstrfuncs.h +++ b/gstrfuncs.h @@ -109,6 +109,7 @@ gpointer g_memdup (gconstpointer mem, * g_strjoinv() concatenates all of str_array's strings, sliding in an * optional separator, the returned string is newly allocated. * g_strfreev() frees the array itself and all of its strings. + * g_strdupv() copies a NULL-terminated array of strings */ gchar** g_strsplit (const gchar *string, const gchar *delimiter, @@ -116,6 +117,7 @@ gchar** g_strsplit (const gchar *string, gchar* g_strjoinv (const gchar *separator, gchar **str_array); void g_strfreev (gchar **str_array); +gchar** g_strdupv (gchar **str_array); G_END_DECLS diff --git a/tests/strfunc-test.c b/tests/strfunc-test.c index 2ff7bf8..7c5cd05 100644 --- a/tests/strfunc-test.c +++ b/tests/strfunc-test.c @@ -60,7 +60,9 @@ main (int argc, char *argv[]) { gchar *string; - + gchar *vec[] = { "Foo", "Bar", NULL }; + gchar **copy; + g_assert (g_strcasecmp ("FroboZZ", "frobozz") == 0); g_assert (g_strcasecmp ("frobozz", "frobozz") == 0); g_assert (g_strcasecmp ("frobozz", "FROBOZZ") == 0); @@ -98,7 +100,7 @@ main (int argc, g_assert (string != NULL); g_assert (strcmp(string, "00021 test ") == 0); g_free (string); - + g_assert (strcmp (g_strcompress("abc\\\\\\\"\\b\\f\\n\\r\\t\\003\\177\\234\\313\\12345z"), "abc\\\"\b\f\n\r\t\003\177\234\313\12345z") == 0); @@ -109,6 +111,14 @@ main (int argc, "\b\f\001\002\003\004"), "abc\\\\\\\"\b\f\\n\\r\\t\003\\177\\234\\313") == 0); + copy = g_strdupv (vec); + g_assert (strcmp (copy[0], "Foo") == 0); + g_assert (strcmp (copy[1], "Bar") == 0); + g_assert (copy[2] == NULL); + g_strfreev (copy); + return 0; } + + -- 2.7.4