2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2012-08-29 19:12:26 +02:00
|
|
|
* Copyright (C) 2003-2012 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2007-02-24 04:14:00 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-02-24 04:14:00 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2012-08-29 19:12:26 +02:00
|
|
|
#include "SongFilter.hxx"
|
2013-07-28 13:25:12 +02:00
|
|
|
#include "Song.hxx"
|
2013-09-05 18:22:02 +02:00
|
|
|
#include "tag/Tag.hxx"
|
2012-08-29 19:12:26 +02:00
|
|
|
|
2008-10-15 19:36:37 +02:00
|
|
|
#include <glib.h>
|
|
|
|
|
2012-08-07 23:59:17 +02:00
|
|
|
#include <assert.h>
|
2013-07-30 20:11:57 +02:00
|
|
|
#include <string.h>
|
2009-01-02 16:24:16 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2007-03-20 21:22:23 +01:00
|
|
|
#define LOCATE_TAG_FILE_KEY "file"
|
|
|
|
#define LOCATE_TAG_FILE_KEY_OLD "filename"
|
2007-02-24 01:54:54 +01:00
|
|
|
#define LOCATE_TAG_ANY_KEY "any"
|
|
|
|
|
2012-08-08 00:45:46 +02:00
|
|
|
unsigned
|
2009-01-24 15:26:59 +01:00
|
|
|
locate_parse_type(const char *str)
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
2009-04-28 09:31:44 +02:00
|
|
|
if (0 == g_ascii_strcasecmp(str, LOCATE_TAG_FILE_KEY) ||
|
|
|
|
0 == g_ascii_strcasecmp(str, LOCATE_TAG_FILE_KEY_OLD))
|
2007-02-24 01:54:54 +01:00
|
|
|
return LOCATE_TAG_FILE_TYPE;
|
|
|
|
|
2009-04-28 09:31:44 +02:00
|
|
|
if (0 == g_ascii_strcasecmp(str, LOCATE_TAG_ANY_KEY))
|
2007-02-24 01:54:54 +01:00
|
|
|
return LOCATE_TAG_ANY_TYPE;
|
|
|
|
|
2012-08-08 00:45:46 +02:00
|
|
|
return tag_name_parse_i(str);
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
SongFilter::Item::Item(unsigned _tag, const char *_value, bool _fold_case)
|
|
|
|
:tag(_tag), fold_case(_fold_case),
|
|
|
|
value(fold_case
|
|
|
|
? g_utf8_casefold(_value, -1)
|
|
|
|
: g_strdup(_value))
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
2012-08-07 23:36:21 +02:00
|
|
|
}
|
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
SongFilter::Item::~Item()
|
2009-01-24 15:56:30 +01:00
|
|
|
{
|
2012-08-29 19:27:03 +02:00
|
|
|
g_free(value);
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
bool
|
|
|
|
SongFilter::Item::StringMatch(const char *s) const
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
2012-08-29 19:27:03 +02:00
|
|
|
assert(value != nullptr);
|
|
|
|
assert(s != nullptr);
|
2007-12-28 03:56:25 +01:00
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
if (fold_case) {
|
|
|
|
char *p = g_utf8_casefold(s, -1);
|
|
|
|
const bool result = strstr(p, value) != NULL;
|
2008-10-15 19:36:37 +02:00
|
|
|
g_free(p);
|
2012-08-07 23:59:17 +02:00
|
|
|
return result;
|
|
|
|
} else {
|
2012-08-29 19:27:03 +02:00
|
|
|
return strcmp(s, value) == 0;
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
2012-08-07 23:59:17 +02:00
|
|
|
}
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
bool
|
2013-07-30 20:11:57 +02:00
|
|
|
SongFilter::Item::Match(const TagItem &item) const
|
2012-08-07 23:59:17 +02:00
|
|
|
{
|
2012-08-29 19:27:03 +02:00
|
|
|
return (tag == LOCATE_TAG_ANY_TYPE || (unsigned)item.type == tag) &&
|
|
|
|
StringMatch(item.value);
|
|
|
|
}
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
bool
|
2013-07-30 20:11:57 +02:00
|
|
|
SongFilter::Item::Match(const Tag &_tag) const
|
2012-08-29 19:27:03 +02:00
|
|
|
{
|
2012-06-27 09:36:02 +02:00
|
|
|
bool visited_types[TAG_NUM_OF_ITEM_TYPES];
|
2013-09-26 18:25:28 +02:00
|
|
|
std::fill_n(visited_types, TAG_NUM_OF_ITEM_TYPES, false);
|
2009-01-24 15:27:09 +01:00
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
for (unsigned i = 0; i < _tag.num_items; i++) {
|
|
|
|
visited_types[_tag.items[i]->type] = true;
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
if (Match(*_tag.items[i]))
|
2012-08-07 23:59:17 +02:00
|
|
|
return true;
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2008-09-29 13:18:49 +02:00
|
|
|
/** If the search critieron was not visited during the sweep
|
|
|
|
* through the song's tag, it means this field is absent from
|
|
|
|
* the tag or empty. Thus, if the searched string is also
|
|
|
|
* empty (first char is a \0), then it's a match as well and
|
2009-01-24 15:27:05 +01:00
|
|
|
* we should return true.
|
2008-09-29 13:18:49 +02:00
|
|
|
*/
|
2012-08-29 19:27:03 +02:00
|
|
|
if (*value == 0 && tag < TAG_NUM_OF_ITEM_TYPES &&
|
|
|
|
!visited_types[tag])
|
2009-01-24 15:27:05 +01:00
|
|
|
return true;
|
2008-09-29 13:18:49 +02:00
|
|
|
|
2012-08-07 23:59:17 +02:00
|
|
|
return false;
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
bool
|
2013-07-28 13:25:12 +02:00
|
|
|
SongFilter::Item::Match(const Song &song) const
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
2012-08-29 19:27:03 +02:00
|
|
|
if (tag == LOCATE_TAG_FILE_TYPE || tag == LOCATE_TAG_ANY_TYPE) {
|
2013-07-28 13:25:12 +02:00
|
|
|
char *uri = song.GetURI();
|
2012-08-29 19:27:03 +02:00
|
|
|
const bool result = StringMatch(uri);
|
2009-01-04 19:09:34 +01:00
|
|
|
g_free(uri);
|
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
if (result || tag == LOCATE_TAG_FILE_TYPE)
|
2012-08-07 23:59:17 +02:00
|
|
|
return result;
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
return song.tag != NULL && Match(*song.tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
SongFilter::SongFilter(unsigned tag, const char *value, bool fold_case)
|
|
|
|
{
|
|
|
|
items.push_back(Item(tag, value, fold_case));
|
|
|
|
}
|
|
|
|
|
|
|
|
SongFilter::~SongFilter()
|
|
|
|
{
|
|
|
|
/* this destructor exists here just so it won't get inlined */
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SongFilter::Parse(const char *tag_string, const char *value, bool fold_case)
|
|
|
|
{
|
|
|
|
unsigned tag = locate_parse_type(tag_string);
|
|
|
|
if (tag == TAG_NUM_OF_ITEM_TYPES)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
items.push_back(Item(tag, value, fold_case));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SongFilter::Parse(unsigned argc, char *argv[], bool fold_case)
|
|
|
|
{
|
|
|
|
if (argc == 0 || argc % 2 != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < argc; i += 2)
|
|
|
|
if (!Parse(argv[i], argv[i + 1], fold_case))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2009-01-24 15:27:05 +01:00
|
|
|
bool
|
2013-07-28 13:25:12 +02:00
|
|
|
SongFilter::Match(const Song &song) const
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
2012-08-29 19:27:03 +02:00
|
|
|
for (const auto &i : items)
|
|
|
|
if (!i.Match(song))
|
2009-01-24 15:27:05 +01:00
|
|
|
return false;
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2009-01-24 15:27:05 +01:00
|
|
|
return true;
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|