mpd/src/tag/Tag.cxx

404 lines
7.7 KiB
C++
Raw Normal View History

/*
2013-01-07 10:36:27 +01:00
* Copyright (C) 2003-2013 The Music Player Daemon Project
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
2013-07-30 20:11:57 +02:00
#include "Tag.hxx"
2013-01-07 10:36:27 +01:00
#include "TagInternal.hxx"
#include "TagPool.hxx"
#include <glib.h>
#include <assert.h>
2013-07-30 20:11:57 +02:00
#include <string.h>
/**
* Maximum number of items managed in the bulk list; if it is
* exceeded, we switch back to "normal" reallocation.
*/
#define BULK_MAX 64
static struct {
#ifndef NDEBUG
2009-02-27 08:06:59 +01:00
bool busy;
#endif
2013-07-30 20:11:57 +02:00
TagItem *items[BULK_MAX];
} bulk;
bool ignore_tag_items[TAG_NUM_OF_ITEM_TYPES];
enum tag_type
tag_name_parse(const char *name)
{
2013-01-07 10:36:27 +01:00
assert(name != nullptr);
for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i) {
2013-01-07 10:36:27 +01:00
assert(tag_item_names[i] != nullptr);
if (strcmp(name, tag_item_names[i]) == 0)
return (enum tag_type)i;
}
return TAG_NUM_OF_ITEM_TYPES;
}
enum tag_type
tag_name_parse_i(const char *name)
{
2013-01-07 10:36:27 +01:00
assert(name != nullptr);
for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i) {
2013-01-07 10:36:27 +01:00
assert(tag_item_names[i] != nullptr);
if (g_ascii_strcasecmp(name, tag_item_names[i]) == 0)
return (enum tag_type)i;
}
return TAG_NUM_OF_ITEM_TYPES;
}
2013-07-30 20:11:57 +02:00
static size_t
items_size(const Tag &tag)
{
2013-07-30 20:11:57 +02:00
return tag.num_items * sizeof(TagItem *);
}
2008-08-29 09:38:21 +02:00
void tag_lib_init(void)
{
/* ignore comments by default */
ignore_tag_items[TAG_COMMENT] = true;
}
void
Tag::Clear()
{
time = -1;
has_playlist = false;
tag_pool_lock.lock();
for (unsigned i = 0; i < num_items; ++i)
tag_pool_put_item(items[i]);
tag_pool_lock.unlock();
2013-07-31 09:02:07 +02:00
if (items == bulk.items) {
#ifndef NDEBUG
assert(bulk.busy);
bulk.busy = false;
#endif
} else
g_free(items);
items = nullptr;
num_items = 0;
}
2013-07-30 20:11:57 +02:00
Tag::~Tag()
{
tag_pool_lock.lock();
2013-07-30 20:11:57 +02:00
for (int i = num_items; --i >= 0; )
tag_pool_put_item(items[i]);
tag_pool_lock.unlock();
2013-07-30 20:11:57 +02:00
if (items == bulk.items) {
#ifndef NDEBUG
assert(bulk.busy);
2009-02-27 08:06:59 +01:00
bulk.busy = false;
#endif
} else
2013-07-30 20:11:57 +02:00
g_free(items);
}
2013-07-30 20:11:57 +02:00
Tag::Tag(const Tag &other)
:time(other.time), has_playlist(other.has_playlist),
items(nullptr),
num_items(other.num_items)
{
2013-07-30 20:11:57 +02:00
if (num_items > 0) {
items = (TagItem **)g_malloc(items_size(other));
2013-07-30 20:11:57 +02:00
tag_pool_lock.lock();
for (unsigned i = 0; i < num_items; i++)
items[i] = tag_pool_dup_item(other.items[i]);
tag_pool_lock.unlock();
}
}
2013-07-30 20:11:57 +02:00
Tag *
Tag::Merge(const Tag &base, const Tag &add)
{
unsigned n;
/* allocate new tag object */
2013-07-30 20:11:57 +02:00
Tag *ret = new Tag();
ret->time = add.time > 0 ? add.time : base.time;
ret->num_items = base.num_items + add.num_items;
2013-01-07 10:36:27 +01:00
ret->items = ret->num_items > 0
2013-07-30 20:11:57 +02:00
? (TagItem **)g_malloc(items_size(*ret))
2013-01-07 10:36:27 +01:00
: nullptr;
tag_pool_lock.lock();
/* copy all items from "add" */
2013-07-30 20:11:57 +02:00
for (unsigned i = 0; i < add.num_items; ++i)
ret->items[i] = tag_pool_dup_item(add.items[i]);
2013-07-30 20:11:57 +02:00
n = add.num_items;
/* copy additional items from "base" */
2013-07-30 20:11:57 +02:00
for (unsigned i = 0; i < base.num_items; ++i)
if (!add.HasType(base.items[i]->type))
ret->items[n++] = tag_pool_dup_item(base.items[i]);
tag_pool_lock.unlock();
assert(n <= ret->num_items);
if (n < ret->num_items) {
/* some tags were not copied - shrink ret->items */
assert(n > 0);
ret->num_items = n;
2013-07-30 20:11:57 +02:00
ret->items = (TagItem **)
g_realloc(ret->items, items_size(*ret));
}
return ret;
}
2013-07-30 20:11:57 +02:00
Tag *
Tag::MergeReplace(Tag *base, Tag *add)
{
2013-01-07 10:36:27 +01:00
if (add == nullptr)
return base;
2013-01-07 10:36:27 +01:00
if (base == nullptr)
return add;
2013-07-30 20:11:57 +02:00
Tag *tag = Merge(*base, *add);
delete base;
delete add;
return tag;
}
2009-01-15 00:21:08 +01:00
const char *
2013-07-30 20:11:57 +02:00
Tag::GetValue(tag_type type) const
{
assert(type < TAG_NUM_OF_ITEM_TYPES);
2013-07-30 20:11:57 +02:00
for (unsigned i = 0; i < num_items; i++)
if (items[i]->type == type)
return items[i]->value;
2009-01-15 00:21:08 +01:00
2013-01-07 10:36:27 +01:00
return nullptr;
2009-01-15 00:21:08 +01:00
}
2013-07-30 20:11:57 +02:00
bool
Tag::HasType(tag_type type) const
2009-01-15 00:21:08 +01:00
{
2013-07-30 20:11:57 +02:00
return GetValue(type) != nullptr;
}
/**
* Replace invalid sequences with the question mark.
*/
static char *
patch_utf8(const char *src, size_t length, const gchar *end)
{
/* duplicate the string, and replace invalid bytes in that
buffer */
char *dest = g_strdup(src);
do {
dest[end - src] = '?';
} while (!g_utf8_validate(end + 1, (src + length) - (end + 1), &end));
return dest;
}
static char *
fix_utf8(const char *str, size_t length)
{
const gchar *end;
char *temp;
gsize written;
2013-01-07 10:36:27 +01:00
assert(str != nullptr);
/* check if the string is already valid UTF-8 */
if (g_utf8_validate(str, length, &end))
2013-01-07 10:36:27 +01:00
return nullptr;
/* no, it's not - try to import it from ISO-Latin-1 */
temp = g_convert(str, length, "utf-8", "iso-8859-1",
2013-01-07 10:36:27 +01:00
nullptr, &written, nullptr);
if (temp != nullptr)
/* success! */
return temp;
/* no, still broken - there's no medication, just patch
invalid sequences */
return patch_utf8(str, length, end);
}
2013-07-30 20:11:57 +02:00
void
Tag::BeginAdd()
{
assert(!bulk.busy);
2013-07-30 20:11:57 +02:00
assert(items == nullptr);
assert(num_items == 0);
#ifndef NDEBUG
2009-02-27 08:06:59 +01:00
bulk.busy = true;
#endif
2013-07-30 20:11:57 +02:00
items = bulk.items;
}
2013-07-30 20:11:57 +02:00
void
Tag::EndAdd()
{
2013-07-30 20:11:57 +02:00
if (items == bulk.items) {
assert(num_items <= BULK_MAX);
2013-07-30 20:11:57 +02:00
if (num_items > 0) {
/* copy the tag items from the bulk list over
to a new list (which fits exactly) */
2013-07-30 20:11:57 +02:00
items = (TagItem **)
g_malloc(items_size(*this));
memcpy(items, bulk.items, items_size(*this));
} else
2013-07-30 20:11:57 +02:00
items = nullptr;
}
#ifndef NDEBUG
2009-02-27 08:06:59 +01:00
bulk.busy = false;
#endif
}
static bool
char_is_non_printable(unsigned char ch)
{
return ch < 0x20;
}
static const char *
find_non_printable(const char *p, size_t length)
{
for (size_t i = 0; i < length; ++i)
if (char_is_non_printable(p[i]))
return p + i;
2013-01-07 10:36:27 +01:00
return nullptr;
}
/**
* Clears all non-printable characters, convert them to space.
2013-01-07 10:36:27 +01:00
* Returns nullptr if nothing needs to be cleared.
*/
static char *
clear_non_printable(const char *p, size_t length)
{
const char *first = find_non_printable(p, length);
char *dest;
2013-01-07 10:36:27 +01:00
if (first == nullptr)
return nullptr;
dest = g_strndup(p, length);
for (size_t i = first - p; i < length; ++i)
if (char_is_non_printable(dest[i]))
dest[i] = ' ';
return dest;
}
static char *
fix_tag_value(const char *p, size_t length)
{
char *utf8, *cleared;
utf8 = fix_utf8(p, length);
2013-01-07 10:36:27 +01:00
if (utf8 != nullptr) {
p = utf8;
length = strlen(p);
}
cleared = clear_non_printable(p, length);
2013-01-07 10:36:27 +01:00
if (cleared == nullptr)
cleared = utf8;
else
g_free(utf8);
return cleared;
}
2013-07-30 20:11:57 +02:00
void
Tag::AddItemInternal(tag_type type, const char *value, size_t len)
{
2013-07-30 20:11:57 +02:00
unsigned int i = num_items;
char *p;
p = fix_tag_value(value, len);
2013-01-07 10:36:27 +01:00
if (p != nullptr) {
value = p;
len = strlen(value);
}
2013-07-30 20:11:57 +02:00
num_items++;
2013-07-30 20:11:57 +02:00
if (items != bulk.items)
/* bulk mode disabled */
2013-07-30 20:11:57 +02:00
items = (TagItem **)
g_realloc(items, items_size(*this));
else if (num_items >= BULK_MAX) {
/* bulk list already full - switch back to non-bulk */
assert(bulk.busy);
2013-07-30 20:11:57 +02:00
items = (TagItem **)g_malloc(items_size(*this));
memcpy(items, bulk.items,
items_size(*this) - sizeof(TagItem *));
}
tag_pool_lock.lock();
2013-07-30 20:11:57 +02:00
items[i] = tag_pool_get_item(type, value, len);
tag_pool_lock.unlock();
g_free(p);
}
2013-07-30 20:11:57 +02:00
void
Tag::AddItem(tag_type type, const char *value, size_t len)
{
if (ignore_tag_items[type])
return;
2013-07-30 20:11:57 +02:00
if (value == nullptr || len == 0)
return;
2013-07-30 20:11:57 +02:00
AddItemInternal(type, value, len);
}
void
Tag::AddItem(tag_type type, const char *value)
{
AddItem(type, value, strlen(value));
}