diff options
author | Thomas Haller <thaller@redhat.com> | 2017-11-21 13:25:57 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2017-11-21 13:48:49 +0100 |
commit | d5b3c6ee53cfa965ee0c219331aab9348bf80107 (patch) | |
tree | a9918972a2f94abc609cf6ec5abe37539ac30c5c | |
parent | 02d1ffa9ca83e46d07b929193648a0f82ebb2512 (diff) | |
download | NetworkManager-d5b3c6ee53cfa965ee0c219331aab9348bf80107.tar.gz |
libnm: sort entries in nm_setting_bond_get_option()
Since the order was arbitrary before, we can also sort it.
Also rework it, to avoid the creating a temporary GList of keys.
-rw-r--r-- | libnm-core/nm-setting-bond.c | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/libnm-core/nm-setting-bond.c b/libnm-core/nm-setting-bond.c index cf08c5b05c..8a15500c28 100644 --- a/libnm-core/nm-setting-bond.c +++ b/libnm-core/nm-setting-bond.c @@ -158,26 +158,34 @@ nm_setting_bond_get_option (NMSettingBond *setting, const char **out_value) { NMSettingBondPrivate *priv; - GList *keys; - const char *_key = NULL, *_value = NULL; + guint i, len; + gs_free NMUtilsNamedValue *options = NULL; + GHashTableIter iter; + const char *key, *value; g_return_val_if_fail (NM_IS_SETTING_BOND (setting), FALSE); priv = NM_SETTING_BOND_GET_PRIVATE (setting); - if (idx >= nm_setting_bond_get_num_options (setting)) + len = g_hash_table_size (priv->options); + if (idx >= len) return FALSE; - keys = g_hash_table_get_keys (priv->options); - _key = g_list_nth_data (keys, idx); - _value = g_hash_table_lookup (priv->options, _key); + i = 0; + options = g_new (NMUtilsNamedValue, len); + g_hash_table_iter_init (&iter, priv->options); + while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value)) { + options[i].name = key; + options[i].value_str = value; + i++; + } + nm_assert (i == len); - if (out_name) - *out_name = _key; - if (out_value) - *out_value = _value; + g_qsort_with_data (options, len, sizeof (options[0]), + nm_utils_named_entry_cmp_with_data, NULL); - g_list_free (keys); + NM_SET_OUT (out_name, options[idx].name); + NM_SET_OUT (out_value, options[idx].value_str); return TRUE; } |