2008-09-23 20:48:39 +02:00
|
|
|
#include "songvec.h"
|
|
|
|
#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)
|
|
|
|
{
|
|
|
|
const Song *a = ((const Song * const *)s1)[0];
|
|
|
|
const Song *b = ((const Song * const *)s2)[0];
|
|
|
|
return strcmp(a->url, b->url);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t sv_size(struct songvec *sv)
|
|
|
|
{
|
|
|
|
return sv->nr * sizeof(Song *);
|
|
|
|
}
|
|
|
|
|
|
|
|
void songvec_sort(struct songvec *sv)
|
|
|
|
{
|
2008-10-07 22:07:34 +02:00
|
|
|
pthread_mutex_lock(&nr_lock);
|
2008-09-23 20:48:39 +02:00
|
|
|
qsort(sv->base, sv->nr, sizeof(Song *), songvec_cmp);
|
2008-10-07 22:07:34 +02:00
|
|
|
pthread_mutex_unlock(&nr_lock);
|
2008-09-23 20:48:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Song *songvec_find(struct songvec *sv, const char *url)
|
|
|
|
{
|
|
|
|
int i;
|
2008-10-07 22:07:34 +02:00
|
|
|
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-09-29 13:17:30 +02:00
|
|
|
int songvec_delete(struct songvec *sv, const 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],
|
|
|
|
(sv->nr - i + 1) * sizeof(Song *));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void songvec_add(struct songvec *sv, Song *add)
|
|
|
|
{
|
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
|
|
|
|
|
|
|
int songvec_for_each(struct songvec *sv, int (*fn)(Song *, void *), void *arg)
|
|
|
|
{
|
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) {
|
|
|
|
Song *song = sv->base[i];
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|