Fill in the blanks
This commit is contained in:
parent
e2fb90a297
commit
48908719fa
|
@ -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);
|
||||
|
||||
|
|
38
src/node.c
38
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,19 +77,29 @@ 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 */
|
||||
/** 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;
|
||||
|
||||
/** I don't test if chilv is null because it
|
||||
has already been tested */
|
||||
else if(parent->childc > 0){
|
||||
child->parent = parent;
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/** If not, reallocate vector to fit the child */
|
||||
parent->childv = realloc(parent->childv,
|
||||
sizeof(child) *(parent->childc+1));
|
||||
|
||||
|
@ -99,9 +111,8 @@ void NOD_NodeAddChild(NOD_Node *parent, NOD_Node *child){
|
|||
}
|
||||
else{
|
||||
fprintf(stderr,
|
||||
"NOD_Error: Failed to reallocate space for child vector \"%s\"\n",
|
||||
parent->name);
|
||||
}
|
||||
"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--;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue