finish tree view
This commit is contained in:
parent
1408cca6b7
commit
fd25215671
80
src/main.c
80
src/main.c
|
@ -3,31 +3,67 @@
|
|||
#include "node.h"
|
||||
|
||||
int main(int argc, char* *argv){
|
||||
NOD_Node* node = NOD_CreateNode("Main Node");
|
||||
NOD_Node* child1 = NOD_CreateNode("First child");
|
||||
NOD_Node* child1child1 = NOD_CreateNode("Child of first child");
|
||||
NOD_Node* child2 = NOD_CreateNode("Second child");
|
||||
NOD_Node* child3 = NOD_CreateNode("Third child");
|
||||
NOD_Node* child3child1 = NOD_CreateNode("First child of third child");
|
||||
NOD_Node* child3child2 = NOD_CreateNode("Second child of third child");
|
||||
NOD_Node *distros = NOD_CreateNode("Distros");
|
||||
NOD_Node *linoox = NOD_CreateNode("GNU/Linux");
|
||||
NOD_Node *debian = NOD_CreateNode("Debian");
|
||||
NOD_Node *ubuntu = NOD_CreateNode("Ubuntu");
|
||||
NOD_Node *mint = NOD_CreateNode("Linux Mint");
|
||||
NOD_Node *devuan = NOD_CreateNode("Devuan");
|
||||
NOD_Node *kali = NOD_CreateNode("Kali Linux");
|
||||
NOD_Node *voidlinux = NOD_CreateNode("Void Linux");
|
||||
NOD_Node *arch = NOD_CreateNode("Arch");
|
||||
NOD_Node *manjaro = NOD_CreateNode("Manjaro");
|
||||
NOD_Node *parabola = NOD_CreateNode("Parabola");
|
||||
NOD_Node *endeavouros = NOD_CreateNode("EndeavourOS");
|
||||
NOD_Node *bsd = NOD_CreateNode("BSD");
|
||||
NOD_Node *freebsd = NOD_CreateNode("FreeBSD");
|
||||
NOD_Node *openbsd = NOD_CreateNode("OpenBSD");
|
||||
NOD_Node *debianbsd = NOD_CreateNode("Debian GNU/kFreeBSD");
|
||||
|
||||
NOD_NodeAddChild(node, child1);
|
||||
NOD_NodeAddChild(child1, child1child1);
|
||||
NOD_NodeAddChild(node, child2);
|
||||
NOD_NodeAddChild(node, child3);
|
||||
NOD_NodeAddChild(child3, child3child1);
|
||||
NOD_NodeAddChild(child3, child3child2);
|
||||
NOD_NodeAddChild(distros, linoox);
|
||||
NOD_NodeAddChild(distros, bsd);
|
||||
|
||||
NOD_NodeUnparent(child2);
|
||||
NOD_NodeAddChild(linoox, debian);
|
||||
NOD_NodeAddChild(linoox, voidlinux);
|
||||
NOD_NodeAddChild(linoox, arch);
|
||||
|
||||
NOD_PrintNodeTree(node);
|
||||
NOD_NodeAddChild(debian, ubuntu);
|
||||
NOD_NodeAddChild(debian, devuan);
|
||||
NOD_NodeAddChild(debian, kali);
|
||||
|
||||
NOD_DestroyNode(node);
|
||||
NOD_DestroyNode(child1);
|
||||
NOD_DestroyNode(child1child1);
|
||||
NOD_DestroyNode(child2);
|
||||
NOD_DestroyNode(child3);
|
||||
NOD_DestroyNode(child3child1);
|
||||
NOD_DestroyNode(child3child2);
|
||||
NOD_NodeAddChild(ubuntu, mint);
|
||||
|
||||
NOD_NodeAddChild(arch, manjaro);
|
||||
NOD_NodeAddChild(arch, parabola);
|
||||
NOD_NodeAddChild(arch, endeavouros);
|
||||
|
||||
NOD_NodeAddChild(bsd, freebsd);
|
||||
NOD_NodeAddChild(bsd, openbsd);
|
||||
NOD_NodeAddChild(bsd, debianbsd);
|
||||
|
||||
//NOD_NodeUnparent(child2);
|
||||
|
||||
NOD_PrintNodeTree(distros);
|
||||
NOD_PrintNodeTree(arch);
|
||||
NOD_PrintNodeTree(linoox);
|
||||
NOD_PrintNodeTree(debian);
|
||||
NOD_PrintNodeTree(freebsd);
|
||||
|
||||
NOD_DestroyNode(distros);
|
||||
NOD_DestroyNode(linoox);
|
||||
NOD_DestroyNode(debian);
|
||||
NOD_DestroyNode(ubuntu);
|
||||
NOD_DestroyNode(mint);
|
||||
NOD_DestroyNode(devuan);
|
||||
NOD_DestroyNode(kali);
|
||||
NOD_DestroyNode(voidlinux);
|
||||
NOD_DestroyNode(arch);
|
||||
NOD_DestroyNode(manjaro);
|
||||
NOD_DestroyNode(parabola);
|
||||
NOD_DestroyNode(endeavouros);
|
||||
NOD_DestroyNode(bsd);
|
||||
NOD_DestroyNode(freebsd);
|
||||
NOD_DestroyNode(openbsd);
|
||||
NOD_DestroyNode(debianbsd);
|
||||
return 0;
|
||||
}
|
||||
|
|
54
src/node.c
54
src/node.c
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
NOD_Node *NOD_CreateNode(char *name){
|
||||
NOD_Node *node = malloc(sizeof(NOD_Node));
|
||||
|
@ -109,32 +110,65 @@ void NOD_NodeUnparent(NOD_Node* node){
|
|||
}
|
||||
}
|
||||
|
||||
void nodetree_print_branch(NOD_Node* node, int level, int pos){
|
||||
int get_child_pos(NOD_Node *node){
|
||||
int count = 0;
|
||||
if(node != NULL && node->parent != NULL){
|
||||
for(int i = 0; i < node->parent->childc; i++){
|
||||
if(node->parent->childv[i] == node) return count;
|
||||
count++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void nodetree_print_branch(NOD_Node *node, char* level){
|
||||
if(node != NULL){
|
||||
|
||||
if(pos+1 != node->parent->childc) printf("├── ");
|
||||
else printf("└── ");
|
||||
printf("\"%s\"\n", node->name);
|
||||
for(int i = 0; i < node->childc; i++){
|
||||
if(pos+1 != node->parent->childc) printf("│ ");
|
||||
for(int i = 0; level[i] != '\0'; i++){
|
||||
if(level[i] == 'y') printf("│ ");
|
||||
else printf(" ");
|
||||
nodetree_print_branch(node->childv[i], level+1, i);
|
||||
}
|
||||
|
||||
if(node->parent != NULL){
|
||||
if(get_child_pos(node)+1 != node->parent->childc){
|
||||
printf("├─ ");
|
||||
strcat(level, "y");
|
||||
}
|
||||
else{
|
||||
printf("└─ ");
|
||||
strcat(level, "n");
|
||||
}
|
||||
}
|
||||
printf("%s\n", node->name);
|
||||
|
||||
/** Recurse */
|
||||
for(int i = 0; i < node->childc; i++){
|
||||
nodetree_print_branch(node->childv[i], level);
|
||||
}
|
||||
|
||||
if(node->parent != NULL){
|
||||
level = (char *)realloc(level, strlen(level) * sizeof(char));
|
||||
level[strlen(level)-1] = '\0';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NOD_PrintNodeTree(NOD_Node *node){
|
||||
if(node != NULL){
|
||||
printf("\"%s\"\n", node->name);
|
||||
char* level = strdup("");
|
||||
|
||||
printf("%s\n", node->name);
|
||||
for(int i = 0; i < node->childc; i++){
|
||||
nodetree_print_branch(node->childv[i], 0, i);
|
||||
nodetree_print_branch(node->childv[i], level);
|
||||
}
|
||||
|
||||
free(level);
|
||||
}
|
||||
}
|
||||
|
||||
int NOD_NodeChildCount(NOD_Node *node){
|
||||
unsigned int count = 0;
|
||||
int count = 0;
|
||||
for(int i = 0; i < node->childc; i++){
|
||||
if(node->childv[i] != NULL) count++;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue