songvec.c 4.84 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "config.h"
21
#include "songvec.h"
22
#include "song.h"
23
#include "tag.h"
24 25

#include <glib.h>
26

27 28
#include <assert.h>
#include <string.h>
29
#include <stdlib.h>
30

31
static GMutex *nr_lock = NULL;
32

33 34 35 36 37 38 39 40
static const char *
tag_get_value_checked(const struct tag *tag, enum tag_type type)
{
	return tag != NULL
		? tag_get_value(tag, type)
		: NULL;
}

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
static int
compare_utf8_string(const char *a, const char *b)
{
	if (a == NULL)
		return b == NULL ? 0 : -1;

	if (b == NULL)
		return 1;

	return g_utf8_collate(a, b);
}

/**
 * Compare two string tag values, ignoring case.  Either one may be
 * NULL.
 */
static int
compare_string_tag_item(const struct tag *a, const struct tag *b,
			enum tag_type type)
{
	return compare_utf8_string(tag_get_value_checked(a, type),
				   tag_get_value_checked(b, type));
}

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/**
 * 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)
{
87 88
	return compare_number_string(tag_get_value_checked(a, type),
				     tag_get_value_checked(b, type));
89 90
}

91 92 93
/* Only used for sorting/searchin a songvec, not general purpose compares */
static int songvec_cmp(const void *s1, const void *s2)
{
94 95
	const struct song *a = ((const struct song * const *)s1)[0];
	const struct song *b = ((const struct song * const *)s2)[0];
96 97
	int ret;

98 99 100 101 102 103
	/* first sort by album */
	ret = compare_string_tag_item(a->tag, b->tag, TAG_ALBUM);
	if (ret != 0)
		return ret;

	/* then sort by disc */
104
	ret = compare_tag_item(a->tag, b->tag, TAG_DISC);
105 106 107 108
	if (ret != 0)
		return ret;

	/* then by track number */
109
	ret = compare_tag_item(a->tag, b->tag, TAG_TRACK);
110 111 112 113
	if (ret != 0)
		return ret;

	/* still no difference?  compare file name */
114
	return g_utf8_collate(a->uri, b->uri);
115 116
}

117
static size_t sv_size(const struct songvec *sv)
118
{
119
	return sv->nr * sizeof(struct song *);
120 121
}

122 123 124 125 126 127 128 129 130 131 132 133 134
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;
}

135 136
void songvec_sort(struct songvec *sv)
{
137
	g_mutex_lock(nr_lock);
138
	qsort(sv->base, sv->nr, sizeof(struct song *), songvec_cmp);
139
	g_mutex_unlock(nr_lock);
140 141
}

142
struct song *
143
songvec_find(const struct songvec *sv, const char *uri)
144 145
{
	int i;
146
	struct song *ret = NULL;
147

148
	g_mutex_lock(nr_lock);
149
	for (i = sv->nr; --i >= 0; ) {
150
		if (strcmp(sv->base[i]->uri, uri))
151 152 153 154
			continue;
		ret = sv->base[i];
		break;
	}
155
	g_mutex_unlock(nr_lock);
156
	return ret;
157 158
}

159 160
int
songvec_delete(struct songvec *sv, const struct song *del)
161
{
162
	size_t i;
163

164
	g_mutex_lock(nr_lock);
165
	for (i = 0; i < sv->nr; ++i) {
166 167
		if (sv->base[i] != del)
			continue;
168
		/* we _don't_ call song_free() here */
169
		if (!--sv->nr) {
170
			g_free(sv->base);
171 172 173
			sv->base = NULL;
		} else {
			memmove(&sv->base[i], &sv->base[i + 1],
174
				(sv->nr - i) * sizeof(struct song *));
175
			sv->base = g_realloc(sv->base, sv_size(sv));
176
		}
177
		g_mutex_unlock(nr_lock);
178
		return i;
179
	}
180
	g_mutex_unlock(nr_lock);
181

182
	return -1; /* not found */
183 184
}

185 186
void
songvec_add(struct songvec *sv, struct song *add)
187
{
188
	g_mutex_lock(nr_lock);
189
	++sv->nr;
190
	sv->base = g_realloc(sv->base, sv_size(sv));
191
	sv->base[sv->nr - 1] = add;
192
	g_mutex_unlock(nr_lock);
193 194
}

195
void songvec_destroy(struct songvec *sv)
196
{
197
	g_mutex_lock(nr_lock);
198
	sv->nr = 0;
199
	g_mutex_unlock(nr_lock);
200 201 202

	g_free(sv->base);
	sv->base = NULL;
203
}
204

205 206
int
songvec_for_each(const struct songvec *sv,
207
		 int (*fn)(struct song *, void *), void *arg)
208
{
209
	size_t i;
210
	size_t prev_nr;
211

212
	g_mutex_lock(nr_lock);
213
	for (i = 0; i < sv->nr; ) {
214
		struct song *song = sv->base[i];
215 216

		assert(song);
217
		assert(*song->uri);
218

219
		prev_nr = sv->nr;
220
		g_mutex_unlock(nr_lock); /* fn() may block */
221
		if (fn(song, arg) < 0)
222
			return -1;
223
		g_mutex_lock(nr_lock); /* sv->nr may change in fn() */
224 225
		if (prev_nr == sv->nr)
			++i;
226
	}
227
	g_mutex_unlock(nr_lock);
228 229 230

	return 0;
}