Merge svn changes up to r28712
[mplayer/glamo.git] / playtreeparser.c
blob9d34cc737bbae411d8edbcf63f320ece81bfa820
2 /// \file
3 /// \ingroup PlaytreeParser
5 #include "config.h"
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <assert.h>
10 #include <errno.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include <ctype.h>
16 #include "m_config.h"
17 #include "playtree.h"
18 #include "playtreeparser.h"
19 #include "stream/stream.h"
20 #include "libmpdemux/demuxer.h"
21 #include "mp_msg.h"
23 struct m_config;
24 extern play_tree_t*
25 asx_parser_build_tree(struct m_config *mconfig, char* buffer, int ref);
27 #define BUF_STEP 1024
29 #define WHITES " \n\r\t"
31 static void
32 strstrip(char* str) {
33 char* i;
35 if (str==NULL)
36 return;
37 for(i = str ; i[0] != '\0' && strchr(WHITES,i[0]) != NULL; i++)
38 /* NOTHING */;
39 if(i[0] != '\0') {
40 memmove(str,i,strlen(i) + 1);
41 for(i = str + strlen(str) - 1 ; strchr(WHITES,i[0]) != NULL; i--)
42 /* NOTHING */;
43 i[1] = '\0';
44 } else
45 str[0] = '\0';
48 static char*
49 play_tree_parser_get_line(play_tree_parser_t* p) {
50 char *end,*line_end;
51 int r,resize = 0;
53 if(p->buffer == NULL) {
54 p->buffer = malloc(BUF_STEP);
55 p->buffer_size = BUF_STEP;
56 p->buffer[0] = 0;
57 p->iter = p->buffer;
60 if(p->stream->eof && (p->buffer_end == 0 || p->iter[0] == '\0'))
61 return NULL;
63 assert(p->buffer_end < p->buffer_size);
64 assert(!p->buffer[p->buffer_end]);
65 while(1) {
67 if(resize) {
68 r = p->iter - p->buffer;
69 p->buffer = (char*)realloc(p->buffer,p->buffer_size+BUF_STEP);
70 p->iter = p->buffer + r;
71 p->buffer_size += BUF_STEP;
72 resize = 0;
75 if(p->buffer_size - p->buffer_end > 1 && ! p->stream->eof) {
76 r = stream_read(p->stream,p->buffer + p->buffer_end,p->buffer_size - p->buffer_end - 1);
77 if(r > 0) {
78 p->buffer_end += r;
79 assert(p->buffer_end < p->buffer_size);
80 p->buffer[p->buffer_end] = '\0';
81 while(strlen(p->buffer + p->buffer_end - r) != r)
82 p->buffer[p->buffer_end - r + strlen(p->buffer + p->buffer_end - r)] = '\n';
84 assert(!p->buffer[p->buffer_end]);
87 end = strchr(p->iter,'\n');
88 if(!end) {
89 if(p->stream->eof) {
90 end = p->buffer + p->buffer_end;
91 break;
93 resize = 1;
94 continue;
96 break;
99 line_end = (end > p->iter && *(end-1) == '\r') ? end-1 : end;
100 if(line_end - p->iter >= 0)
101 p->line = (char*)realloc(p->line,line_end - p->iter+1);
102 else
103 return NULL;
104 if(line_end - p->iter > 0)
105 strncpy(p->line,p->iter,line_end - p->iter);
106 p->line[line_end - p->iter] = '\0';
107 if(end[0] != '\0')
108 end++;
110 if(!p->keep) {
111 if(end[0] != '\0') {
112 p->buffer_end -= end-p->iter;
113 memmove(p->buffer,end,p->buffer_end);
114 } else
115 p->buffer_end = 0;
116 p->buffer[p->buffer_end] = '\0';
117 p->iter = p->buffer;
118 } else
119 p->iter = end;
121 return p->line;
124 static void
125 play_tree_parser_reset(play_tree_parser_t* p) {
126 p->iter = p->buffer;
129 static void
130 play_tree_parser_stop_keeping(play_tree_parser_t* p) {
131 p->keep = 0;
132 if(p->iter && p->iter != p->buffer) {
133 p->buffer_end -= p->iter -p->buffer;
134 if(p->buffer_end)
135 memmove(p->buffer,p->iter,p->buffer_end);
136 p->buffer[p->buffer_end] = 0;
137 p->iter = p->buffer;
142 static play_tree_t*
143 parse_asx(play_tree_parser_t* p) {
144 int comments = 0,get_line = 1;
145 char* line = NULL;
147 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying asx...\n");
149 while(1) {
150 if(get_line) {
151 line = play_tree_parser_get_line(p);
152 if(!line)
153 return NULL;
154 strstrip(line);
155 if(line[0] == '\0')
156 continue;
158 if(!comments) {
159 if(line[0] != '<') {
160 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"First char isn't '<' but '%c'\n",line[0]);
161 mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Buffer = [%s]\n",p->buffer);
162 return NULL;
163 } else if(strncmp(line,"<!--",4) == 0) { // Comments
164 comments = 1;
165 line += 4;
166 if(line[0] != '\0' && strlen(line) > 0)
167 get_line = 0;
168 } else if(strncasecmp(line,"<ASX",4) == 0) // We got an asx element
169 break;
170 else // We don't get an asx
171 return NULL;
172 } else { // Comments
173 char* c;
174 c = strchr(line,'-');
175 if(c) {
176 if (strncmp(c,"--!>",4) == 0) { // End of comments
177 comments = 0;
178 line = c+4;
179 if(line[0] != '\0') // There is some more data on this line : keep it
180 get_line = 0;
182 } else {
183 line = c+1; // Jump the -
184 if(line[0] != '\0') // Some more data
185 get_line = 0;
186 else // End of line
187 get_line = 1;
189 } else // No - on this line (or rest of line) : get next one
190 get_line = 1;
194 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected asx format\n");
196 // We have an asx : load it in memory and parse
198 while((line = play_tree_parser_get_line(p)) != NULL)
199 /* NOTHING */;
201 mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Parsing asx file: [%s]\n",p->buffer);
202 return asx_parser_build_tree(p->mconfig, p->buffer,p->deep);
205 static char*
206 pls_entry_get_value(char* line) {
207 char* i;
209 i = strchr(line,'=');
210 if(!i || i[1] == '\0')
211 return NULL;
212 else
213 return i+1;
216 typedef struct pls_entry {
217 char* file;
218 char* title;
219 char* length;
220 } pls_entry_t;
222 static int
223 pls_read_entry(char* line,pls_entry_t** _e,int* _max_entry,char** val) {
224 int num,max_entry = (*_max_entry);
225 pls_entry_t* e = (*_e);
226 char* v;
228 v = pls_entry_get_value(line);
229 if(!v) {
230 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
231 return 0;
234 num = atoi(line);
235 if(num < 0) {
236 num = max_entry+1;
237 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"No entry index in entry %s\nAssuming %d\n",line,num);
239 if(num > max_entry) {
240 e = (pls_entry_t*)realloc(e,num*sizeof(pls_entry_t));
241 memset(&e[max_entry],0,(num-max_entry)*sizeof(pls_entry_t));
242 max_entry = num;
244 (*_e) = e;
245 (*_max_entry) = max_entry;
246 (*val) = v;
248 return num;
252 static play_tree_t*
253 parse_pls(play_tree_parser_t* p) {
254 char *line,*v;
255 pls_entry_t* entries = NULL;
256 int n_entries = 0,max_entry=0,num;
257 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
259 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying Winamp playlist...\n");
260 while((line = play_tree_parser_get_line(p))) {
261 strstrip(line);
262 if(strlen(line))
263 break;
265 if (!line)
266 return NULL;
267 if(strcasecmp(line,"[playlist]"))
268 return NULL;
269 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected Winamp playlist format\n");
270 play_tree_parser_stop_keeping(p);
271 line = play_tree_parser_get_line(p);
272 if(!line)
273 return NULL;
274 strstrip(line);
275 if(strncasecmp(line,"NumberOfEntries",15) == 0) {
276 v = pls_entry_get_value(line);
277 n_entries = atoi(v);
278 if(n_entries < 0)
279 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Invalid number of entries: very funny!!!\n");
280 else
281 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Playlist claims to have %d entries. Let's see.\n",n_entries);
282 line = play_tree_parser_get_line(p);
285 while(line) {
286 strstrip(line);
287 if(line[0] == '\0') {
288 line = play_tree_parser_get_line(p);
289 continue;
291 if(strncasecmp(line,"File",4) == 0) {
292 num = pls_read_entry(line+4,&entries,&max_entry,&v);
293 if(num < 0)
294 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
295 else
296 entries[num-1].file = strdup(v);
297 } else if(strncasecmp(line,"Title",5) == 0) {
298 num = pls_read_entry(line+5,&entries,&max_entry,&v);
299 if(num < 0)
300 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
301 else
302 entries[num-1].title = strdup(v);
303 } else if(strncasecmp(line,"Length",6) == 0) {
304 num = pls_read_entry(line+6,&entries,&max_entry,&v);
305 if(num < 0)
306 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
307 else
308 entries[num-1].length = strdup(v);
309 } else
310 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Unknown entry type %s\n",line);
311 line = play_tree_parser_get_line(p);
314 for(num = 0; num < max_entry ; num++) {
315 if(entries[num].file == NULL)
316 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Entry %d don't have a file !!!!\n",num+1);
317 else {
318 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding entry %s\n",entries[num].file);
319 entry = play_tree_new();
320 play_tree_add_file(entry,entries[num].file);
321 free(entries[num].file);
322 if(list)
323 play_tree_append_entry(last_entry,entry);
324 else
325 list = entry;
326 last_entry = entry;
328 if(entries[num].title) {
329 // When we have info in playtree we add this info
330 free(entries[num].title);
332 if(entries[num].length) {
333 // When we have info in playtree we add this info
334 free(entries[num].length);
338 free(entries);
340 entry = play_tree_new();
341 play_tree_set_child(entry,list);
342 return entry;
346 Reference Ini-Format: Each entry is assumed a reference
348 static play_tree_t*
349 parse_ref_ini(play_tree_parser_t* p) {
350 char *line,*v;
351 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
353 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying reference-ini playlist...\n");
354 if (!(line = play_tree_parser_get_line(p)))
355 return NULL;
356 strstrip(line);
357 if(strcasecmp(line,"[Reference]"))
358 return NULL;
359 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected reference-ini playlist format\n");
360 play_tree_parser_stop_keeping(p);
361 line = play_tree_parser_get_line(p);
362 if(!line)
363 return NULL;
364 while(line) {
365 strstrip(line);
366 if(strncasecmp(line,"Ref",3) == 0) {
367 v = pls_entry_get_value(line+3);
368 if(!v)
369 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
370 else
372 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding entry %s\n",v);
373 entry = play_tree_new();
374 play_tree_add_file(entry,v);
375 if(list)
376 play_tree_append_entry(last_entry,entry);
377 else
378 list = entry;
379 last_entry = entry;
382 line = play_tree_parser_get_line(p);
385 if(!list) return NULL;
386 entry = play_tree_new();
387 play_tree_set_child(entry,list);
388 return entry;
391 static play_tree_t*
392 parse_m3u(play_tree_parser_t* p) {
393 char* line;
394 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
396 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying extended m3u playlist...\n");
397 if (!(line = play_tree_parser_get_line(p)))
398 return NULL;
399 strstrip(line);
400 if(strcasecmp(line,"#EXTM3U"))
401 return NULL;
402 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected extended m3u playlist format\n");
403 play_tree_parser_stop_keeping(p);
405 while((line = play_tree_parser_get_line(p)) != NULL) {
406 strstrip(line);
407 if(line[0] == '\0')
408 continue;
409 /* EXTM3U files contain such lines:
410 * #EXTINF:<seconds>, <title>
411 * followed by a line with the filename
412 * for now we have no place to put that
413 * so we just skip that extra-info ::atmos
415 if(line[0] == '#') {
416 #if 0 /* code functional */
417 if(strncasecmp(line,"#EXTINF:",8) == 0) {
418 mp_msg(MSGT_PLAYTREE,MSGL_INFO,"[M3U] Duration: %dsec Title: %s\n",
419 strtol(line+8,&line,10), line+2);
421 #endif
422 continue;
424 entry = play_tree_new();
425 play_tree_add_file(entry,line);
426 if(!list)
427 list = entry;
428 else
429 play_tree_append_entry(last_entry,entry);
430 last_entry = entry;
433 if(!list) return NULL;
434 entry = play_tree_new();
435 play_tree_set_child(entry,list);
436 return entry;
439 static play_tree_t*
440 parse_smil(play_tree_parser_t* p) {
441 int entrymode=0;
442 char* line,source[512],*pos,*s_start,*s_end,*src_line;
443 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
444 int is_rmsmil = 0;
445 unsigned int npkt, ttlpkt;
447 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying smil playlist...\n");
449 // Check if smil
450 while((line = play_tree_parser_get_line(p)) != NULL) {
451 strstrip(line);
452 if(line[0] == '\0') // Ignore empties
453 continue;
454 if (strncasecmp(line,"<?xml",5)==0) // smil in xml
455 continue;
456 if (strncasecmp(line,"<!DOCTYPE smil",13)==0) // smil in xml
457 continue;
458 if (strncasecmp(line,"<smil",5)==0 || strncasecmp(line,"<?wpl",5)==0 ||
459 strncasecmp(line,"(smil-document",14)==0)
460 break; // smil header found
461 else
462 return NULL; //line not smil exit
465 if (!line) return NULL;
466 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected smil playlist format\n");
467 play_tree_parser_stop_keeping(p);
469 if (strncasecmp(line,"(smil-document",14)==0) {
470 mp_msg(MSGT_PLAYTREE,MSGL_V,"Special smil-over-realrtsp playlist header\n");
471 is_rmsmil = 1;
472 if (sscanf(line, "(smil-document (ver 1.0)(npkt %u)(ttlpkt %u", &npkt, &ttlpkt) != 2) {
473 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: header parsing failure, assuming single packet.\n");
474 npkt = ttlpkt = 1;
476 if (ttlpkt == 0 || npkt > ttlpkt) {
477 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: bad packet counters (npkk = %u, ttlpkt = %u), assuming single packet.\n",
478 npkt, ttlpkt);
479 npkt = ttlpkt = 1;
483 //Get entries from smil
484 src_line = line;
485 line = NULL;
486 do {
487 strstrip(src_line);
488 if (line) {
489 free(line);
490 line = NULL;
492 /* If we're parsing smil over realrtsp and this is not the last packet and
493 * this is the last line in the packet (terminating with ") ) we must get
494 * the next line, strip the header, and concatenate it to the current line.
496 if (is_rmsmil && npkt != ttlpkt && strstr(src_line,"\")")) {
497 char *payload;
499 line = strdup(src_line);
500 if(!(src_line = play_tree_parser_get_line(p))) {
501 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: can't get line from packet %u/%u.\n", npkt, ttlpkt);
502 break;
504 strstrip(src_line);
505 // Skip header, packet starts after "
506 if(!(payload = strchr(src_line,'\"'))) {
507 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: can't find start of packet, using complete line.\n");
508 payload = src_line;
509 } else
510 payload++;
511 // Skip ") at the end of the last line from the current packet
512 line[strlen(line)-2] = 0;
513 line = realloc(line, strlen(line)+strlen(payload)+1);
514 strcat (line, payload);
515 npkt++;
516 } else
517 line = strdup(src_line);
518 /* Unescape \" to " for smil-over-rtsp */
519 if (is_rmsmil && line[0] != '\0') {
520 int i, j;
522 for (i = 0; i < strlen(line); i++)
523 if (line[i] == '\\' && line[i+1] == '"')
524 for (j = i; line[j]; j++)
525 line[j] = line[j+1];
527 pos = line;
528 while (pos) {
529 if (!entrymode) { // all entries filled so far
530 while ((pos=strchr(pos, '<'))) {
531 if (strncasecmp(pos,"<video",6)==0 || strncasecmp(pos,"<audio",6)==0 || strncasecmp(pos,"<media",6)==0) {
532 entrymode=1;
533 break; // Got a valid tag, exit '<' search loop
535 pos++;
538 if (entrymode) { //Entry found but not yet filled
539 pos = strstr(pos,"src="); // Is source present on this line
540 if (pos != NULL) {
541 entrymode=0;
542 if (pos[4] != '"' && pos[4] != '\'') {
543 mp_msg(MSGT_PLAYTREE,MSGL_V,"Unknown delimiter %c in source line %s\n", pos[4], line);
544 break;
546 s_start=pos+5;
547 s_end=strchr(s_start,pos[4]);
548 if (s_end == NULL) {
549 mp_msg(MSGT_PLAYTREE,MSGL_V,"Error parsing this source line %s\n",line);
550 break;
552 if (s_end-s_start> 511) {
553 mp_msg(MSGT_PLAYTREE,MSGL_V,"Cannot store such a large source %s\n",line);
554 break;
556 strncpy(source,s_start,s_end-s_start);
557 source[(s_end-s_start)]='\0'; // Null terminate
558 entry = play_tree_new();
559 play_tree_add_file(entry,source);
560 if(!list) //Insert new entry
561 list = entry;
562 else
563 play_tree_append_entry(last_entry,entry);
564 last_entry = entry;
565 pos = s_end;
569 } while((src_line = play_tree_parser_get_line(p)) != NULL);
571 if (line)
572 free(line);
574 if(!list) return NULL; // Nothing found
576 entry = play_tree_new();
577 play_tree_set_child(entry,list);
578 return entry;
581 static play_tree_t*
582 embedded_playlist_parse(struct m_config *mconfig, char *line) {
583 int f=DEMUXER_TYPE_PLAYLIST;
584 stream_t* stream;
585 play_tree_parser_t* ptp;
586 play_tree_t* entry;
588 // Get stream opened to link
589 stream=open_stream(line,0,&f);
590 if(!stream) {
591 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Can't open playlist %s\n",line);
592 return NULL;
595 //add new playtree
596 mp_msg(MSGT_PLAYTREE,MSGL_V,"Adding playlist %s to element entryref\n",line);
598 ptp = play_tree_parser_new(stream, mconfig, 1);
599 entry = play_tree_parser_get_play_tree(ptp, 1);
600 play_tree_parser_free(ptp);
601 free_stream(stream);
603 return entry;
606 static play_tree_t*
607 parse_textplain(play_tree_parser_t* p) {
608 char* line;
609 char *c;
610 int embedded;
611 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
613 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying plaintext playlist...\n");
614 play_tree_parser_stop_keeping(p);
616 while((line = play_tree_parser_get_line(p)) != NULL) {
617 strstrip(line);
618 if(line[0] == '\0' || line[0] == '#' || (line[0] == '/' && line[1] == '/'))
619 continue;
621 //Special check for embedded smil or ram reference in file
622 embedded = 0;
623 if (strlen(line) > 5)
624 for(c = line; c[0]; c++ )
625 if ( ((c[0] == '.') && //start with . and next have smil with optional ? or &
626 (tolower(c[1]) == 's') && (tolower(c[2])== 'm') &&
627 (tolower(c[3]) == 'i') && (tolower(c[4]) == 'l') &&
628 (!c[5] || c[5] == '?' || c[5] == '&')) || // or
629 ((c[0] == '.') && // start with . and next have smi or ram with optional ? or &
630 ( ((tolower(c[1]) == 's') && (tolower(c[2])== 'm') && (tolower(c[3]) == 'i')) ||
631 ((tolower(c[1]) == 'r') && (tolower(c[2])== 'a') && (tolower(c[3]) == 'm')) )
632 && (!c[4] || c[4] == '?' || c[4] == '&')) ){
633 entry=embedded_playlist_parse(p->mconfig, line);
634 embedded = 1;
635 break;
638 if (!embedded) { //regular file link
639 entry = play_tree_new();
640 play_tree_add_file(entry,line);
643 if (entry != NULL) {
644 if(!list)
645 list = entry;
646 else
647 play_tree_append_entry(last_entry,entry);
648 last_entry = entry;
652 if(!list) return NULL;
653 entry = play_tree_new();
654 play_tree_set_child(entry,list);
655 return entry;
658 play_tree_t*
659 parse_playtree(stream_t *stream, struct m_config *mconfig, int forced) {
660 play_tree_parser_t* p;
661 play_tree_t* ret;
663 #ifdef MP_DEBUG
664 assert(stream != NULL);
665 #endif
667 p = play_tree_parser_new(stream, mconfig, 0);
668 if(!p)
669 return NULL;
671 ret = play_tree_parser_get_play_tree(p, forced);
672 play_tree_parser_free(p);
674 return ret;
677 static void
678 play_tree_add_basepath(play_tree_t* pt, char* bp) {
679 int i,bl = strlen(bp),fl;
681 if(pt->child) {
682 play_tree_t* i;
683 for(i = pt->child ; i != NULL ; i = i->next)
684 play_tree_add_basepath(i,bp);
685 return;
688 if(!pt->files)
689 return;
691 for(i = 0 ; pt->files[i] != NULL ; i++) {
692 fl = strlen(pt->files[i]);
693 // if we find a full unix path, url:// or X:\ at the beginning,
694 // don't mangle it.
695 if(fl <= 0 || strstr(pt->files[i],"://") || (strstr(pt->files[i],":\\") == pt->files[i] + 1) || (pt->files[i][0] == '/') )
696 continue;
697 // if the path begins with \ then prepend drive letter to it.
698 if (pt->files[i][0] == '\\') {
699 if (pt->files[i][1] == '\\')
700 continue;
701 pt->files[i] = (char*)realloc(pt->files[i],2+fl+1);
702 memmove(pt->files[i] + 2,pt->files[i],fl+1);
703 memcpy(pt->files[i],bp,2);
704 continue;
706 pt->files[i] = (char*)realloc(pt->files[i],bl+fl+1);
707 memmove(pt->files[i] + bl,pt->files[i],fl+1);
708 memcpy(pt->files[i],bp,bl);
712 // Wrapper for play_tree_add_basepath (add base path from file)
713 void play_tree_add_bpf(play_tree_t* pt, char* filename)
715 char *ls, *file;
717 if (pt && filename)
719 file = strdup(filename);
720 if (file)
722 ls = strrchr(file,'/');
723 if(!ls) ls = strrchr(file,'\\');
724 if(ls) {
725 ls[1] = '\0';
726 play_tree_add_basepath(pt,file);
728 free(file);
733 play_tree_t*
734 parse_playlist_file(struct m_config *mconfig, char* file) {
735 stream_t *stream;
736 play_tree_t* ret;
737 int f=DEMUXER_TYPE_PLAYLIST;
739 stream = open_stream(file,0,&f);
741 if(!stream) {
742 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Error while opening playlist file %s: %s\n",file,strerror(errno));
743 return NULL;
746 mp_msg(MSGT_PLAYTREE,MSGL_V,"Parsing playlist file %s...\n",file);
748 ret = parse_playtree(stream, mconfig, 1);
749 free_stream(stream);
751 play_tree_add_bpf(ret, file);
753 return ret;
758 play_tree_parser_t*
759 play_tree_parser_new(stream_t* stream, struct m_config *mconfig, int deep) {
760 play_tree_parser_t* p;
762 p = calloc(1,sizeof(play_tree_parser_t));
763 if(!p)
764 return NULL;
765 p->stream = stream;
766 p->mconfig = mconfig;
767 p->deep = deep;
768 p->keep = 1;
770 return p;
774 void
775 play_tree_parser_free(play_tree_parser_t* p) {
777 #ifdef MP_DEBUG
778 assert(p != NULL);
779 #endif
781 if(p->buffer) free(p->buffer);
782 if(p->line) free(p->line);
783 free(p);
786 play_tree_t*
787 play_tree_parser_get_play_tree(play_tree_parser_t* p, int forced) {
788 play_tree_t* tree = NULL;
790 #ifdef MP_DEBUG
791 assert(p != NULL);
792 #endif
795 while(play_tree_parser_get_line(p) != NULL) {
796 play_tree_parser_reset(p);
798 tree = parse_asx(p);
799 if(tree) break;
800 play_tree_parser_reset(p);
802 tree = parse_pls(p);
803 if(tree) break;
804 play_tree_parser_reset(p);
806 tree = parse_m3u(p);
807 if(tree) break;
808 play_tree_parser_reset(p);
810 tree = parse_ref_ini(p);
811 if(tree) break;
812 play_tree_parser_reset(p);
814 tree = parse_smil(p);
815 if(tree) break;
816 play_tree_parser_reset(p);
818 // Here come the others formats ( textplain must stay the last one )
819 if (forced)
821 tree = parse_textplain(p);
822 if(tree) break;
824 break;
827 if(tree)
828 mp_msg(MSGT_PLAYTREE,MSGL_V,"Playlist successfully parsed\n");
829 else
830 mp_msg(MSGT_PLAYTREE,((forced==1)?MSGL_ERR:MSGL_V),"Error while parsing playlist\n");
832 if(tree)
833 tree = play_tree_cleanup(tree);
835 if(!tree) mp_msg(MSGT_PLAYTREE,((forced==1)?MSGL_WARN:MSGL_V),"Warning: empty playlist\n");
837 return tree;