# HG changeset patch # User albeu # Date 1014297173 0 # Node ID 4b728967bd77ffde206eb0858e5fea18fa0f2021 # Parent e370779ac77f06ffdaa459be8115ee60da386cb2 Playtree parser switch to a by line mode. Winamp (pls) playlist support. diff -r e370779ac77f -r 4b728967bd77 playtreeparser.c --- a/playtreeparser.c Thu Feb 21 13:11:31 2002 +0000 +++ b/playtreeparser.c Thu Feb 21 13:12:53 2002 +0000 @@ -19,211 +19,320 @@ extern play_tree_t* asx_parser_build_tree(char* buffer, int ref); +#define BUF_STEP 1024 + +#define WHITES " \n\r\t" + +static void +strstrip(char* str) { + char* i; + + for(i = str ; i[0] != '\0' && strchr(WHITES,i[0]) != NULL; i++) + /* NOTHING */; + if(i[0] != '\0') { + memmove(str,i,strlen(i)); + for(i = str + strlen(str) ; strchr(WHITES,i[0]) != NULL; i--) + /* NOTHING */; + i[1] = '\0'; + } else + str[0] = '\0'; +} + +static char* +play_tree_parser_get_line(play_tree_parser_t* p) { + char *end,*line_end; + int r,resize = 0; + + if(p->buffer == NULL) { + p->buffer = (char*)malloc(BUF_STEP); + p->buffer_size = BUF_STEP; + p->iter = p->buffer; + } + + if(p->stream->eof && p->buffer_end == 0) + return NULL; + + while(1) { + + if(resize) { + r = p->iter - p->buffer; + p->buffer = (char*)realloc(p->buffer,p->buffer_size+BUF_STEP); + p->iter = p->buffer + r; + p->buffer_size += BUF_STEP; + resize = 0; + } + + if(p->buffer_size - p->buffer_end > 1 && ! p->stream->eof) { + r = stream_read(p->stream,p->buffer + p->buffer_end,p->buffer_size - p->buffer_end - 1); + if(r > 0) { + p->buffer_end += r; + p->buffer[p->buffer_end] = '\0'; + } + } + + end = strchr(p->iter,'\n'); + if(!end) { + if(p->stream->eof) { + end = p->buffer + p->buffer_end + 1; + break; + } + resize = 1; + continue; + } + break; + } + + line_end = ((*(end-1)) == '\r') ? end-1 : end; + p->line = (char*)realloc(p->line,line_end - p->iter+1); + if(!p->line) + return NULL; + strncpy(p->line,p->iter,line_end - p->iter); + p->line[line_end - p->iter] = '\0'; + end++; + + if(!p->keep) { + if(end[1] != '\0') { + p->buffer_end = strlen(end); + memmove(p->buffer,end,p->buffer_end); + p->buffer[p->buffer_end] = '\0'; + } else + p->buffer_end = 0; + p->iter = p->buffer; + } else + p->iter = end; + + return p->line; +} + +static void +play_tree_parser_reset(play_tree_parser_t* p) { + p->iter = p->buffer; +} + +static void +play_tree_parser_stop_keeping(play_tree_parser_t* p) { + p->keep = 0; + if(p->iter && p->iter != p->buffer) { + p->buffer_end -= p->iter -p->buffer; + if(p->buffer_end) + memmove(p->buffer,p->iter,p->buffer_end); + p->iter = p->buffer; + } +} + play_tree_t* parse_asx(play_tree_parser_t* p) { - int r; - int comments = 0,read = 1,eof = 0; + int comments = 0,get_line = 1; + char* line = NULL; mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying asx...\n"); while(1) { - if(read && eof) // Eof reached before anything useful - return NULL; - if(read) { - if(p->buffer_size - p->buffer_end < 50) p->buffer_size += 255; - p->buffer = (char*)realloc(p->buffer,p->buffer_size*sizeof(char)); - if(p->buffer == NULL) { - mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",p->buffer_size*sizeof(char)); - p->buffer_size = p->buffer_end = 0; + if(get_line) { + line = play_tree_parser_get_line(p); + if(!line) return NULL; - } - - r = stream_read(p->stream,p->buffer+p->buffer_end,p->buffer_size-p->buffer_end-1); - if(r < 0) { - mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't read from stream r=%d\n",r); - return NULL; - } else if(r == 0) - eof = 1; - p->buffer_end += r; - p->buffer[p->buffer_end] = '\0'; + strstrip(line); + if(line[0] == '\0') + continue; } - - if(comments) { // Jump comments - int e; - char* end = strstr(p->buffer,"-->"); - if(!end) { - if(p->buffer[p->buffer_end-1] != '-') - p->buffer_end = 0; // Drop buffer content if last char isn't '-' - continue; - } - comments = 0; - e = end - p->buffer + 3; - if(e >= p->buffer_end) { // > seems impossible - p->buffer_end = 0; // Drop buffer content - read = 1; - continue; - } - p->buffer_end -= e; - memmove(p->buffer,end+3,p->buffer_end); // Drop comments - continue; - } - - for(r= 0 ; r < p->buffer_end ; r++) { - if(strchr(" \n\r\t",p->buffer[r]) != NULL) // Jump space - continue; - if(p->buffer[r] != '<') { - mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"First char isn't '<' but '%c'\n",p->buffer[r]); + if(!comments) { + if(line[0] != '<') { + mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"First char isn't '<' but '%c'\n",line[0]); mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Buffer = [%s]\n",p->buffer); return NULL; - } - break; // Stop on first '<' - } - if(r > p->buffer_end-4) { // We need more - if(r > 0) { // Drop unuseful beggining - p->buffer_end -= r; - memmove(p->buffer,&p->buffer[r],p->buffer_end); - } - read = 1; - continue; - } + } else if(strncmp(line,"",4) == 0) { // End of comments + comments = 0; + line = c+4; + if(line[0] != '\0') // There is some more data on this line : keep it + get_line = 0; - if(strncmp(&p->buffer[r],"