Wrap up version 1.3.3.
[minidlna.git] / tagutils / tagutils-plist.c
blob2253e2d43a5102e4823afc3fe6ee857a105d1596
1 //=========================================================================
2 // FILENAME : playlist.c
3 // DESCRIPTION : Playlist
4 //=========================================================================
5 // Copyright (c) 2008- NETGEAR, Inc. All Rights Reserved.
6 //=========================================================================
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.
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.
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/>.
23 #include <stdio.h>
24 #include <string.h>
25 #include <ctype.h>
27 #include "tagutils.h"
28 #include "log.h"
31 #define MAX_BUF 4096
33 static FILE *fp = 0;
34 static int _utf8bom = 0;
35 static int _trackno;
37 static int (*_next_track)(struct song_metadata*, struct stat*, char*, char*);
38 static int _m3u_pls_next_track(struct song_metadata*, struct stat*, char*, char*);
40 int
41 start_plist(const char *path, struct song_metadata *psong, struct stat *stat, char *lang, char *type)
43 char *fname, *suffix;
45 _next_track = 0;
46 _utf8bom = 0;
47 _trackno = 0;
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;
54 if(!_next_track)
56 DPRINTF(E_ERROR, L_SCANNER, "Unsupported playlist type <%s> (%s)\n", type, path);
57 return -1;
60 if(!(fp = fopen(path, "rb")))
62 DPRINTF(E_ERROR, L_SCANNER, "Cannot open %s\n", path);
63 return -1;
66 if(!psong)
67 return 0;
69 memset((void*)psong, 0, sizeof(struct song_metadata));
70 psong->is_plist = 1;
71 psong->path = strdup(path);
72 psong->type = type;
74 fname = strrchr(psong->path, '/');
75 psong->basename = fname ? fname + 1 : psong->path;
77 psong->title = strdup(psong->basename);
78 suffix = strrchr(psong->title, '.');
79 if(suffix) *suffix = '\0';
81 if(stat)
83 if(!psong->time_modified)
84 psong->time_modified = stat->st_mtime;
85 psong->file_size = stat->st_size;
88 return 0;
91 int
92 _m3u_pls_next_track(struct song_metadata *psong, struct stat *stat, char *lang, char *type)
94 char buf[MAX_BUF], *p;
95 int len;
97 memset((void*)psong, 0, sizeof(struct song_metadata));
99 // read first line
100 p = fgets(buf, MAX_BUF, fp);
101 if(!p)
103 fclose(fp);
104 return 1;
107 if(strcasecmp(type, "m3u") == 0)
109 // check BOM
110 if(!_utf8bom && p[0] == '\xef' && p[1] == '\xbb' && p[2] == '\xbf')
112 _utf8bom = 1;
113 p += 3;
117 while(p)
119 while(isspace(*p)) p++;
121 if(!(*p) || *p == '#')
122 goto next_line;
124 if(!isprint(*p))
126 DPRINTF(E_ERROR, L_SCANNER, "Playlist looks bad (unprintable characters)\n");
127 fclose(fp);
128 return 2;
131 if(strcasecmp(type, "pls") == 0)
133 // verify that it's a valid pls playlist
134 if(!_trackno)
136 if(strncmp(p, "[playlist]", 10))
137 break;
138 _trackno++;
139 goto next_line;
142 if(strncmp(p, "File", 4) != 0)
143 goto next_line;
145 psong->track = strtol(p+4, &p, 10);
146 if(!psong->track || !p++)
147 goto next_line;
148 _trackno = psong->track;
150 else if(strcasecmp(type, "m3u") == 0)
151 psong->track = ++_trackno;
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;
158 next_line:
159 p = fgets(buf, MAX_BUF, fp);
162 fclose(fp);
163 return 1;
167 next_plist_track(struct song_metadata *psong, struct stat *stat, char *lang, char *type)
169 if(_next_track)
170 return _next_track(psong, stat, lang, type);
171 return -1;