Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_trees.c

Nilorea Library tree test.

Nilorea Library tree test

Author
Castagnier Mickael
Version
1.0
Date
03/01/2025
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include "nilorea/n_log.h"
#include "nilorea/n_str.h"
#ifndef __windows__
#include <sys/wait.h>
#endif
void usage(void) {
fprintf(stderr,
" -v version\n"
" -V log level: LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_DEBUG\n"
" -h help\n");
}
void process_args(int argc, char** argv) {
int getoptret = 0,
log_level = LOG_DEBUG; /* default log level */
/* Arguments optionnels */
/* -v version
* -V log level
* -h help
*/
while ((getoptret = getopt(argc, argv, "hvV:")) != EOF) {
switch (getoptret) {
case 'v':
fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
exit(1);
case 'V':
if (!strncmp("LOG_NULL", optarg, 5)) {
} else {
if (!strncmp("LOG_NOTICE", optarg, 6)) {
} else {
if (!strncmp("LOG_INFO", optarg, 7)) {
} else {
if (!strncmp("LOG_ERR", optarg, 5)) {
} else {
if (!strncmp("LOG_DEBUG", optarg, 5)) {
} else {
fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
exit(-1);
}
}
}
}
}
break;
default:
case '?': {
if (optopt == 'V') {
fprintf(stderr, "\n Missing log level\n");
} else if (optopt == 'p') {
fprintf(stderr, "\n Missing port\n");
} else if (optopt != 's') {
fprintf(stderr, "\n Unknow missing option %c\n", optopt);
}
usage();
exit(1);
}
case 'h': {
usage();
exit(1);
}
}
}
} /* void process_args( ... ) */
int main(int argc, char** argv) {
/* processing args and set log_level */
process_args(argc, argv);
// Create a quad tree with int coordinates
// Example data pointers (could be any type)
int data1 = 100;
int data2 = 200;
// Insert points into the quad tree
COORD_VALUE x1, y1, x2, y2;
x1.i = 5;
y1.i = 5;
x2.i = 9;
y2.i = 7;
insert(qt, &(qt->root), x1, y1, &data1);
insert(qt, &(qt->root), x2, y2, &data2);
// Search for a point in the quad tree
QUADTREE_NODE* result = search(qt, qt->root, x1, y1);
if (result && result->data_ptr) {
printf("Found node at (");
qt->print(result->x);
printf(", ");
qt->print(result->y);
printf(") with data: %d\n", *(int*)result->data_ptr);
} else {
printf("Node not found or has no data.\n");
}
// Free the quad tree
free(qt);
exit(0);
}
void usage(void)
Definition ex_common.c:22
void process_args(int argc, char **argv)
Definition ex_common.c:29
int main(void)
int getoptret
Definition ex_fluid.c:42
int log_level
Definition ex_fluid.c:43
#define LOG_DEBUG
debug-level messages
Definition n_log.h:65
#define LOG_ERR
error conditions
Definition n_log.h:57
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:104
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:61
#define LOG_NULL
no log output
Definition n_log.h:27
#define LOG_INFO
informational
Definition n_log.h:63
print_func print
pointer to print function
Definition n_trees.h:152
COORD_VALUE y
Y coordinate.
Definition n_trees.h:130
QUADTREE_NODE * root
tree list first node
Definition n_trees.h:154
void * data_ptr
Pointer to data, can be NULL.
Definition n_trees.h:134
COORD_VALUE x
X coordinate.
Definition n_trees.h:128
void free_quadtree(QUADTREE_NODE *root)
Function to free the quad tree.
Definition n_trees.c:313
QUADTREE_NODE * search(QUADTREE *qt, QUADTREE_NODE *root, COORD_VALUE x, COORD_VALUE y)
Function to search for a point in the quad tree.
Definition n_trees.c:291
void insert(QUADTREE *qt, QUADTREE_NODE **root, COORD_VALUE x, COORD_VALUE y, void *data_ptr)
Function to insert a point into the quad tree.
Definition n_trees.c:266
QUADTREE * create_quadtree(int coord_type)
Function to create a new quad tree.
Definition n_trees.c:207
@ COORD_INT
Definition n_trees.h:88
structure of a quad tree
Definition n_trees.h:146
structure of a quad tree node
Definition n_trees.h:126
Union to store the coordinate values.
Definition n_trees.h:94
Common headers and low-level functions & define.
Generic log system.
N_STR and string function declaration.
trees module headers