From e2fb90a2970822b43b3134f5919049d367e7378c Mon Sep 17 00:00:00 2001 From: Dendy Date: Sun, 24 Jan 2021 18:35:00 +0100 Subject: [PATCH] Implement reparenting --- src/main.c | 4 ++++ src/node.c | 26 +++++++++++++++++--------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/main.c b/src/main.c index 2daafbf..cf1eafa 100644 --- a/src/main.c +++ b/src/main.c @@ -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); diff --git a/src/node.c b/src/node.c index 2e2b215..c003552 100644 --- a/src/node.c +++ b/src/node.c @@ -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));