[retro-gtk/auto-goodness: 3/3] core-descriptor: Use auto cleanups and throws where possible



commit 2d4e4256f053b41887e7ba4b7cb5db2b05db9a11
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Mon Jan 27 10:30:56 2020 +0100

    core-descriptor: Use auto cleanups and throws where possible
    
    This makes the code simpler and safer.

 retro-gtk/retro-core-descriptor.c | 292 +++++++++-----------------------------
 1 file changed, 71 insertions(+), 221 deletions(-)
---
diff --git a/retro-gtk/retro-core-descriptor.c b/retro-gtk/retro-core-descriptor.c
index 1759e6c..59c5be5 100644
--- a/retro-gtk/retro-core-descriptor.c
+++ b/retro-gtk/retro-core-descriptor.c
@@ -2,6 +2,8 @@
 
 #include "retro-core-descriptor.h"
 
+#include "retro-error-private.h"
+
 struct _RetroCoreDescriptor
 {
   GObject parent_instance;
@@ -73,7 +75,7 @@ has_group_prefixed (RetroCoreDescriptor *self,
                     const gchar         *group_prefix,
                     const gchar         *group_suffix)
 {
-  gchar *group;
+  g_autofree gchar *group = NULL;
   gboolean result;
 
   g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), FALSE);
@@ -82,7 +84,6 @@ has_group_prefixed (RetroCoreDescriptor *self,
 
   group = g_strconcat (group_prefix, group_suffix, NULL);
   result = g_key_file_has_group (self->key_file, group);
-  g_free (group);
 
   return result;
 }
@@ -94,9 +95,9 @@ has_key_prefixed (RetroCoreDescriptor  *self,
                   const gchar          *key,
                   GError              **error)
 {
-  gchar *group;
+  g_autofree gchar *group = NULL;
   gboolean result;
-  GError *tmp_error = NULL;
+  g_autoptr (RetroError) tmp_error = NULL;
 
   g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), FALSE);
   g_return_val_if_fail (group_prefix != NULL, FALSE);
@@ -108,14 +109,7 @@ has_key_prefixed (RetroCoreDescriptor  *self,
                                group,
                                key,
                                &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_free (group);
-    g_propagate_error (error, tmp_error);
-
-    return FALSE;
-  }
-
-  g_free (group);
+  retro_throw_val_if_error (error, tmp_error, FALSE);
 
   return result;
 }
@@ -127,9 +121,9 @@ get_string_prefixed (RetroCoreDescriptor  *self,
                      const gchar          *key,
                      GError              **error)
 {
-  gchar *group;
-  gchar *result;
-  GError *tmp_error = NULL;
+  g_autofree gchar *group = NULL;
+  g_autofree gchar *result = NULL;
+  g_autoptr (RetroError) tmp_error = NULL;
 
   g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), NULL);
   g_return_val_if_fail (group_prefix != NULL, NULL);
@@ -142,16 +136,9 @@ get_string_prefixed (RetroCoreDescriptor  *self,
                                   group,
                                   key,
                                   &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_free (group);
-    g_propagate_error (error, tmp_error);
-
-    return NULL;
-  }
-
-  g_free (group);
+  retro_throw_val_if_error (error, tmp_error, NULL);
 
-  return result;
+  return g_steal_pointer (&result);
 }
 
 static gchar **
@@ -162,9 +149,9 @@ get_string_list_prefixed (RetroCoreDescriptor  *self,
                           gsize                *length,
                           GError              **error)
 {
-  gchar *group;
-  gchar **result;
-  GError *tmp_error = NULL;
+  g_autofree gchar *group = NULL;
+  g_auto (GStrv) result = NULL;
+  g_autoptr (RetroError) tmp_error = NULL;
 
   g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), NULL);
   g_return_val_if_fail (group_prefix != NULL, NULL);
@@ -178,14 +165,7 @@ get_string_list_prefixed (RetroCoreDescriptor  *self,
                                        key,
                                        length,
                                        &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-  g_free (group);
-    g_propagate_error (error, tmp_error);
-
-    return NULL;
-  }
-
-  g_free (group);
+  retro_throw_val_if_error (error, tmp_error, NULL);
 
   return result;
 }
