1 /* the Music Player Daemon (MPD)
2 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 * This project's homepage is: http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "directory.h"
22 #include "myfprintf.h"
27 #include "tagTracker.h"
29 #include "storedPlaylist.h"
31 typedef struct _ListCommandItem
{
34 LocateTagItem
*conditionals
;
37 typedef struct _LocateTagItemArray
{
42 typedef struct _SearchStats
{
43 LocateTagItemArray locateArray
;
45 unsigned long playTime
;
48 static int countSongsInDirectory(int fd
, Directory
* directory
, void *data
)
50 int *count
= (int *)data
;
52 *count
+= directory
->songs
->numberOfNodes
;
57 static int printDirectoryInDirectory(int fd
, Directory
* directory
,
60 if (directory
->path
) {
61 fdprintf(fd
, "directory: %s\n", getDirectoryPath(directory
));
66 static int printSongInDirectory(int fd
, Song
* song
, void *data
)
68 printSongUrl(fd
, song
);
72 static int searchInDirectory(int fd
, Song
* song
, void *data
)
74 LocateTagItemArray
*array
= data
;
76 if (strstrSearchTags(song
, array
->numItems
, array
->items
))
77 printSongInfo(fd
, song
);
82 int searchForSongsIn(int fd
, char *name
, int numItems
, LocateTagItem
* items
)
87 char **originalNeedles
= xmalloc(numItems
* sizeof(char *));
88 LocateTagItemArray array
;
90 for (i
= 0; i
< numItems
; i
++) {
91 originalNeedles
[i
] = items
[i
].needle
;
92 items
[i
].needle
= strDupToUpper(originalNeedles
[i
]);
95 array
.numItems
= numItems
;
98 ret
= traverseAllIn(fd
, name
, searchInDirectory
, NULL
, &array
);
100 for (i
= 0; i
< numItems
; i
++) {
101 free(items
[i
].needle
);
102 items
[i
].needle
= originalNeedles
[i
];
105 free(originalNeedles
);
110 static int findInDirectory(int fd
, Song
* song
, void *data
)
112 LocateTagItemArray
*array
= data
;
114 if (tagItemsFoundAndMatches(song
, array
->numItems
, array
->items
))
115 printSongInfo(fd
, song
);
120 int findSongsIn(int fd
, char *name
, int numItems
, LocateTagItem
* items
)
122 LocateTagItemArray array
;
124 array
.numItems
= numItems
;
127 return traverseAllIn(fd
, name
, findInDirectory
, NULL
, (void *)&array
);
130 static void printSearchStats(int fd
, SearchStats
*stats
)
132 fdprintf(fd
, "songs: %i\n", stats
->numberOfSongs
);
133 fdprintf(fd
, "playtime: %li\n", stats
->playTime
);
136 static int searchStatsInDirectory(int fd
, Song
* song
, void *data
)
138 SearchStats
*stats
= data
;
140 if (tagItemsFoundAndMatches(song
, stats
->locateArray
.numItems
,
141 stats
->locateArray
.items
)) {
142 stats
->numberOfSongs
++;
143 if (song
->tag
->time
> 0)
144 stats
->playTime
+= song
->tag
->time
;
150 int searchStatsForSongsIn(int fd
, char *name
, int numItems
,
151 LocateTagItem
* items
)
156 stats
.locateArray
.numItems
= numItems
;
157 stats
.locateArray
.items
= items
;
158 stats
.numberOfSongs
= 0;
161 ret
= traverseAllIn(fd
, name
, searchStatsInDirectory
, NULL
, &stats
);
163 printSearchStats(fd
, &stats
);
168 int printAllIn(int fd
, char *name
)
170 return traverseAllIn(fd
, name
, printSongInDirectory
,
171 printDirectoryInDirectory
, NULL
);
174 static int directoryAddSongToPlaylist(int fd
, Song
* song
, void *data
)
176 return addSongToPlaylist(fd
, song
, 0);
179 static int directoryAddSongToStoredPlaylist(int fd
, Song
*song
, void *data
)
181 if (appendSongToStoredPlaylistByPath(fd
, (char *)data
, song
) != 0)
186 int addAllIn(int fd
, char *name
)
188 return traverseAllIn(fd
, name
, directoryAddSongToPlaylist
, NULL
, NULL
);
191 int addAllInToStoredPlaylist(int fd
, char *name
, char *utf8file
)
193 return traverseAllIn(fd
, name
, directoryAddSongToStoredPlaylist
, NULL
,
197 static int directoryPrintSongInfo(int fd
, Song
* song
, void *data
)
199 return printSongInfo(fd
, song
);
202 static int sumSongTime(int fd
, Song
* song
, void *data
)
204 unsigned long *time
= (unsigned long *)data
;
206 if (song
->tag
&& song
->tag
->time
>= 0)
207 *time
+= song
->tag
->time
;
212 int printInfoForAllIn(int fd
, char *name
)
214 return traverseAllIn(fd
, name
, directoryPrintSongInfo
,
215 printDirectoryInDirectory
, NULL
);
218 int countSongsIn(int fd
, char *name
)
221 void *ptr
= (void *)&count
;
223 traverseAllIn(fd
, name
, NULL
, countSongsInDirectory
, ptr
);
228 unsigned long sumSongTimesIn(int fd
, char *name
)
230 unsigned long dbPlayTime
= 0;
231 void *ptr
= (void *)&dbPlayTime
;
233 traverseAllIn(fd
, name
, sumSongTime
, NULL
, ptr
);
238 static ListCommandItem
*newListCommandItem(int tagType
, int numConditionals
,
239 LocateTagItem
* conditionals
)
241 ListCommandItem
*item
= xmalloc(sizeof(ListCommandItem
));
243 item
->tagType
= tagType
;
244 item
->numConditionals
= numConditionals
;
245 item
->conditionals
= conditionals
;
250 static void freeListCommandItem(ListCommandItem
* item
)
255 static void visitTag(int fd
, Song
* song
, int tagType
)
258 MpdTag
*tag
= song
->tag
;
260 if (tagType
== LOCATE_TAG_FILE_TYPE
) {
261 printSongUrl(fd
, song
);
268 for (i
= 0; i
< tag
->numOfItems
; i
++) {
269 if (tag
->items
[i
].type
== tagType
) {
270 visitInTagTracker(tagType
, tag
->items
[i
].value
);
275 static int listUniqueTagsInDirectory(int fd
, Song
* song
, void *data
)
277 ListCommandItem
*item
= data
;
279 if (tagItemsFoundAndMatches(song
, item
->numConditionals
,
280 item
->conditionals
)) {
281 visitTag(fd
, song
, item
->tagType
);
287 int listAllUniqueTags(int fd
, int type
, int numConditionals
,
288 LocateTagItem
* conditionals
)
291 ListCommandItem
*item
= newListCommandItem(type
, numConditionals
,
294 if (type
>= 0 && type
<= TAG_NUM_OF_ITEM_TYPES
) {
295 resetVisitedFlagsInTagTracker(type
);
298 ret
= traverseAllIn(fd
, NULL
, listUniqueTagsInDirectory
, NULL
,
301 if (type
>= 0 && type
<= TAG_NUM_OF_ITEM_TYPES
) {
302 printVisitedInTagTracker(fd
, type
);
305 freeListCommandItem(item
);
310 static int sumSavedFilenameMemoryInDirectory(int fd
, Directory
* dir
,
318 *sum
+= (strlen(getDirectoryPath(dir
)) + 1 - sizeof(Directory
*)) *
319 dir
->songs
->numberOfNodes
;
324 static int sumSavedFilenameMemoryInSong(int fd
, Song
* song
, void *data
)
328 *sum
+= strlen(song
->url
) + 1;
333 void printSavedMemoryFromFilenames(void)
337 traverseAllIn(STDERR_FILENO
, NULL
, sumSavedFilenameMemoryInSong
,
338 sumSavedFilenameMemoryInDirectory
, (void *)&sum
);
340 DEBUG("saved memory from filenames: %i\n", sum
);