Rewrite .pls/.url file parser

Get rid of the I/O layer, ameliorate bitrate parsing, restructure code.
This commit is contained in:
Julien BLACHE 2009-04-20 15:14:40 +02:00
parent 1eaa3979e6
commit 0a61b5a51d

View File

@ -1,4 +1,7 @@
/* /*
* Copyright (C) 2009 Julien BLACHE <jb@jblache.org>
*
* Rewritten from mt-daapd code:
* Copyright (C) 2003 Ron Pedde (ron@pedde.com) * Copyright (C) 2003 Ron Pedde (ron@pedde.com)
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -17,111 +20,127 @@
*/ */
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
# include "config.h" # include <config.h>
#endif #endif
#include <fcntl.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h>
#include <limits.h>
#include <errno.h>
#include "daapd.h" #include "daapd.h"
#include "err.h" #include "err.h"
#include "mp3-scanner.h" #include "ff-dbstruct.h"
/**
* Get info from a "url" file -- a media stream file.
* This should really get more metainfo, but I'll leave that
* to later.
*
* @param filename .url file to process
* @param pmp3 MP3FILE structure that must be filled
* @returns TRUE if file should be added to db, FALSE otherwise
*/
int scan_get_urlinfo(char *filename, MP3FILE *pmp3) {
IOHANDLE hfile;
char *head, *tail;
char linebuffer[256];
uint32_t len;
char *urltemp;
int ret;
DPRINTF(E_DBG,L_SCAN,"Getting URL file info\n"); int
scan_get_urlinfo(char *file, struct media_file_info *mfi)
{
FILE *fp;
char *head;
char *tail;
char buf[256];
size_t len;
long intval;
if(!(hfile = io_new())) { DPRINTF(E_DBG, L_SCAN, "Getting URL file info\n");
DPRINTF(E_LOG,L_SCAN,"Can't create file handle\n");
return FALSE; fp = fopen(file, "r");
if (!fp)
{
DPRINTF(E_WARN, L_SCAN, "Could not open '%s' for reading: %s\n", file, strerror(errno));
return FALSE;
} }
urltemp = io_urlencode(filename); head = fgets(buf, sizeof(buf), fp);
if (!urltemp) fclose(fp);
{
DPRINTF(E_WARN,L_SCAN,"Could not open %s for reading: out of memory\n",filename);
io_dispose(hfile);
return FALSE;
}
ret = io_open(hfile,"file://%s?ascii=1",filename); if (!head)
free(urltemp); {
if (!ret) { DPRINTF(E_WARN, L_SCAN, "Error reading from file '%s': %s", file, strerror(errno));
DPRINTF(E_WARN,L_SCAN,"Could not open %s for reading: %s\n",filename,
io_errstr(hfile)); return FALSE;
io_dispose(hfile);
return FALSE;
} }
io_buffer(hfile); len = strlen(buf);
len = sizeof(linebuffer);
if(!io_readline(hfile,(unsigned char *)linebuffer,&len)) { if (buf[len - 1] != '\n')
DPRINTF(E_WARN,L_SCAN,"Error reading from file %s: %s",filename,io_errstr(hfile)); {
io_close(hfile); DPRINTF(E_WARN, L_SCAN, "URL info in file '%s' too large for buffer\n", file);
io_dispose(hfile);
return FALSE; return FALSE;
} }
while((linebuffer[strlen(linebuffer)-1] == '\n') || while (isspace(buf[len - 1]))
(linebuffer[strlen(linebuffer)-1] == '\r')) { {
linebuffer[strlen(linebuffer)-1] = '\0'; len--;
buf[len] = '\0';
} }
head=linebuffer; tail = strchr(head, ',');
tail=strchr(head,','); if (!tail)
if(!tail) { {
DPRINTF(E_LOG,L_SCAN,"Badly formatted .url file - must be bitrate,descr,url\n"); DPRINTF(E_LOG, L_SCAN, "Badly formatted .url file; expected format is bitrate,descr,url\n");
io_close(hfile);
io_dispose(hfile); return FALSE;
return FALSE;
} }
pmp3->bitrate=atoi(head); head = tail + 1;
head=++tail; tail = strchr(head, ',');
tail=strchr(head,','); if (!tail)
if(!tail) { {
DPRINTF(E_LOG,L_SCAN,"Badly formatted .url file - must be bitrate,descr,url\n"); DPRINTF(E_LOG, L_SCAN, "Badly formatted .url file; expected format is bitrate,descr,url\n");
io_close(hfile);
io_dispose(hfile); return FALSE;
return FALSE; }
*tail = '\0';
mfi->title = strdup(head);
mfi->url = strdup(tail + 1);
errno = 0;
intval = strtol(buf, &tail, 10);
if (((errno == ERANGE) && ((intval == LONG_MAX) || (intval == LONG_MIN)))
|| ((errno != 0) && (intval == 0)))
{
DPRINTF(E_WARN, L_SCAN, "Could not read bitrate: %s\n", strerror(errno));
free(mfi->title);
free(mfi->url);
return FALSE;
} }
*tail++='\0'; if (tail == buf)
{
DPRINTF(E_WARN, L_SCAN, "No bitrate found\n");
pmp3->title=strdup(head); free(mfi->title);
pmp3->url=strdup(tail); free(mfi->url);
return FALSE;
}
io_close(hfile); if (intval > INT_MAX)
io_dispose(hfile); {
DPRINTF(E_WARN, L_SCAN, "Bitrate too large\n");
DPRINTF(E_DBG,L_SCAN," Title: %s\n",pmp3->title); free(mfi->title);
DPRINTF(E_DBG,L_SCAN," Bitrate: %d\n",pmp3->bitrate); free(mfi->url);
DPRINTF(E_DBG,L_SCAN," URL: %s\n",pmp3->url); return FALSE;
}
pmp3->type = strdup("pls"); mfi->bitrate = (int)intval;
/* codectype = NULL */
pmp3->description = strdup("Playlist URL");
return TRUE; DPRINTF(E_DBG, L_SCAN," Title: %s\n", mfi->title);
DPRINTF(E_DBG, L_SCAN," Bitrate: %d\n", mfi->bitrate);
DPRINTF(E_DBG, L_SCAN," URL: %s\n", mfi->url);
mfi->type = strdup("pls");
/* codectype = NULL */
mfi->description = strdup("Playlist URL");
return TRUE;
} }