songvec.c 3.39 KB
Newer Older
1
#include "songvec.h"
2
#include "song.h"
3
#include "tag.h"
4 5

#include <glib.h>
6

7 8
#include <assert.h>
#include <string.h>
9
#include <stdlib.h>
10

11
static GMutex *nr_lock = NULL;
12

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/**
 * Compare two tag values which should contain an integer value
 * (e.g. disc or track number).  Either one may be NULL.
 */
static int
compare_number_string(const char *a, const char *b)
{
	long ai = a == NULL ? 0 : strtol(a, NULL, 10);
	long bi = b == NULL ? 0 : strtol(b, NULL, 10);

	if (ai <= 0)
		return bi <= 0 ? 0 : -1;

	if (bi <= 0)
		return 1;

	return ai - bi;
}

static int
compare_tag_item(const struct tag *a, const struct tag *b, enum tag_type type)
{
	if (a == NULL)
		return b == NULL ? 0 : -1;

	if (b == NULL)
		return 1;

	return compare_number_string(tag_get_value(a, type),
				     tag_get_value(b, type));
}

45 46 47
/* Only used for sorting/searchin a songvec, not general purpose compares */
static int songvec_cmp(const void *s1, const void *s2)
{
48 49
	const struct song *a = ((const struct song * const *)s1)[0];
	const struct song *b = ((const struct song * const *)s2)[0];
50 51 52 53 54 55 56 57 58 59 60 61 62
	int ret;

	/* first sort by disc */
	ret = compare_tag_item(a->tag, b->tag, TAG_ITEM_DISC);
	if (ret != 0)
		return ret;

	/* then by track number */
	ret = compare_tag_item(a->tag, b->tag, TAG_ITEM_TRACK);
	if (ret != 0)
		return ret;

	/* still no difference?  compare file name */
63
	return g_utf8_collate(a->url, b->url);
64 65
}

66
static size_t sv_size(const struct songvec *sv)
67
{
68
	return sv->nr * sizeof(struct song *);
69 70
}

71 72 73 74 75 76 77 78 79 80 81 82 83
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;
}

84 85
void songvec_sort(struct songvec *sv)
{
86
	g_mutex_lock(nr_lock);
87
	qsort(sv->base, sv->nr, sizeof(struct song *), songvec_cmp);
88
	g_mutex_unlock(nr_lock);
89 90
}

91
struct song *
92
songvec_find(const struct songvec *sv, const char *url)
93 94
{
	int i;
95
	struct song *ret = NULL;
96

97
	g_mutex_lock(nr_lock);
98 99 100 101 102 103
	for (i = sv->nr; --i >= 0; ) {
		if (strcmp(sv->base[i]->url, url))
			continue;
		ret = sv->base[i];
		break;
	}
104
	g_mutex_unlock(nr_lock);
105
	return ret;
106 107
}

108 109
int
songvec_delete(struct songvec *sv, const struct song *del)
110
{
111
	size_t i;
112

113
	g_mutex_lock(nr_lock);
114
	for (i = 0; i < sv->nr; ++i) {
115 116
		if (sv->base[i] != del)
			continue;
117
		/* we _don't_ call song_free() here */
118
		if (!--sv->nr) {
119
			g_free(sv->base);
120 121 122
			sv->base = NULL;
		} else {
			memmove(&sv->base[i], &sv->base[i + 1],
123
				(sv->nr - i) * sizeof(struct song *));
124
			sv->base = g_realloc(sv->base, sv_size(sv));
125
		}
126
		g_mutex_unlock(nr_lock);
127
		return i;
128
	}
129
	g_mutex_unlock(nr_lock);
130

131
	return -1; /* not found */
132 133
}

134 135
void
songvec_add(struct songvec *sv, struct song *add)
136
{
137
	g_mutex_lock(nr_lock);
138
	++sv->nr;
139
	sv->base = g_realloc(sv->base, sv_size(sv));
140
	sv->base[sv->nr - 1] = add;
141
	g_mutex_unlock(nr_lock);
142 143
}

144
void songvec_destroy(struct songvec *sv)
145
{
146
	g_mutex_lock(nr_lock);
147
	sv->nr = 0;
148
	g_mutex_unlock(nr_lock);
149 150 151

	g_free(sv->base);
	sv->base = NULL;
152
}
153

154 155
int
songvec_for_each(const struct songvec *sv,
156
		 int (*fn)(struct song *, void *), void *arg)
157
{
158
	size_t i;
159
	size_t prev_nr;
160

161
	g_mutex_lock(nr_lock);
162
	for (i = 0; i < sv->nr; ) {
163
		struct song *song = sv->base[i];
164 165 166 167

		assert(song);
		assert(*song->url);

168
		prev_nr = sv->nr;
169
		g_mutex_unlock(nr_lock); /* fn() may block */
170
		if (fn(song, arg) < 0)
171
			return -1;
172
		g_mutex_lock(nr_lock); /* sv->nr may change in fn() */
173 174
		if (prev_nr == sv->nr)
			++i;
175
	}
176
	g_mutex_unlock(nr_lock);
177 178 179

	return 0;
}