Adding count command. It's usage is identical to find, but instead of

returning a list of matching songs, the number of results and total play
time of the results are returned.

git-svn-id: https://svn.musicpd.org/mpd/trunk@5950 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
J. Alexander Treuman 2007-04-25 23:46:11 +00:00
parent 7fd9859540
commit 7a8a9c10e1
4 changed files with 72 additions and 1 deletions

View File

@ -17,6 +17,7 @@ ver 0.13.0 (2007/??/??)
* New tagtypes command for retrieving a list of available tag types * New tagtypes command for retrieving a list of available tag types
* Fix a bug where no ACK was returned if loading a playlist failed * Fix a bug where no ACK was returned if loading a playlist failed
* Fix a bug where db_update in stats would be 0 after initial database creation * Fix a bug where db_update in stats would be 0 after initial database creation
* New count command for getting stats on found songs (similar to "find")
* Lots of bug fixes, cleaned up code, and performance improvements * Lots of bug fixes, cleaned up code, and performance improvements
ver 0.12.2 (2007/3/20) ver 0.12.2 (2007/3/20)

View File

@ -96,6 +96,7 @@
#define COMMAND_PLAYLISTFIND "playlistfind" #define COMMAND_PLAYLISTFIND "playlistfind"
#define COMMAND_PLAYLISTSEARCH "playlistsearch" #define COMMAND_PLAYLISTSEARCH "playlistsearch"
#define COMMAND_TAGTYPES "tagtypes" #define COMMAND_TAGTYPES "tagtypes"
#define COMMAND_COUNT "count"
#define COMMAND_STATUS_VOLUME "volume" #define COMMAND_STATUS_VOLUME "volume"
#define COMMAND_STATUS_STATE "state" #define COMMAND_STATUS_STATE "state"
@ -518,6 +519,27 @@ static int handleSearch(int fd, int *permission, int argc, char *argv[])
return ret; return ret;
} }
static int handleCount(int fd, int *permission, int argc, char *argv[])
{
int ret;
LocateTagItem *items;
int numItems = newLocateTagItemArrayFromArgArray(argv + 1,
argc - 1,
&items);
if (numItems <= 0) {
commandError(fd, ACK_ERROR_ARG, "incorrect arguments");
return -1;
}
ret = searchStatsForSongsIn(fd, NULL, numItems, items);
freeLocateTagItemArray(numItems, items);
return ret;
}
static int handlePlaylistFind(int fd, int *permission, int argc, char *argv[]) static int handlePlaylistFind(int fd, int *permission, int argc, char *argv[])
{ {
LocateTagItem *items; LocateTagItem *items;
@ -1051,6 +1073,7 @@ void initCommands(void)
addCommand(COMMAND_PLAYLISTFIND, PERMISSION_READ, 2, -1, handlePlaylistFind, NULL); addCommand(COMMAND_PLAYLISTFIND, PERMISSION_READ, 2, -1, handlePlaylistFind, NULL);
addCommand(COMMAND_PLAYLISTSEARCH, PERMISSION_READ, 2, -1, handlePlaylistSearch, NULL); addCommand(COMMAND_PLAYLISTSEARCH, PERMISSION_READ, 2, -1, handlePlaylistSearch, NULL);
addCommand(COMMAND_TAGTYPES, PERMISSION_READ, 0, 0, handleTagTypes, NULL); addCommand(COMMAND_TAGTYPES, PERMISSION_READ, 0, 0, handleTagTypes, NULL);
addCommand(COMMAND_COUNT, PERMISSION_READ, 2, -1, handleCount, NULL);
sortList(commandList); sortList(commandList);
} }

View File

@ -38,6 +38,12 @@ typedef struct _LocateTagItemArray {
LocateTagItem *items; LocateTagItem *items;
} LocateTagItemArray; } LocateTagItemArray;
typedef struct _SearchStats {
LocateTagItemArray locateArray;
int numberOfSongs;
int playTime;
} SearchStats;
static int countSongsInDirectory(int fd, Directory * directory, void *data) static int countSongsInDirectory(int fd, Directory * directory, void *data)
{ {
int *count = (int *)data; int *count = (int *)data;
@ -120,6 +126,44 @@ int findSongsIn(int fd, char *name, int numItems, LocateTagItem * items)
return traverseAllIn(fd, name, findInDirectory, NULL, (void *)&array); return traverseAllIn(fd, name, findInDirectory, NULL, (void *)&array);
} }
static void printSearchStats(int fd, SearchStats *stats)
{
fdprintf(fd, "songs: %i\n", stats->numberOfSongs);
fdprintf(fd, "playtime: %i\n", stats->playTime);
}
static int searchStatsInDirectory(int fd, Song * song, void *data)
{
SearchStats *stats = data;
if (tagItemsFoundAndMatches(song, stats->locateArray.numItems,
stats->locateArray.items)) {
stats->numberOfSongs++;
if (song->tag->time > 0)
stats->playTime += song->tag->time;
}
return 0;
}
int searchStatsForSongsIn(int fd, char *name, int numItems,
LocateTagItem * items)
{
SearchStats stats;
int ret;
stats.locateArray.numItems = numItems;
stats.locateArray.items = items;
stats.numberOfSongs = 0;
stats.playTime = 0;
ret = traverseAllIn(fd, name, searchStatsInDirectory, NULL, &stats);
if (ret == 0)
printSearchStats(fd, &stats);
return ret;
}
int printAllIn(int fd, char *name) int printAllIn(int fd, char *name)
{ {
return traverseAllIn(fd, name, printSongInDirectory, return traverseAllIn(fd, name, printSongInDirectory,

View File

@ -32,10 +32,13 @@ int addAllInToStoredPlaylist(int fd, char *name, char *utf8file);
int printInfoForAllIn(int fd, char *name); int printInfoForAllIn(int fd, char *name);
int searchForSongsIn(int fd, char *name, int numItems, int searchForSongsIn(int fd, char *name, int numItems,
LocateTagItem * items); LocateTagItem * items);
int findSongsIn(int fd, char *name, int numItems, LocateTagItem * items); int findSongsIn(int fd, char *name, int numItems, LocateTagItem * items);
int searchStatsForSongsIn(int fd, char *name, int numItems,
LocateTagItem * items);
int countSongsIn(int fd, char *name); int countSongsIn(int fd, char *name);
unsigned long sumSongTimesIn(int fd, char *name); unsigned long sumSongTimesIn(int fd, char *name);