From 2f50c8d9157eb1ea8e3bbbc1b3a7a9a5c0d9a14f Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 27 Apr 2018 17:28:09 +0200 Subject: [PATCH] clients/common: add nmc_objects_sort_by_path() helper --- clients/common/nm-client-utils.c | 41 ++++++++++++++++++++++++++++++++ clients/common/nm-client-utils.h | 2 ++ 2 files changed, 43 insertions(+) diff --git a/clients/common/nm-client-utils.c b/clients/common/nm-client-utils.c index b4b4c1b9c4..f683c7ce81 100644 --- a/clients/common/nm-client-utils.c +++ b/clients/common/nm-client-utils.c @@ -26,6 +26,47 @@ #include "nm-device-bridge.h" #include "nm-device-team.h" +/*****************************************************************************/ + +static int +_nmc_objects_sort_by_path_cmp (gconstpointer pa, gconstpointer pb, gpointer user_data) +{ + NMObject *a = *((NMObject **) pa); + NMObject *b = *((NMObject **) pb); + + NM_CMP_SELF (a, b); + NM_CMP_RETURN (nm_utils_dbus_path_cmp (nm_object_get_path (a), + nm_object_get_path (b))); + return 0; +} + +const NMObject ** +nmc_objects_sort_by_path (const NMObject *const* objs, gssize len) +{ + const NMObject **arr; + gsize i, l; + + if (len < 0) + l = NM_PTRARRAY_LEN (objs); + else + l = len; + + arr = g_new (const NMObject *, l + 1); + for (i = 0; i < l; i++) + arr[i] = objs[i]; + arr[l] = NULL; + + if (l > 1) { + g_qsort_with_data (arr, + l, + sizeof (gpointer), + _nmc_objects_sort_by_path_cmp, + NULL); + } + return arr; +} + +/*****************************************************************************/ /* * Convert string to unsigned integer. * If required, the resulting number is checked to be in the range. diff --git a/clients/common/nm-client-utils.h b/clients/common/nm-client-utils.h index cd66276565..cc0b7330ac 100644 --- a/clients/common/nm-client-utils.h +++ b/clients/common/nm-client-utils.h @@ -30,6 +30,8 @@ typedef enum { NMC_TRI_STATE_UNKNOWN, } NMCTriStateValue; +const NMObject **nmc_objects_sort_by_path (const NMObject *const*objs, gssize len); + const char *nmc_string_is_valid (const char *input, const char **allowed, GError **error); gboolean nmc_string_to_uint (const char *str,