diff options
author | Max Kellermann <max@duempel.org> | 2008-09-16 19:11:39 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2008-09-16 19:11:39 +0200 |
commit | 3ac0e9238308d8acea506fc95679fe8d5ae44f45 (patch) | |
tree | 189238754a1c09e66dcdd811efb32aef9c5dfe1a /src | |
parent | a6858acfdead6285a010a31b1fc7c8a730e91788 (diff) | |
download | mpd-3ac0e9238308d8acea506fc95679fe8d5ae44f45.tar.gz mpd-3ac0e9238308d8acea506fc95679fe8d5ae44f45.tar.xz mpd-3ac0e9238308d8acea506fc95679fe8d5ae44f45.zip |
playlist: hide direct accesses in inline functions
Added functions like playlist_length(), playlist_get(),
playlist_replace(), playlist_remove(). Don't access the
mpdclient_playlist struct directly.
Diffstat (limited to 'src')
-rw-r--r-- | src/mpdclient.c | 39 | ||||
-rw-r--r-- | src/playlist.c | 17 | ||||
-rw-r--r-- | src/playlist.h | 73 |
3 files changed, 92 insertions, 37 deletions
diff --git a/src/mpdclient.c b/src/mpdclient.c index 8449ca42b..7c2cd32f7 100644 --- a/src/mpdclient.c +++ b/src/mpdclient.c @@ -255,7 +255,7 @@ mpdclient_update(mpdclient_t *c) /* check if the playlist needs an update */ if (c->playlist.id != c->status->playlist) { - if (c->playlist.list) + if (playlist_is_empty(&c->playlist)) retval = mpdclient_playlist_update_changes(c); else retval = mpdclient_playlist_update(c); @@ -421,8 +421,7 @@ mpdclient_cmd_add(mpdclient_t *c, struct mpd_song *song) #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD /* add the song to playlist */ - c->playlist.list = g_list_append(c->playlist.list, mpd_songDup(song)); - c->playlist.length++; + playlist_append(&c->playlist, song); /* increment the playlist id, so we dont retrives a new playlist */ c->playlist.id++; @@ -460,7 +459,7 @@ mpdclient_cmd_delete(mpdclient_t *c, gint idx) c->playlist.id++; /* remove the song from the playlist */ - g_array_remove_index(c->playlist.list, idx); + playlist_remove(&c->playlist, idx); /* call playlist updated callback */ mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song); @@ -471,9 +470,6 @@ mpdclient_cmd_delete(mpdclient_t *c, gint idx) c->need_update = TRUE; } - /* free song */ - mpd_freeSong(song); - #else c->need_update = TRUE; #endif @@ -506,13 +502,8 @@ mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index) return n; #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE - /* update the songs position field */ - n = song1->pos; - song1->pos = song2->pos; - song2->pos = n; - /* update the array */ - g_array_index(c->playlist.list, struct mpd_song *, old_index) = song2; - g_array_index(c->playlist.list, struct mpd_song *, new_index) = song1; + /* update the playlist */ + playlist_swap(&c->playlist, old_index, new_index); /* increment the playlist id, so we dont retrives a new playlist */ c->playlist.id++; @@ -666,10 +657,9 @@ mpdclient_playlist_update(mpdclient_t *c) mpd_sendPlaylistInfoCommand(c->connection,-1); while ((entity = mpd_getNextInfoEntity(c->connection))) { - if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) { - struct mpd_song *song = mpd_songDup(entity->info.song); - g_array_append_val(c->playlist.list, song); - } + if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) + playlist_append(&c->playlist, entity->info.song); + mpd_freeInfoEntity(entity); } @@ -700,20 +690,17 @@ mpdclient_playlist_update_changes(mpdclient_t *c) mpd_sendPlChangesCommand(c->connection, c->playlist.id); while ((entity = mpd_getNextInfoEntity(c->connection)) != NULL) { - struct mpd_song *song = mpd_songDup(entity->info.song); + struct mpd_song *song = entity->info.song; if (song->pos >= 0 && (guint)song->pos < c->playlist.list->len) { /* update song */ D("updating pos:%d, id=%d - %s\n", song->pos, song->id, song->file); - mpd_freeSong(g_array_index(c->playlist.list, - struct mpd_song *, song->pos)); - g_array_index(c->playlist.list, - struct mpd_song *, song->pos) = song; + playlist_replace(&c->playlist, song->pos, song); } else { /* add a new song */ D("adding song at pos %d\n", song->pos); - g_array_append_val(c->playlist.list, song); + playlist_append(&c->playlist, song); } mpd_freeInfoEntity(entity); @@ -722,12 +709,10 @@ mpdclient_playlist_update_changes(mpdclient_t *c) /* remove trailing songs */ while ((guint)c->status->playlistLength < c->playlist.list->len) { guint pos = c->playlist.list->len - 1; - struct mpd_song *song = g_array_index(c->playlist.list, struct mpd_song *, pos); /* Remove the last playlist entry */ D("removing song at pos %d\n", pos); - mpd_freeSong(song); - g_array_remove_index(c->playlist.list, pos); + playlist_remove(&c->playlist, pos); } c->song = NULL; diff --git a/src/playlist.c b/src/playlist.c index b600343d7..d14f870b0 100644 --- a/src/playlist.c +++ b/src/playlist.c @@ -44,7 +44,8 @@ playlist_clear(struct mpdclient_playlist *playlist) guint i; for (i = 0; i < playlist->list->len; ++i) { - struct mpd_song *song = g_array_index(playlist->list, struct mpd_song *, i); + struct mpd_song *song = playlist_get(playlist, i); + mpd_freeSong(song); } @@ -69,7 +70,7 @@ playlist_get_song(mpdclient_t *c, gint idx) if (idx < 0 || (guint)idx >= c->playlist.list->len) return NULL; - return g_array_index(c->playlist.list, struct mpd_song *, idx); + return playlist_get(&c->playlist, idx); } struct mpd_song * @@ -78,8 +79,7 @@ playlist_lookup_song(mpdclient_t *c, gint id) guint i; for (i = 0; i < c->playlist.list->len; ++i) { - struct mpd_song *song = g_array_index(c->playlist.list, - struct mpd_song *, i); + struct mpd_song *song = playlist_get(&c->playlist, i); if (song->id == id) return song; } @@ -93,8 +93,7 @@ playlist_get_index(mpdclient_t *c, struct mpd_song *song) guint i; for (i = 0; i < c->playlist.list->len; ++i) { - if (g_array_index(c->playlist.list, struct mpd_song *, i) - == song) + if (playlist_get(&c->playlist, i) == song) return (gint)i; } @@ -107,8 +106,7 @@ playlist_get_index_from_id(mpdclient_t *c, gint id) guint i; for (i = 0; i < c->playlist.list->len; ++i) { - struct mpd_song *song = g_array_index(c->playlist.list, - struct mpd_song *, i); + struct mpd_song *song = playlist_get(&c->playlist, i); if (song->id == id) return (gint)i; } @@ -122,8 +120,7 @@ playlist_get_index_from_file(mpdclient_t *c, gchar *filename) guint i; for (i = 0; i < c->playlist.list->len; ++i) { - struct mpd_song *song = g_array_index(c->playlist.list, - struct mpd_song *, i); + struct mpd_song *song = playlist_get(&c->playlist, i); if(strcmp(song->file, filename) == 0) return (gint)i; } diff --git a/src/playlist.h b/src/playlist.h index 946ca631c..377ed84a2 100644 --- a/src/playlist.h +++ b/src/playlist.h @@ -24,6 +24,7 @@ #include "libmpdclient.h" +#include <assert.h> #include <glib.h> struct mpdclient; @@ -49,6 +50,78 @@ playlist_clear(struct mpdclient_playlist *playlist); /* free a playlist */ gint mpdclient_playlist_free(mpdclient_playlist_t *playlist); +static inline guint +playlist_length(const struct mpdclient_playlist *playlist) +{ + assert(playlist != NULL); + assert(playlist->list != NULL); + + return playlist->list->len; +} + +static inline gboolean +playlist_is_empty(const struct mpdclient_playlist *playlist) +{ + return playlist_length(playlist) == 0; +} + +static inline struct mpd_song * +playlist_get(const struct mpdclient_playlist *playlist, guint idx) +{ + assert(idx < playlist_length(playlist)); + + return g_array_index(playlist->list, struct mpd_song *, idx); +} + +static inline void +playlist_append(struct mpdclient_playlist *playlist, const mpd_Song *song) +{ + mpd_Song *song2 = mpd_songDup(song); + g_array_append_val(playlist->list, song2); +} + +static inline void +playlist_set(const struct mpdclient_playlist *playlist, guint idx, + const mpd_Song *song) +{ + assert(idx < playlist_length(playlist)); + + g_array_index(playlist->list, mpd_Song *, idx) = mpd_songDup(song); +} + +static inline void +playlist_replace(struct mpdclient_playlist *playlist, guint idx, + const mpd_Song *song) +{ + mpd_freeSong(playlist_get(playlist, idx)); + playlist_set(playlist, idx, song); +} + +static inline void +playlist_remove(struct mpdclient_playlist *playlist, guint idx) +{ + mpd_Song *song = playlist_get(playlist, idx); + mpd_freeSong(song); + g_array_remove_index(playlist->list, idx); +} + +static inline void +playlist_swap(struct mpdclient_playlist *playlist, guint idx1, guint idx2) +{ + mpd_Song *song1 = playlist_get(playlist, idx1); + mpd_Song *song2 = playlist_get(playlist, idx2); + gint n; + + /* update the songs position field */ + n = song1->pos; + song1->pos = song2->pos; + song2->pos = n; + + /* update the array */ + g_array_index(playlist->list, struct mpd_song *, idx1) = song2; + g_array_index(playlist->list, struct mpd_song *, idx2) = song1; +} + struct mpd_song *playlist_lookup_song(struct mpdclient *c, gint id); struct mpd_song *playlist_get_song(struct mpdclient *c, gint index); |