Fill in the blanks

This commit is contained in:
Dendy 2021-01-24 19:33:20 +01:00
parent e2fb90a297
commit 48908719fa
Signed by: dendy
GPG Key ID: 0168B35FFD7F608F
2 changed files with 35 additions and 30 deletions

View File

@ -28,8 +28,8 @@ int main(int argc, char* *argv){
NOD_NodeAddChild(linoox, arch); NOD_NodeAddChild(linoox, arch);
NOD_NodeAddChild(debian, ubuntu); NOD_NodeAddChild(debian, ubuntu);
NOD_NodeAddChild(debian, devuan);
NOD_NodeAddChild(debian, kali); NOD_NodeAddChild(debian, kali);
NOD_NodeAddChild(debian, devuan);
NOD_NodeAddChild(ubuntu, mint); NOD_NodeAddChild(ubuntu, mint);
@ -40,13 +40,6 @@ int main(int argc, char* *argv){
NOD_NodeAddChild(bsd, freebsd); NOD_NodeAddChild(bsd, freebsd);
NOD_NodeAddChild(bsd, openbsd); NOD_NodeAddChild(bsd, openbsd);
NOD_NodeAddChild(bsd, debianbsd); 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); //NOD_NodeUnparent(child2);

View File

@ -55,7 +55,9 @@ void NOD_NodeAddChild(NOD_Node *parent, NOD_Node *child){
/** Check if child already in vector */ /** Check if child already in vector */
for(int i = 0; i < parent->childc; i++){ for(int i = 0; i < parent->childc; i++){
if(parent->childv[i] == child){ 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; return;
} }
} }
@ -67,7 +69,7 @@ void NOD_NodeAddChild(NOD_Node *parent, NOD_Node *child){
} }
/** Create child vector if it hasn't been used */ /** 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)); parent->childv = malloc(sizeof(child));
if(parent->childv != NULL){ if(parent->childv != NULL){
@ -75,33 +77,42 @@ void NOD_NodeAddChild(NOD_Node *parent, NOD_Node *child){
parent->childc = 1; parent->childc = 1;
child->parent = parent; child->parent = parent;
return;
} }
else{ else{
fprintf(stderr, fprintf(stderr,
"NOD_Error: Failed to allocate space for child vector \"%s\"\n", "NOD_Error: Failed to allocate space on \"%s\"for child vector \"%s\"\n",
parent->name); parent->name, child->name);
} }
} }
/** Realloc if it already exists */ /** If there is a blank space, use it */
for(int i = 0; i < parent->childc; i++){
/** I don't test if chilv is null because it if(parent->childv[i] == NULL){
has already been tested */ printf("NOD_Info: Reused blank space for child :D\n");
else if(parent->childc > 0){ parent->childv[i] = 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; child->parent = parent;
return;
} }
else{ }
fprintf(stderr,
"NOD_Error: Failed to reallocate space for child vector \"%s\"\n", /** If not, reallocate vector to fit the child */
parent->name); 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 */ /* Free allocated memory if last child */
if(i + 1 == parent->childc){ if(i + 1 == parent->childc){
//fprintf(stderr, fprintf(stderr,
//"NOD_Debug: Freeing last child of vector...\n"); "NOD_Debug: Freeing last child \"%s\" of vector \"%s\"...\n",
node->name, parent->name);
parent->childv = realloc(parent->childv, sizeof(node) *parent->childc); parent->childv = realloc(parent->childv, sizeof(node) *parent->childc);
parent->childc--; parent->childc--;
} }