2008-09-23 20:48:39 +02:00
|
|
|
#include "songvec.h"
|
2008-10-08 10:49:11 +02:00
|
|
|
#include "song.h"
|
2008-09-23 20:48:39 +02:00
|
|
|
#include "utils.h"
|
|
|
|
|
2008-10-07 22:07:34 +02:00
|
|
|
static pthread_mutex_t nr_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
2008-09-23 20:48:39 +02:00
|
|
|
/* Only used for sorting/searchin a songvec, not general purpose compares */
|
|
|
|
static int songvec_cmp(const void *s1, const void *s2)
|
|
|
|
{
|
2008-10-08 10:49:11 +02:00
|
|
|
const struct song *a = ((const struct song * const *)s1)[0];
|
|
|
|
const struct song *b = ((const struct song * const *)s2)[0];
|
2008-09-23 20:48:39 +02:00
|
|
|
return strcmp(a->url, b->url);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t sv_size(struct songvec *sv)
|
|
|
|
{
|
2008-10-08 10:49:11 +02:00
|
|
|
return sv->nr * sizeof(struct song *);
|
2008-09-23 20:48:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void songvec_sort(struct songvec *sv)
|
|
|
|
{
|
2008-10-07 22:07:34 +02:00
|
|
|
pthread_mutex_lock(&nr_lock);
|
2008-10-08 10:49:11 +02:00
|
|
|
qsort(sv->base, sv->nr, sizeof(struct song *), songvec_cmp);
|
2008-10-07 22:07:34 +02:00
|
|
|
pthread_mutex_unlock(&nr_lock);
|
2008-09-23 20:48:39 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
struct song *
|
2008-10-07 22:09:55 +02:00
|
|
|
songvec_find(const struct songvec *sv, const char *url)
|
2008-09-23 20:48:39 +02:00
|
|
|
{
|
|
|
|
int i;
|
2008-10-08 10:49:11 +02:00
|
|
|
struct song *ret = NULL;
|
2008-09-23 20:48:39 +02:00
|
|
|
|
2008-10-07 22:07:34 +02:00
|
|
|
pthread_mutex_lock(&nr_lock);
|
|
|
|
for (i = sv->nr; --i >= 0; ) {
|
|
|
|
if (strcmp(sv->base[i]->url, url))
|
|
|
|
continue;
|
|
|
|
ret = sv->base[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&nr_lock);
|
|
|
|
return ret;
|
2008-09-23 20:48:39 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
int
|
|
|
|
songvec_delete(struct songvec *sv, const struct song *del)
|
2008-09-23 20:48:39 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2008-10-07 22:07:34 +02:00
|
|
|
pthread_mutex_lock(&nr_lock);
|
2008-09-23 20:48:39 +02:00
|
|
|
for (i = sv->nr; --i >= 0; ) {
|
|
|
|
if (sv->base[i] != del)
|
|
|
|
continue;
|
|
|
|
/* we _don't_ call freeSong() here */
|
|
|
|
if (!--sv->nr) {
|
|
|
|
free(sv->base);
|
|
|
|
sv->base = NULL;
|
|
|
|
} else {
|
|
|
|
memmove(&sv->base[i], &sv->base[i + 1],
|
2008-10-08 10:49:11 +02:00
|
|
|
(sv->nr - i + 1) * sizeof(struct song *));
|
2008-09-23 20:48:39 +02:00
|
|
|
sv->base = xrealloc(sv->base, sv_size(sv));
|
|
|
|
}
|
2008-10-07 22:07:34 +02:00
|
|
|
break;
|
2008-09-23 20:48:39 +02:00
|
|
|
}
|
2008-10-07 22:07:34 +02:00
|
|
|
pthread_mutex_unlock(&nr_lock);
|
2008-09-23 20:48:39 +02:00
|
|
|
|
2008-10-07 22:07:34 +02:00
|
|
|
return i;
|
2008-09-23 20:48:39 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
void
|
|
|
|
songvec_add(struct songvec *sv, struct song *add)
|
2008-09-23 20:48:39 +02:00
|
|
|
{
|
2008-10-07 22:07:34 +02:00
|
|
|
pthread_mutex_lock(&nr_lock);
|
2008-09-23 20:48:39 +02:00
|
|
|
++sv->nr;
|
|
|
|
sv->base = xrealloc(sv->base, sv_size(sv));
|
|
|
|
sv->base[sv->nr - 1] = add;
|
2008-10-07 22:07:34 +02:00
|
|
|
pthread_mutex_unlock(&nr_lock);
|
2008-09-23 20:48:39 +02:00
|
|
|
}
|
|
|
|
|
2008-09-29 12:17:13 +02:00
|
|
|
void songvec_destroy(struct songvec *sv)
|
2008-09-23 20:48:39 +02:00
|
|
|
{
|
2008-10-07 22:07:34 +02:00
|
|
|
pthread_mutex_lock(&nr_lock);
|
2008-09-23 22:38:43 +02:00
|
|
|
if (sv->base) {
|
|
|
|
free(sv->base);
|
|
|
|
sv->base = NULL;
|
|
|
|
}
|
2008-09-23 20:48:39 +02:00
|
|
|
sv->nr = 0;
|
2008-10-07 22:07:34 +02:00
|
|
|
pthread_mutex_unlock(&nr_lock);
|
2008-09-23 20:48:39 +02:00
|
|
|
}
|
2008-10-07 22:07:29 +02:00
|
|
|
|
2008-10-07 22:09:55 +02:00
|
|
|
int
|
|
|
|
songvec_for_each(const struct songvec *sv,
|
2008-10-08 10:49:11 +02:00
|
|
|
int (*fn)(struct song *, void *), void *arg)
|
2008-10-07 22:07:29 +02:00
|
|
|
{
|
2008-10-07 22:07:34 +02:00
|
|
|
size_t i;
|
2008-10-07 22:07:29 +02:00
|
|
|
|
2008-10-07 22:07:34 +02:00
|
|
|
pthread_mutex_lock(&nr_lock);
|
|
|
|
for (i = 0; i < sv->nr; ++i) {
|
2008-10-08 10:49:11 +02:00
|
|
|
struct song *song = sv->base[i];
|
2008-10-07 22:07:34 +02:00
|
|
|
|
|
|
|
assert(song);
|
|
|
|
assert(*song->url);
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&nr_lock); /* fn() may block */
|
|
|
|
if (fn(song, arg) < 0)
|
2008-10-07 22:07:29 +02:00
|
|
|
return -1;
|
2008-10-07 22:07:34 +02:00
|
|
|
pthread_mutex_lock(&nr_lock); /* sv->nr may change in fn() */
|
2008-10-07 22:07:29 +02:00
|
|
|
}
|
2008-10-07 22:07:34 +02:00
|
|
|
pthread_mutex_unlock(&nr_lock);
|
2008-10-07 22:07:29 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|