stats: convert to C++

This commit is contained in:
Max Kellermann 2012-08-02 18:55:53 +02:00
parent 8bdf7917c4
commit 0a3ada4fea
2 changed files with 32 additions and 27 deletions

View File

@ -338,7 +338,7 @@ src_mpd_SOURCES = \
src/resolver.c src/resolver.h \ src/resolver.c src/resolver.h \
src/socket_util.c \ src/socket_util.c \
src/state_file.c \ src/state_file.c \
src/stats.c \ src/Stats.cxx \
src/tag.c \ src/tag.c \
src/tag_pool.c \ src/tag_pool.c \
src/tag_print.c \ src/tag_print.c \

View File

@ -18,15 +18,24 @@
*/ */
#include "config.h" #include "config.h"
extern "C" {
#include "stats.h" #include "stats.h"
#include "database.h" #include "database.h"
#include "db_visitor.h" #include "db_selection.h"
#include "tag.h" #include "tag.h"
#include "song.h" #include "song.h"
#include "client.h" #include "client.h"
#include "player_control.h" #include "player_control.h"
#include "strset.h" #include "strset.h"
#include "client_internal.h" #include "client_internal.h"
}
#include "DatabaseGlue.hxx"
#include "DatabasePlugin.hxx"
#include <functional>
#include <set>
struct stats stats; struct stats stats;
@ -40,13 +49,17 @@ void stats_global_finish(void)
g_timer_destroy(stats.timer); g_timer_destroy(stats.timer);
} }
struct visit_data { struct StringLess {
struct strset *artists; gcc_pure
struct strset *albums; bool operator()(const char *a, const char *b) const {
return strcmp(a, b) < 0;
}
}; };
typedef std::set<const char *, StringLess> StringSet;
static void static void
visit_tag(struct visit_data *data, const struct tag *tag) visit_tag(StringSet &artists, StringSet &albums, const struct tag *tag)
{ {
if (tag->time > 0) if (tag->time > 0)
stats.song_duration += tag->time; stats.song_duration += tag->time;
@ -56,11 +69,11 @@ visit_tag(struct visit_data *data, const struct tag *tag)
switch (item->type) { switch (item->type) {
case TAG_ARTIST: case TAG_ARTIST:
strset_add(data->artists, item->value); artists.insert(item->value);
break; break;
case TAG_ALBUM: case TAG_ALBUM:
strset_add(data->albums, item->value); albums.insert(item->value);
break; break;
default: default:
@ -70,41 +83,33 @@ visit_tag(struct visit_data *data, const struct tag *tag)
} }
static bool static bool
collect_stats_song(struct song *song, void *_data, collect_stats_song(StringSet &artists, StringSet &albums, struct song *song)
G_GNUC_UNUSED GError **error_r)
{ {
struct visit_data *data = _data;
++stats.song_count; ++stats.song_count;
if (song->tag != NULL) if (song->tag != NULL)
visit_tag(data, song->tag); visit_tag(artists, albums, song->tag);
return true; return true;
} }
static const struct db_visitor collect_stats_visitor = {
.song = collect_stats_song,
};
void stats_update(void) void stats_update(void)
{ {
struct visit_data data;
stats.song_count = 0; stats.song_count = 0;
stats.song_duration = 0; stats.song_duration = 0;
stats.artist_count = 0; stats.artist_count = 0;
data.artists = strset_new(); struct db_selection selection;
data.albums = strset_new(); db_selection_init(&selection, "", true);
db_walk("", &collect_stats_visitor, &data, NULL); StringSet artists, albums;
using namespace std::placeholders;
const auto f = std::bind(collect_stats_song,
std::ref(artists), std::ref(albums), _1);
GetDatabase()->Visit(&selection, f, NULL);
stats.artist_count = strset_size(data.artists); stats.artist_count = artists.size();
stats.album_count = strset_size(data.albums); stats.album_count = albums.size();
strset_free(data.artists);
strset_free(data.albums);
} }
int stats_print(struct client *client) int stats_print(struct client *client)