mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-03-22 18:30:37 +01:00
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:
parent
fe7b4641d6
commit
d5c212b145
2 changed files with 25 additions and 0 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue