Helpers.cxx 2.12 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.
 */

Max Kellermann's avatar
Max Kellermann committed
20
#include "Helpers.hxx"
21 22
#include "Stats.hxx"
#include "Interface.hxx"
23
#include "LightSong.hxx"
24
#include "tag/Tag.hxx"
25 26 27 28 29 30 31

#include <set>

#include <string.h>

struct StringLess {
	gcc_pure
32
	bool operator()(const char *a, const char *b) const noexcept {
33 34 35 36 37 38
		return strcmp(a, b) < 0;
	}
};

typedef std::set<const char *, StringLess> StringSet;

39 40
static void
StatsVisitTag(DatabaseStats &stats, StringSet &artists, StringSet &albums,
41
	      const Tag &tag) noexcept
42
{
43
	if (!tag.duration.IsNegative())
44
		stats.total_duration += tag.duration;
45

46
	for (const auto &item : tag) {
47 48
		switch (item.type) {
		case TAG_ARTIST:
49
			artists.emplace(item.value);
50 51 52
			break;

		case TAG_ALBUM:
53
			albums.emplace(item.value);
54 55 56 57 58 59 60 61
			break;

		default:
			break;
		}
	}
}

62
static void
63
StatsVisitSong(DatabaseStats &stats, StringSet &artists, StringSet &albums,
64
	       const LightSong &song) noexcept
65 66 67
{
	++stats.song_count;

68
	StatsVisitTag(stats, artists, albums, song.tag);
69 70
}

71 72
DatabaseStats
GetStats(const Database &db, const DatabaseSelection &selection)
73
{
74
	DatabaseStats stats;
75 76 77 78 79 80 81
	stats.Clear();

	StringSet artists, albums;
	using namespace std::placeholders;
	const auto f = std::bind(StatsVisitSong,
				 std::ref(stats), std::ref(artists),
				 std::ref(albums), _1);
82
	db.Visit(selection, f);
83 84 85

	stats.artist_count = artists.size();
	stats.album_count = albums.size();
86
	return stats;
87
}