1//========================================================================= 2// FILENAME : playlist.c 3// DESCRIPTION : Playlist 4//========================================================================= 5// Copyright (c) 2008- NETGEAR, Inc. All Rights Reserved. 6//========================================================================= 7 8/* 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 */ 23 24#include <stdio.h> 25#include <string.h> 26#include <ctype.h> 27 28#include "misc.h" 29#include "tagutils.h" 30#include "textutils.h" 31#include "log.h" 32 33 34#define MAX_BUF 4096 35 36static FILE *fp = 0; 37static int _utf8bom = 0; 38static int _trackno; 39 40static int (*_next_track)(struct song_metadata*, struct stat*, char*, char*); 41static int _m3u_next_track(struct song_metadata*, struct stat*, char*, char*); 42static int _pls_next_track(struct song_metadata*, struct stat*, char*, char*); 43 44int 45start_plist(const char *path, struct song_metadata *psong, struct stat *stat, char *lang, char *type) 46{ 47 char *fname, *suffix; 48 49 _next_track = 0; 50 _utf8bom = 0; 51 _trackno = 0; 52 53 if(strcmp(type, "m3u") == 0) 54 _next_track = _m3u_next_track; 55 else if(strcmp(type, "pls") == 0) 56 _next_track = _pls_next_track; 57 58 if(!_next_track) 59 { 60 DPRINTF(E_ERROR, L_SCANNER, "Unsupported playlist type <%s>\n", type); 61 return -1; 62 } 63 64 if(!(fp = fopen(path, "rb"))) 65 { 66 DPRINTF(E_ERROR, L_SCANNER, "Cannot open %s\n", path); 67 return -1; 68 } 69 70 if(!psong) 71 return 0; 72 73 memset((void*)psong, 0, sizeof(struct song_metadata)); 74 psong->is_plist = 1; 75 psong->path = strdup(path); 76 psong->type = type; 77 78 fname = strrchr(psong->path, '/'); 79 psong->basename = fname ? fname + 1 : psong->path; 80 81 psong->title = strdup(psong->basename); 82 suffix = strrchr(psong->title, '.'); 83 if(suffix) *suffix = '\0'; 84 85 if(stat) 86 { 87 if(!psong->time_modified) 88 psong->time_modified = stat->st_mtime; 89 psong->file_size = stat->st_size; 90 } 91 92 return 0; 93} 94 95int 96_m3u_next_track(struct song_metadata *psong, struct stat *stat, char *lang, char *type) 97{ 98 char buf[MAX_BUF], *p; 99 int len; 100 101 // 102 memset((void*)psong, 0, sizeof(struct song_metadata)); 103 104 // read first line, check BOM 105 p = fgets(buf, MAX_BUF, fp); 106 if(!p) 107 { 108 fclose(fp); 109 return 1; 110 } 111 112 if(!_utf8bom && p[0] == '\xef' && p[1] == '\xbb' && p[2] == '\xbf') 113 { 114 _utf8bom = 1; 115 p += 3; 116 } 117 118 while(p) 119 { 120 while(isspace(*p)) p++; 121 if(*p && *p != '#') 122 { 123 // check dos format 124 len = strlen((char*)p); 125 126 while(p[len - 1] == '\r' || p[len - 1] == '\n') 127 { 128 p[--len] = '\0'; 129 } 130 psong->path = strdup(p); 131 psong->track = ++_trackno; 132 return 0; 133 } 134 p = fgets(buf, MAX_BUF, fp); 135 continue; 136 } 137 138 fclose(fp); 139 return 1; 140} 141 142int 143_pls_next_track(struct song_metadata *psong, struct stat *stat, char *lang, char *type) 144{ 145 char buf[MAX_BUF], *p; 146 int len; 147 148 memset((void*)psong, 0, sizeof(struct song_metadata)); 149 150 // read first line 151 p = fgets(buf, MAX_BUF, fp); 152 153 while(p) 154 { 155 while(isspace(*p)) p++; 156 if(*p && *p != '#') 157 { 158 // verify that it's a valid pls playlist 159 if(!_trackno) 160 { 161 if(strncmp(p, "[playlist]", 10)) 162 break; 163 _trackno++; 164 goto next_line; 165 } 166 167 if(strncmp(p, "File", 4)) 168 goto next_line; 169 170 psong->track = strtol(p+4, &p, 10); 171 if(!psong->track || !p) 172 goto next_line; 173 _trackno = psong->track; 174 // check dos format 175 len = strlen(++p); 176 177 while(p[len - 1] == '\r' || p[len - 1] == '\n') 178 { 179 p[--len] = '\0'; 180 } 181 psong->path = strdup(p); 182 return 0; 183 } 184next_line: 185 p = fgets(buf, MAX_BUF, fp); 186 } 187 188 fclose(fp); 189 return 1; 190} 191 192int 193next_plist_track(struct song_metadata *psong, struct stat *stat, char *lang, char *type) 194{ 195 if(_next_track) 196 return _next_track(psong, stat, lang, type); 197 return -1; 198} 199