Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_stack.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_stack.h"
16
17void usage(void)
18{
19 fprintf( stderr, " -v version\n"
20 " -V log level: LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_DEBUG\n"
21 " -h help\n" );
22}
23
24void process_args( int argc, char **argv )
25{
26 int getoptret = 0,
27 log_level = LOG_DEBUG ; /* default log level */
28
29 /* Arguments optionnels */
30 /* -v version
31 * -V log level
32 * -h help
33 */
34 while( ( getoptret = getopt( argc, argv, "hvV:" ) ) != EOF)
35 {
36 switch( getoptret )
37 {
38 case 'v' :
39 fprintf( stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__ );
40 exit( 1 );
41 case 'V' :
42 if( !strncmp( "LOG_NULL", optarg, 5 ) )
43 {
44 log_level = LOG_NULL ;
45 }
46 else
47 {
48 if( !strncmp( "LOG_NOTICE", optarg, 6 ) )
49 {
50 log_level = LOG_NOTICE;
51 }
52 else
53 {
54 if( !strncmp( "LOG_INFO", optarg, 7 ) )
55 {
56 log_level = LOG_INFO;
57 }
58 else
59 {
60 if( !strncmp( "LOG_ERR", optarg, 5 ) )
61 {
62 log_level = LOG_ERR;
63 }
64 else
65 {
66 if( !strncmp( "LOG_DEBUG", optarg, 5 ) )
67 {
68 log_level = LOG_DEBUG;
69 }
70 else
71 {
72 fprintf( stderr, "%s n'est pas un niveau de log valide.\n", optarg );
73 exit( -1 );
74 }
75 }
76 }
77 }
78 }
79 break;
80 default :
81 case '?' :
82 {
83 if( optopt == 'V' )
84 {
85 fprintf( stderr, "\n Missing log level\n" );
86 }
87 else if( optopt == 'p' )
88 {
89 fprintf( stderr, "\n Missing port\n" );
90 }
91 else if( optopt != 's' )
92 {
93 fprintf( stderr, "\n Unknow missing option %c\n", optopt );
94 }
95 usage();
96 exit( 1 );
97 }
98 case 'h' :
99 {
100 usage();
101 exit( 1 );
102 }
103 }
104 }
105 set_log_level( log_level );
106} /* void process_args( ... ) */
107
108
109
110int main(int argc, char **argv)
111{
113
114 /* processing args and set log_level */
115 process_args( argc, argv );
116
117 STACK *stack = new_stack( 16 );
118 n_log( LOG_INFO , "created stack of 16 elements at %p" , stack );
119
120 for( int it = 0 ; it < 20 ; it ++ )
121 {
122 int32_t nb = rand()%10 ;
123 bool btest = rand()%1 ;
124
125 stack_push( stack , nb );
126 stack_push( stack , btest );
127 }
128
129 for( int it = 0 ; it < 20 ; it ++ )
130 {
131 STACK_ITEM *item = NULL ;
132 item = stack_peek( stack , stack -> tail );
133 if( item )
134 {
135 uint8_t status = STACK_IS_UNDEFINED ;
136 switch( item -> v_type )
137 {
138 case STACK_ITEM_BOOL:
139 {
140 bool bval = stack_pop_b( stack , &status );
141 n_log( LOG_INFO , "got bool: %d" , bval );
142 status = STACK_ITEM_OK ;
143 }
144 break;
145 case STACK_ITEM_INT32:
146 {
147 int32_t val = stack_pop_i32( stack , &status );
148 n_log( LOG_INFO , "got int32_t: %d" , val );
149 status = STACK_ITEM_OK ;
150 }
151 break;
152 default:
153 n_log( LOG_ERR , "uknown type %d" , item -> v_type );
154 break;
155 }
156 if( status != STACK_ITEM_OK )
157 {
158 n_log( LOG_ERR , "error popping value ! status: %d" , status );
159 }
160 }
161 }
162
163 delete_stack( &stack );
164
165 exit( 0 );
166}
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:74
#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
bool stack_pop_b(STACK *stack, uint8_t *status)
helper to pop a bool
Definition n_stack.c:197
STACK_ITEM * stack_peek(STACK *stack, size_t position)
peek in the stack with un stacking the stack item
Definition n_stack.c:72
#define STACK_ITEM_INT32
v_type value for a int32_t
Definition n_stack.h:34
#define STACK_ITEM_OK
code for a successfully retrieved item
Definition n_stack.h:55
#define STACK_ITEM_BOOL
v_type value for a bool
Definition n_stack.h:24
#define STACK_IS_UNDEFINED
code for a NULL stack state
Definition n_stack.h:51
int32_t stack_pop_i32(STACK *stack, uint8_t *status)
helper to pop a int32_t
Definition n_stack.c:440
#define stack_push(__STACK, __VAL)
stack push helper with automatic value selection
Definition n_stack.h:135
bool delete_stack(STACK **stack)
delete a STACK *stack
Definition n_stack.c:38
STACK * new_stack(size_t size)
allocate a new STACK
Definition n_stack.c:17
STACK structure.
Definition n_stack.h:104
structure of a STACK item
Definition n_stack.h:88
Common headers and low-level hugly functions & define.
Generic log system.
N_STR and string function declaration.