From 48908719facb97d83be7893d91e211800972c217 Mon Sep 17 00:00:00 2001 From: Dendy Date: Sun, 24 Jan 2021 19:33:20 +0100 Subject: [PATCH] Fill in the blanks --- src/main.c | 9 +-------- src/node.c | 56 +++++++++++++++++++++++++++++++++--------------------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/main.c b/src/main.c index cf1eafa..ce4b12f 100644 --- a/src/main.c +++ b/src/main.c @@ -28,8 +28,8 @@ int main(int argc, char* *argv){ NOD_NodeAddChild(linoox, arch); NOD_NodeAddChild(debian, ubuntu); - NOD_NodeAddChild(debian, devuan); NOD_NodeAddChild(debian, kali); + NOD_NodeAddChild(debian, devuan); NOD_NodeAddChild(ubuntu, mint); @@ -40,13 +40,6 @@ 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); //NOD_NodeUnparent(child2); diff --git a/src/node.c b/src/node.c index c003552..9c26814 100644 --- a/src/node.c +++ b/src/node.c @@ -55,7 +55,9 @@ void NOD_NodeAddChild(NOD_Node *parent, NOD_Node *child){ /** Check if child already in vector */ for(int i = 0; i < parent->childc; i++){ if(parent->childv[i] == child){ - fprintf(stderr, "NOD_Warning: Tried to add a child already in the vector.\n"); + fprintf(stderr, "NOD_Warning: Adding child \"%s\" already in the vector \"%s\".\n", + child->name, + parent->name); return; } } @@ -67,7 +69,7 @@ void NOD_NodeAddChild(NOD_Node *parent, NOD_Node *child){ } /** Create child vector if it hasn't been used */ - if(parent->childc == 0 || parent->childv == NULL){ + if(parent->childc <= 0 || parent->childv == NULL){ parent->childv = malloc(sizeof(child)); if(parent->childv != NULL){ @@ -75,33 +77,42 @@ void NOD_NodeAddChild(NOD_Node *parent, NOD_Node *child){ parent->childc = 1; child->parent = parent; + + return; } else{ fprintf(stderr, - "NOD_Error: Failed to allocate space for child vector \"%s\"\n", - parent->name); + "NOD_Error: Failed to allocate space on \"%s\"for child vector \"%s\"\n", + parent->name, child->name); } } - /** Realloc if it already exists */ - - /** I don't test if chilv is null because it - has already been tested */ - else if(parent->childc > 0){ - parent->childv = realloc(parent->childv, - sizeof(child) *(parent->childc+1)); - - if(parent->childv != NULL){ - parent->childv[parent->childc] = child; - parent->childc++; + /** If there is a blank space, use it */ + for(int i = 0; i < parent->childc; i++){ + if(parent->childv[i] == NULL){ + printf("NOD_Info: Reused blank space for child :D\n"); + parent->childv[i] = child; child->parent = parent; + + return; } - else{ - fprintf(stderr, - "NOD_Error: Failed to reallocate space for child vector \"%s\"\n", - parent->name); - } + } + + /** If not, reallocate vector to fit the child */ + parent->childv = realloc(parent->childv, + sizeof(child) *(parent->childc+1)); + + if(parent->childv != NULL){ + parent->childv[parent->childc] = child; + parent->childc++; + + child->parent = parent; + } + else{ + fprintf(stderr, + "NOD_Error: Failed to reallocate space on \"%s\"for child vector \"%s\"\n", + parent->name, child->name); } } @@ -117,8 +128,9 @@ void NOD_NodeUnparent(NOD_Node *node){ /* Free allocated memory if last child */ if(i + 1 == parent->childc){ - //fprintf(stderr, - //"NOD_Debug: Freeing last child of vector...\n"); + fprintf(stderr, + "NOD_Debug: Freeing last child \"%s\" of vector \"%s\"...\n", + node->name, parent->name); parent->childv = realloc(parent->childv, sizeof(node) *parent->childc); parent->childc--; }