sad

simple audio daemon
git clone git://git.2f30.org/sad.git
Log | Files | Refs | LICENSE

commit 31e2c7e6f831a591244ba81569074b1ef79e6522
parent 2a2c264897eb9efdfe1884a57e3b1c2756666446
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Fri Dec 26 13:53:29 +0100

playlist: dynamic list, grow by 4096

Diffstat:
playlist.c | 35+++++++++++++++++++++++------------
sad.h | 3++-
2 files changed, 25 insertions(+), 13 deletions(-)
diff --git a/playlist.c b/playlist.c @@ -1,7 +1,9 @@ #include <sys/select.h> +#include <err.h> #include <limits.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include "sad.h" @@ -18,10 +20,19 @@ initplaylist(void) Song * addplaylist(const char *path) { - Song *s; + Song *s; + Song **p; + + if (!playlist.nsongs || playlist.nsongs + 1 > playlist.maxsongs) { + playlist.maxsongs += 4096; + if (!(p = reallocarray(playlist.songs, playlist.maxsongs, sizeof(Song *)))) + err(1, "reallocarray"); + playlist.songs = p; + } + if (!(s = calloc(1, sizeof(Song)))) + err(1, "calloc"); - /* TODO: should expand the array dynamically */ - s = &playlist.songs[playlist.nsongs]; + playlist.songs[playlist.nsongs] = s; strncpy(s->path, path, sizeof(s->path)); s->path[sizeof(s->path) - 1] = '\0'; s->id = rollingid++; @@ -37,7 +48,7 @@ findsong(const char *path) int i; for (i = 0; i < playlist.nsongs; i++) { - s = &playlist.songs[i]; + s = playlist.songs[i]; if (!strcmp(s->path, path)) return s; } @@ -51,7 +62,7 @@ findsongid(int id) int i; for (i = 0; i < playlist.nsongs; i++) { - s = &playlist.songs[i]; + s = playlist.songs[i]; if (s->id == id) return s; } @@ -66,16 +77,16 @@ getnextsong(void) cur = playlist.cursong; for (i = 0; i < playlist.nsongs; i++) { - s = &playlist.songs[i]; + s = playlist.songs[i]; if (s->id == cur->id) break; } if (i == playlist.nsongs) return NULL; if (i == playlist.nsongs - 1) - s = &playlist.songs[0]; + s = playlist.songs[0]; else - s = &playlist.songs[i + 1]; + s = playlist.songs[i + 1]; return s; } @@ -87,16 +98,16 @@ getprevsong(void) cur = playlist.cursong; for (i = 0; i < playlist.nsongs; i++) { - s = &playlist.songs[i]; + s = playlist.songs[i]; if (s->id == cur->id) break; } if (i == playlist.nsongs) return NULL; if (i == 0) - s = &playlist.songs[playlist.nsongs - 1]; + s = playlist.songs[playlist.nsongs - 1]; else - s = &playlist.songs[i - 1]; + s = playlist.songs[i - 1]; return s; } @@ -119,7 +130,7 @@ dumpplaylist(int fd) int i; for (i = 0; i < playlist.nsongs; i++) { - s = &playlist.songs[i]; + s = playlist.songs[i]; dprintf(fd, "%d: %s\n", s->id, s->path); } } diff --git a/sad.h b/sad.h @@ -20,9 +20,10 @@ typedef struct { } Song; typedef struct { - Song songs[4096]; + Song **songs; Song *cursong; size_t nsongs; + size_t maxsongs; } Playlist; typedef struct {