From ee2e73cd7b5a1de68c8316e916c4ef3a88302bed Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Sun, 4 Aug 2013 18:53:43 +0200 Subject: piano: Generic linked lists MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Introduces generic linked list structure and functions (like append, delete, …). Removes a lot of copy&pasted code and improves code readability/reusability. Heads up: This change breaks libpiano’s ABI. --- src/libpiano/piano.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'src/libpiano/piano.c') diff --git a/src/libpiano/piano.c b/src/libpiano/piano.c index 9020a6a..b519f49 100644 --- a/src/libpiano/piano.c +++ b/src/libpiano/piano.c @@ -1,5 +1,5 @@ /* -Copyright (c) 2008-2012 +Copyright (c) 2008-2013 Lars-Dominik Braun Permission is hereby granted, free of charge, to any person obtaining a copy @@ -80,7 +80,7 @@ static void PianoDestroyArtists (PianoArtist_t *artists) { free (curArtist->musicId); free (curArtist->seedId); lastArtist = curArtist; - curArtist = curArtist->next; + curArtist = (PianoArtist_t *) curArtist->head.next; free (lastArtist); } } @@ -113,7 +113,7 @@ static void PianoDestroyStations (PianoStation_t *stations) { curStation = stations; while (curStation != NULL) { lastStation = curStation; - curStation = curStation->next; + curStation = (PianoStation_t *) curStation->head.next; PianoDestroyStation (lastStation); free (lastStation); } @@ -141,7 +141,7 @@ void PianoDestroyPlaylist (PianoSong_t *playlist) { free (curSong->detailUrl); free (curSong->trackToken); lastSong = curSong; - curSong = curSong->next; + curSong = (PianoSong_t *) curSong->head.next; free (lastSong); } } @@ -163,7 +163,7 @@ static void PianoDestroyGenres (PianoGenre_t *genres) { free (curGenre->name); free (curGenre->musicId); lastGenre = curGenre; - curGenre = curGenre->next; + curGenre = (PianoGenre_t *) curGenre->head.next; free (lastGenre); } } @@ -201,7 +201,7 @@ void PianoDestroy (PianoHandle_t *ph) { PianoDestroyGenres (curGenreCat->genres); free (curGenreCat->name); lastGenreCat = curGenreCat; - curGenreCat = curGenreCat->next; + curGenreCat = (PianoGenreCategory_t *) curGenreCat->head.next; free (lastGenreCat); } memset (ph, 0, sizeof (*ph)); @@ -221,14 +221,17 @@ void PianoDestroyRequest (PianoRequest_t *req) { * @param search for this * @return the first station structure matching the given id */ -PianoStation_t *PianoFindStationById (PianoStation_t *stations, - const char *searchStation) { - while (stations != NULL) { - if (strcmp (stations->id, searchStation) == 0) { - return stations; +PianoStation_t *PianoFindStationById (PianoStation_t * const stations, + const char * const searchStation) { + assert (searchStation != NULL); + + PianoStation_t *currStation = stations; + PianoListForeachP (currStation) { + if (strcmp (currStation->id, searchStation) == 0) { + return currStation; } - stations = stations->next; } + return NULL; } -- cgit v1.2.3