10 #include <sys/types.h>
16 #include "playtreeparser.h"
17 #include "libmpdemux/stream.h"
21 #if defined(__CYGWIN__) || defined(__OS2__)
28 asx_parser_build_tree(char* buffer
, int ref
);
32 #define WHITES " \n\r\t"
40 for(i
= str
; i
[0] != '\0' && strchr(WHITES
,i
[0]) != NULL
; i
++)
43 memmove(str
,i
,strlen(i
) + 1);
44 for(i
= str
+ strlen(str
) - 1 ; strchr(WHITES
,i
[0]) != NULL
; i
--)
52 play_tree_parser_get_line(play_tree_parser_t
* p
) {
56 if(p
->buffer
== NULL
) {
57 p
->buffer
= (char*)malloc(BUF_STEP
);
58 p
->buffer_size
= BUF_STEP
;
62 if(p
->stream
->eof
&& (p
->buffer_end
== 0 || p
->iter
[0] == '\0'))
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
;
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);
79 p
->buffer
[p
->buffer_end
] = '\0';
83 end
= strchr(p
->iter
,'\n');
86 end
= p
->buffer
+ p
->buffer_end
;
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);
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';
108 p
->buffer_end
-= end
-p
->iter
;
109 memmove(p
->buffer
,end
,p
->buffer_end
);
110 p
->buffer
[p
->buffer_end
] = '\0';
121 play_tree_parser_reset(play_tree_parser_t
* p
) {
126 play_tree_parser_stop_keeping(play_tree_parser_t
* p
) {
128 if(p
->iter
&& p
->iter
!= p
->buffer
) {
129 p
->buffer_end
-= p
->iter
-p
->buffer
;
131 memmove(p
->buffer
,p
->iter
,p
->buffer_end
);
138 parse_asx(play_tree_parser_t
* p
) {
139 int comments
= 0,get_line
= 1;
142 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying asx...\n");
146 line
= play_tree_parser_get_line(p
);
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
);
158 } else if(strncmp(line
,"<!--",4) == 0) { // Comments
161 if(line
[0] != '\0' && strlen(line
) > 0)
163 } else if(strncasecmp(line
,"<ASX",4) == 0) // We got an asx element
165 else // We don't get an asx
169 c
= strchr(line
,'-');
171 if (strncmp(c
,"--!>",4) == 0) { // End of comments
174 if(line
[0] != '\0') // There is some more data on this line : keep it
178 line
= c
+1; // Jump the -
179 if(line
[0] != '\0') // Some more data
184 } else // No - on this line (or rest of line) : get next one
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
)
196 mp_msg(MSGT_PLAYTREE
,MSGL_DBG3
,"Parsing asx file: [%s]\n",p
->buffer
);
197 return asx_parser_build_tree(p
->buffer
,p
->deep
);
201 pls_entry_get_value(char* line
) {
204 i
= strchr(line
,'=');
205 if(!i
|| i
[1] == '\0')
211 typedef struct pls_entry
{
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
);
223 v
= pls_entry_get_value(line
);
225 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
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
));
240 (*_max_entry
) = max_entry
;
248 parse_pls(play_tree_parser_t
* p
) {
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
)))
258 if(strcasecmp(line
,"[playlist]"))
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
);
266 if(strncasecmp(line
,"NumberOfEntries",15) == 0) {
267 v
= pls_entry_get_value(line
);
270 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Invalid number of entries: very funny!!!\n");
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
);
278 if(line
[0] == '\0') {
279 line
= play_tree_parser_get_line(p
);
282 if(strncasecmp(line
,"File",4) == 0) {
283 num
= pls_read_entry(line
+4,&entries
,&max_entry
,&v
);
285 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
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
);
291 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
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
);
297 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
299 entries
[num
-1].length
= strdup(v
);
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);
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
);
314 play_tree_append_entry(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
);
331 entry
= play_tree_new();
332 play_tree_set_child(entry
,list
);
337 Reference Ini-Format: Each entry is assumed a reference
340 parse_ref_ini(play_tree_parser_t
* p
) {
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
)))
348 if(strcasecmp(line
,"[Reference]"))
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
);
357 if(strncasecmp(line
,"Ref",3) == 0) {
358 v
= pls_entry_get_value(line
+3);
360 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
363 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Adding entry %s\n",v
);
364 entry
= play_tree_new();
365 play_tree_add_file(entry
,v
);
367 play_tree_append_entry(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
);
383 parse_m3u(play_tree_parser_t
* p
) {
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
)))
391 if(strcasecmp(line
,"#EXTM3U"))
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
) {
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
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);
415 entry
= play_tree_new();
416 play_tree_add_file(entry
,line
);
420 play_tree_append_entry(last_entry
,entry
);
424 if(!list
) return NULL
;
425 entry
= play_tree_new();
426 play_tree_set_child(entry
,list
);
431 parse_textplain(play_tree_parser_t
* p
) {
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
) {
442 entry
= play_tree_new();
443 play_tree_add_file(entry
,line
);
447 play_tree_append_entry(last_entry
,entry
);
451 if(!list
) return NULL
;
452 entry
= play_tree_new();
453 play_tree_set_child(entry
,list
);
458 parse_playtree(stream_t
*stream
, int forced
) {
459 play_tree_parser_t
* p
;
463 assert(stream
!= NULL
);
466 p
= play_tree_parser_new(stream
,0);
470 ret
= play_tree_parser_get_play_tree(p
, forced
);
471 play_tree_parser_free(p
);
477 play_tree_add_basepath(play_tree_t
* pt
, char* bp
) {
478 int i
,bl
= strlen(bp
),fl
;
482 for(i
= pt
->child
; i
!= NULL
; i
= i
->next
)
483 play_tree_add_basepath(i
,bp
);
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
],"://"))
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
)
507 file
= strdup(filename
);
510 ls
= strrchr(file
,PATH_SEP
);
513 play_tree_add_basepath(pt
,file
);
521 parse_playlist_file(char* file
) {
526 stream
= open_stream(file
,0,&f
);
529 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Error while opening playlist file %s: %s\n",file
,strerror(errno
));
533 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Parsing playlist file %s...\n",file
);
535 ret
= parse_playtree(stream
,1);
538 play_tree_add_bpf(ret
, file
);
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
));
561 play_tree_parser_free(play_tree_parser_t
* p
) {
567 if(p
->buffer
) free(p
->buffer
);
568 if(p
->line
) free(p
->line
);
573 play_tree_parser_get_play_tree(play_tree_parser_t
* p
, int forced
) {
574 play_tree_t
* tree
= NULL
;
581 while(play_tree_parser_get_line(p
) != NULL
) {
582 play_tree_parser_reset(p
);
586 play_tree_parser_reset(p
);
590 play_tree_parser_reset(p
);
594 play_tree_parser_reset(p
);
596 tree
= parse_ref_ini(p
);
598 play_tree_parser_reset(p
);
600 // Here come the others formats ( textplain must stay the last one )
603 tree
= parse_textplain(p
);
610 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Playlist successfully parsed\n");
612 mp_msg(MSGT_PLAYTREE
,((forced
==1)?MSGL_ERR
:MSGL_V
),"Error while parsing playlist\n");
615 tree
= play_tree_cleanup(tree
);
617 if(!tree
) mp_msg(MSGT_PLAYTREE
,((forced
==1)?MSGL_WARN
:MSGL_V
),"Warning: empty playlist\n");