[easytag/wip/glist: 4/12] Refactor list handling in et_core.c



commit c03c567ea1935ef7987dc4923d7cf73b27b0a0cb
Author: David King <amigadave amigadave com>
Date:   Thu May 23 19:59:37 2013 +0100

    Refactor list handling in et_core.c

 src/et_core.c |  121 +++++++++++++++++++++------------------------------------
 1 files changed, 45 insertions(+), 76 deletions(-)
---
diff --git a/src/et_core.c b/src/et_core.c
index 441b94a..d4b5c89 100644
--- a/src/et_core.c
+++ b/src/et_core.c
@@ -647,17 +647,15 @@ GList *ET_Add_File_To_File_List (gchar *filename)
 
 gboolean ET_Create_Artist_Album_File_List (void)
 {
-    GList *ETFileList;
+    GList *l;
 
     if (ETCore->ETArtistAlbumFileList)
         ET_Free_Artist_Album_File_List();
 
-    ETFileList = g_list_first(ETCore->ETFileList);
-    while (ETFileList)
+    for (l = g_list_first (ETCore->ETFileList); l != NULL; l = g_list_next (l))
     {
-        ET_File *ETFile = (ET_File *)ETFileList->data;
+        ET_File *ETFile = (ET_File *)l->data;
         ET_Add_File_To_Artist_Album_File_List(ETFile);
-        ETFileList = ETFileList->next;
     }
     //ET_Debug_Print_Artist_Album_List(__FILE__,__LINE__,__FUNCTION__);
     return TRUE;
@@ -2074,7 +2072,7 @@ gboolean ET_Set_Displayed_File_List (GList *ETFileList)
  */
 gboolean ET_Free_File_List (void)
 {
-    g_return_val_if_fail (ETCore != NULL || ETCore->ETFileList != NULL, FALSE);
+    g_return_val_if_fail (ETCore != NULL && ETCore->ETFileList != NULL, FALSE);
 
     g_list_free_full (ETCore->ETFileList,
                       (GDestroyNotify)ET_Free_File_List_Item);
@@ -2115,8 +2113,8 @@ gboolean ET_Free_File_List_Item (ET_File *ETFile)
         }
         g_free(ETFile->ETFileExtension);
         g_free(ETFile);
-        ETFile = NULL;
     }
+
     return TRUE;
 }
 
@@ -2126,21 +2124,12 @@ gboolean ET_Free_File_List_Item (ET_File *ETFile)
  */
 gboolean ET_Free_File_Name_List (GList *FileNameList)
 {
-    GList *list;
-
     g_return_val_if_fail (FileNameList != NULL, FALSE);
 
-    list = g_list_last(FileNameList);
-    while (list)
-    {
-        if ( (File_Name *)list->data )
-            ET_Free_File_Name_Item( (File_Name *)list->data );
+    FileNameList = g_list_first (FileNameList);
+
+    g_list_free_full (FileNameList, (GDestroyNotify)ET_Free_File_Name_Item);
 
-        if (!list->prev) break;
-        list = list->prev;
-    }
-    g_list_free(list);
-    FileNameList = NULL;
     return TRUE;
 }
 
@@ -2156,7 +2145,7 @@ gboolean ET_Free_File_Name_Item (File_Name *FileName)
     g_free(FileName->value_utf8);
     g_free(FileName->value_ck);
     g_free(FileName);
-    FileName = NULL;
+
     return TRUE;
 }
 