@@ -197,7 +177,7 @@ retro_core_descriptor_check_has_required_key (RetroCoreDescriptor  *self,
                                               GError              **error)
 {
   gboolean has_key;
-  GError *tmp_error = NULL;
+  g_autoptr (RetroError) tmp_error = NULL;
 
   g_return_if_fail (RETRO_IS_CORE_DESCRIPTOR (self));
   g_return_if_fail (group != NULL);
@@ -207,11 +187,7 @@ retro_core_descriptor_check_has_required_key (RetroCoreDescriptor  *self,
                                 RETRO_CORE_DESCRIPTOR_LIBRETRO_GROUP,
                                 RETRO_CORE_DESCRIPTOR_TYPE_KEY,
                                 &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_propagate_error (error, tmp_error);
-
-    return;
-  }
+  retro_throw_if_error (error, tmp_error);
 
   if (!has_key)
     g_set_error (error,
@@ -229,7 +205,7 @@ void
 retro_core_descriptor_check_libretro_group (RetroCoreDescriptor  *self,
                                             GError              **error)
 {
-  GError *tmp_error = NULL;
+  g_autoptr (RetroError) tmp_error = NULL;
 
   g_return_if_fail (RETRO_IS_CORE_DESCRIPTOR (self));
 
@@ -237,41 +213,25 @@ retro_core_descriptor_check_libretro_group (RetroCoreDescriptor  *self,
                                                 RETRO_CORE_DESCRIPTOR_LIBRETRO_GROUP,
                                                 RETRO_CORE_DESCRIPTOR_TYPE_KEY,
                                                 &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_propagate_error (error, tmp_error);
-
-    return;
-  }
+  retro_throw_if_error (error, tmp_error);
 
   retro_core_descriptor_check_has_required_key (self,
                                                 RETRO_CORE_DESCRIPTOR_LIBRETRO_GROUP,
                                                 RETRO_CORE_DESCRIPTOR_NAME_KEY,
                                                 &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_propagate_error (error, tmp_error);
-
-    return;
-  }
+  retro_throw_if_error (error, tmp_error);
 
   retro_core_descriptor_check_has_required_key (self,
                                                 RETRO_CORE_DESCRIPTOR_LIBRETRO_GROUP,
                                                 RETRO_CORE_DESCRIPTOR_MODULE_KEY,
                                                 &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_propagate_error (error, tmp_error);
-
-    return;
-  }
+  retro_throw_if_error (error, tmp_error);
 
   retro_core_descriptor_check_has_required_key (self,
                                                 RETRO_CORE_DESCRIPTOR_LIBRETRO_GROUP,
                                                 RETRO_CORE_DESCRIPTOR_LIBRETRO_VERSION_KEY,
                                                 &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_propagate_error (error, tmp_error);
-
-    return;
-  }
+  retro_throw_if_error (error, tmp_error);
 }
 
 // FIXME static
@@ -281,10 +241,9 @@ retro_core_descriptor_check_platform_group (RetroCoreDescriptor  *self,
                                             GError              **error)
 {
   gboolean has_key;
-  gchar **firmwares;
-  gchar **firmware_p;
-  gchar *firmware_group;
-  GError *tmp_error = NULL;
+  g_auto (GStrv) firmwares = NULL;
+  GStrv firmware_p;
+  g_autoptr (RetroError) tmp_error = NULL;
 
   g_return_if_fail (RETRO_IS_CORE_DESCRIPTOR (self));
   g_return_if_fail (group != NULL);
@@ -298,11 +257,7 @@ retro_core_descriptor_check_platform_group (RetroCoreDescriptor  *self,
                                 group,
                                 RETRO_CORE_DESCRIPTOR_PLATFORM_FIRMWARES_KEY,
                                 &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_propagate_error (error, tmp_error);
-
-    return;
-  }
+  retro_throw_if_error (error, tmp_error);
 
   if (!has_key)
     return;
@@ -312,14 +267,10 @@ retro_core_descriptor_check_platform_group (RetroCoreDescriptor  *self,
                                           RETRO_CORE_DESCRIPTOR_PLATFORM_FIRMWARES_KEY,
                                           NULL,
                                           &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_propagate_error (error, tmp_error);
-
-    return;
-  }
+  retro_throw_if_error (error, tmp_error);
 
   for (firmware_p = firmwares; *firmware_p != NULL; firmware_p++) {
-    firmware_group = g_strconcat (RETRO_CORE_DESCRIPTOR_FIRMWARE_GROUP_PREFIX, *firmware_p, NULL);
+    g_autofree gchar *firmware_group = g_strconcat (RETRO_CORE_DESCRIPTOR_FIRMWARE_GROUP_PREFIX, 
*firmware_p, NULL);
     if (!g_key_file_has_group (self->key_file, firmware_group)) {
       g_set_error (error,
                    RETRO_CORE_DESCRIPTOR_ERROR,
@@ -330,16 +281,9 @@ retro_core_descriptor_check_platform_group (RetroCoreDescriptor  *self,
                    firmware_group,
                    group);
 
-      g_free (firmware_group);
-      g_strfreev (firmwares);
-
       return;
     }
-
-    g_free (firmware_group);
   }
-
-  g_strfreev (firmwares);
 }
 
 static void
@@ -347,7 +291,7 @@ retro_core_descriptor_check_firmware_group (RetroCoreDescriptor  *self,
                                             const gchar          *group,
                                             GError              **error)
 {
-  GError *tmp_error = NULL;
+  g_autoptr (RetroError) tmp_error = NULL;
 
   g_return_if_fail (RETRO_IS_CORE_DESCRIPTOR (self));
   g_return_if_fail (group != NULL);
@@ -356,21 +300,13 @@ retro_core_descriptor_check_firmware_group (RetroCoreDescriptor  *self,
                                                 group,
                                                 RETRO_CORE_DESCRIPTOR_FIRMWARE_PATH_KEY,
                                                 &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_propagate_error (error, tmp_error);
-
-    return;
-  }
+  retro_throw_if_error (error, tmp_error);
 
   retro_core_descriptor_check_has_required_key (self,
                                                 group,
                                                 RETRO_CORE_DESCRIPTOR_FIRMWARE_MANDATORY_KEY,
                                                 &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_propagate_error (error, tmp_error);
-
-    return;
-  }
+  retro_throw_if_error (error, tmp_error);
 }
 
 /* Public */
@@ -389,16 +325,12 @@ retro_core_descriptor_has_icon (RetroCoreDescriptor  *self,
                                 GError              **error)
 {
   gboolean has_icon;
-  GError *tmp_error = NULL;
+  g_autoptr (RetroError) tmp_error = NULL;
 
   g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), FALSE);
 
   has_icon = g_key_file_has_key (self->key_file, RETRO_CORE_DESCRIPTOR_LIBRETRO_GROUP, 
RETRO_CORE_DESCRIPTOR_ICON_KEY, &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_propagate_error (error, tmp_error);
-
-    return FALSE;
-  }
+  retro_throw_val_if_error (error, tmp_error, FALSE);
 
   return has_icon;
 }
@@ -414,16 +346,13 @@ retro_core_descriptor_has_icon (RetroCoreDescriptor  *self,
 gchar *
 retro_core_descriptor_get_uri (RetroCoreDescriptor *self)
 {
-  gchar *result;
-  GFile *file;
+  g_autoptr (GFile) file = NULL;
 
   g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), NULL);
 
   file = g_file_new_for_path (self->filename);
-  result = g_file_get_uri (file);
-  g_object_unref (file);
 
-  return result;
+  return g_file_get_uri (file);
 }
 
 /**
@@ -455,9 +384,8 @@ gboolean
 retro_core_descriptor_get_is_game (RetroCoreDescriptor  *self,
                                    GError              **error)
 {
-  gchar *type;
-  gboolean is_game;
-  GError *tmp_error = NULL;
+  g_autofree gchar *type = NULL;
+  g_autoptr (RetroError) tmp_error = NULL;
 
   g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), FALSE);
 
@@ -465,16 +393,9 @@ retro_core_descriptor_get_is_game (RetroCoreDescriptor  *self,
                                   RETRO_CORE_DESCRIPTOR_LIBRETRO_GROUP,
                                   RETRO_CORE_DESCRIPTOR_TYPE_KEY,
                                   &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_propagate_error (error, tmp_error);
-
-    return FALSE;
-  }
+  retro_throw_val_if_error (error, tmp_error, FALSE);
 
-  is_game = g_strcmp0 (type, RETRO_CORE_DESCRIPTOR_TYPE_GAME) == 0;
-  g_free (type);
-
-  return is_game;
+  return g_strcmp0 (type, RETRO_CORE_DESCRIPTOR_TYPE_GAME) == 0;
 }
 
 /**
@@ -490,9 +411,8 @@ gboolean
 retro_core_descriptor_get_is_emulator (RetroCoreDescriptor  *self,
                                        GError              **error)
 {
-  gchar *type;
-  gboolean is_game;
-  GError *tmp_error = NULL;
+  g_autofree gchar *type = NULL;
+  g_autoptr (RetroError) tmp_error = NULL;
 
   g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), FALSE);
 
@@ -500,16 +420,9 @@ retro_core_descriptor_get_is_emulator (RetroCoreDescriptor  *self,
                                   RETRO_CORE_DESCRIPTOR_LIBRETRO_GROUP,
                                   RETRO_CORE_DESCRIPTOR_TYPE_KEY,
                                   &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_propagate_error (error, tmp_error);
-
-    return FALSE;
-  }
-
-  is_game = g_strcmp0 (type, RETRO_CORE_DESCRIPTOR_TYPE_EMULATOR) == 0;
-  g_free (type);
+  retro_throw_val_if_error (error, tmp_error, FALSE);
 
-  return is_game;
+  return g_strcmp0 (type, RETRO_CORE_DESCRIPTOR_TYPE_EMULATOR) == 0;
 }
 
 /**
@@ -544,9 +457,8 @@ GIcon *
 retro_core_descriptor_get_icon (RetroCoreDescriptor  *self,
                                 GError              **error)
 {
-  gchar *icon_name;
-  GIcon *icon;
-  GError *tmp_error = NULL;
+  g_autofree gchar *icon_name = NULL;
+  g_autoptr (RetroError) tmp_error = NULL;
 
   g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), NULL);
 
@@ -554,16 +466,9 @@ retro_core_descriptor_get_icon (RetroCoreDescriptor  *self,
                                      RETRO_CORE_DESCRIPTOR_LIBRETRO_GROUP,
                                      RETRO_CORE_DESCRIPTOR_ICON_KEY,
                                      &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_propagate_error (error, tmp_error);
-
-    return NULL;
-  }
+  retro_throw_val_if_error (error, tmp_error, FALSE);
 
-  icon = G_ICON (g_themed_icon_new (icon_name));
-  g_free (icon_name);
-
-  return icon;
+  return G_ICON (g_themed_icon_new (icon_name));
 }
 
 /**
@@ -600,41 +505,27 @@ GFile *
 retro_core_descriptor_get_module_file (RetroCoreDescriptor  *self,
                                        GError              **error)
 {
-  GFile *file;
-  GFile *dir;
-  gchar *module;
-  GFile *module_file;
-  GError *tmp_error = NULL;
+  g_autoptr (GFile) file = NULL;
+  g_autoptr (GFile) dir = NULL;
+  g_autofree gchar *module = NULL;
+  g_autoptr (GFile) module_file = NULL;
+  g_autoptr (RetroError) tmp_error = NULL;
 
   g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), NULL);
 
   file = g_file_new_for_path (self->filename);
   dir = g_file_get_parent (file);
-  g_object_unref (file);
   if (dir == NULL)
     return NULL;
 
   module = retro_core_descriptor_get_module (self, &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_propagate_error (error, tmp_error);
-    g_object_unref (dir);
-
-    return NULL;
-  }
+  retro_throw_val_if_error (error, tmp_error, NULL);
 
   module_file = g_file_get_child (dir, module);
-  if (!g_file_query_exists (module_file, NULL)) {
-    g_object_unref (dir);
-    g_free (module);
-    g_object_unref (module_file);
-
+  if (!g_file_query_exists (module_file, NULL))
     return NULL;
-  }
-
-  g_object_unref (dir);
-  g_free (module);
 
-  return module_file;
+  return g_steal_pointer (&module_file);
 }
 
 /**
@@ -858,9 +749,9 @@ retro_core_descriptor_get_is_firmware_mandatory (RetroCoreDescriptor  *self,
                                                  const gchar          *firmware,
                                                  GError              **error)
 {
-  gchar *group;
+  g_autofree gchar *group = NULL;
   gboolean result;
-  GError *tmp_error = NULL;
+  g_autoptr (RetroError) tmp_error = NULL;
 
   g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), FALSE);
   g_return_val_if_fail (firmware != NULL, FALSE);
@@ -870,14 +761,7 @@ retro_core_descriptor_get_is_firmware_mandatory (RetroCoreDescriptor  *self,
                                    group,
                                    RETRO_CORE_DESCRIPTOR_FIRMWARE_MANDATORY_KEY,
                                    &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_free (group);
-    g_propagate_error (error, tmp_error);
-
-    return FALSE;
-  }
-
-  g_free (group);
+  retro_throw_val_if_error (error, tmp_error, FALSE);
 
   return result;
 }
@@ -899,9 +783,9 @@ retro_core_descriptor_get_platform_supports_mime_types (RetroCoreDescriptor  *se
                                                         const gchar * const  *mime_types,
                                                         GError              **error)
 {
-  gchar **supported_mime_types;
+  g_auto (GStrv) supported_mime_types = NULL;
   gsize supported_mime_types_length;
-  GError *tmp_error = NULL;
+  g_autoptr (RetroError) tmp_error = NULL;
 
   g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), FALSE);
   g_return_val_if_fail (platform != NULL, FALSE);
@@ -912,21 +796,12 @@ retro_core_descriptor_get_platform_supports_mime_types (RetroCoreDescriptor  *se
                                          platform,
                                          &supported_mime_types_length,
                                          &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_propagate_error (error, tmp_error);
-
-    return FALSE;
-  }
+  retro_throw_val_if_error (error, tmp_error, FALSE);
 
   for (; *mime_types != NULL; mime_types++)
     if (!g_strv_contains ((const char * const *) supported_mime_types,
-                          *mime_types)) {
-      g_strfreev (supported_mime_types);
-
+                          *mime_types))
       return FALSE;
-    }
-
-  g_strfreev (supported_mime_types);
 
   return TRUE;
 }
@@ -944,11 +819,10 @@ RetroCoreDescriptor *
 retro_core_descriptor_new (const gchar  *filename,
                            GError      **error)
 {
-  RetroCoreDescriptor *self;
-  gchar ** groups;
+  g_autoptr (RetroCoreDescriptor) self = NULL;
+  g_auto (GStrv) groups;
   gsize groups_length;
-  gsize i;
-  GError *tmp_error = NULL;
+  g_autoptr (RetroError) tmp_error = NULL;
 
   g_return_val_if_fail (filename != NULL, NULL);
 
@@ -959,48 +833,24 @@ retro_core_descriptor_new (const gchar  *filename,
                              filename,
                              G_KEY_FILE_NONE,
                              &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_object_unref (self);
-    g_propagate_error (error, tmp_error);
-
-    return NULL;
-  }
+  retro_throw_val_if_error (error, tmp_error, NULL);
 
   retro_core_descriptor_check_libretro_group (self, &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_object_unref (self);
-    g_propagate_error (error, tmp_error);
-
-    return NULL;
-  }
+  retro_throw_val_if_error (error, tmp_error, NULL);
 
   groups = g_key_file_get_groups (self->key_file, &groups_length);
-  for (i = 0; i < groups_length; i++) {
+  for (gsize i = 0; i < groups_length; i++) {
     if (g_str_has_prefix (groups[i],
                           RETRO_CORE_DESCRIPTOR_PLATFORM_GROUP_PREFIX)) {
       retro_core_descriptor_check_platform_group (self, groups[i], &tmp_error);
-      if (G_UNLIKELY (tmp_error != NULL)) {
-        g_strfreev (groups);
-        g_object_unref (self);
-        g_propagate_error (error, tmp_error);
-
-        return NULL;
-      }
+      retro_throw_val_if_error (error, tmp_error, NULL);
     }
     else if (g_str_has_prefix (groups[i],
                                RETRO_CORE_DESCRIPTOR_FIRMWARE_GROUP_PREFIX)) {
       retro_core_descriptor_check_firmware_group (self, groups[i], &tmp_error);
-      if (G_UNLIKELY (tmp_error != NULL)) {
-        g_strfreev (groups);
-        g_object_unref (self);
-        g_propagate_error (error, tmp_error);
-
-        return NULL;
-      }
+      retro_throw_val_if_error (error, tmp_error, NULL);
     }
   }
 
-  g_strfreev (groups);
-
-  return self;
+  return g_steal_pointer (&self);
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]