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