Blender  V2.93
iterator.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  *
19  * - Blender Foundation, 2003-2009
20  * - Peter Schlaile <peter [at] schlaile [dot] de> 2005/2006
21  */
22 
27 #include <string.h>
28 
29 #include "MEM_guardedalloc.h"
30 
31 #include "DNA_scene_types.h"
32 #include "DNA_sequence_types.h"
33 
34 #include "BLI_listbase.h"
35 
36 #include "BKE_scene.h"
37 
38 #include "SEQ_iterator.h"
39 
40 /* ************************* iterator ************************** */
41 /* *************** (replaces old WHILE_SEQ) ********************* */
42 /* **************** use now SEQ_ALL_BEGIN () SEQ_ALL_END ***************** */
43 
44 /* sequence strip iterator:
45  * - builds a full array, recursively into meta strips
46  */
47 
48 static void seq_count(ListBase *seqbase, int *tot)
49 {
50  Sequence *seq;
51 
52  for (seq = seqbase->first; seq; seq = seq->next) {
53  (*tot)++;
54 
55  if (seq->seqbase.first) {
56  seq_count(&seq->seqbase, tot);
57  }
58  }
59 }
60 
61 static void seq_build_array(ListBase *seqbase, Sequence ***array, int depth)
62 {
63  Sequence *seq;
64 
65  for (seq = seqbase->first; seq; seq = seq->next) {
66  seq->depth = depth;
67 
68  if (seq->seqbase.first) {
69  seq_build_array(&seq->seqbase, array, depth + 1);
70  }
71 
72  **array = seq;
73  (*array)++;
74  }
75 }
76 
77 static void seq_array(Editing *ed,
78  const bool use_current_sequences,
79  Sequence ***r_seqarray,
80  int *r_seqarray_len)
81 {
82  Sequence **array;
83 
84  *r_seqarray = NULL;
85  *r_seqarray_len = 0;
86 
87  if (ed == NULL) {
88  return;
89  }
90 
91  if (use_current_sequences) {
92  seq_count(ed->seqbasep, r_seqarray_len);
93  }
94  else {
95  seq_count(&ed->seqbase, r_seqarray_len);
96  }
97 
98  if (*r_seqarray_len == 0) {
99  return;
100  }
101 
102  *r_seqarray = array = MEM_mallocN(sizeof(Sequence *) * (*r_seqarray_len), "SeqArray");
103  if (use_current_sequences) {
104  seq_build_array(ed->seqbasep, &array, 0);
105  }
106  else {
107  seq_build_array(&ed->seqbase, &array, 0);
108  }
109 }
110 
111 void SEQ_iterator_begin(Editing *ed, SeqIterator *iter, const bool use_current_sequences)
112 {
113  memset(iter, 0, sizeof(*iter));
114  seq_array(ed, use_current_sequences, &iter->array, &iter->tot);
115 
116  if (iter->tot) {
117  iter->cur = 0;
118  iter->seq = iter->array[iter->cur];
119  iter->valid = 1;
120  }
121 }
122 
124 {
125  if (++iter->cur < iter->tot) {
126  iter->seq = iter->array[iter->cur];
127  }
128  else {
129  iter->valid = 0;
130  }
131 }
132 
134 {
135  if (iter->array) {
136  MEM_freeN(iter->array);
137  }
138 
139  iter->valid = 0;
140 }
141 
143  int (*apply_fn)(Sequence *seq, void *),
144  void *arg)
145 {
146  Sequence *iseq;
147  for (iseq = seqbase->first; iseq; iseq = iseq->next) {
148  if (SEQ_iterator_recursive_apply(iseq, apply_fn, arg) == -1) {
149  return -1; /* bail out */
150  }
151  }
152  return 1;
153 }
154 
155 int SEQ_iterator_recursive_apply(Sequence *seq, int (*apply_fn)(Sequence *, void *), void *arg)
156 {
157  int ret = apply_fn(seq, arg);
158 
159  if (ret == -1) {
160  return -1; /* bail out */
161  }
162 
163  if (ret && seq->seqbase.first) {
164  ret = SEQ_iterator_seqbase_recursive_apply(&seq->seqbase, apply_fn, arg);
165  }
166 
167  return ret;
168 }
Read Guarded memory(de)allocation.
void SEQ_iterator_end(SeqIterator *iter)
Definition: iterator.c:133
int SEQ_iterator_recursive_apply(Sequence *seq, int(*apply_fn)(Sequence *, void *), void *arg)
Definition: iterator.c:155
static void seq_build_array(ListBase *seqbase, Sequence ***array, int depth)
Definition: iterator.c:61
void SEQ_iterator_begin(Editing *ed, SeqIterator *iter, const bool use_current_sequences)
Definition: iterator.c:111
void SEQ_iterator_next(SeqIterator *iter)
Definition: iterator.c:123
static void seq_count(ListBase *seqbase, int *tot)
Definition: iterator.c:48
static void seq_array(Editing *ed, const bool use_current_sequences, Sequence ***r_seqarray, int *r_seqarray_len)
Definition: iterator.c:77
int SEQ_iterator_seqbase_recursive_apply(ListBase *seqbase, int(*apply_fn)(Sequence *seq, void *), void *arg)
Definition: iterator.c:142
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
return ret
ListBase seqbase
ListBase * seqbasep
void * first
Definition: DNA_listBase.h:47
struct Sequence ** array
Definition: SEQ_iterator.h:34
struct Sequence * seq
Definition: SEQ_iterator.h:37
ListBase seqbase
struct Sequence * next