Code

release v0.29
[ncmpc.git] / src / playlist.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 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.
9  *
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.
14  *
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 "playlist.h"
22 #include <string.h>
24 void
25 playlist_init(struct mpdclient_playlist *playlist)
26 {
27         playlist->version = 0;
28         playlist->list = g_ptr_array_sized_new(1024);
29 }
31 void
32 playlist_clear(struct mpdclient_playlist *playlist)
33 {
34         playlist->version = 0;
36         for (unsigned i = 0; i < playlist->list->len; ++i) {
37                 struct mpd_song *song = playlist_get(playlist, i);
39                 mpd_song_free(song);
40         }
42         g_ptr_array_set_size(playlist->list, 0);
43 }
45 gint
46 mpdclient_playlist_free(struct mpdclient_playlist *playlist)
47 {
48         if (playlist->list != NULL) {
49                 playlist_clear(playlist);
50                 g_ptr_array_free(playlist->list, TRUE);
51         }
53         memset(playlist, 0, sizeof(*playlist));
54         return 0;
55 }
57 const struct mpd_song *
58 playlist_get_song(const struct mpdclient_playlist *playlist, gint idx)
59 {
60         if (idx < 0 || (guint)idx >= playlist_length(playlist))
61                 return NULL;
63         return playlist_get(playlist, idx);
64 }
66 void
67 playlist_move(struct mpdclient_playlist *playlist,
68               unsigned dest, unsigned src)
69 {
70         assert(playlist != NULL);
71         assert(src < playlist_length(playlist));
72         assert(dest < playlist_length(playlist));
73         assert(src != dest);
75         struct mpd_song *song = playlist_get(playlist, src);
77         if (src < dest) {
78                 memmove(&playlist->list->pdata[src],
79                         &playlist->list->pdata[src + 1],
80                         sizeof(playlist->list->pdata[0]) * (dest - src));
81                 playlist->list->pdata[dest] = song;
82         } else {
83                 memmove(&playlist->list->pdata[dest + 1],
84                         &playlist->list->pdata[dest],
85                         sizeof(playlist->list->pdata[0]) * (src - dest));
86                 playlist->list->pdata[dest] = song;
87         }
88 }
90 const struct mpd_song *
91 playlist_lookup_song(const struct mpdclient_playlist *playlist, unsigned id)
92 {
93         for (guint i = 0; i < playlist_length(playlist); ++i) {
94                 struct mpd_song *song = playlist_get(playlist, i);
95                 if (mpd_song_get_id(song) == id)
96                         return song;
97         }
99         return NULL;
102 gint
103 playlist_get_index(const struct mpdclient_playlist *playlist,
104                    const struct mpd_song *song)
106         for (guint i = 0; i < playlist_length(playlist); ++i) {
107                 if (playlist_get(playlist, i) == song)
108                         return (gint)i;
109         }
111         return -1;
114 gint
115 playlist_get_index_from_id(const struct mpdclient_playlist *playlist,
116                            unsigned id)
118         for (guint i = 0; i < playlist_length(playlist); ++i) {
119                 const struct mpd_song *song = playlist_get(playlist, i);
120                 if (mpd_song_get_id(song) == id)
121                         return (gint)i;
122         }
124         return -1;
127 gint
128 playlist_get_index_from_file(const struct mpdclient_playlist *playlist,
129                              const gchar *filename)
131         for (guint i = 0; i < playlist_length(playlist); ++i) {
132                 const struct mpd_song *song = playlist_get(playlist, i);
134                 if (strcmp(mpd_song_get_uri(song), filename) == 0)
135                         return (gint)i;
136         }
138         return -1;
141 gint
142 playlist_get_id_from_uri(const struct mpdclient_playlist *playlist,
143                          const gchar *uri)
145         for (guint i = 0; i < playlist_length(playlist); ++i) {
146                 const struct mpd_song *song = playlist_get(playlist, i);
148                 if (strcmp(mpd_song_get_uri(song), uri) == 0)
149                         return mpd_song_get_id(song);
150         }
152         return -1;