Kestrel Interface
Loading...
Searching...
No Matches
kest_sequence.c
Go to the documentation of this file.
1#include "kest_int.h"
2
3#ifndef PRINTLINES_ALLOWED
4#define PRINTLINES_ALLOWED 0
5#endif
6
7static const char *FNAME = "kest_sequence.c";
8
10
12{
13 if (!sequence)
14 return ERR_NULL_PTR;
15
16 sequence->name = NULL;
17 sequence->profiles = NULL;
18
19 sequence->active = 0;
20 sequence->unsaved_changes = 1;
21 sequence->position = NULL;
22
23 #ifdef KEST_ENABLE_UI
24 sequence->view_page = NULL;
25 #endif
26
27 sequence->fname[0] = 0;
28 sequence->has_fname = 0;
29
30 sequence->listings = NULL;
31
32 sequence->main_sequence = 0;
33
34 #ifdef KEST_ENABLE_REPRESENTATIONS
35 sequence->file_rep.representee = sequence;
36 sequence->file_rep.representer = NULL;
37 sequence->file_rep.update = kest_sequence_file_rep_update;
38 sequence->representations = NULL;
39 kest_representation_pll_safe_append(&sequence->representations, &sequence->file_rep);
40 #endif
41
42 return NO_ERROR;
43}
44
46{
47 if (!sequence || !profile)
48 return ERR_NULL_PTR;
49
50 seq_profile_ll *new_node = kest_alloc(sizeof(seq_profile_ll));
51
52 if (!new_node)
53 return ERR_ALLOC_FAIL;
54
55 new_node->data = profile;
56 new_node->next = NULL;
57 new_node->prev = NULL;
58
59 if (!sequence->profiles)
60 {
61 sequence->profiles = new_node;
62 return NO_ERROR;
63 }
64
65 seq_profile_ll *current = sequence->profiles;
66
67 while (current)
68 {
69 if (!current->next)
70 break;
71 current = current->next;
72 }
73
74 current->next = new_node;
75 new_node->prev = current;
76
77 profile->sequence = sequence;
78
79 return NO_ERROR;
80}
81
82
84{
85 KEST_PRINTF("sequence_append_profile_rp, line %d\n", __LINE__);
86
87 if (!sequence || !profile)
88 return NULL;
89
90
91 KEST_PRINTF("sequence_append_profile_rp, line %d\n", __LINE__);
92 seq_profile_ll *new_node = kest_alloc(sizeof(seq_profile_ll));
93
94 if (!new_node)
95 return NULL;
96
97
98 KEST_PRINTF("sequence_append_profile_rp, line %d\n", __LINE__);
99 new_node->data = profile;
100 new_node->next = NULL;
101 new_node->prev = NULL;
102
103
104 KEST_PRINTF("sequence_append_profile_rp, line %d\n", __LINE__);
105 if (!sequence->profiles)
106 {
107 sequence->profiles = new_node;
108 return new_node;
109 }
110
111 KEST_PRINTF("sequence_append_profile_rp, line %d\n", __LINE__);
112
113 seq_profile_ll *current = sequence->profiles;
114
115
116 KEST_PRINTF("sequence_append_profile_rp, line %d\n", __LINE__);
117 while (current)
118 {
119 if (!current->next)
120 break;
121 current = current->next;
122 }
123
124
125 KEST_PRINTF("sequence_append_profile_rp, line %d\n", __LINE__);
126 current->next = new_node;
127 new_node->prev = current;
128
129
130 KEST_PRINTF("sequence_append_profile_rp, line %d\n", __LINE__);
132
133 KEST_PRINTF("sequence_append_profile_rp, line %d\n", __LINE__);
134
135 return new_node;
136}
137
138int kest_sequence_move_profile(kest_sequence *sequence, int pos, int new_pos)
139{
140 if (!sequence)
141 return ERR_NULL_PTR;
142
143 if (pos < 0 || new_pos < 0)
144 return ERR_BAD_ARGS;
145
146 seq_profile_ll *target = sequence->profiles;
147 seq_profile_ll *prev = NULL;
148
149 for (int i = 0; i < pos; i++)
150 {
151 prev = target;
152 if (target)
153 {
154 target = target->next;
155 }
156 }
157
158 if (!target)
159 return ERR_BAD_ARGS;
160
161 if (prev)
162 {
163 prev->next = target->next;
164 }
165
166 if (new_pos == 0)
167 {
168 target->next = sequence->profiles;
169 sequence->profiles = target;
170 return NO_ERROR;
171 }
172
173 seq_profile_ll *current = sequence->profiles;
174
175 for (int i = 0; i < new_pos; i++)
176 {
177 prev = current;
178 if (current)
179 current = current->next;
180 }
181
182 if (prev)
183 {
184 target->next = prev->next;
185 prev->next = target;
186 }
187 else
188 {
189 return ERR_BAD_ARGS;
190 }
191
193
194 return NO_ERROR;
195}
196
198{
199 if (!sequence)
200 return ERR_NULL_PTR;
201
202 seq_profile_ll *current = sequence->profiles;
203
204 while (current)
205 {
206 if (current->data == profile)
207 break;
208
209 current = current->next;
210 }
211
212 if (current)
213 {
214 if (current->prev)
215 current->prev->next = current->next;
216 else
217 sequence->profiles = current->next;
218
219 kest_free(current);
220 }
221 else
222 {
223 return ERR_BAD_ARGS;
224 }
225
227
228 return NO_ERROR;
229}
230
232{
233 if (!sequence)
234 return ERR_NULL_PTR;
235
236 int ret_val = kest_sequence_remove_profile(sequence, profile);
237
238 if (profile && ret_val == NO_ERROR)
239 {
240 kest_free_profile(profile);
241 }
242
243 return NO_ERROR;
244}
245
247{
248 return;
249}
250
252{
253 #ifdef KEST_ENABLE_UI
254 if (!sequence || !listing)
255 return ERR_NULL_PTR;
256
257 kest_menu_item_pll *nl = kest_menu_item_pll_append(sequence->listings, listing);
258
259 if (nl)
260 sequence->listings = nl;
261 else
262 return ERR_ALLOC_FAIL;
263
264 return NO_ERROR;
265 #else
267 #endif
268}
269
271{
272 if (!sequence)
273 return ERR_NULL_PTR;
274
275 if (!sequence->profiles)
276 {
277 KEST_PRINTF("Sequence is empty !\n");
278 return NO_ERROR;
279 }
280
281 global_cxt.sequence = sequence;
282 sequence->active = 1;
283
285
286 sequence->position = sequence->profiles;
287
289
290 return NO_ERROR;
291}
292
294{
295 if (!sequence || !profile)
296 return ERR_NULL_PTR;
297
298 if (!sequence->profiles)
299 {
300 KEST_PRINTF("Sequence is empty !\n");
301 return NO_ERROR;
302 }
303
304 global_cxt.sequence = sequence;
305 sequence->active = 1;
306
307 seq_profile_ll *current = sequence->profiles;
308 int found = 0;
309
310 while (current && !found)
311 {
312 if (current->data == profile)
313 found = 1;
314 else
315 current = current->next;
316 }
317
318 if (!found)
319 return ERR_BAD_ARGS;
320
322
323 sequence->position = current;
324
326
327 return NO_ERROR;
328}
329
330
332{
333 if (!sequence)
334 return ERR_NULL_PTR;
335
336 KEST_PRINTF("regressing sequence\n");
337
338 if (!sequence->profiles)
339 {
340 KEST_PRINTF("Error: empty sequence\n");
341 return ERR_BAD_ARGS;
342 }
343
344 if (!sequence->active || !sequence->position)
345 {
346 KEST_PRINTF("Error: sequence not active\n");
347 return ERR_BAD_ARGS;
348 }
349
350 if (!sequence->position->prev)
351 {
352 KEST_PRINTF("Can't regress sequence; sequence at start already\n");
353 return NO_ERROR;
354 }
355
356 sequence->position = sequence->position->prev;
357
359
360 return NO_ERROR;
361}
362
364{
365 if (!sequence)
366 return ERR_NULL_PTR;
367
368 KEST_PRINTF("advancing sequence. sequence = %p\n", sequence);
369
370 if (!sequence->profiles)
371 {
372 KEST_PRINTF("Error: empty sequence\n");
373 return ERR_BAD_ARGS;
374 }
375
376 KEST_PRINTF("Sequence nonempty\n");
377
378 if (!sequence->active || !sequence->position)
379 {
380 KEST_PRINTF("Error: sequence not active\n");
381 return ERR_BAD_ARGS;
382 }
383
384 KEST_PRINTF("Sequence active. Position: %p\n", sequence->position);
385
386 if (!sequence->position->next)
387 {
388 KEST_PRINTF("Can't regress sequence; sequence at end already\n");
389 return NO_ERROR;
390 }
391
392 KEST_PRINTF("sequence->position->next = %p\n", sequence->position->next);
393
394 sequence->position = sequence->position->next;
395
396 KEST_PRINTF("New sequence->position: %p. sequence->position->data: %p\n",
397 sequence->position, (sequence->position) ? sequence->position->data : NULL);
398
400}
401
403{
404 if (!sequence)
405 return ERR_NULL_PTR;
406
407 global_cxt.sequence = NULL;
408 sequence->active = 0;
409
411
412 sequence->position = NULL;
413
415
416 return NO_ERROR;
417}
418
419
421{
422 if (!sequence)
423 return ERR_NULL_PTR;
424
425 global_cxt.sequence = NULL;
426 sequence->active = 0;
427
428 sequence->position = NULL;
429
431
432 return NO_ERROR;
433}
434
436{
437 KEST_PRINTF("kest_sequence_activate_at\n");
438 if (!sequence)
439 return ERR_NULL_PTR;
440
441 seq_profile_ll *current = sequence->profiles;
442
443 while (current)
444 {
445 if (current->data && current->data == profile)
446 {
447 sequence->position = current;
448 sequence->active = 1;
449
451 return NO_ERROR;
452 }
453
454 current = current->next;
455 }
456
457 KEST_PRINTF("kest_sequence_activate_at done\n");
458 return ERR_BAD_ARGS;
459}
460
462{
463 #ifdef KEST_ENABLE_UI
464 if (!sequence)
465 return ERR_NULL_PTR;
466
467 kest_representation_pll *nl = kest_representation_pll_append(sequence->representations, rep);
468
469 if (nl)
470 sequence->representations = nl;
471 else
472 return ERR_ALLOC_FAIL;
473
474 return NO_ERROR;
475 #else
477 #endif
478}
479
481{
482 #ifdef KEST_ENABLE_REPRESENTATIONS
483 if (!sequence)
484 return ERR_NULL_PTR;
485
486 kest_representation_pll *current = sequence->representations;
487
488 if (!current)
489 {
490 KEST_PRINTF("Sequence %p has no representations.\n", sequence);
491 }
492 else
493 {
494 int i = 0;
495 while (current)
496 {
497 if (current->data)
498 {
499 KEST_PRINTF("Seq %p rep %d: {.representer = %p, representee = %p, update = %p}\n",
500 sequence, i, current->data->representer, current->data->representee, current->data->update);
501 }
502
503 current = current->next;
504 i++;
505 }
506 }
507
508 if (sequence->representations)
509 queue_representation_list_update(sequence->representations);
510
511 return NO_ERROR;
512 #else
514 #endif
515}
516
517
518void kest_sequence_file_rep_update(void *representer, void *representee)
519{
520 if (!representee)
521 return;
522
523 kest_sequence *sequence = (kest_sequence*)representee;
524
525 save_sequence(sequence);
526
527 return;
528}
void kest_free(void *ptr)
Definition kest_alloc.c:32
void * kest_alloc(size_t size)
Definition kest_alloc.c:11
int set_active_profile_from_sequence(kest_profile *profile)
#define ERR_ALLOC_FAIL
#define ERR_BAD_ARGS
#define NO_ERROR
#define ERR_FEATURE_DISABLED
#define ERR_NULL_PTR
int save_sequence(kest_sequence *sequence)
Definition kest_files.c:921
kest_context global_cxt
Definition kest_int.c:12
#define IMPLEMENT_LINKED_PTR_LIST(X)
#define KEST_PRINTF(...)
Definition kest_printf.h:10
void kest_free_profile(kest_profile *profile)
int queue_representation_list_update(kest_representation_pll *reps)
int kest_sequence_regress(kest_sequence *sequence)
int kest_sequence_activate_at(kest_sequence *sequence, kest_profile *profile)
int kest_sequence_stop(kest_sequence *sequence)
int init_m_sequence(kest_sequence *sequence)
int kest_sequence_stop_from_profile(kest_sequence *sequence)
int kest_sequence_remove_profile(kest_sequence *sequence, kest_profile *profile)
int kest_sequence_add_representation(kest_sequence *sequence, kest_representation *rep)
void free_sequence(kest_sequence *sequence)
int kest_sequence_begin_at(kest_sequence *sequence, kest_profile *profile)
int kest_sequence_advance(kest_sequence *sequence)
int kest_sequence_delete_profile(kest_sequence *sequence, kest_profile *profile)
int kest_sequence_move_profile(kest_sequence *sequence, int pos, int new_pos)
void kest_sequence_file_rep_update(void *representer, void *representee)
int kest_sequence_update_representations(kest_sequence *sequence)
int sequence_append_profile(kest_sequence *sequence, kest_profile *profile)
int kest_sequence_add_menu_listing(kest_sequence *sequence, kest_menu_item *listing)
seq_profile_ll * sequence_append_profile_rp(kest_sequence *sequence, kest_profile *profile)
int kest_sequence_begin(kest_sequence *sequence)
seq_profile_ll * position
char fname[KEST_FILENAME_LEN]
struct kest_menu_item_pll * listings
seq_profile_ll * profiles
kest_profile * data
struct seq_profile_ll * next
struct seq_profile_ll * prev