aboutsummaryrefslogtreecommitdiffstats
path: root/src/dbUtils.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/dbUtils.c159
1 files changed, 98 insertions, 61 deletions
diff --git a/src/dbUtils.c b/src/dbUtils.c
index 519c1802d..0eb485f1e 100644
--- a/src/dbUtils.c
+++ b/src/dbUtils.c
@@ -24,7 +24,7 @@
#include "playlist.h"
#include "song.h"
#include "tag.h"
-#include "tagTracker.h"
+#include "strset.h"
#include "log.h"
#include "storedPlaylist.h"
@@ -45,7 +45,7 @@ typedef struct _SearchStats {
unsigned long playTime;
} SearchStats;
-static int countSongsInDirectory(mpd_unused int fd, Directory * directory,
+static int countSongsInDirectory(Directory * directory,
void *data)
{
int *count = (int *)data;
@@ -55,24 +55,32 @@ static int countSongsInDirectory(mpd_unused int fd, Directory * directory,
return 0;
}
-static int printDirectoryInDirectory(int fd, Directory * directory,
- mpd_unused void *data)
+static int printDirectoryInDirectory(Directory * directory, void *data)
{
+ int fd = (int)(size_t)data;
if (directory->path) {
fdprintf(fd, "directory: %s\n", getDirectoryPath(directory));
}
return 0;
}
-static int printSongInDirectory(int fd, Song * song, mpd_unused void *data)
+static int printSongInDirectory(Song * song, mpd_unused void *data)
{
+ int fd = (int)(size_t)data;
printSongUrl(fd, song);
return 0;
}
-static int searchInDirectory(int fd, Song * song, void *data)
+struct search_data {
+ int fd;
+ LocateTagItemArray array;
+};
+
+static int searchInDirectory(Song * song, void *_data)
{
- LocateTagItemArray *array = data;
+ struct search_data *data = _data;
+ int fd = data->fd;
+ LocateTagItemArray *array = &data->array;
if (strstrSearchTags(song, array->numItems, array->items))
printSongInfo(fd, song);
@@ -80,23 +88,25 @@ static int searchInDirectory(int fd, Song * song, void *data)
return 0;
}
-int searchForSongsIn(int fd, char *name, int numItems, LocateTagItem * items)
+int searchForSongsIn(int fd, const char *name, int numItems,
+ LocateTagItem * items)
{
int ret;
int i;
char **originalNeedles = xmalloc(numItems * sizeof(char *));
- LocateTagItemArray array;
+ struct search_data data;
for (i = 0; i < numItems; i++) {
originalNeedles[i] = items[i].needle;
items[i].needle = strDupToUpper(originalNeedles[i]);
}
- array.numItems = numItems;
- array.items = items;
+ data.fd = fd;
+ data.array.numItems = numItems;
+ data.array.items = items;
- ret = traverseAllIn(fd, name, searchInDirectory, NULL, &array);
+ ret = traverseAllIn(name, searchInDirectory, NULL, &data);
for (i = 0; i < numItems; i++) {
free(items[i].needle);
@@ -108,9 +118,11 @@ int searchForSongsIn(int fd, char *name, int numItems, LocateTagItem * items)
return ret;
}
-static int findInDirectory(int fd, Song * song, void *data)
+static int findInDirectory(Song * song, void *_data)
{
- LocateTagItemArray *array = data;
+ struct search_data *data = _data;
+ int fd = data->fd;
+ LocateTagItemArray *array = &data->array;
if (tagItemsFoundAndMatches(song, array->numItems, array->items))
printSongInfo(fd, song);
@@ -118,14 +130,15 @@ static int findInDirectory(int fd, Song * song, void *data)
return 0;
}
-int findSongsIn(int fd, char *name, int numItems, LocateTagItem * items)
+int findSongsIn(int fd, const char *name, int numItems, LocateTagItem * items)
{
- LocateTagItemArray array;
+ struct search_data data;
- array.numItems = numItems;
- array.items = items;
+ data.fd = fd;
+ data.array.numItems = numItems;
+ data.array.items = items;
- return traverseAllIn(fd, name, findInDirectory, NULL, (void *)&array);
+ return traverseAllIn(name, findInDirectory, NULL, &data);
}
static void printSearchStats(int fd, SearchStats *stats)
@@ -134,7 +147,7 @@ static void printSearchStats(int fd, SearchStats *stats)
fdprintf(fd, "playtime: %li\n", stats->playTime);
}
-static int searchStatsInDirectory(mpd_unused int fd, Song * song, void *data)
+static int searchStatsInDirectory(Song * song, void *data)
{
SearchStats *stats = data;
@@ -148,7 +161,7 @@ static int searchStatsInDirectory(mpd_unused int fd, Song * song, void *data)
return 0;
}
-int searchStatsForSongsIn(int fd, char *name, int numItems,
+int searchStatsForSongsIn(int fd, const char *name, int numItems,
LocateTagItem * items)
{
SearchStats stats;
@@ -159,49 +172,60 @@ int searchStatsForSongsIn(int fd, char *name, int numItems,
stats.numberOfSongs = 0;
stats.playTime = 0;
- ret = traverseAllIn(fd, name, searchStatsInDirectory, NULL, &stats);
+ ret = traverseAllIn(name, searchStatsInDirectory, NULL, &stats);
if (ret == 0)
printSearchStats(fd, &stats);
return ret;
}
-int printAllIn(int fd, char *name)
+int printAllIn(int fd, const char *name)
{
- return traverseAllIn(fd, name, printSongInDirectory,
- printDirectoryInDirectory, NULL);
+ return traverseAllIn(name, printSongInDirectory,
+ printDirectoryInDirectory, (void*)(size_t)fd);
}
-static int directoryAddSongToPlaylist(int fd, Song * song,
- mpd_unused void *data)
+static int directoryAddSongToPlaylist(Song * song, mpd_unused void *data)
{
- return addSongToPlaylist(fd, song, NULL);
+ return addSongToPlaylist(song, NULL);
}
-static int directoryAddSongToStoredPlaylist(int fd, Song *song, void *data)
+struct add_data {
+ const char *path;
+};
+
+static int directoryAddSongToStoredPlaylist(Song *song, void *_data)
{
- if (appendSongToStoredPlaylistByPath(fd, (char *)data, song) != 0)
+ struct add_data *data = _data;
+
+ if (appendSongToStoredPlaylistByPath(data->path, song) != 0)
return -1;
return 0;
}
-int addAllIn(int fd, char *name)
+int addAllIn(const char *name)
{
- return traverseAllIn(fd, name, directoryAddSongToPlaylist, NULL, NULL);
+ return traverseAllIn(name, directoryAddSongToPlaylist, NULL, NULL);
}
-int addAllInToStoredPlaylist(int fd, char *name, char *utf8file)
+int addAllInToStoredPlaylist(const char *name, const char *utf8file)
{
- return traverseAllIn(fd, name, directoryAddSongToStoredPlaylist, NULL,
- (void *)utf8file);
+ struct add_data data = {
+ .path = utf8file,
+ };
+
+ return traverseAllIn(name, directoryAddSongToStoredPlaylist, NULL,
+ &data);
}
-static int directoryPrintSongInfo(int fd, Song * song, mpd_unused void *data)
+static int directoryPrintSongInfo(Song * song, void *data)
{
+ int fd = (int)(size_t)data;
+
return printSongInfo(fd, song);
}
-static int sumSongTime(mpd_unused int fd, Song * song, void *data)
+static int sumSongTime(Song * song, void *data)
{
unsigned long *sum_time = (unsigned long *)data;
@@ -211,28 +235,28 @@ static int sumSongTime(mpd_unused int fd, Song * song, void *data)
return 0;
}
-int printInfoForAllIn(int fd, char *name)
+int printInfoForAllIn(int fd, const char *name)
{
- return traverseAllIn(fd, name, directoryPrintSongInfo,
- printDirectoryInDirectory, NULL);
+ return traverseAllIn(name, directoryPrintSongInfo,
+ printDirectoryInDirectory, (void*)(size_t)fd);
}
-int countSongsIn(int fd, char *name)
+int countSongsIn(const char *name)
{
int count = 0;
void *ptr = (void *)&count;
- traverseAllIn(fd, name, NULL, countSongsInDirectory, ptr);
+ traverseAllIn(name, NULL, countSongsInDirectory, ptr);
return count;
}
-unsigned long sumSongTimesIn(int fd, char *name)
+unsigned long sumSongTimesIn(const char *name)
{
unsigned long dbPlayTime = 0;
void *ptr = (void *)&dbPlayTime;
- traverseAllIn(fd, name, sumSongTime, NULL, ptr);
+ traverseAllIn(name, sumSongTime, NULL, ptr);
return dbPlayTime;
}
@@ -254,10 +278,11 @@ static void freeListCommandItem(ListCommandItem * item)
free(item);
}
-static void visitTag(int fd, Song * song, enum tag_type tagType)
+static void visitTag(int fd, struct strset *set,
+ Song * song, enum tag_type tagType)
{
int i;
- MpdTag *tag = song->tag;
+ struct mpd_tag *tag = song->tag;
if (tagType == LOCATE_TAG_FILE_TYPE) {
printSongUrl(fd, song);
@@ -268,19 +293,26 @@ static void visitTag(int fd, Song * song, enum tag_type tagType)
return;
for (i = 0; i < tag->numOfItems; i++) {
- if (tag->items[i].type == tagType) {
- visitInTagTracker(tagType, tag->items[i].value);
+ if (tag->items[i]->type == tagType) {
+ strset_add(set, tag->items[i]->value);
}
}
}
-static int listUniqueTagsInDirectory(int fd, Song * song, void *data)
+struct list_tags_data {
+ int fd;
+ ListCommandItem *item;
+ struct strset *set;
+};
+
+static int listUniqueTagsInDirectory(Song * song, void *_data)
{
- ListCommandItem *item = data;
+ struct list_tags_data *data = _data;
+ ListCommandItem *item = data->item;
if (tagItemsFoundAndMatches(song, item->numConditionals,
item->conditionals)) {
- visitTag(fd, song, item->tagType);
+ visitTag(data->fd, data->set, song, item->tagType);
}
return 0;
@@ -290,18 +322,26 @@ int listAllUniqueTags(int fd, int type, int numConditionals,
LocateTagItem * conditionals)
{
int ret;
+ struct list_tags_data data;
ListCommandItem *item = newListCommandItem(type, numConditionals,
conditionals);
+ data.item = item;
if (type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES) {
- resetVisitedFlagsInTagTracker(type);
+ data.set = strset_new();
}
- ret = traverseAllIn(fd, NULL, listUniqueTagsInDirectory, NULL,
- (void *)item);
+ ret = traverseAllIn(NULL, listUniqueTagsInDirectory, NULL, &data);
if (type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES) {
- printVisitedInTagTracker(fd, type);
+ const char *value;
+
+ strset_rewind(data.set);
+
+ while ((value = strset_next(data.set)) != NULL)
+ fdprintf(fd, "%s: %s\n", mpdTagItemKeys[type], value);
+
+ strset_free(data.set);
}
freeListCommandItem(item);
@@ -309,9 +349,7 @@ int listAllUniqueTags(int fd, int type, int numConditionals,
return ret;
}
-static int sumSavedFilenameMemoryInDirectory(mpd_unused int fd,
- Directory * dir,
- void *data)
+static int sumSavedFilenameMemoryInDirectory(Directory * dir, void *data)
{
int *sum = data;
@@ -324,8 +362,7 @@ static int sumSavedFilenameMemoryInDirectory(mpd_unused int fd,
return 0;
}
-static int sumSavedFilenameMemoryInSong(mpd_unused int fd, Song * song,
- void *data)
+static int sumSavedFilenameMemoryInSong(Song * song, void *data)
{
int *sum = data;
@@ -338,7 +375,7 @@ void printSavedMemoryFromFilenames(void)
{
int sum = 0;
- traverseAllIn(STDERR_FILENO, NULL, sumSavedFilenameMemoryInSong,
+ traverseAllIn(NULL, sumSavedFilenameMemoryInSong,
sumSavedFilenameMemoryInDirectory, (void *)&sum);
DEBUG("saved memory from filenames: %i\n", sum);