Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_trees.c
1
7#include <stdio.h>
8#include <errno.h>
9#include <string.h>
10#include <sys/types.h>
11
12#include "nilorea/n_common.h"
13#include "nilorea/n_log.h"
14#include "nilorea/n_str.h"
15#include "nilorea/n_trees.h"
16
17#ifndef __windows__
18#include <sys/wait.h>
19#endif
20
21void usage(void) {
22 fprintf(stderr,
23 " -v version\n"
24 " -V log level: LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_DEBUG\n"
25 " -h help\n");
26}
27
28void process_args(int argc, char** argv) {
29 int getoptret = 0,
30 log_level = LOG_DEBUG; /* default log level */
31
32 /* Arguments optionnels */
33 /* -v version
34 * -V log level
35 * -h help
36 */
37 while ((getoptret = getopt(argc, argv, "hvV:")) != EOF) {
38 switch (getoptret) {
39 case 'v':
40 fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
41 exit(1);
42 case 'V':
43 if (!strncmp("LOG_NULL", optarg, 5)) {
44 log_level = LOG_NULL;
45 } else {
46 if (!strncmp("LOG_NOTICE", optarg, 6)) {
47 log_level = LOG_NOTICE;
48 } else {
49 if (!strncmp("LOG_INFO", optarg, 7)) {
50 log_level = LOG_INFO;
51 } else {
52 if (!strncmp("LOG_ERR", optarg, 5)) {
53 log_level = LOG_ERR;
54 } else {
55 if (!strncmp("LOG_DEBUG", optarg, 5)) {
56 log_level = LOG_DEBUG;
57 } else {
58 fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
59 exit(-1);
60 }
61 }
62 }
63 }
64 }
65 break;
66 default:
67 case '?': {
68 if (optopt == 'V') {
69 fprintf(stderr, "\n Missing log level\n");
70 } else if (optopt == 'p') {
71 fprintf(stderr, "\n Missing port\n");
72 } else if (optopt != 's') {
73 fprintf(stderr, "\n Unknow missing option %c\n", optopt);
74 }
75 usage();
76 exit(1);
77 }
78 case 'h': {
79 usage();
80 exit(1);
81 }
82 }
83 }
84 set_log_level(log_level);
85} /* void process_args( ... ) */
86
87int main(int argc, char** argv) {
88 /* processing args and set log_level */
89 process_args(argc, argv);
90
91 // Create a quad tree with int coordinates
92 QUADTREE* qt = create_quadtree(COORD_INT);
93
94 // Example data pointers (could be any type)
95 int data1 = 100;
96 int data2 = 200;
97
98 // Insert points into the quad tree
99 COORD_VALUE x1, y1, x2, y2;
100 x1.i = 5;
101 y1.i = 5;
102 x2.i = 9;
103 y2.i = 7;
104
105 insert(qt, &(qt->root), x1, y1, &data1);
106 insert(qt, &(qt->root), x2, y2, &data2);
107
108 // Search for a point in the quad tree
109 QUADTREE_NODE* result = search(qt, qt->root, x1, y1);
110 if (result && result->data_ptr) {
111 printf("Found node at (");
112 qt->print(result->x);
113 printf(", ");
114 qt->print(result->y);
115 printf(") with data: %d\n", *(int*)result->data_ptr);
116 } else {
117 printf("Node not found or has no data.\n");
118 }
119
120 // Free the quad tree
121 free_quadtree(qt->root);
122 free(qt);
123
124 exit(0);
125}
#define LOG_DEBUG
debug-level messages
Definition n_log.h:64
#define LOG_ERR
error conditions
Definition n_log.h:56
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:91
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:60
#define LOG_NULL
no log output
Definition n_log.h:26
#define LOG_INFO
informational
Definition n_log.h:62
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:312
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:290
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:265
QUADTREE * create_quadtree(int coord_type)
Function to create a new quad tree.
Definition n_trees.c:206
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