stream_cdda: work around libcdparanoia caching issues
[mplayer.git] / playtree.c
blobc9a4cbc69a360dc5cb2717a94508e2af28ff7f48
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 /// \file
20 /// \ingroup Playtree
22 #include "config.h"
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #ifdef MP_DEBUG
29 #include <assert.h>
30 #endif
31 #include "m_config.h"
32 #include "playtree.h"
33 #include "mp_msg.h"
35 static int
36 play_tree_is_valid(play_tree_t* pt);
38 play_tree_t*
39 play_tree_new(void) {
40 play_tree_t* r = calloc(1,sizeof(play_tree_t));
41 if(r == NULL) {
42 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",(int)sizeof(play_tree_t));
43 return NULL;
45 r->entry_type = PLAY_TREE_ENTRY_NODE;
46 return r;
49 void
50 play_tree_free(play_tree_t* pt, int children) {
51 play_tree_t* iter;
53 #ifdef MP_DEBUG
54 assert(pt != NULL);
55 #endif
57 if(children) {
58 for(iter = pt->child; iter != NULL; ) {
59 play_tree_t* nxt=iter->next;
60 play_tree_free(iter,1);
61 iter = nxt;
63 pt->child = NULL;
66 play_tree_remove(pt,0,0);
68 for(iter = pt->child ; iter != NULL ; iter = iter->next)
69 iter->parent = NULL;
71 if (pt->params) {
72 int i;
73 for(i = 0 ; pt->params[i].name != NULL ; i++) {
74 free(pt->params[i].name);
75 free(pt->params[i].value);
77 free(pt->params);
79 if(pt->files) {
80 int i;
81 for(i = 0 ; pt->files[i] != NULL ; i++)
82 free(pt->files[i]);
83 free(pt->files);
86 free(pt);
89 void
90 play_tree_free_list(play_tree_t* pt, int children) {
91 play_tree_t* iter;
93 #ifdef MP_DEBUG
94 assert(pt != NULL);
95 #endif
97 for(iter = pt ; iter->prev != NULL ; iter = iter->prev)
98 /* NOTHING */;
100 while(iter) {
101 play_tree_t* nxt = iter->next;
102 play_tree_free(iter, children);
103 iter = nxt;
109 void
110 play_tree_append_entry(play_tree_t* pt, play_tree_t* entry) {
111 play_tree_t* iter;
113 #ifdef MP_DEBUG
114 assert(pt != NULL);
115 assert(entry != NULL);
116 #endif
118 if(pt == entry)
119 return;
121 for(iter = pt ; iter->next != NULL ; iter = iter->next)
122 /* NOTHING */;
124 entry->parent = iter->parent;
125 entry->prev = iter;
126 entry->next = NULL;
127 iter->next = entry;
130 void
131 play_tree_prepend_entry(play_tree_t* pt, play_tree_t* entry) {
132 play_tree_t* iter;
134 #ifdef MP_DEBUG
135 assert(pt != NULL);
136 assert(entry != NULL);
137 #endif
139 for(iter = pt ; iter->prev != NULL; iter = iter->prev)
140 /* NOTHING */;
142 entry->prev = NULL;
143 entry->next = iter;
144 entry->parent = iter->parent;
146 iter->prev = entry;
147 if(entry->parent) {
148 #ifdef MP_DEBUG
149 assert(entry->parent->child == iter);
150 #endif
151 entry->parent->child = entry;
155 void
156 play_tree_insert_entry(play_tree_t* pt, play_tree_t* entry) {
158 #ifdef MP_DEBUG
159 assert(pt != NULL);
160 assert(entry != NULL);
161 #endif
163 entry->parent = pt->parent;
164 entry->prev = pt;
165 if(pt->next) {
166 #ifdef MP_DEBUG
167 assert(pt->next->prev == pt);
168 #endif
169 entry->next = pt->next;
170 entry->next->prev = entry;
171 } else
172 entry->next = NULL;
173 pt->next = entry;
177 void
178 play_tree_remove(play_tree_t* pt, int free_it, int with_children) {
180 #ifdef MP_DEBUG
181 assert(pt != NULL);
182 #endif
184 // Middle of list
185 if(pt->prev && pt->next) {
186 #ifdef MP_DEBUG
187 assert(pt->prev->next == pt);
188 assert(pt->next->prev == pt);
189 #endif
190 pt->prev->next = pt->next;
191 pt->next->prev = pt->prev;
192 } // End of list
193 else if(pt->prev) {
194 #ifdef MP_DEBUG
195 assert(pt->prev->next == pt);
196 #endif
197 pt->prev->next = NULL;
198 } // Beginning of list
199 else if(pt->next) {
200 #ifdef MP_DEBUG
201 assert(pt->next->prev == pt);
202 #endif
203 pt->next->prev = NULL;
204 if(pt->parent) {
205 #ifdef MP_DEBUG
206 assert(pt->parent->child == pt);
207 #endif
208 pt->parent->child = pt->next;
210 } // The only one
211 else if(pt->parent) {
212 #ifdef MP_DEBUG
213 assert(pt->parent->child == pt);
214 #endif
215 pt->parent->child = NULL;
218 pt->prev = pt->next = pt->parent = NULL;
219 if(free_it)
220 play_tree_free(pt,with_children);
224 void
225 play_tree_set_child(play_tree_t* pt, play_tree_t* child) {
226 play_tree_t* iter;
228 /* Roughly validate input data. Both, pt and child are going to be
229 * dereferenced, hence assure they're not NULL.
231 if (!pt || !child) {
232 mp_msg(MSGT_PLAYTREE, MSGL_ERR, "Internal error, attempt to add an empty child or use empty playlist\n");
233 return;
236 #ifdef MP_DEBUG
237 assert(pt->entry_type == PLAY_TREE_ENTRY_NODE);
238 #endif
240 //DEBUG_FF: Where are the children freed?
241 // Attention in using this function!
242 for(iter = pt->child ; iter != NULL ; iter = iter->next)
243 iter->parent = NULL;
245 // Go back to first one
246 for(iter = child ; iter->prev != NULL ; iter = iter->prev)
247 /* NOTHING */;
249 pt->child = iter;
251 for( ; iter != NULL ; iter= iter->next)
252 iter->parent = pt;
256 void
257 play_tree_set_parent(play_tree_t* pt, play_tree_t* parent) {
258 play_tree_t* iter;
260 #ifdef MP_DEBUG
261 assert(pt != NULL);
262 #endif
264 if(pt->parent)
265 pt->parent->child = NULL;
267 for(iter = pt ; iter != NULL ; iter = iter->next)
268 iter->parent = parent;
270 if(pt->prev) {
271 for(iter = pt->prev ; iter->prev != NULL ; iter = iter->prev)
272 iter->parent = parent;
273 iter->parent = parent;
274 parent->child = iter;
275 } else
276 parent->child = pt;
281 void
282 play_tree_add_file(play_tree_t* pt,char* file) {
283 int n = 0;
285 #ifdef MP_DEBUG
286 assert(pt != NULL);
287 assert(pt->child == NULL);
288 assert(file != NULL);
289 #endif
291 if(pt->entry_type != PLAY_TREE_ENTRY_NODE &&
292 pt->entry_type != PLAY_TREE_ENTRY_FILE)
293 return;
295 if(pt->files) {
296 for(n = 0 ; pt->files[n] != NULL ; n++)
297 /* NOTHING */;
299 pt->files = realloc(pt->files, (n + 2) * sizeof(char*));
300 if(pt->files ==NULL) {
301 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",(n+2)*(int)sizeof(char*));
302 return;
305 pt->files[n] = strdup(file);
306 pt->files[n+1] = NULL;
308 pt->entry_type = PLAY_TREE_ENTRY_FILE;
313 play_tree_remove_file(play_tree_t* pt,char* file) {
314 int n,f = -1;
316 #ifdef MP_DEBUG
317 assert(pt != NULL);
318 assert(file != NULL);
319 assert(pt->entry_type != PLAY_TREE_ENTRY_NODE);
320 #endif
322 for(n=0 ; pt->files[n] != NULL ; n++) {
323 if(strcmp(file,pt->files[n]) == 0)
324 f = n;
327 if(f < 0) // Not found
328 return 0;
330 #ifdef MP_DEBUG
331 assert(n > f);
332 #endif
334 free(pt->files[f]);
336 if(n > 1) {
337 memmove(&pt->files[f],&pt->files[f+1],(n-f)*sizeof(char*));
338 pt->files = realloc(pt->files, n * sizeof(char*));
339 if(pt->files == NULL) {
340 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",(n+2)*(int)sizeof(char*));
341 return -1;
343 } else {
344 free(pt->files);
345 pt->files = NULL;
348 return 1;
351 void
352 play_tree_set_param(play_tree_t* pt, char* name, char* val) {
353 int n = 0;
355 #ifdef MP_DEBUG
356 assert(pt != NULL);
357 assert(name != NULL);
358 #endif
360 if(pt->params)
361 for ( ; pt->params[n].name != NULL ; n++ ) { }
363 pt->params = realloc(pt->params, (n + 2) * sizeof(play_tree_param_t));
364 if(pt->params == NULL) {
365 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't realloc params (%d bytes of memory)\n",(n+2)*(int)sizeof(play_tree_param_t));
366 return;
368 pt->params[n].name = strdup(name);
369 pt->params[n].value = val != NULL ? strdup(val) : NULL;
370 memset(&pt->params[n+1],0,sizeof(play_tree_param_t));
372 return;
376 play_tree_unset_param(play_tree_t* pt, char* name) {
377 int n,ni = -1;
379 #ifdef MP_DEBUG
380 assert(pt != NULL);
381 assert(name != NULL);
382 assert(pt->params != NULL);
383 #endif
385 for(n = 0 ; pt->params[n].name != NULL ; n++) {
386 if(strcasecmp(pt->params[n].name,name) == 0)
387 ni = n;
390 if(ni < 0)
391 return 0;
393 free(pt->params[ni].name);
394 free(pt->params[ni].value);
396 if(n > 1) {
397 memmove(&pt->params[ni],&pt->params[ni+1],(n-ni)*sizeof(play_tree_param_t));
398 pt->params = realloc(pt->params, n * sizeof(play_tree_param_t));
399 if(pt->params == NULL) {
400 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",n*(int)sizeof(play_tree_param_t));
401 return -1;
403 } else {
404 free(pt->params);
405 pt->params = NULL;
408 return 1;
411 void
412 play_tree_set_params_from(play_tree_t* dest,play_tree_t* src) {
413 int i;
415 #ifdef MP_DEBUG
416 assert(dest != NULL);
417 assert(src != NULL);
418 #endif
420 if(!src->params)
421 return;
423 for(i = 0; src->params[i].name != NULL ; i++)
424 play_tree_set_param(dest,src->params[i].name,src->params[i].value);
425 if(src->flags & PLAY_TREE_RND) // pass the random flag too
426 dest->flags |= PLAY_TREE_RND;
430 static void
431 play_tree_unset_flag(play_tree_t* pt, int flags , int deep) {
432 play_tree_t* i;
434 pt->flags &= ~flags;
436 if(deep && pt->child) {
437 if(deep > 0) deep--;
438 for(i = pt->child ; i ; i = i->next)
439 play_tree_unset_flag(i,flags,deep);
444 //////////////////////////////////// ITERATOR //////////////////////////////////////
446 static void
447 play_tree_iter_push_params(play_tree_iter_t* iter) {
448 int n;
449 play_tree_t* pt;
450 #ifdef MP_DEBUG
451 assert(iter != NULL);
452 assert(iter->config != NULL);
453 assert(iter->tree != NULL);
454 #endif
456 pt = iter->tree;
458 // We always push a config because we can set some option
459 // while playing
460 m_config_push(iter->config);
462 if(pt->params == NULL)
463 return;
466 for(n = 0; pt->params[n].name != NULL ; n++) {
467 int e;
468 if((e = m_config_set_option(iter->config,pt->params[n].name,pt->params[n].value)) < 0) {
469 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Error %d while setting option '%s' with value '%s'\n",e,
470 pt->params[n].name,pt->params[n].value);
474 if(!pt->child)
475 iter->entry_pushed = 1;
476 return;
479 play_tree_iter_t*
480 play_tree_iter_new(play_tree_t* pt,m_config_t* config) {
481 play_tree_iter_t* iter;
483 #ifdef MP_DEBUG
484 assert(pt != NULL);
485 assert(config != NULL);
486 #endif
488 if( ! play_tree_is_valid(pt))
489 return NULL;
491 iter = calloc(1,sizeof(play_tree_iter_t));
492 if(! iter) {
493 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate new iterator (%d bytes of memory)\n",(int)sizeof(play_tree_iter_t));
494 return NULL;
496 iter->root = pt;
497 iter->tree = NULL;
498 iter->config = config;
500 if(pt->parent)
501 iter->loop = pt->parent->loop;
503 return iter;
506 void
507 play_tree_iter_free(play_tree_iter_t* iter) {
509 #ifdef MP_DEBUG
510 assert(iter != NULL);
511 #endif
513 if(iter->status_stack) {
514 #ifdef MP_DEBUG
515 assert(iter->stack_size > 0);
516 #endif
517 free(iter->status_stack);
520 free(iter);
523 static play_tree_t*
524 play_tree_rnd_step(play_tree_t* pt) {
525 int count = 0;
526 int r;
527 play_tree_t *i,*head;
529 // Count how many free choice we have
530 for(i = pt ; i->prev ; i = i->prev)
531 if(!(i->flags & PLAY_TREE_RND_PLAYED)) count++;
532 head = i;
533 if(!(i->flags & PLAY_TREE_RND_PLAYED)) count++;
534 for(i = pt->next ; i ; i = i->next)
535 if(!(i->flags & PLAY_TREE_RND_PLAYED)) count++;
537 if(!count) return NULL;
539 r = (int)((float)(count) * rand() / (RAND_MAX + 1.0));
541 for(i = head ; i ; i=i->next) {
542 if(!(i->flags & PLAY_TREE_RND_PLAYED)) r--;
543 if(r < 0) return i;
546 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Random stepping error\n");
547 return NULL;
552 play_tree_iter_step(play_tree_iter_t* iter, int d,int with_nodes) {
553 play_tree_t* pt;
555 if ( !iter ) return PLAY_TREE_ITER_ENTRY;
556 if ( !iter->root ) return PLAY_TREE_ITER_ENTRY;
558 #ifdef MP_DEBUG
559 assert(iter != NULL);
560 assert(iter->root != NULL);
561 //printf("PT : Stepping = %d\n",d);
562 #endif
564 if(iter->tree == NULL) {
565 iter->tree = iter->root;
566 return play_tree_iter_step(iter,0,with_nodes);
569 if(iter->config && iter->entry_pushed > 0) {
570 iter->entry_pushed = 0;
571 m_config_pop(iter->config);
574 if(iter->tree->parent && (iter->tree->parent->flags & PLAY_TREE_RND))
575 iter->mode = PLAY_TREE_ITER_RND;
576 else
577 iter->mode = PLAY_TREE_ITER_NORMAL;
579 iter->file = -1;
580 if(iter->mode == PLAY_TREE_ITER_RND)
581 pt = play_tree_rnd_step(iter->tree);
582 else if( d > 0 ) {
583 int i;
584 pt = iter->tree;
585 for(i = d ; i > 0 && pt ; i--)
586 pt = pt->next;
587 d = i ? i : 1;
588 } else if(d < 0) {
589 int i;
590 pt = iter->tree;
591 for(i = d ; i < 0 && pt ; i++)
592 pt = pt->prev;
593 d = i ? i : -1;
594 } else
595 pt = iter->tree;
597 if(pt == NULL) { // No next
598 // Must we loop?
599 if (iter->mode == PLAY_TREE_ITER_RND) {
600 if (iter->root->loop == 0)
601 return PLAY_TREE_ITER_END;
602 play_tree_unset_flag(iter->root, PLAY_TREE_RND_PLAYED, -1);
603 if (iter->root->loop > 0) iter->root->loop--;
604 // try again
605 return play_tree_iter_step(iter, 0, with_nodes);
606 } else
607 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) ) ) ) {
608 if(d > 0) { // Go back to the first one
609 for(pt = iter->tree ; pt->prev != NULL; pt = pt->prev)
610 /* NOTHNG */;
611 if(iter->loop > 0) iter->loop--;
612 } else if( d < 0 ) { // Or the last one
613 for(pt = iter->tree ; pt->next != NULL; pt = pt->next)
614 /* NOTHNG */;
615 if(iter->loop >= 0 && iter->loop < iter->tree->parent->loop) iter->loop++;
617 iter->tree = pt;
618 return play_tree_iter_step(iter,0,with_nodes);
620 // Go up one level
621 return play_tree_iter_up_step(iter,d,with_nodes);
625 // Is there any valid child?
626 if(pt->child && play_tree_is_valid(pt->child)) {
627 iter->tree = pt;
628 if(with_nodes) { // Stop on the node
629 return PLAY_TREE_ITER_NODE;
630 } else // Or follow it
631 return play_tree_iter_down_step(iter,d,with_nodes);
634 // Is it a valid entry?
635 if(! play_tree_is_valid(pt)) {
636 if(d == 0) { // Can this happen ? FF: Yes!
637 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"What to do now ???? Infinite loop if we continue\n");
638 return PLAY_TREE_ITER_ERROR;
639 } // Not a valid entry : go to next one
640 return play_tree_iter_step(iter,d,with_nodes);
643 #ifdef MP_DEBUG
644 assert(pt->files != NULL);
645 #endif
647 iter->tree = pt;
649 for(d = 0 ; iter->tree->files[d] != NULL ; d++)
650 /* NOTHING */;
651 iter->num_files = d;
653 if(iter->config) {
654 play_tree_iter_push_params(iter);
655 iter->entry_pushed = 1;
656 if(iter->mode == PLAY_TREE_ITER_RND)
657 pt->flags |= PLAY_TREE_RND_PLAYED;
660 return PLAY_TREE_ITER_ENTRY;
664 static int
665 play_tree_is_valid(play_tree_t* pt) {
666 play_tree_t* iter;
668 #ifdef MP_DEBUG
669 assert(pt != NULL);
670 #endif
672 if(pt->entry_type != PLAY_TREE_ENTRY_NODE) {
673 #ifdef MP_DEBUG
674 assert(pt->child == NULL);
675 #endif
676 return 1;
678 else if (pt->child != NULL) {
679 for(iter = pt->child ; iter != NULL ; iter = iter->next) {
680 if(play_tree_is_valid(iter))
681 return 1;
684 return 0;
688 play_tree_iter_up_step(play_tree_iter_t* iter, int d,int with_nodes) {
690 #ifdef MP_DEBUG
691 assert(iter != NULL);
692 assert(iter->tree != NULL);
693 //printf("PT : Go UP\n");
694 #endif
696 iter->file = -1;
697 if(iter->tree->parent == iter->root->parent)
698 return PLAY_TREE_ITER_END;
700 #ifdef MP_DEBUG
701 assert(iter->tree->parent != NULL);
702 assert(iter->stack_size > 0);
703 assert(iter->status_stack != NULL);
704 #endif
706 iter->stack_size--;
707 iter->loop = iter->status_stack[iter->stack_size];
708 if(iter->stack_size > 0)
709 iter->status_stack = realloc(iter->status_stack, iter->stack_size * sizeof(int));
710 else {
711 free(iter->status_stack);
712 iter->status_stack = NULL;
714 if(iter->stack_size > 0 && iter->status_stack == NULL) {
715 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",iter->stack_size*(int)sizeof(char*));
716 return PLAY_TREE_ITER_ERROR;
718 iter->tree = iter->tree->parent;
720 // Pop subtree params
721 if(iter->config) {
722 m_config_pop(iter->config);
723 if(iter->mode == PLAY_TREE_ITER_RND)
724 iter->tree->flags |= PLAY_TREE_RND_PLAYED;
727 return play_tree_iter_step(iter,d,with_nodes);
731 play_tree_iter_down_step(play_tree_iter_t* iter, int d,int with_nodes) {
733 #ifdef MP_DEBUG
734 assert(iter->tree->files == NULL);
735 assert(iter->tree->child != NULL);
736 assert(iter->tree->child->parent == iter->tree);
737 //printf("PT : Go DOWN\n");
738 #endif
740 iter->file = -1;
742 // Push subtree params
743 if(iter->config)
744 play_tree_iter_push_params(iter);
746 iter->stack_size++;
747 iter->status_stack = realloc(iter->status_stack, iter->stack_size * sizeof(int));
748 if(iter->status_stack == NULL) {
749 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",iter->stack_size*(int)sizeof(int));
750 return PLAY_TREE_ITER_ERROR;
752 iter->status_stack[iter->stack_size-1] = iter->loop;
753 // Set new status
754 iter->loop = iter->tree->loop-1;
755 if(d >= 0)
756 iter->tree = iter->tree->child;
757 else {
758 play_tree_t* pt;
759 for(pt = iter->tree->child ; pt->next != NULL ; pt = pt->next)
760 /*NOTING*/;
761 iter->tree = pt;
764 return play_tree_iter_step(iter,0,with_nodes);
767 char*
768 play_tree_iter_get_file(play_tree_iter_t* iter, int d) {
769 #ifdef MP_DEBUG
770 assert(iter != NULL);
771 assert(iter->tree->child == NULL);
772 #endif
774 if(iter->tree->files == NULL)
775 return NULL;
777 #ifdef MP_DEBUG
778 assert(iter->num_files > 0);
779 #endif
781 if(iter->file >= iter->num_files-1 || iter->file < -1)
782 return NULL;
784 if(d > 0) {
785 if(iter->file >= iter->num_files - 1)
786 iter->file = 0;
787 else
788 iter->file++;
789 } else if(d < 0) {
790 if(iter->file <= 0)
791 iter->file = iter->num_files - 1;
792 else
793 iter->file--;
795 return iter->tree->files[iter->file];
798 play_tree_t*
799 play_tree_cleanup(play_tree_t* pt) {
800 play_tree_t* iter, *tmp, *first;
802 #ifdef MP_DEBUG
803 assert(pt != NULL);
804 #endif
806 if( ! play_tree_is_valid(pt)) {
807 play_tree_remove(pt,1,1);
808 return NULL;
811 first = pt->child;
813 for(iter = pt->child ; iter != NULL ; ) {
814 tmp = iter;
815 iter = iter->next;
816 if(! play_tree_is_valid(tmp)) {
817 play_tree_remove(tmp,1,1);
818 if(tmp == first) first = iter;
822 for(iter = first ; iter != NULL ; ) {
823 tmp = iter;
824 iter = iter->next;
825 play_tree_cleanup(tmp);
828 return pt;
832 play_tree_iter_t*
833 play_tree_iter_new_copy(play_tree_iter_t* old) {
834 play_tree_iter_t* iter;
836 #ifdef MP_DEBUG
837 assert(old != NULL);
838 #endif
840 iter = malloc(sizeof(play_tree_iter_t));
841 if(iter == NULL) {
842 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",(int)sizeof(play_tree_iter_t));
843 return NULL;
846 memcpy(iter,old,sizeof(play_tree_iter_t));
847 if(old->status_stack) {
848 iter->status_stack = malloc(old->stack_size * sizeof(int));
849 if(iter->status_stack == NULL) {
850 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",old->stack_size * (int)sizeof(int));
851 free(iter);
852 return NULL;
854 memcpy(iter->status_stack,old->status_stack,iter->stack_size*sizeof(int));
856 iter->config = NULL;
858 return iter;
861 // HIGH Level API, by Fabian Franz (mplayer@fabian-franz.de)
863 play_tree_iter_t* pt_iter_create(play_tree_t** ppt, m_config_t* config)
865 play_tree_iter_t* r=NULL;
866 #ifdef MP_DEBUG
867 assert(*ppt!=NULL);
868 #endif
870 *ppt=play_tree_cleanup(*ppt);
872 if(*ppt) {
873 r = play_tree_iter_new(*ppt,config);
874 if (r && play_tree_iter_step(r,0,0) != PLAY_TREE_ITER_ENTRY)
876 play_tree_iter_free(r);
877 r = NULL;
881 return r;
884 void pt_iter_destroy(play_tree_iter_t** iter)
886 if (iter && *iter)
888 free(*iter);
889 iter=NULL;
893 char* pt_iter_get_file(play_tree_iter_t* iter, int d)
895 int i=0;
896 char* r;
898 if (iter==NULL)
899 return NULL;
901 r = play_tree_iter_get_file(iter,d);
903 while (!r && d!=0)
905 if (play_tree_iter_step(iter,d,0) != PLAY_TREE_ITER_ENTRY)
906 break;
907 r=play_tree_iter_get_file(iter,d);
908 i++;
911 return r;
914 void pt_iter_insert_entry(play_tree_iter_t* iter, play_tree_t* entry)
916 play_tree_t *pt = iter->tree;
917 #ifdef MP_DEBUG
918 assert(pt!=NULL);
919 assert(entry!=NULL);
920 assert(entry!=pt);
921 #endif
923 play_tree_insert_entry(pt, entry);
924 play_tree_set_params_from(entry,pt);
927 void pt_iter_replace_entry(play_tree_iter_t* iter, play_tree_t* entry)
929 play_tree_t *pt = iter->tree;
931 pt_iter_insert_entry(iter, entry);
932 play_tree_remove(pt, 1, 1);
933 iter->tree=entry;
936 //Add a new file as a new entry
937 void pt_add_file(play_tree_t** ppt, char* filename)
939 play_tree_t *pt = *ppt, *entry = play_tree_new();
940 #ifdef MP_DEBUG
941 assert(entry!=NULL);
942 #endif
944 play_tree_add_file(entry, filename);
945 if (pt)
946 play_tree_append_entry(pt, entry);
947 else
949 pt=entry;
950 *ppt=pt;
952 play_tree_set_params_from(entry,pt);
955 void pt_iter_goto_head(play_tree_iter_t* iter)
957 iter->tree=iter->root;
958 play_tree_iter_step(iter, 0, 0);