1/*
2 * Copyright (C) 2009-2010 Julien BLACHE <jb@jblache.org>
3 *
4 * Rewritten from mt-daapd code:
5 * Copyright (C) 2003 Ron Pedde (ron@pedde.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 */
21
22#ifdef HAVE_CONFIG_H
23# include <config.h>
24#endif
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <ctype.h>
30#include <errno.h>
31
32#include "logger.h"
33#include "db.h"
34#include "misc.h"
35#include "filescanner.h"
36
37
38int
39scan_url_file(char *file, struct media_file_info *mfi)
40{
41  FILE *fp;
42  char *head;
43  char *tail;
44  char buf[256];
45  size_t len;
46  int ret;
47
48  DPRINTF(E_DBG, L_SCAN, "Getting URL file info\n");
49
50  fp = fopen(file, "r");
51  if (!fp)
52    {
53      DPRINTF(E_WARN, L_SCAN, "Could not open '%s' for reading: %s\n", file, strerror(errno));
54
55      return -1;
56    }
57
58  head = fgets(buf, sizeof(buf), fp);
59  fclose(fp);
60
61  if (!head)
62    {
63      DPRINTF(E_WARN, L_SCAN, "Error reading from file '%s': %s", file, strerror(errno));
64
65      return -1;
66    }
67
68  len = strlen(buf);
69
70  if (buf[len - 1] != '\n')
71    {
72      DPRINTF(E_WARN, L_SCAN, "URL info in file '%s' too large for buffer\n", file);
73
74      return -1;
75    }
76
77  while (isspace(buf[len - 1]))
78    {
79      len--;
80      buf[len] = '\0';
81    }
82
83  tail = strchr(head, ',');
84  if (!tail)
85    {
86      DPRINTF(E_LOG, L_SCAN, "Badly formatted .url file; expected format is bitrate,descr,url\n");
87
88      return -1;
89    }
90
91  head = tail + 1;
92  tail = strchr(head, ',');
93  if (!tail)
94    {
95      DPRINTF(E_LOG, L_SCAN, "Badly formatted .url file; expected format is bitrate,descr,url\n");
96
97      return -1;
98    }
99  *tail = '\0';
100
101  mfi->title = strdup(head);
102  mfi->url = strdup(tail + 1);
103
104  ret = safe_atou32(buf, &mfi->bitrate);
105  if (ret < 0)
106    {
107      DPRINTF(E_WARN, L_SCAN, "Could not read bitrate\n");
108
109      return -1;
110    }
111
112  DPRINTF(E_DBG, L_SCAN,"  Title:    %s\n", mfi->title);
113  DPRINTF(E_DBG, L_SCAN,"  Bitrate:  %d\n", mfi->bitrate);
114  DPRINTF(E_DBG, L_SCAN,"  URL:      %s\n", mfi->url);
115
116  mfi->type = strdup("pls");
117  /* codectype = NULL */
118  mfi->description = strdup("Playlist URL");
119
120  return 0;
121}
122