diff options
author | Warren Dukes <warren.dukes@gmail.com> | 2004-11-10 20:11:34 +0000 |
---|---|---|
committer | Warren Dukes <warren.dukes@gmail.com> | 2004-11-10 20:11:34 +0000 |
commit | 0337ec851c643503afd2287f03ca42b5adf5cad7 (patch) | |
tree | 0b6ead2f83f5b9e390cdff163bafb5fe9e6e4bfc | |
parent | 0e8aa9674f10a6ca1ad69b3846b5a4b5e9188928 (diff) | |
download | mpd-0337ec851c643503afd2287f03ca42b5adf5cad7.tar.gz mpd-0337ec851c643503afd2287f03ca42b5adf5cad7.tar.xz mpd-0337ec851c643503afd2287f03ca42b5adf5cad7.zip |
search/find rewrite
git-svn-id: https://svn.musicpd.org/mpd/branches/r2562-metadata-handling-rewrite@2584 09075e82-0dd4-0310-85a5-a0d7c8717e4f
Diffstat (limited to '')
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/command.c | 33 | ||||
-rw-r--r-- | src/directory.c | 207 | ||||
-rw-r--r-- | src/directory.h | 33 |
4 files changed, 54 insertions, 221 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 8b1f3d1f6..dbde9a3a0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -24,6 +24,7 @@ mpd_headers = \ charConv.h \ command.h \ conf.h \ + dbUtils.h \ decode.h \ directory.h \ inputPlugin.h \ @@ -67,6 +68,7 @@ mpd_SOURCES = \ charConv.c \ command.c \ conf.c \ + dbUtils.c \ decode.c \ directory.c \ inputPlugin.c \ diff --git a/src/command.c b/src/command.c index a179c2af4..35045b472 100644 --- a/src/command.c +++ b/src/command.c @@ -31,6 +31,7 @@ #include "audio.h" #include "buffer2array.h" #include "log.h" +#include "dbUtils.h" #include <stdlib.h> #include <string.h> @@ -441,13 +442,41 @@ int handlePlaylistId(FILE * fp, unsigned int * permission, int handleFind(FILE * fp, unsigned int * permission, int argArrayLength, char ** argArray) { - return findSongsIn(fp,NULL,argArray[1],argArray[2]); + int ret; + + LocateTagItem * item = newLocateTagItem(argArray[1], argArray[2]); + + if(!item) { + commandError(fp, ACK_ERROR_ARG, "\%s\" isn't recognized", + argArray[1]); + return -1; + } + + ret = findSongsIn(fp, NULL, item); + + freeLocateTagItem(item); + + return ret; } int handleSearch(FILE * fp, unsigned int * permission, int argArrayLength, char ** argArray) { - return searchForSongsIn(fp,NULL,argArray[1],argArray[2]); + int ret; + + LocateTagItem * item = newLocateTagItem(argArray[1], argArray[2]); + + if(!item) { + commandError(fp, ACK_ERROR_ARG, "\%s\" isn't recognized", + argArray[1]); + return -1; + } + + ret = searchForSongsIn(fp, NULL, item); + + freeLocateTagItem(item); + + return ret; } int listHandleUpdate(FILE * fp, unsigned int * permission, int argArrayLength, diff --git a/src/directory.c b/src/directory.c index 443c8bfba..5ac78afaf 100644 --- a/src/directory.c +++ b/src/directory.c @@ -33,6 +33,8 @@ #include "sig_handlers.h" #include "player.h" #include "tagTracker.h" +#include "list.h" +#include "dbUtils.h" #include <string.h> #include <sys/types.h> @@ -55,11 +57,6 @@ #define DIRECTORY_MPD_VERSION "mpd_version: " #define DIRECTORY_FS_CHARSET "fs_charset: " -#define DIRECTORY_SEARCH_ALBUM "album" -#define DIRECTORY_SEARCH_ARTIST "artist" -#define DIRECTORY_SEARCH_TITLE "title" -#define DIRECTORY_SEARCH_FILENAME "filename" - #define DIRECTORY_UPDATE_EXIT_NOUPDATE 0 #define DIRECTORY_UPDATE_EXIT_UPDATE 1 #define DIRECTORY_UPDATE_EXIT_ERROR 2 @@ -68,21 +65,6 @@ #define DIRECTORY_RETURN_UPDATE 1 #define DIRECTORY_RETURN_ERROR -1 -typedef List DirectoryList; - -typedef struct _DirectoryStat { - ino_t inode; - dev_t device; -} DirectoryStat; - -typedef struct _Directory { - char * utf8name; - DirectoryList * subDirectories; - SongList * songs; - struct _Directory * parent; - DirectoryStat * stat; -} Directory; - Directory * mp3rootDirectory = NULL; char * directory_db; @@ -1173,182 +1155,6 @@ int traverseAllIn(FILE * fp, char * name, data); } -int countSongsInDirectory(FILE * fp, Directory * directory, void * data) { - int * count = (int *)data; - - *count+=directory->songs->numberOfNodes; - - return 0; -} - -int printDirectoryInDirectory(FILE * fp, Directory * directory, void * data) { - if(directory->utf8name) { - myfprintf(fp,"directory: %s\n",directory->utf8name); - } - return 0; -} - -int printSongInDirectory(FILE * fp, Song * song, void * data) { - myfprintf(fp,"file: %s\n",song->utf8url); - return 0; -} - -static inline int strstrSearchTag(Song * song, int type, char * str) { - int i; - char * dup; - - if(!song->tag) return 0; - - for(i = 0; i < song->tag->numOfItems; i++) { - if(song->tag->items[i].type != type) continue; - - dup = strDupToUpper(song->tag->items[i].value); - if(strstr(dup, str)) return 1; - free(dup); - } - - return 0; -} - -int searchForAlbumInDirectory(FILE * fp, Song * song, void * string) { - if(strstrSearchTag(song, TAG_ITEM_ALBUM, (char *)string)) { - printSongInfo(fp, song); - } - return 0; -} - -int searchForArtistInDirectory(FILE * fp, Song * song, void * string) { - if(strstrSearchTag(song, TAG_ITEM_ARTIST, (char *)string)) { - printSongInfo(fp, song); - } - return 0; -} - -int searchForTitleInDirectory(FILE * fp, Song * song, void * string) { - if(strstrSearchTag(song, TAG_ITEM_TITLE, (char *)string)) { - printSongInfo(fp, song); - } - return 0; -} - -int searchForFilenameInDirectory(FILE * fp, Song * song, void * string) { - char * dup = strDupToUpper(song->utf8url); - if(strstr(dup,(char *)string)) printSongInfo(fp,song); - free(dup); - return 0; -} - -int searchForSongsIn(FILE * fp, char * name, char * item, char * string) { - char * dup = strDupToUpper(string); - int ret = -1; - - if(strcmp(item,DIRECTORY_SEARCH_ALBUM)==0) { - ret = traverseAllIn(fp,name,searchForAlbumInDirectory,NULL, - (void *)dup); - } - else if(strcmp(item,DIRECTORY_SEARCH_ARTIST)==0) { - ret = traverseAllIn(fp,name,searchForArtistInDirectory,NULL, - (void *)dup); - } - else if(strcmp(item,DIRECTORY_SEARCH_TITLE)==0) { - ret = traverseAllIn(fp,name,searchForTitleInDirectory,NULL, - (void *)dup); - } - else if(strcmp(item,DIRECTORY_SEARCH_FILENAME)==0) { - ret = traverseAllIn(fp,name,searchForFilenameInDirectory,NULL, - (void *)dup); - } - else commandError(fp, ACK_ERROR_ARG, "unknown table", NULL); - - free(dup); - - return ret; -} - -static inline int tagItemFoundAndMatches(Song * song, int type, char * str) { - int i; - - if(!song->tag) return 0; - - for(i = 0; i < song->tag->numOfItems; i++) { - if(song->tag->items[i].type != type) continue; - - if(0 == strcmp(str, song->tag->items[i].value)) return 1; - } - - return 0; -} - -int findAlbumInDirectory(FILE * fp, Song * song, void * string) { - if(tagItemFoundAndMatches(song, TAG_ITEM_ALBUM, (char *)string)) - { - printSongInfo(fp,song); - } - - return 0; -} - -int findArtistInDirectory(FILE * fp, Song * song, void * string) { - if(tagItemFoundAndMatches(song, TAG_ITEM_ARTIST, (char *)string)) - { - printSongInfo(fp,song); - } - - return 0; -} - -int findSongsIn(FILE * fp, char * name, char * item, char * string) { - if(strcmp(item,DIRECTORY_SEARCH_ALBUM)==0) { - return traverseAllIn(fp,name,findAlbumInDirectory,NULL, - (void *)string); - } - else if(strcmp(item,DIRECTORY_SEARCH_ARTIST)==0) { - return traverseAllIn(fp,name,findArtistInDirectory,NULL, - (void *)string); - } - - commandError(fp, ACK_ERROR_ARG, "unknown table", NULL); - return -1; -} - -int printAllIn(FILE * fp, char * name) { - return traverseAllIn(fp,name,printSongInDirectory, - printDirectoryInDirectory,NULL); -} - -int directoryAddSongToPlaylist(FILE * fp, Song * song, void * data) { - return addSongToPlaylist(fp, song, 0); -} - -int addAllIn(FILE * fp, char * name) { - return traverseAllIn(fp,name,directoryAddSongToPlaylist,NULL,NULL); -} - -int directoryPrintSongInfo(FILE * fp, Song * song, void * data) { - return printSongInfo(fp,song); -} - -int sumSongTime(FILE * fp, Song * song, void * data) { - unsigned long * time = (unsigned long *)data; - - if(song->tag && song->tag->time>=0) *time+=song->tag->time; - - return 0; -} - -int printInfoForAllIn(FILE * fp, char * name) { - return traverseAllIn(fp,name,directoryPrintSongInfo,printDirectoryInDirectory,NULL); -} - -int countSongsIn(FILE * fp, char * name) { - int count = 0; - void * ptr = (void *)&count; - - traverseAllIn(fp,name,NULL,countSongsInDirectory,ptr); - - return count; -} - void freeAllDirectoryStats(Directory * directory) { ListNode * node = directory->subDirectories->firstNode; @@ -1360,15 +1166,6 @@ void freeAllDirectoryStats(Directory * directory) { freeDirectoryStatFromDirectory(directory); } -unsigned long sumSongTimesIn(FILE * fp, char * name) { - unsigned long dbPlayTime = 0; - void * ptr = (void *)&dbPlayTime; - - traverseAllIn(fp,name,sumSongTime,NULL,ptr); - - return dbPlayTime; -} - void initMp3Directory() { struct stat st; diff --git a/src/directory.h b/src/directory.h index b11f18ffb..b0b2c843c 100644 --- a/src/directory.h +++ b/src/directory.h @@ -27,6 +27,21 @@ #include <stdio.h> #include <sys/param.h> +typedef List DirectoryList; + +typedef struct _DirectoryStat { + ino_t inode; + dev_t device; +} DirectoryStat; + +typedef struct _Directory { + char * utf8name; + DirectoryList * subDirectories; + SongList * songs; + struct _Directory * parent; + DirectoryStat * stat; +} Directory; + extern char * directory_db; void readDirectoryDBIfUpdateIsFinished(); @@ -51,22 +66,12 @@ int readDirectoryDB(); void updateMp3Directory(); -int printAllIn(FILE * fp, char * name); - -int addAllIn(FILE * fp, char * name); - -int printInfoForAllIn(FILE * fp, char * name); - -int searchForSongsIn(FILE * fp, char * name, char * item, char * string); - -int findSongsIn(FILE * fp, char * name, char * item, char * string); - -int countSongsIn(FILE * fp, char * name); - -unsigned long sumSongTimesIn(FILE * fp, char * name); - Song * getSongFromDB(char * file); time_t getDbModTime(); +int traverseAllIn(FILE * fp, char * name, + int (*forEachSong)(FILE *, Song *, void *), + int (*forEachDir)(FILE *, Directory *, void *), + void * data); #endif |