[Mplayer-cvslog] CVS: main playtreeparser.c,1.5,1.6 playtreeparser.h,1.1,1.2

Alban Bedel CVS albeu at mplayer.dev.hu
Thu Feb 21 14:12:56 CET 2002


Update of /cvsroot/mplayer/main
In directory mplayer:/var/tmp.root/cvs-serv27466

Modified Files:
	playtreeparser.c playtreeparser.h 
Log Message:
Playtree parser switch to a by line mode.
Winamp (pls) playlist support.


Index: playtreeparser.c
===================================================================
RCS file: /cvsroot/mplayer/main/playtreeparser.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- playtreeparser.c	15 Feb 2002 10:33:03 -0000	1.5
+++ playtreeparser.c	21 Feb 2002 13:12:53 -0000	1.6
@@ -19,212 +19,321 @@
 extern play_tree_t*
 asx_parser_build_tree(char* buffer, int ref);
 
+#define BUF_STEP 1024
 
-play_tree_t*
-parse_asx(play_tree_parser_t* p) {
-  int r;
-  int comments = 0,read = 1,eof = 0;
+#define WHITES " \n\r\t"
 
-  mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying asx...\n");
-  
+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(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;
-	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';
-    }
 
-    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;
-    } 
+    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;
+    }
     
-    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]);
-	mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Buffer = [%s]\n",p->buffer);
-	return NULL;
+    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';
       }
-      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);
+    
+    end = strchr(p->iter,'\n');
+    if(!end) {
+      if(p->stream->eof) {
+	end = p->buffer + p->buffer_end + 1;
+	break;
       }
-      read = 1;
+      resize = 1;
       continue;
     }
+    break;
+  }
 
-    if(strncmp(&p->buffer[r],"<!--",4) == 0) { // Comments
-      read = 0;
-      comments = 1;
-      continue;
-    }
+  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;
 
-    if(strncasecmp(&p->buffer[r],"<ASX",4) != 0) // First element is not a comment nor an asx : end
-      return NULL;
-	
-	
-    mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected asx format\n");
-    break;
+  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;
   }
-  // We have an asx : load it in memory and parse
+}
+
 
-  while(!eof) {
-    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;
+play_tree_t*
+parse_asx(play_tree_parser_t* p) {
+  int comments = 0,get_line = 1;
+  char* line = NULL;
+
+  mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying asx...\n");
+  
+  while(1) {
+    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)
-      p->buffer_end += r;
-    if(r <= 0)
-      break;
-    p->buffer[p->buffer_end] = '\0';
+      strstrip(line);
+      if(line[0] == '\0')
+	continue;
+    }
+    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;
+      } else if(strncmp(line,"<!--",4) == 0) { // Comments
+	comments = 1;
+	line += 4;
+	if(line[0] != '\0' && strlen(line) > 0)
+	  get_line = 0;
+      } else if(strncasecmp(line,"<ASX",4) == 0) // We got an asx element
+	break;
+      else // We don't get an asx
+	return NULL;
+    } else { // Comments
+      char* c;
+      c = strchr(line,'-');
+      if(c) {
+	if (strncmp(c,"--!>",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;
+
+	} else {
+	  line = c+1; // Jump the -
+	  if(line[0] != '\0') // Some more data
+	    get_line = 0;
+	  else  // End of line
+	    get_line = 1;
+	}
+      } else // No - on this line (or rest of line) : get next one
+	get_line = 1;
+    }
   }
+	
+  mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected asx format\n");
+
+  // We have an asx : load it in memory and parse
+
+  while((line = play_tree_parser_get_line(p)) != NULL)
+    /* NOTHING */;
 
  mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Parsing asx file : [%s]\n",p->buffer);
  return asx_parser_build_tree(p->buffer,p->deep);
 }
 
