Make AVI demuxer more resilient against broken or incomplete files.
[mplayer/glamo.git] / playtreeparser.c
blobab80bde0201514924f7fa5bf0b5f288d5753474c
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"
24 extern play_tree_t*
25 asx_parser_build_tree(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->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,"<smil",5)==0 || strncasecmp(line,"<?wpl",5)==0 ||
457 strncasecmp(line,"(smil-document",14)==0)
458 break; // smil header found
459 else
460 return NULL; //line not smil exit
463 if (!line) return NULL;
464 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected smil playlist format\n");
465 play_tree_parser_stop_keeping(p);
467 if (strncasecmp(line,"(smil-document",14)==0) {
468 mp_msg(MSGT_PLAYTREE,MSGL_V,"Special smil-over-realrtsp playlist header\n");
469 is_rmsmil = 1;
470 if (sscanf(line, "(smil-document (ver 1.0)(npkt %u)(ttlpkt %u", &npkt, &ttlpkt) != 2) {
471 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: header parsing failure, assuming single packet.\n");
472 npkt = ttlpkt = 1;
474 if (ttlpkt == 0 || npkt > ttlpkt) {
475 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: bad packet counters (npkk = %u, ttlpkt = %u), assuming single packet.\n",
476 npkt, ttlpkt);
477 npkt = ttlpkt = 1;
481 //Get entries from smil
482 src_line = line;
483 line = NULL;
484 do {
485 strstrip(src_line);
486 if (line) {
487 free(line);
488 line = NULL;
490 /* If we're parsing smil over realrtsp and this is not the last packet and
491 * this is the last line in the packet (terminating with ") ) we must get
492 * the next line, strip the header, and concatenate it to the current line.
494 if (is_rmsmil && npkt != ttlpkt && strstr(src_line,"\")")) {
495 char *payload;
497 line = strdup(src_line);
498 if(!(src_line = play_tree_parser_get_line(p))) {
499 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: can't get line from packet %u/%u.\n", npkt, ttlpkt);
500 break;
502 strstrip(src_line);
503 // Skip header, packet starts after "
504 if(!(payload = strchr(src_line,'\"'))) {
505 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: can't find start of packet, using complete line.\n");
506 payload = src_line;
507 } else
508 payload++;
509 // Skip ") at the end of the last line from the current packet
510 line[strlen(line)-2] = 0;
511 line = realloc(line, strlen(line)+strlen(payload)+1);
512 strcat (line, payload);
513 npkt++;
514 } else
515 line = strdup(src_line);
516 /* Unescape \" to " for smil-over-rtsp */
517 if (is_rmsmil && line[0] != '\0') {
518 int i, j;
520 for (i = 0; i < strlen(line); i++)
521 if (line[i] == '\\' && line[i+1] == '"')
522 for (j = i; line[j]; j++)
523 line[j] = line[j+1];
525 pos = line;
526 while (pos) {
527 if (!entrymode) { // all entries filled so far
528 while ((pos=strchr(pos, '<'))) {
529 if (strncasecmp(pos,"<video",6)==0 || strncasecmp(pos,"<audio",6)==0 || strncasecmp(pos,"<media",6)==0) {
530 entrymode=1;
531 break; // Got a valid tag, exit '<' search loop
533 pos++;
536 if (entrymode) { //Entry found but not yet filled
537 pos = strstr(pos,"src="); // Is source present on this line
538 if (pos != NULL) {
539 entrymode=0;
540 if (pos[4] != '"' && pos[4] != '\'') {
541 mp_msg(MSGT_PLAYTREE,MSGL_V,"Unknown delimiter %c in source line %s\n", pos[4], line);
542 break;
544 s_start=pos+5;
545 s_end=strchr(s_start,pos[4]);
546 if (s_end == NULL) {
547 mp_msg(MSGT_PLAYTREE,MSGL_V,"Error parsing this source line %s\n",line);
548 break;
550 if (s_end-s_start> 511) {
551 mp_msg(MSGT_PLAYTREE,MSGL_V,"Cannot store such a large source %s\n",line);
552 break;
554 strncpy(source,s_start,s_end-s_start);
555 source[(s_end-s_start)]='\0'; // Null terminate
556 entry = play_tree_new();
557 play_tree_add_file(entry,source);
558 if(!list) //Insert new entry
559 list = entry;
560 else
561 play_tree_append_entry(last_entry,entry);
562 last_entry = entry;
563 pos = s_end;
567 } while((src_line = play_tree_parser_get_line(p)) != NULL);
569 if (line)
570 free(line);
572 if(!list) return NULL; // Nothing found
574 entry = play_tree_new();
575 play_tree_set_child(entry,list);
576 return entry;
579 static play_tree_t*
580 embedded_playlist_parse(char *line) {
581 int f=DEMUXER_TYPE_PLAYLIST;
582 stream_t* stream;
583 play_tree_parser_t* ptp;
584 play_tree_t* entry;
586 // Get stream opened to link
587 stream=open_stream(line,0,&f);
588 if(!stream) {
589 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Can't open playlist %s\n",line);
590 return NULL;
593 //add new playtree
594 mp_msg(MSGT_PLAYTREE,MSGL_V,"Adding playlist %s to element entryref\n",line);
596 ptp = play_tree_parser_new(stream,1);
597 entry = play_tree_parser_get_play_tree(ptp, 1);
598 play_tree_parser_free(ptp);
599 free_stream(stream);
601 return entry;
604 static play_tree_t*
605 parse_textplain(play_tree_parser_t* p) {
606 char* line;
607 char *c;
608 int embedded;
609 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
611 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying plaintext playlist...\n");
612 play_tree_parser_stop_keeping(p);
614 while((line = play_tree_parser_get_line(p)) != NULL) {
615 strstrip(line);
616 if(line[0] == '\0' || line[0] == '#' || (line[0] == '/' && line[1] == '/'))
617 continue;
619 //Special check for embedded smil or ram reference in file
620 embedded = 0;
621 if (strlen(line) > 5)
622 for(c = line; c[0]; c++ )
623 if ( ((c[0] == '.') && //start with . and next have smil with optional ? or &
624 (tolower(c[1]) == 's') && (tolower(c[2])== 'm') &&
625 (tolower(c[3]) == 'i') && (tolower(c[4]) == 'l') &&
626 (!c[5] || c[5] == '?' || c[5] == '&')) || // or
627 ((c[0] == '.') && // start with . and next have smi or ram with optional ? or &
628 ( ((tolower(c[1]) == 's') && (tolower(c[2])== 'm') && (tolower(c[3]) == 'i')) ||
629 ((tolower(c[1]) == 'r') && (tolower(c[2])== 'a') && (tolower(c[3]) == 'm')) )
630 && (!c[4] || c[4] == '?' || c[4] == '&')) ){
631 entry=embedded_playlist_parse(line);
632 embedded = 1;
633 break;
636 if (!embedded) { //regular file link
637 entry = play_tree_new();
638 play_tree_add_file(entry,line);
641 if (entry != NULL) {
642 if(!list)
643 list = entry;
644 else
645 play_tree_append_entry(last_entry,entry);
646 last_entry = entry;
650 if(!list) return NULL;
651 entry = play_tree_new();
652 play_tree_set_child(entry,list);
653 return entry;
656 play_tree_t*
657 parse_playtree(stream_t *stream, int forced) {
658 play_tree_parser_t* p;
659 play_tree_t* ret;
661 #ifdef MP_DEBUG
662 assert(stream != NULL);
663 #endif
665 p = play_tree_parser_new(stream,0);
666 if(!p)
667 return NULL;
669 ret = play_tree_parser_get_play_tree(p, forced);
670 play_tree_parser_free(p);
672 return ret;
675 static void
676 play_tree_add_basepath(play_tree_t* pt, char* bp) {
677 int i,bl = strlen(bp),fl;
679 if(pt->child) {
680 play_tree_t* i;
681 for(i = pt->child ; i != NULL ; i = i->next)
682 play_tree_add_basepath(i,bp);
683 return;
686 if(!pt->files)
687 return;
689 for(i = 0 ; pt->files[i] != NULL ; i++) {
690 fl = strlen(pt->files[i]);
691 // if we find a full unix path, url:// or X:\ at the beginning,
692 // don't mangle it.
693 if(fl <= 0 || strstr(pt->files[i],"://") || (strstr(pt->files[i],":\\") == pt->files[i] + 1) || (pt->files[i][0] == '/') )
694 continue;
695 // if the path begins with \ then prepend drive letter to it.
696 if (pt->files[i][0] == '\\') {
697 if (pt->files[i][1] == '\\')
698 continue;
699 pt->files[i] = (char*)realloc(pt->files[i],2+fl+1);
700 memmove(pt->files[i] + 2,pt->files[i],fl+1);
701 memcpy(pt->files[i],bp,2);
702 continue;
704 pt->files[i] = (char*)realloc(pt->files[i],bl+fl+1);
705 memmove(pt->files[i] + bl,pt->files[i],fl+1);
706 memcpy(pt->files[i],bp,bl);
710 // Wrapper for play_tree_add_basepath (add base path from file)
711 void play_tree_add_bpf(play_tree_t* pt, char* filename)
713 char *ls, *file;
715 if (pt && filename)
717 file = strdup(filename);
718 if (file)
720 ls = strrchr(file,'/');
721 if(!ls) ls = strrchr(file,'\\');
722 if(ls) {
723 ls[1] = '\0';
724 play_tree_add_basepath(pt,file);
726 free(file);
731 play_tree_t*
732 parse_playlist_file(char* file) {
733 stream_t *stream;
734 play_tree_t* ret;
735 int f=DEMUXER_TYPE_PLAYLIST;
737 stream = open_stream(file,0,&f);
739 if(!stream) {
740 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Error while opening playlist file %s: %s\n",file,strerror(errno));
741 return NULL;
744 mp_msg(MSGT_PLAYTREE,MSGL_V,"Parsing playlist file %s...\n",file);
746 ret = parse_playtree(stream,1);
747 free_stream(stream);
749 play_tree_add_bpf(ret, file);
751 return ret;
756 play_tree_parser_t*
757 play_tree_parser_new(stream_t* stream,int deep) {
758 play_tree_parser_t* p;
760 p = calloc(1,sizeof(play_tree_parser_t));
761 if(!p)
762 return NULL;
763 p->stream = stream;
764 p->deep = deep;
765 p->keep = 1;
767 return p;
771 void
772 play_tree_parser_free(play_tree_parser_t* p) {
774 #ifdef MP_DEBUG
775 assert(p != NULL);
776 #endif
778 if(p->buffer) free(p->buffer);
779 if(p->line) free(p->line);
780 free(p);
783 play_tree_t*
784 play_tree_parser_get_play_tree(play_tree_parser_t* p, int forced) {
785 play_tree_t* tree = NULL;
787 #ifdef MP_DEBUG
788 assert(p != NULL);
789 #endif
792 while(play_tree_parser_get_line(p) != NULL) {
793 play_tree_parser_reset(p);
795 tree = parse_asx(p);
796 if(tree) break;
797 play_tree_parser_reset(p);
799 tree = parse_pls(p);
800 if(tree) break;
801 play_tree_parser_reset(p);
803 tree = parse_m3u(p);
804 if(tree) break;
805 play_tree_parser_reset(p);
807 tree = parse_ref_ini(p);
808 if(tree) break;
809 play_tree_parser_reset(p);
811 tree = parse_smil(p);
812 if(tree) break;
813 play_tree_parser_reset(p);
815 // Here come the others formats ( textplain must stay the last one )
816 if (forced)
818 tree = parse_textplain(p);
819 if(tree) break;
821 break;
824 if(tree)
825 mp_msg(MSGT_PLAYTREE,MSGL_V,"Playlist successfully parsed\n");
826 else
827 mp_msg(MSGT_PLAYTREE,((forced==1)?MSGL_ERR:MSGL_V),"Error while parsing playlist\n");
829 if(tree)
830 tree = play_tree_cleanup(tree);
832 if(!tree) mp_msg(MSGT_PLAYTREE,((forced==1)?MSGL_WARN:MSGL_V),"Warning: empty playlist\n");
834 return tree;