diff options
author | Thomas Haller <thaller@redhat.com> | 2020-05-06 13:45:03 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2020-05-06 15:19:27 +0200 |
commit | 5056e0d3c805d0077b68aef32eb4470db6ebe5ac (patch) | |
tree | 2e9f5d9b0d9ad2231390fbc45e3459dea8859775 /shared | |
parent | 8dd74fe3648b73f812dcd04168730a08a246987e (diff) | |
download | NetworkManager-5056e0d3c805d0077b68aef32eb4470db6ebe5ac.tar.gz |
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.
Diffstat (limited to 'shared')
-rw-r--r-- | shared/nm-glib-aux/nm-shared-utils.h | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/shared/nm-glib-aux/nm-shared-utils.h b/shared/nm-glib-aux/nm-shared-utils.h index ddad14e545..590059ec4e 100644 --- a/shared/nm-glib-aux/nm-shared-utils.h +++ b/shared/nm-glib-aux/nm-shared-utils.h @@ -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__ */ |