shared: add nm_utils_strv_make_deep_copied() helper

At several places we create strv arrays where the
strings themself are not deep-copied.

This helper function iterates over such an "const char **"
array, clones the strings, and updates the strv array
inplace to be a "char **" strv array.

This helper function is to reduce code duplication.
This commit is contained in:
Thomas Haller 2017-12-10 12:18:21 +01:00
parent fe7b4641d6
commit d5c212b145
2 changed files with 25 additions and 0 deletions

View file

@ -1175,3 +1175,20 @@ nm_utils_strdict_get_keys (const GHashTable *hash,
NM_SET_OUT (out_length, length);
return names;
}
char **
nm_utils_strv_make_deep_copied (const char **strv)
{
gsize i;
/* it takes a strv dictionary, and copies each
* strings. Note that this updates @strv *in-place*
* and returns it. */
if (!strv)
return NULL;
for (i = 0; strv[i]; i++)
strv[i] = g_strdup (strv[i]);
return (char **) strv;
}

View file

@ -447,6 +447,14 @@ const char **nm_utils_strdict_get_keys (const GHashTable *hash,
gboolean sorted,
guint *out_length);
char **nm_utils_strv_make_deep_copied (const char **strv);
static inline char **
nm_utils_strv_make_deep_copied_nonnull (const char **strv)
{
return nm_utils_strv_make_deep_copied (strv) ?: g_new0 (char *, 1);
}
/*****************************************************************************/
#define NM_UTILS_NS_PER_SECOND ((gint64) 1000000000)