renamed vidixlib.c to vidix.c
[mplayer/greg.git] / playtree.c
bloba10c43703fe6a551209549b03a79b4a6fe9cb7cc
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 && iter->entry_pushed > 0) {
714 m_config_pop(iter->config);
715 iter->entry_pushed = 0;
716 if(iter->mode == PLAY_TREE_ITER_RND)
717 iter->tree->flags |= PLAY_TREE_RND_PLAYED;
720 return play_tree_iter_step(iter,d,with_nodes);
724 play_tree_iter_down_step(play_tree_iter_t* iter, int d,int with_nodes) {
726 #ifdef MP_DEBUG
727 assert(iter->tree->files == NULL);
728 assert(iter->tree->child != NULL);
729 assert(iter->tree->child->parent == iter->tree);
730 //printf("PT : Go DOWN\n");
731 #endif
733 iter->file = -1;
735 // Push subtree params
736 if(iter->config)
737 play_tree_iter_push_params(iter);
739 iter->stack_size++;
740 iter->status_stack = (int*)realloc(iter->status_stack,iter->stack_size*sizeof(int));
741 if(iter->status_stack == NULL) {
742 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",iter->stack_size*(int)sizeof(int));
743 return PLAY_TREE_ITER_ERROR;
745 iter->status_stack[iter->stack_size-1] = iter->loop;
746 // Set new status
747 iter->loop = iter->tree->loop-1;
748 if(d >= 0)
749 iter->tree = iter->tree->child;
750 else {
751 play_tree_t* pt;
752 for(pt = iter->tree->child ; pt->next != NULL ; pt = pt->next)
753 /*NOTING*/;
754 iter->tree = pt;
757 return play_tree_iter_step(iter,0,with_nodes);
760 char*
761 play_tree_iter_get_file(play_tree_iter_t* iter, int d) {
762 #ifdef MP_DEBUG
763 assert(iter != NULL);
764 assert(iter->tree->child == NULL);
765 #endif
767 if(iter->tree->files == NULL)
768 return NULL;
770 #ifdef MP_DEBUG
771 assert(iter->num_files > 0);
772 #endif
774 if(iter->file >= iter->num_files-1 || iter->file < -1)
775 return NULL;
777 if(d > 0) {
778 if(iter->file >= iter->num_files - 1)
779 iter->file = 0;
780 else
781 iter->file++;
782 } else if(d < 0) {
783 if(iter->file <= 0)
784 iter->file = iter->num_files - 1;
785 else
786 iter->file--;
788 return iter->tree->files[iter->file];
791 play_tree_t*
792 play_tree_cleanup(play_tree_t* pt) {
793 play_tree_t* iter, *tmp, *first;
795 #ifdef MP_DEBUG
796 assert(pt != NULL);
797 #endif
799 if( ! play_tree_is_valid(pt)) {
800 play_tree_remove(pt,1,1);
801 return NULL;
804 first = pt->child;
806 for(iter = pt->child ; iter != NULL ; ) {
807 tmp = iter;
808 iter = iter->next;
809 if(! play_tree_is_valid(tmp)) {
810 play_tree_remove(tmp,1,1);
811 if(tmp == first) first = iter;
815 for(iter = first ; iter != NULL ; ) {
816 tmp = iter;
817 iter = iter->next;
818 play_tree_cleanup(tmp);
821 return pt;
825 play_tree_iter_t*
826 play_tree_iter_new_copy(play_tree_iter_t* old) {
827 play_tree_iter_t* iter;
829 #ifdef MP_DEBUG
830 assert(old != NULL);
831 #endif
833 iter = malloc(sizeof(play_tree_iter_t));
834 if(iter == NULL) {
835 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",(int)sizeof(play_tree_iter_t));
836 return NULL;
839 memcpy(iter,old,sizeof(play_tree_iter_t));
840 if(old->status_stack) {
841 iter->status_stack = malloc(old->stack_size * sizeof(int));
842 if(iter->status_stack == NULL) {
843 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",old->stack_size * (int)sizeof(int));
844 free(iter);
845 return NULL;
847 memcpy(iter->status_stack,old->status_stack,iter->stack_size*sizeof(int));
849 iter->config = NULL;
851 return iter;
854 // HIGH Level API, by Fabian Franz (mplayer@fabian-franz.de)
856 play_tree_iter_t* pt_iter_create(play_tree_t** ppt, m_config_t* config)
858 play_tree_iter_t* r=NULL;
859 #ifdef MP_DEBUG
860 assert(*ppt!=NULL);
861 #endif
863 *ppt=play_tree_cleanup(*ppt);
865 if(*ppt) {
866 r = play_tree_iter_new(*ppt,config);
867 if (r && play_tree_iter_step(r,0,0) != PLAY_TREE_ITER_ENTRY)
869 play_tree_iter_free(r);
870 r = NULL;
874 return r;
877 void pt_iter_destroy(play_tree_iter_t** iter)
879 if (iter && *iter)
881 free(*iter);
882 iter=NULL;
886 char* pt_iter_get_file(play_tree_iter_t* iter, int d)
888 int i=0;
889 char* r;
891 if (iter==NULL)
892 return NULL;
894 r = play_tree_iter_get_file(iter,d);
896 while (!r && d!=0)
898 if (play_tree_iter_step(iter,d,0) != PLAY_TREE_ITER_ENTRY)
899 break;
900 r=play_tree_iter_get_file(iter,d);
901 i++;
904 return r;
907 void pt_iter_insert_entry(play_tree_iter_t* iter, play_tree_t* entry)
909 play_tree_t *pt = iter->tree;
910 #ifdef MP_DEBUG
911 assert(pt!=NULL);
912 assert(entry!=NULL);
913 assert(entry!=pt);
914 #endif
916 play_tree_insert_entry(pt, entry);
917 play_tree_set_params_from(entry,pt);
920 void pt_iter_replace_entry(play_tree_iter_t* iter, play_tree_t* entry)
922 play_tree_t *pt = iter->tree;
924 pt_iter_insert_entry(iter, entry);
925 play_tree_remove(pt, 1, 1);
926 iter->tree=entry;
929 //Add a new file as a new entry
930 void pt_add_file(play_tree_t** ppt, char* filename)
932 play_tree_t *pt = *ppt, *entry = play_tree_new();
933 #ifdef MP_DEBUG
934 assert(entry!=NULL);
935 #endif
937 play_tree_add_file(entry, filename);
938 if (pt)
939 play_tree_append_entry(pt, entry);
940 else
942 pt=entry;
943 *ppt=pt;
945 play_tree_set_params_from(entry,pt);
948 void pt_add_gui_file(play_tree_t** ppt, char* path, char* file)
950 char* wholename = malloc(strlen(path)+strlen(file)+2);
952 if (wholename)
954 strcpy(wholename, path);
955 strcat(wholename, "/");
956 strcat(wholename, file);
957 pt_add_file(ppt, wholename);
958 free(wholename); // As pt_add_file strdups it anyway!
962 void pt_iter_goto_head(play_tree_iter_t* iter)
964 iter->tree=iter->root;
965 play_tree_iter_step(iter, 0, 0);