Implement reparenting

This commit is contained in:
Dendy 2021-01-24 18:35:00 +01:00
parent 91eacbb8c7
commit e2fb90a297
Signed by: dendy
GPG Key ID: 0168B35FFD7F608F
2 changed files with 21 additions and 9 deletions

View File

@ -40,6 +40,10 @@ int main(int argc, char* *argv){
NOD_NodeAddChild(bsd, freebsd);
NOD_NodeAddChild(bsd, openbsd);
NOD_NodeAddChild(bsd, debianbsd);
NOD_NodeAddChild(debian, debianbsd);
NOD_NodeAddChild(debian, debianbsd);
NOD_NodeAddChild(debianbsd, debian);
NOD_NodeAddChild(linoox, debianbsd);
NOD_DestroyNodeBranch(bsd);
NOD_DestroyNodeBranch(ubuntu);

View File

@ -20,7 +20,8 @@ NOD_Node *NOD_CreateNode(char *name){
void NOD_DestroyNode(NOD_Node *node){
if(node != NULL){
printf("[ INF ] Deleting node \"%s\"\n", node->name);
printf("NOD_Info: Deleting node \"%s\"\n", node->name);
/** Cleanup dependencies on this node by its parent */
NOD_NodeUnparent(node);
free(node);
}
@ -42,14 +43,15 @@ void NOD_NodeAddChild(NOD_Node *parent, NOD_Node *child){
fprintf(stderr, "NOD_Warning: A node cannot be added to itself.\n");
return;
}
NOD_Node *tmp_parent = parent->parent;
while(tmp_parent != NULL){
if(tmp_parent == child){
fprintf(stderr, "NOD_Warning: Can't add a parent of a node as its child.\n");
return;
}
tmp_parent = tmp_parent->parent;
}
///** Don't allow tree-breaking reparenting */
//NOD_Node *tmp_parent = parent->parent;
//while(tmp_parent != NULL){
//if(tmp_parent == child){
//fprintf(stderr, "NOD_Warning: Can't add a parent of a node as its child.\n");
//return;
//}
//tmp_parent = tmp_parent->parent;
//}
/** Check if child already in vector */
for(int i = 0; i < parent->childc; i++){
if(parent->childv[i] == child){
@ -58,6 +60,12 @@ void NOD_NodeAddChild(NOD_Node *parent, NOD_Node *child){
}
}
/** If it had a parent before, unparent before reparenting */
if(child->parent != NULL && child->parent != parent){
printf("NOD_Info: Reparenting node \"%s\"\n", child->name);
NOD_NodeUnparent(child);
}
/** Create child vector if it hasn't been used */
if(parent->childc == 0 || parent->childv == NULL){
parent->childv = malloc(sizeof(child));