~ubuntu-branches/ubuntu/quantal/mplayer2/quantal-proposed

« back to all changes in this revision

Viewing changes to playtree.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-01-12 22:59:30 UTC
  • mfrom: (5.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20120112225930-jsg10o7na7nk73w5
Tags: 2.0-426-gc32b3ed-2
* Upload to unstable
* don't build-depend on libcdparanoia-dev on the hurd

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include <stdio.h>
26
26
#include <unistd.h>
27
27
#include <errno.h>
28
 
#ifdef MP_DEBUG
29
28
#include <assert.h>
30
 
#endif
 
29
 
 
30
#include "talloc.h"
 
31
 
31
32
#include "m_config.h"
32
33
#include "playtree.h"
33
34
#include "mp_msg.h"
38
39
play_tree_t*
39
40
play_tree_new(void) {
40
41
  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;
44
 
  }
45
42
  r->entry_type = PLAY_TREE_ENTRY_NODE;
46
43
  return r;
47
44
}
50
47
play_tree_free(play_tree_t* pt, int children) {
51
48
  play_tree_t* iter;
52
49
 
53
 
#ifdef MP_DEBUG
54
50
  assert(pt != NULL);
55
 
#endif
56
51
 
57
52
  if(children) {
58
53
    for(iter = pt->child; iter != NULL; ) {
68
63
  for(iter = pt->child ; iter != NULL ; iter = iter->next)
69
64
    iter->parent = NULL;
70
65
 
71
 
  //free(pt->params);
 
66
  talloc_free(pt->params);
 
67
 
72
68
  if(pt->files) {
73
69
    int i;
74
70
    for(i = 0 ; pt->files[i] != NULL ; i++)
83
79
play_tree_free_list(play_tree_t* pt, int children) {
84
80
  play_tree_t* iter;
85
81
 
86
 
#ifdef MP_DEBUG
87
82
  assert(pt != NULL);
88
 
#endif
89
83
 
90
84
  for(iter = pt ; iter->prev != NULL ; iter = iter->prev)
91
85
    /* NOTHING */;
103
97
play_tree_append_entry(play_tree_t* pt, play_tree_t* entry) {
104
98
  play_tree_t* iter;
105
99
 
106
 
#ifdef MP_DEBUG
107
100
  assert(pt != NULL);
108
101
  assert(entry != NULL);
109
 
#endif
110
102
 
111
103
  if(pt == entry)
112
104
    return;
124
116
play_tree_prepend_entry(play_tree_t* pt, play_tree_t* entry) {
125
117
  play_tree_t* iter;
126
118
 
127
 
#ifdef MP_DEBUG
128
119
  assert(pt != NULL);
129
120
  assert(entry != NULL);
130
 
#endif
131
121
 
132
122
  for(iter = pt ; iter->prev != NULL; iter = iter->prev)
133
123
    /* NOTHING */;
138
128
 
139
129
  iter->prev = entry;
140
130
  if(entry->parent) {
141
 
#ifdef MP_DEBUG
142
131
    assert(entry->parent->child == iter);
143
 
#endif
144
132
    entry->parent->child = entry;
145
133
  }
146
134
}
148
136
void
149
137
play_tree_insert_entry(play_tree_t* pt, play_tree_t* entry) {
150
138
 
151
 
#ifdef MP_DEBUG
152
139
  assert(pt != NULL);
153
140
  assert(entry != NULL);
154
 
#endif
155
141
 
156
142
  entry->parent = pt->parent;
157
143
  entry->prev = pt;
158
144
  if(pt->next) {
159
 
#ifdef MP_DEBUG
160
145
    assert(pt->next->prev == pt);
161
 
#endif
162
146
    entry->next = pt->next;
163
147
    entry->next->prev = entry;
164
148
  } else
170
154
void
171
155
play_tree_remove(play_tree_t* pt, int free_it, int with_children) {
172
156
 
173
 
#ifdef MP_DEBUG
174
157
  assert(pt != NULL);
175
 
#endif
176
158
 
177
159
  // Middle of list
178
160
  if(pt->prev && pt->next) {
179
 
#ifdef MP_DEBUG
180
161
    assert(pt->prev->next == pt);
181
162
    assert(pt->next->prev == pt);
182
 
#endif
183
163
    pt->prev->next = pt->next;
184
164
    pt->next->prev = pt->prev;
185
165
  } // End of list
186
166
  else if(pt->prev) {
187
 
#ifdef MP_DEBUG
188
167
    assert(pt->prev->next == pt);
189
 
#endif
190
168
    pt->prev->next = NULL;
191
169
  } // Beginning of list
192
170
  else if(pt->next) {
193
 
#ifdef MP_DEBUG
194
171
    assert(pt->next->prev == pt);
195
 
#endif
196
172
    pt->next->prev = NULL;
197
173
    if(pt->parent) {
198
 
#ifdef MP_DEBUG
199
174
      assert(pt->parent->child == pt);
200
 
#endif
201
175
      pt->parent->child = pt->next;
202
176
    }
203
177
  } // The only one
204
178
  else if(pt->parent) {
205
 
#ifdef MP_DEBUG
206
179
    assert(pt->parent->child == pt);
207
 
#endif
208
180
    pt->parent->child = NULL;
209
181
  }
210
182
 
226
198
    return;
227
199
  }
228
200
 
229
 
#ifdef MP_DEBUG
230
201
  assert(pt->entry_type == PLAY_TREE_ENTRY_NODE);
231
 
#endif
232
202
 
233
203
  //DEBUG_FF: Where are the children freed?
234
204
  // Attention in using this function!
250
220
play_tree_set_parent(play_tree_t* pt, play_tree_t* parent) {
251
221
  play_tree_t* iter;
252
222
 
253
 
#ifdef MP_DEBUG
254
223
  assert(pt != NULL);
255
 
#endif
256
224
 
257
225
  if(pt->parent)
258
226
    pt->parent->child = NULL;
272
240
 
273
241
 
274
242
void
275
 
play_tree_add_file(play_tree_t* pt,char* file) {
 
243
play_tree_add_file(play_tree_t* pt,const char* file) {
276
244
  int n = 0;
277
245
 
278
 
#ifdef MP_DEBUG
279
246
  assert(pt != NULL);
280
247
  assert(pt->child == NULL);
281
248
  assert(file != NULL);
282
 
#endif
283
249
 
284
250
  if(pt->entry_type != PLAY_TREE_ENTRY_NODE &&
285
251
     pt->entry_type != PLAY_TREE_ENTRY_FILE)
303
269
}
304
270
 
305
271
int
306
 
play_tree_remove_file(play_tree_t* pt,char* file) {
 
272
play_tree_remove_file(play_tree_t* pt,const char* file) {
307
273
  int n,f = -1;
308
274
 
309
 
#ifdef MP_DEBUG
310
275
  assert(pt != NULL);
311
276
  assert(file != NULL);
312
277
  assert(pt->entry_type != PLAY_TREE_ENTRY_NODE);
313
 
#endif
314
278
 
315
279
  for(n=0 ; pt->files[n] != NULL ; n++) {
316
280
    if(strcmp(file,pt->files[n]) == 0)
320
284
  if(f < 0) // Not found
321
285
    return 0;
322
286
 
323
 
#ifdef MP_DEBUG
324
 
  assert(n > f);
325
 
#endif
326
 
 
327
287
  free(pt->files[f]);
328
288
 
329
289
  if(n > 1) {
342
302
}
343
303
 
344
304
void
345
 
play_tree_set_param(play_tree_t* pt, char* name, char* val) {
 
305
play_tree_set_param(play_tree_t* pt, struct bstr name, struct bstr val) {
346
306
  int n = 0;
347
307
 
348
 
#ifdef MP_DEBUG
349
308
  assert(pt != NULL);
350
 
  assert(name != NULL);
351
 
#endif
352
309
 
353
310
  if(pt->params)
354
311
    for ( ; pt->params[n].name != NULL ; n++ ) { }
355
312
 
356
 
  pt->params = realloc(pt->params, (n + 2) * sizeof(play_tree_param_t));
357
 
  if(pt->params == NULL) {
358
 
      mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't realloc params (%d bytes of memory)\n",(n+2)*(int)sizeof(play_tree_param_t));
359
 
      return;
360
 
  }
361
 
  pt->params[n].name = strdup(name);
362
 
  pt->params[n].value = val != NULL ? strdup(val) : NULL;
 
313
  pt->params = talloc_realloc(NULL, pt->params, struct play_tree_param, n + 2);
 
314
  pt->params[n].name = bstrdup0(pt->params, name);
 
315
  pt->params[n].value = bstrdup0(pt->params, val);
363
316
  memset(&pt->params[n+1],0,sizeof(play_tree_param_t));
364
317
 
365
318
  return;
366
319
}
367
320
 
368
321
int
369
 
play_tree_unset_param(play_tree_t* pt, char* name) {
 
322
play_tree_unset_param(play_tree_t* pt, const char* name) {
370
323
  int n,ni = -1;
371
324
 
372
 
#ifdef MP_DEBUG
373
325
  assert(pt != NULL);
374
326
  assert(name != NULL);
375
327
  assert(pt->params != NULL);
376
 
#endif
377
328
 
378
329
  for(n = 0 ; pt->params[n].name != NULL ; n++) {
379
330
    if(strcasecmp(pt->params[n].name,name) == 0)
383
334
  if(ni < 0)
384
335
    return 0;
385
336
 
386
 
  free(pt->params[ni].name);
387
 
  free(pt->params[ni].value);
 
337
  talloc_free(pt->params[ni].name);
 
338
  talloc_free(pt->params[ni].value);
388
339
 
389
340
  if(n > 1) {
390
341
    memmove(&pt->params[ni],&pt->params[ni+1],(n-ni)*sizeof(play_tree_param_t));
391
 
    pt->params = realloc(pt->params, n * sizeof(play_tree_param_t));
392
 
    if(pt->params == NULL) {
393
 
      mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",n*(int)sizeof(play_tree_param_t));
394
 
      return -1;
395
 
    }
 
342
    pt->params = talloc_realloc(NULL, pt->params, struct play_tree_param, n);
396
343
  } else {
397
 
    free(pt->params);
 
344
    talloc_free(pt->params);
398
345
    pt->params = NULL;
399
346
  }
400
347
 
405
352
play_tree_set_params_from(play_tree_t* dest,play_tree_t* src) {
406
353
  int i;
407
354
 
408
 
#ifdef MP_DEBUG
409
355
  assert(dest != NULL);
410
356
  assert(src != NULL);
411
 
#endif
412
357
 
413
358
  if(!src->params)
414
359
    return;
415
360
 
416
361
  for(i = 0; src->params[i].name != NULL ; i++)
417
 
    play_tree_set_param(dest,src->params[i].name,src->params[i].value);
 
362
      play_tree_set_param(dest, bstr(src->params[i].name), bstr(src->params[i].value));
418
363
  if(src->flags & PLAY_TREE_RND) // pass the random flag too
419
364
    dest->flags |= PLAY_TREE_RND;
420
365
 
440
385
play_tree_iter_push_params(play_tree_iter_t* iter) {
441
386
  int n;
442
387
  play_tree_t* pt;
443
 
#ifdef MP_DEBUG
444
 
  assert(iter != NULL);
445
388
  assert(iter->config != NULL);
446
389
  assert(iter->tree != NULL);
447
 
#endif
448
390
 
449
391
  pt = iter->tree;
450
392
 
458
400
 
459
401
  for(n = 0; pt->params[n].name != NULL ; n++) {
460
402
    int e;
461
 
    if((e = m_config_set_option(iter->config,pt->params[n].name,pt->params[n].value)) < 0) {
 
403
    if((e = m_config_set_option0(iter->config, pt->params[n].name,
 
404
                                 pt->params[n].value, false)) < 0) {
462
405
      mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Error %d while setting option '%s' with value '%s'\n",e,
463
406
             pt->params[n].name,pt->params[n].value);
464
407
    }
466
409
 
467
410
  if(!pt->child)
468
411
    iter->entry_pushed = 1;
469
 
  return;
470
412
}
471
413
 
472
414
play_tree_iter_t*
473
415
play_tree_iter_new(play_tree_t* pt,m_config_t* config) {
474
416
  play_tree_iter_t* iter;
475
417
 
476
 
#ifdef MP_DEBUG
477
418
  assert(pt != NULL);
478
419
  assert(config != NULL);
479
 
#endif
480
420
 
481
421
  if( ! play_tree_is_valid(pt))
482
422
    return NULL;
499
439
void
500
440
play_tree_iter_free(play_tree_iter_t* iter) {
501
441
 
502
 
#ifdef MP_DEBUG
503
442
  assert(iter != NULL);
504
 
#endif
505
443
 
506
444
  if(iter->status_stack) {
507
 
#ifdef MP_DEBUG
508
445
    assert(iter->stack_size > 0);
509
 
#endif
510
446
    free(iter->status_stack);
511
447
  }
512
448
 
548
484
  if ( !iter ) return PLAY_TREE_ITER_ENTRY;
549
485
  if ( !iter->root ) return PLAY_TREE_ITER_ENTRY;
550
486
 
551
 
#ifdef MP_DEBUG
552
487
  assert(iter != NULL);
553
488
  assert(iter->root != NULL);
554
 
  //printf("PT : Stepping = %d\n",d);
555
 
#endif
556
489
 
557
490
  if(iter->tree == NULL) {
558
491
    iter->tree = iter->root;
633
566
    return play_tree_iter_step(iter,d,with_nodes);
634
567
  }
635
568
 
636
 
#ifdef MP_DEBUG
637
569
  assert(pt->files != NULL);
638
 
#endif
639
570
 
640
571
  iter->tree = pt;
641
572
 
658
589
play_tree_is_valid(play_tree_t* pt) {
659
590
  play_tree_t* iter;
660
591
 
661
 
#ifdef MP_DEBUG
662
 
  assert(pt != NULL);
663
 
#endif
664
 
 
665
592
  if(pt->entry_type != PLAY_TREE_ENTRY_NODE) {
666
 
#ifdef MP_DEBUG
667
593
    assert(pt->child == NULL);
668
 
#endif
669
594
    return 1;
670
595
  }
671
596
  else if (pt->child != NULL) {
680
605
int
681
606
play_tree_iter_up_step(play_tree_iter_t* iter, int d,int with_nodes) {
682
607
 
683
 
#ifdef MP_DEBUG
684
608
  assert(iter != NULL);
685
609
  assert(iter->tree != NULL);
686
 
  //printf("PT : Go UP\n");
687
 
#endif
688
610
 
689
611
  iter->file = -1;
690
612
  if(iter->tree->parent == iter->root->parent)
691
613
    return PLAY_TREE_ITER_END;
692
614
 
693
 
#ifdef MP_DEBUG
694
615
  assert(iter->tree->parent != NULL);
695
616
  assert(iter->stack_size > 0);
696
617
  assert(iter->status_stack != NULL);
697
 
#endif
698
618
 
699
619
  iter->stack_size--;
700
620
  iter->loop = iter->status_stack[iter->stack_size];
723
643
int
724
644
play_tree_iter_down_step(play_tree_iter_t* iter, int d,int with_nodes) {
725
645
 
726
 
#ifdef MP_DEBUG
727
646
  assert(iter->tree->files == NULL);
728
647
  assert(iter->tree->child != NULL);
729
648
  assert(iter->tree->child->parent == iter->tree);
730
 
  //printf("PT : Go DOWN\n");
731
 
#endif
732
649
 
733
650
  iter->file = -1;
734
651
 
759
676
 
760
677
char*
761
678
play_tree_iter_get_file(play_tree_iter_t* iter, int d) {
762
 
#ifdef MP_DEBUG
763
679
  assert(iter != NULL);
764
680
  assert(iter->tree->child == NULL);
765
 
#endif
766
681
 
767
682
  if(iter->tree->files == NULL)
768
683
    return NULL;
769
684
 
770
 
#ifdef MP_DEBUG
771
685
  assert(iter->num_files > 0);
772
 
#endif
773
686
 
774
687
  if(iter->file >= iter->num_files-1 || iter->file < -1)
775
688
    return NULL;
792
705
play_tree_cleanup(play_tree_t* pt) {
793
706
  play_tree_t* iter, *tmp, *first;
794
707
 
795
 
#ifdef MP_DEBUG
796
708
  assert(pt != NULL);
797
 
#endif
798
709
 
799
710
  if( ! play_tree_is_valid(pt)) {
800
711
    play_tree_remove(pt,1,1);
826
737
play_tree_iter_new_copy(play_tree_iter_t* old) {
827
738
  play_tree_iter_t* iter;
828
739
 
829
 
#ifdef MP_DEBUG
830
740
  assert(old != NULL);
831
 
#endif
832
741
 
833
742
  iter = malloc(sizeof(play_tree_iter_t));
834
743
  if(iter == NULL) {
856
765
play_tree_iter_t* pt_iter_create(play_tree_t** ppt, m_config_t* config)
857
766
{
858
767
  play_tree_iter_t* r=NULL;
859
 
#ifdef MP_DEBUG
860
768
  assert(*ppt!=NULL);
861
 
#endif
862
769
 
863
770
  *ppt=play_tree_cleanup(*ppt);
864
771
 
907
814
void pt_iter_insert_entry(play_tree_iter_t* iter, play_tree_t* entry)
908
815
{
909
816
  play_tree_t *pt = iter->tree;
910
 
#ifdef MP_DEBUG
911
817
  assert(pt!=NULL);
912
818
  assert(entry!=NULL);
913
819
  assert(entry!=pt);
914
 
#endif
915
820
 
916
821
  play_tree_insert_entry(pt, entry);
917
822
  play_tree_set_params_from(entry,pt);
927
832
}
928
833
 
929
834
//Add a new file as a new entry
930
 
void pt_add_file(play_tree_t** ppt, char* filename)
 
835
void pt_add_file(play_tree_t** ppt, const char* filename)
931
836
{
932
837
  play_tree_t *pt = *ppt, *entry = play_tree_new();
933
 
#ifdef MP_DEBUG
934
 
  assert(entry!=NULL);
935
 
#endif
936
838
 
937
839
  play_tree_add_file(entry, filename);
938
840
  if (pt)