1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2017 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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.
25 playlist_init(struct mpdclient_playlist
*playlist
)
27 playlist
->version
= 0;
28 playlist
->list
= g_ptr_array_sized_new(1024);
32 playlist_clear(struct mpdclient_playlist
*playlist
)
34 playlist
->version
= 0;
36 for (unsigned i
= 0; i
< playlist
->list
->len
; ++i
) {
37 struct mpd_song
*song
= playlist_get(playlist
, i
);
42 g_ptr_array_set_size(playlist
->list
, 0);
46 mpdclient_playlist_free(struct mpdclient_playlist
*playlist
)
48 if (playlist
->list
!= NULL
) {
49 playlist_clear(playlist
);
50 g_ptr_array_free(playlist
->list
, TRUE
);
53 memset(playlist
, 0, sizeof(*playlist
));
57 const struct mpd_song
*
58 playlist_get_song(const struct mpdclient_playlist
*playlist
, gint idx
)
60 if (idx
< 0 || (guint
)idx
>= playlist_length(playlist
))
63 return playlist_get(playlist
, idx
);
67 playlist_move(struct mpdclient_playlist
*playlist
,
68 unsigned dest
, unsigned src
)
70 assert(playlist
!= NULL
);
71 assert(src
< playlist_length(playlist
));
72 assert(dest
< playlist_length(playlist
));
75 struct mpd_song
*song
= playlist_get(playlist
, src
);
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
;
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
;
90 const struct mpd_song
*
91 playlist_lookup_song(const struct mpdclient_playlist
*playlist
, unsigned id
)
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
)
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
)
115 playlist_get_index_from_id(const struct mpdclient_playlist
*playlist
,
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
)
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)
142 playlist_get_id_from_uri(const struct mpdclient_playlist
*playlist
,
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
);