switch_ratio may not work with some filter chains
[mplayer/greg.git] / playtree.c
blob6d3c4f42dac21a2c31544ef68976e0839c92d027
2 #include "config.h"
3 #include <stdlib.h>
4 #include <string.h>
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <errno.h>
8 #ifdef MP_DEBUG
9 #include <assert.h>
10 #endif
11 #include "m_config.h"
12 #include "playtree.h"
13 #include "mp_msg.h"
15 static int
16 play_tree_is_valid(play_tree_t* pt);
18 play_tree_t*
19 play_tree_new(void) {
20 play_tree_t* r = (play_tree_t*)calloc(1,sizeof(play_tree_t));
21 if(r == NULL)
22 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",sizeof(play_tree_t));
23 r->entry_type = PLAY_TREE_ENTRY_NODE;
24 return r;
27 void
28 play_tree_free(play_tree_t* pt, int childs) {
29 play_tree_t* iter;
31 #ifdef MP_DEBUG
32 assert(pt != NULL);
33 #endif
35 if(childs) {
36 for(iter = pt->child; iter != NULL; ) {
37 play_tree_t* nxt=iter->next;
38 play_tree_free(iter,1);
39 iter = nxt;
41 pt->child = NULL;
44 play_tree_remove(pt,0,0);
46 for(iter = pt->child ; iter != NULL ; iter = iter->next)
47 iter->parent = NULL;
49 //if(pt->params) free(pt->params);
50 if(pt->files) {
51 int i;
52 for(i = 0 ; pt->files[i] != NULL ; i++)
53 free(pt->files[i]);
54 free(pt->files);
57 free(pt);
60 void
61 play_tree_free_list(play_tree_t* pt, int childs) {
62 play_tree_t* iter;
64 #ifdef MP_DEBUG
65 assert(pt != NULL);
66 #endif
68 for(iter = pt ; iter->prev != NULL ; iter = iter->prev)
69 /* NOTHING */;
71 while(iter) {
72 play_tree_t* nxt = iter->next;
73 play_tree_free(iter,childs);
74 iter = nxt;
80 void
81 play_tree_append_entry(play_tree_t* pt, play_tree_t* entry) {
82 play_tree_t* iter;
84 #ifdef MP_DEBUG
85 assert(pt != NULL);
86 assert(entry != NULL);
87 #endif
89 if(pt == entry)
90 return;
92 for(iter = pt ; iter->next != NULL ; iter = iter->next)
93 /* NOTHING */;
95 entry->parent = iter->parent;
96 entry->prev = iter;
97 entry->next = NULL;
98 iter->next = entry;
101 void
102 play_tree_prepend_entry(play_tree_t* pt, play_tree_t* entry) {
103 play_tree_t* iter;
105 #ifdef MP_DEBUG
106 assert(pt != NULL);
107 assert(entry != NULL);
108 #endif
110 for(iter = pt ; iter->prev != NULL; iter = iter->prev)
111 /* NOTHING */;
113 entry->prev = NULL;
114 entry->next = iter;
115 entry->parent = iter->parent;
117 iter->prev = entry;
118 if(entry->parent) {
119 #ifdef MP_DEBUG
120 assert(entry->parent->child == iter);
121 #endif
122 entry->parent->child = entry;
126 void
127 play_tree_insert_entry(play_tree_t* pt, play_tree_t* entry) {
129 #ifdef MP_DEBUG
130 assert(pt != NULL);
131 assert(entry != NULL);
132 #endif
134 entry->parent = pt->parent;
135 entry->prev = pt;
136 if(pt->next) {
137 #ifdef MP_DEBUG
138 assert(pt->next->prev == pt);
139 #endif
140 entry->next = pt->next;
141 entry->next->prev = entry;
142 } else
143 entry->next = NULL;
144 pt->next = entry;
148 void
149 play_tree_remove(play_tree_t* pt, int free_it,int with_childs) {
151 #ifdef MP_DEBUG
152 assert(pt != NULL);
153 #endif
155 // Middle of list
156 if(pt->prev && pt->next) {
157 #ifdef MP_DEBUG
158 assert(pt->prev->next == pt);
159 assert(pt->next->prev == pt);
160 #endif
161 pt->prev->next = pt->next;
162 pt->next->prev = pt->prev;
163 } // End of list
164 else if(pt->prev) {
165 #ifdef MP_DEBUG
166 assert(pt->prev->next == pt);
167 #endif
168 pt->prev->next = NULL;
169 } // Begining of list
170 else if(pt->next) {
171 #ifdef MP_DEBUG
172 assert(pt->next->prev == pt);
173 #endif
174 pt->next->prev = NULL;
175 if(pt->parent) {
176 #ifdef MP_DEBUG
177 assert(pt->parent->child == pt);
178 #endif
179 pt->parent->child = pt->next;
181 } // The only one
182 else if(pt->parent) {
183 #ifdef MP_DEBUG
184 assert(pt->parent->child == pt);
185 #endif
186 pt->parent->child = NULL;
189 pt->prev = pt->next = pt->parent = NULL;
190 if(free_it)
191 play_tree_free(pt,with_childs);
195 void
196 play_tree_set_child(play_tree_t* pt, play_tree_t* child) {
197 play_tree_t* iter;
199 #ifdef MP_DEBUG
200 assert(pt != NULL);
201 assert(pt->entry_type == PLAY_TREE_ENTRY_NODE);
202 #endif
204 //DEBUG_FF: Where are the childs freed ?
205 // Attention in using this function!
206 for(iter = pt->child ; iter != NULL ; iter = iter->next)
207 iter->parent = NULL;
209 // Go back to first one
210 for(iter = child ; iter->prev != NULL ; iter = iter->prev)
211 /* NOTHING */;
213 pt->child = iter;
215 for( ; iter != NULL ; iter= iter->next)
216 iter->parent = pt;
220 void
221 play_tree_set_parent(play_tree_t* pt, play_tree_t* parent) {
222 play_tree_t* iter;
224 #ifdef MP_DEBUG
225 assert(pt != NULL);
226 #endif
228 if(pt->parent)
229 pt->parent->child = NULL;
231 for(iter = pt ; iter != NULL ; iter = iter->next)
232 iter->parent = parent;
234 if(pt->prev) {
235 for(iter = pt->prev ; iter->prev != NULL ; iter = iter->prev)
236 iter->parent = parent;
237 iter->parent = parent;
238 parent->child = iter;
239 } else
240 parent->child = pt;
245 void
246 play_tree_add_file(play_tree_t* pt,char* file) {
247 int n = 0;
248 char* e;
250 #ifdef MP_DEBUG
251 assert(pt != NULL);
252 assert(pt->child == NULL);
253 assert(file != NULL);
254 #endif
256 if(pt->entry_type != PLAY_TREE_ENTRY_NODE &&
257 pt->entry_type != PLAY_TREE_ENTRY_FILE)
258 return;
260 if(pt->files) {
261 for(n = 0 ; pt->files[n] != NULL ; n++)
262 /* NOTHING */;
264 pt->files = (char**)realloc(pt->files,(n+2)*sizeof(char*));
265 if(pt->files ==NULL) {
266 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",(n+2)*sizeof(char*));
267 return;
270 e = pt->files[n] = strdup(file);
271 pt->files[n+1] = NULL;
273 pt->entry_type = PLAY_TREE_ENTRY_FILE;
278 play_tree_remove_file(play_tree_t* pt,char* file) {
279 int n,f = -1;
281 #ifdef MP_DEBUG
282 assert(pt != NULL);
283 assert(file != NULL);
284 assert(pt->entry_type != PLAY_TREE_ENTRY_NODE);
285 #endif
287 for(n=0 ; pt->files[n] != NULL ; n++) {
288 if(strcmp(file,pt->files[n]) == 0)
289 f = n;
292 if(f < 0) // Not found
293 return 0;
295 #ifdef MP_DEBUG
296 assert(n > f);
297 #endif
299 free(pt->files[f]);
301 if(n > 1) {
302 memmove(&pt->files[f],&pt->files[f+1],(n-f)*sizeof(char*));
303 pt->files = (char**)realloc(pt->files,n*sizeof(char*));
304 if(pt->files == NULL) {
305 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",(n+2)*sizeof(char*));
306 return -1;
308 } else {
309 free(pt->files);
310 pt->files = NULL;
313 return 1;
316 void
317 play_tree_set_param(play_tree_t* pt, char* name, char* val) {
318 int n = 0,ni = -1;
320 #ifdef MP_DEBUG
321 assert(pt != NULL);
322 assert(name != NULL);
323 #endif
325 if(pt->params) {
326 for( ; pt->params[n].name != NULL ; n++) {
327 if(strcasecmp(pt->params[n].name,name) == 0)
328 ni = n;
332 if(ni > 0) {
333 if(pt->params[n].value != NULL) free(pt->params[n].value);
334 pt->params[n].value = val != NULL ? strdup(val) : NULL;
335 return;
338 pt->params = (play_tree_param_t*)realloc(pt->params,(n+2)*sizeof(play_tree_param_t));
339 if(pt->params == NULL) {
340 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't realloc params (%d bytes of memory)\n",(n+2)*sizeof(play_tree_param_t));
341 return;
343 pt->params[n].name = strdup(name);
344 pt->params[n].value = val != NULL ? strdup(val) : NULL;
345 memset(&pt->params[n+1],0,sizeof(play_tree_param_t));
347 return;
351 play_tree_unset_param(play_tree_t* pt, char* name) {
352 int n,ni = -1;
354 #ifdef MP_DEBUG
355 assert(pt != NULL);
356 assert(name != NULL);
357 assert(pt->params != NULL);
358 #endif
360 for(n = 0 ; pt->params[n].name != NULL ; n++) {
361 if(strcasecmp(pt->params[n].name,name) == 0)
362 ni = n;
365 if(ni < 0)
366 return 0;
368 if(pt->params[ni].name) free(pt->params[ni].name);
369 if(pt->params[ni].value) free(pt->params[ni].value);
371 if(n > 1) {
372 memmove(&pt->params[ni],&pt->params[ni+1],(n-ni)*sizeof(play_tree_param_t));
373 pt->params = (play_tree_param_t*)realloc(pt->params,n*sizeof(play_tree_param_t));
374 if(pt->params == NULL) {
375 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",n*sizeof(play_tree_param_t));
376 return -1;
378 } else {
379 free(pt->params);
380 pt->params = NULL;
383 return 1;
386 void
387 play_tree_set_params_from(play_tree_t* dest,play_tree_t* src) {
388 int i;
390 #ifdef MP_DEBUG
391 assert(dest != NULL);
392 assert(src != NULL);
393 #endif
395 if(!src->params)
396 return;
398 for(i = 0; src->params[i].name != NULL ; i++)
399 play_tree_set_param(dest,src->params[i].name,src->params[i].value);
400 if(src->flags & PLAY_TREE_RND) // pass the random flag too
401 dest->flags |= PLAY_TREE_RND;
405 // all childs if deep < 0
406 void
407 play_tree_set_flag(play_tree_t* pt, int flags , int deep) {
408 play_tree_t* i;
410 pt->flags |= flags;
412 if(deep && pt->child) {
413 if(deep > 0) deep--;
414 for(i = pt->child ; i ; i = i->next)
415 play_tree_set_flag(i,flags,deep);
419 void
420 play_tree_unset_flag(play_tree_t* pt, int flags , int deep) {
421 play_tree_t* i;
423 pt->flags &= ~flags;
425 if(deep && pt->child) {
426 if(deep > 0) deep--;
427 for(i = pt->child ; i ; i = i->next)
428 play_tree_unset_flag(i,flags,deep);
433 //////////////////////////////////// ITERATOR //////////////////////////////////////
435 static void
436 play_tree_iter_push_params(play_tree_iter_t* iter) {
437 int n;
438 play_tree_t* pt;
439 #ifdef MP_DEBUG
440 assert(iter != NULL);
441 assert(iter->config != NULL);
442 assert(iter->tree != NULL);
443 #endif
445 pt = iter->tree;
447 // We always push a config because we can set some option
448 // while playing
449 m_config_push(iter->config);
451 if(pt->params == NULL)
452 return;
455 for(n = 0; pt->params[n].name != NULL ; n++) {
456 int e;
457 if((e = m_config_set_option(iter->config,pt->params[n].name,pt->params[n].value)) < 0) {
458 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Error %d while setting option '%s' with value '%s'\n",e,
459 pt->params[n].name,pt->params[n].value);
463 if(!pt->child)
464 iter->entry_pushed = 1;
465 return;
468 play_tree_iter_t*
469 play_tree_iter_new(play_tree_t* pt,m_config_t* config) {
470 play_tree_iter_t* iter;
472 #ifdef MP_DEBUG
473 assert(pt != NULL);
474 assert(config != NULL);
475 #endif
477 if( ! play_tree_is_valid(pt))
478 return NULL;
480 iter = (play_tree_iter_t*)calloc(1,sizeof(play_tree_iter_t));
481 if(! iter) {
482 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate new iterator (%d bytes of memory)\n",sizeof(play_tree_iter_t));
483 return NULL;
485 iter->root = pt;
486 iter->tree = NULL;
487 iter->config = config;
489 if(pt->parent)
490 iter->loop = pt->parent->loop;
492 return iter;
495 void
496 play_tree_iter_free(play_tree_iter_t* iter) {
498 #ifdef MP_DEBUG
499 assert(iter != NULL);
500 #endif
502 if(iter->status_stack) {
503 #ifdef MP_DEBUG
504 assert(iter->stack_size > 0);
505 #endif
506 free(iter->status_stack);
509 free(iter);
512 static play_tree_t*
513 play_tree_rnd_step(play_tree_t* pt) {
514 int count = 0;
515 int r;
516 play_tree_t *i,*head;
518 // Count how many free choice we have
519 for(i = pt ; i->prev ; i = i->prev)
520 if(!(i->flags & PLAY_TREE_RND_PLAYED)) count++;
521 head = i;
522 if(!(i->flags & PLAY_TREE_RND_PLAYED)) count++;
523 for(i = pt->next ; i ; i = i->next)
524 if(!(i->flags & PLAY_TREE_RND_PLAYED)) count++;
526 if(!count) return NULL;
528 r = (int)((count-1.0) * rand() / RAND_MAX);
530 for(i = head ; i ; i=i->next) {
531 if(!(i->flags & PLAY_TREE_RND_PLAYED)) r--;
532 if(r < 0) return i;
535 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Random stepping error\n");
536 return NULL;
541 play_tree_iter_step(play_tree_iter_t* iter, int d,int with_nodes) {
542 play_tree_t* pt;
544 if ( !iter ) return PLAY_TREE_ITER_ENTRY;
545 if ( !iter->root ) return PLAY_TREE_ITER_ENTRY;
547 #ifdef MP_DEBUG
548 assert(iter != NULL);
549 assert(iter->root != NULL);
550 //printf("PT : Stepping = %d\n",d);
551 #endif
553 if(iter->tree == NULL) {
554 iter->tree = iter->root;
555 return play_tree_iter_step(iter,0,with_nodes);
558 if(iter->config && iter->entry_pushed > 0) {
559 iter->entry_pushed = 0;
560 m_config_pop(iter->config);
563 if(iter->tree->parent && (iter->tree->parent->flags & PLAY_TREE_RND))
564 iter->mode = PLAY_TREE_ITER_RND;
565 else
566 iter->mode = PLAY_TREE_ITER_NORMAL;
568 iter->file = -1;
569 if(iter->mode == PLAY_TREE_ITER_RND)
570 pt = play_tree_rnd_step(iter->tree);
571 else if( d > 0 ) {
572 int i;
573 pt = iter->tree;
574 for(i = d ; i > 0 && pt ; i--)
575 pt = pt->next;
576 d = i ? i : 1;
577 } else if(d < 0) {
578 int i;
579 pt = iter->tree;
580 for(i = d ; i < 0 && pt ; i++)
581 pt = pt->prev;
582 d = i ? i : -1;
583 } else
584 pt = iter->tree;
586 if(pt == NULL) { // No next
587 // Must we loop?
588 if(iter->tree->parent && iter->tree->parent->loop != 0 && ((d > 0 && iter->loop != 0) || ( d < 0 && (iter->loop < 0 || iter->loop < iter->tree->parent->loop) ) ) ) {
589 if(d > 0) { // Go back to the first one
590 for(pt = iter->tree ; pt->prev != NULL; pt = pt->prev)
591 /* NOTHNG */;
592 if(iter->loop > 0) iter->loop--;
593 } else if( d < 0 ) { // Or the last one
594 for(pt = iter->tree ; pt->next != NULL; pt = pt->next)
595 /* NOTHNG */;
596 if(iter->loop >= 0 && iter->loop < iter->tree->parent->loop) iter->loop++;
598 iter->tree = pt;
599 return play_tree_iter_step(iter,0,with_nodes);
601 // Go up one level
602 return play_tree_iter_up_step(iter,d,with_nodes);
606 // Is there any valid childs ?
607 if(pt->child && play_tree_is_valid(pt->child)) {
608 iter->tree = pt;
609 if(with_nodes) { // Stop on the node
610 return PLAY_TREE_ITER_NODE;
611 } else // Or follow it
612 return play_tree_iter_down_step(iter,d,with_nodes);
615 // Is it a valid enty ?
616 if(! play_tree_is_valid(pt)) {
617 if(d == 0) { // Can this happen ? FF: Yes!
618 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"What to do now ???? Infinite loop if we continue\n");
619 return PLAY_TREE_ITER_ERROR;
620 } // Not a valid entry : go to next one
621 return play_tree_iter_step(iter,d,with_nodes);
624 #ifdef MP_DEBUG
625 assert(pt->files != NULL);
626 #endif
628 iter->tree = pt;
630 for(d = 0 ; iter->tree->files[d] != NULL ; d++)
631 /* NOTHING */;
632 iter->num_files = d;
634 if(iter->config) {
635 play_tree_iter_push_params(iter);
636 iter->entry_pushed = 1;
637 if(iter->mode == PLAY_TREE_ITER_RND)
638 pt->flags |= PLAY_TREE_RND_PLAYED;
641 return PLAY_TREE_ITER_ENTRY;
645 static int
646 play_tree_is_valid(play_tree_t* pt) {
647 play_tree_t* iter;
649 #ifdef MP_DEBUG
650 assert(pt != NULL);
651 #endif
653 if(pt->entry_type != PLAY_TREE_ENTRY_NODE) {
654 #ifdef MP_DEBUG
655 assert(pt->child == NULL);
656 #endif
657 return 1;
659 else if (pt->child != NULL) {
660 for(iter = pt->child ; iter != NULL ; iter = iter->next) {
661 if(play_tree_is_valid(iter))
662 return 1;
665 return 0;
669 play_tree_iter_up_step(play_tree_iter_t* iter, int d,int with_nodes) {
671 #ifdef MP_DEBUG
672 assert(iter != NULL);
673 assert(iter->tree != NULL);
674 //printf("PT : Go UP\n");
675 #endif
677 iter->file = -1;
678 if(iter->tree->parent == iter->root->parent)
679 return PLAY_TREE_ITER_END;
681 #ifdef MP_DEBUG
682 assert(iter->tree->parent != NULL);
683 assert(iter->stack_size > 0);
684 assert(iter->status_stack != NULL);
685 #endif
687 iter->stack_size--;
688 iter->loop = iter->status_stack[iter->stack_size];
689 if(iter->stack_size > 0)
690 iter->status_stack = (int*)realloc(iter->status_stack,iter->stack_size*sizeof(int));
691 else {
692 free(iter->status_stack);
693 iter->status_stack = NULL;
695 if(iter->stack_size > 0 && iter->status_stack == NULL) {
696 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",iter->stack_size*sizeof(char*));
697 return PLAY_TREE_ITER_ERROR;
699 iter->tree = iter->tree->parent;
701 // Pop subtree params
702 if(iter->config) {
703 m_config_pop(iter->config);
704 if(iter->mode == PLAY_TREE_ITER_RND)
705 iter->tree->flags |= PLAY_TREE_RND_PLAYED;
708 return play_tree_iter_step(iter,d,with_nodes);
712 play_tree_iter_down_step(play_tree_iter_t* iter, int d,int with_nodes) {
714 #ifdef MP_DEBUG
715 assert(iter->tree->files == NULL);
716 assert(iter->tree->child != NULL);
717 assert(iter->tree->child->parent == iter->tree);
718 //printf("PT : Go DOWN\n");
719 #endif
721 iter->file = -1;
723 // Push subtree params
724 if(iter->config)
725 play_tree_iter_push_params(iter);
727 iter->stack_size++;
728 iter->status_stack = (int*)realloc(iter->status_stack,iter->stack_size*sizeof(int));
729 if(iter->status_stack == NULL) {
730 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",iter->stack_size*sizeof(int));
731 return PLAY_TREE_ITER_ERROR;
733 iter->status_stack[iter->stack_size-1] = iter->loop;
734 // Set new status
735 iter->loop = iter->tree->loop-1;
736 if(d >= 0)
737 iter->tree = iter->tree->child;
738 else {
739 play_tree_t* pt;
740 for(pt = iter->tree->child ; pt->next != NULL ; pt = pt->next)
741 /*NOTING*/;
742 iter->tree = pt;
745 return play_tree_iter_step(iter,0,with_nodes);
748 char*
749 play_tree_iter_get_file(play_tree_iter_t* iter, int d) {
750 char* entry;
751 #ifdef MP_DEBUG
752 assert(iter != NULL);
753 assert(iter->tree->child == NULL);
754 #endif
756 if(iter->tree->files == NULL)
757 return NULL;
759 #ifdef MP_DEBUG
760 assert(iter->num_files > 0);
761 #endif
763 if(iter->file >= iter->num_files-1 || iter->file < -1)
764 return NULL;
766 if(d > 0) {
767 if(iter->file >= iter->num_files - 1)
768 iter->file = 0;
769 else
770 iter->file++;
771 } else if(d < 0) {
772 if(iter->file <= 0)
773 iter->file = iter->num_files - 1;
774 else
775 iter->file--;
777 return iter->tree->files[iter->file];
780 play_tree_t*
781 play_tree_cleanup(play_tree_t* pt) {
782 play_tree_t* iter, *tmp, *first;
784 #ifdef MP_DEBUG
785 assert(pt != NULL);
786 #endif
788 if( ! play_tree_is_valid(pt)) {
789 play_tree_remove(pt,1,1);
790 return NULL;
793 first = pt->child;
795 for(iter = pt->child ; iter != NULL ; ) {
796 tmp = iter;
797 iter = iter->next;
798 if(! play_tree_is_valid(tmp)) {
799 play_tree_remove(tmp,1,1);
800 if(tmp == first) first = iter;
804 for(iter = first ; iter != NULL ; ) {
805 tmp = iter;
806 iter = iter->next;
807 play_tree_cleanup(tmp);
810 return pt;
814 play_tree_iter_t*
815 play_tree_iter_new_copy(play_tree_iter_t* old) {
816 play_tree_iter_t* iter;
818 #ifdef MP_DEBUG
819 assert(old != NULL);
820 #endif
822 iter = (play_tree_iter_t*)malloc(sizeof(play_tree_iter_t));
823 if(iter == NULL) {
824 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",sizeof(play_tree_iter_t));
825 return NULL;
828 memcpy(iter,old,sizeof(play_tree_iter_t));
829 if(old->status_stack) {
830 iter->status_stack = (int*)malloc(old->stack_size * sizeof(int));
831 if(iter->status_stack == NULL) {
832 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",old->stack_size * sizeof(int));
833 free(iter);
834 return NULL;
836 memcpy(iter->status_stack,old->status_stack,iter->stack_size*sizeof(int));
838 iter->config = NULL;
840 return iter;
843 // HIGH Level API, by Fabian Franz (mplayer@fabian-franz.de)
845 play_tree_iter_t* pt_iter_create(play_tree_t** ppt, m_config_t* config)
847 play_tree_iter_t* r=NULL;
848 #ifdef MP_DEBUG
849 assert(*ppt!=NULL);
850 #endif
852 *ppt=play_tree_cleanup(*ppt);
854 if(*ppt) {
855 r = play_tree_iter_new(*ppt,config);
856 if (r && play_tree_iter_step(r,0,0) != PLAY_TREE_ITER_ENTRY)
858 play_tree_iter_free(r);
859 r = NULL;
863 return r;
866 void pt_iter_destroy(play_tree_iter_t** iter)
868 if (iter && *iter)
870 free(*iter);
871 iter=NULL;
875 char* pt_iter_get_file(play_tree_iter_t* iter, int d)
877 int i=0;
878 char* r;
880 if (iter==NULL)
881 return NULL;
883 r = play_tree_iter_get_file(iter,d);
885 while (!r && d!=0)
887 if (play_tree_iter_step(iter,d,0) != PLAY_TREE_ITER_ENTRY)
888 break;
889 r=play_tree_iter_get_file(iter,d);
890 i++;
893 return r;
896 void pt_iter_insert_entry(play_tree_iter_t* iter, play_tree_t* entry)
898 play_tree_t *pt = iter->tree;
899 #ifdef MP_DEBUG
900 assert(pt!=NULL);
901 assert(entry!=NULL);
902 assert(entry!=pt);
903 #endif
905 play_tree_insert_entry(pt, entry);
906 play_tree_set_params_from(entry,pt);
909 void pt_iter_replace_entry(play_tree_iter_t* iter, play_tree_t* entry)
911 play_tree_t *pt = iter->tree;
913 pt_iter_insert_entry(iter, entry);
914 play_tree_remove(pt, 1, 1);
915 iter->tree=entry;
918 //Add a new file as a new entry
919 void pt_add_file(play_tree_t** ppt, char* filename)
921 play_tree_t *pt = *ppt, *entry = play_tree_new();
922 #ifdef MP_DEBUG
923 assert(entry!=NULL);
924 #endif
926 play_tree_add_file(entry, filename);
927 if (pt)
928 play_tree_append_entry(pt, entry);
929 else
931 pt=entry;
932 *ppt=pt;
934 play_tree_set_params_from(entry,pt);
937 void pt_add_gui_file(play_tree_t** ppt, char* path, char* file)
939 char* wholename = malloc(strlen(path)+strlen(file)+2);
941 if (wholename)
943 strcpy(wholename, path);
944 strcat(wholename, "/");
945 strcat(wholename, file);
946 pt_add_file(ppt, wholename);
947 free(wholename); // As pt_add_file strdups it anyway!
951 void pt_iter_goto_head(play_tree_iter_t* iter)
953 iter->tree=iter->root;
954 play_tree_iter_step(iter, 0, 0);