2008-09-23 20:48:39 +02:00
|
|
|
#include "songvec.h"
|
2008-10-08 10:49:11 +02:00
|
|
|
#include "song.h"
|
2009-01-03 14:53:02 +01:00
|
|
|
|
|
|
|
#include <glib.h>
|
2008-09-23 20:48:39 +02:00
|
|
|
|
2008-10-08 10:49:29 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2009-01-03 14:53:02 +01:00
|
|
|
#include <stdlib.h>
|
2008-10-08 10:49:29 +02:00
|
|
|
|
2008-12-28 21:02:42 +01:00
|
|
|
static GMutex *nr_lock = NULL;
|
2008-10-07 22:07:34 +02:00
|
|
|
|
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];
|
2009-01-15 00:09:18 +01:00
|
|
|
return g_utf8_collate(a->url, b->url);
|
2008-09-23 20:48:39 +02:00
|
|
|
}
|
|
|
|
|
2008-10-21 01:31:36 +02:00
|
|
|
static size_t sv_size(const struct songvec *sv)
|
2008-09-23 20:48:39 +02:00
|
|
|
{
|
2008-10-08 10:49:11 +02:00
|
|
|
return sv->nr * sizeof(struct song *);
|
2008-09-23 20:48:39 +02:00
|
|
|
}
|
|
|
|
|
2008-12-28 21:02:42 +01:00
|
|
|
void songvec_init(void)
|
|
|
|
{
|
|
|
|
g_assert(nr_lock == NULL);
|
|
|
|
nr_lock = g_mutex_new();
|
|
|
|
}
|
|
|
|
|
|
|
|
void songvec_deinit(void)
|
|
|
|
{
|
|
|
|
g_assert(nr_lock != NULL);
|
|
|
|
g_mutex_free(nr_lock);
|
|
|
|
nr_lock = NULL;
|
|
|
|
}
|
|
|
|
|
2008-09-23 20:48:39 +02:00
|
|
|
void songvec_sort(struct songvec *sv)
|
|
|
|
{
|
2008-12-28 21:02:42 +01:00
|
|
|
g_mutex_lock(nr_lock);
|
2008-10-08 10:49:11 +02:00
|
|
|
qsort(sv->base, sv->nr, sizeof(struct song *), songvec_cmp);
|
2008-12-28 21:02:42 +01:00
|
|
|
g_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-12-28 21:02:42 +01:00
|
|
|
g_mutex_lock(nr_lock);
|
2008-10-07 22:07:34 +02:00
|
|
|
for (i = sv->nr; --i >= 0; ) {
|
|
|
|
if (strcmp(sv->base[i]->url, url))
|
|
|
|
continue;
|
|
|
|
ret = sv->base[i];
|
|
|
|
break;
|
|
|
|
}
|
2008-12-28 21:02:42 +01:00
|
|
|
g_mutex_unlock(nr_lock);
|
2008-10-07 22:07:34 +02:00
|
|
|
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
|
|
|
{
|
2008-10-14 11:10:00 +02:00
|
|
|
size_t i;
|
2008-09-23 20:48:39 +02:00
|
|
|
|
2008-12-28 21:02:42 +01:00
|
|
|
g_mutex_lock(nr_lock);
|
2008-10-14 11:10:00 +02:00
|
|
|
for (i = 0; i < sv->nr; ++i) {
|
2008-09-23 20:48:39 +02:00
|
|
|
if (sv->base[i] != del)
|
|
|
|
continue;
|
2008-10-13 09:55:07 +02:00
|
|
|
/* we _don't_ call song_free() here */
|
2008-09-23 20:48:39 +02:00
|
|
|
if (!--sv->nr) {
|
2009-01-03 14:53:02 +01:00
|
|
|
g_free(sv->base);
|
2008-09-23 20:48:39 +02:00
|
|
|
sv->base = NULL;
|
|
|
|
} else {
|
|
|
|
memmove(&sv->base[i], &sv->base[i + 1],
|
2008-10-14 11:10:00 +02:00
|
|
|
(sv->nr - i) * sizeof(struct song *));
|
2009-01-03 14:53:02 +01:00
|
|
|
sv->base = g_realloc(sv->base, sv_size(sv));
|
2008-09-23 20:48:39 +02:00
|
|
|
}
|
2008-12-28 21:02:42 +01:00
|
|
|
g_mutex_unlock(nr_lock);
|
2008-10-14 11:10:00 +02:00
|
|
|
return i;
|
2008-09-23 20:48:39 +02:00
|
|
|
}
|
2008-12-28 21:02:42 +01:00
|
|
|
g_mutex_unlock(nr_lock);
|
2008-09-23 20:48:39 +02:00
|
|
|
|
2008-10-14 11:10:00 +02:00
|
|
|
return -1; /* not found */
|
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-12-28 21:02:42 +01:00
|
|
|
g_mutex_lock(nr_lock);
|
2008-09-23 20:48:39 +02:00
|
|
|
++sv->nr;
|
2009-01-03 14:53:02 +01:00
|
|
|
sv->base = g_realloc(sv->base, sv_size(sv));
|
2008-09-23 20:48:39 +02:00
|
|
|
sv->base[sv->nr - 1] = add;
|
2008-12-28 21:02:42 +01:00
|
|
|
g_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-12-28 21:02:42 +01:00
|
|
|
g_mutex_lock(nr_lock);
|
2008-10-13 09:55:07 +02:00
|
|
|
sv->nr = 0;
|
2008-12-28 21:02:42 +01:00
|
|
|
g_mutex_unlock(nr_lock);
|
2009-01-03 14:53:02 +01:00
|
|
|
|
|
|
|
g_free(sv->base);
|
|
|
|
sv->base = NULL;
|
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-21 01:35:11 +02:00
|
|
|
size_t prev_nr;
|
2008-10-07 22:07:29 +02:00
|
|
|
|
2008-12-28 21:02:42 +01:00
|
|
|
g_mutex_lock(nr_lock);
|
2008-10-21 01:35:11 +02:00
|
|
|
for (i = 0; i < sv->nr; ) {
|
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);
|
|
|
|
|
2008-10-21 01:35:11 +02:00
|
|
|
prev_nr = sv->nr;
|
2008-12-28 21:02:42 +01:00
|
|
|
g_mutex_unlock(nr_lock); /* fn() may block */
|
2008-10-07 22:07:34 +02:00
|
|
|
if (fn(song, arg) < 0)
|
2008-10-07 22:07:29 +02:00
|
|
|
return -1;
|
2008-12-28 21:02:42 +01:00
|
|
|
g_mutex_lock(nr_lock); /* sv->nr may change in fn() */
|
2008-10-21 01:35:11 +02:00
|
|
|
if (prev_nr == sv->nr)
|
|
|
|
++i;
|
2008-10-07 22:07:29 +02:00
|
|
|
}
|
2008-12-28 21:02:42 +01:00
|
|
|
g_mutex_unlock(nr_lock);
|
2008-10-07 22:07:29 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|