From: Alexander Larsson Date: Tue, 23 May 2023 07:26:26 +0000 (+0200) Subject: Add ot_keyfile_get_tristate_with_default() helper X-Git-Tag: archive/raspbian/2023.7-3+rpi1~1^2~9^2~1^2~29^2~13 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=02d24d2a3854185cb1f4174f04efa59f1c6d53e9;p=ostree.git Add ot_keyfile_get_tristate_with_default() helper This parses keys like yes/no/maybe. The introduced OtTristate type is compatible with the existing _OstreeFeatureSupport type. --- diff --git a/src/libotutil/ot-keyfile-utils.c b/src/libotutil/ot-keyfile-utils.c index 141b2f7a..a1250dc6 100644 --- a/src/libotutil/ot-keyfile-utils.c +++ b/src/libotutil/ot-keyfile-utils.c @@ -60,6 +60,50 @@ ot_keyfile_get_boolean_with_default (GKeyFile *keyfile, const char *section, con return TRUE; } +gboolean +ot_keyfile_get_tristate_with_default (GKeyFile *keyfile, const char *section, const char *value, + OtTristate default_value, OtTristate *out_tri, GError **error) +{ + g_return_val_if_fail (keyfile != NULL, FALSE); + g_return_val_if_fail (section != NULL, FALSE); + g_return_val_if_fail (value != NULL, FALSE); + + GError *temp_error = NULL; + g_autofree char *ret_value = g_key_file_get_value (keyfile, section, value, &temp_error); + if (temp_error) + { + if (is_notfound (temp_error)) + { + g_clear_error (&temp_error); + g_assert (ret_value == NULL); + *out_tri = default_value; + return TRUE; + } + + g_propagate_error (error, temp_error); + return FALSE; + } + + ret_value = g_strstrip (ret_value); + + if (strcmp (ret_value, "yes") == 0 || strcmp (ret_value, "true") == 0 + || strcmp (ret_value, "1") == 0) + *out_tri = OT_TRISTATE_YES; + else if (strcmp (ret_value, "no") == 0 || strcmp (ret_value, "false") == 0 + || strcmp (ret_value, "0") == 0) + *out_tri = OT_TRISTATE_NO; + else if (strcmp (ret_value, "maybe") == 0) + *out_tri = OT_TRISTATE_MAYBE; + else + { + g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE, + "Invalid tri-state value: %s", ret_value); + return FALSE; + } + + return TRUE; +} + gboolean ot_keyfile_get_value_with_default (GKeyFile *keyfile, const char *section, const char *value, const char *default_value, char **out_value, GError **error) diff --git a/src/libotutil/ot-keyfile-utils.h b/src/libotutil/ot-keyfile-utils.h index ae70f1c7..eb97c8d7 100644 --- a/src/libotutil/ot-keyfile-utils.h +++ b/src/libotutil/ot-keyfile-utils.h @@ -23,12 +23,23 @@ #include +typedef enum +{ + OT_TRISTATE_NO, + OT_TRISTATE_MAYBE, + OT_TRISTATE_YES, +} OtTristate; + G_BEGIN_DECLS gboolean ot_keyfile_get_boolean_with_default (GKeyFile *keyfile, const char *section, const char *value, gboolean default_value, gboolean *out_bool, GError **error); +gboolean ot_keyfile_get_tristate_with_default (GKeyFile *keyfile, const char *section, + const char *value, OtTristate default_value, + OtTristate *out_tri, GError **error); + gboolean ot_keyfile_get_value_with_default (GKeyFile *keyfile, const char *section, const char *value, const char *default_value, char **out_value, GError **error);