libnm: add _nm_utils_ptrarray_find_first() utility function

This commit is contained in:
Thomas Haller 2015-05-12 11:29:29 +02:00
parent 0a3c1f5774
commit 3377cd7e18
2 changed files with 28 additions and 0 deletions

View file

@ -112,6 +112,8 @@ GPtrArray *_nm_utils_copy_array (const GPtrArray *array,
GDestroyNotify free_func);
GPtrArray *_nm_utils_copy_object_array (const GPtrArray *array);
gssize _nm_utils_ptrarray_find_first (gpointer *list, gssize len, gconstpointer needle);
gboolean _nm_utils_string_in_list (const char *str,
const char **valid_strings);

View file

@ -631,6 +631,32 @@ _nm_utils_copy_object_array (const GPtrArray *array)
return _nm_utils_copy_array (array, g_object_ref, g_object_unref);
}
/* have @list of type 'gpointer *' instead of 'gconstpointer *' to
* reduce the necessity for annoying const-casts. */
gssize
_nm_utils_ptrarray_find_first (gpointer *list, gssize len, gconstpointer needle)
{
gssize i;
if (len == 0)
return -1;
if (len > 0) {
g_return_val_if_fail (list, -1);
for (i = 0; i < len; i++) {
if (list[i] == needle)
return i;
}
} else {
g_return_val_if_fail (needle, -1);
for (i = 0; list && list[i]; i++) {
if (list[i] == needle)
return i;
}
}
return -1;
}
GVariant *
_nm_utils_bytes_to_dbus (const GValue *prop_value)
{