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{
23 fprintf( stderr, " -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{
30 int getoptret = 0,
31 log_level = LOG_DEBUG ; /* default log level */
32
33 /* Arguments optionnels */
34 /* -v version
35 * -V log level
36 * -h help
37 */
38 while( ( getoptret = getopt( argc, argv, "hvV:" ) ) != EOF)
39 {
40 switch( getoptret )
41 {
42 case 'v' :
43 fprintf( stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__ );
44 exit( 1 );
45 case 'V' :
46 if( !strncmp( "LOG_NULL", optarg, 5 ) )
47 {
48 log_level = LOG_NULL ;
49 }
50 else
51 {
52 if( !strncmp( "LOG_NOTICE", optarg, 6 ) )
53 {
54 log_level = LOG_NOTICE;
55 }
56 else
57 {
58 if( !strncmp( "LOG_INFO", optarg, 7 ) )
59 {
60 log_level = LOG_INFO;
61 }
62 else
63 {
64 if( !strncmp( "LOG_ERR", optarg, 5 ) )
65 {
66 log_level = LOG_ERR;
67 }
68 else
69 {
70 if( !strncmp( "LOG_DEBUG", optarg, 5 ) )
71 {
72 log_level = LOG_DEBUG;
73 }
74 else
75 {
76 fprintf( stderr, "%s n'est pas un niveau de log valide.\n", optarg );
77 exit( -1 );
78 }
79 }
80 }
81 }
82 }
83 break;
84 default :
85 case '?' :
86 {
87 if( optopt == 'V' )
88 {
89 fprintf( stderr, "\n Missing log level\n" );
90 }
91 else if( optopt == 'p' )
92 {
93 fprintf( stderr, "\n Missing port\n" );
94 }
95 else if( optopt != 's' )
96 {
97 fprintf( stderr, "\n Unknow missing option %c\n", optopt );
98 }
99 usage();
100 exit( 1 );
101 }
102 case 'h' :
103 {
104 usage();
105 exit( 1 );
106 }
107 }
108 }
109 set_log_level( log_level );
110} /* void process_args( ... ) */
111
112
113int main(int argc, char **argv)
114{
115
116 /* processing args and set log_level */
117 process_args( argc, argv );
118
119 // Create a quad tree with int coordinates
120 QUADTREE *qt = create_quadtree(COORD_INT);
121
122 // Example data pointers (could be any type)
123 int data1 = 100;
124 int data2 = 200;
125
126 // Insert points into the quad tree
127 COORD_VALUE x1, y1, x2, y2;
128 x1.i = 5; y1.i = 5;
129 x2.i = 9; y2.i = 7;
130
131 insert(qt, &(qt->root), x1, y1, &data1);
132 insert(qt, &(qt->root), x2, y2, &data2);
133
134 // Search for a point in the quad tree
135 QUADTREE_NODE *result = search(qt, qt->root, x1, y1);
136 if (result && result->data_ptr) {
137 printf("Found node at (");
138 qt->print(result->x);
139 printf(", ");
140 qt->print(result->y);
141 printf(") with data: %d\n", *(int*)result->data_ptr);
142 } else {
143 printf("Node not found or has no data.\n");
144 }
145
146 // Free the quad tree
147 free_quadtree(qt->root);
148 free(qt);
149
150 exit( 0 );
151}
#define LOG_DEBUG
debug-level messages
Definition n_log.h:66
#define LOG_ERR
error conditions
Definition n_log.h:58
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:97
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:62
#define LOG_NULL
no log output
Definition n_log.h:27
#define LOG_INFO
informational
Definition n_log.h:64
print_func print
pointer to print function
Definition n_trees.h:157
COORD_VALUE y
Y coordinate.
Definition n_trees.h:135
QUADTREE_NODE * root
tree list first node
Definition n_trees.h:159
void * data_ptr
Pointer to data, can be NULL.
Definition n_trees.h:139
COORD_VALUE x
X coordinate.
Definition n_trees.h:133
void free_quadtree(QUADTREE_NODE *root)
Function to free the quad tree.
Definition n_trees.c:304
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:282
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:257
QUADTREE * create_quadtree(int coord_type)
Function to create a new quad tree.
Definition n_trees.c:207
structure of a quad tree
Definition n_trees.h:151
structure of a quad tree node
Definition n_trees.h:131
Union to store the coordinate values.
Definition n_trees.h:99
Common headers and low-level hugly functions & define.
Generic log system.
N_STR and string function declaration.
trees module headers