shared: add nm_utils_strdict_get_keys() helper

At various places we get the (string) keys of a GHashTable.
Add a helper function that does that, including an argument
for optional sorting.

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

View File

@@ -1148,3 +1148,30 @@ nm_utils_named_values_from_str_dict (GHashTable *hash, guint *out_len)
NM_SET_OUT (out_len, len);
return values;
}
const char **
nm_utils_strdict_get_keys (const GHashTable *hash,
gboolean sorted,
guint *out_length)
{
const char **names;
guint length;
if ( !hash
|| !g_hash_table_size ((GHashTable *) hash)) {
NM_SET_OUT (out_length, 0);
return NULL;
}
names = (const char **) g_hash_table_get_keys_as_array ((GHashTable *) hash, &length);
if ( sorted
&& length > 1) {
g_qsort_with_data (names,
length,
sizeof (char *),
nm_strcmp_p_with_data,
NULL);
}
NM_SET_OUT (out_length, length);
return names;
}

View File

@@ -443,6 +443,10 @@ typedef struct {
NMUtilsNamedValue *nm_utils_named_values_from_str_dict (GHashTable *hash, guint *out_len);
const char **nm_utils_strdict_get_keys (const GHashTable *hash,
gboolean sorted,
guint *out_length);
/*****************************************************************************/
#define NM_UTILS_NS_PER_SECOND ((gint64) 1000000000)