From 47334cfaa2315828e6f7b959a1bde180c9c29604 Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Mon, 16 Mar 2020 03:22:23 +0100 Subject: moved tree_* functions to tree.h/.c --- src/tree.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/tree.h | 35 ++++++++++++++++++++++ src/zonefile.c | 81 -------------------------------------------------- src/zonefile.h | 17 ++--------- 4 files changed, 131 insertions(+), 96 deletions(-) create mode 100644 src/tree.c create mode 100644 src/tree.h (limited to 'src') 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; +} diff --git a/src/tree.h b/src/tree.h new file mode 100644 index 0000000..958b5ed --- /dev/null +++ b/src/tree.h @@ -0,0 +1,35 @@ +/* + * tree.h + * (c) 2019 Jonas Gunz + * License: MIT + * */ + +#pragma once + +#include +#include + +struct tree_node { + char* key; + void* data; + struct tree_node* above; + struct tree_node* below; +}; + +int tree_insert ( struct tree_node** _root, char* _key, void* _data ); + +int tree_balance( struct tree_node** _root ); + +void* tree_get ( struct tree_node** _root, char* _query ); + +int tree_destroy( struct tree_node** _root ); + +/** + * ignore-case alphabetical string compare + * returns: + * 0 :: _1 == _2 + * -1 :: _1 < _2 + * +1 :: _1 > _2 + * */ +int string_compare ( char* _1, char* _2 ); + diff --git a/src/zonefile.c b/src/zonefile.c index 3ed47c8..7ef3535 100644 --- a/src/zonefile.c +++ b/src/zonefile.c @@ -16,85 +16,4 @@ int zonefile_query ( char* _hostname, struct record_entry* _entry ) return 1; } -static int tree_insert ( struct record_node* _root, struct record_entry* _node ) -{ - struct record_node* node = _root; - - while(node) { - int ret = string_compare ( node->rr->name, _node->name ); - if ( ret > 0 ) { - node = node->above; - } else if ( ret < 0 ) { - node = node->below; - } else { //Already exists - return 1; - } - } - - node = malloc (sizeof(*node)); - if(!node) - return 1; - - node->rr = _node; - - return 0; -} - -static int tree_balance ( struct record_node* _root ) -{ - return 1; -} - -static struct record_entry* tree_get ( struct record_node* _root, char* _query ) -{ - struct record_node* node = _root; - - while(node) { - int ret = string_compare ( node->rr->name, _query ); - if ( ret > 0 ) { - node = node->above; - } else if ( ret < 0 ) { - node = node->below; - } else { - break; - } - } - return node ? node->rr : NULL; - - return 0; -} - -static int tree_destroy ( struct record_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; -} diff --git a/src/zonefile.h b/src/zonefile.h index 94a0de7..633e824 100644 --- a/src/zonefile.h +++ b/src/zonefile.h @@ -9,6 +9,8 @@ #include #include +#include "tree.h" + struct record_entry { char* name; uint32_t ttl; @@ -30,18 +32,3 @@ int zonefile_parse ( char* _filename, struct record_node* _dns_zone ); int zonefile_query ( char* _hostname, struct record_entry* _entry ); -static int tree_insert ( struct record_node* _root, struct record_entry* _node ); - -static int tree_balance ( struct record_node* _root ); - -static struct record_entry* tree_get ( struct record_node* _root, char* _query ); - -static int tree_destroy ( struct record_node* _root ); - -/** - * returns: - * 0 :: _1 == _2 - * -1 :: _1 < _2 - * +1 :: _1 > _2 - * */ -int string_compare ( char* _1, char* _2 ); -- cgit v1.2.3