Makefile: remove outdated vf_halfpack entry
[mplayer/kovensky.git] / playtreeparser.c
blob8868d20694037d48aa079d02b3bd9cd5604a2fb6
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 /// \file
20 /// \ingroup PlaytreeParser
22 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <assert.h>
27 #include <errno.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <ctype.h>
33 #include "m_config.h"
34 #include "playtree.h"
35 #include "playtreeparser.h"
36 #include "stream/stream.h"
37 #include "libmpdemux/demuxer.h"
38 #include "mp_msg.h"
40 struct m_config;
41 extern play_tree_t*
42 asx_parser_build_tree(struct m_config *mconfig, char* buffer, int ref);
44 #define BUF_STEP 1024
46 #define WHITES " \n\r\t"
48 static void
49 strstrip(char* str) {
50 char* i;
52 if (str==NULL)
53 return;
54 for(i = str ; i[0] != '\0' && strchr(WHITES,i[0]) != NULL; i++)
55 /* NOTHING */;
56 if(i[0] != '\0') {
57 memmove(str,i,strlen(i) + 1);
58 for(i = str + strlen(str) - 1 ; strchr(WHITES,i[0]) != NULL; i--)
59 /* NOTHING */;
60 i[1] = '\0';
61 } else
62 str[0] = '\0';
65 static char*
66 play_tree_parser_get_line(play_tree_parser_t* p) {
67 char *end,*line_end;
68 int r,resize = 0;
70 if(p->buffer == NULL) {
71 p->buffer = malloc(BUF_STEP);
72 p->buffer_size = BUF_STEP;
73 p->buffer[0] = 0;
74 p->iter = p->buffer;
77 if(p->stream->eof && (p->buffer_end == 0 || p->iter[0] == '\0'))
78 return NULL;
80 assert(p->buffer_end < p->buffer_size);
81 assert(!p->buffer[p->buffer_end]);
82 while(1) {
84 if(resize) {
85 r = p->iter - p->buffer;
86 p->buffer = (char*)realloc(p->buffer,p->buffer_size+BUF_STEP);
87 p->iter = p->buffer + r;
88 p->buffer_size += BUF_STEP;
89 resize = 0;
92 if(p->buffer_size - p->buffer_end > 1 && ! p->stream->eof) {
93 r = stream_read(p->stream,p->buffer + p->buffer_end,p->buffer_size - p->buffer_end - 1);
94 if(r > 0) {
95 p->buffer_end += r;
96 assert(p->buffer_end < p->buffer_size);
97 p->buffer[p->buffer_end] = '\0';
98 while(strlen(p->buffer + p->buffer_end - r) != r)
99 p->buffer[p->buffer_end - r + strlen(p->buffer + p->buffer_end - r)] = '\n';
101 assert(!p->buffer[p->buffer_end]);
104 end = strchr(p->iter,'\n');
105 if(!end) {
106 if(p->stream->eof) {
107 end = p->buffer + p->buffer_end;
108 break;
110 resize = 1;
111 continue;
113 break;
116 line_end = (end > p->iter && *(end-1) == '\r') ? end-1 : end;
117 if(line_end - p->iter >= 0)
118 p->line = (char*)realloc(p->line,line_end - p->iter+1);
119 else
120 return NULL;
121 if(line_end - p->iter > 0)
122 strncpy(p->line,p->iter,line_end - p->iter);
123 p->line[line_end - p->iter] = '\0';
124 if(end[0] != '\0')
125 end++;
127 if(!p->keep) {
128 if(end[0] != '\0') {
129 p->buffer_end -= end-p->iter;
130 memmove(p->buffer,end,p->buffer_end);
131 } else
132 p->buffer_end = 0;
133 p->buffer[p->buffer_end] = '\0';
134 p->iter = p->buffer;
135 } else
136 p->iter = end;
138 return p->line;
141 static void
142 play_tree_parser_reset(play_tree_parser_t* p) {
143 p->iter = p->buffer;
146 static void
147 play_tree_parser_stop_keeping(play_tree_parser_t* p) {
148 p->keep = 0;
149 if(p->iter && p->iter != p->buffer) {
150 p->buffer_end -= p->iter -p->buffer;
151 if(p->buffer_end)
152 memmove(p->buffer,p->iter,p->buffer_end);
153 p->buffer[p->buffer_end] = 0;
154 p->iter = p->buffer;
159 static play_tree_t*
160 parse_asx(play_tree_parser_t* p) {
161 int comments = 0,get_line = 1;
162 char* line = NULL;
164 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying asx...\n");
166 while(1) {
167 if(get_line) {
168 line = play_tree_parser_get_line(p);
169 if(!line)
170 return NULL;
171 strstrip(line);
172 if(line[0] == '\0')
173 continue;
175 if(!comments) {
176 if(line[0] != '<') {
177 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"First char isn't '<' but '%c'\n",line[0]);
178 mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Buffer = [%s]\n",p->buffer);
179 return NULL;
180 } else if(strncmp(line,"<!--",4) == 0) { // Comments
181 comments = 1;
182 line += 4;
183 if(line[0] != '\0' && strlen(line) > 0)
184 get_line = 0;
185 } else if(strncasecmp(line,"<ASX",4) == 0) // We got an asx element
186 break;
187 else // We don't get an asx
188 return NULL;
189 } else { // Comments
190 char* c;
191 c = strchr(line,'-');
192 if(c) {
193 if (strncmp(c,"--!>",4) == 0) { // End of comments
194 comments = 0;
195 line = c+4;
196 if(line[0] != '\0') // There is some more data on this line : keep it
197 get_line = 0;
199 } else {
200 line = c+1; // Jump the -
201 if(line[0] != '\0') // Some more data
202 get_line = 0;
203 else // End of line
204 get_line = 1;
206 } else // No - on this line (or rest of line) : get next one
207 get_line = 1;
211 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected asx format\n");
213 // We have an asx : load it in memory and parse
215 while((line = play_tree_parser_get_line(p)) != NULL)
216 /* NOTHING */;
218 mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Parsing asx file: [%s]\n",p->buffer);
219 return asx_parser_build_tree(p->mconfig, p->buffer,p->deep);
222 static char*
223 pls_entry_get_value(char* line) {
224 char* i;
226 i = strchr(line,'=');
227 if(!i || i[1] == '\0')
228 return NULL;
229 else
230 return i+1;
233 typedef struct pls_entry {
234 char* file;
235 char* title;
236 char* length;
237 } pls_entry_t;
239 static int
240 pls_read_entry(char* line,pls_entry_t** _e,int* _max_entry,char** val) {
241 int num,max_entry = (*_max_entry);
242 pls_entry_t* e = (*_e);
243 char* v;
245 v = pls_entry_get_value(line);
246 if(!v) {
247 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
248 return 0;
251 num = atoi(line);
252 if(num < 0) {
253 num = max_entry+1;
254 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"No entry index in entry %s\nAssuming %d\n",line,num);
256 if(num > max_entry) {
257 e = (pls_entry_t*)realloc(e,num*sizeof(pls_entry_t));
258 memset(&e[max_entry],0,(num-max_entry)*sizeof(pls_entry_t));
259 max_entry = num;
261 (*_e) = e;
262 (*_max_entry) = max_entry;
263 (*val) = v;
265 return num;
269 static play_tree_t*
270 parse_pls(play_tree_parser_t* p) {
271 char *line,*v;
272 pls_entry_t* entries = NULL;
273 int n_entries = 0,max_entry=0,num;
274 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
276 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying Winamp playlist...\n");
277 while((line = play_tree_parser_get_line(p))) {
278 strstrip(line);
279 if(strlen(line))
280 break;
282 if (!line)
283 return NULL;
284 if(strcasecmp(line,"[playlist]"))
285 return NULL;
286 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected Winamp playlist format\n");
287 play_tree_parser_stop_keeping(p);
288 line = play_tree_parser_get_line(p);
289 if(!line)
290 return NULL;
291 strstrip(line);
292 if(strncasecmp(line,"NumberOfEntries",15) == 0) {
293 v = pls_entry_get_value(line);
294 n_entries = atoi(v);
295 if(n_entries < 0)
296 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Invalid number of entries: very funny!!!\n");
297 else
298 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Playlist claims to have %d entries. Let's see.\n",n_entries);
299 line = play_tree_parser_get_line(p);
302 while(line) {
303 strstrip(line);
304 if(line[0] == '\0') {
305 line = play_tree_parser_get_line(p);
306 continue;
308 if(strncasecmp(line,"File",4) == 0) {
309 num = pls_read_entry(line+4,&entries,&max_entry,&v);
310 if(num < 0)
311 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
312 else
313 entries[num-1].file = strdup(v);
314 } else if(strncasecmp(line,"Title",5) == 0) {
315 num = pls_read_entry(line+5,&entries,&max_entry,&v);
316 if(num < 0)
317 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
318 else
319 entries[num-1].title = strdup(v);
320 } else if(strncasecmp(line,"Length",6) == 0) {
321 num = pls_read_entry(line+6,&entries,&max_entry,&v);
322 if(num < 0)
323 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
324 else
325 entries[num-1].length = strdup(v);
326 } else
327 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Unknown entry type %s\n",line);
328 line = play_tree_parser_get_line(p);
331 for(num = 0; num < max_entry ; num++) {
332 if(entries[num].file == NULL)
333 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Entry %d don't have a file !!!!\n",num+1);
334 else {
335 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding entry %s\n",entries[num].file);
336 entry = play_tree_new();
337 play_tree_add_file(entry,entries[num].file);
338 free(entries[num].file);
339 if(list)
340 play_tree_append_entry(last_entry,entry);
341 else
342 list = entry;
343 last_entry = entry;
345 if(entries[num].title) {
346 // When we have info in playtree we add this info
347 free(entries[num].title);
349 if(entries[num].length) {
350 // When we have info in playtree we add this info
351 free(entries[num].length);
355 free(entries);
357 entry = play_tree_new();
358 play_tree_set_child(entry,list);
359 return entry;
363 Reference Ini-Format: Each entry is assumed a reference
365 static play_tree_t*
366 parse_ref_ini(play_tree_parser_t* p) {
367 char *line,*v;
368 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
370 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying reference-ini playlist...\n");
371 if (!(line = play_tree_parser_get_line(p)))
372 return NULL;
373 strstrip(line);
374 if(strcasecmp(line,"[Reference]"))
375 return NULL;
376 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected reference-ini playlist format\n");
377 play_tree_parser_stop_keeping(p);
378 line = play_tree_parser_get_line(p);
379 if(!line)
380 return NULL;
381 while(line) {
382 strstrip(line);
383 if(strncasecmp(line,"Ref",3) == 0) {
384 v = pls_entry_get_value(line+3);
385 if(!v)
386 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
387 else
389 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding entry %s\n",v);
390 entry = play_tree_new();
391 play_tree_add_file(entry,v);
392 if(list)
393 play_tree_append_entry(last_entry,entry);
394 else
395 list = entry;
396 last_entry = entry;
399 line = play_tree_parser_get_line(p);
402 if(!list) return NULL;
403 entry = play_tree_new();
404 play_tree_set_child(entry,list);
405 return entry;
408 static play_tree_t*
409 parse_m3u(play_tree_parser_t* p) {
410 char* line;
411 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
413 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying extended m3u playlist...\n");
414 if (!(line = play_tree_parser_get_line(p)))
415 return NULL;
416 strstrip(line);
417 if(strcasecmp(line,"#EXTM3U"))
418 return NULL;
419 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected extended m3u playlist format\n");
420 play_tree_parser_stop_keeping(p);
422 while((line = play_tree_parser_get_line(p)) != NULL) {
423 strstrip(line);
424 if(line[0] == '\0')
425 continue;
426 /* EXTM3U files contain such lines:
427 * #EXTINF:<seconds>, <title>
428 * followed by a line with the filename
429 * for now we have no place to put that
430 * so we just skip that extra-info ::atmos
432 if(line[0] == '#') {
433 #if 0 /* code functional */
434 if(strncasecmp(line,"#EXTINF:",8) == 0) {
435 mp_msg(MSGT_PLAYTREE,MSGL_INFO,"[M3U] Duration: %dsec Title: %s\n",
436 strtol(line+8,&line,10), line+2);
438 #endif
439 continue;
441 entry = play_tree_new();
442 play_tree_add_file(entry,line);
443 if(!list)
444 list = entry;
445 else
446 play_tree_append_entry(last_entry,entry);
447 last_entry = entry;
450 if(!list) return NULL;
451 entry = play_tree_new();
452 play_tree_set_child(entry,list);
453 return entry;
456 static play_tree_t*
457 parse_smil(play_tree_parser_t* p) {
458 int entrymode=0;
459 char* line,source[512],*pos,*s_start,*s_end,*src_line;
460 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
461 int is_rmsmil = 0;
462 unsigned int npkt, ttlpkt;
464 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying smil playlist...\n");
466 // Check if smil
467 while((line = play_tree_parser_get_line(p)) != NULL) {
468 strstrip(line);
469 if(line[0] == '\0') // Ignore empties
470 continue;
471 if (strncasecmp(line,"<?xml",5)==0) // smil in xml
472 continue;
473 if (strncasecmp(line,"<!DOCTYPE smil",13)==0) // smil in xml
474 continue;
475 if (strncasecmp(line,"<smil",5)==0 || strncasecmp(line,"<?wpl",5)==0 ||
476 strncasecmp(line,"(smil-document",14)==0)
477 break; // smil header found
478 else
479 return NULL; //line not smil exit
482 if (!line) return NULL;
483 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected smil playlist format\n");
484 play_tree_parser_stop_keeping(p);
486 if (strncasecmp(line,"(smil-document",14)==0) {
487 mp_msg(MSGT_PLAYTREE,MSGL_V,"Special smil-over-realrtsp playlist header\n");
488 is_rmsmil = 1;
489 if (sscanf(line, "(smil-document (ver 1.0)(npkt %u)(ttlpkt %u", &npkt, &ttlpkt) != 2) {
490 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: header parsing failure, assuming single packet.\n");
491 npkt = ttlpkt = 1;
493 if (ttlpkt == 0 || npkt > ttlpkt) {
494 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: bad packet counters (npkk = %u, ttlpkt = %u), assuming single packet.\n",
495 npkt, ttlpkt);
496 npkt = ttlpkt = 1;
500 //Get entries from smil
501 src_line = line;
502 line = NULL;
503 do {
504 strstrip(src_line);
505 if (line) {
506 free(line);
507 line = NULL;
509 /* If we're parsing smil over realrtsp and this is not the last packet and
510 * this is the last line in the packet (terminating with ") ) we must get
511 * the next line, strip the header, and concatenate it to the current line.
513 if (is_rmsmil && npkt != ttlpkt && strstr(src_line,"\")")) {
514 char *payload;
516 line = strdup(src_line);
517 if(!(src_line = play_tree_parser_get_line(p))) {
518 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: can't get line from packet %u/%u.\n", npkt, ttlpkt);
519 break;
521 strstrip(src_line);
522 // Skip header, packet starts after "
523 if(!(payload = strchr(src_line,'\"'))) {
524 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: can't find start of packet, using complete line.\n");
525 payload = src_line;
526 } else
527 payload++;
528 // Skip ") at the end of the last line from the current packet
529 line[strlen(line)-2] = 0;
530 line = realloc(line, strlen(line)+strlen(payload)+1);
531 strcat (line, payload);
532 npkt++;
533 } else
534 line = strdup(src_line);
535 /* Unescape \" to " for smil-over-rtsp */
536 if (is_rmsmil && line[0] != '\0') {
537 int i, j;
539 for (i = 0; i < strlen(line); i++)
540 if (line[i] == '\\' && line[i+1] == '"')
541 for (j = i; line[j]; j++)
542 line[j] = line[j+1];
544 pos = line;
545 while (pos) {
546 if (!entrymode) { // all entries filled so far
547 while ((pos=strchr(pos, '<'))) {
548 if (strncasecmp(pos,"<video",6)==0 || strncasecmp(pos,"<audio",6)==0 || strncasecmp(pos,"<media",6)==0) {
549 entrymode=1;
550 break; // Got a valid tag, exit '<' search loop
552 pos++;
555 if (entrymode) { //Entry found but not yet filled
556 pos = strstr(pos,"src="); // Is source present on this line
557 if (pos != NULL) {
558 entrymode=0;
559 if (pos[4] != '"' && pos[4] != '\'') {
560 mp_msg(MSGT_PLAYTREE,MSGL_V,"Unknown delimiter %c in source line %s\n", pos[4], line);
561 break;
563 s_start=pos+5;
564 s_end=strchr(s_start,pos[4]);
565 if (s_end == NULL) {
566 mp_msg(MSGT_PLAYTREE,MSGL_V,"Error parsing this source line %s\n",line);
567 break;
569 if (s_end-s_start> 511) {
570 mp_msg(MSGT_PLAYTREE,MSGL_V,"Cannot store such a large source %s\n",line);
571 break;
573 strncpy(source,s_start,s_end-s_start);
574 source[(s_end-s_start)]='\0'; // Null terminate
575 entry = play_tree_new();
576 play_tree_add_file(entry,source);
577 if(!list) //Insert new entry
578 list = entry;
579 else
580 play_tree_append_entry(last_entry,entry);
581 last_entry = entry;
582 pos = s_end;
586 } while((src_line = play_tree_parser_get_line(p)) != NULL);
588 if (line)
589 free(line);
591 if(!list) return NULL; // Nothing found
593 entry = play_tree_new();
594 play_tree_set_child(entry,list);
595 return entry;
598 static play_tree_t*
599 embedded_playlist_parse(struct m_config *mconfig, char *line) {
600 int f=DEMUXER_TYPE_PLAYLIST;
601 stream_t* stream;
602 play_tree_parser_t* ptp;
603 play_tree_t* entry;
605 // Get stream opened to link
606 stream=open_stream(line,0,&f);
607 if(!stream) {
608 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Can't open playlist %s\n",line);
609 return NULL;
612 //add new playtree
613 mp_msg(MSGT_PLAYTREE,MSGL_V,"Adding playlist %s to element entryref\n",line);
615 ptp = play_tree_parser_new(stream, mconfig, 1);
616 entry = play_tree_parser_get_play_tree(ptp, 1);
617 play_tree_parser_free(ptp);
618 free_stream(stream);
620 return entry;
623 static play_tree_t*
624 parse_textplain(play_tree_parser_t* p) {
625 char* line;
626 char *c;
627 int embedded;
628 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
630 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying plaintext playlist...\n");
631 play_tree_parser_stop_keeping(p);
633 while((line = play_tree_parser_get_line(p)) != NULL) {
634 strstrip(line);
635 if(line[0] == '\0' || line[0] == '#' || (line[0] == '/' && line[1] == '/'))
636 continue;
638 //Special check for embedded smil or ram reference in file
639 embedded = 0;
640 if (strlen(line) > 5)
641 for(c = line; c[0]; c++ )
642 if ( ((c[0] == '.') && //start with . and next have smil with optional ? or &
643 (tolower(c[1]) == 's') && (tolower(c[2])== 'm') &&
644 (tolower(c[3]) == 'i') && (tolower(c[4]) == 'l') &&
645 (!c[5] || c[5] == '?' || c[5] == '&')) || // or
646 ((c[0] == '.') && // start with . and next have smi or ram with optional ? or &
647 ( ((tolower(c[1]) == 's') && (tolower(c[2])== 'm') && (tolower(c[3]) == 'i')) ||
648 ((tolower(c[1]) == 'r') && (tolower(c[2])== 'a') && (tolower(c[3]) == 'm')) )
649 && (!c[4] || c[4] == '?' || c[4] == '&')) ){
650 entry=embedded_playlist_parse(p->mconfig, line);
651 embedded = 1;
652 break;
655 if (!embedded) { //regular file link
656 entry = play_tree_new();
657 play_tree_add_file(entry,line);
660 if (entry != NULL) {
661 if(!list)
662 list = entry;
663 else
664 play_tree_append_entry(last_entry,entry);
665 last_entry = entry;
669 if(!list) return NULL;
670 entry = play_tree_new();
671 play_tree_set_child(entry,list);
672 return entry;
675 play_tree_t*
676 parse_playtree(stream_t *stream, struct m_config *mconfig, int forced) {
677 play_tree_parser_t* p;
678 play_tree_t* ret;
680 #ifdef MP_DEBUG
681 assert(stream != NULL);
682 #endif
684 p = play_tree_parser_new(stream, mconfig, 0);
685 if(!p)
686 return NULL;
688 ret = play_tree_parser_get_play_tree(p, forced);
689 play_tree_parser_free(p);
691 return ret;
694 static void
695 play_tree_add_basepath(play_tree_t* pt, char* bp) {
696 int i,bl = strlen(bp),fl;
698 if(pt->child) {
699 play_tree_t* i;
700 for(i = pt->child ; i != NULL ; i = i->next)
701 play_tree_add_basepath(i,bp);
702 return;
705 if(!pt->files)
706 return;
708 for(i = 0 ; pt->files[i] != NULL ; i++) {
709 fl = strlen(pt->files[i]);
710 // if we find a full unix path, url:// or X:\ at the beginning,
711 // don't mangle it.
712 if(fl <= 0 || strstr(pt->files[i],"://") || (strstr(pt->files[i],":\\") == pt->files[i] + 1) || (pt->files[i][0] == '/') )
713 continue;
714 // if the path begins with \ then prepend drive letter to it.
715 if (pt->files[i][0] == '\\') {
716 if (pt->files[i][1] == '\\')
717 continue;
718 pt->files[i] = (char*)realloc(pt->files[i],2+fl+1);
719 memmove(pt->files[i] + 2,pt->files[i],fl+1);
720 memcpy(pt->files[i],bp,2);
721 continue;
723 pt->files[i] = (char*)realloc(pt->files[i],bl+fl+1);
724 memmove(pt->files[i] + bl,pt->files[i],fl+1);
725 memcpy(pt->files[i],bp,bl);
729 // Wrapper for play_tree_add_basepath (add base path from file)
730 void play_tree_add_bpf(play_tree_t* pt, char* filename)
732 char *ls, *file;
734 if (pt && filename)
736 file = strdup(filename);
737 if (file)
739 ls = strrchr(file,'/');
740 if(!ls) ls = strrchr(file,'\\');
741 if(ls) {
742 ls[1] = '\0';
743 play_tree_add_basepath(pt,file);
745 free(file);
750 play_tree_t*
751 parse_playlist_file(struct m_config *mconfig, char* file) {
752 stream_t *stream;
753 play_tree_t* ret;
754 int f=DEMUXER_TYPE_PLAYLIST;
756 stream = open_stream(file,0,&f);
758 if(!stream) {
759 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Error while opening playlist file %s: %s\n",file,strerror(errno));
760 return NULL;
763 mp_msg(MSGT_PLAYTREE,MSGL_V,"Parsing playlist file %s...\n",file);
765 ret = parse_playtree(stream, mconfig, 1);
766 free_stream(stream);
768 play_tree_add_bpf(ret, file);
770 return ret;
775 play_tree_parser_t*
776 play_tree_parser_new(stream_t* stream, struct m_config *mconfig, int deep) {
777 play_tree_parser_t* p;
779 p = calloc(1,sizeof(play_tree_parser_t));
780 if(!p)
781 return NULL;
782 p->stream = stream;
783 p->mconfig = mconfig;
784 p->deep = deep;
785 p->keep = 1;
787 return p;
791 void
792 play_tree_parser_free(play_tree_parser_t* p) {
794 #ifdef MP_DEBUG
795 assert(p != NULL);
796 #endif
798 if(p->buffer) free(p->buffer);
799 if(p->line) free(p->line);
800 free(p);
803 play_tree_t*
804 play_tree_parser_get_play_tree(play_tree_parser_t* p, int forced) {
805 play_tree_t* tree = NULL;
807 #ifdef MP_DEBUG
808 assert(p != NULL);
809 #endif
812 while(play_tree_parser_get_line(p) != NULL) {
813 play_tree_parser_reset(p);
815 tree = parse_asx(p);
816 if(tree) break;
817 play_tree_parser_reset(p);
819 tree = parse_pls(p);
820 if(tree) break;
821 play_tree_parser_reset(p);
823 tree = parse_m3u(p);
824 if(tree) break;
825 play_tree_parser_reset(p);
827 tree = parse_ref_ini(p);
828 if(tree) break;
829 play_tree_parser_reset(p);
831 tree = parse_smil(p);
832 if(tree) break;
833 play_tree_parser_reset(p);
835 // Here come the others formats ( textplain must stay the last one )
836 if (forced)
838 tree = parse_textplain(p);
839 if(tree) break;
841 break;
844 if(tree)
845 mp_msg(MSGT_PLAYTREE,MSGL_V,"Playlist successfully parsed\n");
846 else
847 mp_msg(MSGT_PLAYTREE,((forced==1)?MSGL_ERR:MSGL_V),"Error while parsing playlist\n");
849 if(tree)
850 tree = play_tree_cleanup(tree);
852 if(!tree) mp_msg(MSGT_PLAYTREE,((forced==1)?MSGL_WARN:MSGL_V),"Warning: empty playlist\n");
854 return tree;