shared: add nm_strvarray_*() helper API

GPtrArray does not support NULL terminating the pointer array. That
makes it cumbersome to use it for tracking a strv array. Add a few
helper functions nm_strvarray_*() that help using a GArray instead.
This commit is contained in:
Thomas Haller 2020-05-06 13:45:03 +02:00
parent 8dd74fe364
commit 5056e0d3c8
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -1905,4 +1905,52 @@ gboolean nm_utils_ifname_valid (const char* name,
NMUtilsIfaceType type,
GError **error);
/*****************************************************************************/
static inline GArray *
nm_strvarray_ensure (GArray **p)
{
if (!*p) {
*p = g_array_new (TRUE, FALSE, sizeof (char *));
g_array_set_clear_func (*p, nm_indirect_g_free);
}
return *p;
}
static inline void
nm_strvarray_add (GArray *array, const char *str)
{
char *s;
s = g_strdup (str);
g_array_append_val (array, s);
}
static inline const char *const*
nm_strvarray_get_strv (GArray **arr, guint *length)
{
if (!*arr) {
NM_SET_OUT (length, 0);
return (const char *const*) arr;
}
NM_SET_OUT (length, (*arr)->len);
return &g_array_index (*arr, const char *, 0);
}
static inline void
nm_strvarray_set_strv (GArray **array, const char *const*strv)
{
gs_unref_array GArray *array_old = NULL;
array_old = g_steal_pointer (array);
if (!strv || !strv[0])
return;
nm_strvarray_ensure (array);
for (; strv[0]; strv++)
nm_strvarray_add (*array, strv[0]);
}
#endif /* __NM_SHARED_UTILS_H__ */