adding the code documentation guide lines
[mplayer/glamo.git] / playtreeparser.c
blob153d1948cff2a19f42c2c2c021fb22075d38a5aa
2 #include "config.h"
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #ifdef MP_DEBUG
7 #include <assert.h>
8 #endif
9 #include <errno.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14 #include <ctype.h>
15 #include "m_config.h"
16 #include "playtree.h"
17 #include "playtreeparser.h"
18 #include "libmpdemux/stream.h"
19 #include "libmpdemux/demuxer.h"
20 #include "mp_msg.h"
23 extern play_tree_t*
24 asx_parser_build_tree(char* buffer, int ref);
26 #define BUF_STEP 1024
28 #define WHITES " \n\r\t"
30 static void
31 strstrip(char* str) {
32 char* i;
34 if (str==NULL)
35 return;
36 for(i = str ; i[0] != '\0' && strchr(WHITES,i[0]) != NULL; i++)
37 /* NOTHING */;
38 if(i[0] != '\0') {
39 memmove(str,i,strlen(i) + 1);
40 for(i = str + strlen(str) - 1 ; strchr(WHITES,i[0]) != NULL; i--)
41 /* NOTHING */;
42 i[1] = '\0';
43 } else
44 str[0] = '\0';
47 static char*
48 play_tree_parser_get_line(play_tree_parser_t* p) {
49 char *end,*line_end;
50 int r,resize = 0;
52 if(p->buffer == NULL) {
53 p->buffer = (char*)malloc(BUF_STEP);
54 p->buffer_size = BUF_STEP;
55 p->iter = p->buffer;
58 if(p->stream->eof && (p->buffer_end == 0 || p->iter[0] == '\0'))
59 return NULL;
61 while(1) {
63 if(resize) {
64 r = p->iter - p->buffer;
65 p->buffer = (char*)realloc(p->buffer,p->buffer_size+BUF_STEP);
66 p->iter = p->buffer + r;
67 p->buffer_size += BUF_STEP;
68 resize = 0;
71 if(p->buffer_size - p->buffer_end > 1 && ! p->stream->eof) {
72 r = stream_read(p->stream,p->buffer + p->buffer_end,p->buffer_size - p->buffer_end - 1);
73 if(r > 0) {
74 p->buffer_end += r;
75 p->buffer[p->buffer_end] = '\0';
79 end = strchr(p->iter,'\n');
80 if(!end) {
81 if(p->stream->eof) {
82 end = p->buffer + p->buffer_end;
83 break;
85 resize = 1;
86 continue;
88 break;
91 line_end = ((*(end-1)) == '\r') ? end-1 : end;
92 if(line_end - p->iter >= 0)
93 p->line = (char*)realloc(p->line,line_end - p->iter+1);
94 else
95 return NULL;
96 if(line_end - p->iter > 0)
97 strncpy(p->line,p->iter,line_end - p->iter);
98 p->line[line_end - p->iter] = '\0';
99 if(end[0] != '\0')
100 end++;
102 if(!p->keep) {
103 if(end[0] != '\0') {
104 p->buffer_end -= end-p->iter;
105 memmove(p->buffer,end,p->buffer_end);
106 p->buffer[p->buffer_end] = '\0';
107 } else
108 p->buffer_end = 0;
109 p->iter = p->buffer;
110 } else
111 p->iter = end;
113 return p->line;
116 static void
117 play_tree_parser_reset(play_tree_parser_t* p) {
118 p->iter = p->buffer;
121 static void
122 play_tree_parser_stop_keeping(play_tree_parser_t* p) {
123 p->keep = 0;
124 if(p->iter && p->iter != p->buffer) {
125 p->buffer_end -= p->iter -p->buffer;
126 if(p->buffer_end)
127 memmove(p->buffer,p->iter,p->buffer_end);
128 p->iter = p->buffer;
133 play_tree_t*
134 parse_asx(play_tree_parser_t* p) {
135 int comments = 0,get_line = 1;
136 char* line = NULL;
138 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying asx...\n");
140 while(1) {
141 if(get_line) {
142 line = play_tree_parser_get_line(p);
143 if(!line)
144 return NULL;
145 strstrip(line);
146 if(line[0] == '\0')
147 continue;
149 if(!comments) {
150 if(line[0] != '<') {
151 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"First char isn't '<' but '%c'\n",line[0]);
152 mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Buffer = [%s]\n",p->buffer);
153 return NULL;
154 } else if(strncmp(line,"<!--",4) == 0) { // Comments
155 comments = 1;
156 line += 4;
157 if(line[0] != '\0' && strlen(line) > 0)
158 get_line = 0;
159 } else if(strncasecmp(line,"<ASX",4) == 0) // We got an asx element
160 break;
161 else // We don't get an asx
162 return NULL;
163 } else { // Comments
164 char* c;
165 c = strchr(line,'-');
166 if(c) {
167 if (strncmp(c,"--!>",4) == 0) { // End of comments
168 comments = 0;
169 line = c+4;
170 if(line[0] != '\0') // There is some more data on this line : keep it
171 get_line = 0;
173 } else {
174 line = c+1; // Jump the -
175 if(line[0] != '\0') // Some more data
176 get_line = 0;
177 else // End of line
178 get_line = 1;
180 } else // No - on this line (or rest of line) : get next one
181 get_line = 1;
185 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected asx format\n");
187 // We have an asx : load it in memory and parse
189 while((line = play_tree_parser_get_line(p)) != NULL)
190 /* NOTHING */;
192 mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Parsing asx file: [%s]\n",p->buffer);
193 return asx_parser_build_tree(p->buffer,p->deep);
196 static char*
197 pls_entry_get_value(char* line) {
198 char* i;
200 i = strchr(line,'=');
201 if(!i || i[1] == '\0')
202 return NULL;
203 else
204 return i+1;
207 typedef struct pls_entry {
208 char* file;
209 char* title;
210 char* length;
211 } pls_entry_t;
213 static int
214 pls_read_entry(char* line,pls_entry_t** _e,int* _max_entry,char** val) {
215 int num,max_entry = (*_max_entry);
216 pls_entry_t* e = (*_e);
217 char* v;
219 v = pls_entry_get_value(line);
220 if(!v) {
221 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
222 return 0;
225 num = atoi(line);
226 if(num < 0) {
227 num = max_entry+1;
228 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"No entry index in entry %s\nAssuming %d\n",line,num);
230 if(num > max_entry) {
231 e = (pls_entry_t*)realloc(e,num*sizeof(pls_entry_t));
232 memset(&e[max_entry],0,(num-max_entry)*sizeof(pls_entry_t));
233 max_entry = num;
235 (*_e) = e;
236 (*_max_entry) = max_entry;
237 (*val) = v;
239 return num;
243 play_tree_t*
244 parse_pls(play_tree_parser_t* p) {
245 char *line,*v;
246 pls_entry_t* entries = NULL;
247 int n_entries = 0,max_entry=0,num;
248 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
250 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying Winamp playlist...\n");
251 if (!(line = play_tree_parser_get_line(p)))
252 return NULL;
253 strstrip(line);
254 if(strcasecmp(line,"[playlist]"))
255 return NULL;
256 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected Winamp playlist format\n");
257 play_tree_parser_stop_keeping(p);
258 line = play_tree_parser_get_line(p);
259 if(!line)
260 return NULL;
261 strstrip(line);
262 if(strncasecmp(line,"NumberOfEntries",15) == 0) {
263 v = pls_entry_get_value(line);
264 n_entries = atoi(v);
265 if(n_entries < 0)
266 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Invalid number of entries: very funny!!!\n");
267 else
268 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Playlist claims to have %d entries. Let's see.\n",n_entries);
269 line = play_tree_parser_get_line(p);
272 while(line) {
273 strstrip(line);
274 if(line[0] == '\0') {
275 line = play_tree_parser_get_line(p);
276 continue;
278 if(strncasecmp(line,"File",4) == 0) {
279 num = pls_read_entry(line+4,&entries,&max_entry,&v);
280 if(num < 0)
281 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
282 else
283 entries[num-1].file = strdup(v);
284 } else if(strncasecmp(line,"Title",5) == 0) {
285 num = pls_read_entry(line+5,&entries,&max_entry,&v);
286 if(num < 0)
287 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
288 else
289 entries[num-1].title = strdup(v);
290 } else if(strncasecmp(line,"Length",6) == 0) {
291 num = pls_read_entry(line+6,&entries,&max_entry,&v);
292 if(num < 0)
293 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
294 else
295 entries[num-1].length = strdup(v);
296 } else
297 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Unknown entry type %s\n",line);
298 line = play_tree_parser_get_line(p);
301 for(num = 0; num < max_entry ; num++) {
302 if(entries[num].file == NULL)
303 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Entry %d don't have a file !!!!\n",num+1);
304 else {
305 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding entry %s\n",entries[num].file);
306 entry = play_tree_new();
307 play_tree_add_file(entry,entries[num].file);
308 free(entries[num].file);
309 if(list)
310 play_tree_append_entry(last_entry,entry);
311 else
312 list = entry;
313 last_entry = entry;
315 if(entries[num].title) {
316 // When we have info in playtree we add this info
317 free(entries[num].title);
319 if(entries[num].length) {
320 // When we have info in playtree we add this info
321 free(entries[num].length);
325 free(entries);
327 entry = play_tree_new();
328 play_tree_set_child(entry,list);
329 return entry;
333 Reference Ini-Format: Each entry is assumed a reference
335 play_tree_t*
336 parse_ref_ini(play_tree_parser_t* p) {
337 char *line,*v;
338 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
340 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying reference-ini playlist...\n");
341 if (!(line = play_tree_parser_get_line(p)))
342 return NULL;
343 strstrip(line);
344 if(strcasecmp(line,"[Reference]"))
345 return NULL;
346 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected reference-ini playlist format\n");
347 play_tree_parser_stop_keeping(p);
348 line = play_tree_parser_get_line(p);
349 if(!line)
350 return NULL;
351 while(line) {
352 strstrip(line);
353 if(strncasecmp(line,"Ref",3) == 0) {
354 v = pls_entry_get_value(line+3);
355 if(!v)
356 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
357 else
359 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding entry %s\n",v);
360 entry = play_tree_new();
361 play_tree_add_file(entry,v);
362 if(list)
363 play_tree_append_entry(last_entry,entry);
364 else
365 list = entry;
366 last_entry = entry;
369 line = play_tree_parser_get_line(p);
372 if(!list) return NULL;
373 entry = play_tree_new();
374 play_tree_set_child(entry,list);
375 return entry;
378 play_tree_t*
379 parse_m3u(play_tree_parser_t* p) {
380 char* line;
381 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
383 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying extended m3u playlist...\n");
384 if (!(line = play_tree_parser_get_line(p)))
385 return NULL;
386 strstrip(line);
387 if(strcasecmp(line,"#EXTM3U"))
388 return NULL;
389 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected extended m3u playlist format\n");
390 play_tree_parser_stop_keeping(p);
392 while((line = play_tree_parser_get_line(p)) != NULL) {
393 strstrip(line);
394 if(line[0] == '\0')
395 continue;
396 /* EXTM3U files contain such lines:
397 * #EXTINF:<seconds>, <title>
398 * followed by a line with the filename
399 * for now we have no place to put that
400 * so we just skip that extra-info ::atmos
402 if(line[0] == '#') {
403 #if 0 /* code functional */
404 if(strncasecmp(line,"#EXTINF:",8) == 0) {
405 mp_msg(MSGT_PLAYTREE,MSGL_INFO,"[M3U] Duration: %dsec Title: %s\n",
406 strtol(line+8,&line,10), line+2);
408 #endif
409 continue;
411 entry = play_tree_new();
412 play_tree_add_file(entry,line);
413 if(!list)
414 list = entry;
415 else
416 play_tree_append_entry(last_entry,entry);
417 last_entry = entry;
420 if(!list) return NULL;
421 entry = play_tree_new();
422 play_tree_set_child(entry,list);
423 return entry;
426 play_tree_t*
427 parse_smil(play_tree_parser_t* p) {
428 int entrymode=0;
429 char* line,source[512],*pos,*s_start,*s_end;
430 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
432 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying smil playlist...\n");
434 // Check if smil
435 while((line = play_tree_parser_get_line(p)) != NULL) {
436 strstrip(line);
437 if(line[0] == '\0') // Ignore empties
438 continue;
439 if (strncasecmp(line,"<smil",5)==0)
440 break; // smil header found
441 else
442 return NULL; //line not smil exit
445 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected smil playlist format\n");
446 play_tree_parser_stop_keeping(p);
449 //Get entries from smil
450 while((line = play_tree_parser_get_line(p)) != NULL) {
451 strstrip(line);
452 if (line[0]=='\0')
453 continue;
454 if (!entrymode) { // all entries filled so far
455 if (strncasecmp(line,"<video",6)==0 || strncasecmp(line,"<audio",6)==0) {
456 pos=strstr(line,"src="); // Is source present on this line
457 if (pos !=NULL) {
458 s_start=pos+5;
459 s_end=strchr(s_start,'"');
460 if (s_end == NULL) {
461 mp_msg(MSGT_PLAYTREE,MSGL_V,"Error parsing this source line %s\n",line);
462 continue;
464 if (s_end-s_start> 511) {
465 mp_msg(MSGT_PLAYTREE,MSGL_V,"Cannot store such a large source %s\n",line);
466 continue;
468 strncpy(source,s_start,s_end-s_start);
469 source[(s_end-s_start)]='\0'; // Null terminate
470 entry = play_tree_new();
471 play_tree_add_file(entry,source);
472 if(!list) //Insert new entry
473 list = entry;
474 else
475 play_tree_append_entry(last_entry,entry);
476 last_entry = entry;
477 } else {
478 entrymode=1;
481 } else { //Entry found but not yet filled
482 pos = strstr(line,"src="); // Is source present on this line
483 if (pos != NULL) {
484 entrymode=0;
485 s_start=pos+5;
486 s_end=strchr(s_start,'"');
487 if (s_end == NULL) {
488 mp_msg(MSGT_PLAYTREE,MSGL_V,"Error parsing this source line %s\n",line);
489 continue;
491 if (s_end-s_start> 511) {
492 mp_msg(MSGT_PLAYTREE,MSGL_V,"Cannot store such a large source %s\n",line);
493 continue;
495 strncpy(source,s_start,s_end-s_start);
496 source[(s_end-s_start)]='\0'; // Null terminate
497 entry = play_tree_new();
498 play_tree_add_file(entry,source);
499 if(!list) //Insert new entry
500 list = entry;
501 else
502 play_tree_append_entry(last_entry,entry);
503 last_entry = entry;
508 if(!list) return NULL; // Nothing found
510 entry = play_tree_new();
511 play_tree_set_child(entry,list);
512 return entry;
515 play_tree_t*
516 embedded_playlist_parse(char *line) {
517 int f=DEMUXER_TYPE_PLAYLIST;
518 stream_t* stream;
519 play_tree_parser_t* ptp;
520 play_tree_t* entry;
522 // Get stream opened to link
523 stream=open_stream(line,0,&f);
524 if(!stream) {
525 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Can't open playlist %s\n",line);
526 return NULL;
529 //add new playtree
530 mp_msg(MSGT_PLAYTREE,MSGL_V,"Adding playlist %s to element entryref\n",line);
532 ptp = play_tree_parser_new(stream,1);
533 entry = play_tree_parser_get_play_tree(ptp, 1);
534 play_tree_parser_free(ptp);
535 free_stream(stream);
537 return entry;
540 play_tree_t*
541 parse_textplain(play_tree_parser_t* p) {
542 char* line;
543 char *c;
544 int embedded;
545 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
547 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying plaintext playlist...\n");
548 play_tree_parser_stop_keeping(p);
550 while((line = play_tree_parser_get_line(p)) != NULL) {
551 strstrip(line);
552 if(line[0] == '\0' || line[0] == '#' || (line[0] == '/' && line[1] == '/'))
553 continue;
555 //Special check for embedded smil or ram reference in file
556 embedded = 0;
557 if (strlen(line) > 5)
558 for(c = line; c[0]; c++ )
559 if ( ((c[0] == '.') && //start with . and next have smil with optional ? or &
560 (tolower(c[1]) == 's') && (tolower(c[2])== 'm') &&
561 (tolower(c[3]) == 'i') && (tolower(c[4]) == 'l') &&
562 (!c[5] || c[5] == '?' || c[5] == '&')) || // or
563 ((c[0] == '.') && // start with . and next have smi or ram with optional ? or &
564 ( ((tolower(c[1]) == 's') && (tolower(c[2])== 'm') && (tolower(c[3]) == 'i')) ||
565 ((tolower(c[1]) == 'r') && (tolower(c[2])== 'a') && (tolower(c[3]) == 'm')) )
566 && (!c[4] || c[4] == '?' || c[4] == '&')) ){
567 entry=embedded_playlist_parse(line);
568 embedded = 1;
569 break;
572 if (!embedded) { //regular file link
573 entry = play_tree_new();
574 play_tree_add_file(entry,line);
577 if (entry != NULL) {
578 if(!list)
579 list = entry;
580 else
581 play_tree_append_entry(last_entry,entry);
582 last_entry = entry;
586 if(!list) return NULL;
587 entry = play_tree_new();
588 play_tree_set_child(entry,list);
589 return entry;
592 play_tree_t*
593 parse_playtree(stream_t *stream, int forced) {
594 play_tree_parser_t* p;
595 play_tree_t* ret;
597 #ifdef MP_DEBUG
598 assert(stream != NULL);
599 #endif
601 p = play_tree_parser_new(stream,0);
602 if(!p)
603 return NULL;
605 ret = play_tree_parser_get_play_tree(p, forced);
606 play_tree_parser_free(p);
608 return ret;
611 static void
612 play_tree_add_basepath(play_tree_t* pt, char* bp) {
613 int i,bl = strlen(bp),fl;
615 if(pt->child) {
616 play_tree_t* i;
617 for(i = pt->child ; i != NULL ; i = i->next)
618 play_tree_add_basepath(i,bp);
619 return;
622 if(!pt->files)
623 return;
625 for(i = 0 ; pt->files[i] != NULL ; i++) {
626 fl = strlen(pt->files[i]);
627 // if we find url:// or X:\ at the beginning, don't mangle it.
628 if(fl <= 0 || strstr(pt->files[i],"://") || strstr(pt->files[i],":\\") == pt->files[i] + 1)
629 continue;
630 // if the path begins with \ then prepend drive letter to it.
631 if (pt->files[i][0] == '\\') {
632 pt->files[i] = (char*)realloc(pt->files[i],2+fl+1);
633 memmove(pt->files[i] + 2,pt->files[i],fl+1);
634 memcpy(pt->files[i],bp,2);
635 return;
637 pt->files[i] = (char*)realloc(pt->files[i],bl+fl+1);
638 memmove(pt->files[i] + bl,pt->files[i],fl+1);
639 memcpy(pt->files[i],bp,bl);
643 // Wrapper for play_tree_add_basepath (add base path from file)
644 void play_tree_add_bpf(play_tree_t* pt, char* filename)
646 char *ls, *file;
648 if (pt && filename)
650 file = strdup(filename);
651 if (file)
653 ls = strrchr(file,'/');
654 if(!ls) ls = strrchr(file,'\\');
655 if(ls) {
656 ls[1] = '\0';
657 play_tree_add_basepath(pt,file);
659 free(file);
664 play_tree_t*
665 parse_playlist_file(char* file) {
666 stream_t *stream;
667 play_tree_t* ret;
668 int f=DEMUXER_TYPE_PLAYLIST;
670 stream = open_stream(file,0,&f);
672 if(!stream) {
673 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Error while opening playlist file %s: %s\n",file,strerror(errno));
674 return NULL;
677 mp_msg(MSGT_PLAYTREE,MSGL_V,"Parsing playlist file %s...\n",file);
679 ret = parse_playtree(stream,1);
680 free_stream(stream);
682 play_tree_add_bpf(ret, file);
684 return ret;
689 play_tree_parser_t*
690 play_tree_parser_new(stream_t* stream,int deep) {
691 play_tree_parser_t* p;
693 p = (play_tree_parser_t*)calloc(1,sizeof(play_tree_parser_t));
694 if(!p)
695 return NULL;
696 p->stream = stream;
697 p->deep = deep;
698 p->keep = 1;
700 return p;
704 void
705 play_tree_parser_free(play_tree_parser_t* p) {
707 #ifdef MP_DEBUG
708 assert(p != NULL);
709 #endif
711 if(p->buffer) free(p->buffer);
712 if(p->line) free(p->line);
713 free(p);
716 play_tree_t*
717 play_tree_parser_get_play_tree(play_tree_parser_t* p, int forced) {
718 play_tree_t* tree = NULL;
720 #ifdef MP_DEBUG
721 assert(p != NULL);
722 #endif
725 while(play_tree_parser_get_line(p) != NULL) {
726 play_tree_parser_reset(p);
728 tree = parse_asx(p);
729 if(tree) break;
730 play_tree_parser_reset(p);
732 tree = parse_pls(p);
733 if(tree) break;
734 play_tree_parser_reset(p);
736 tree = parse_m3u(p);
737 if(tree) break;
738 play_tree_parser_reset(p);
740 tree = parse_ref_ini(p);
741 if(tree) break;
742 play_tree_parser_reset(p);
744 tree = parse_smil(p);
745 if(tree) break;
746 play_tree_parser_reset(p);
748 // Here come the others formats ( textplain must stay the last one )
749 if (forced)
751 tree = parse_textplain(p);
752 if(tree) break;
754 break;
757 if(tree)
758 mp_msg(MSGT_PLAYTREE,MSGL_V,"Playlist successfully parsed\n");
759 else
760 mp_msg(MSGT_PLAYTREE,((forced==1)?MSGL_ERR:MSGL_V),"Error while parsing playlist\n");
762 if(tree)
763 tree = play_tree_cleanup(tree);
765 if(!tree) mp_msg(MSGT_PLAYTREE,((forced==1)?MSGL_WARN:MSGL_V),"Warning: empty playlist\n");
767 return tree;