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, see <http://www.gnu.org/licenses/>. 21 */ 22 23#include <stdio.h> 24#include <string.h> 25#include <ctype.h> 26 27#include "tagutils.h" 28#include "log.h" 29 30 31#define MAX_BUF 4096 32 33static FILE *fp = 0; 34static int _utf8bom = 0; 35static int _trackno; 36 37static int (*_next_track)(struct song_metadata*, struct stat*, char*, char*); 38static int _m3u_pls_next_track(struct song_metadata*, struct stat*, char*, char*); 39 40int 41start_plist(const char *path, struct song_metadata *psong, struct stat *stat, char *lang, char *type) 42{ 43 char *fname, *suffix; 44 45 _next_track = 0; 46 _utf8bom = 0; 47 _trackno = 0; 48 49 if(strcasecmp(type, "m3u") == 0) 50 _next_track = _m3u_pls_next_track; 51 else if(strcasecmp(type, "pls") == 0) 52 _next_track = _m3u_pls_next_track; 53 54 if(!_next_track) 55 { 56 DPRINTF(E_ERROR, L_SCANNER, "Unsupported playlist type <%s> (%s)\n", type, path); 57 return -1; 58 } 59 60 if(!(fp = fopen(path, "rb"))) 61 { 62 DPRINTF(E_ERROR, L_SCANNER, "Cannot open %s\n", path); 63 return -1; 64 } 65 66 if(!psong) 67 return 0; 68 69 memset((void*)psong, 0, sizeof(struct song_metadata)); 70 psong->is_plist = 1; 71 psong->path = strdup(path); 72 psong->type = type; 73 74 fname = strrchr(psong->path, '/'); 75 psong->basename = fname ? fname + 1 : psong->path; 76 77 psong->title = strdup(psong->basename); 78 suffix = strrchr(psong->title, '.'); 79 if(suffix) *suffix = '\0'; 80 81 if(stat) 82 { 83 if(!psong->time_modified) 84 psong->time_modified = stat->st_mtime; 85 psong->file_size = stat->st_size; 86 } 87 88 return 0; 89} 90 91int 92_m3u_pls_next_track(struct song_metadata *psong, struct stat *stat, char *lang, char *type) 93{ 94 char buf[MAX_BUF], *p; 95 int len; 96 97 memset((void*)psong, 0, sizeof(struct song_metadata)); 98 99 // read first line 100 p = fgets(buf, MAX_BUF, fp); 101 if(!p) 102 { 103 fclose(fp); 104 return 1; 105 } 106 107 if(strcasecmp(type, "m3u") == 0) 108 { 109 // check BOM 110 if(!_utf8bom && p[0] == '\xef' && p[1] == '\xbb' && p[2] == '\xbf') 111 { 112 _utf8bom = 1; 113 p += 3; 114 } 115 } 116 117 while(p) 118 { 119 while(isspace(*p)) p++; 120 121 if(!(*p) || *p == '#') 122 goto next_line; 123 124 if(!isprint(*p)) 125 { 126 DPRINTF(E_ERROR, L_SCANNER, "Playlist looks bad (unprintable characters)\n"); 127 fclose(fp); 128 return 2; 129 } 130 131 if(strcasecmp(type, "pls") == 0) 132 { 133 // verify that it's a valid pls playlist 134 if(!_trackno) 135 { 136 if(strncmp(p, "[playlist]", 10)) 137 break; 138 _trackno++; 139 goto next_line; 140 } 141 142 if(strncmp(p, "File", 4) != 0) 143 goto next_line; 144 145 psong->track = strtol(p+4, &p, 10); 146 if(!psong->track || !p++) 147 goto next_line; 148 _trackno = psong->track; 149 } 150 else if(strcasecmp(type, "m3u") == 0) 151 psong->track = ++_trackno; 152 153 len = strlen(p); 154 while(p[len-1] == '\r' || p[len-1] == '\n') 155 p[--len] = '\0'; 156 psong->path = strdup(p); 157 return 0; 158next_line: 159 p = fgets(buf, MAX_BUF, fp); 160 } 161 162 fclose(fp); 163 return 1; 164} 165 166int 167next_plist_track(struct song_metadata *psong, struct stat *stat, char *lang, char *type) 168{ 169 if(_next_track) 170 return _next_track(psong, stat, lang, type); 171 return -1; 172} 173