natsuki.mplayerhq.hu -> rsync.mplayerhq.hu
[mplayer/glamo.git] / playtreeparser.c
blob50b424b9dc8410fc5e03da51b017056cbbb7aa02
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 "libmpdemux/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 = (char*)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';
82 end = strchr(p->iter,'\n');
83 if(!end) {
84 if(p->stream->eof) {
85 end = p->buffer + p->buffer_end;
86 break;
88 resize = 1;
89 continue;
91 break;
94 line_end = ((*(end-1)) == '\r') ? end-1 : end;
95 if(line_end - p->iter >= 0)
96 p->line = (char*)realloc(p->line,line_end - p->iter+1);
97 else
98 return NULL;
99 if(line_end - p->iter > 0)
100 strncpy(p->line,p->iter,line_end - p->iter);
101 p->line[line_end - p->iter] = '\0';
102 if(end[0] != '\0')
103 end++;
105 if(!p->keep) {
106 if(end[0] != '\0') {
107 p->buffer_end -= end-p->iter;
108 memmove(p->buffer,end,p->buffer_end);
109 p->buffer[p->buffer_end] = '\0';
110 } else
111 p->buffer_end = 0;
112 p->iter = p->buffer;
113 } else
114 p->iter = end;
116 return p->line;
119 static void
120 play_tree_parser_reset(play_tree_parser_t* p) {
121 p->iter = p->buffer;
124 static void
125 play_tree_parser_stop_keeping(play_tree_parser_t* p) {
126 p->keep = 0;
127 if(p->iter && p->iter != p->buffer) {
128 p->buffer_end -= p->iter -p->buffer;
129 if(p->buffer_end)
130 memmove(p->buffer,p->iter,p->buffer_end);
131 p->iter = p->buffer;
136 play_tree_t*
137 parse_asx(play_tree_parser_t* p) {
138 int comments = 0,get_line = 1;
139 char* line = NULL;
141 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying asx...\n");
143 while(1) {
144 if(get_line) {
145 line = play_tree_parser_get_line(p);
146 if(!line)
147 return NULL;
148 strstrip(line);
149 if(line[0] == '\0')
150 continue;
152 if(!comments) {
153 if(line[0] != '<') {
154 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"First char isn't '<' but '%c'\n",line[0]);
155 mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Buffer = [%s]\n",p->buffer);
156 return NULL;
157 } else if(strncmp(line,"<!--",4) == 0) { // Comments
158 comments = 1;
159 line += 4;
160 if(line[0] != '\0' && strlen(line) > 0)
161 get_line = 0;
162 } else if(strncasecmp(line,"<ASX",4) == 0) // We got an asx element
163 break;
164 else // We don't get an asx
165 return NULL;
166 } else { // Comments
167 char* c;
168 c = strchr(line,'-');
169 if(c) {
170 if (strncmp(c,"--!>",4) == 0) { // End of comments
171 comments = 0;
172 line = c+4;
173 if(line[0] != '\0') // There is some more data on this line : keep it
174 get_line = 0;
176 } else {
177 line = c+1; // Jump the -
178 if(line[0] != '\0') // Some more data
179 get_line = 0;
180 else // End of line
181 get_line = 1;
183 } else // No - on this line (or rest of line) : get next one
184 get_line = 1;
188 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected asx format\n");
190 // We have an asx : load it in memory and parse
192 while((line = play_tree_parser_get_line(p)) != NULL)
193 /* NOTHING */;
195 mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Parsing asx file: [%s]\n",p->buffer);
196 return asx_parser_build_tree(p->buffer,p->deep);
199 static char*
200 pls_entry_get_value(char* line) {
201 char* i;
203 i = strchr(line,'=');
204 if(!i || i[1] == '\0')
205 return NULL;
206 else
207 return i+1;
210 typedef struct pls_entry {
211 char* file;
212 char* title;
213 char* length;
214 } pls_entry_t;
216 static int
217 pls_read_entry(char* line,pls_entry_t** _e,int* _max_entry,char** val) {
218 int num,max_entry = (*_max_entry);
219 pls_entry_t* e = (*_e);
220 char* v;
222 v = pls_entry_get_value(line);
223 if(!v) {
224 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
225 return 0;
228 num = atoi(line);
229 if(num < 0) {
230 num = max_entry+1;
231 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"No entry index in entry %s\nAssuming %d\n",line,num);
233 if(num > max_entry) {
234 e = (pls_entry_t*)realloc(e,num*sizeof(pls_entry_t));
235 memset(&e[max_entry],0,(num-max_entry)*sizeof(pls_entry_t));
236 max_entry = num;
238 (*_e) = e;
239 (*_max_entry) = max_entry;
240 (*val) = v;
242 return num;
246 play_tree_t*
247 parse_pls(play_tree_parser_t* p) {
248 char *line,*v;
249 pls_entry_t* entries = NULL;
250 int n_entries = 0,max_entry=0,num;
251 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
253 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying Winamp playlist...\n");
254 if (!(line = play_tree_parser_get_line(p)))
255 return NULL;
256 strstrip(line);
257 if(strcasecmp(line,"[playlist]"))
258 return NULL;
259 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected Winamp playlist format\n");
260 play_tree_parser_stop_keeping(p);
261 line = play_tree_parser_get_line(p);
262 if(!line)
263 return NULL;
264 strstrip(line);
265 if(strncasecmp(line,"NumberOfEntries",15) == 0) {
266 v = pls_entry_get_value(line);
267 n_entries = atoi(v);
268 if(n_entries < 0)
269 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Invalid number of entries: very funny!!!\n");
270 else
271 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Playlist claims to have %d entries. Let's see.\n",n_entries);
272 line = play_tree_parser_get_line(p);
275 while(line) {
276 strstrip(line);
277 if(line[0] == '\0') {
278 line = play_tree_parser_get_line(p);
279 continue;
281 if(strncasecmp(line,"File",4) == 0) {
282 num = pls_read_entry(line+4,&entries,&max_entry,&v);
283 if(num < 0)
284 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
285 else
286 entries[num-1].file = strdup(v);
287 } else if(strncasecmp(line,"Title",5) == 0) {
288 num = pls_read_entry(line+5,&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].title = strdup(v);
293 } else if(strncasecmp(line,"Length",6) == 0) {
294 num = pls_read_entry(line+6,&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].length = strdup(v);
299 } else
300 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Unknown entry type %s\n",line);
301 line = play_tree_parser_get_line(p);
304 for(num = 0; num < max_entry ; num++) {
305 if(entries[num].file == NULL)
306 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Entry %d don't have a file !!!!\n",num+1);
307 else {
308 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding entry %s\n",entries[num].file);
309 entry = play_tree_new();
310 play_tree_add_file(entry,entries[num].file);
311 free(entries[num].file);
312 if(list)
313 play_tree_append_entry(last_entry,entry);
314 else
315 list = entry;
316 last_entry = entry;
318 if(entries[num].title) {
319 // When we have info in playtree we add this info
320 free(entries[num].title);
322 if(entries[num].length) {
323 // When we have info in playtree we add this info
324 free(entries[num].length);
328 free(entries);
330 entry = play_tree_new();
331 play_tree_set_child(entry,list);
332 return entry;
336 Reference Ini-Format: Each entry is assumed a reference
338 play_tree_t*
339 parse_ref_ini(play_tree_parser_t* p) {
340 char *line,*v;
341 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
343 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying reference-ini playlist...\n");
344 if (!(line = play_tree_parser_get_line(p)))
345 return NULL;
346 strstrip(line);
347 if(strcasecmp(line,"[Reference]"))
348 return NULL;
349 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected reference-ini playlist format\n");
350 play_tree_parser_stop_keeping(p);
351 line = play_tree_parser_get_line(p);
352 if(!line)
353 return NULL;
354 while(line) {
355 strstrip(line);
356 if(strncasecmp(line,"Ref",3) == 0) {
357 v = pls_entry_get_value(line+3);
358 if(!v)
359 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
360 else
362 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding entry %s\n",v);
363 entry = play_tree_new();
364 play_tree_add_file(entry,v);
365 if(list)
366 play_tree_append_entry(last_entry,entry);
367 else
368 list = entry;
369 last_entry = entry;
372 line = play_tree_parser_get_line(p);
375 if(!list) return NULL;
376 entry = play_tree_new();
377 play_tree_set_child(entry,list);
378 return entry;
381 play_tree_t*
382 parse_m3u(play_tree_parser_t* p) {
383 char* line;
384 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
386 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying extended m3u playlist...\n");
387 if (!(line = play_tree_parser_get_line(p)))
388 return NULL;
389 strstrip(line);
390 if(strcasecmp(line,"#EXTM3U"))
391 return NULL;
392 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected extended m3u playlist format\n");
393 play_tree_parser_stop_keeping(p);
395 while((line = play_tree_parser_get_line(p)) != NULL) {
396 strstrip(line);
397 if(line[0] == '\0')
398 continue;
399 /* EXTM3U files contain such lines:
400 * #EXTINF:<seconds>, <title>
401 * followed by a line with the filename
402 * for now we have no place to put that
403 * so we just skip that extra-info ::atmos
405 if(line[0] == '#') {
406 #if 0 /* code functional */
407 if(strncasecmp(line,"#EXTINF:",8) == 0) {
408 mp_msg(MSGT_PLAYTREE,MSGL_INFO,"[M3U] Duration: %dsec Title: %s\n",
409 strtol(line+8,&line,10), line+2);
411 #endif
412 continue;
414 entry = play_tree_new();
415 play_tree_add_file(entry,line);
416 if(!list)
417 list = entry;
418 else
419 play_tree_append_entry(last_entry,entry);
420 last_entry = entry;
423 if(!list) return NULL;
424 entry = play_tree_new();
425 play_tree_set_child(entry,list);
426 return entry;
429 play_tree_t*
430 parse_smil(play_tree_parser_t* p) {
431 int entrymode=0;
432 char* line,source[512],*pos,*s_start,*s_end;
433 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
435 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying smil playlist...\n");
437 // Check if smil
438 while((line = play_tree_parser_get_line(p)) != NULL) {
439 strstrip(line);
440 if(line[0] == '\0') // Ignore empties
441 continue;
442 if (strncasecmp(line,"<smil",5)==0 || strncasecmp(line,"<?wpl",5)==0)
443 break; // smil header found
444 else
445 return NULL; //line not smil exit
448 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected smil playlist format\n");
449 play_tree_parser_stop_keeping(p);
452 //Get entries from smil
453 while((line = play_tree_parser_get_line(p)) != NULL) {
454 strstrip(line);
455 if (line[0]=='\0')
456 continue;
457 if (!entrymode) { // all entries filled so far
458 if (strncasecmp(line,"<video",6)==0 || strncasecmp(line,"<audio",6)==0 || strncasecmp(line,"<media",6)) {
459 pos=strstr(line,"src="); // Is source present on this line
460 if (pos !=NULL) {
461 s_start=pos+5;
462 s_end=strchr(s_start,'"');
463 if (s_end == NULL) {
464 mp_msg(MSGT_PLAYTREE,MSGL_V,"Error parsing this source line %s\n",line);
465 continue;
467 if (s_end-s_start> 511) {
468 mp_msg(MSGT_PLAYTREE,MSGL_V,"Cannot store such a large source %s\n",line);
469 continue;
471 strncpy(source,s_start,s_end-s_start);
472 source[(s_end-s_start)]='\0'; // Null terminate
473 entry = play_tree_new();
474 play_tree_add_file(entry,source);
475 if(!list) //Insert new entry
476 list = entry;
477 else
478 play_tree_append_entry(last_entry,entry);
479 last_entry = entry;
480 } else {
481 entrymode=1;
484 } else { //Entry found but not yet filled
485 pos = strstr(line,"src="); // Is source present on this line
486 if (pos != NULL) {
487 entrymode=0;
488 s_start=pos+5;
489 s_end=strchr(s_start,'"');
490 if (s_end == NULL) {
491 mp_msg(MSGT_PLAYTREE,MSGL_V,"Error parsing this source line %s\n",line);
492 continue;
494 if (s_end-s_start> 511) {
495 mp_msg(MSGT_PLAYTREE,MSGL_V,"Cannot store such a large source %s\n",line);
496 continue;
498 strncpy(source,s_start,s_end-s_start);
499 source[(s_end-s_start)]='\0'; // Null terminate
500 entry = play_tree_new();
501 play_tree_add_file(entry,source);
502 if(!list) //Insert new entry
503 list = entry;
504 else
505 play_tree_append_entry(last_entry,entry);
506 last_entry = entry;
511 if(!list) return NULL; // Nothing found
513 entry = play_tree_new();
514 play_tree_set_child(entry,list);
515 return entry;
518 play_tree_t*
519 embedded_playlist_parse(char *line) {
520 int f=DEMUXER_TYPE_PLAYLIST;
521 stream_t* stream;
522 play_tree_parser_t* ptp;
523 play_tree_t* entry;
525 // Get stream opened to link
526 stream=open_stream(line,0,&f);
527 if(!stream) {
528 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Can't open playlist %s\n",line);
529 return NULL;
532 //add new playtree
533 mp_msg(MSGT_PLAYTREE,MSGL_V,"Adding playlist %s to element entryref\n",line);
535 ptp = play_tree_parser_new(stream,1);
536 entry = play_tree_parser_get_play_tree(ptp, 1);
537 play_tree_parser_free(ptp);
538 free_stream(stream);
540 return entry;
543 play_tree_t*
544 parse_textplain(play_tree_parser_t* p) {
545 char* line;
546 char *c;
547 int embedded;
548 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
550 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying plaintext playlist...\n");
551 play_tree_parser_stop_keeping(p);
553 while((line = play_tree_parser_get_line(p)) != NULL) {
554 strstrip(line);
555 if(line[0] == '\0' || line[0] == '#' || (line[0] == '/' && line[1] == '/'))
556 continue;
558 //Special check for embedded smil or ram reference in file
559 embedded = 0;
560 if (strlen(line) > 5)
561 for(c = line; c[0]; c++ )
562 if ( ((c[0] == '.') && //start with . and next have smil with optional ? or &
563 (tolower(c[1]) == 's') && (tolower(c[2])== 'm') &&
564 (tolower(c[3]) == 'i') && (tolower(c[4]) == 'l') &&
565 (!c[5] || c[5] == '?' || c[5] == '&')) || // or
566 ((c[0] == '.') && // start with . and next have smi or ram with optional ? or &
567 ( ((tolower(c[1]) == 's') && (tolower(c[2])== 'm') && (tolower(c[3]) == 'i')) ||
568 ((tolower(c[1]) == 'r') && (tolower(c[2])== 'a') && (tolower(c[3]) == 'm')) )
569 && (!c[4] || c[4] == '?' || c[4] == '&')) ){
570 entry=embedded_playlist_parse(line);
571 embedded = 1;
572 break;
575 if (!embedded) { //regular file link
576 entry = play_tree_new();
577 play_tree_add_file(entry,line);
580 if (entry != NULL) {
581 if(!list)
582 list = entry;
583 else
584 play_tree_append_entry(last_entry,entry);
585 last_entry = entry;
589 if(!list) return NULL;
590 entry = play_tree_new();
591 play_tree_set_child(entry,list);
592 return entry;
595 play_tree_t*
596 parse_playtree(stream_t *stream, int forced) {
597 play_tree_parser_t* p;
598 play_tree_t* ret;
600 #ifdef MP_DEBUG
601 assert(stream != NULL);
602 #endif
604 p = play_tree_parser_new(stream,0);
605 if(!p)
606 return NULL;
608 ret = play_tree_parser_get_play_tree(p, forced);
609 play_tree_parser_free(p);
611 return ret;
614 static void
615 play_tree_add_basepath(play_tree_t* pt, char* bp) {
616 int i,bl = strlen(bp),fl;
618 if(pt->child) {
619 play_tree_t* i;
620 for(i = pt->child ; i != NULL ; i = i->next)
621 play_tree_add_basepath(i,bp);
622 return;
625 if(!pt->files)
626 return;
628 for(i = 0 ; pt->files[i] != NULL ; i++) {
629 fl = strlen(pt->files[i]);
630 // if we find a full unix path, url:// or X:\ at the beginning,
631 // don't mangle it.
632 if(fl <= 0 || strstr(pt->files[i],"://") || (strstr(pt->files[i],":\\") == pt->files[i] + 1) || (pt->files[i][0] == '/') )
633 continue;
634 // if the path begins with \ then prepend drive letter to it.
635 if (pt->files[i][0] == '\\') {
636 pt->files[i] = (char*)realloc(pt->files[i],2+fl+1);
637 memmove(pt->files[i] + 2,pt->files[i],fl+1);
638 memcpy(pt->files[i],bp,2);
639 return;
641 pt->files[i] = (char*)realloc(pt->files[i],bl+fl+1);
642 memmove(pt->files[i] + bl,pt->files[i],fl+1);
643 memcpy(pt->files[i],bp,bl);
647 // Wrapper for play_tree_add_basepath (add base path from file)
648 void play_tree_add_bpf(play_tree_t* pt, char* filename)
650 char *ls, *file;
652 if (pt && filename)
654 file = strdup(filename);
655 if (file)
657 ls = strrchr(file,'/');
658 if(!ls) ls = strrchr(file,'\\');
659 if(ls) {
660 ls[1] = '\0';
661 play_tree_add_basepath(pt,file);
663 free(file);
668 play_tree_t*
669 parse_playlist_file(char* file) {
670 stream_t *stream;
671 play_tree_t* ret;
672 int f=DEMUXER_TYPE_PLAYLIST;
674 stream = open_stream(file,0,&f);
676 if(!stream) {
677 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Error while opening playlist file %s: %s\n",file,strerror(errno));
678 return NULL;
681 mp_msg(MSGT_PLAYTREE,MSGL_V,"Parsing playlist file %s...\n",file);
683 ret = parse_playtree(stream,1);
684 free_stream(stream);
686 play_tree_add_bpf(ret, file);
688 return ret;
693 play_tree_parser_t*
694 play_tree_parser_new(stream_t* stream,int deep) {
695 play_tree_parser_t* p;
697 p = (play_tree_parser_t*)calloc(1,sizeof(play_tree_parser_t));
698 if(!p)
699 return NULL;
700 p->stream = stream;
701 p->deep = deep;
702 p->keep = 1;
704 return p;
708 void
709 play_tree_parser_free(play_tree_parser_t* p) {
711 #ifdef MP_DEBUG
712 assert(p != NULL);
713 #endif
715 if(p->buffer) free(p->buffer);
716 if(p->line) free(p->line);
717 free(p);
720 play_tree_t*
721 play_tree_parser_get_play_tree(play_tree_parser_t* p, int forced) {
722 play_tree_t* tree = NULL;
724 #ifdef MP_DEBUG
725 assert(p != NULL);
726 #endif
729 while(play_tree_parser_get_line(p) != NULL) {
730 play_tree_parser_reset(p);
732 tree = parse_asx(p);
733 if(tree) break;
734 play_tree_parser_reset(p);
736 tree = parse_pls(p);
737 if(tree) break;
738 play_tree_parser_reset(p);
740 tree = parse_m3u(p);
741 if(tree) break;
742 play_tree_parser_reset(p);
744 tree = parse_ref_ini(p);
745 if(tree) break;
746 play_tree_parser_reset(p);
748 tree = parse_smil(p);
749 if(tree) break;
750 play_tree_parser_reset(p);
752 // Here come the others formats ( textplain must stay the last one )
753 if (forced)
755 tree = parse_textplain(p);
756 if(tree) break;
758 break;
761 if(tree)
762 mp_msg(MSGT_PLAYTREE,MSGL_V,"Playlist successfully parsed\n");
763 else
764 mp_msg(MSGT_PLAYTREE,((forced==1)?MSGL_ERR:MSGL_V),"Error while parsing playlist\n");
766 if(tree)
767 tree = play_tree_cleanup(tree);
769 if(!tree) mp_msg(MSGT_PLAYTREE,((forced==1)?MSGL_WARN:MSGL_V),"Warning: empty playlist\n");
771 return tree;