2006-07-13 21:20:34 +02:00
|
|
|
/* the Music Player Daemon (MPD)
|
2006-07-14 21:37:45 +02:00
|
|
|
* (c)2003-2006 by Warren Dukes (warren.dukes@gmail.com)
|
2006-07-13 21:20:34 +02:00
|
|
|
* This project's homepage is: http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
#include "tagTracker.h"
|
|
|
|
|
2006-07-16 18:50:54 +02:00
|
|
|
#include "list.h"
|
2004-11-10 22:58:27 +01:00
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static List *tagLists[TAG_NUM_OF_ITEM_TYPES] = {
|
2004-11-10 22:58:27 +01:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct tagTrackerItem {
|
|
|
|
int count;
|
|
|
|
mpd_sint8 visited;
|
|
|
|
} TagTrackerItem;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
char *getTagItemString(int type, char *string)
|
|
|
|
{
|
|
|
|
ListNode *node;
|
2006-07-16 18:50:54 +02:00
|
|
|
int pos;
|
2004-11-14 15:42:07 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tagLists[type] == NULL) {
|
2006-07-16 18:50:54 +02:00
|
|
|
tagLists[type] = makeList(free, 1);
|
|
|
|
sortList(tagLists[type]);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (findNodeInList(tagLists[type], string, &node, &pos)) {
|
|
|
|
((TagTrackerItem *) node->data)->count++;
|
|
|
|
} else {
|
|
|
|
TagTrackerItem *item = malloc(sizeof(TagTrackerItem));
|
2004-11-10 22:58:27 +01:00
|
|
|
item->count = 1;
|
|
|
|
item->visited = 0;
|
2006-07-16 18:50:54 +02:00
|
|
|
node = insertInListBeforeNode(tagLists[type], node, pos,
|
2006-07-20 18:02:40 +02:00
|
|
|
string, item);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2006-07-16 18:50:54 +02:00
|
|
|
return node->key;
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void removeTagItemString(int type, char *string)
|
|
|
|
{
|
|
|
|
ListNode *node;
|
2006-07-16 18:50:54 +02:00
|
|
|
int pos;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
|
|
|
assert(string);
|
|
|
|
|
2004-11-14 15:58:00 +01:00
|
|
|
assert(tagLists[type]);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tagLists[type] == NULL)
|
|
|
|
return;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (findNodeInList(tagLists[type], string, &node, &pos)) {
|
|
|
|
TagTrackerItem *item = node->data;
|
2004-11-10 22:58:27 +01:00
|
|
|
item->count--;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (item->count <= 0)
|
|
|
|
deleteNodeFromList(tagLists[type], node);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tagLists[type]->numberOfNodes == 0) {
|
2004-11-10 22:58:27 +01:00
|
|
|
freeList(tagLists[type]);
|
|
|
|
tagLists[type] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int getNumberOfTagItems(int type)
|
|
|
|
{
|
|
|
|
if (tagLists[type] == NULL)
|
|
|
|
return 0;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2006-07-16 18:50:54 +02:00
|
|
|
return tagLists[type]->numberOfNodes;
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
2006-07-15 05:34:48 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void printMemorySavedByTagTracker(void)
|
|
|
|
{
|
2004-11-10 22:58:27 +01:00
|
|
|
int i;
|
2006-07-20 18:02:40 +02:00
|
|
|
ListNode *node;
|
2004-11-10 22:58:27 +01:00
|
|
|
size_t sum = 0;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < TAG_NUM_OF_ITEM_TYPES; i++) {
|
|
|
|
if (!tagLists[i])
|
|
|
|
continue;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2006-07-16 18:50:54 +02:00
|
|
|
sum -= sizeof(List);
|
|
|
|
|
|
|
|
node = tagLists[i]->firstNode;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (node != NULL) {
|
2006-07-16 18:50:54 +02:00
|
|
|
sum -= sizeof(ListNode);
|
|
|
|
sum -= sizeof(TagTrackerItem);
|
|
|
|
sum -= sizeof(node->key);
|
2006-07-20 18:02:40 +02:00
|
|
|
sum += (strlen(node->key) + 1) * (*((int *)node->data));
|
2006-07-16 18:50:54 +02:00
|
|
|
node = node->nextNode;
|
|
|
|
}
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2004-11-11 06:25:05 +01:00
|
|
|
DEBUG("saved memory from tags: %li\n", (long)sum);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void resetVisitedFlagsInTagTracker(int type)
|
|
|
|
{
|
|
|
|
ListNode *node;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!tagLists[type])
|
|
|
|
return;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2006-07-16 18:50:54 +02:00
|
|
|
node = tagLists[type]->firstNode;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (node) {
|
|
|
|
((TagTrackerItem *) node->data)->visited = 0;
|
2006-07-16 18:50:54 +02:00
|
|
|
node = node->nextNode;
|
|
|
|
}
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void visitInTagTracker(int type, char *str)
|
|
|
|
{
|
|
|
|
void *item;
|
2004-11-11 14:15:41 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!tagLists[type])
|
|
|
|
return;
|
2004-11-11 14:15:41 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!findInList(tagLists[type], str, &item))
|
|
|
|
return;
|
2004-11-11 14:15:41 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
((TagTrackerItem *) item)->visited = 1;
|
2005-11-16 15:43:04 +01:00
|
|
|
}
|
2004-11-11 14:15:41 +01:00
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
void printVisitedInTagTracker(int fd, int type)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
ListNode *node;
|
|
|
|
TagTrackerItem *item;
|
2006-07-16 18:50:54 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!tagLists[type])
|
|
|
|
return;
|
2006-07-16 18:50:54 +02:00
|
|
|
|
|
|
|
node = tagLists[type]->firstNode;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (node) {
|
2006-07-16 18:50:54 +02:00
|
|
|
item = node->data;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (item->visited) {
|
2006-07-30 05:43:38 +02:00
|
|
|
fdprintf(fd, "%s: %s\n", mpdTagItemKeys[type],
|
|
|
|
node->key);
|
2006-07-16 18:50:54 +02:00
|
|
|
}
|
|
|
|
node = node->nextNode;
|
|
|
|
}
|
2004-11-11 14:18:44 +01:00
|
|
|
}
|