2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2011-01-29 10:13:54 +01:00
|
|
|
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2008-09-23 20:48:39 +02:00
|
|
|
#include "songvec.h"
|
2008-10-08 10:49:11 +02:00
|
|
|
#include "song.h"
|
2009-01-15 00:22:30 +01:00
|
|
|
#include "tag.h"
|
2012-01-21 10:47:13 +01:00
|
|
|
#include "db_lock.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
|
|
|
|
2009-10-31 17:32:00 +01:00
|
|
|
static const char *
|
|
|
|
tag_get_value_checked(const struct tag *tag, enum tag_type type)
|
|
|
|
{
|
|
|
|
return tag != NULL
|
|
|
|
? tag_get_value(tag, type)
|
|
|
|
: NULL;
|
|
|
|
}
|
|
|
|
|
2009-10-31 17:36:56 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2009-01-15 00:22:30 +01:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2009-10-31 17:32:00 +01:00
|
|
|
return compare_number_string(tag_get_value_checked(a, type),
|
|
|
|
tag_get_value_checked(b, type));
|
2009-01-15 00:22:30 +01: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:22:30 +01:00
|
|
|
int ret;
|
|
|
|
|
2009-10-31 17:36:56 +01:00
|
|
|
/* first sort by album */
|
|
|
|
ret = compare_string_tag_item(a->tag, b->tag, TAG_ALBUM);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* then sort by disc */
|
2009-10-13 16:12:45 +02:00
|
|
|
ret = compare_tag_item(a->tag, b->tag, TAG_DISC);
|
2009-01-15 00:22:30 +01:00
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* then by track number */
|
2009-10-13 16:12:45 +02:00
|
|
|
ret = compare_tag_item(a->tag, b->tag, TAG_TRACK);
|
2009-01-15 00:22:30 +01:00
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* still no difference? compare file name */
|
2009-10-13 18:01:06 +02:00
|
|
|
return g_utf8_collate(a->uri, b->uri);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void songvec_sort(struct songvec *sv)
|
|
|
|
{
|
2012-01-21 10:47:13 +01:00
|
|
|
db_lock();
|
2008-10-08 10:49:11 +02:00
|
|
|
qsort(sv->base, sv->nr, sizeof(struct song *), songvec_cmp);
|
2012-01-21 10:47:13 +01:00
|
|
|
db_unlock();
|
2008-09-23 20:48:39 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
struct song *
|
2009-10-13 18:01:06 +02:00
|
|
|
songvec_find(const struct songvec *sv, const char *uri)
|
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
|
|
|
|
2012-01-21 10:47:13 +01:00
|
|
|
db_lock();
|
2008-10-07 22:07:34 +02:00
|
|
|
for (i = sv->nr; --i >= 0; ) {
|
2009-10-13 18:01:06 +02:00
|
|
|
if (strcmp(sv->base[i]->uri, uri))
|
2008-10-07 22:07:34 +02:00
|
|
|
continue;
|
|
|
|
ret = sv->base[i];
|
|
|
|
break;
|
|
|
|
}
|
2012-01-21 10:47:13 +01:00
|
|
|
db_unlock();
|
2008-10-07 22:07:34 +02:00
|
|
|
return ret;
|
2008-09-23 20:48:39 +02:00
|
|
|
}
|
|
|
|
|
2012-01-24 18:06:25 +01:00
|
|
|
void
|
2008-10-08 10:49:11 +02:00
|
|
|
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
|
|
|
|
2012-01-21 10:47:13 +01:00
|
|
|
db_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
|
|
|
}
|
2012-01-21 10:47:13 +01:00
|
|
|
db_unlock();
|
2012-01-24 18:06:25 +01:00
|
|
|
return;
|
2008-09-23 20:48:39 +02:00
|
|
|
}
|
2012-01-21 10:47:13 +01:00
|
|
|
db_unlock();
|
2008-09-23 20:48:39 +02:00
|
|
|
|
2012-01-24 18:06:25 +01:00
|
|
|
assert(false);
|
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
|
|
|
{
|
2012-01-21 10:47:13 +01:00
|
|
|
db_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;
|
2012-01-21 10:47:13 +01:00
|
|
|
db_unlock();
|
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
|
|
|
{
|
2012-01-21 10:47:13 +01:00
|
|
|
db_lock();
|
2008-10-13 09:55:07 +02:00
|
|
|
sv->nr = 0;
|
2012-01-21 10:47:13 +01:00
|
|
|
db_unlock();
|
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
|
|
|
|
2012-01-21 10:47:13 +01:00
|
|
|
db_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);
|
2009-10-13 18:01:06 +02:00
|
|
|
assert(*song->uri);
|
2008-10-07 22:07:34 +02:00
|
|
|
|
2008-10-21 01:35:11 +02:00
|
|
|
prev_nr = sv->nr;
|
2012-01-21 10:47:13 +01:00
|
|
|
db_unlock(); /* 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;
|
2012-01-21 10:47:13 +01:00
|
|
|
db_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
|
|
|
}
|
2012-01-21 10:47:13 +01:00
|
|
|
db_unlock();
|
2008-10-07 22:07:29 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|