Add ot_keyfile_get_tristate_with_default() helper
authorAlexander Larsson <alexl@redhat.com>
Tue, 23 May 2023 07:26:26 +0000 (09:26 +0200)
committerAlexander Larsson <alexl@redhat.com>
Wed, 31 May 2023 08:55:14 +0000 (10:55 +0200)
This parses keys like yes/no/maybe. The introduced OtTristate type
is compatible with the existing _OstreeFeatureSupport type.

src/libotutil/ot-keyfile-utils.c
src/libotutil/ot-keyfile-utils.h

index 141b2f7a6931fff6394cf5de3d6189f1b6144f4f..a1250dc64fde044c4aa3d3759d0d803ccd113f49 100644 (file)
@@ -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)
index ae70f1c7522c724f26ddeaefcb0fd645f09d51eb..eb97c8d7c615b261c8766347094af2002d8cbfa9 100644 (file)
 
 #include <gio/gio.h>
 
+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);