[retro-gtk/gtk-cleanup: 8/12] module-iterator: Use autocleanups



commit 61bbede165433883709671ac77d707200615553a
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Sun May 24 14:24:57 2020 +0200

    module-iterator: Use autocleanups
    
    This makes the code safer and more concise.

 retro-gtk/retro-module-iterator.c | 89 ++++++++++++---------------------------
 1 file changed, 27 insertions(+), 62 deletions(-)
---
diff --git a/retro-gtk/retro-module-iterator.c b/retro-gtk/retro-module-iterator.c
index da29782..fbd8169 100644
--- a/retro-gtk/retro-module-iterator.c
+++ b/retro-gtk/retro-module-iterator.c
@@ -73,28 +73,22 @@ new_for_subdirectory (const gchar *lookup_path,
 static gboolean
 was_current_directory_visited (RetroModuleIterator *self)
 {
-  GFile *current_directory_file;
-  gchar *current_directory_path;
-  gboolean result;
+  g_autoptr (GFile) current_directory_file =
+    g_file_new_for_path (self->directories[self->current_directory]);
+  g_autofree gchar *current_directory_path =
+    g_file_get_path (current_directory_file);
 
-  current_directory_file = g_file_new_for_path (self->directories[self->current_directory]);
-  current_directory_path = g_file_get_path (current_directory_file);
-  g_object_unref (current_directory_file);
-  result = g_hash_table_contains (self->visited, current_directory_path);
-  g_free (current_directory_path);
-
-  return result;
+  return g_hash_table_contains (self->visited, current_directory_path);
 }
 
 static void
 set_current_directory_as_visited (RetroModuleIterator *self)
 {
-  GFile *current_directory_file;
-  gchar *current_directory_path;
+  g_autoptr (GFile) current_directory_file =
+    g_file_new_for_path (self->directories[self->current_directory]);
+  gchar *current_directory_path =
+    g_file_get_path (current_directory_file);
 
-  current_directory_file = g_file_new_for_path (self->directories[self->current_directory]);
-  current_directory_path = g_file_get_path (current_directory_file);
-  g_object_unref (current_directory_file);
   g_hash_table_add (self->visited, current_directory_path);
 }
 
@@ -110,10 +104,7 @@ next_in_sub_directory (RetroModuleIterator *self)
     return TRUE;
   }
 
-  if (self->sub_directory != NULL) {
-    g_object_unref (self->sub_directory);
-    self->sub_directory = NULL;
-  }
+  g_clear_object (&self->sub_directory);
 
   return FALSE;
 }
@@ -124,12 +115,9 @@ iterate_next_in_current_path (RetroModuleIterator  *self,
                               GFileInfo            *info,
                               GError              **error)
 {
-  const gchar *sub_directory_basename;
-  GFile *sub_directory_file;
-  gchar *sub_directory_path;
   const gchar *core_descriptor_basename;
-  GFile *core_descriptor_file;
-  gchar *core_descriptor_path;
+  g_autoptr (GFile) core_descriptor_file = NULL;
+  g_autofree gchar *core_descriptor_path = NULL;
   RetroCoreDescriptor *core_descriptor;
   GError *tmp_error = NULL;
 
@@ -139,19 +127,17 @@ iterate_next_in_current_path (RetroModuleIterator  *self,
   if (self->recursive &&
       g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY &&
       self->sub_directory == NULL) {
-    sub_directory_basename = g_file_info_get_name (info);
-    sub_directory_file = g_file_get_child (directory, sub_directory_basename);
-    sub_directory_path = g_file_get_path (sub_directory_file);
-      g_object_unref (sub_directory_file);
-
-    if (g_hash_table_contains (self->visited, sub_directory_path)) {
-      g_free (sub_directory_path);
-
+    const gchar *sub_directory_basename =
+      g_file_info_get_name (info);
+    g_autoptr (GFile) sub_directory_file =
+      g_file_get_child (directory, sub_directory_basename);
+    g_autofree gchar *sub_directory_path =
+      g_file_get_path (sub_directory_file);
+
+    if (g_hash_table_contains (self->visited, sub_directory_path))
       return FALSE;
-    }
 
     self->sub_directory = new_for_subdirectory (sub_directory_path, self->visited);
-    g_free (sub_directory_path);
 
     return next_in_sub_directory (self);
   }
@@ -162,18 +148,15 @@ iterate_next_in_current_path (RetroModuleIterator  *self,
 
   core_descriptor_file = g_file_get_child (directory, core_descriptor_basename);
   core_descriptor_path = g_file_get_path (core_descriptor_file);
-  g_object_unref (core_descriptor_file);
   core_descriptor = retro_core_descriptor_new (core_descriptor_path, &tmp_error);
   if (G_UNLIKELY (tmp_error != NULL)) {
     g_debug ("%s", tmp_error->message);
 
     g_error_free (tmp_error);
-    g_free (core_descriptor_path);
 
     return FALSE;
   }
 
-  g_free (core_descriptor_path);
   g_clear_object (&self->core_descriptor);
   self->core_descriptor = core_descriptor;
 
@@ -184,9 +167,7 @@ static gboolean
 next_in_current_path (RetroModuleIterator  *self,
                       GError              **error)
 {
-  GFile *directory = NULL;
-  GFileInfo *info = NULL;
-  gboolean found = FALSE;
+  g_autoptr (GFile) directory = NULL;
 
   GError *tmp_error = NULL;
 
@@ -204,28 +185,22 @@ next_in_current_path (RetroModuleIterator  *self,
                                  &tmp_error);
     if (G_UNLIKELY (tmp_error != NULL)) {
       g_propagate_error (error, tmp_error);
-      g_object_unref (directory);
       g_clear_object (&self->file_enumerator);
 
       return FALSE;
     }
   }
 
-  if (self->file_enumerator == NULL) {
-    g_object_unref (directory);
-
+  if (self->file_enumerator == NULL)
     return FALSE;
-  }
 
   while (TRUE) {
-    if (info != NULL)
-      g_object_unref (info);
+    gboolean found;
+    g_autoptr (GFileInfo) info =
+      g_file_enumerator_next_file (self->file_enumerator, NULL, &tmp_error);
 
-    info = g_file_enumerator_next_file (self->file_enumerator, NULL, &tmp_error);
     if (G_UNLIKELY (tmp_error != NULL)) {
       g_propagate_error (error, tmp_error);
-      g_clear_object (&info);
-      g_object_unref (directory);
 
       return FALSE;
     }
@@ -236,22 +211,15 @@ next_in_current_path (RetroModuleIterator  *self,
     found = iterate_next_in_current_path (self, directory, info, &tmp_error);
     if (G_UNLIKELY (tmp_error != NULL)) {
       g_propagate_error (error, tmp_error);
-      g_object_unref (info);
-      g_object_unref (directory);
 
       return FALSE;
     }
 
-    if (found) {
-      g_object_unref (info);
-      g_object_unref (directory);
-
+    if (found)
       return TRUE;
-    }
   }
 
   g_clear_object (&self->file_enumerator);
-  g_object_unref (directory);
 
   return FALSE;
 }
@@ -313,10 +281,7 @@ retro_module_iterator_next (RetroModuleIterator *self)
 
   g_clear_object (&self->file_enumerator);
   g_clear_object (&self->core_descriptor);
-  if (self->sub_directory != NULL) {
-    g_object_unref (self->sub_directory);
-    self->sub_directory = NULL;
-  }
+  g_clear_object (&self->sub_directory);
 
   return FALSE;
 }


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