First cut of itunes xml parsing -- this should augment song info. Still no playlist info

This commit is contained in:
Ron Pedde 2005-04-27 06:13:26 +00:00
parent 7a7cd186bb
commit 27a7426bc2
9 changed files with 1215 additions and 16 deletions

View File

@ -35,6 +35,7 @@ mt_daapd_SOURCES = main.c daapd.h rend.h uici.c uici.h webserver.c \
mp3-scanner.h mp3-scanner.c rend-unix.h strcasestr.c strcasestr.h strsep.c \
dynamic-art.c dynamic-art.h query.c query.h ssc.c ssc.h \
db-generic.c db-generic.h dispatch.c dispatch.h wma.c \
scan-xml.c ezxml.c ezxml.h \
$(PRENDSRC) $(ORENDSRC) $(HRENDSRC) $(OGGVORBISSRC) $(FLACSRC) \
$(SQLITEDB)

View File

@ -47,6 +47,7 @@ static pthread_mutex_t db_sqlite_mutex = PTHREAD_MUTEX_INITIALIZER; /**< sqlite
static sqlite_vm *db_sqlite_pvm;
static int db_sqlite_in_scan=0;
static int db_sqlite_reload=0;
static int db_sqlite_in_playlist_scan=0;
static char db_path[PATH_MAX + 1];
@ -284,6 +285,7 @@ int db_sqlite_start_scan(void) {
}
db_sqlite_in_scan=1;
db_sqlite_in_playlist_scan=0;
return 0;
}
@ -301,6 +303,9 @@ int db_sqlite_end_song_scan(void) {
db_sqlite_exec(E_FATAL,"drop table updated");
}
db_sqlite_in_scan=0;
db_sqlite_in_playlist_scan=1;
return 0;
}
@ -319,9 +324,8 @@ int db_sqlite_end_scan(void) {
}
db_sqlite_update_playlists();
db_sqlite_reload=0;
db_sqlite_in_scan=0;
return 0;
}
@ -583,7 +587,7 @@ int db_sqlite_add(MP3FILE *pmp3) {
db_sqlite_exec(E_FATAL,"INSERT INTO updated VALUES (last_insert_rowid())");
}
if(!db_sqlite_in_scan)
if((!db_sqlite_in_scan) && (!db_sqlite_in_playlist_scan))
db_sqlite_update_playlists();
DPRINTF(E_SPAM,L_DB,"Exiting db_sqlite_add\n");
@ -627,6 +631,7 @@ int db_sqlite_update(MP3FILE *pmp3) {
"time_modified=%d," // time_modified
"db_timestamp=%d," // db_timestamp
"bpm=%d," // bpm
"disabled=%d," // disabled
"compilation=%d," // compilation
"rating=%d," // rating
"sample_count=%d," // sample_count
@ -655,6 +660,7 @@ int db_sqlite_update(MP3FILE *pmp3) {
pmp3->time_modified,
pmp3->db_timestamp,
pmp3->bpm,
pmp3->disabled,
pmp3->compilation,
pmp3->rating,
pmp3->sample_count,
@ -666,7 +672,7 @@ int db_sqlite_update(MP3FILE *pmp3) {
pmp3->path);
}
if(!db_sqlite_in_scan)
if((!db_sqlite_in_scan) && (!db_sqlite_in_playlist_scan))
db_sqlite_update_playlists();
return 0;

712
src/ezxml.c Normal file
View File

@ -0,0 +1,712 @@
/* ezxml.c
*
* Copyright 2005 Aaron Voisine <aaron@voisine.org>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#ifndef EZXML_NOMMAP
#include <sys/mman.h>
#endif // EZXML_NOMMAP
#include <sys/stat.h>
#include "ezxml.h"
#define EZXML_TXTM 0x80 // flag value meaning txt was malloced
#define EZXML_WS "\t\r\n " // whitespace
typedef struct ezxml_root *ezxml_root_t;
struct ezxml_root { // additional data for the root tag
struct ezxml xml; // is a super-struct built on top of ezxml struct
ezxml_t cur; // current xml tree insertion point
void *m; // original xml string
size_t len; // length of allocated memory for mmap, or -1 for malloc
void *u; // UTF-8 conversion of string if original was UTF-16
const char *err; // error string
char ***pi; // processing instructions
};
const char *EZXML_NOATTR[] = { NULL }; // empty tag attribute list
// called when parser finds close of tag
#define ezxml_close_tag(root) root->cur = root->cur->parent;
// returns the first child tag with the given name or NULL if not found
ezxml_t ezxml_child(ezxml_t xml, const char *name)
{
xml = (xml) ? xml->child : NULL;
while (xml && strcmp(name, xml->name)) xml = xml->sibling;
return xml;
}
// returns the Nth tag with the same name in the same subsection or NULL if not
// found
ezxml_t ezxml_idx(ezxml_t xml, int idx)
{
for (; xml && idx; idx--) xml = xml->next;
return xml;
}
// returns the value of the requested tag attribute or NULL if not found
const char *ezxml_attr(ezxml_t xml, const char *attr)
{
int i = 0;
if (! xml) return NULL;
while (xml->attr[i] && strcmp(attr, xml->attr[i])) i += 2;
return (xml->attr[i]) ? xml->attr[i + 1] : NULL;
}
// same as ezxml_get but takes an alredy initialized va_list
ezxml_t ezxml_vget(ezxml_t xml, va_list ap)
{
char *name = va_arg(ap, char *);
int idx = -1;
if (name && *name) {
idx = va_arg(ap, int);
xml = ezxml_child(xml, name);
}
return (idx < 0) ? xml : ezxml_vget(ezxml_idx(xml, idx), ap);
}
// Traverses the xml tree to retrive a specific subtag. Takes a variable
// length list of tag names and indexes. The argument list must be terminated
// by either an index of -1 or an empty string tag name. Example:
// title = ezxml_get(library, "shelf", 0, "book", 2, "title", -1);
// This retrieves the title of the 3rd book on the 1st shelf of library.
// Returns NULL if not found.
ezxml_t ezxml_get(ezxml_t xml, ...)
{
va_list ap;
ezxml_t ret;
va_start(ap, xml);
ret = ezxml_vget(xml, ap);
va_end(ap);
return ret;
}
// returns a NULL terminated array of processing instructions for the given
// target
const char **ezxml_pi(ezxml_t xml, const char *target)
{
static const char *nopi = NULL;
ezxml_root_t root;
int i = 0;
while (xml->parent) xml = xml->parent;
root = (ezxml_root_t)xml;
if (! root->pi) return &nopi;
while (root->pi[i] && strcmp(target, root->pi[i][0])) i++;
return (root->pi[i]) ? (const char **)root->pi[i] + 1 : &nopi;
}
// Converts \r or \r\n to a single \n. If decode is non-zero, decodes ampersand
// sequences in place. Returns s.
char *ezxml_decode(char *s, int decode)
{
int b;
char *e, *ret = s;
long c, d;
for (;;) {
while (*s && *s != '\r' && *s != '&') s++;
if (! *s) return ret;
else if (*s == '\r') {
*(s++) = '\n';
if (*s == '\n') memmove(s, (s + 1), strlen(s));
continue;
}
else if (! decode) { s++; continue; }
else if (! strncmp(s, "&lt;", 4)) *(s++) = '<';
else if (! strncmp(s, "&gt;", 4)) *(s++) = '>';
else if (! strncmp(s, "&quot;", 6)) *(s++) = '"';
else if (! strncmp(s, "&apos;", 6)) *(s++) = '\'';
else if (! strncmp(s, "&amp;", 5)) s++;
else if (! strncmp(s, "&#", 2)) {
if (s[2] == 'x') c = strtol(s + 3, &e, 16); // base 16
else c = strtol(s + 2, &e, 10); // base 10
if (! c || *e != ';') { s++; continue; } // not a &#nnn; sequence
if (c < 0x80) *(s++) = c; // US-ASCII subset
else { // multi-byte UTF-8 sequence
for (b = 0, d = c; d; d /= 2) b++; // number of bits in c
b = (b - 2) / 5; // number of bytes in payload
*(s++) = (0xFF << (7 - b)) | (c >> (6 * b)); // head
while (b) *(s++) = 0x80 | ((c >> (6 * --b)) & 0x3F); // payload
}
}
else { s++; continue; }
memmove(s, strchr(s, ';') + 1, strlen(strchr(s, ';')));
}
}
// called when parser finds start of new tag
void ezxml_open_tag(ezxml_root_t root, char *name, char **attr)
{
ezxml_t xml = root->cur;
if (xml->name) xml = ezxml_add_child(xml, name, strlen(xml->txt));
else xml->name = name;
xml->attr = (const char **)attr;
root->cur = xml;
}
// called when parser finds character content between open and closing tag
void ezxml_char_content(ezxml_root_t root, char *s, size_t len, short decode)
{
ezxml_t xml = root->cur;
size_t l;
if (! xml || ! xml->name || ! len) return;
s[len] = '\0';
ezxml_decode(s, decode);
if (! *(xml->txt)) xml->txt = s;
else { // allocate our own memory and make a copy
l = strlen(xml->txt);
if (! (xml->flags & EZXML_TXTM)) {
xml->txt = strcpy(malloc(l + len + 1), xml->txt);
xml->flags |= EZXML_TXTM;
}
else xml->txt = realloc(xml->txt, l + len + 1);
strcpy(xml->txt + l, s);
}
}
// called when the parser finds an xml processing instruction
void ezxml_proc_inst(ezxml_root_t root, char *s, size_t len)
{
int i = 0, j = 1;
char *target = s;
s[len] = '\0'; // null terminate instruction
*(s += strcspn(s, EZXML_WS)) = '\0'; // null terminate target
s += strspn(s + 1, EZXML_WS) + 1; // skip whitespace after target
if (! root->pi) *(root->pi = malloc(sizeof(char**))) = NULL;
while (root->pi[i] && strcmp(target, root->pi[i][0])) i++;
if (! root->pi[i]) { // new target
root->pi = realloc(root->pi, sizeof(char **) * (i + 2));
root->pi[i] = malloc(sizeof(char *) * 2);
root->pi[i][0] = target;
root->pi[i][1] = (char *)(root->pi[i + 1] = NULL); // terminate pi list
}
while (root->pi[i][j]) j++;
root->pi[i] = realloc(root->pi[i], sizeof(char *) * (j + 2));
root->pi[i][j] = s;
root->pi[i][j + 1] = NULL;
}
// set an error string and return root
ezxml_t ezxml_seterr(ezxml_root_t root, const char *err)
{
root->err = err;
return (ezxml_t)root;
}
// converts a UTF-16 string to UTF-8, returns a new string the must be freed or
// NULL if no conversion was needed
char *ezxml_to_utf8(char **s, size_t *len)
{
char *u;
size_t l = 0, sl, max = *len;
long c, c2;
int b, be = (**s == '\xFE') ? 1 : (**s == '\xFF') ? 0 : -1;
if (be == -1) return NULL; // not UTF-16
u = malloc(max);
for (sl = 2; sl < *len - 1; sl += 2) {
c = (be) ? ((long)(*s)[sl] << 8) | (*s)[sl + 1] : // big-endian
((long)(*s)[sl + 1] << 8) | (*s)[sl]; // little-endian
if (c >= 0xD800 && c <= 0xDFFF && (sl += 2) < *len - 1) { // high-half
c2 = (be) ? ((long)(*s)[sl] << 8) | (*s)[sl + 1] : // big-endian
((long)(*s)[sl + 1] << 8) | (*s)[sl]; // little-endian
c = (((c & 0x3FF) << 10) | (c2 & 0x3FF)) + 0x10000;
}
while (l + 6 > max) u = realloc(u, max += EZXML_BUFSIZE);
if (c < 0x80) u[l++] = c; // US-ASCII subset
else { // multi-byte UTF-8 sequence
for (b = 0, c2 = c; c2; c2 /= 2) b++; // bits in c
b = (b - 2) / 5; // bytes in payload;
u[l++] = (0xFF << (7 - b)) | (c >> (6 * b)); // head
while (b) u[l++] = 0x80 | ((c >> (6 * --b)) & 0x3F); // payload
}
}
return *s = realloc(u, *len = l);
}
// parse the given xml string and return an ezxml structure
ezxml_t ezxml_parse_str(char *s, size_t len)
{
ezxml_root_t root = (ezxml_root_t)ezxml_new(NULL);
char *d, **attr, q, e;
int l;
root->m = s;
if (! len) return ezxml_seterr(root, "root tag missing");
root->u = ezxml_to_utf8(&s, &len);
e = s[len - 1];
s[len - 1] = '\0';
while (*s && *s != '<') s++; // find first tag
if (! *s) return ezxml_seterr(root, "root tag missing");
for (;;) {
attr = (char **)EZXML_NOATTR;
d = ++s;
if (isalpha(*s) || *s == '_' || *s == ':' || *s < '\0') { // new tag
if (! root->cur) return ezxml_seterr(root, "unmatched closing tag");
s += strcspn(s, EZXML_WS "/>");
while (isspace(*s)) *(s++) = '\0';
l = 0;
while (*s && *s != '/' && *s != '>') { // new tag attribute
attr = (! l) ? malloc(3 * sizeof(char *)) :
realloc(attr, (l + 3) * sizeof(char *));
attr[l] = s;
s += strcspn(s, EZXML_WS "=/>");
if (*s == '=' || isspace(*s)) {
*(s++) = '\0';
q = *(s += strspn(s, EZXML_WS "="));
if (q == '"' || q == '\'') { // attribute value
attr[l + 1] = ++s;
while (*s && *s != q) s++;
if (*s) *(s++) = '\0';
else {
free(attr);
return ezxml_seterr(root, (q == '"') ?
"missing \"" : "missing '");
}
ezxml_decode(attr[l + 1], 1);
}
else attr[l + 1] = "";
}
else attr[l + 1] = "";
attr[(l += 2)] = NULL;
while (isspace(*s)) s++;
}
if (*s == '/') { // self closing tag
*(s++) = '\0';
if ((*s && *s != '>') || (! *s && e != '>')) {
if (l) free(attr);
return ezxml_seterr(root, "missing >");
}
ezxml_open_tag(root, d, attr);
ezxml_close_tag(root);
}
else if (*s == '>' || (! *s && e == '>')) { // open tag
q = *s;
*s = '\0';
ezxml_open_tag(root, d, attr);
*s = q;
}
else {
if (l) free(attr);
return ezxml_seterr(root, "missing >");
}
}
else if (*s == '/') { // close tag
if (! root->cur) return ezxml_seterr(root, "unmatched closing tag");
ezxml_close_tag(root);
while (*s && *s != '>') s++;
if (! *s && e != '>') return ezxml_seterr(root, "missing >");
}
else if (! strncmp(s, "!--", 3)) { // comment
do { s = strstr(s, "--"); } while (s && *(s += 2) && *s != '>');
if (! s || (! *s && e != '>'))
return ezxml_seterr(root, "unclosed <!--");
}
else if (! strncmp(s, "![CDATA[", 8)) { // cdata
if ((s = strstr(s, "]]>")))
ezxml_char_content(root, d + 8, (s += 2) - d - 10, 0);
else return ezxml_seterr(root, "unclosed <![CDATA[");
}
else if (! strncmp(s, "!DOCTYPE", 8)) { // skip <!DOCTYPE declarations
for (l = 0; *s && ((! l && *s != '>') || (l && (*s != ']' ||
*(s + strspn(s + 1, EZXML_WS) + 1) != '>')));
l = (*s == '[') ? 1 : l) s += strcspn(s + 1, "[]>") + 1;
if (! *s && e != '>')
return ezxml_seterr(root, "unclosed <!DOCTYPE");
}
else if (*s == '?') { // <?...?> processing instructions
do { s = strchr(s, '?'); } while (s && *(++s) && *s != '>');
if (! s || (! *s && e != '>'))
return ezxml_seterr(root, "unclosed <?");
else ezxml_proc_inst(root, d + 1, s - d - 2);
}
else return ezxml_seterr(root, "unexpected <");
if (! s || ! *s) break;
*s = '\0';
d = ++s;
if (*s && *s != '<') { // tag character content
while (*s && *s != '<') s++;
if (*s) ezxml_char_content(root, d, s - d, 1);
else break;
}
else if (! *s) break;
}
return (root->cur) ? ezxml_seterr(root, (root->cur->name) ? "unclosed tag" :
"root tag missing") : (ezxml_t)root;
}
// Wrapper for ezxml_parse_str() that accepts a file stream. Reads the entire
// stream into memory and then parses it. For xml files, use ezxml_parse_file()
// or ezxml_parse_fd()
ezxml_t ezxml_parse_fp(FILE *fp)
{
ezxml_root_t ret;
size_t l, len = 0;
void *s;
if (! (s = malloc(EZXML_BUFSIZE))) return NULL;
do {
len += (l = fread((s + len), 1, EZXML_BUFSIZE, fp));
if (l == EZXML_BUFSIZE) s = realloc(s, len + EZXML_BUFSIZE);
} while (s && l == EZXML_BUFSIZE);
if (! s) return NULL;
ret = (ezxml_root_t)ezxml_parse_str(s, len);
ret->len = -1; // so we know to free s in ezxml_free()
return (ezxml_t)ret;
}
// A wrapper for ezxml_parse_str() that accepts a file descriptor. First
// attempts to mem map the file. Failing that, reads the file into memory.
// Returns NULL on failure.
ezxml_t ezxml_parse_fd(int fd)
{
ezxml_root_t ret;
struct stat stat;
size_t len, ps = (size_t)sysconf(_SC_PAGESIZE);
void *m;
if (fd < 0) return NULL;
fstat(fd, &stat);
len = (stat.st_size + ps - 1) & ~(ps - 1); // round up to next page boundry
#ifndef EZXML_NOMMAP
if ((m = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)) !=
MAP_FAILED) {
madvise(m, len, MADV_SEQUENTIAL); // optimize for sequential access
ret = (ezxml_root_t)ezxml_parse_str(m, stat.st_size);
madvise(m, ret->len = len, MADV_NORMAL); // put it back to normal
}
else { // mmap failed, read file into memory
#endif // EZXML_NOMMAP
len = read(fd, m = malloc(stat.st_size), stat.st_size);
ret = (ezxml_root_t)ezxml_parse_str(m, len);
ret->len = -1;
#ifndef EZXML_NOMMAP
}
#endif // EZXML_NOMMAP
return (ezxml_t)ret;
}
// a wrapper for ezxml_parse_fd that accepts a file name
ezxml_t ezxml_parse_file(const char *file)
{
int fd = open(file, O_RDONLY, 0);
ezxml_t ret = ezxml_parse_fd(fd);
if (fd >= 0) close(fd);
return ret;
}
// Encodes ampersand sequences appending the results to dst, reallocating dst
// if it's length excedes max. Returns *dst.
char *ezxml_ampencode(const char *s, size_t len, char **dst, size_t *dlen,
size_t *max)
{
const char *e;
for (e = s + len; s != e; s++) {
while (*dlen + 10 > *max) *dst = realloc(*dst, *max += EZXML_BUFSIZE);
switch (*s) {
case '\0': return *dst;
case '&': *dlen += sprintf(*dst + *dlen, "&amp;"); break;
case '<': *dlen += sprintf(*dst + *dlen, "&lt;"); break;
case '>': *dlen += sprintf(*dst + *dlen, "&gt;"); break;
case '"': *dlen += sprintf(*dst + *dlen, "&quot;"); break;
default:
if (*s >= ' ' || *s < '\0' || *s == '\n' || *s == '\t')
(*dst)[(*dlen)++] = *s;
else *dlen += sprintf(*dst + *dlen, "&#%02d;", *s);
}
}
return *dst;
}
// Recursively converts each tag to xml appending it to s. Reallocates s if it's
// length excedes max. start is the location of the previous tag in the parent
// tag's character content. Returns *s.
char *ezxml_toxml_r(ezxml_t xml, char **s, size_t *len, size_t *max,
size_t start)
{
int i;
char *txt = (xml->parent) ? xml->parent->txt : "";
// parent character content up to this tag
ezxml_ampencode(txt + start, xml->off - start, s, len, max);
while (*len + strlen(xml->name) + 4 > *max) // reallocate s
*s = realloc(*s, *max += EZXML_BUFSIZE);
*len += sprintf(*s + *len, "<%s", xml->name); // open tag
for (i = 0; xml->attr[i]; i += 2) { // tag attributes
while (*len + strlen(xml->attr[i]) + 7 > *max) // reallocate s
*s = realloc(*s, *max += EZXML_BUFSIZE);
*len += sprintf(*s + *len, " %s=\"", xml->attr[i]);
ezxml_ampencode(xml->attr[i + 1], -1, s, len, max);
*len += sprintf(*s + *len, "\"");
}
if (xml->child || *(xml->txt)) { // tag content
*len += sprintf(*s + *len, ">");
if (xml->child) ezxml_toxml_r(xml->child, s, len, max, 0);
else ezxml_ampencode(xml->txt, -1, s, len, max); // char content
while (*len + strlen(xml->name) + 4 > *max) // reallocate s
*s = realloc(*s, *max += EZXML_BUFSIZE);
*len += sprintf(*s + *len, "</%s>", xml->name); // close tag
}
else *len += sprintf(*s + *len, "/>"); // self closing tag
if (xml->ordered) return ezxml_toxml_r(xml->ordered, s, len, max, xml->off);
return ezxml_ampencode(txt + xml->off, -1, s, len, max);
}
// converts an ezxml structure back to xml, returning it as a string that must
// be freed
char *ezxml_toxml(ezxml_t xml)
{
ezxml_t p = xml->parent;
size_t len = 0, max = EZXML_BUFSIZE;
char *s = strcpy(malloc(max), "");
if (! xml || ! xml->name) return realloc(s, len + 1);
xml->parent = NULL;
ezxml_toxml_r(xml, &s, &len, &max, 0);
xml->parent = p;
return realloc(s, len + 1);
}
// free the memory allocated for the ezxml structure
void ezxml_free(ezxml_t xml)
{
ezxml_root_t root = (ezxml_root_t)xml;
int i;
if (! xml) return;
ezxml_free(xml->child);
ezxml_free(xml->ordered);
if (! xml->parent) {
if (root->pi) {
for (i = 0; root->pi[i]; i++) free(root->pi[i]);
free(root->pi);
}
if (root->len == -1) free(root->m);
#ifndef EZXML_NOMMAP
else if (root->len) munmap(root->m, root->len);
#endif // EZXML_NOMMAP
if (root->u) free(root->u);
}
if (xml->attr[0]) free(xml->attr);
if ((xml->flags & EZXML_TXTM)) free(xml->txt);
free(xml);
}
// return parser error message or empty string if none
const char *ezxml_error(ezxml_t xml)
{
while (xml->parent) xml = xml->parent;
return ((ezxml_root_t)xml)->err;
}
// returns a new empty ezxml structure with the given root tag name
ezxml_t ezxml_new(const char *name)
{
ezxml_root_t root = (ezxml_root_t)memset(malloc(sizeof(struct ezxml_root)),
'\0', sizeof(struct ezxml_root));
root->xml.name = name;
root->xml.attr = EZXML_NOATTR;
root->cur = (ezxml_t)root;
root->err = root->xml.txt = "";
return (ezxml_t)root;
}
// adds a child tag. offset is the location of the child tag relative to the
// start of the parrent tag's character content. returns the child tag
ezxml_t ezxml_add_child(ezxml_t xml, const char *name, size_t offset)
{
ezxml_t sub, child = (ezxml_t)malloc(sizeof(struct ezxml));
if (xml->child) { // already have sub tags
sub = xml->child;
while (sub->ordered) sub = sub->ordered;
sub->ordered = child;
sub = xml->child;
while (sub->sibling && strcmp(sub->name, name)) sub = sub->sibling;
if (! strcmp(sub->name, name)) { // already have this tag type
while (sub->next) sub = sub->next;
sub->next = child;
}
else sub->sibling = child;
}
else xml->child = child; // first sub tag
// initialize new tag
child->name = name;
child->attr = EZXML_NOATTR;
child->off = offset;
child->parent = xml;
child->next = child->child = child->sibling = child->ordered = NULL;
child->txt = "";
child->flags = 0;
return child;
}
// sets the character content for the given tag
void ezxml_set_txt(ezxml_t xml, const char *txt)
{
if (xml->flags & EZXML_TXTM) { // existing txt was malloced
free(xml->txt);
xml->flags &= ~EZXML_TXTM;
}
xml->txt = (char *)txt;
}
// Sets the given tag attribute or adds a new attribute if not found. A value of
// NULL will remove the specified attribute.
void ezxml_set_attr(ezxml_t xml, const char *name, const char *value)
{
int l = 0, c = 0;
while (xml->attr[l] && strcmp(xml->attr[l], name)) l += 2;
if (! xml->attr[l]) { // not found, add as new attribute
if (! value) return; // nothing to do
if (xml->attr == EZXML_NOATTR) xml->attr = malloc(3 * sizeof(char *));
else xml->attr = realloc(xml->attr, (l + 3) * sizeof(char *));
xml->attr[l] = name;
xml->attr[l + 2] = NULL;
}
if (value) xml->attr[l + 1] = value; // set attribute
else { // remove attribute
while (xml->attr[l + 2 + c]) c += 2; // find end of attribute list
memmove(xml->attr + l, xml->attr + l + 2, (c + 1) * sizeof(char *));
xml->attr = realloc(xml->attr, (l + c + 1) * sizeof(char *));
}
}
// removes a tag along with all its subtags
void ezxml_remove(ezxml_t xml)
{
ezxml_t cur;
if (! xml) return; // nothing to do
if (xml->next) xml->next->sibling = xml->sibling; // patch sibling list
if (xml->parent) { // not root tag
cur = xml->parent->child; // find head of subtag list
if (cur == xml) xml->parent->child = xml->ordered; // first subtag
else { // not first subtag
while (cur->ordered != xml) cur = cur->ordered;
cur->ordered = cur->ordered->ordered; // patch ordered list
cur = xml->parent->child; // go back to head of subtag list
if (strcmp(cur->name, xml->name)) { // not in first sibling list
while(strcmp(cur->sibling->name, xml->name)) cur = cur->sibling;
if (cur->sibling == xml) { // first of a sibling list
if (xml->next) cur->sibling = xml->next;
else cur->sibling = cur->sibling->sibling;
}
else cur = cur->sibling; // not first of a sibling list
}
while (cur->next && cur->next != xml) cur = cur->next;
if (cur->next) cur->next = cur->next->next; // patch next list
}
}
xml->ordered = NULL;
ezxml_free(xml);
}
#ifdef EZXML_TEST // test harness
int main(int argc, char **argv)
{
ezxml_t xml;
char *s;
if (argc != 2) return fprintf(stderr, "usage: %s xmlfile\n", argv[0]);
xml = ezxml_parse_file(argv[1]);
printf("%s", (s = ezxml_toxml(xml)));
fprintf(stderr, "%s", ezxml_error(xml));
free(s);
ezxml_free(xml);
return 0;
}
#endif // EZXML_TEST

133
src/ezxml.h Normal file
View File

@ -0,0 +1,133 @@
/* ezxml.h
*
* Copyright 2005 Aaron Voisine <aaron@voisine.org>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef _EZXML_H
#define _EZXML_H
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <fcntl.h>
#ifdef __cplusplus
extern "C" {
#endif
#define EZXML_BUFSIZE 1024
typedef struct ezxml *ezxml_t;
struct ezxml {
const char *name; // tag name
const char **attr; // tag attributes { name, value, name, value, ... NULL }
char *txt; // tag character content, empty string if none
size_t off; // tag offset in parent tag character content
ezxml_t next; // next tag with same name in this section at this depth
ezxml_t sibling; // next tag with different name in same section and depth
ezxml_t ordered; // next tag, same section and depth, in original order
ezxml_t child; // head of sub tag list, NULL if none
ezxml_t parent; // parent tag, NULL if current tag is root tag
short flags; // additional information, only used internally for now
};
// returns the next tag of the same name in the same section and depth or NULL
// if not found
#define ezxml_next(xml) xml->next
// returns the tag character content or empty string if none
#define ezxml_txt(xml) xml->txt
// Given a string of xml data and its length, parses it and creates an ezxml
// structure. For efficiency, modifies the data by adding null terminators
// and decoding ampersand sequences. If you don't want this, copy the data and
// pass in the copy. Returns NULL on failure.
ezxml_t ezxml_parse_str(char *s, size_t len);
// A wrapper for ezxml_parse_str() that accepts a file descriptor. First
// attempts to mem map the file. Failing that, reads the file into memory.
// Returns NULL on failure.
ezxml_t ezxml_parse_fd(int fd);
// a wrapper for ezxml_parse_fd() that accepts a file name
ezxml_t ezxml_parse_file(const char *file);
// Wrapper for ezxml_parse_str() that accepts a file stream. Reads the entire
// stream into memory and then parses it. For xml files, use ezxml_parse_file()
// or ezxml_parse_fd()
ezxml_t ezxml_parse_fp(FILE *fp);
// returns the first child tag (one level deeper) with the given name or NULL if
// not found
ezxml_t ezxml_child(ezxml_t xml, const char *name);
// Returns the Nth tag with the same name in the same section at the same depth
// or NULL if not found. An index of 0 returns the tag given.
ezxml_t ezxml_idx(ezxml_t xml, int idx);
// returns the value of the requested tag attribute, or NULL if not found
const char *ezxml_attr(ezxml_t xml, const char *attr);
// Traverses the ezxml sturcture to retrive a specific subtag. Takes a variable
// length list of tag names and indexes. The argument list must be terminated
// by either an index of -1 or an empty string tag name. Example:
// title = ezxml_get(library, "shelf", 0, "book", 2, "title", -1);
// This retrieves the title of the 3rd book on the 1st shelf of library.
// Returns NULL if not found.
ezxml_t ezxml_get(ezxml_t xml, ...);
// Converts an ezxml structure back to xml. Returns a string of xml data that
// must be freed.
char *ezxml_toxml(ezxml_t xml);
// returns a NULL terminated array of processing instructions for the given
// target
const char **ezxml_pi(ezxml_t xml, const char *target);
// frees the memory allocated for an ezxml structure
void ezxml_free(ezxml_t xml);
// returns parser error message or empty string if none
const char *ezxml_error(ezxml_t xml);
// returns a new empty ezxml structure with the given root tag name
ezxml_t ezxml_new(const char *name);
// Adds a child tag. offset is the location of the child tag relative to the
// start of the parrent tag's character content. Returns the child tag.
ezxml_t ezxml_add_child(ezxml_t xml, const char *name, size_t offset);
// sets the character content for the given tag
void ezxml_set_txt(ezxml_t xml, const char *txt);
// Sets the given tag attribute or adds a new attribute if not found. A value of
// NULL will remove the specified attribute.
void ezxml_set_attr(ezxml_t xml, const char *name, const char *value);
// removes a tag along with all its subtags
void ezxml_remove(ezxml_t xml);
#ifdef __cplusplus
}
#endif
#endif // _EZXML_H

View File

@ -314,6 +314,11 @@ extern int scan_get_flacinfo(char *filename, MP3FILE *pmp3);
/** \see wma.c */
int scan_get_wmainfo(char *filename, MP3FILE *pmp3);
/* playlist scanners */
/** \see scan-xml.c */
int scan_xml_playlist(char *filename);
/* For known types, I'm gong to use the "official" apple
* daap.songformat, daap.songdescription, and daap.songcodecsubtype.
@ -393,10 +398,24 @@ void scan_add_playlistlist(char *path) {
*/
void scan_process_playlistlist(void) {
PLAYLISTLIST *pnext;
char *ext;
while(scan_playlistlist.next) {
pnext=scan_playlistlist.next;
scan_static_playlist(pnext->path);
ext=pnext->path;
if(strrchr(pnext->path,'.')) {
ext = strrchr(pnext->path,'.');
}
if(strcasecmp(ext,".xml") == 0) {
scan_xml_playlist(pnext->path);
} else if(strcasecmp(ext,".m3u") == 0) {
scan_static_playlist(pnext->path);
} else {
DPRINTF(E_LOG,L_SCAN,"Unknown playlist type: %s\n",ext);
}
free(pnext->path);
scan_playlistlist.next=pnext->next;
free(pnext);
@ -518,6 +537,8 @@ int scan_path(char *path) {
config.process_m3u){
/* we found an m3u file */
scan_add_playlistlist(mp3_path);
} else if((strcasecmp(pde->d_name,"iTunes Music Library.xml")==0)) {
scan_add_playlistlist(mp3_path);
} else if (((ext = strrchr(pde->d_name, '.')) != NULL) &&
(strcasestr(config.extensions, ext))) {
/* only scan if it's been changed, or empty db */

306
src/scan-xml.c Normal file
View File

@ -0,0 +1,306 @@
/*
* $Id$
* Implementation file iTunes metainfo scanning
*
* Copyright (C) 2005 Ron Pedde (ron@pedde.com)
*
* 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
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "db-generic.h"
#include "err.h"
#include "ezxml.h"
#include "mp3-scanner.h"
/* Forwards */
int scan_xml_playlist(char *filename);
/* Globals */
static char *scan_xml_itunes_version = NULL;
static char *scan_xml_itunes_base_path = NULL;
static char *scan_xml_real_base_path = NULL;
#define MAYBECOPY(a) if(!mp3.a) mp3.a = pmp3->a
/**
* urldecode a string, returning a string pointer which must
* be freed by the calling function or NULL on error (ENOMEM)
*
* \param string string to convert
* \param space as plus whether to convert '+' chars to spaces (no, for iTunes)
*/
char *scan_xml_urldecode(char *string, int space_as_plus) {
char *pnew;
char *src,*dst;
int val=0;
pnew=(char*)malloc(strlen(string)+1);
if(!pnew)
return NULL;
src=string;
dst=pnew;
while(*src) {
switch(*src) {
case '+':
if(space_as_plus) {
*dst++=' ';
} else {
*dst++=*src;
}
src++;
break;
case '%':
/* this is hideous */
src++;
if(*src) {
if((*src <= '9') && (*src >='0'))
val=(*src - '0');
else if((tolower(*src) <= 'f')&&(tolower(*src) >= 'a'))
val=10+(tolower(*src) - 'a');
src++;
}
if(*src) {
val *= 16;
if((*src <= '9') && (*src >='0'))
val+=(*src - '0');
else if((tolower(*src) <= 'f')&&(tolower(*src) >= 'a'))
val+=(10+(tolower(*src) - 'a'));
src++;
}
*dst++=val;
break;
default:
*dst++=*src++;
break;
}
}
*dst='\0';
return pnew;
}
/**
* give an ezxml_t node for the Tracks dictionary, walk though
* and update metainfo
*
* \param tracksdict ezxml node for the tracks dictionary
*/
int scan_xml_process_tracks(ezxml_t tracksdict) {
char *base_path;
char *song_path=NULL;
char physical_path[PATH_MAX];
char real_path[PATH_MAX];
ezxml_t track,songdict,songkey,value;
MP3FILE mp3;
MP3FILE *pmp3;
/* urldecode the base_path */
base_path=scan_xml_urldecode(scan_xml_itunes_base_path,0);
for(track=ezxml_child(tracksdict,"key"); track; track=track->next) {
memset((void*)&mp3,0x00,sizeof(mp3));
if(song_path)
free(song_path);
song_path = NULL;
songdict=track->ordered;
DPRINTF(E_DBG,L_SCAN,"Found track %s\n",track->txt);
for(songkey=ezxml_child(songdict,"key"); songkey; songkey=songkey->next) {
/* walking through the song elements, hanging the data on the mp3 file */
value=songkey->ordered;
if(!strcasecmp(songkey->txt,"Name")) {
mp3.title = value->txt;
} else if(!strcasecmp(songkey->txt,"Artist")) {
mp3.artist = value->txt;
} else if(!strcasecmp(songkey->txt,"Album")) {
mp3.album = value->txt;
} else if(!strcasecmp(songkey->txt,"Genre")) {
mp3.genre = value->txt;
} else if(!strcasecmp(songkey->txt,"Total Time")) {
mp3.song_length = atoi(value->txt);
} else if(!strcasecmp(songkey->txt,"Track Number")) {
mp3.track = atoi(value->txt);
} else if(!strcasecmp(songkey->txt,"Track Count")) {
mp3.total_tracks = atoi(value->txt);
} else if(!strcasecmp(songkey->txt,"Year")) {
mp3.year = atoi(value->txt);
} else if(!strcasecmp(songkey->txt,"Bit Rate")) {
mp3.bitrate = atoi(value->txt);
} else if(!strcasecmp(songkey->txt,"Sample Rate")) {
mp3.samplerate = atoi(value->txt);
} else if(!strcasecmp(songkey->txt,"Play Count")) {
mp3.play_count = atoi(value->txt);
} else if(!strcasecmp(songkey->txt,"Rating")) {
mp3.rating = atoi(value->txt);
} else if(!strcasecmp(songkey->txt,"Disabled")) {
mp3.disabled=1;
} else if(!strcasecmp(songkey->txt,"Disc Number")) {
mp3.disc = atoi(value->txt);
} else if(!strcasecmp(songkey->txt,"Disc Count")) {
mp3.total_discs = atoi(value->txt);
} else if(!strcasecmp(songkey->txt,"Compilation")) {
mp3.compilation=1;
} else if(!strcasecmp(songkey->txt,"Location")) {
song_path = scan_xml_urldecode(value->txt,0);
}
}
DPRINTF(E_DBG,L_SCAN,"Found track at path %s\n",song_path);
/* song is parsed, now see if we can find a corresponding song */
if(song_path && (strlen(song_path) > strlen(base_path))) {
sprintf(physical_path,"%siTunes Music/%s",scan_xml_real_base_path,
(char*)&song_path[strlen(base_path)]);
realpath(physical_path,real_path);
DPRINTF(E_DBG,L_SCAN,"Real Path: %s\n", real_path);
/* now we have the real path -- is it in the database? */
pmp3=db_fetch_path(real_path);
if(pmp3) {
/* yup... let's update it with the iTunes info */
mp3.path = pmp3->path;
mp3.fname = pmp3->fname;
MAYBECOPY(title);
MAYBECOPY(artist);
MAYBECOPY(album);
MAYBECOPY(genre);
MAYBECOPY(comment);
MAYBECOPY(type);
MAYBECOPY(composer);
MAYBECOPY(orchestra);
MAYBECOPY(conductor);
MAYBECOPY(grouping);
MAYBECOPY(url);
MAYBECOPY(bitrate);
MAYBECOPY(samplerate);
MAYBECOPY(song_length);
MAYBECOPY(file_size);
MAYBECOPY(year);
MAYBECOPY(track);
MAYBECOPY(total_tracks);
MAYBECOPY(disc);
MAYBECOPY(total_discs);
MAYBECOPY(time_added);
MAYBECOPY(time_modified);
MAYBECOPY(time_played);
MAYBECOPY(play_count);
MAYBECOPY(rating);
MAYBECOPY(db_timestamp);
MAYBECOPY(disabled);
MAYBECOPY(bpm);
MAYBECOPY(id);
MAYBECOPY(description);
MAYBECOPY(codectype);
MAYBECOPY(item_kind);
MAYBECOPY(data_kind);
MAYBECOPY(force_update);
MAYBECOPY(sample_count);
MAYBECOPY(compilation);
db_add(&mp3);
db_dispose_item(pmp3);
}
}
}
free(base_path);
if(song_path)
free(song_path);
return 0;
}
/**
* scan an iTunes xml music database file, augmenting
* the metainfo with that found in the xml file
*/
int scan_xml_playlist(char *filename) {
ezxml_t itpl,maindict,key,value;
char *working_base;
if(scan_xml_itunes_version) {
free(scan_xml_itunes_version);
scan_xml_itunes_version = NULL;
}
if(scan_xml_itunes_base_path) {
free(scan_xml_itunes_base_path);
scan_xml_itunes_base_path = NULL;
}
if(scan_xml_real_base_path) {
free(scan_xml_real_base_path);
scan_xml_real_base_path = NULL;
}
/* find the base dir of the itunes playlist itself */
working_base = strdup(filename);
if(strrchr(working_base,'/')) {
*(strrchr(working_base,'/') + 1) = '\x0';
scan_xml_real_base_path = strdup(working_base);
} else {
scan_xml_real_base_path = strdup("/");
}
free(working_base);
DPRINTF(E_SPAM,L_SCAN,"Parsing xml file: %s\n",filename);
itpl = ezxml_parse_file(filename);
if(itpl == NULL) {
return -1;
}
DPRINTF(E_SPAM,L_SCAN,"File parsed... processing\n");
maindict = ezxml_child(itpl,"dict");
/* we are parsing a dict entry -- this will be a seriese of key/value pairs */
for(key = ezxml_child(maindict,"key"); key; key=key->next) {
DPRINTF(E_SPAM,L_SCAN,"Found key %s\n",key->txt);
value = key->ordered;
if(!value) /* badly formed xml file */
return -1;
if(!scan_xml_itunes_version && (strcasecmp(key->txt,"Application Version") == 0)) {
scan_xml_itunes_version=strdup(value->txt);
DPRINTF(E_DBG,L_SCAN,"iTunes Version %s\n",scan_xml_itunes_version);
} else if (!scan_xml_itunes_base_path && (strcasecmp(key->txt,"Music Folder") == 0)) {
scan_xml_itunes_base_path=strdup(value->txt);
DPRINTF(E_DBG,L_SCAN,"iTunes base path: %s\n",scan_xml_itunes_base_path);
} else if (strcasecmp(key->txt,"Tracks") == 0) {
scan_xml_process_tracks(value);
} else if (strcasecmp(key->txt,"Playlists") == 0) {
DPRINTF(E_DBG,L_SCAN,"Skipping iTunes playlists.... for now\n");
}
}
return 0;
}

View File

@ -19,6 +19,11 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@ -33,6 +38,7 @@ extern int scan_get_wmainfo(char *filename, MP3FILE *pmp3);
extern int scan_get_ogginfo(char *filename, MP3FILE *pmp3);
extern int scan_get_flacinfo(char *filename, MP3FILE *pmp3);
/*
* Typedefs
*/
@ -41,7 +47,6 @@ typedef struct scannerlist_tag {
int (*scanner)(char *file, MP3FILE *pmp3);
} SCANNERLIST;
/*
* Globals
*/
@ -53,8 +58,13 @@ SCANNERLIST scanner_list[] = {
{ NULL, NULL }
};
char *av0;
/**
* dump a mp3 file
*
* \param pmp3 mp3 file to dump
*/
void dump_mp3(MP3FILE *pmp3) {
int min,sec;
@ -87,6 +97,11 @@ void dump_mp3(MP3FILE *pmp3) {
printf("compilation...: %d\n",pmp3->compilation);
}
/*
* dump suage
*/
void usage(int errorcode) {
fprintf(stderr,"Usage: %s [options] input-file\n\n",av0);
fprintf(stderr,"options:\n\n");
@ -138,11 +153,12 @@ int main(int argc, char *argv[]) {
plist++;
}
if(!plist->ext) {
fprintf(stderr,"Cannot find dispatch for file of type %s\n",ext);
if(plist->ext) {
fprintf(stderr,"dispatching as single-file metatag parser\n");
plist->scanner(argv[0],&mp3);
dump_mp3(&mp3);
} else {
fprintf(stderr,"unknown file extension: %s\n",ext);
exit(-1);
}
plist->scanner(argv[0],&mp3);
dump_mp3(&mp3);
}

View File

@ -1,7 +1,9 @@
CC=gcc
CFLAGS := $(CFLAGS) -g
LDFLAGS := $(LDFLAGS) -logg -lvorbisfile -lFLAC
CFLAGS := $(CFLAGS) -g -I/sw/include -DHAVE_CONFIG_H -I. -I..
LDFLAGS := $(LDFLAGS) -L/sw/lib -logg -lvorbisfile -lFLAC -lvorbis
scanner: scanner-driver.o restart.o wma.o err.o flac.o ogg.o
$(CC) -o scanner $(LDFLAGS) scanner-driver.o restart.o wma.o err.o flac.o ogg.o
OBJECTS=scanner-driver.o restart.o wma.o err.o flac.o ogg.o
scanner: $(OBJECTS)
$(CC) -o scanner $(LDFLAGS) $(OBJECTS)

View File

@ -1,6 +1,8 @@
#ifndef _STRCASESTR_H_
#define _STRCASESTR_H_
#ifndef HAVE_STRCASESTR
extern char * strcasestr(char* haystack, char* needle);
#endif
#endif