glib-compat: add g_hash_table_get_keys_as_array() compat function
This commit is contained in:
@@ -303,4 +303,43 @@ _g_key_file_save_to_file (GKeyFile *key_file,
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#if !GLIB_CHECK_VERSION(2, 40, 0) || defined (NM_GLIB_COMPAT_H_TEST)
|
||||||
|
static inline gpointer *
|
||||||
|
_nm_g_hash_table_get_keys_as_array (GHashTable *hash_table,
|
||||||
|
guint *length)
|
||||||
|
{
|
||||||
|
GHashTableIter iter;
|
||||||
|
gpointer key, *ret;
|
||||||
|
guint i = 0;
|
||||||
|
|
||||||
|
g_return_val_if_fail (hash_table, NULL);
|
||||||
|
|
||||||
|
ret = g_new0 (gpointer, g_hash_table_size (hash_table) + 1);
|
||||||
|
g_hash_table_iter_init (&iter, hash_table);
|
||||||
|
|
||||||
|
while (g_hash_table_iter_next (&iter, &key, NULL))
|
||||||
|
ret[i++] = key;
|
||||||
|
|
||||||
|
ret[i] = NULL;
|
||||||
|
|
||||||
|
if (length)
|
||||||
|
*length = i;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#if !GLIB_CHECK_VERSION(2, 40, 0)
|
||||||
|
#define g_hash_table_get_keys_as_array(hash_table, length) \
|
||||||
|
G_GNUC_EXTENSION ({ \
|
||||||
|
_nm_g_hash_table_get_keys_as_array (hash_table, length); \
|
||||||
|
})
|
||||||
|
#else
|
||||||
|
#define g_hash_table_get_keys_as_array(hash_table, length) \
|
||||||
|
G_GNUC_EXTENSION ({ \
|
||||||
|
G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
|
||||||
|
(g_hash_table_get_keys_as_array) ((hash_table), (length)); \
|
||||||
|
G_GNUC_END_IGNORE_DEPRECATIONS \
|
||||||
|
})
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif /* __NM_GLIB_H__ */
|
#endif /* __NM_GLIB_H__ */
|
||||||
|
@@ -4534,6 +4534,41 @@ test_g_ptr_array_insert (void)
|
|||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_g_hash_table_get_keys_as_array (void)
|
||||||
|
{
|
||||||
|
GHashTable *table = g_hash_table_new (g_str_hash, g_str_equal);
|
||||||
|
guint length;
|
||||||
|
char **keys;
|
||||||
|
|
||||||
|
g_hash_table_insert (table, "one", "1");
|
||||||
|
g_hash_table_insert (table, "two", "2");
|
||||||
|
g_hash_table_insert (table, "three", "3");
|
||||||
|
|
||||||
|
keys = (char **) _nm_g_hash_table_get_keys_as_array (table, &length);
|
||||||
|
g_assert (keys);
|
||||||
|
g_assert_cmpuint (length, ==, 3);
|
||||||
|
|
||||||
|
g_assert ( !strcmp (keys[0], "one")
|
||||||
|
|| !strcmp (keys[1], "one")
|
||||||
|
|| !strcmp (keys[2], "one"));
|
||||||
|
|
||||||
|
g_assert ( !strcmp (keys[0], "two")
|
||||||
|
|| !strcmp (keys[1], "two")
|
||||||
|
|| !strcmp (keys[2], "two"));
|
||||||
|
|
||||||
|
g_assert ( !strcmp (keys[0], "three")
|
||||||
|
|| !strcmp (keys[1], "three")
|
||||||
|
|| !strcmp (keys[2], "three"));
|
||||||
|
|
||||||
|
g_assert (!keys[3]);
|
||||||
|
|
||||||
|
g_free (keys);
|
||||||
|
g_hash_table_unref (table);
|
||||||
|
}
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
static int
|
static int
|
||||||
_test_find_binary_search_cmp (gconstpointer a, gconstpointer b, gpointer dummy)
|
_test_find_binary_search_cmp (gconstpointer a, gconstpointer b, gpointer dummy)
|
||||||
{
|
{
|
||||||
@@ -4903,6 +4938,7 @@ int main (int argc, char **argv)
|
|||||||
g_test_add_func ("/core/general/_nm_utils_ascii_str_to_int64", test_nm_utils_ascii_str_to_int64);
|
g_test_add_func ("/core/general/_nm_utils_ascii_str_to_int64", test_nm_utils_ascii_str_to_int64);
|
||||||
g_test_add_func ("/core/general/nm_utils_is_power_of_two", test_nm_utils_is_power_of_two);
|
g_test_add_func ("/core/general/nm_utils_is_power_of_two", test_nm_utils_is_power_of_two);
|
||||||
g_test_add_func ("/core/general/_glib_compat_g_ptr_array_insert", test_g_ptr_array_insert);
|
g_test_add_func ("/core/general/_glib_compat_g_ptr_array_insert", test_g_ptr_array_insert);
|
||||||
|
g_test_add_func ("/core/general/_glib_compat_g_hash_table_get_keys_as_array", test_g_hash_table_get_keys_as_array);
|
||||||
g_test_add_func ("/core/general/_nm_utils_ptrarray_find_binary_search", test_nm_utils_ptrarray_find_binary_search);
|
g_test_add_func ("/core/general/_nm_utils_ptrarray_find_binary_search", test_nm_utils_ptrarray_find_binary_search);
|
||||||
g_test_add_func ("/core/general/_nm_utils_strstrdictkey", test_nm_utils_strstrdictkey);
|
g_test_add_func ("/core/general/_nm_utils_strstrdictkey", test_nm_utils_strstrdictkey);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user