-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrcbc_data_hookup.c
276 lines (244 loc) · 6.46 KB
/
rcbc_data_hookup.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <assert.h>
#include "rcbc_data_hookup.h"
#include "console.h"
/**
* Hookup deconstructor.
* @param hookup The hookup to free.
* \memberof Hookup
**/
void Hookup_0Hookup(Hookup* hookup) {
DEBUG_M("Entering function...");
free(hookup->id);
hookup->ptr = NULL;
hookup->id = NULL;
free(hookup);
DEBUG_H("finished...");
return;
}
/**
* String class functions.
* \memberof Hookup
*/
static const ClassFunctions Hookup_c = {
(void*)Hookup_0Hookup
};
/**
* Allocates a hookup
* @param id The XML id.
* @param pointer The pointer to the data, or the pointer to the place.
* @return A pointer to a new Hookup or NULL on error.
* \memberof Hookup
*/
Hookup* Hookup_Hookup(char* id, void* pointer) {
DEBUG_M("Entering function...");
if(!id) {
WARNING("Tried to generate NULL hookup.");
return;
}
ALLOCATE(Hookup, hookup);
hookup->class_ = &Hookup_c;
int id_len = (strlen(id) + 1);
hookup->id = malloc(id_len * sizeof(char));
strncpy(hookup->id, id, id_len);
hookup->ptr = pointer;
hookup->hooked = 0;
DEBUG_H("exiting function...");
return hookup;
}
/**
* Finds a hookup based on its id.
* @param roothookup The head node of the List containing the Hookups.
* @param id The XML id of the node to find.
* @return The hookup if found or NULL.
* \memberof Hookup
*/
Hookup* Hookup_Find(List* roothookup, char* id) {
ListNode* node = roothookup->first;
DEBUG_M("%sHookup_Find(%s'%s'%s)", COLOUR_LIGHT_BLUE, COLOUR_YELLOW, id, COLOUR_LIGHT_BLUE);
while(node) {
DEBUG_H("\tCHECKING: '%s'", ((Hookup*)node->data)->id);
if(node->data
&& strcasecmp(((Hookup*)node->data)->id, id) == 0)
{
DEBUG_H("\tFound matching node %s", SYMBOL_SMILEY);
return node->data;
}
node = node->next;
}
return NULL;
}
/**
* Prints a list of hookups for debugging
* @param rootnode Lost of nodes to print.
* \memberof Hookup
*/
void Hookup_Debug(List* rootnode) {
ListNode* itr;
Hookup* hookup;
DEBUG(DEBUG_MEDIUM, "%sHookup_Debug", COLOUR_YELLOW);
for(itr = rootnode->first; itr; itr = itr->next) {
hookup = itr->data;
DEBUG(DEBUG_HIGH, "ptr: %p, ID: '%s', pointer:%p", hookup, hookup->id, hookup->ptr);
}
}
/**
* Links a source hookup data to the sinks pointer.
* @param source The hookup with the pointer to the source data.
* @param sink The hookup with the poitner to the data destiantion.
* \memberof Hookup
*/
void Hookup_Execute_Link(Hookup* source, Hookup* destination) {
DEBUG_H("Entering function...", source->id);
if(!destination) {
ERROR("No destination for node.");
return;
}
if(!destination->ptr) {
ERROR("No destination->ptr for node.");
return;
}
source->hooked = 1;
destination->hooked = 1;
*destination->ptr = source->ptr;
}
/**
* Connects a single source data hookup to all matching sinks.
* @param source The hookup to be hooked up.
* @param sinks A List of sink hookups.
* \memberof Hookup
*/
void Hookup_Execute(Hookup* source, List* sinks) {
ListNode* node = sinks->first;
DEBUG_M("%sHookup_Find(%s'%s'%s)", COLOUR_LIGHT_BLUE, COLOUR_YELLOW, source->id, COLOUR_LIGHT_BLUE);
while(node) {
DEBUG_H("\tCHECKING: '%s'", ((Hookup*)node->data)->id);
if(node->data
&& strcasecmp(((Hookup*)node->data)->id, source->id) == 0)
{
DEBUG_H("\tFound matching node %s", SYMBOL_SMILEY);
//return node->data;
Hookup_Execute_Link(source, node->data);
}
node = node->next;
}
}
/**
* Connects all the source data to sink pointers.
* For instance a jpeg filename with the material, and the material with
* the triangle mesh.
* @param sources The List containting source data to be hooked up.
* @param sinks The List containing pointers to poitner to put the data.
* \memberof Hookup
*/
void Hookups_Execute(List* sources, List* sinks) {
DEBUG_M("Entering function...");
if(sources == NULL) {
return;
}
assert(sources);
assert(sinks);
Hookup* source;
Hookup* destination;
ListNode* itr;
DEBUG_V("\tLoop begiing...");
for(itr = sources->first; itr; itr = itr->next) {
DEBUG(DEBUG_VERY_HIGH, "\t\tLoop......");
source = itr->data;
if(!source) {
continue;
}
Hookup_Execute(source, sinks);
}
}
/**
* Removes node form list.
* @param node The node to remove from the secondary list.
* @param sinks The sinks.
* \memberof Hookup
*/
/*void HoopkUps_DitchSecondary(ListNode* node, List* sinks) {
assert(node);
assert(sinks);
ListNode* itr = sinks->first;
while(itr) {
//Hookup* hookup = itr->data;
if(itr->data == node->data) {
DEBUG_H("Removing secondary...");
List_DeleteNode(sinks, itr);
}
itr = itr->next;
}
}*/
/**
* DELETE's any hookup data that failed to find a sink from a List.
* Also deletes the hookup itself and the list node.
* @param list Pointer to the List head node containing the source hookups.
* \memberof Hookup
*/
void Hookups_DeleteMissing(List* list, List* sinks) {
DEBUG_M("Entering function...");
Hookup* hookup;
ListNode* itr;
ListNode* self;
//for(itr = list->first; itr; itr = itr->next) {
itr = list->first;
while(itr) {
hookup = itr->data;
self = itr;
itr = itr->next;
if(hookup->hooked == 0) {
DEBUG_M("\tMissing hookup found.....");
if(hookup->ptr) {
// Don't delete Images as they are shared, but deref...
if(isImage(hookup->ptr)) {
DEBUG_H("\t\tIMAGE FOUND NOT DELETED YAY!");
Image* image = hookup->ptr;
image->refs--;
} else {
DELETE(hookup->ptr);
}
hookup->ptr = NULL;
}
}
#warning ['TODO'] Remember why this is done here and not in the if above
DELETE(hookup);
List_DeleteNode(list, self);
}
DEBUG_H("Exiting function...");
}
/**
* Removes a refrence to an unused Image from a List of images.
* Will also remove it from the other List if its there too.
* @param images The list of Images.
* @param sinks Another list that the Image will also be removed from.
* \memberof Hookup
*/
/*void Hookups_DerefrenceMissingImages(List* images) {
DEBUG_M("Entering function...");
Hookup* hookup;
Image* testimg = NEW(Image, "");
ListNode* itr;
ListNode* self;
itr = images->first;
while(itr) {
hookup = itr->data;
self = itr;
itr = itr->next;
if(hookup->hooked == 0) {
Image* image = hookup->ptr;
// Check to ensure its an image...
//if(image->class_->deconstructor == testimg->class_->deconstructor) {
DEBUG_M("\tUnused image found.....");
//DELETE(hookup->ptr);
//hookup->ptr = NULL;
Image_DeRefrence(hookup->ptr);
hookup->ptr = NULL;
self->data = NULL;
DELETE(hookup);
List_DeleteNode(images, self);
//}
}
}
DELETE(testimg);
DEBUG_H("Exiting...");
}*/