-play_tree_t*
-parse_textplain(play_tree_parser_t* p) {
-  char* end;
+static char*
+pls_entry_get_value(char* line) {
+  char* i;
+  
+  i = strchr(line,'=');
+  if(!i || i[1] == '\0')
+    return NULL;
+  else
+    return i+1;
+}
+
+typedef struct pls_entry {
   char* file;
-  int eof = 0,r,p_end=-1,resize = 0;
-  play_tree_t *list = NULL, *entry = NULL;
+  char* title;
+  char* length;
+} pls_entry_t;
+
+static int
+pls_read_entry(char* line,pls_entry_t** _e,int* _max_entry,char** val) {
+  int num,max_entry = (*_max_entry);
+  pls_entry_t* e = (*_e);
+  char* v;
+
+  v = pls_entry_get_value(line);
+  if(!v) {
+    mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
+    return 0;
+  }
 
-  mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying plaintext...\n");
+  num = atoi(line);
+  if(num < 0) {
+    num = max_entry+1;
+    mp_msg(MSGT_PLAYTREE,MSGL_WARN,"No entry index in entry %s\nAssuming %d\n",line,num);
+  }
+  if(num > max_entry) {
+    e = (pls_entry_t*)realloc(e,num*sizeof(pls_entry_t));
+    memset(&e[max_entry],0,(num-max_entry)*sizeof(pls_entry_t));
+    max_entry = num;
+  }
+  (*_e) = e;
+  (*_max_entry) = max_entry;
+  (*val) = v;
 
-  if(p->buffer_size < 255 && ! p->stream->eof) {
-    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;
-      return NULL;
-      }
+  return num;
+}
+
+
+play_tree_t*
+parse_pls(play_tree_parser_t* p) {
+  char *line,*v;
+  pls_entry_t* entries = NULL;
+  int n_entries = 0,max_entry=0,num;
+  play_tree_t *list = NULL, *entry = NULL;
+
+  mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying winamp playlist...\n");
+  line = play_tree_parser_get_line(p);
+  strstrip(line);
+  if(strcasecmp(line,"[playlist]"))
+    return NULL;
+  mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected winamp playlist format\n");
+  play_tree_parser_stop_keeping(p);
+  line = play_tree_parser_get_line(p);
+  if(!line)
+    return NULL;
+  strstrip(line);
+  if(strncasecmp(line,"NumberOfEntries",15) == 0) {
+    v = pls_entry_get_value(line);
+    n_entries = atoi(v);
+    if(n_entries < 0)
+      mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Invalid number of entries : very funny !!!\n");
+    else
+      mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Playlist claim to have %d entries. Let's see.\n",n_entries);
+    line = play_tree_parser_get_line(p);
   }
 
+  while(line) {
+    strstrip(line);
+    if(line[0] == '\0') {
+      line = play_tree_parser_get_line(p);
+      continue;
+    }
+    if(strncasecmp(line,"File",4) == 0) {
+      num = pls_read_entry(line+4,&entries,&max_entry,&v);
+      if(num < 0)
+	mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
+      else
+	entries[num-1].file = strdup(v);
+    } else if(strncasecmp(line,"Title",5) == 0) {
+      num = pls_read_entry(line+5,&entries,&max_entry,&v);
+      if(num < 0)
+	mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
+      else
+	entries[num-1].title = strdup(v);
+    } else if(strncasecmp(line,"Length",6) == 0) {
+      num = pls_read_entry(line+6,&entries,&max_entry,&v);
+      if(num < 0)
+	mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
+      else
+	entries[num-1].length = strdup(v);
+    } else 
+      mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Unknow entry type %s\n",line);
+    line = play_tree_parser_get_line(p);
+  }
 
-  while(!eof) {
-    if(resize) {
-      p->buffer_size += 255;
-      p->buffer = (char*)realloc(p->buffer,p->buffer_size*sizeof(char));
-      resize = 0;
-      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(list) play_tree_free_list(list,1);
-	return NULL;
-      }
+  for(num = 0; num < max_entry ; num++) {
+    if(entries[num].file == NULL)
+      mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Entry %d don't have a file !!!!\n",num+1);
+    else {
+      entry = play_tree_new();
+      play_tree_add_file(entry,entries[num].file);
+      free(entries[num].file);
+      if(list)
+	play_tree_append_entry(list,entry);
+      else
+	list = entry;
     }
-    if(!p->stream->eof) {
-      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;
-      }
-    p->buffer_end += r;
-    p->buffer[p->buffer_end] = '\0';
-    } else eof = 1;
-    r = 0;
-    while(1) {
-      p_end = r;
-      for( ; p->buffer[r] != '\0' ; r++) {
-	if(strchr(" \n\r\t",p->buffer[r]) != NULL)
-	  continue;
-	break;
-      }
-      if(p->buffer[r] == '\0') {
-	p_end = r;
-	if(!eof)
-	  resize = 1;
-	break;
-      }
-      end = strchr(&p->buffer[r],'\n');      
-      if(!end) {
-	if(!eof) {
-	  p_end = r;
-	  if(p_end == 0) {
-	    resize = 1;
-	  }
-	  break;
-	}
-	entry = play_tree_new();
-	play_tree_add_file(entry,&p->buffer[r]);
-	r = p->buffer_end;
-	
-      }
-      else {
-	if(*(end-1) == '\r') end--;
-	file = (char*)malloc((end-(&p->buffer[r])+1)*sizeof(char));
-	if(file == 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(list) play_tree_free_list(list,1);
-	  return NULL;
-	}
-	// TODO : Check if the given file exist and is readable (or it'a an url)	  
-	strncpy(file,&p->buffer[r],end-(&p->buffer[r]));
-	file[end-(&p->buffer[r])] = '\0';
-	entry = play_tree_new();
-	play_tree_add_file(entry,file);	
-	free(file);
-	r += end-(&p->buffer[r]);
-	p_end = r;
-      }
-      if(entry) {
-	mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding file %s to playlist\n",entry->files[0]);
-	if(list) play_tree_append_entry(list,entry);
-	else list = entry;
-	entry = NULL;
-      }
+    if(entries[num].title) {
+      // When we have info in playtree we add this info
+      free(entries[num].title);
     }
-    if(!eof && p_end > 0 && p_end < p->buffer_end) {
-      memmove(p->buffer,&p->buffer[p_end],p->buffer_end-p_end);
-      p->buffer_end -= p_end;
-    } else if(!eof && !resize && p_end == p->buffer_end) {
-      p->buffer_end = 0;
-      p->buffer[0] = '\0';
+    if(entries[num].length) {
+      // When we have info in playtree we add this info
+      free(entries[num].length);
     }
   }
+
+  free(entries);
+	
+  return list;
+}
+
+play_tree_t*
+parse_textplain(play_tree_parser_t* p) {
+  char* line;
+  play_tree_t *list = NULL, *entry = NULL;
+
+  mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying plaintext...\n");
+  play_tree_parser_stop_keeping(p);
+
+  while((line = play_tree_parser_get_line(p)) != NULL) {
+    strstrip(line);
+    if(line[0] == '\0')
+      continue;
+    entry = play_tree_new();
+    play_tree_add_file(entry,line);
+    if(!list)
+      list = entry;
+    else
+      play_tree_append_entry(list,entry);
+  }
    
   if(!list) return NULL;
   entry = play_tree_new();
@@ -290,6 +399,7 @@
     return NULL;
   p->stream = stream;
   p->deep = deep;
+  p->keep = 1;
 
   return p;
 
@@ -303,7 +413,8 @@
 #endif
 
   if(p->buffer) free(p->buffer);
-  free(p->buffer);
+  if(p->line) free(p->line);
+  free(p);
 }
 
 play_tree_t*
@@ -314,9 +425,18 @@
   assert(p != NULL);
 #endif
 
-  while(1) {
+
+  while(play_tree_parser_get_line(p) != NULL) {
+    play_tree_parser_reset(p);
+
     tree = parse_asx(p);
     if(tree) break;
+    play_tree_parser_reset(p);
+
+    tree = parse_pls(p);
+    if(tree) break;
+    play_tree_parser_reset(p);
+
     // Here come the others formats ( textplain must stay the last one )
     tree = parse_textplain(p);
     if(tree) break;
@@ -331,10 +451,6 @@
     tree = play_tree_cleanup(tree);
   
   if(!tree) mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Warning empty playlist\n");
-
-  if(p->buffer) free(p->buffer);
-  p->buffer = NULL;
-  p->buffer_end = p->buffer_size = 0;
 
   return tree;
 }

Index: playtreeparser.h
===================================================================
RCS file: /cvsroot/mplayer/main/playtreeparser.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- playtreeparser.h	22 Jan 2002 22:46:12 -0000	1.1
+++ playtreeparser.h	21 Feb 2002 13:12:53 -0000	1.2
@@ -7,9 +7,9 @@
 
 typedef struct play_tree_parser {
   stream_t* stream;
-  char* buffer;
+  char *buffer,*iter,*line;
   int buffer_size , buffer_end;
-  int deep;
+  int deep,keep;
 } play_tree_parser_t;
 
 




More information about the MPlayer-cvslog mailing list