aboutsummaryrefslogtreecommitdiff
path: root/src/tree.c
diff options
context:
space:
mode:
authorGravatar Jonas Gunz <himself@jonasgunz.de> 2020-03-16 03:22:23 +0100
committerGravatar Jonas Gunz <himself@jonasgunz.de> 2020-03-16 03:22:23 +0100
commit47334cfaa2315828e6f7b959a1bde180c9c29604 (patch)
treeacf365c3c947664faa97b40e892c63eadb332b35 /src/tree.c
parent9dc2a544f88ddedea24d16f05170cb90858c3e9a (diff)
downloaddns-47334cfaa2315828e6f7b959a1bde180c9c29604.tar.gz
moved tree_* functions to tree.h/.c
Diffstat (limited to 'src/tree.c')
-rw-r--r--src/tree.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/tree.c b/src/tree.c
new file mode 100644
index 0000000..c6fc33b
--- /dev/null
+++ b/src/tree.c
@@ -0,0 +1,94 @@
+/*
+ * tree.c
+ * (c) 2019 Jonas Gunz
+ * License: MIT
+ * */
+
+#include "tree.h"
+
+
+int tree_insert ( struct tree_node** _root, char* _key, void* _data )
+{
+ struct tree_node** node = _root;
+
+ while( *node ) {
+ int ret = string_compare ( (*node)->key, _key );
+ if ( ret > 0 ) {
+ node = & (*node)->above;
+ } else if ( ret < 0 ) {
+ node = & (*node)->below;
+ } else { //Already exists
+ return 1;
+ }
+ }
+
+ *node = malloc (sizeof(typeof(**node)));
+ if( ! *node )
+ return 1;
+
+ (*node)->key = _key;
+ (*node)->data = _data;
+
+ return 0;
+}
+
+int tree_balance ( struct tree_node** _root )
+{
+ return 1;
+}
+
+int tree_destroy ( struct tree_node** _root )
+{
+ return 1;
+}
+
+int string_compare ( char* _1, char* _2 )
+{
+ if ( !_1 || !_2 )
+ return 99;
+ int i;
+ for (i = 0; _1[i] && _2[i]; i++) {
+ char c1 = _1[i];
+ char c2 = _2[i];
+
+ //Convert to uppercase
+ if ( c1 >= 97 && c1 <= 122 )
+ c1 -= 32;
+ if ( c2 >= 97 && c2 <= 122 )
+ c2 -= 32;
+
+ if (c1 > c2)
+ return 1;
+ if (c1 < c2)
+ return -1;
+ }
+
+ if ( _1[i] == _2[i] )
+ return 0;
+ if ( _1[i] )
+ return 1;
+ if ( _2[i] )
+ return -1;
+
+ //TODO WARN may reach end of non-void function
+}
+
+void* tree_get ( struct tree_node** _root, char* _query )
+{
+ struct tree_node** node = _root;
+
+ while(*node) {
+ int ret = string_compare ( (*node)->key, _query );
+ if ( ret > 0 ) {
+ node = & (*node)->above;
+ } else if ( ret < 0 ) {
+ node = & (*node)->below;
+ } else {
+ break;
+ }
+ }
+
+ return *node ? (*node)->key : NULL;
+
+ return 0;
+}