SongSort.cxx 2.55 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "SongSort.hxx"
22
#include "Song.hxx"
23
#include "tag/Tag.hxx"
24
#include "lib/icu/Collate.hxx"
25 26

#include <stdlib.h>
27

28
static int
29
compare_utf8_string(const char *a, const char *b) noexcept
30
{
31 32
	if (a == nullptr)
		return b == nullptr ? 0 : -1;
33

34
	if (b == nullptr)
35 36
		return 1;

37
	return IcuCollate(a, b);
38 39 40 41
}

/**
 * Compare two string tag values, ignoring case.  Either one may be
42
 * nullptr.
43 44
 */
static int
45
compare_string_tag_item(const Tag &a, const Tag &b, TagType type) noexcept
46
{
47 48
	return compare_utf8_string(a.GetValue(type),
				   b.GetValue(type));
49 50
}

51 52
/**
 * Compare two tag values which should contain an integer value
53
 * (e.g. disc or track number).  Either one may be nullptr.
54 55
 */
static int
56
compare_number_string(const char *a, const char *b) noexcept
57
{
58 59
	long ai = a == nullptr ? 0 : strtol(a, nullptr, 10);
	long bi = b == nullptr ? 0 : strtol(b, nullptr, 10);
60 61 62 63 64 65 66 67 68 69 70

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

	if (bi <= 0)
		return 1;

	return ai - bi;
}

static int
71
compare_tag_item(const Tag &a, const Tag &b, TagType type) noexcept
72
{
73 74
	return compare_number_string(a.GetValue(type),
				     b.GetValue(type));
75 76
}

77
/* Only used for sorting/searchin a songvec, not general purpose compares */
78 79
gcc_pure
static bool
80
song_cmp(const Song &a, const Song &b) noexcept
81
{
82 83
	int ret;

84
	/* first sort by album */
85
	ret = compare_string_tag_item(a.tag, b.tag, TAG_ALBUM);
86
	if (ret != 0)
87
		return ret < 0;
88 89

	/* then sort by disc */
90
	ret = compare_tag_item(a.tag, b.tag, TAG_DISC);
91
	if (ret != 0)
92
		return ret < 0;
93 94

	/* then by track number */
95
	ret = compare_tag_item(a.tag, b.tag, TAG_TRACK);
96
	if (ret != 0)
97
		return ret < 0;
98 99

	/* still no difference?  compare file name */
100
	return IcuCollate(a.uri, b.uri) < 0;
101 102
}

103
void
104
song_list_sort(SongList &songs) noexcept
105
{
106
	songs.sort(song_cmp);
107
}