Code

file: moved strcmp() call to inline function same_song()
[ncmpc.git] / src / filelist.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "filelist.h"
21 #include "libmpdclient.h"
23 #include <stdbool.h>
24 #include <string.h>
25 #include <assert.h>
27 struct filelist *
28 filelist_new(const char *path)
29 {
30         struct filelist *filelist = g_malloc(sizeof(*filelist));
32         filelist->path = g_strdup(path);
33         filelist->entries = g_ptr_array_new();
35         return filelist;
36 }
38 void
39 filelist_free(struct filelist *filelist)
40 {
41         guint i;
43         for (i = 0; i < filelist_length(filelist); ++i) {
44                 struct filelist_entry *entry = filelist_get(filelist, i);
46                 if (entry->entity)
47                         mpd_freeInfoEntity(entry->entity);
49                 g_slice_free(struct filelist_entry, entry);
50         }
52         g_ptr_array_free(filelist->entries, TRUE);
53         g_free(filelist->path);
54         g_free(filelist);
55 }
57 struct filelist_entry *
58 filelist_append(struct filelist *filelist, struct mpd_InfoEntity *entity)
59 {
60         struct filelist_entry *entry = g_slice_new(struct filelist_entry);
62         entry->flags = 0;
63         entry->entity = entity;
65         g_ptr_array_add(filelist->entries, entry);
67         return entry;
68 }
70 struct filelist_entry *
71 filelist_prepend(struct filelist *filelist, struct mpd_InfoEntity *entity)
72 {
73         struct filelist_entry *entry = filelist_append(filelist, entity);
75         /* this is very slow, but we should optimize screen_artist.c
76            later so that this function can be removed, so I'm not in
77            the mood to implement something better here */
79         if (!filelist_is_empty(filelist)) {
80                 guint i;
82                 for (i = filelist_length(filelist) - 1; i > 0; --i)
83                         g_ptr_array_index(filelist->entries, i) =
84                                 filelist_get(filelist, i - 1);
86                 g_ptr_array_index(filelist->entries, 0) = entry;
87         }
89         return entry;
90 }
92 void
93 filelist_move(struct filelist *filelist, struct filelist *from)
94 {
95         guint i;
97         for (i = 0; i < filelist_length(from); ++i)
98                 g_ptr_array_add(filelist->entries,
99                                 g_ptr_array_index(from->entries, i));
101         g_ptr_array_set_size(from->entries, 0);
104 static gint
105 filelist_compare_indirect(gconstpointer ap, gconstpointer bp, gpointer data)
107         GCompareFunc compare_func = data;
108         gconstpointer a = *(const gconstpointer*)ap;
109         gconstpointer b = *(const gconstpointer*)bp;
111         return compare_func(a, b);
114 /* Sorts the whole filelist, at the moment used by filelist_search */
115 void
116 filelist_sort_all(struct filelist *filelist, GCompareFunc compare_func)
118         g_ptr_array_sort_with_data(filelist->entries,
119                         filelist_compare_indirect,
120                         compare_func);
124 /* Only sorts the directories and playlist files.
125  * The songs stay in the order it came from mpd. */
126 void
127 filelist_sort_dir_play(struct filelist *filelist, GCompareFunc compare_func)
129         unsigned first, last;
130         const mpd_InfoEntity *iter;
132         assert(filelist && filelist->entries);
134         if (filelist->entries->len < 2)
135                 return;
136         iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, 0))->entity;
137         /* This can only happen at the beginning of the filelist,
138          * because NULL stands for "[..]" */
139         if (iter == NULL) {
140                 iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, 1))->entity;
141                 first = 1;
142         }
143         else
144                 first = 0;
145         /* find the last directory entry */
146         for (last = first+1; last < filelist->entries->len; last++) {
147                 iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, last))->entity;
148                 if (iter->type != MPD_INFO_ENTITY_TYPE_DIRECTORY)
149                         break;
150         }
151         if (last == filelist->entries->len - 1)
152                 last++;
153         /* sort the directories */
154         if (last - first > 1)
155                 g_qsort_with_data(filelist->entries->pdata + first,
156                                 last - first, sizeof(gpointer),
157                                 filelist_compare_indirect, compare_func);
158         /* find the first playlist entry */
159         for (first = last; first < filelist->entries->len; first++) {
160                 iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, first))->entity;
161                 if (iter->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
162                         break;
163         }
164         /* sort the playlist entries */
165         if (filelist->entries->len - first > 1)
166                 g_qsort_with_data(filelist->entries->pdata + first,
167                                 filelist->entries->len - first, sizeof(gpointer),
168                                 filelist_compare_indirect, compare_func);
171 static bool
172 same_song(const struct mpd_song *a, const struct mpd_song *b)
174         return strcmp(a->file, b->file) == 0;
177 int
178 filelist_find_song(struct filelist *fl, const struct mpd_song *song)
180         guint i;
182         assert(song != NULL);
184         for (i = 0; i < filelist_length(fl); ++i) {
185                 struct filelist_entry *entry = filelist_get(fl, i);
186                 mpd_InfoEntity *entity  = entry->entity;
188                 if (entity && entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
189                         struct mpd_song *song2 = entity->info.song;
191                         if (same_song(song, song2))
192                                 return i;
193                 }
194         }
196         return -1;
199 int
200 filelist_find_directory(struct filelist *filelist, const char *name)
202         guint i;
204         assert(name != NULL);
206         for (i = 0; i < filelist_length(filelist); ++i) {
207                 struct filelist_entry *entry = filelist_get(filelist, i);
208                 mpd_InfoEntity *entity  = entry->entity;
210                 if (entity && entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY &&
211                     strcmp(entity->info.directory->path, name) == 0)
212                         return i;
213         }
215         return -1;