2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-02-24 00:41:20 +01: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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tag.h"
|
tag: added a pool for tag items
The new source tag_pool.c manages a pool of reference counted tag_item
objects. This is used to merge tag items of the same type and value,
saving lots of memory. Formerly, only the value itself was pooled,
wasting memory for all the pointers and tag_item structs.
The following results were measured with massif. Started MPD on
amd64, typed "mpc", no song being played. My music database contains
35k tagged songs. The results are what massif reports as "peak".
0.13.2: total 14,131,392; useful 11,408,972; extra 2,722,420
eric: total 18,370,696; useful 15,648,182; extra 2,722,514
mk f34f694: total 15,833,952; useful 13,111,470; extra 2,722,482
mk now: total 12,837,632; useful 10,626,383; extra 2,211,249
This patch set saves 20% memory, and does a good job in reducing heap
fragmentation.
2008-08-29 09:38:37 +02:00
|
|
|
#include "tag_pool.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "myfprintf.h"
|
2004-03-09 21:55:51 +01:00
|
|
|
#include "utils.h"
|
2004-04-13 04:20:46 +02:00
|
|
|
#include "utf8.h"
|
2004-04-13 04:38:09 +02:00
|
|
|
#include "log.h"
|
2004-10-05 19:16:26 +02:00
|
|
|
#include "conf.h"
|
2004-11-10 22:58:27 +01:00
|
|
|
#include "tagTracker.h"
|
2007-03-20 21:12:53 +01:00
|
|
|
#include "song.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
const char *mpdTagItemKeys[TAG_NUM_OF_ITEM_TYPES] = {
|
2004-11-10 22:58:27 +01:00
|
|
|
"Artist",
|
|
|
|
"Album",
|
|
|
|
"Title",
|
|
|
|
"Track",
|
|
|
|
"Name",
|
|
|
|
"Genre",
|
2005-03-05 23:24:10 +01:00
|
|
|
"Date",
|
|
|
|
"Composer",
|
|
|
|
"Performer",
|
2006-04-30 21:55:56 +02:00
|
|
|
"Comment",
|
|
|
|
"Disc"
|
2004-11-10 22:58:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static mpd_sint8 ignoreTagItems[TAG_NUM_OF_ITEM_TYPES];
|
|
|
|
|
2008-08-29 09:38:21 +02:00
|
|
|
void tag_lib_init(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-11-10 22:58:27 +01:00
|
|
|
int quit = 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
char *temp;
|
|
|
|
char *s;
|
|
|
|
char *c;
|
|
|
|
ConfigParam *param;
|
2004-11-10 22:58:27 +01:00
|
|
|
int i;
|
2004-11-11 22:43:39 +01:00
|
|
|
|
|
|
|
/* parse the "metadata_to_use" config parameter below */
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
memset(ignoreTagItems, 0, TAG_NUM_OF_ITEM_TYPES);
|
2006-07-20 18:02:40 +02:00
|
|
|
ignoreTagItems[TAG_ITEM_COMMENT] = 1; /* ignore comments by default */
|
2004-11-10 22:58:27 +01:00
|
|
|
|
|
|
|
param = getConfigParam(CONF_METADATA_TO_USE);
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
if (!param)
|
|
|
|
return;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
|
|
|
memset(ignoreTagItems, 1, TAG_NUM_OF_ITEM_TYPES);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (0 == strcasecmp(param->value, "none"))
|
|
|
|
return;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2006-08-26 08:25:57 +02:00
|
|
|
temp = c = s = xstrdup(param->value);
|
2006-07-20 18:02:40 +02:00
|
|
|
while (!quit) {
|
|
|
|
if (*s == ',' || *s == '\0') {
|
|
|
|
if (*s == '\0')
|
|
|
|
quit = 1;
|
2004-11-10 22:58:27 +01:00
|
|
|
*s = '\0';
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < TAG_NUM_OF_ITEM_TYPES; i++) {
|
|
|
|
if (strcasecmp(c, mpdTagItemKeys[i]) == 0) {
|
2004-11-10 22:58:27 +01:00
|
|
|
ignoreTagItems[i] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (strlen(c) && i == TAG_NUM_OF_ITEM_TYPES) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("error parsing metadata item \"%s\" at "
|
2006-07-20 18:02:40 +02:00
|
|
|
"line %i\n", c, param->line);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
s++;
|
|
|
|
c = s;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
free(temp);
|
2004-04-13 04:20:46 +02:00
|
|
|
}
|
|
|
|
|
2008-08-29 09:38:21 +02:00
|
|
|
void tag_print_types(int fd)
|
2007-03-31 20:43:16 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < TAG_NUM_OF_ITEM_TYPES; i++) {
|
|
|
|
if (ignoreTagItems[i] == 0)
|
|
|
|
fdprintf(fd, "tagtype: %s\n", mpdTagItemKeys[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-29 09:38:21 +02:00
|
|
|
void tag_print(int fd, struct tag *tag)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-11-10 22:58:27 +01:00
|
|
|
int i;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tag->time >= 0)
|
2007-03-20 22:05:42 +01:00
|
|
|
fdprintf(fd, SONG_TIME "%i\n", tag->time);
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < tag->numOfItems; i++) {
|
2008-08-29 09:38:29 +02:00
|
|
|
fdprintf(fd, "%s: %s\n", mpdTagItemKeys[tag->items[i]->type],
|
|
|
|
tag->items[i]->value);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
2004-04-13 04:20:46 +02:00
|
|
|
}
|
|
|
|
|
2008-08-29 09:38:21 +02:00
|
|
|
struct tag *tag_ape_load(char *file)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-08-29 09:38:11 +02:00
|
|
|
struct tag *ret = NULL;
|
2008-03-26 11:37:36 +01:00
|
|
|
FILE *fp;
|
2005-03-07 01:51:21 +01:00
|
|
|
int tagCount;
|
2006-07-20 18:02:40 +02:00
|
|
|
char *buffer = NULL;
|
|
|
|
char *p;
|
2008-03-26 11:38:07 +01:00
|
|
|
size_t tagLen;
|
|
|
|
size_t size;
|
2005-03-07 01:51:21 +01:00
|
|
|
unsigned long flags;
|
|
|
|
int i;
|
2006-07-20 18:02:40 +02:00
|
|
|
char *key;
|
2005-03-07 01:51:21 +01:00
|
|
|
|
|
|
|
struct {
|
|
|
|
unsigned char id[8];
|
|
|
|
unsigned char version[4];
|
|
|
|
unsigned char length[4];
|
|
|
|
unsigned char tagCount[4];
|
|
|
|
unsigned char flags[4];
|
|
|
|
unsigned char reserved[8];
|
|
|
|
} footer;
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
const char *apeItems[7] = {
|
2005-03-07 01:51:21 +01:00
|
|
|
"title",
|
|
|
|
"artist",
|
|
|
|
"album",
|
|
|
|
"comment",
|
|
|
|
"genre",
|
|
|
|
"track",
|
|
|
|
"year"
|
|
|
|
};
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int tagItems[7] = {
|
2005-03-07 01:51:21 +01:00
|
|
|
TAG_ITEM_TITLE,
|
|
|
|
TAG_ITEM_ARTIST,
|
|
|
|
TAG_ITEM_ALBUM,
|
|
|
|
TAG_ITEM_COMMENT,
|
|
|
|
TAG_ITEM_GENRE,
|
|
|
|
TAG_ITEM_TRACK,
|
|
|
|
TAG_ITEM_DATE,
|
|
|
|
};
|
|
|
|
|
|
|
|
fp = fopen(file, "r");
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!fp)
|
|
|
|
return NULL;
|
2005-03-07 01:51:21 +01:00
|
|
|
|
|
|
|
/* determine if file has an apeV2 tag */
|
2006-07-20 18:02:40 +02:00
|
|
|
if (fseek(fp, 0, SEEK_END))
|
|
|
|
goto fail;
|
2008-03-26 11:38:07 +01:00
|
|
|
size = (size_t)ftell(fp);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (fseek(fp, size - sizeof(footer), SEEK_SET))
|
|
|
|
goto fail;
|
|
|
|
if (fread(&footer, 1, sizeof(footer), fp) != sizeof(footer))
|
|
|
|
goto fail;
|
|
|
|
if (memcmp(footer.id, "APETAGEX", sizeof(footer.id)) != 0)
|
|
|
|
goto fail;
|
|
|
|
if (readLEuint32(footer.version) != 2000)
|
|
|
|
goto fail;
|
2005-03-07 01:51:21 +01:00
|
|
|
|
2006-08-11 23:50:56 +02:00
|
|
|
/* find beginning of ape tag */
|
2005-03-07 01:51:21 +01:00
|
|
|
tagLen = readLEuint32(footer.length);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tagLen < sizeof(footer))
|
|
|
|
goto fail;
|
|
|
|
if (fseek(fp, size - tagLen, SEEK_SET))
|
|
|
|
goto fail;
|
2005-03-07 01:51:21 +01:00
|
|
|
|
|
|
|
/* read tag into buffer */
|
|
|
|
tagLen -= sizeof(footer);
|
2006-11-26 04:51:46 +01:00
|
|
|
if (tagLen <= 0)
|
|
|
|
goto fail;
|
2006-08-26 08:25:57 +02:00
|
|
|
buffer = xmalloc(tagLen);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (fread(buffer, 1, tagLen, fp) != tagLen)
|
|
|
|
goto fail;
|
2005-03-07 01:51:21 +01:00
|
|
|
|
|
|
|
/* read tags */
|
|
|
|
tagCount = readLEuint32(footer.tagCount);
|
|
|
|
p = buffer;
|
2006-07-20 18:02:40 +02:00
|
|
|
while (tagCount-- && tagLen > 10) {
|
2006-07-17 23:46:32 +02:00
|
|
|
size = readLEuint32((unsigned char *)p);
|
2005-03-07 01:51:21 +01:00
|
|
|
p += 4;
|
|
|
|
tagLen -= 4;
|
2006-07-17 23:46:32 +02:00
|
|
|
flags = readLEuint32((unsigned char *)p);
|
2005-03-07 01:51:21 +01:00
|
|
|
p += 4;
|
|
|
|
tagLen -= 4;
|
|
|
|
|
|
|
|
/* get the key */
|
|
|
|
key = p;
|
2006-07-20 18:02:40 +02:00
|
|
|
while (tagLen - size > 0 && *p != '\0') {
|
2005-03-07 01:51:21 +01:00
|
|
|
p++;
|
|
|
|
tagLen--;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
tagLen--;
|
|
|
|
|
|
|
|
/* get the value */
|
2008-03-26 11:38:07 +01:00
|
|
|
if (tagLen < size)
|
2006-07-20 18:02:40 +02:00
|
|
|
goto fail;
|
2005-03-07 01:51:21 +01:00
|
|
|
|
|
|
|
/* we only care about utf-8 text tags */
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!(flags & (0x3 << 1))) {
|
|
|
|
for (i = 0; i < 7; i++) {
|
|
|
|
if (strcasecmp(key, apeItems[i]) == 0) {
|
|
|
|
if (!ret)
|
2008-08-29 09:38:21 +02:00
|
|
|
ret = tag_new();
|
|
|
|
tag_add_item_n(ret, tagItems[i],
|
|
|
|
p, size);
|
2005-03-07 01:51:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p += size;
|
|
|
|
tagLen -= size;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2006-08-10 00:18:06 +02:00
|
|
|
fail:
|
2006-07-20 18:02:40 +02:00
|
|
|
if (fp)
|
|
|
|
fclose(fp);
|
|
|
|
if (buffer)
|
|
|
|
free(buffer);
|
2005-03-07 01:51:21 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-29 09:38:21 +02:00
|
|
|
struct tag *tag_new(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-08-29 09:38:11 +02:00
|
|
|
struct tag *ret = xmalloc(sizeof(*ret));
|
2004-11-10 22:58:27 +01:00
|
|
|
ret->items = NULL;
|
2004-03-11 01:16:49 +01:00
|
|
|
ret->time = -1;
|
2004-11-10 22:58:27 +01:00
|
|
|
ret->numOfItems = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-29 09:38:11 +02:00
|
|
|
static void deleteItem(struct tag *tag, int idx)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-01-26 13:46:21 +01:00
|
|
|
assert(idx < tag->numOfItems);
|
2007-03-09 15:44:09 +01:00
|
|
|
tag->numOfItems--;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
tag: added a pool for tag items
The new source tag_pool.c manages a pool of reference counted tag_item
objects. This is used to merge tag items of the same type and value,
saving lots of memory. Formerly, only the value itself was pooled,
wasting memory for all the pointers and tag_item structs.
The following results were measured with massif. Started MPD on
amd64, typed "mpc", no song being played. My music database contains
35k tagged songs. The results are what massif reports as "peak".
0.13.2: total 14,131,392; useful 11,408,972; extra 2,722,420
eric: total 18,370,696; useful 15,648,182; extra 2,722,514
mk f34f694: total 15,833,952; useful 13,111,470; extra 2,722,482
mk now: total 12,837,632; useful 10,626,383; extra 2,211,249
This patch set saves 20% memory, and does a good job in reducing heap
fragmentation.
2008-08-29 09:38:37 +02:00
|
|
|
tag_pool_put_item(tag->items[idx]);
|
2008-01-26 13:46:21 +01:00
|
|
|
/* free(tag->items[idx].value); */
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
if (tag->numOfItems - idx > 0) {
|
|
|
|
memmove(tag->items + idx, tag->items + idx + 1,
|
|
|
|
tag->numOfItems - idx);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tag->numOfItems > 0) {
|
2006-08-26 08:25:57 +02:00
|
|
|
tag->items = xrealloc(tag->items,
|
2008-08-29 09:38:11 +02:00
|
|
|
tag->numOfItems * sizeof(*tag->items));
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2004-11-10 22:58:27 +01:00
|
|
|
free(tag->items);
|
|
|
|
tag->items = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-29 09:38:21 +02:00
|
|
|
void tag_clear_items_by_type(struct tag *tag, enum tag_type type)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-03-26 11:37:36 +01:00
|
|
|
int i;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < tag->numOfItems; i++) {
|
2008-08-29 09:38:29 +02:00
|
|
|
if (tag->items[i]->type == type) {
|
2004-11-10 22:58:27 +01:00
|
|
|
deleteItem(tag, i);
|
|
|
|
/* decrement since when just deleted this node */
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-29 09:38:11 +02:00
|
|
|
static void clearMpdTag(struct tag *tag)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-11-10 22:58:27 +01:00
|
|
|
int i;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < tag->numOfItems; i++) {
|
2005-11-19 11:52:47 +01:00
|
|
|
/* free(tag->items[i].value); */
|
tag: added a pool for tag items
The new source tag_pool.c manages a pool of reference counted tag_item
objects. This is used to merge tag items of the same type and value,
saving lots of memory. Formerly, only the value itself was pooled,
wasting memory for all the pointers and tag_item structs.
The following results were measured with massif. Started MPD on
amd64, typed "mpc", no song being played. My music database contains
35k tagged songs. The results are what massif reports as "peak".
0.13.2: total 14,131,392; useful 11,408,972; extra 2,722,420
eric: total 18,370,696; useful 15,648,182; extra 2,722,514
mk f34f694: total 15,833,952; useful 13,111,470; extra 2,722,482
mk now: total 12,837,632; useful 10,626,383; extra 2,211,249
This patch set saves 20% memory, and does a good job in reducing heap
fragmentation.
2008-08-29 09:38:37 +02:00
|
|
|
tag_pool_put_item(tag->items[i]);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tag->items)
|
|
|
|
free(tag->items);
|
2004-11-10 22:58:27 +01:00
|
|
|
tag->items = NULL;
|
|
|
|
|
|
|
|
tag->numOfItems = 0;
|
|
|
|
|
|
|
|
tag->time = -1;
|
2004-05-31 13:42:46 +02:00
|
|
|
}
|
|
|
|
|
2008-08-29 09:38:21 +02:00
|
|
|
void tag_free(struct tag *tag)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
clearMpdTag(tag);
|
2004-02-24 00:41:20 +01:00
|
|
|
free(tag);
|
|
|
|
}
|
|
|
|
|
2008-08-29 09:38:21 +02:00
|
|
|
struct tag *tag_dup(struct tag *tag)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-08-29 09:38:11 +02:00
|
|
|
struct tag *ret;
|
2004-11-10 22:58:27 +01:00
|
|
|
int i;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!tag)
|
|
|
|
return NULL;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-08-29 09:38:21 +02:00
|
|
|
ret = tag_new();
|
2004-11-10 22:58:27 +01:00
|
|
|
ret->time = tag->time;
|
2004-06-01 12:28:06 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < tag->numOfItems; i++) {
|
2008-08-29 09:38:29 +02:00
|
|
|
tag_add_item(ret, tag->items[i]->type, tag->items[i]->value);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
2004-06-01 12:28:06 +02:00
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
return ret;
|
2004-06-01 12:28:06 +02:00
|
|
|
}
|
|
|
|
|
2008-08-29 09:38:21 +02:00
|
|
|
int tag_equal(struct tag *tag1, struct tag *tag2)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-11-10 22:58:27 +01:00
|
|
|
int i;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tag1 == NULL && tag2 == NULL)
|
|
|
|
return 1;
|
|
|
|
else if (!tag1 || !tag2)
|
|
|
|
return 0;
|
2004-06-01 12:28:06 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tag1->time != tag2->time)
|
|
|
|
return 0;
|
2004-06-01 12:28:06 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tag1->numOfItems != tag2->numOfItems)
|
|
|
|
return 0;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < tag1->numOfItems; i++) {
|
2008-08-29 09:38:29 +02:00
|
|
|
if (tag1->items[i]->type != tag2->items[i]->type)
|
2006-07-20 18:02:40 +02:00
|
|
|
return 0;
|
2008-08-29 09:38:29 +02:00
|
|
|
if (strcmp(tag1->items[i]->value, tag2->items[i]->value)) {
|
2004-11-10 22:58:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2004-06-01 12:28:06 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
return 1;
|
2004-06-01 12:28:06 +02:00
|
|
|
}
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2008-08-29 09:39:04 +02:00
|
|
|
static inline const char *fix_utf8(const char *str, size_t *length_r) {
|
|
|
|
const char *temp;
|
2008-08-29 09:38:54 +02:00
|
|
|
|
2008-08-29 09:38:56 +02:00
|
|
|
assert(str != NULL);
|
|
|
|
|
2008-08-29 09:39:01 +02:00
|
|
|
if (validUtf8String(str, *length_r))
|
2008-08-29 09:38:54 +02:00
|
|
|
return str;
|
|
|
|
|
|
|
|
DEBUG("not valid utf8 in tag: %s\n",str);
|
|
|
|
temp = latin1StrToUtf8Dup(str);
|
2008-08-29 09:39:01 +02:00
|
|
|
*length_r = strlen(temp);
|
2008-08-29 09:38:54 +02:00
|
|
|
return temp;
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2008-08-29 09:38:11 +02:00
|
|
|
static void appendToTagItems(struct tag *tag, enum tag_type type,
|
2008-08-28 20:02:17 +02:00
|
|
|
const char *value, size_t len)
|
2004-11-10 22:58:27 +01:00
|
|
|
{
|
2008-08-28 20:02:16 +02:00
|
|
|
unsigned int i = tag->numOfItems;
|
2008-08-29 09:39:04 +02:00
|
|
|
const char *p;
|
|
|
|
|
|
|
|
p = fix_utf8(value, &len);
|
|
|
|
if (memchr(p, '\n', len) != NULL) {
|
|
|
|
char *duplicated = xmalloc(len + 1);
|
|
|
|
memcpy(duplicated, p, len);
|
|
|
|
duplicated[len] = '\0';
|
|
|
|
if (p != value)
|
|
|
|
xfree(p);
|
|
|
|
|
|
|
|
stripReturnChar(duplicated);
|
|
|
|
p = duplicated;
|
|
|
|
}
|
2004-11-10 22:58:27 +01:00
|
|
|
|
|
|
|
tag->numOfItems++;
|
2008-08-29 09:38:11 +02:00
|
|
|
tag->items = xrealloc(tag->items,
|
|
|
|
tag->numOfItems * sizeof(*tag->items));
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2008-08-29 09:39:04 +02:00
|
|
|
tag->items[i] = tag_pool_get_item(type, p, len);
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2008-08-29 09:39:04 +02:00
|
|
|
if (p != value)
|
|
|
|
xfree(p);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2008-08-29 09:38:21 +02:00
|
|
|
void tag_add_item_n(struct tag *tag, enum tag_type itemType,
|
|
|
|
const char *value, size_t len)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
if (ignoreTagItems[itemType])
|
2007-11-21 13:15:00 +01:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
return;
|
2007-11-21 13:15:00 +01:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!value || !len)
|
|
|
|
return;
|
2004-11-10 23:13:30 +01:00
|
|
|
|
|
|
|
/* we can't hold more than 255 items */
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tag->numOfItems == 255)
|
|
|
|
return;
|
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
appendToTagItems(tag, itemType, value, len);
|
|
|
|
}
|