-
Notifications
You must be signed in to change notification settings - Fork 0
/
Group.c
401 lines (349 loc) · 9 KB
/
Group.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "tools.h"
#include "ContainerNode.h"
#include "Instance.h"
#include "ContainerRoot.h"
#include "Group.h"
#include "NamedElement.h"
#include "Visitor.h"
#define DEBUG 0
#if DEBUG
#define PRINTF(...) printf(__VA_ARGS__)
#else
#define PRINTF(...)
#endif
Instance* newPoly_Group()
{
Group* pGroupObj = NULL;
Instance* pObj = new_Instance();
/* Allocating memory */
pGroupObj = (Group*)malloc(sizeof(Group));
if (pGroupObj == NULL)
{
pObj->Delete(pObj);
return NULL;
}
pObj->pDerivedObj = pGroupObj; /* Pointing to derived object */
pGroupObj->subNodes = NULL;
pGroupObj->eContainer = NULL;
pGroupObj->AddSubNodes = Group_AddSubNodes;
pGroupObj->RemoveSubNodes = Group_RemoveSubNodes;
pGroupObj->FindSubNodesByID = Group_FindSubNodesByID;
pGroupObj->FindByPath = Group_FindByPath;
pObj->internalGetKey = Group_internalGetKey;
pObj->metaClassName = Group_metaClassName;
pObj->VisitAttributes = Group_VisitAttributes;
pObj->VisitPathAttributes = Group_VisitPathAttributes;
pObj->VisitReferences = Group_VisitReferences;
pObj->VisitPathReferences = Group_VisitPathReferences;
pObj->Delete = deletePoly_Group;
return pObj;
}
Group* new_Group()
{
Group* pGroupObj = NULL;
Instance* pObj = new_Instance();
if(pObj == NULL)
return NULL;
/* Allocating memory */
pGroupObj = (Group*)malloc(sizeof(Group));
if (pGroupObj == NULL)
{
return NULL;
}
/*pObj->pDerivedObj = pGroupObj; Pointing to derived object */
pGroupObj->super = pObj;
/*pGroupObj->subNodes = hashmap_new();*/
pGroupObj->subNodes = NULL;
pGroupObj->eContainer = NULL;
pGroupObj->AddSubNodes = Group_AddSubNodes;
pGroupObj->RemoveSubNodes = Group_RemoveSubNodes;
pGroupObj->internalGetKey = Group_internalGetKey;
pGroupObj->metaClassName = Group_metaClassName;
pObj->super->metaClassName = Group_metaClassName;
pGroupObj->VisitAttributes = Group_VisitAttributes;
pGroupObj->VisitPathAttributes = Group_VisitPathAttributes;
pGroupObj->VisitReferences = Group_VisitReferences;
pGroupObj->VisitPathReferences = Group_VisitPathReferences;
pGroupObj->FindSubNodesByID = Group_FindSubNodesByID;
pGroupObj->FindByPath = Group_FindByPath;
pGroupObj->Delete = delete_Group;
return pGroupObj;
}
char* Group_internalGetKey(void* const this)
{
Group *pObj = (Group*)this;
return pObj->super->internalGetKey(pObj->super);
}
char* Group_metaClassName(void* const this)
{
char *name;
name = malloc(sizeof(char) * (strlen("Group")) + 1);
if(name != NULL)
strcpy(name, "Group");
else
return NULL;
return name;
}
ContainerNode* Group_FindSubNodesByID(Group* const this, char* id)
{
ContainerNode* value = NULL;
if(this->subNodes != NULL)
{
if(hashmap_get(this->subNodes, id, (void**)(&value)) == MAP_OK)
return value;
else
return NULL;
}
else
{
return NULL;
}
}
void Group_AddSubNodes(Group* const this, ContainerNode* ptr)
{
ContainerNode* container = NULL;
char *internalKey = ptr->internalGetKey(ptr);
if(internalKey == NULL)
{
PRINTF("The ContainerNode cannot be added in Group because the key is not defined\n");
}
else
{
if(this->subNodes == NULL)
{
this->subNodes = hashmap_new();
}
if(hashmap_get(this->subNodes, internalKey, (void**)(&container)) == MAP_MISSING)
{
/*container = (ContainerNode*)ptr;*/
hashmap_put(this->subNodes, internalKey, ptr);
}
}
}
void Group_RemoveSubNodes(Group* const this, ContainerNode* ptr)
{
char *internalKey = ptr->internalGetKey(ptr);
if(internalKey == NULL)
{
PRINTF("The ContainerNode cannot be removed in Group because the key is not defined\n");
}
else
{
hashmap_remove(this->subNodes, internalKey);
free(internalKey);
}
}
void deletePoly_Group(void* const this)
{
if(this != NULL)
{
Instance *pObj = (Instance*)this;
Group* pGroupObj;
pGroupObj = pObj->pDerivedObj;
/*destroy derived obj*/
/*
* TODO verify NULLity
* TODO fix polymorphism
*/
hashmap_free(pGroupObj->subNodes);
free(pGroupObj->eContainer);
free(pGroupObj);
/*destroy base Obj*/
delete_Instance(this);
}
}
void delete_Group(void* const this)
{
if(this != NULL)
{
Group *pObj = (Group*)this;
/* destroy base object */
delete_Instance(pObj->super);
/* destroy data memebers */
/*
* TODO verify NULLity
* TODO fix polymorphism
*/
hashmap_free(pObj->subNodes);
free(pObj->eContainer);
free(this);
/*this = NULL;*/
}
}
void Group_VisitAttributes(void *const this, char *parent, Visitor *visitor, bool recursive)
{
Instance_VisitAttributes(((Group*)(this))->super, parent, visitor, recursive);
}
void Group_VisitPathAttributes(void *const this, char *parent, Visitor *visitor, bool recursive)
{
Instance_VisitPathAttributes(((Group*)(this))->super, parent, visitor, recursive);
}
void Group_VisitReferences(void* const this, char* parent, Visitor* visitor, bool recursive)
{
char path[256];
memset(&path[0], 0, sizeof(path));
if(((Group*)(this))->subNodes != NULL)
{
visitor->action("subNodes", SQBRACKET, NULL);
int i;
int length = hashmap_length(((Group*)(this))->subNodes);
/* subNodes */
hashmap_map* m = ((Group*)(this))->subNodes;
/* compare subNodes */
for(i = 0; i< m->table_size; i++)
{
if(m->data[i].in_use != 0)
{
any_t data = (any_t) (m->data[i].data);
ContainerNode* n = data;
sprintf(path,"nodes[%s]", n->internalGetKey(n));
visitor->action(path, STRREF, NULL);
if(length > 1)
{
visitor->action(NULL, COLON, NULL);
length--;
}
else
visitor->action(NULL, RETURN, NULL);
}
}
visitor->action(NULL, CLOSESQBRACKETCOLON, NULL);
}
else
{
visitor->action("subNodes", SQBRACKET, NULL);
visitor->action(NULL, CLOSESQBRACKETCOLON, NULL);
}
Instance_VisitReferences(((Group*)(this))->super, parent, visitor, false);
}
void Group_VisitPathReferences(void *const this, char *parent, Visitor *visitor, bool recursive)
{
char path[256];
memset(&path[0], 0, sizeof(path));
Instance_VisitPathReferences(((Group*)(this))->super, parent, visitor, false);
if(((Group*)(this))->subNodes != NULL)
{
int i;
/* subNodes */
hashmap_map* m = ((Group*)(this))->subNodes;
/* compare subNodes */
for(i = 0; i< m->table_size; i++)
{
if(m->data[i].in_use != 0)
{
any_t data = (any_t) (m->data[i].data);
ContainerNode* n = data;
sprintf(path,"%s/%s\\subNodes", parent, n->path);
visitor->action(path, REFERENCE, parent);
}
}
}
}
void* Group_FindByPath(char* attribute, void* const this)
{
Group *pObj = (Group*)this;
/* There is no local attributes */
/* Instance attributes and references */
if(!strcmp("name",attribute) || !strcmp("metaData",attribute) || !strcmp("started",attribute) || !strcmp("typeDefinition",attribute))
{
return Instance_FindByPath(attribute, pObj->super);/*return this->super->metaData;*/
}
/* Local references */
else
{
char path[250];
memset(&path[0], 0, sizeof(path));
char token[100];
memset(&token[0], 0, sizeof(token));
char *obj = NULL;
char key[50];
memset(&key[0], 0, sizeof(key));
char nextPath[150];
memset(&nextPath[0], 0, sizeof(nextPath));
char *nextAttribute = NULL;
strcpy(path, attribute);
if(strchr(path, '[') != NULL)
{
obj = strdup(strtok(path, "["));
strcpy(path, attribute);
PRINTF("Object: %s\n", obj);
strcpy(token, strtok(path, "]"));
strcpy(path, attribute);
sprintf(token, "%s]", token);
PRINTF("Token: %s\n", token);
sscanf(token, "%*[^[][%[^]]", key);
PRINTF("Key: %s\n", key);
if((strchr(path, '\\')) != NULL)
{
nextAttribute = strtok(NULL, "\\");
PRINTF("Attribute: %s\n", nextAttribute);
if(strchr(nextAttribute, '['))
{
sprintf(nextPath, "%s\\%s", ++nextAttribute, strtok(NULL, "\\"));
PRINTF("Next Path: %s\n", nextPath);
}
else
{
strcpy(nextPath, nextAttribute);
PRINTF("Next Path: %s\n", nextPath);
}
}
else
{
nextAttribute = strtok(path, "]");
bool isFirst = true;
char *fragPath = NULL;
while ((fragPath = strtok(NULL, "]")) != NULL) {
PRINTF("Attribute: %s]\n", fragPath);
if (isFirst) {
sprintf(nextPath, "%s]", ++fragPath);
isFirst = false;
} else {
sprintf(nextPath, "%s/%s]", nextPath, ++fragPath);
}
PRINTF("Next Path: %s\n", nextPath);
}
if (strlen(nextPath) == 0) {
PRINTF("Attribute: NULL\n");
PRINTF("Next Path: NULL\n");
nextAttribute = NULL;
}
}
}
else
{
if ((nextAttribute = strtok(path, "\\")) != NULL) {
if ((nextAttribute = strtok(NULL, "\\")) != NULL) {
PRINTF("Attribute: %s\n", nextAttribute);
} else {
nextAttribute = strtok(path, "\\");
PRINTF("Attribute: %s\n", nextAttribute);
}
}
}
if(!strcmp("nodes", obj))
{
free(obj);
if(nextAttribute == NULL)
{
return pObj->FindSubNodesByID(pObj, key);
}
else
{
ContainerNode* contnode = pObj->FindSubNodesByID(pObj, key);
if(contnode != NULL)
return contnode->FindByPath(nextPath, contnode);
else
return NULL;
}
}
else
{
free(obj);
return Instance_FindByPath(attribute, pObj->super);
}
}
}