Merge svn changes up to r27441
[mplayer.git] / playtree.c
blobead621b4fef4eb1f217d0c7c8d4f5a0d52bbb27a
2 /// \file
3 /// \ingroup Playtree
5 #include "config.h"
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #ifdef MP_DEBUG
12 #include <assert.h>
13 #endif
14 #include "m_config.h"
15 #include "playtree.h"
16 #include "mp_msg.h"
18 static int
19 play_tree_is_valid(play_tree_t* pt);
21 play_tree_t*
22 play_tree_new(void) {
23 play_tree_t* r = calloc(1,sizeof(play_tree_t));
24 if(r == NULL)
25 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",(int)sizeof(play_tree_t));
26 r->entry_type = PLAY_TREE_ENTRY_NODE;
27 return r;
30 void
31 play_tree_free(play_tree_t* pt, int children) {
32 play_tree_t* iter;
34 #ifdef MP_DEBUG
35 assert(pt != NULL);
36 #endif
38 if(children) {
39 for(iter = pt->child; iter != NULL; ) {
40 play_tree_t* nxt=iter->next;
41 play_tree_free(iter,1);
42 iter = nxt;
44 pt->child = NULL;
47 play_tree_remove(pt,0,0);
49 for(iter = pt->child ; iter != NULL ; iter = iter->next)
50 iter->parent = NULL;
52 //if(pt->params) free(pt->params);
53 if(pt->files) {
54 int i;
55 for(i = 0 ; pt->files[i] != NULL ; i++)
56 free(pt->files[i]);
57 free(pt->files);
60 free(pt);
63 void
64 play_tree_free_list(play_tree_t* pt, int children) {
65 play_tree_t* iter;
67 #ifdef MP_DEBUG
68 assert(pt != NULL);
69 #endif
71 for(iter = pt ; iter->prev != NULL ; iter = iter->prev)
72 /* NOTHING */;
74 while(iter) {
75 play_tree_t* nxt = iter->next;
76 play_tree_free(iter, children);
77 iter = nxt;
83 void
84 play_tree_append_entry(play_tree_t* pt, play_tree_t* entry) {
85 play_tree_t* iter;
87 #ifdef MP_DEBUG
88 assert(pt != NULL);
89 assert(entry != NULL);
90 #endif
92 if(pt == entry)
93 return;
95 for(iter = pt ; iter->next != NULL ; iter = iter->next)
96 /* NOTHING */;
98 entry->parent = iter->parent;
99 entry->prev = iter;
100 entry->next = NULL;
101 iter->next = entry;
104 void
105 play_tree_prepend_entry(play_tree_t* pt, play_tree_t* entry) {
106 play_tree_t* iter;
108 #ifdef MP_DEBUG
109 assert(pt != NULL);
110 assert(entry != NULL);
111 #endif
113 for(iter = pt ; iter->prev != NULL; iter = iter->prev)
114 /* NOTHING */;
116 entry->prev = NULL;
117 entry->next = iter;
118 entry->parent = iter->parent;
120 iter->prev = entry;
121 if(entry->parent) {
122 #ifdef MP_DEBUG
123 assert(entry->parent->child == iter);
124 #endif
125 entry->parent->child = entry;
129 void
130 play_tree_insert_entry(play_tree_t* pt, play_tree_t* entry) {
132 #ifdef MP_DEBUG
133 assert(pt != NULL);
134 assert(entry != NULL);
135 #endif
137 entry->parent = pt->parent;
138 entry->prev = pt;
139 if(pt->next) {
140 #ifdef MP_DEBUG
141 assert(pt->next->prev == pt);
142 #endif
143 entry->next = pt->next;
144 entry->next->prev = entry;
145 } else
146 entry->next = NULL;
147 pt->next = entry;
151 void
152 play_tree_remove(play_tree_t* pt, int free_it, int with_children) {
154 #ifdef MP_DEBUG
155 assert(pt != NULL);
156 #endif
158 // Middle of list
159 if(pt->prev && pt->next) {
160 #ifdef MP_DEBUG
161 assert(pt->prev->next == pt);
162 assert(pt->next->prev == pt);
163 #endif
164 pt->prev->next = pt->next;
165 pt->next->prev = pt->prev;
166 } // End of list
167 else if(pt->prev) {
168 #ifdef MP_DEBUG
169 assert(pt->prev->next == pt);
170 #endif
171 pt->prev->next = NULL;
172 } // Beginning of list
173 else if(pt->next) {
174 #ifdef MP_DEBUG
175 assert(pt->next->prev == pt);
176 #endif
177 pt->next->prev = NULL;
178 if(pt->parent) {
179 #ifdef MP_DEBUG
180 assert(pt->parent->child == pt);
181 #endif
182 pt->parent->child = pt->next;
184 } // The only one
185 else if(pt->parent) {
186 #ifdef MP_DEBUG
187 assert(pt->parent->child == pt);
188 #endif
189 pt->parent->child = NULL;
192 pt->prev = pt->next = pt->parent = NULL;
193 if(free_it)
194 play_tree_free(pt,with_children);
198 void
199 play_tree_set_child(play_tree_t* pt, play_tree_t* child) {
200 play_tree_t* iter;
202 #ifdef MP_DEBUG
203 assert(pt != NULL);
204 assert(pt->entry_type == PLAY_TREE_ENTRY_NODE);
205 #endif
207 //DEBUG_FF: Where are the children freed?
208 // Attention in using this function!
209 for(iter = pt->child ; iter != NULL ; iter = iter->next)
210 iter->parent = NULL;
212 // Go back to first one
213 for(iter = child ; iter->prev != NULL ; iter = iter->prev)
214 /* NOTHING */;
216 pt->child = iter;
218 for( ; iter != NULL ; iter= iter->next)
219 iter->parent = pt;
223 void
224 play_tree_set_parent(play_tree_t* pt, play_tree_t* parent) {
225 play_tree_t* iter;
227 #ifdef MP_DEBUG
228 assert(pt != NULL);
229 #endif
231 if(pt->parent)
232 pt->parent->child = NULL;
234 for(iter = pt ; iter != NULL ; iter = iter->next)
235 iter->parent = parent;
237 if(pt->prev) {
238 for(iter = pt->prev ; iter->prev != NULL ; iter = iter->prev)
239 iter->parent = parent;
240 iter->parent = parent;
241 parent->child = iter;
242 } else
243 parent->child = pt;
248 void
249 play_tree_add_file(play_tree_t* pt,char* file) {
250 int n = 0;
251 char* e;
253 #ifdef MP_DEBUG
254 assert(pt != NULL);
255 assert(pt->child == NULL);
256 assert(file != NULL);
257 #endif
259 if(pt->entry_type != PLAY_TREE_ENTRY_NODE &&
260 pt->entry_type != PLAY_TREE_ENTRY_FILE)
261 return;
263 if(pt->files) {
264 for(n = 0 ; pt->files[n] != NULL ; n++)
265 /* NOTHING */;
267 pt->files = (char**)realloc(pt->files,(n+2)*sizeof(char*));
268 if(pt->files ==NULL) {
269 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",(n+2)*(int)sizeof(char*));
270 return;
273 e = pt->files[n] = strdup(file);
274 pt->files[n+1] = NULL;
276 pt->entry_type = PLAY_TREE_ENTRY_FILE;
281 play_tree_remove_file(play_tree_t* pt,char* file) {
282 int n,f = -1;
284 #ifdef MP_DEBUG
285 assert(pt != NULL);
286 assert(file != NULL);
287 assert(pt->entry_type != PLAY_TREE_ENTRY_NODE);
288 #endif
290 for(n=0 ; pt->files[n] != NULL ; n++) {
291 if(strcmp(file,pt->files[n]) == 0)
292 f = n;
295 if(f < 0) // Not found
296 return 0;
298 #ifdef MP_DEBUG
299 assert(n > f);
300 #endif
302 free(pt->files[f]);
304 if(n > 1) {
305 memmove(&pt->files[f],&pt->files[f+1],(n-f)*sizeof(char*));
306 pt->files = (char**)realloc(pt->files,n*sizeof(char*));
307 if(pt->files == NULL) {
308 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",(n+2)*(int)sizeof(char*));
309 return -1;
311 } else {
312 free(pt->files);
313 pt->files = NULL;
316 return 1;
319 void
320 play_tree_set_param(play_tree_t* pt, char* name, char* val) {
321 int n = 0,ni = -1;
323 #ifdef MP_DEBUG
324 assert(pt != NULL);
325 assert(name != NULL);
326 #endif
328 if(pt->params) {
329 for( ; pt->params[n].name != NULL ; n++) {
330 if(strcasecmp(pt->params[n].name,name) == 0)
331 ni = n;
335 if(ni > 0) {
336 if(pt->params[n].value != NULL) free(pt->params[n].value);
337 pt->params[n].value = val != NULL ? strdup(val) : NULL;
338 return;
341 pt->params = (play_tree_param_t*)realloc(pt->params,(n+2)*sizeof(play_tree_param_t));
342 if(pt->params == NULL) {
343 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't realloc params (%d bytes of memory)\n",(n+2)*(int)sizeof(play_tree_param_t));
344 return;
346 pt->params[n].name = strdup(name);
347 pt->params[n].value = val != NULL ? strdup(val) : NULL;
348 memset(&pt->params[n+1],0,sizeof(play_tree_param_t));
350 return;
354 play_tree_unset_param(play_tree_t* pt, char* name) {
355 int n,ni = -1;
357 #ifdef MP_DEBUG
358 assert(pt != NULL);
359 assert(name != NULL);
360 assert(pt->params != NULL);
361 #endif
363 for(n = 0 ; pt->params[n].name != NULL ; n++) {
364 if(strcasecmp(pt->params[n].name,name) == 0)
365 ni = n;
368 if(ni < 0)
369 return 0;
371 if(pt->params[ni].name) free(pt->params[ni].name);
372 if(pt->params[ni].value) free(pt->params[ni].value);
374 if(n > 1) {
375 memmove(&pt->params[ni],&pt->params[ni+1],(n-ni)*sizeof(play_tree_param_t));
376 pt->params = (play_tree_param_t*)realloc(pt->params,n*sizeof(play_tree_param_t));
377 if(pt->params == NULL) {
378 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",n*(int)sizeof(play_tree_param_t));
379 return -1;
381 } else {
382 free(pt->params);
383 pt->params = NULL;
386 return 1;
389 void
390 play_tree_set_params_from(play_tree_t* dest,play_tree_t* src) {
391 int i;
393 #ifdef MP_DEBUG
394 assert(dest != NULL);
395 assert(src != NULL);
396 #endif
398 if(!src->params)
399 return;
401 for(i = 0; src->params[i].name != NULL ; i++)
402 play_tree_set_param(dest,src->params[i].name,src->params[i].value);
403 if(src->flags & PLAY_TREE_RND) // pass the random flag too
404 dest->flags |= PLAY_TREE_RND;
408 // all children if deep < 0
409 void
410 play_tree_set_flag(play_tree_t* pt, int flags , int deep) {
411 play_tree_t* i;
413 pt->flags |= flags;
415 if(deep && pt->child) {
416 if(deep > 0) deep--;
417 for(i = pt->child ; i ; i = i->next)
418 play_tree_set_flag(i,flags,deep);
422 void
423 play_tree_unset_flag(play_tree_t* pt, int flags , int deep) {
424 play_tree_t* i;
426 pt->flags &= ~flags;
428 if(deep && pt->child) {
429 if(deep > 0) deep--;
430 for(i = pt->child ; i ; i = i->next)
431 play_tree_unset_flag(i,flags,deep);
436 //////////////////////////////////// ITERATOR //////////////////////////////////////
438 static void
439 play_tree_iter_push_params(play_tree_iter_t* iter) {
440 int n;
441 play_tree_t* pt;
442 #ifdef MP_DEBUG
443 assert(iter != NULL);
444 assert(iter->config != NULL);
445 assert(iter->tree != NULL);
446 #endif
448 pt = iter->tree;
450 // We always push a config because we can set some option
451 // while playing
452 m_config_push(iter->config);
454 if(pt->params == NULL)
455 return;
458 for(n = 0; pt->params[n].name != NULL ; n++) {
459 int e;
460 if((e = m_config_set_option(iter->config,pt->params[n].name,pt->params[n].value)) < 0) {
461 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Error %d while setting option '%s' with value '%s'\n",e,
462 pt->params[n].name,pt->params[n].value);
466 if(!pt->child)
467 iter->entry_pushed = 1;
468 return;
471 play_tree_iter_t*
472 play_tree_iter_new(play_tree_t* pt,m_config_t* config) {
473 play_tree_iter_t* iter;
475 #ifdef MP_DEBUG
476 assert(pt != NULL);
477 assert(config != NULL);
478 #endif
480 if( ! play_tree_is_valid(pt))
481 return NULL;
483 iter = calloc(1,sizeof(play_tree_iter_t));
484 if(! iter) {
485 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate new iterator (%d bytes of memory)\n",(int)sizeof(play_tree_iter_t));
486 return NULL;
488 iter->root = pt;
489 iter->tree = NULL;
490 iter->config = config;
492 if(pt->parent)
493 iter->loop = pt->parent->loop;
495 return iter;
498 void
499 play_tree_iter_free(play_tree_iter_t* iter) {
501 #ifdef MP_DEBUG
502 assert(iter != NULL);
503 #endif
505 if(iter->status_stack) {
506 #ifdef MP_DEBUG
507 assert(iter->stack_size > 0);
508 #endif
509 free(iter->status_stack);
512 free(iter);
515 static play_tree_t*
516 play_tree_rnd_step(play_tree_t* pt) {
517 int count = 0;
518 int r;
519 play_tree_t *i,*head;
521 // Count how many free choice we have
522 for(i = pt ; i->prev ; i = i->prev)
523 if(!(i->flags & PLAY_TREE_RND_PLAYED)) count++;
524 head = i;
525 if(!(i->flags & PLAY_TREE_RND_PLAYED)) count++;
526 for(i = pt->next ; i ; i = i->next)
527 if(!(i->flags & PLAY_TREE_RND_PLAYED)) count++;
529 if(!count) return NULL;
531 r = (int)((float)(count) * rand() / (RAND_MAX + 1.0));
533 for(i = head ; i ; i=i->next) {
534 if(!(i->flags & PLAY_TREE_RND_PLAYED)) r--;
535 if(r < 0) return i;
538 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Random stepping error\n");
539 return NULL;
544 play_tree_iter_step(play_tree_iter_t* iter, int d,int with_nodes) {
545 play_tree_t* pt;
547 if ( !iter ) return PLAY_TREE_ITER_ENTRY;
548 if ( !iter->root ) return PLAY_TREE_ITER_ENTRY;
550 #ifdef MP_DEBUG
551 assert(iter != NULL);
552 assert(iter->root != NULL);
553 //printf("PT : Stepping = %d\n",d);
554 #endif
556 if(iter->tree == NULL) {
557 iter->tree = iter->root;
558 return play_tree_iter_step(iter,0,with_nodes);
561 if(iter->config && iter->entry_pushed > 0) {
562 iter->entry_pushed = 0;
563 m_config_pop(iter->config);
566 if(iter->tree->parent && (iter->tree->parent->flags & PLAY_TREE_RND))
567 iter->mode = PLAY_TREE_ITER_RND;
568 else
569 iter->mode = PLAY_TREE_ITER_NORMAL;
571 iter->file = -1;
572 if(iter->mode == PLAY_TREE_ITER_RND)
573 pt = play_tree_rnd_step(iter->tree);
574 else if( d > 0 ) {
575 int i;
576 pt = iter->tree;
577 for(i = d ; i > 0 && pt ; i--)
578 pt = pt->next;
579 d = i ? i : 1;
580 } else if(d < 0) {
581 int i;
582 pt = iter->tree;
583 for(i = d ; i < 0 && pt ; i++)
584 pt = pt->prev;
585 d = i ? i : -1;
586 } else
587 pt = iter->tree;
589 if(pt == NULL) { // No next
590 // Must we loop?
591 if (iter->mode == PLAY_TREE_ITER_RND) {
592 if (iter->root->loop == 0)
593 return PLAY_TREE_ITER_END;
594 play_tree_unset_flag(iter->root, PLAY_TREE_RND_PLAYED, -1);
595 if (iter->root->loop > 0) iter->root->loop--;
596 // try again
597 return play_tree_iter_step(iter, 0, with_nodes);
598 } else
599 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) ) ) ) {
600 if(d > 0) { // Go back to the first one
601 for(pt = iter->tree ; pt->prev != NULL; pt = pt->prev)
602 /* NOTHNG */;
603 if(iter->loop > 0) iter->loop--;
604 } else if( d < 0 ) { // Or the last one
605 for(pt = iter->tree ; pt->next != NULL; pt = pt->next)
606 /* NOTHNG */;
607 if(iter->loop >= 0 && iter->loop < iter->tree->parent->loop) iter->loop++;
609 iter->tree = pt;
610 return play_tree_iter_step(iter,0,with_nodes);
612 // Go up one level
613 return play_tree_iter_up_step(iter,d,with_nodes);
617 // Is there any valid child?
618 if(pt->child && play_tree_is_valid(pt->child)) {
619 iter->tree = pt;
620 if(with_nodes) { // Stop on the node
621 return PLAY_TREE_ITER_NODE;
622 } else // Or follow it
623 return play_tree_iter_down_step(iter,d,with_nodes);
626 // Is it a valid entry?
627 if(! play_tree_is_valid(pt)) {
628 if(d == 0) { // Can this happen ? FF: Yes!
629 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"What to do now ???? Infinite loop if we continue\n");
630 return PLAY_TREE_ITER_ERROR;
631 } // Not a valid entry : go to next one
632 return play_tree_iter_step(iter,d,with_nodes);
635 #ifdef MP_DEBUG
636 assert(pt->files != NULL);
637 #endif
639 iter->tree = pt;
641 for(d = 0 ; iter->tree->files[d] != NULL ; d++)
642 /* NOTHING */;
643 iter->num_files = d;
645 if(iter->config) {
646 play_tree_iter_push_params(iter);
647 iter->entry_pushed = 1;
648 if(iter->mode == PLAY_TREE_ITER_RND)
649 pt->flags |= PLAY_TREE_RND_PLAYED;
652 return PLAY_TREE_ITER_ENTRY;
656 static int
657 play_tree_is_valid(play_tree_t* pt) {
658 play_tree_t* iter;
660 #ifdef MP_DEBUG
661 assert(pt != NULL);
662 #endif
664 if(pt->entry_type != PLAY_TREE_ENTRY_NODE) {
665 #ifdef MP_DEBUG
666 assert(pt->child == NULL);
667 #endif
668 return 1;
670 else if (pt->child != NULL) {
671 for(iter = pt->child ; iter != NULL ; iter = iter->next) {
672 if(play_tree_is_valid(iter))
673 return 1;
676 return 0;
680 play_tree_iter_up_step(play_tree_iter_t* iter, int d,int with_nodes) {
682 #ifdef MP_DEBUG
683 assert(iter != NULL);
684 assert(iter->tree != NULL);
685 //printf("PT : Go UP\n");
686 #endif
688 iter->file = -1;
689 if(iter->tree->parent == iter->root->parent)
690 return PLAY_TREE_ITER_END;
692 #ifdef MP_DEBUG
693 assert(iter->tree->parent != NULL);
694 assert(iter->stack_size > 0);
695 assert(iter->status_stack != NULL);
696 #endif
698 iter->stack_size--;
699 iter->loop = iter->status_stack[iter->stack_size];
700 if(iter->stack_size > 0)
701 iter->status_stack = (int*)realloc(iter->status_stack,iter->stack_size*sizeof(int));
702 else {
703 free(iter->status_stack);
704 iter->status_stack = NULL;
706 if(iter->stack_size > 0 && iter->status_stack == NULL) {
707 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",iter->stack_size*(int)sizeof(char*));
708 return PLAY_TREE_ITER_ERROR;
710 iter->tree = iter->tree->parent;
712 // Pop subtree params
713 if(iter->config) {
714 m_config_pop(iter->config);
715 if(iter->mode == PLAY_TREE_ITER_RND)
716 iter->tree->flags |= PLAY_TREE_RND_PLAYED;
719 return play_tree_iter_step(iter,d,with_nodes);
723 play_tree_iter_down_step(play_tree_iter_t* iter, int d,int with_nodes) {
725 #ifdef MP_DEBUG
726 assert(iter->tree->files == NULL);
727 assert(iter->tree->child != NULL);
728 assert(iter->tree->child->parent == iter->tree);
729 //printf("PT : Go DOWN\n");
730 #endif
732 iter->file = -1;
734 // Push subtree params
735 if(iter->config)
736 play_tree_iter_push_params(iter);
738 iter->stack_size++;
739 iter->status_stack = (int*)realloc(iter->status_stack,iter->stack_size*sizeof(int));
740 if(iter->status_stack == NULL) {
741 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",iter->stack_size*(int)sizeof(int));
742 return PLAY_TREE_ITER_ERROR;
744 iter->status_stack[iter->stack_size-1] = iter->loop;
745 // Set new status
746 iter->loop = iter->tree->loop-1;
747 if(d >= 0)
748 iter->tree = iter->tree->child;
749 else {
750 play_tree_t* pt;
751 for(pt = iter->tree->child ; pt->next != NULL ; pt = pt->next)
752 /*NOTING*/;
753 iter->tree = pt;
756 return play_tree_iter_step(iter,0,with_nodes);
759 char*
760 play_tree_iter_get_file(play_tree_iter_t* iter, int d) {
761 #ifdef MP_DEBUG
762 assert(iter != NULL);
763 assert(iter->tree->child == NULL);
764 #endif
766 if(iter->tree->files == NULL)
767 return NULL;
769 #ifdef MP_DEBUG
770 assert(iter->num_files > 0);
771 #endif
773 if(iter->file >= iter->num_files-1 || iter->file < -1)
774 return NULL;
776 if(d > 0) {
777 if(iter->file >= iter->num_files - 1)
778 iter->file = 0;
779 else
780 iter->file++;
781 } else if(d < 0) {
782 if(iter->file <= 0)
783 iter->file = iter->num_files - 1;
784 else
785 iter->file--;
787 return iter->tree->files[iter->file];
790 play_tree_t*
791 play_tree_cleanup(play_tree_t* pt) {
792 play_tree_t* iter, *tmp, *first;
794 #ifdef MP_DEBUG
795 assert(pt != NULL);
796 #endif
798 if( ! play_tree_is_valid(pt)) {
799 play_tree_remove(pt,1,1);
800 return NULL;
803 first = pt->child;
805 for(iter = pt->child ; iter != NULL ; ) {
806 tmp = iter;
807 iter = iter->next;
808 if(! play_tree_is_valid(tmp)) {
809 play_tree_remove(tmp,1,1);
810 if(tmp == first) first = iter;
814 for(iter = first ; iter != NULL ; ) {
815 tmp = iter;
816 iter = iter->next;
817 play_tree_cleanup(tmp);
820 return pt;
824 play_tree_iter_t*
825 play_tree_iter_new_copy(play_tree_iter_t* old) {
826 play_tree_iter_t* iter;
828 #ifdef MP_DEBUG
829 assert(old != NULL);
830 #endif
832 iter = malloc(sizeof(play_tree_iter_t));
833 if(iter == NULL) {
834 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",(int)sizeof(play_tree_iter_t));
835 return NULL;
838 memcpy(iter,old,sizeof(play_tree_iter_t));
839 if(old->status_stack) {
840 iter->status_stack = malloc(old->stack_size * sizeof(int));
841 if(iter->status_stack == NULL) {
842 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",old->stack_size * (int)sizeof(int));
843 free(iter);
844 return NULL;
846 memcpy(iter->status_stack,old->status_stack,iter->stack_size*sizeof(int));
848 iter->config = NULL;
850 return iter;
853 // HIGH Level API, by Fabian Franz (mplayer@fabian-franz.de)
855 play_tree_iter_t* pt_iter_create(play_tree_t** ppt, m_config_t* config)
857 play_tree_iter_t* r=NULL;
858 #ifdef MP_DEBUG
859 assert(*ppt!=NULL);
860 #endif
862 *ppt=play_tree_cleanup(*ppt);
864 if(*ppt) {
865 r = play_tree_iter_new(*ppt,config);
866 if (r && play_tree_iter_step(r,0,0) != PLAY_TREE_ITER_ENTRY)
868 play_tree_iter_free(r);
869 r = NULL;
873 return r;
876 void pt_iter_destroy(play_tree_iter_t** iter)
878 if (iter && *iter)
880 free(*iter);
881 iter=NULL;
885 char* pt_iter_get_file(play_tree_iter_t* iter, int d)
887 int i=0;
888 char* r;
890 if (iter==NULL)
891 return NULL;
893 r = play_tree_iter_get_file(iter,d);
895 while (!r && d!=0)
897 if (play_tree_iter_step(iter,d,0) != PLAY_TREE_ITER_ENTRY)
898 break;
899 r=play_tree_iter_get_file(iter,d);
900 i++;
903 return r;
906 void pt_iter_insert_entry(play_tree_iter_t* iter, play_tree_t* entry)
908 play_tree_t *pt = iter->tree;
909 #ifdef MP_DEBUG
910 assert(pt!=NULL);
911 assert(entry!=NULL);
912 assert(entry!=pt);
913 #endif
915 play_tree_insert_entry(pt, entry);
916 play_tree_set_params_from(entry,pt);
919 void pt_iter_replace_entry(play_tree_iter_t* iter, play_tree_t* entry)
921 play_tree_t *pt = iter->tree;
923 pt_iter_insert_entry(iter, entry);
924 play_tree_remove(pt, 1, 1);
925 iter->tree=entry;
928 //Add a new file as a new entry
929 void pt_add_file(play_tree_t** ppt, char* filename)
931 play_tree_t *pt = *ppt, *entry = play_tree_new();
932 #ifdef MP_DEBUG
933 assert(entry!=NULL);
934 #endif
936 play_tree_add_file(entry, filename);
937 if (pt)
938 play_tree_append_entry(pt, entry);
939 else
941 pt=entry;
942 *ppt=pt;
944 play_tree_set_params_from(entry,pt);
947 void pt_add_gui_file(play_tree_t** ppt, char* path, char* file)
949 char* wholename = malloc(strlen(path)+strlen(file)+2);
951 if (wholename)
953 strcpy(wholename, path);
954 strcat(wholename, "/");
955 strcat(wholename, file);
956 pt_add_file(ppt, wholename);
957 free(wholename); // As pt_add_file strdups it anyway!
961 void pt_iter_goto_head(play_tree_iter_t* iter)
963 iter->tree=iter->root;
964 play_tree_iter_step(iter, 0, 0);