changeset 9290:0e043196d176

this patch adds a high-level-api to playtree, allowing to use it more easily and hopefully also letting one doing less mistakes, as current low-level-api is not rock-solid ... patch by Fabian Franz <FabianFranz@gmx.de>
author arpi
date Wed, 05 Feb 2003 23:02:05 +0000
parents f08b6945e839
children 64b8c5a07c2c
files playtree.c playtree.h
diffstat 2 files changed, 161 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/playtree.c	Wed Feb 05 21:32:59 2003 +0000
+++ b/playtree.c	Wed Feb 05 23:02:05 2003 +0000
@@ -201,6 +201,8 @@
   assert(pt->entry_type == PLAY_TREE_ENTRY_NODE);
 #endif
 
+  //DEBUG_FF: Where are the childs freed ?
+  // Attention in using this function!
   for(iter = pt->child ; iter != NULL ; iter = iter->next)
     iter->parent = NULL;
   
@@ -344,8 +346,10 @@
   }
 
   pt->params = (play_tree_param_t*)realloc(pt->params,(n+2)*sizeof(play_tree_param_t));
-  if(pt->params == NULL)
-    printf("Can't realloc params\n");
+  if(pt->params == NULL) {
+      mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't realloc params (%d bytes of memory)\n",(n+2)*sizeof(play_tree_param_t));
+      return;
+  }
   pt->params[n].name = strdup(name);
   pt->params[n].value = val != NULL ? strdup(val) : NULL;
   memset(&pt->params[n+1],0,sizeof(play_tree_param_t));
@@ -482,7 +486,10 @@
     return NULL;
 
   iter = (play_tree_iter_t*)calloc(1,sizeof(play_tree_iter_t));
-  if(! iter) return NULL;
+  if(! iter) {
+      mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate new iterator (%d bytes of memory)\n",sizeof(play_tree_iter_t));
+      return NULL;
+  }
   iter->root = pt;
   iter->tree = NULL;
   iter->config = config;
