Initial revision 6759
[qball-mpd.git] / src / .svn / text-base / dbUtils.c.svn-base
blobf83ae4c212461f370613b54e2bd0d75f653ce881
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
4  *
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.
9  *
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
17  */
19 #include "dbUtils.h"
21 #include "directory.h"
22 #include "myfprintf.h"
23 #include "utils.h"
24 #include "playlist.h"
25 #include "song.h"
26 #include "tag.h"
27 #include "tagTracker.h"
28 #include "log.h"
29 #include "storedPlaylist.h"
31 typedef struct _ListCommandItem {
32         mpd_sint8 tagType;
33         int numConditionals;
34         LocateTagItem *conditionals;
35 } ListCommandItem;
37 typedef struct _LocateTagItemArray {
38         int numItems;
39         LocateTagItem *items;
40 } LocateTagItemArray;
42 typedef struct _SearchStats {
43         LocateTagItemArray locateArray;
44         int numberOfSongs;
45         unsigned long playTime;
46 } SearchStats;
48 static int countSongsInDirectory(int fd, Directory * directory, void *data)
50         int *count = (int *)data;
52         *count += directory->songs->numberOfNodes;
54         return 0;
57 static int printDirectoryInDirectory(int fd, Directory * directory,
58                                      void *data)
60         if (directory->path) {
61                 fdprintf(fd, "directory: %s\n", getDirectoryPath(directory));
62         }
63         return 0;
66 static int printSongInDirectory(int fd, Song * song, void *data)
68         printSongUrl(fd, song);
69         return 0;
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);
79         return 0;
82 int searchForSongsIn(int fd, char *name, int numItems, LocateTagItem * items)
84         int ret = -1;
85         int i;
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]);
93         }
95         array.numItems = numItems;
96         array.items = items;
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];
103         }
105         free(originalNeedles);
107         return ret;
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);
117         return 0;
120 int findSongsIn(int fd, char *name, int numItems, LocateTagItem * items)
122         LocateTagItemArray array;
124         array.numItems = numItems;
125         array.items = items;
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;
145         }
147         return 0;
150 int searchStatsForSongsIn(int fd, char *name, int numItems,
151                           LocateTagItem * items)
153         SearchStats stats;
154         int ret;
156         stats.locateArray.numItems = numItems;
157         stats.locateArray.items = items;
158         stats.numberOfSongs = 0;
159         stats.playTime = 0;
161         ret = traverseAllIn(fd, name, searchStatsInDirectory, NULL, &stats);
162         if (ret == 0)
163                 printSearchStats(fd, &stats);
165         return ret;
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)
182                 return -1;
183         return 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,
194                              (void *)utf8file);
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;
209         return 0;
212 int printInfoForAllIn(int fd, char *name)
214         return traverseAllIn(fd, name, directoryPrintSongInfo,
215                              printDirectoryInDirectory, NULL);
218 int countSongsIn(int fd, char *name)
220         int count = 0;
221         void *ptr = (void *)&count;
223         traverseAllIn(fd, name, NULL, countSongsInDirectory, ptr);
225         return count;
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);
235         return dbPlayTime;
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;
247         return item;
250 static void freeListCommandItem(ListCommandItem * item)
252         free(item);
255 static void visitTag(int fd, Song * song, int tagType)
257         int i;
258         MpdTag *tag = song->tag;
260         if (tagType == LOCATE_TAG_FILE_TYPE) {
261                 printSongUrl(fd, song);
262                 return;
263         }
265         if (!tag)
266                 return;
268         for (i = 0; i < tag->numOfItems; i++) {
269                 if (tag->items[i].type == tagType) {
270                         visitInTagTracker(tagType, tag->items[i].value);
271                 }
272         }
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);
282         }
284         return 0;
287 int listAllUniqueTags(int fd, int type, int numConditionals,
288                       LocateTagItem * conditionals)
290         int ret;
291         ListCommandItem *item = newListCommandItem(type, numConditionals,
292                                                    conditionals);
294         if (type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES) {
295                 resetVisitedFlagsInTagTracker(type);
296         }
298         ret = traverseAllIn(fd, NULL, listUniqueTagsInDirectory, NULL,
299                             (void *)item);
301         if (type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES) {
302                 printVisitedInTagTracker(fd, type);
303         }
305         freeListCommandItem(item);
307         return ret;
310 static int sumSavedFilenameMemoryInDirectory(int fd, Directory * dir,
311                                              void *data)
313         int *sum = data;
315         if (!dir->path)
316                 return 0;
318         *sum += (strlen(getDirectoryPath(dir)) + 1 - sizeof(Directory *)) *
319             dir->songs->numberOfNodes;
321         return 0;
324 static int sumSavedFilenameMemoryInSong(int fd, Song * song, void *data)
326         int *sum = data;
328         *sum += strlen(song->url) + 1;
330         return 0;
333 void printSavedMemoryFromFilenames(void)
335         int sum = 0;
337         traverseAllIn(STDERR_FILENO, NULL, sumSavedFilenameMemoryInSong,
338                       sumSavedFilenameMemoryInDirectory, (void *)&sum);
340         DEBUG("saved memory from filenames: %i\n", sum);