Initial revision 6759
[qball-mpd.git] / src / .svn / text-base / tagTracker.c.svn-base
blobab356e5000f47a9eea30b17183e2974180f50d70
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 "tagTracker.h"
21 #include "tree.h"
22 #include "log.h"
23 #include "utils.h"
24 #include "myfprintf.h"
26 #include <assert.h>
27 #include <stdlib.h>
29 static Tree *tagTrees[TAG_NUM_OF_ITEM_TYPES];
31 typedef struct tagTrackerItem {
32         int count;
33         mpd_sint8 visited;
34 } TagTrackerItem;
36 char *getTagItemString(int type, char *string)
38         TreeIterator iter;
39         
40         if (tagTrees[type] == NULL) 
41         {
42                 tagTrees[type] = MakeTree((TreeCompareKeyFunction)strcmp, 
43                                           (TreeFreeFunction)free, 
44                                           (TreeFreeFunction)free);
45         }
47         if (FindInTree(tagTrees[type], string, &iter)) 
48         {
49                 ((TagTrackerItem *)GetTreeKeyData(&iter).data)->count++;
50                 return (char *)GetTreeKeyData(&iter).key;
51         } 
52         else 
53         {
54                 TagTrackerItem *item = xmalloc(sizeof(TagTrackerItem));
55                 char *key = xstrdup(string);
56                 item->count = 1;
57                 item->visited = 0;
58                 InsertInTree(tagTrees[type], key, item);
59                 return key;
60         }
63 void removeTagItemString(int type, char *string)
65         TreeIterator iter;
66         
67         assert(string);
69         assert(tagTrees[type]);
70         if (tagTrees[type] == NULL)
71                 return;
73         if (FindInTree(tagTrees[type], string, &iter)) 
74         {
75                 TagTrackerItem * item = 
76                         (TagTrackerItem *)GetTreeKeyData(&iter).data;
77                 item->count--;
78                 if (item->count <= 0)
79                         RemoveFromTreeByIterator(tagTrees[type], &iter);
80         }
82         if (GetTreeSize(tagTrees[type]) == 0) 
83         {
84                 FreeTree(tagTrees[type]);
85                 tagTrees[type] = NULL;
86         }
89 int getNumberOfTagItems(int type)
91         if (tagTrees[type] == NULL)
92                 return 0;
94         return GetTreeSize(tagTrees[type]);
97 void resetVisitedFlagsInTagTracker(int type)
99         TreeIterator iter;
101         if (!tagTrees[type])
102                 return;
104         for (SetTreeIteratorToBegin(tagTrees[type], &iter);
105              !IsTreeIteratorAtEnd(&iter);
106              IncrementTreeIterator(&iter))
107         {
108                 ((TagTrackerItem *)GetTreeKeyData(&iter).data)->visited = 0;
109         }
112 void visitInTagTracker(int type, char *str)
114         TreeIterator iter;
116         if (!tagTrees[type])
117                 return;
119         if (!FindInTree(tagTrees[type], str, &iter))
120                 return;
122         ((TagTrackerItem *)GetTreeKeyData(&iter).data)->visited = 1;
125 void printVisitedInTagTracker(int fd, int type)
127         TreeIterator iter;
128         TagTrackerItem * item;
130         if (!tagTrees[type])
131                 return;
133         for (SetTreeIteratorToBegin(tagTrees[type], &iter);
134              !IsTreeIteratorAtEnd(&iter);
135              IncrementTreeIterator(&iter))
136         {
137                 item = ((TagTrackerItem *)GetTreeKeyData(&iter).data);
139                 if (item->visited) 
140                 {
141                         fdprintf(fd, 
142                                  "%s: %s\n", 
143                                  mpdTagItemKeys[type],
144                                  (char *)GetTreeKeyData(&iter).key);
145                 }
146         }