From 7298798a64fb5ae571b29610408bc28edce0aeae Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 28 Apr 2017 11:53:07 +0200 Subject: ifcfg-rh: add svGetValueEnum() --- src/settings/plugins/ifcfg-rh/shvar.c | 28 ++++++++++++++++++++++++++++ src/settings/plugins/ifcfg-rh/shvar.h | 4 ++++ 2 files changed, 32 insertions(+) diff --git a/src/settings/plugins/ifcfg-rh/shvar.c b/src/settings/plugins/ifcfg-rh/shvar.c index 76c8d7c926..cddf3daa72 100644 --- a/src/settings/plugins/ifcfg-rh/shvar.c +++ b/src/settings/plugins/ifcfg-rh/shvar.c @@ -1057,6 +1057,34 @@ svGetValueInt64 (shvarFile *s, const char *key, guint base, gint64 min, gint64 m return result; } +gboolean +svGetValueEnum (shvarFile *s, const char *key, + GType gtype, int *out_value, + GError **error) +{ + gs_free char *to_free = NULL; + const char *svalue; + gs_free char *err_token = NULL; + int value; + + svalue = _svGetValue (s, key, &to_free); + if (!svalue) { + /* don't touch out_value. The caller is supposed + * to initialize it with the default value. */ + return TRUE; + } + + if (!nm_utils_enum_from_str (gtype, svalue, &value, &err_token)) { + g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN, + "Invalid token \"%s\" in \"%s\" for %s", + err_token, svalue, key); + return FALSE; + } + + NM_SET_OUT (out_value, value); + return TRUE; +} + /*****************************************************************************/ /* Same as svSetValueStr() but it preserves empty @value -- contrary to diff --git a/src/settings/plugins/ifcfg-rh/shvar.h b/src/settings/plugins/ifcfg-rh/shvar.h index 7ad2bc8863..8654d73c75 100644 --- a/src/settings/plugins/ifcfg-rh/shvar.h +++ b/src/settings/plugins/ifcfg-rh/shvar.h @@ -64,6 +64,10 @@ gint svGetValueBoolean (shvarFile *s, const char *key, gint def); gint64 svGetValueInt64 (shvarFile *s, const char *key, guint base, gint64 min, gint64 max, gint64 fallback); +gboolean svGetValueEnum (shvarFile *s, const char *key, + GType gtype, int *out_value, + GError **error); + /* Set the variable equal to the value . * If does not exist, and the pointer is set, append * the key=value pair after that line. Otherwise, prepend the pair -- cgit v1.2.1