Initial commit

This commit is contained in:
Dendy 2021-01-24 10:54:52 +01:00
commit f053b6cc5a
Signed by: dendy
GPG Key ID: 0168B35FFD7F608F
5 changed files with 273 additions and 0 deletions

97
.gitignore vendored Normal file
View File

@ -0,0 +1,97 @@
# Created by https://www.toptal.com/developers/gitignore/api/vim,c,linux
# Edit at https://www.toptal.com/developers/gitignore?templates=vim,c,linux
### C ###
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
### Linux ###
*~
# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*
# KDE directory preferences
.directory
# Linux trash folder which might appear on any partition or disk
.Trash-*
# .nfs files are created when an open file is removed but is still being accessed
.nfs*
### Vim ###
# Swap
[._]*.s[a-v][a-z]
!*.svg # comment out if you don't need vector files
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]
# Session
Session.vim
Sessionx.vim
# Temporary
.netrwhist
# Auto-generated tag files
tags
# Persistent undo
[._]*.un~
# End of https://www.toptal.com/developers/gitignore/api/vim,c,linux
main
build/
assets2/

15
Makefile Normal file
View File

@ -0,0 +1,15 @@
##
# Nodesystem
#
# @file
# @version 0.1
CC="tcc"
main: src/main.c src/node.c
tcc -o $@ -I src $^
clean:
rm -rf main
# end

37
src/main.c Normal file
View File

@ -0,0 +1,37 @@
#include<stdio.h>
#include "node.h"
int main(int argc, char** argv){
NOD_Node* node = NOD_CreateNode("test node");
NOD_Node* testchild = NOD_CreateNode("I'm just a child");
NOD_Node* otherchild = NOD_CreateNode("Another child");
NOD_Node* justtotest = NOD_CreateNode("This one is a test");
NOD_NodeAddChild(node, testchild);
NOD_NodeAddChild(node, otherchild);
NOD_NodeAddChild(node, justtotest);
NOD_NodeAddChild(node, justtotest);
NOD_NodeAddChild(node, justtotest);
NOD_NodeAddChild(node, node);
NOD_NodeAddChild(testchild, node);
printf("\nThe name of the node is \"%s\"\n", node->name);
for(int i = 0; i < node->childc; i++){
printf("Childnode: \"%s\"\n", node->childv[i]->name);
}
printf("\nThe name of the node is \"%s\"\n", node->name);
for(int i = 0; i < node->childc; i++){
printf("Childnode: \"%s\"\n", node->childv[i]->name);
}
printf("\nThe amount of children is %d, the real size is %d\n",
node->childc,
(int)sizeof(testchild)
);
NOD_DestroyNode(node);
NOD_DestroyNode(testchild);
return 0;
}

105
src/node.c Normal file
View File

@ -0,0 +1,105 @@
#include "node.h"
#include <stdlib.h>
#include <stdio.h>
NOD_Node* NOD_CreateNode(char* name){
NOD_Node* node = malloc(sizeof(NOD_Node));
if(node != NULL){
node->name = name;
node->parent = NULL;
node->childv = NULL;
node->childc = 0;
}
return node;
}
void NOD_DestroyNode(NOD_Node* node){
if(node != NULL){
free(node);
}
}
void NOD_NodeAddChild(NOD_Node* parent, NOD_Node* child){
if(parent != NULL && child != NULL){
/** Prevent recursion */
if(parent == 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;
}
/** 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");
return;
}
}
/** Create child vector if it hasn't been used */
if(parent->childc == 0 || parent->childv == NULL){
parent->childv = malloc(sizeof(child));
if(parent->childv != NULL){
parent->childv[0] = child;
parent->childc = 1;
child->parent = parent;
}
else{
fprintf(stderr,
"NOD_Error: Failed to allocate space for child vector \"%s\"\n",
parent->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++;
child->parent = parent;
}
else{
fprintf(stderr,
"NOD_Error: Failed to reallocate space for child vector \"%s\"\n",
parent->name);
}
}
}
}
void NOD_NodeRemoveChild(NOD_Node* parent, NOD_Node* child){
if(parent != NULL && child != NULL){
if(parent->childc < 1 || parent->childv == NULL){
fprintf(stderr,
"NOD_Error: Cannot remove child \"%s\". Node \"%s\" has no children.\n",
child->name,
parent->name);
}
}
}
void NOD_PrintNodeTree(NOD_Node* node){
if(node != NULL){
printf("%s", node->name);
}
}

19
src/node.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef __NODE_H_
#define __NODE_H_
typedef struct node{
char* name;
int childc;
struct node** childv;
struct node* parent;
} NOD_Node;
NOD_Node* NOD_CreateNode(char* name);
void NOD_DestroyNode(NOD_Node* node);
void NOD_NodeAddChild(NOD_Node* parent, NOD_Node* child);
void NOD_NodeRemoveChild(NOD_Node* parent, NOD_Node* child);
#endif // __NODE_H_