Remove .gitignore do nothing is ignored.
[synfig.git] / synfig-osx / trunk / launcher / x-hook.c
1 /* x-hook.c
2    $Id: x-hook.c,v 1.2 2003/04/16 00:42:14 jharper Exp $
3
4    Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
5
6    Permission is hereby granted, free of charge, to any person
7    obtaining a copy of this software and associated documentation files
8    (the "Software"), to deal in the Software without restriction,
9    including without limitation the rights to use, copy, modify, merge,
10    publish, distribute, sublicense, and/or sell copies of the Software,
11    and to permit persons to whom the Software is furnished to do so,
12    subject to the following conditions:
13
14    The above copyright notice and this permission notice shall be
15    included in all copies or substantial portions of the Software.
16
17    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20    NONINFRINGEMENT.  IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
21    HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24    DEALINGS IN THE SOFTWARE.
25
26    Except as contained in this notice, the name(s) of the above
27    copyright holders shall not be used in advertising or otherwise to
28    promote the sale, use or other dealings in this Software without
29    prior written authorization. */
30
31 #include "x-hook.h"
32 #include <stdlib.h>
33 #include <assert.h>
34
35 #define CELL_NEW(f,d) X_PFX (list_prepend) ((x_list *) (f), (d))
36 #define CELL_FREE(c)  X_PFX (list_free_1) (c)
37 #define CELL_FUN(c)   ((x_hook_function *) ((c)->next))
38 #define CELL_DATA(c)  ((c)->data)
39
40 X_EXTERN x_list *
41 X_PFX (hook_add) (x_list *lst, x_hook_function *fun, void *data)
42 {
43     return X_PFX (list_prepend) (lst, CELL_NEW (fun, data));
44 }
45
46 X_EXTERN x_list *
47 X_PFX (hook_remove) (x_list *lst, x_hook_function *fun, void *data)
48 {
49     x_list *node, *cell;
50     x_list *to_delete = NULL;
51
52     for (node = lst; node != NULL; node = node->next)
53     {
54         cell = node->data;
55         if (CELL_FUN (cell) == fun && CELL_DATA (cell) == data)
56             to_delete = X_PFX (list_prepend) (to_delete, cell);
57     }
58
59     for (node = to_delete; node != NULL; node = node->next)
60     {
61         cell = node->data;
62         lst = X_PFX (list_remove) (lst, cell);
63         CELL_FREE (cell);
64     }
65
66     X_PFX (list_free) (to_delete);
67 }
68
69 X_EXTERN void
70 X_PFX (hook_run) (x_list *lst, void *arg)
71 {
72     x_list *node, *cell;
73     x_hook_function **fun;
74     void **data;
75     int length, i;
76
77     length = X_PFX (list_length) (lst);
78     fun = alloca (sizeof (x_hook_function *) * length);
79     data = alloca (sizeof (void *) * length);
80
81     for (i = 0, node = lst; node != NULL; node = node->next, i++)
82     {
83         cell = node->data;
84         fun[i] = CELL_FUN (cell);
85         data[i] = CELL_DATA (cell);
86     }
87
88     for (i = 0; i < length; i++)
89     {
90         (*fun[i]) (arg, data[i]);
91     }
92 }
93
94 X_EXTERN void
95 X_PFX (hook_free) (x_list *lst)
96 {
97     x_list *node;
98
99     for (node = lst; node != NULL; node = node->next)
100     {
101         CELL_FREE (node->data);
102     }
103
104     X_PFX (list_free) (lst);
105 }