ifcfg-rh: add svGetValueEnum()

This commit is contained in:
Thomas Haller 2017-04-28 11:53:07 +02:00
parent e81bdf19fa
commit 7298798a64
2 changed files with 32 additions and 0 deletions

View file

@ -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

View file

@ -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 <key> equal to the value <value>.
* If <key> does not exist, and the <current> pointer is set, append
* the key=value pair after that line. Otherwise, prepend the pair