@@ -615,7 +622,7 @@
 
   // Is it a valid enty ?
   if(! play_tree_is_valid(pt)) {
-    if(d == 0) { // Can this happen ?
+    if(d == 0) { // Can this happen ? FF: Yes!
       mp_msg(MSGT_PLAYTREE,MSGL_ERR,"What to do now ???? Infinite loop if we continue\n");
       return PLAY_TREE_ITER_ERROR;
     } // Not a valid entry : go to next one
@@ -883,4 +890,116 @@
   return iter;
 }
 
+// HIGH Level API, by Fabian Franz (mplayer@fabian-franz.de)
+//
+play_tree_iter_t* pt_iter_create(play_tree_t** ppt, m_config_t* config)
+{
+  play_tree_iter_t* r=NULL;
+#ifdef MP_DEBUG
+  assert(*ppt!=NULL);
+#endif
+  
+  *ppt=play_tree_cleanup(*ppt);
+  
+  if(*ppt) {
+    r = play_tree_iter_new(*ppt,config);
+    if (r && play_tree_iter_step(r,0,0) != PLAY_TREE_ITER_ENTRY) 
+    {
+      play_tree_iter_free(r);
+      r = NULL;
+    }
+  }
 
+  return r;
+}
+
+void pt_iter_destroy(play_tree_iter_t** iter)
+{
+  if (iter && *iter)
+  {
+    free(*iter);
+    iter=NULL;
+  }
+}
+
+char* pt_iter_get_file(play_tree_iter_t* iter, int d)
+{
+  int i=0;
+  char* r;
+
+  if (iter==NULL)
+    return NULL;
+  
+  r = play_tree_iter_get_file(iter,d);
+  
+  while (!r && d!=0)
+  {
+    if (play_tree_iter_step(iter,d,0) != PLAY_TREE_ITER_ENTRY)
+        break;
+    r=play_tree_iter_get_file(iter,d);
+    i++;
+  }
+
+  return r;
+}
+
+void pt_iter_insert_entry(play_tree_iter_t* iter, play_tree_t* entry)
+{
+  play_tree_t *pt = iter->tree;
+#ifdef MP_DEBUG
+  assert(pt!=NULL);
+  assert(entry!=NULL);
+  assert(entry!=pt);
+#endif
+
+  play_tree_insert_entry(pt, entry);
+  play_tree_set_params_from(entry,pt);
+}
+
+void pt_iter_replace_entry(play_tree_iter_t* iter, play_tree_t* entry)
+{
+  play_tree_t *pt = iter->tree;
+
+  pt_iter_insert_entry(iter, entry);
+  play_tree_remove(pt, 1, 1);
+  iter->tree=entry;
+}
+
+//Add a new file as a new entry
+void pt_add_file(play_tree_t** ppt, char* filename)
+{
+  play_tree_t *pt = *ppt, *entry = play_tree_new();
+#ifdef MP_DEBUG
+  assert(entry!=NULL);
+#endif
+ 
+  play_tree_add_file(entry, filename);
+  if (pt)
+    play_tree_append_entry(pt, entry);
+  else
+  {
+    pt=entry;
+    *ppt=pt;
+  }
+  play_tree_set_params_from(entry,pt);
+}
+
+void pt_add_gui_file(play_tree_t** ppt, char* path, char* file)
+{
+  char* wholename = malloc(strlen(path)+strlen(file)+3);
+
+  if (wholename)
+  {
+    strcpy(wholename, path);
+    strcat(wholename, "/");
+    strcat(wholename, file);
+    pt_add_file(ppt, wholename);
+    free(wholename); // As pt_add_file strdups it anyway!
+  }
+}
+
+void pt_iter_goto_head(play_tree_iter_t* iter)
+{
+  iter->tree=iter->root;
+  play_tree_iter_step(iter, 0, 0);
+}
--- a/playtree.h	Wed Feb 05 21:32:59 2003 +0000
+++ b/playtree.h	Wed Feb 05 23:02:05 2003 +0000
@@ -166,4 +166,42 @@
 play_tree_t*
 parse_playlist_file(char* file);
 
+// Highlevel API with pt-suffix to different from low-level API
+// by Fabian Franz (mplayer@fabian-franz.de)
+
+// Cleanups pt and creates a new iter
+play_tree_iter_t* pt_iter_create(play_tree_t** pt, struct m_config* config);
+
+// Frees the iter
+void pt_iter_destroy(play_tree_iter_t** iter);
+
+// Gets the next available file in the direction (d=-1 || d=+1)
+char* pt_iter_get_file(play_tree_iter_t* iter, int d);
+
+// Two Macros that implement forward and backward direction
+#define pt_iter_get_next_file(iter) pt_iter_get_file(iter, 1)
+#define pt_iter_get_prev_file(iter) pt_iter_get_file(iter, -1)
+
+// Inserts entry into the playtree
+void pt_iter_insert_entry(play_tree_iter_t* iter, play_tree_t* entry);
+
+//Replaces current entry in playtree with entry
+//by doing insert and remove
+void pt_iter_replace_entry(play_tree_iter_t* iter, play_tree_t* entry);
+
+// Adds a new file to the playtree, 
+// if it is not valid it is created
+void pt_add_file(play_tree_t** ppt, char* filename);
+
+// Performs a convert to playtree-syntax, by concat path/file
+// and performs pt_add_file
+void pt_add_gui_file(play_tree_t** ppt, char* path, char* file);
+
+//Two macros to use only the iter and not the other things
+#define pt_iter_add_file(iter, filename) pt_add_file(&iter->tree, filename)
+#define pt_iter_add_gui_file(iter, path, name) pt_add_gui_file(&iter->tree, path, name)
+
+// Resets the iter and goes back to head
+void pt_iter_goto_head(play_tree_iter_t* iter);
+
 #endif