Document lmin and lmax lavcopts; mpeg vrc_buf_size values
[mplayer.git] / playtreeparser.c
blob21da6f675270e245f7cccce64948043ca4a1f477
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 "m_config.h"
15 #include "playtree.h"
16 #include "playtreeparser.h"
17 #include "libmpdemux/stream.h"
18 #include "mp_msg.h"
21 #if defined(__CYGWIN__) || defined(__OS2__)
22 #define PATH_SEP '\\'
23 #else
24 #define PATH_SEP '/'
25 #endif
27 extern play_tree_t*
28 asx_parser_build_tree(char* buffer, int ref);
30 #define BUF_STEP 1024
32 #define WHITES " \n\r\t"
34 static void
35 strstrip(char* str) {
36 char* i;
38 if (str==NULL)
39 return;
40 for(i = str ; i[0] != '\0' && strchr(WHITES,i[0]) != NULL; i++)
41 /* NOTHING */;
42 if(i[0] != '\0') {
43 memmove(str,i,strlen(i) + 1);
44 for(i = str + strlen(str) - 1 ; strchr(WHITES,i[0]) != NULL; i--)
45 /* NOTHING */;
46 i[1] = '\0';
47 } else
48 str[0] = '\0';
51 static char*
52 play_tree_parser_get_line(play_tree_parser_t* p) {
53 char *end,*line_end;
54 int r,resize = 0;
56 if(p->buffer == NULL) {
57 p->buffer = (char*)malloc(BUF_STEP);
58 p->buffer_size = BUF_STEP;
59 p->iter = p->buffer;
62 if(p->stream->eof && (p->buffer_end == 0 || p->iter[0] == '\0'))
63 return NULL;
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 p->buffer[p->buffer_end] = '\0';
83 end = strchr(p->iter,'\n');
84 if(!end) {
85 if(p->stream->eof) {
86 end = p->buffer + p->buffer_end;
87 break;
89 resize = 1;
90 continue;
92 break;
95 line_end = ((*(end-1)) == '\r') ? end-1 : end;
96 if(line_end - p->iter >= 0)
97 p->line = (char*)realloc(p->line,line_end - p->iter+1);
98 else
99 return NULL;
100 if(line_end - p->iter > 0)
101 strncpy(p->line,p->iter,line_end - p->iter);
102 p->line[line_end - p->iter] = '\0';
103 if(end[0] != '\0')
104 end++;
106 if(!p->keep) {
107 if(end[0] != '\0') {
108 p->buffer_end -= end-p->iter;
109 memmove(p->buffer,end,p->buffer_end);
110 p->buffer[p->buffer_end] = '\0';
111 } else
112 p->buffer_end = 0;
113 p->iter = p->buffer;
114 } else
115 p->iter = end;
117 return p->line;
120 static void
121 play_tree_parser_reset(play_tree_parser_t* p) {
122 p->iter = p->buffer;
125 static void
126 play_tree_parser_stop_keeping(play_tree_parser_t* p) {
127 p->keep = 0;
128 if(p->iter && p->iter != p->buffer) {
129 p->buffer_end -= p->iter -p->buffer;
130 if(p->buffer_end)
131 memmove(p->buffer,p->iter,p->buffer_end);
132 p->iter = p->buffer;
137 play_tree_t*
138 parse_asx(play_tree_parser_t* p) {
139 int comments = 0,get_line = 1;
140 char* line = NULL;
142 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying asx...\n");
144 while(1) {
145 if(get_line) {
146 line = play_tree_parser_get_line(p);
147 if(!line)
148 return NULL;
149 strstrip(line);
150 if(line[0] == '\0')
151 continue;
153 if(!comments) {
154 if(line[0] != '<') {
155 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"First char isn't '<' but '%c'\n",line[0]);
156 mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Buffer = [%s]\n",p->buffer);
157 return NULL;
158 } else if(strncmp(line,"<!--",4) == 0) { // Comments
159 comments = 1;
160 line += 4;
161 if(line[0] != '\0' && strlen(line) > 0)
162 get_line = 0;
163 } else if(strncasecmp(line,"<ASX",4) == 0) // We got an asx element
164 break;
165 else // We don't get an asx
166 return NULL;
167 } else { // Comments
168 char* c;
169 c = strchr(line,'-');
170 if(c) {
171 if (strncmp(c,"--!>",4) == 0) { // End of comments
172 comments = 0;
173 line = c+4;
174 if(line[0] != '\0') // There is some more data on this line : keep it
175 get_line = 0;
177 } else {
178 line = c+1; // Jump the -
179 if(line[0] != '\0') // Some more data
180 get_line = 0;
181 else // End of line
182 get_line = 1;
184 } else // No - on this line (or rest of line) : get next one
185 get_line = 1;
189 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected asx format\n");
191 // We have an asx : load it in memory and parse
193 while((line = play_tree_parser_get_line(p)) != NULL)
194 /* NOTHING */;
196 mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Parsing asx file: [%s]\n",p->buffer);
197 return asx_parser_build_tree(p->buffer,p->deep);
200 static char*
201 pls_entry_get_value(char* line) {
202 char* i;
204 i = strchr(line,'=');
205 if(!i || i[1] == '\0')
206 return NULL;
207 else
208 return i+1;
211 typedef struct pls_entry {
212 char* file;
213 char* title;
214 char* length;
215 } pls_entry_t;
217 static int
218 pls_read_entry(char* line,pls_entry_t** _e,int* _max_entry,char** val) {
219 int num,max_entry = (*_max_entry);
220 pls_entry_t* e = (*_e);
221 char* v;
223 v = pls_entry_get_value(line);
224 if(!v) {
225 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
226 return 0;
229 num = atoi(line);
230 if(num < 0) {
231 num = max_entry+1;
232 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"No entry index in entry %s\nAssuming %d\n",line,num);
234 if(num > max_entry) {
235 e = (pls_entry_t*)realloc(e,num*sizeof(pls_entry_t));
236 memset(&e[max_entry],0,(num-max_entry)*sizeof(pls_entry_t));
237 max_entry = num;
239 (*_e) = e;
240 (*_max_entry) = max_entry;
241 (*val) = v;
243 return num;
247 play_tree_t*
248 parse_pls(play_tree_parser_t* p) {
249 char *line,*v;
250 pls_entry_t* entries = NULL;
251 int n_entries = 0,max_entry=0,num;
252 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
254 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying Winamp playlist...\n");
255 if (!(line = play_tree_parser_get_line(p)))
256 return NULL;
257 strstrip(line);
258 if(strcasecmp(line,"[playlist]"))
259 return NULL;
260 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected Winamp playlist format\n");
261 play_tree_parser_stop_keeping(p);
262 line = play_tree_parser_get_line(p);
263 if(!line)
264 return NULL;
265 strstrip(line);
266 if(strncasecmp(line,"NumberOfEntries",15) == 0) {
267 v = pls_entry_get_value(line);
268 n_entries = atoi(v);
269 if(n_entries < 0)
270 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Invalid number of entries: very funny!!!\n");
271 else
272 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Playlist claims to have %d entries. Let's see.\n",n_entries);
273 line = play_tree_parser_get_line(p);
276 while(line) {
277 strstrip(line);
278 if(line[0] == '\0') {
279 line = play_tree_parser_get_line(p);
280 continue;
282 if(strncasecmp(line,"File",4) == 0) {
283 num = pls_read_entry(line+4,&entries,&max_entry,&v);
284 if(num < 0)
285 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
286 else
287 entries[num-1].file = strdup(v);
288 } else if(strncasecmp(line,"Title",5) == 0) {
289 num = pls_read_entry(line+5,&entries,&max_entry,&v);
290 if(num < 0)
291 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
292 else
293 entries[num-1].title = strdup(v);
294 } else if(strncasecmp(line,"Length",6) == 0) {
295 num = pls_read_entry(line+6,&entries,&max_entry,&v);
296 if(num < 0)
297 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
298 else
299 entries[num-1].length = strdup(v);
300 } else
301 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Unknown entry type %s\n",line);
302 line = play_tree_parser_get_line(p);
305 for(num = 0; num < max_entry ; num++) {
306 if(entries[num].file == NULL)
307 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Entry %d don't have a file !!!!\n",num+1);
308 else {
309 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding entry %s\n",entries[num].file);
310 entry = play_tree_new();
311 play_tree_add_file(entry,entries[num].file);
312 free(entries[num].file);
313 if(list)
314 play_tree_append_entry(last_entry,entry);
315 else
316 list = entry;
317 last_entry = entry;
319 if(entries[num].title) {
320 // When we have info in playtree we add this info
321 free(entries[num].title);
323 if(entries[num].length) {
324 // When we have info in playtree we add this info
325 free(entries[num].length);
329 free(entries);
331 entry = play_tree_new();
332 play_tree_set_child(entry,list);
333 return entry;
337 Reference Ini-Format: Each entry is assumed a reference
339 play_tree_t*
340 parse_ref_ini(play_tree_parser_t* p) {
341 char *line,*v;
342 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
344 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying reference-ini playlist...\n");
345 if (!(line = play_tree_parser_get_line(p)))
346 return NULL;
347 strstrip(line);
348 if(strcasecmp(line,"[Reference]"))
349 return NULL;
350 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected reference-ini playlist format\n");
351 play_tree_parser_stop_keeping(p);
352 line = play_tree_parser_get_line(p);
353 if(!line)
354 return NULL;
355 while(line) {
356 strstrip(line);
357 if(strncasecmp(line,"Ref",3) == 0) {
358 v = pls_entry_get_value(line+3);
359 if(!v)
360 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
361 else
363 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding entry %s\n",v);
364 entry = play_tree_new();
365 play_tree_add_file(entry,v);
366 if(list)
367 play_tree_append_entry(last_entry,entry);
368 else
369 list = entry;
370 last_entry = entry;
373 line = play_tree_parser_get_line(p);
376 if(!list) return NULL;
377 entry = play_tree_new();
378 play_tree_set_child(entry,list);
379 return entry;
382 play_tree_t*
383 parse_m3u(play_tree_parser_t* p) {
384 char* line;
385 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
387 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying extended m3u playlist...\n");
388 if (!(line = play_tree_parser_get_line(p)))
389 return NULL;
390 strstrip(line);
391 if(strcasecmp(line,"#EXTM3U"))
392 return NULL;
393 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected extended m3u playlist format\n");
394 play_tree_parser_stop_keeping(p);
396 while((line = play_tree_parser_get_line(p)) != NULL) {
397 strstrip(line);
398 if(line[0] == '\0')
399 continue;
400 /* EXTM3U files contain such lines:
401 * #EXTINF:<seconds>, <title>
402 * followed by a line with the filename
403 * for now we have no place to put that
404 * so we just skip that extra-info ::atmos
406 if(line[0] == '#') {
407 #if 0 /* code functional */
408 if(strncasecmp(line,"#EXTINF:",8) == 0) {
409 mp_msg(MSGT_PLAYTREE,MSGL_INFO,"[M3U] Duration: %dsec Title: %s\n",
410 strtol(line+8,&line,10), line+2);
412 #endif
413 continue;
415 entry = play_tree_new();
416 play_tree_add_file(entry,line);
417 if(!list)
418 list = entry;
419 else
420 play_tree_append_entry(last_entry,entry);
421 last_entry = entry;
424 if(!list) return NULL;
425 entry = play_tree_new();
426 play_tree_set_child(entry,list);
427 return entry;
430 play_tree_t*
431 parse_textplain(play_tree_parser_t* p) {
432 char* line;
433 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
435 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying plaintext playlist...\n");
436 play_tree_parser_stop_keeping(p);
438 while((line = play_tree_parser_get_line(p)) != NULL) {
439 strstrip(line);
440 if(line[0] == '\0')
441 continue;
442 entry = play_tree_new();
443 play_tree_add_file(entry,line);
444 if(!list)
445 list = entry;
446 else
447 play_tree_append_entry(last_entry,entry);
448 last_entry = entry;
451 if(!list) return NULL;
452 entry = play_tree_new();
453 play_tree_set_child(entry,list);
454 return entry;
457 play_tree_t*
458 parse_playtree(stream_t *stream, int forced) {
459 play_tree_parser_t* p;
460 play_tree_t* ret;
462 #ifdef MP_DEBUG
463 assert(stream != NULL);
464 #endif
466 p = play_tree_parser_new(stream,0);
467 if(!p)
468 return NULL;
470 ret = play_tree_parser_get_play_tree(p, forced);
471 play_tree_parser_free(p);
473 return ret;
476 static void
477 play_tree_add_basepath(play_tree_t* pt, char* bp) {
478 int i,bl = strlen(bp),fl;
480 if(pt->child) {
481 play_tree_t* i;
482 for(i = pt->child ; i != NULL ; i = i->next)
483 play_tree_add_basepath(i,bp);
484 return;
487 if(!pt->files)
488 return;
490 for(i = 0 ; pt->files[i] != NULL ; i++) {
491 fl = strlen(pt->files[i]);
492 if(fl <= 0 || pt->files[i][0] == PATH_SEP || strstr(pt->files[i],"://"))
493 continue;
494 pt->files[i] = (char*)realloc(pt->files[i],bl+fl+1);
495 memmove(pt->files[i] + bl,pt->files[i],fl+1);
496 memcpy(pt->files[i],bp,bl);
500 // Wrapper for play_tree_add_basepath (add base path from file)
501 void play_tree_add_bpf(play_tree_t* pt, char* filename)
503 char *ls, *file;
505 if (pt && filename)
507 file = strdup(filename);
508 if (file)
510 ls = strrchr(file,PATH_SEP);
511 if(ls) {
512 ls[1] = '\0';
513 play_tree_add_basepath(pt,file);
515 free(file);
520 play_tree_t*
521 parse_playlist_file(char* file) {
522 stream_t *stream;
523 play_tree_t* ret;
524 int f;
526 stream = open_stream(file,0,&f);
528 if(!stream) {
529 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Error while opening playlist file %s: %s\n",file,strerror(errno));
530 return NULL;
533 mp_msg(MSGT_PLAYTREE,MSGL_V,"Parsing playlist file %s...\n",file);
535 ret = parse_playtree(stream,1);
536 free_stream(stream);
538 play_tree_add_bpf(ret, file);
540 return ret;
545 play_tree_parser_t*
546 play_tree_parser_new(stream_t* stream,int deep) {
547 play_tree_parser_t* p;
549 p = (play_tree_parser_t*)calloc(1,sizeof(play_tree_parser_t));
550 if(!p)
551 return NULL;
552 p->stream = stream;
553 p->deep = deep;
554 p->keep = 1;
556 return p;
560 void
561 play_tree_parser_free(play_tree_parser_t* p) {
563 #ifdef MP_DEBUG
564 assert(p != NULL);
565 #endif
567 if(p->buffer) free(p->buffer);
568 if(p->line) free(p->line);
569 free(p);
572 play_tree_t*
573 play_tree_parser_get_play_tree(play_tree_parser_t* p, int forced) {
574 play_tree_t* tree = NULL;
576 #ifdef MP_DEBUG
577 assert(p != NULL);
578 #endif
581 while(play_tree_parser_get_line(p) != NULL) {
582 play_tree_parser_reset(p);
584 tree = parse_asx(p);
585 if(tree) break;
586 play_tree_parser_reset(p);
588 tree = parse_pls(p);
589 if(tree) break;
590 play_tree_parser_reset(p);
592 tree = parse_m3u(p);
593 if(tree) break;
594 play_tree_parser_reset(p);
596 tree = parse_ref_ini(p);
597 if(tree) break;
598 play_tree_parser_reset(p);
600 // Here come the others formats ( textplain must stay the last one )
601 if (forced)
603 tree = parse_textplain(p);
604 if(tree) break;
606 break;
609 if(tree)
610 mp_msg(MSGT_PLAYTREE,MSGL_V,"Playlist successfully parsed\n");
611 else
612 mp_msg(MSGT_PLAYTREE,((forced==1)?MSGL_ERR:MSGL_V),"Error while parsing playlist\n");
614 if(tree)
615 tree = play_tree_cleanup(tree);
617 if(!tree) mp_msg(MSGT_PLAYTREE,((forced==1)?MSGL_WARN:MSGL_V),"Warning: empty playlist\n");
619 return tree;