Too much spare time on tuesday, compared about 2/3 of the english manpage with the...
[mplayer/greg.git] / playtree.c
blobe0c92b2f148cb68e21c3e2334c1eec89bed90225
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)((float)(count) * rand() / (RAND_MAX + 1.0));
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->mode == PLAY_TREE_ITER_RND) {
589 if (iter->root->loop == 0)
590 return PLAY_TREE_ITER_END;
591 play_tree_unset_flag(iter->root, PLAY_TREE_RND_PLAYED, -1);
592 if (iter->root->loop > 0) iter->root->loop--;
593 // try again
594 return play_tree_iter_step(iter, 0, with_nodes);
595 } else
596 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) ) ) ) {
597 if(d > 0) { // Go back to the first one
598 for(pt = iter->tree ; pt->prev != NULL; pt = pt->prev)
599 /* NOTHNG */;
600 if(iter->loop > 0) iter->loop--;
601 } else if( d < 0 ) { // Or the last one
602 for(pt = iter->tree ; pt->next != NULL; pt = pt->next)
603 /* NOTHNG */;
604 if(iter->loop >= 0 && iter->loop < iter->tree->parent->loop) iter->loop++;
606 iter->tree = pt;
607 return play_tree_iter_step(iter,0,with_nodes);
609 // Go up one level
610 return play_tree_iter_up_step(iter,d,with_nodes);
614 // Is there any valid childs ?
615 if(pt->child && play_tree_is_valid(pt->child)) {
616 iter->tree = pt;
617 if(with_nodes) { // Stop on the node
618 return PLAY_TREE_ITER_NODE;
619 } else // Or follow it
620 return play_tree_iter_down_step(iter,d,with_nodes);
623 // Is it a valid enty ?
624 if(! play_tree_is_valid(pt)) {
625 if(d == 0) { // Can this happen ? FF: Yes!
626 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"What to do now ???? Infinite loop if we continue\n");
627 return PLAY_TREE_ITER_ERROR;
628 } // Not a valid entry : go to next one
629 return play_tree_iter_step(iter,d,with_nodes);
632 #ifdef MP_DEBUG
633 assert(pt->files != NULL);
634 #endif
636 iter->tree = pt;
638 for(d = 0 ; iter->tree->files[d] != NULL ; d++)
639 /* NOTHING */;
640 iter->num_files = d;
642 if(iter->config) {
643 play_tree_iter_push_params(iter);
644 iter->entry_pushed = 1;
645 if(iter->mode == PLAY_TREE_ITER_RND)
646 pt->flags |= PLAY_TREE_RND_PLAYED;
649 return PLAY_TREE_ITER_ENTRY;
653 static int
654 play_tree_is_valid(play_tree_t* pt) {
655 play_tree_t* iter;
657 #ifdef MP_DEBUG
658 assert(pt != NULL);
659 #endif
661 if(pt->entry_type != PLAY_TREE_ENTRY_NODE) {
662 #ifdef MP_DEBUG
663 assert(pt->child == NULL);
664 #endif
665 return 1;
667 else if (pt->child != NULL) {
668 for(iter = pt->child ; iter != NULL ; iter = iter->next) {
669 if(play_tree_is_valid(iter))
670 return 1;
673 return 0;
677 play_tree_iter_up_step(play_tree_iter_t* iter, int d,int with_nodes) {
679 #ifdef MP_DEBUG
680 assert(iter != NULL);
681 assert(iter->tree != NULL);
682 //printf("PT : Go UP\n");
683 #endif
685 iter->file = -1;
686 if(iter->tree->parent == iter->root->parent)
687 return PLAY_TREE_ITER_END;
689 #ifdef MP_DEBUG
690 assert(iter->tree->parent != NULL);
691 assert(iter->stack_size > 0);
692 assert(iter->status_stack != NULL);
693 #endif
695 iter->stack_size--;
696 iter->loop = iter->status_stack[iter->stack_size];
697 if(iter->stack_size > 0)
698 iter->status_stack = (int*)realloc(iter->status_stack,iter->stack_size*sizeof(int));
699 else {
700 free(iter->status_stack);
701 iter->status_stack = NULL;
703 if(iter->stack_size > 0 && iter->status_stack == NULL) {
704 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",iter->stack_size*sizeof(char*));
705 return PLAY_TREE_ITER_ERROR;
707 iter->tree = iter->tree->parent;
709 // Pop subtree params
710 if(iter->config) {
711 m_config_pop(iter->config);
712 if(iter->mode == PLAY_TREE_ITER_RND)
713 iter->tree->flags |= PLAY_TREE_RND_PLAYED;
716 return play_tree_iter_step(iter,d,with_nodes);
720 play_tree_iter_down_step(play_tree_iter_t* iter, int d,int with_nodes) {
722 #ifdef MP_DEBUG
723 assert(iter->tree->files == NULL);
724 assert(iter->tree->child != NULL);
725 assert(iter->tree->child->parent == iter->tree);
726 //printf("PT : Go DOWN\n");
727 #endif
729 iter->file = -1;
731 // Push subtree params
732 if(iter->config)
733 play_tree_iter_push_params(iter);
735 iter->stack_size++;
736 iter->status_stack = (int*)realloc(iter->status_stack,iter->stack_size*sizeof(int));
737 if(iter->status_stack == NULL) {
738 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",iter->stack_size*sizeof(int));
739 return PLAY_TREE_ITER_ERROR;
741 iter->status_stack[iter->stack_size-1] = iter->loop;
742 // Set new status
743 iter->loop = iter->tree->loop-1;
744 if(d >= 0)
745 iter->tree = iter->tree->child;
746 else {
747 play_tree_t* pt;
748 for(pt = iter->tree->child ; pt->next != NULL ; pt = pt->next)
749 /*NOTING*/;
750 iter->tree = pt;
753 return play_tree_iter_step(iter,0,with_nodes);
756 char*
757 play_tree_iter_get_file(play_tree_iter_t* iter, int d) {
758 char* entry;
759 #ifdef MP_DEBUG
760 assert(iter != NULL);
761 assert(iter->tree->child == NULL);
762 #endif
764 if(iter->tree->files == NULL)
765 return NULL;
767 #ifdef MP_DEBUG
768 assert(iter->num_files > 0);
769 #endif
771 if(iter->file >= iter->num_files-1 || iter->file < -1)
772 return NULL;
774 if(d > 0) {
775 if(iter->file >= iter->num_files - 1)
776 iter->file = 0;
777 else
778 iter->file++;
779 } else if(d < 0) {
780 if(iter->file <= 0)
781 iter->file = iter->num_files - 1;
782 else
783 iter->file--;
785 return iter->tree->files[iter->file];
788 play_tree_t*
789 play_tree_cleanup(play_tree_t* pt) {
790 play_tree_t* iter, *tmp, *first;
792 #ifdef MP_DEBUG
793 assert(pt != NULL);
794 #endif
796 if( ! play_tree_is_valid(pt)) {
797 play_tree_remove(pt,1,1);
798 return NULL;
801 first = pt->child;
803 for(iter = pt->child ; iter != NULL ; ) {
804 tmp = iter;
805 iter = iter->next;
806 if(! play_tree_is_valid(tmp)) {
807 play_tree_remove(tmp,1,1);
808 if(tmp == first) first = iter;
812 for(iter = first ; iter != NULL ; ) {
813 tmp = iter;
814 iter = iter->next;
815 play_tree_cleanup(tmp);
818 return pt;
822 play_tree_iter_t*
823 play_tree_iter_new_copy(play_tree_iter_t* old) {
824 play_tree_iter_t* iter;
826 #ifdef MP_DEBUG
827 assert(old != NULL);
828 #endif
830 iter = (play_tree_iter_t*)malloc(sizeof(play_tree_iter_t));
831 if(iter == NULL) {
832 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",sizeof(play_tree_iter_t));
833 return NULL;
836 memcpy(iter,old,sizeof(play_tree_iter_t));
837 if(old->status_stack) {
838 iter->status_stack = (int*)malloc(old->stack_size * sizeof(int));
839 if(iter->status_stack == NULL) {
840 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",old->stack_size * sizeof(int));
841 free(iter);
842 return NULL;
844 memcpy(iter->status_stack,old->status_stack,iter->stack_size*sizeof(int));
846 iter->config = NULL;
848 return iter;
851 // HIGH Level API, by Fabian Franz (mplayer@fabian-franz.de)
853 play_tree_iter_t* pt_iter_create(play_tree_t** ppt, m_config_t* config)
855 play_tree_iter_t* r=NULL;
856 #ifdef MP_DEBUG
857 assert(*ppt!=NULL);
858 #endif
860 *ppt=play_tree_cleanup(*ppt);
862 if(*ppt) {
863 r = play_tree_iter_new(*ppt,config);
864 if (r && play_tree_iter_step(r,0,0) != PLAY_TREE_ITER_ENTRY)
866 play_tree_iter_free(r);
867 r = NULL;
871 return r;
874 void pt_iter_destroy(play_tree_iter_t** iter)
876 if (iter && *iter)
878 free(*iter);
879 iter=NULL;
883 char* pt_iter_get_file(play_tree_iter_t* iter, int d)
885 int i=0;
886 char* r;
888 if (iter==NULL)
889 return NULL;
891 r = play_tree_iter_get_file(iter,d);
893 while (!r && d!=0)
895 if (play_tree_iter_step(iter,d,0) != PLAY_TREE_ITER_ENTRY)
896 break;
897 r=play_tree_iter_get_file(iter,d);
898 i++;
901 return r;
904 void pt_iter_insert_entry(play_tree_iter_t* iter, play_tree_t* entry)
906 play_tree_t *pt = iter->tree;
907 #ifdef MP_DEBUG
908 assert(pt!=NULL);
909 assert(entry!=NULL);
910 assert(entry!=pt);
911 #endif
913 play_tree_insert_entry(pt, entry);
914 play_tree_set_params_from(entry,pt);
917 void pt_iter_replace_entry(play_tree_iter_t* iter, play_tree_t* entry)
919 play_tree_t *pt = iter->tree;
921 pt_iter_insert_entry(iter, entry);
922 play_tree_remove(pt, 1, 1);
923 iter->tree=entry;
926 //Add a new file as a new entry
927 void pt_add_file(play_tree_t** ppt, char* filename)
929 play_tree_t *pt = *ppt, *entry = play_tree_new();
930 #ifdef MP_DEBUG
931 assert(entry!=NULL);
932 #endif
934 play_tree_add_file(entry, filename);
935 if (pt)
936 play_tree_append_entry(pt, entry);
937 else
939 pt=entry;
940 *ppt=pt;
942 play_tree_set_params_from(entry,pt);
945 void pt_add_gui_file(play_tree_t** ppt, char* path, char* file)
947 char* wholename = malloc(strlen(path)+strlen(file)+2);
949 if (wholename)
951 strcpy(wholename, path);
952 strcat(wholename, "/");
953 strcat(wholename, file);
954 pt_add_file(ppt, wholename);
955 free(wholename); // As pt_add_file strdups it anyway!
959 void pt_iter_goto_head(play_tree_iter_t* iter)
961 iter->tree=iter->root;
962 play_tree_iter_step(iter, 0, 0);