@@ -2167,21 +2156,20 @@ gboolean ET_Free_File_Name_Item (File_Name *FileName)
 static gboolean
 ET_Free_File_Tag_List (GList *FileTagList)
 {
-    GList *list;
+    GList *l;
 
     g_return_val_if_fail (FileTagList != NULL, FALSE);
 
-    list = g_list_last(FileTagList);
-    while (list)
-    {
-        if ( (File_Tag *)list->data )
-            ET_Free_File_Tag_Item( (File_Tag *)list->data );
+    FileTagList = g_list_first (FileTagList);
 
-        if (!list->prev) break;
-        list = list->prev;
+    for (l = FileTagList; l != NULL; l = g_list_next (l))
+    {
+        if ((File_Tag *)l->data)
+            ET_Free_File_Tag_Item ((File_Tag *)l->data);
     }
-    g_list_free(list);
-    FileTagList = NULL;
+
+    g_list_free (FileTagList);
+
     return TRUE;
 }
 
@@ -2192,16 +2180,7 @@ ET_Free_File_Tag_List (GList *FileTagList)
 static gboolean
 ET_Free_File_Tag_Item_Other_Field (File_Tag *FileTag)
 {
-    GList *other = FileTag->other;
-
-    while (other)
-    {
-        g_free(other->data);
-
-        if (!other->next) break;
-        other = other->next;
-    }
-    g_list_free(FileTag->other);
+    g_list_free_full (FileTag->other, g_free);
 
     return TRUE;
 }
@@ -2262,22 +2241,15 @@ ET_Free_File_Info_Item (ET_File_Info *ETFileInfo)
 static gboolean
 ET_Free_History_File_List (void)
 {
-    GList *list;
-
-    g_return_val_if_fail (ETCore != NULL || ETCore->ETHistoryFileList != NULL,
+    g_return_val_if_fail (ETCore != NULL && ETCore->ETHistoryFileList != NULL,
                           FALSE);
 
-    ETCore->ETHistoryFileList = g_list_first(ETCore->ETHistoryFileList);
-    list = ETCore->ETHistoryFileList;
-    while (list)
-    {
-        g_free( (ET_History_File *)list->data );
+    ETCore->ETHistoryFileList = g_list_first (ETCore->ETHistoryFileList);
+
+    g_list_free_full (ETCore->ETHistoryFileList, g_free);
 
-        if (!list->next) break;
-        list = list->next;
-    }
-    g_list_free(ETCore->ETHistoryFileList);
     ETCore->ETHistoryFileList = NULL;
+
     return TRUE;
 }
 
@@ -2287,10 +2259,9 @@ ET_Free_History_File_List (void)
 static gboolean
 ET_Free_Displayed_File_List (void)
 {
-    g_return_val_if_fail (ETCore != NULL ||
-                          ETCore->ETFileDisplayedList != NULL, FALSE);
+    g_return_val_if_fail (ETCore != NULL
+                          && ETCore->ETFileDisplayedList != NULL, FALSE);
 
-    ETCore->ETFileDisplayedList = g_list_first(ETCore->ETFileDisplayedList);
     ETCore->ETFileDisplayedList = NULL;
 
     return TRUE;
@@ -2303,29 +2274,26 @@ ET_Free_Displayed_File_List (void)
 static gboolean
 ET_Free_Artist_Album_File_List (void)
 {
-    GList *ArtistList;
-    GList *AlbumList;
-    GList *etfilelist;
+    GList *l;
 
-    g_return_val_if_fail (ETCore != NULL ||
-                          ETCore->ETArtistAlbumFileList != NULL, FALSE);
+    g_return_val_if_fail (ETCore != NULL
+                          && ETCore->ETArtistAlbumFileList != NULL, FALSE);
 
-    ArtistList = ETCore->ETArtistAlbumFileList;
-    while (ArtistList)
+    for (l = ETCore->ETArtistAlbumFileList; l != NULL; l = g_list_next (l))
     {
-        AlbumList = (GList *)ArtistList->data;
-        while (AlbumList)
+        GList *m;
+
+       for (m = (GList *)l->data; m != NULL; m = g_list_next (m))
         {
-            etfilelist = (GList *)AlbumList->data;
-            if (etfilelist)
-                g_list_free(etfilelist);
-            AlbumList = AlbumList->next;
+            GList *n = (GList *)m->data;
+            if (n)
+                g_list_free (n);
         }
-        if (ArtistList->data) // Free AlbumList list
-            g_list_free((GList *)ArtistList->data);
 
-        ArtistList = ArtistList->next;
+        if (l->data) /* Free AlbumList list. */
+            g_list_free ((GList *)l->data);
     }
+
     if (ETCore->ETArtistAlbumFileList)
         g_list_free(ETCore->ETArtistAlbumFileList);
 
@@ -2348,15 +2316,16 @@ static gboolean
 ET_Copy_File_Tag_Item_Other_Field (ET_File *ETFile, File_Tag *FileTag)
 {
     File_Tag *FileTagCur;
-    GList *list = NULL;
+    GList *l;
 
     FileTagCur = (File_Tag *)(ETFile->FileTag)->data;
-    list = FileTagCur->other;
-    while (list)
+
+    for (l = FileTagCur->other; l != NULL; l = g_list_next (l))
     {
-        FileTag->other = g_list_append(FileTag->other,g_strdup((gchar *)list->data));
-        list = list->next;
+        FileTag->other = g_list_append (FileTag->other,
+                                        g_strdup ((gchar *)l->data));
     }
+
     return TRUE;
 }
 


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