[glib/wip/gsubprocess-ostreams: 7/9] Add g_ptr_array_addv()
- From: Colin Walters <walters src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/wip/gsubprocess-ostreams: 7/9] Add g_ptr_array_addv()
- Date: Wed, 25 Jul 2012 23:24:18 +0000 (UTC)
commit 1c0030b6fc52a7538fe0b0216f6f6a59cdf58a40
Author: Colin Walters <walters verbum org>
Date: Wed Jul 25 08:13:30 2012 -0400
Add g_ptr_array_addv()
Constructing a GStrv (char**) from a partially static set occurs in
various circumstances; in particular, the argument vector for
g_spawn_async_with_pipes() or the forthcoming GSubprocess class.
glib/garray.c | 26 ++++++++++++++++++++++++++
glib/garray.h | 2 ++
glib/tests/array-test.c | 17 +++++++++++++++++
3 files changed, 45 insertions(+), 0 deletions(-)
---
diff --git a/glib/garray.c b/glib/garray.c
index 0cb3713..6e90cf5 100644
--- a/glib/garray.c
+++ b/glib/garray.c
@@ -1353,6 +1353,32 @@ g_ptr_array_add (GPtrArray *farray,
}
/**
+ * g_ptr_array_addv:
+ * @array: a #GPtrArray.
+ * @...: List of strings, %NULL terminated
+ *
+ * Adds all pointer arguments to the end of the pointer array. The
+ * array will grow in size automatically if necessary.
+ **/
+void
+g_ptr_array_addv (GPtrArray *farray,
+ ...)
+{
+ GRealPtrArray* array = (GRealPtrArray*) farray;
+ va_list args;
+ gpointer arg;
+
+ g_return_if_fail (array);
+
+ va_start (args, farray);
+
+ while ((arg = va_arg (args, gpointer)) != NULL)
+ g_ptr_array_add (farray, arg);
+
+ va_end (args);
+}
+
+/**
* g_ptr_array_sort:
* @array: a #GPtrArray.
* @compare_func: comparison function.
diff --git a/glib/garray.h b/glib/garray.h
index eb37b92..38741a0 100644
--- a/glib/garray.h
+++ b/glib/garray.h
@@ -138,6 +138,8 @@ void g_ptr_array_remove_range (GPtrArray *array,
guint length);
void g_ptr_array_add (GPtrArray *array,
gpointer data);
+void g_ptr_array_addv (GPtrArray *array,
+ ...) G_GNUC_NULL_TERMINATED;
void g_ptr_array_sort (GPtrArray *array,
GCompareFunc compare_func);
void g_ptr_array_sort_with_data (GPtrArray *array,
diff --git a/glib/tests/array-test.c b/glib/tests/array-test.c
index 5f9ccd5..5bcaf4b 100644
--- a/glib/tests/array-test.c
+++ b/glib/tests/array-test.c
@@ -367,6 +367,22 @@ pointer_array_add (void)
}
static void
+pointer_array_addv (void)
+{
+ GPtrArray *gparray;
+ gpointer *segment;
+
+ gparray = g_ptr_array_new ();
+
+ g_ptr_array_addv (gparray, "foo", "bar", "baz", NULL);
+ g_assert_cmpint (gparray->len, ==, 3);
+ g_ptr_array_addv (gparray, "hello", "world", NULL);
+ g_assert_cmpint (gparray->len, ==, 5);
+
+ g_ptr_array_free (gparray, TRUE);
+}
+
+static void
pointer_array_ref_count (void)
{
GPtrArray *gparray;
@@ -853,6 +869,7 @@ main (int argc, char *argv[])
/* pointer arrays */
g_test_add_func ("/pointerarray/add", pointer_array_add);
+ g_test_add_func ("/pointerarray/addv", pointer_array_addv);
g_test_add_func ("/pointerarray/ref-count", pointer_array_ref_count);
g_test_add_func ("/pointerarray/free-func", pointer_array_free_func);
g_test_add_func ("/pointerarray/sort", pointer_array_sort);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]