Simplify, do not duplicate buffer size calculation
[mplayer/glamo.git] / playtreeparser.c
blob0d4c0fe43d048d8faccc8a458a064fcdb76124c6
2 /// \file
3 /// \ingroup PlaytreeParser
5 #include "config.h"
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #ifdef MP_DEBUG
10 #include <assert.h>
11 #endif
12 #include <errno.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include <ctype.h>
18 #include "m_config.h"
19 #include "playtree.h"
20 #include "playtreeparser.h"
21 #include "stream/stream.h"
22 #include "libmpdemux/demuxer.h"
23 #include "mp_msg.h"
26 extern play_tree_t*
27 asx_parser_build_tree(char* buffer, int ref);
29 #define BUF_STEP 1024
31 #define WHITES " \n\r\t"
33 static void
34 strstrip(char* str) {
35 char* i;
37 if (str==NULL)
38 return;
39 for(i = str ; i[0] != '\0' && strchr(WHITES,i[0]) != NULL; i++)
40 /* NOTHING */;
41 if(i[0] != '\0') {
42 memmove(str,i,strlen(i) + 1);
43 for(i = str + strlen(str) - 1 ; strchr(WHITES,i[0]) != NULL; i--)
44 /* NOTHING */;
45 i[1] = '\0';
46 } else
47 str[0] = '\0';
50 static char*
51 play_tree_parser_get_line(play_tree_parser_t* p) {
52 char *end,*line_end;
53 int r,resize = 0;
55 if(p->buffer == NULL) {
56 p->buffer = malloc(BUF_STEP);
57 p->buffer_size = BUF_STEP;
58 p->iter = p->buffer;
61 if(p->stream->eof && (p->buffer_end == 0 || p->iter[0] == '\0'))
62 return NULL;
64 while(1) {
66 if(resize) {
67 r = p->iter - p->buffer;
68 p->buffer = (char*)realloc(p->buffer,p->buffer_size+BUF_STEP);
69 p->iter = p->buffer + r;
70 p->buffer_size += BUF_STEP;
71 resize = 0;
74 if(p->buffer_size - p->buffer_end > 1 && ! p->stream->eof) {
75 r = stream_read(p->stream,p->buffer + p->buffer_end,p->buffer_size - p->buffer_end - 1);
76 if(r > 0) {
77 p->buffer_end += r;
78 p->buffer[p->buffer_end] = '\0';
79 while(strlen(p->buffer + p->buffer_end - r) != r)
80 p->buffer[p->buffer_end - r + strlen(p->buffer + p->buffer_end - r)] = '\n';
84 end = strchr(p->iter,'\n');
85 if(!end) {
86 if(p->stream->eof) {
87 end = p->buffer + p->buffer_end;
88 break;
90 resize = 1;
91 continue;
93 break;
96 line_end = (end > p->iter && *(end-1) == '\r') ? end-1 : end;
97 if(line_end - p->iter >= 0)
98 p->line = (char*)realloc(p->line,line_end - p->iter+1);
99 else
100 return NULL;
101 if(line_end - p->iter > 0)
102 strncpy(p->line,p->iter,line_end - p->iter);
103 p->line[line_end - p->iter] = '\0';
104 if(end[0] != '\0')
105 end++;
107 if(!p->keep) {
108 if(end[0] != '\0') {
109 p->buffer_end -= end-p->iter;
110 memmove(p->buffer,end,p->buffer_end);
111 p->buffer[p->buffer_end] = '\0';
112 } else
113 p->buffer_end = 0;
114 p->iter = p->buffer;
115 } else
116 p->iter = end;
118 return p->line;
121 static void
122 play_tree_parser_reset(play_tree_parser_t* p) {
123 p->iter = p->buffer;
126 static void
127 play_tree_parser_stop_keeping(play_tree_parser_t* p) {
128 p->keep = 0;
129 if(p->iter && p->iter != p->buffer) {
130 p->buffer_end -= p->iter -p->buffer;
131 if(p->buffer_end)
132 memmove(p->buffer,p->iter,p->buffer_end);
133 p->iter = p->buffer;
138 static play_tree_t*
139 parse_asx(play_tree_parser_t* p) {
140 int comments = 0,get_line = 1;
141 char* line = NULL;
143 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying asx...\n");
145 while(1) {
146 if(get_line) {
147 line = play_tree_parser_get_line(p);
148 if(!line)
149 return NULL;
150 strstrip(line);
151 if(line[0] == '\0')
152 continue;
154 if(!comments) {
155 if(line[0] != '<') {
156 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"First char isn't '<' but '%c'\n",line[0]);
157 mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Buffer = [%s]\n",p->buffer);
158 return NULL;
159 } else if(strncmp(line,"<!--",4) == 0) { // Comments
160 comments = 1;
161 line += 4;
162 if(line[0] != '\0' && strlen(line) > 0)
163 get_line = 0;
164 } else if(strncasecmp(line,"<ASX",4) == 0) // We got an asx element
165 break;
166 else // We don't get an asx
167 return NULL;
168 } else { // Comments
169 char* c;
170 c = strchr(line,'-');
171 if(c) {
172 if (strncmp(c,"--!>",4) == 0) { // End of comments
173 comments = 0;
174 line = c+4;
175 if(line[0] != '\0') // There is some more data on this line : keep it
176 get_line = 0;
178 } else {
179 line = c+1; // Jump the -
180 if(line[0] != '\0') // Some more data
181 get_line = 0;
182 else // End of line
183 get_line = 1;
185 } else // No - on this line (or rest of line) : get next one
186 get_line = 1;
190 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected asx format\n");
192 // We have an asx : load it in memory and parse
194 while((line = play_tree_parser_get_line(p)) != NULL)
195 /* NOTHING */;
197 mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Parsing asx file: [%s]\n",p->buffer);
198 return asx_parser_build_tree(p->buffer,p->deep);
201 static char*
202 pls_entry_get_value(char* line) {
203 char* i;
205 i = strchr(line,'=');
206 if(!i || i[1] == '\0')
207 return NULL;
208 else
209 return i+1;
212 typedef struct pls_entry {
213 char* file;
214 char* title;
215 char* length;
216 } pls_entry_t;
218 static int
219 pls_read_entry(char* line,pls_entry_t** _e,int* _max_entry,char** val) {
220 int num,max_entry = (*_max_entry);
221 pls_entry_t* e = (*_e);
222 char* v;
224 v = pls_entry_get_value(line);
225 if(!v) {
226 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
227 return 0;
230 num = atoi(line);
231 if(num < 0) {
232 num = max_entry+1;
233 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"No entry index in entry %s\nAssuming %d\n",line,num);
235 if(num > max_entry) {
236 e = (pls_entry_t*)realloc(e,num*sizeof(pls_entry_t));
237 memset(&e[max_entry],0,(num-max_entry)*sizeof(pls_entry_t));
238 max_entry = num;
240 (*_e) = e;
241 (*_max_entry) = max_entry;
242 (*val) = v;
244 return num;
248 static play_tree_t*
249 parse_pls(play_tree_parser_t* p) {
250 char *line,*v;
251 pls_entry_t* entries = NULL;
252 int n_entries = 0,max_entry=0,num;
253 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
255 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying Winamp playlist...\n");
256 while((line = play_tree_parser_get_line(p))) {
257 strstrip(line);
258 if(strlen(line))
259 break;
261 if (!line)
262 return NULL;
263 if(strcasecmp(line,"[playlist]"))
264 return NULL;
265 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected Winamp playlist format\n");
266 play_tree_parser_stop_keeping(p);
267 line = play_tree_parser_get_line(p);
268 if(!line)
269 return NULL;
270 strstrip(line);
271 if(strncasecmp(line,"NumberOfEntries",15) == 0) {
272 v = pls_entry_get_value(line);
273 n_entries = atoi(v);
274 if(n_entries < 0)
275 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Invalid number of entries: very funny!!!\n");
276 else
277 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Playlist claims to have %d entries. Let's see.\n",n_entries);
278 line = play_tree_parser_get_line(p);
281 while(line) {
282 strstrip(line);
283 if(line[0] == '\0') {
284 line = play_tree_parser_get_line(p);
285 continue;
287 if(strncasecmp(line,"File",4) == 0) {
288 num = pls_read_entry(line+4,&entries,&max_entry,&v);
289 if(num < 0)
290 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
291 else
292 entries[num-1].file = strdup(v);
293 } else if(strncasecmp(line,"Title",5) == 0) {
294 num = pls_read_entry(line+5,&entries,&max_entry,&v);
295 if(num < 0)
296 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
297 else
298 entries[num-1].title = strdup(v);
299 } else if(strncasecmp(line,"Length",6) == 0) {
300 num = pls_read_entry(line+6,&entries,&max_entry,&v);
301 if(num < 0)
302 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
303 else
304 entries[num-1].length = strdup(v);
305 } else
306 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Unknown entry type %s\n",line);
307 line = play_tree_parser_get_line(p);
310 for(num = 0; num < max_entry ; num++) {
311 if(entries[num].file == NULL)
312 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Entry %d don't have a file !!!!\n",num+1);
313 else {
314 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding entry %s\n",entries[num].file);
315 entry = play_tree_new();
316 play_tree_add_file(entry,entries[num].file);
317 free(entries[num].file);
318 if(list)
319 play_tree_append_entry(last_entry,entry);
320 else
321 list = entry;
322 last_entry = entry;
324 if(entries[num].title) {
325 // When we have info in playtree we add this info
326 free(entries[num].title);
328 if(entries[num].length) {
329 // When we have info in playtree we add this info
330 free(entries[num].length);
334 free(entries);
336 entry = play_tree_new();
337 play_tree_set_child(entry,list);
338 return entry;
342 Reference Ini-Format: Each entry is assumed a reference
344 static play_tree_t*
345 parse_ref_ini(play_tree_parser_t* p) {
346 char *line,*v;
347 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
349 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying reference-ini playlist...\n");
350 if (!(line = play_tree_parser_get_line(p)))
351 return NULL;
352 strstrip(line);
353 if(strcasecmp(line,"[Reference]"))
354 return NULL;
355 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected reference-ini playlist format\n");
356 play_tree_parser_stop_keeping(p);
357 line = play_tree_parser_get_line(p);
358 if(!line)
359 return NULL;
360 while(line) {
361 strstrip(line);
362 if(strncasecmp(line,"Ref",3) == 0) {
363 v = pls_entry_get_value(line+3);
364 if(!v)
365 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
366 else
368 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding entry %s\n",v);
369 entry = play_tree_new();
370 play_tree_add_file(entry,v);
371 if(list)
372 play_tree_append_entry(last_entry,entry);
373 else
374 list = entry;
375 last_entry = entry;
378 line = play_tree_parser_get_line(p);
381 if(!list) return NULL;
382 entry = play_tree_new();
383 play_tree_set_child(entry,list);
384 return entry;
387 static play_tree_t*
388 parse_m3u(play_tree_parser_t* p) {
389 char* line;
390 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
392 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying extended m3u playlist...\n");
393 if (!(line = play_tree_parser_get_line(p)))
394 return NULL;
395 strstrip(line);
396 if(strcasecmp(line,"#EXTM3U"))
397 return NULL;
398 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected extended m3u playlist format\n");
399 play_tree_parser_stop_keeping(p);
401 while((line = play_tree_parser_get_line(p)) != NULL) {
402 strstrip(line);
403 if(line[0] == '\0')
404 continue;
405 /* EXTM3U files contain such lines:
406 * #EXTINF:<seconds>, <title>
407 * followed by a line with the filename
408 * for now we have no place to put that
409 * so we just skip that extra-info ::atmos
411 if(line[0] == '#') {
412 #if 0 /* code functional */
413 if(strncasecmp(line,"#EXTINF:",8) == 0) {
414 mp_msg(MSGT_PLAYTREE,MSGL_INFO,"[M3U] Duration: %dsec Title: %s\n",
415 strtol(line+8,&line,10), line+2);
417 #endif
418 continue;
420 entry = play_tree_new();
421 play_tree_add_file(entry,line);
422 if(!list)
423 list = entry;
424 else
425 play_tree_append_entry(last_entry,entry);
426 last_entry = entry;
429 if(!list) return NULL;
430 entry = play_tree_new();
431 play_tree_set_child(entry,list);
432 return entry;
435 static play_tree_t*
436 parse_smil(play_tree_parser_t* p) {
437 int entrymode=0;
438 char* line,source[512],*pos,*s_start,*s_end,*src_line;
439 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
440 int is_rmsmil = 0;
441 unsigned int npkt, ttlpkt;
443 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying smil playlist...\n");
445 // Check if smil
446 while((line = play_tree_parser_get_line(p)) != NULL) {
447 strstrip(line);
448 if(line[0] == '\0') // Ignore empties
449 continue;
450 if (strncasecmp(line,"<?xml",5)==0) // smil in xml
451 continue;
452 if (strncasecmp(line,"<smil",5)==0 || strncasecmp(line,"<?wpl",5)==0 ||
453 strncasecmp(line,"(smil-document",14)==0)
454 break; // smil header found
455 else
456 return NULL; //line not smil exit
459 if (!line) return NULL;
460 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected smil playlist format\n");
461 play_tree_parser_stop_keeping(p);
463 if (strncasecmp(line,"(smil-document",14)==0) {
464 mp_msg(MSGT_PLAYTREE,MSGL_V,"Special smil-over-realrtsp playlist header\n");
465 is_rmsmil = 1;
466 if (sscanf(line, "(smil-document (ver 1.0)(npkt %u)(ttlpkt %u", &npkt, &ttlpkt) != 2) {
467 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: header parsing failure, assuming single packet.\n");
468 npkt = ttlpkt = 1;
470 if (ttlpkt == 0 || npkt > ttlpkt) {
471 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: bad packet counters (npkk = %u, ttlpkt = %u), assuming single packet.\n",
472 npkt, ttlpkt);
473 npkt = ttlpkt = 1;
477 //Get entries from smil
478 src_line = line;
479 line = NULL;
480 do {
481 strstrip(src_line);
482 if (line) {
483 free(line);
484 line = NULL;
486 /* If we're parsing smil over realrtsp and this is not the last packet and
487 * this is the last line in the packet (terminating with ") ) we must get
488 * the next line, strip the header, and concatenate it to the current line.
490 if (is_rmsmil && npkt != ttlpkt && strstr(src_line,"\")")) {
491 char *payload;
493 line = strdup(src_line);
494 if(!(src_line = play_tree_parser_get_line(p))) {
495 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: can't get line from packet %u/%u.\n", npkt, ttlpkt);
496 break;
498 strstrip(src_line);
499 // Skip header, packet starts after "
500 if(!(payload = strchr(src_line,'\"'))) {
501 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: can't find start of packet, using complete line.\n");
502 payload = src_line;
503 } else
504 payload++;
505 // Skip ") at the end of the last line from the current packet
506 line[strlen(line)-2] = 0;
507 line = realloc(line, strlen(line)+strlen(payload)+1);
508 strcat (line, payload);
509 npkt++;
510 } else
511 line = strdup(src_line);
512 /* Unescape \" to " for smil-over-rtsp */
513 if (is_rmsmil && line[0] != '\0') {
514 int i, j;
516 for (i = 0; i < strlen(line); i++)
517 if (line[i] == '\\' && line[i+1] == '"')
518 for (j = i; line[j]; j++)
519 line[j] = line[j+1];
521 pos = line;
522 while (pos) {
523 if (!entrymode) { // all entries filled so far
524 while ((pos=strchr(pos, '<'))) {
525 if (strncasecmp(pos,"<video",6)==0 || strncasecmp(pos,"<audio",6)==0 || strncasecmp(pos,"<media",6)==0) {
526 entrymode=1;
527 break; // Got a valid tag, exit '<' search loop
529 pos++;
532 if (entrymode) { //Entry found but not yet filled
533 pos = strstr(pos,"src="); // Is source present on this line
534 if (pos != NULL) {
535 entrymode=0;
536 if (pos[4] != '"' && pos[4] != '\'') {
537 mp_msg(MSGT_PLAYTREE,MSGL_V,"Unknown delimiter %c in source line %s\n", pos[4], line);
538 break;
540 s_start=pos+5;
541 s_end=strchr(s_start,pos[4]);
542 if (s_end == NULL) {
543 mp_msg(MSGT_PLAYTREE,MSGL_V,"Error parsing this source line %s\n",line);
544 break;
546 if (s_end-s_start> 511) {
547 mp_msg(MSGT_PLAYTREE,MSGL_V,"Cannot store such a large source %s\n",line);
548 break;
550 strncpy(source,s_start,s_end-s_start);
551 source[(s_end-s_start)]='\0'; // Null terminate
552 entry = play_tree_new();
553 play_tree_add_file(entry,source);
554 if(!list) //Insert new entry
555 list = entry;
556 else
557 play_tree_append_entry(last_entry,entry);
558 last_entry = entry;
559 pos = s_end;
563 } while((src_line = play_tree_parser_get_line(p)) != NULL);
565 if (line)
566 free(line);
568 if(!list) return NULL; // Nothing found
570 entry = play_tree_new();
571 play_tree_set_child(entry,list);
572 return entry;
575 static play_tree_t*
576 embedded_playlist_parse(char *line) {
577 int f=DEMUXER_TYPE_PLAYLIST;
578 stream_t* stream;
579 play_tree_parser_t* ptp;
580 play_tree_t* entry;
582 // Get stream opened to link
583 stream=open_stream(line,0,&f);
584 if(!stream) {
585 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Can't open playlist %s\n",line);
586 return NULL;
589 //add new playtree
590 mp_msg(MSGT_PLAYTREE,MSGL_V,"Adding playlist %s to element entryref\n",line);
592 ptp = play_tree_parser_new(stream,1);
593 entry = play_tree_parser_get_play_tree(ptp, 1);
594 play_tree_parser_free(ptp);
595 free_stream(stream);
597 return entry;
600 static play_tree_t*
601 parse_textplain(play_tree_parser_t* p) {
602 char* line;
603 char *c;
604 int embedded;
605 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
607 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying plaintext playlist...\n");
608 play_tree_parser_stop_keeping(p);
610 while((line = play_tree_parser_get_line(p)) != NULL) {
611 strstrip(line);
612 if(line[0] == '\0' || line[0] == '#' || (line[0] == '/' && line[1] == '/'))
613 continue;
615 //Special check for embedded smil or ram reference in file
616 embedded = 0;
617 if (strlen(line) > 5)
618 for(c = line; c[0]; c++ )
619 if ( ((c[0] == '.') && //start with . and next have smil with optional ? or &
620 (tolower(c[1]) == 's') && (tolower(c[2])== 'm') &&
621 (tolower(c[3]) == 'i') && (tolower(c[4]) == 'l') &&
622 (!c[5] || c[5] == '?' || c[5] == '&')) || // or
623 ((c[0] == '.') && // start with . and next have smi or ram with optional ? or &
624 ( ((tolower(c[1]) == 's') && (tolower(c[2])== 'm') && (tolower(c[3]) == 'i')) ||
625 ((tolower(c[1]) == 'r') && (tolower(c[2])== 'a') && (tolower(c[3]) == 'm')) )
626 && (!c[4] || c[4] == '?' || c[4] == '&')) ){
627 entry=embedded_playlist_parse(line);
628 embedded = 1;
629 break;
632 if (!embedded) { //regular file link
633 entry = play_tree_new();
634 play_tree_add_file(entry,line);
637 if (entry != NULL) {
638 if(!list)
639 list = entry;
640 else
641 play_tree_append_entry(last_entry,entry);
642 last_entry = entry;
646 if(!list) return NULL;
647 entry = play_tree_new();
648 play_tree_set_child(entry,list);
649 return entry;
652 play_tree_t*
653 parse_playtree(stream_t *stream, int forced) {
654 play_tree_parser_t* p;
655 play_tree_t* ret;
657 #ifdef MP_DEBUG
658 assert(stream != NULL);
659 #endif
661 p = play_tree_parser_new(stream,0);
662 if(!p)
663 return NULL;
665 ret = play_tree_parser_get_play_tree(p, forced);
666 play_tree_parser_free(p);
668 return ret;
671 static void
672 play_tree_add_basepath(play_tree_t* pt, char* bp) {
673 int i,bl = strlen(bp),fl;
675 if(pt->child) {
676 play_tree_t* i;
677 for(i = pt->child ; i != NULL ; i = i->next)
678 play_tree_add_basepath(i,bp);
679 return;
682 if(!pt->files)
683 return;
685 for(i = 0 ; pt->files[i] != NULL ; i++) {
686 fl = strlen(pt->files[i]);
687 // if we find a full unix path, url:// or X:\ at the beginning,
688 // don't mangle it.
689 if(fl <= 0 || strstr(pt->files[i],"://") || (strstr(pt->files[i],":\\") == pt->files[i] + 1) || (pt->files[i][0] == '/') )
690 continue;
691 // if the path begins with \ then prepend drive letter to it.
692 if (pt->files[i][0] == '\\') {
693 if (pt->files[i][1] == '\\')
694 continue;
695 pt->files[i] = (char*)realloc(pt->files[i],2+fl+1);
696 memmove(pt->files[i] + 2,pt->files[i],fl+1);
697 memcpy(pt->files[i],bp,2);
698 continue;
700 pt->files[i] = (char*)realloc(pt->files[i],bl+fl+1);
701 memmove(pt->files[i] + bl,pt->files[i],fl+1);
702 memcpy(pt->files[i],bp,bl);
706 // Wrapper for play_tree_add_basepath (add base path from file)
707 void play_tree_add_bpf(play_tree_t* pt, char* filename)
709 char *ls, *file;
711 if (pt && filename)
713 file = strdup(filename);
714 if (file)
716 ls = strrchr(file,'/');
717 if(!ls) ls = strrchr(file,'\\');
718 if(ls) {
719 ls[1] = '\0';
720 play_tree_add_basepath(pt,file);
722 free(file);
727 play_tree_t*
728 parse_playlist_file(char* file) {
729 stream_t *stream;
730 play_tree_t* ret;
731 int f=DEMUXER_TYPE_PLAYLIST;
733 stream = open_stream(file,0,&f);
735 if(!stream) {
736 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Error while opening playlist file %s: %s\n",file,strerror(errno));
737 return NULL;
740 mp_msg(MSGT_PLAYTREE,MSGL_V,"Parsing playlist file %s...\n",file);
742 ret = parse_playtree(stream,1);
743 free_stream(stream);
745 play_tree_add_bpf(ret, file);
747 return ret;
752 play_tree_parser_t*
753 play_tree_parser_new(stream_t* stream,int deep) {
754 play_tree_parser_t* p;
756 p = calloc(1,sizeof(play_tree_parser_t));
757 if(!p)
758 return NULL;
759 p->stream = stream;
760 p->deep = deep;
761 p->keep = 1;
763 return p;
767 void
768 play_tree_parser_free(play_tree_parser_t* p) {
770 #ifdef MP_DEBUG
771 assert(p != NULL);
772 #endif
774 if(p->buffer) free(p->buffer);
775 if(p->line) free(p->line);
776 free(p);
779 play_tree_t*
780 play_tree_parser_get_play_tree(play_tree_parser_t* p, int forced) {
781 play_tree_t* tree = NULL;
783 #ifdef MP_DEBUG
784 assert(p != NULL);
785 #endif
788 while(play_tree_parser_get_line(p) != NULL) {
789 play_tree_parser_reset(p);
791 tree = parse_asx(p);
792 if(tree) break;
793 play_tree_parser_reset(p);
795 tree = parse_pls(p);
796 if(tree) break;
797 play_tree_parser_reset(p);
799 tree = parse_m3u(p);
800 if(tree) break;
801 play_tree_parser_reset(p);
803 tree = parse_ref_ini(p);
804 if(tree) break;
805 play_tree_parser_reset(p);
807 tree = parse_smil(p);
808 if(tree) break;
809 play_tree_parser_reset(p);
811 // Here come the others formats ( textplain must stay the last one )
812 if (forced)
814 tree = parse_textplain(p);
815 if(tree) break;
817 break;
820 if(tree)
821 mp_msg(MSGT_PLAYTREE,MSGL_V,"Playlist successfully parsed\n");
822 else
823 mp_msg(MSGT_PLAYTREE,((forced==1)?MSGL_ERR:MSGL_V),"Error while parsing playlist\n");
825 if(tree)
826 tree = play_tree_cleanup(tree);
828 if(!tree) mp_msg(MSGT_PLAYTREE,((forced==1)?MSGL_WARN:MSGL_V),"Warning: empty playlist\n");
830 return tree;