Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_stack.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
27#include <stdio.h>
28#include <errno.h>
29#include <string.h>
30#include <sys/types.h>
31
32#include "nilorea/n_common.h"
33#include "nilorea/n_log.h"
34#include "nilorea/n_str.h"
35#include "nilorea/n_stack.h"
36
37void usage(void) {
38 fprintf(stderr,
39 " -v version\n"
40 " -V log level: LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_DEBUG\n"
41 " -h help\n");
42}
43
44void process_args(int argc, char** argv) {
45 int getoptret = 0,
46 log_level = LOG_DEBUG; /* default log level */
47
48 /* Arguments optionnels */
49 /* -v version
50 * -V log level
51 * -h help
52 */
53 while ((getoptret = getopt(argc, argv, "hvV:")) != EOF) {
54 switch (getoptret) {
55 case 'v':
56 fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
57 exit(1);
58 case 'V':
59 if (!strcmp("LOG_NULL", optarg))
61 else if (!strcmp("LOG_NOTICE", optarg))
63 else if (!strcmp("LOG_INFO", optarg))
65 else if (!strcmp("LOG_ERR", optarg))
67 else if (!strcmp("LOG_DEBUG", optarg))
69 else {
70 fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
71 exit(-1);
72 }
73 break;
74 default:
75 case '?': {
76 if (optopt == 'V') {
77 fprintf(stderr, "\n Missing log level\n");
78 } else if (optopt == 'p') {
79 fprintf(stderr, "\n Missing port\n");
80 } else if (optopt != 's') {
81 fprintf(stderr, "\n Unknow missing option %c\n", optopt);
82 }
83 usage();
84 exit(1);
85 }
86 case 'h': {
87 usage();
88 exit(1);
89 }
90 }
91 }
93} /* void process_args( ... ) */
94
95int main(int argc, char** argv) {
97
98 /* processing args and set log_level */
99 process_args(argc, argv);
100
101 STACK* stack = new_stack(16);
102 n_log(LOG_INFO, "created stack of 16 elements at %p", stack);
103
104 for (int it = 0; it < 20; it++) {
105 int32_t nb = rand() % 10;
106 bool btest = rand() % 2;
107
108 stack_push(stack, nb);
109 stack_push(stack, btest);
110 }
111
112 for (int it = 0; it < 20; it++) {
113 STACK_ITEM* item = NULL;
114 item = stack_peek(stack, stack->tail);
115 if (item) {
116 uint8_t status = STACK_IS_UNDEFINED;
117 switch (item->v_type) {
118 case STACK_ITEM_BOOL: {
119 bool bval = stack_pop_b(stack, &status);
120 n_log(LOG_INFO, "got bool: %d", bval);
121 status = STACK_ITEM_OK;
122 } break;
123 case STACK_ITEM_INT32: {
124 int32_t val = stack_pop_i32(stack, &status);
125 n_log(LOG_INFO, "got int32_t: %d", val);
126 status = STACK_ITEM_OK;
127 } break;
128 default:
129 n_log(LOG_ERR, "uknown type %d", item->v_type);
130 break;
131 }
132 if (status != STACK_ITEM_OK) {
133 n_log(LOG_ERR, "error popping value ! status: %d", status);
134 }
135 }
136 }
137
138 delete_stack(&stack);
139
140 /* test additional type variants and stack_is_full, stack_is_empty */
141 stack = new_stack(8);
142
143 n_log(LOG_INFO, "stack_is_empty on new stack: %d", stack_is_empty(stack));
144
145 /* push various types */
146 char cval = 'A';
147 stack_push(stack, cval);
148 n_log(LOG_INFO, "pushed char: %c", cval);
149
150 double dval = 3.14159;
151 stack_push(stack, dval);
152 n_log(LOG_INFO, "pushed double: %f", dval);
153
154 float fval = 2.71f;
155 stack_push(stack, fval);
156 n_log(LOG_INFO, "pushed float: %f", fval);
157
158 uint8_t u8val = 255;
159 stack_push(stack, u8val);
160 n_log(LOG_INFO, "pushed uint8: %u", u8val);
161
162 int8_t i8val = -42;
163 stack_push(stack, i8val);
164 n_log(LOG_INFO, "pushed int8: %d", i8val);
165
166 uint32_t u32val = 123456;
167 stack_push(stack, u32val);
168 n_log(LOG_INFO, "pushed uint32: %u", u32val);
169
170 int data_value = 999;
171 void* pval = &data_value;
172 stack_push(stack, pval);
173 n_log(LOG_INFO, "pushed pointer: %p", pval);
174
175 int32_t i32val = -789;
176 stack_push(stack, i32val);
177 n_log(LOG_INFO, "pushed int32: %d", i32val);
178
179 n_log(LOG_INFO, "stack_is_full: %d", stack_is_full(stack));
180
181 /* pop them back in reverse order */
182 uint8_t pop_status = STACK_IS_UNDEFINED;
183 STACK_ITEM* peek_item = NULL;
184 while ((peek_item = stack_peek(stack, stack->tail)) != NULL) {
185 pop_status = STACK_IS_UNDEFINED;
186 switch (peek_item->v_type) {
187 case STACK_ITEM_CHAR: {
188 char cv = stack_pop_c(stack, &pop_status);
189 n_log(LOG_INFO, "popped char: %c", cv);
190 } break;
191 case STACK_ITEM_DOUBLE: {
192 double dv = stack_pop_d(stack, &pop_status);
193 n_log(LOG_INFO, "popped double: %f", dv);
194 } break;
195 case STACK_ITEM_FLOAT: {
196 float fv = stack_pop_f(stack, &pop_status);
197 n_log(LOG_INFO, "popped float: %f", fv);
198 } break;
199 case STACK_ITEM_UINT8: {
200 uint8_t uv = stack_pop_ui8(stack, &pop_status);
201 n_log(LOG_INFO, "popped uint8: %u", uv);
202 } break;
203 case STACK_ITEM_INT8: {
204 int8_t iv = stack_pop_i8(stack, &pop_status);
205 n_log(LOG_INFO, "popped int8: %d", iv);
206 } break;
207 case STACK_ITEM_UINT32: {
208 uint32_t uv = stack_pop_ui32(stack, &pop_status);
209 n_log(LOG_INFO, "popped uint32: %u", uv);
210 } break;
211 case STACK_ITEM_INT32: {
212 int32_t iv = stack_pop_i32(stack, &pop_status);
213 n_log(LOG_INFO, "popped int32: %d", iv);
214 } break;
215 case STACK_ITEM_PTR: {
216 void* pv = stack_pop_p(stack, &pop_status);
217 n_log(LOG_INFO, "popped pointer: %p (value=%d)", pv, pv ? *(int*)pv : 0);
218 } break;
219 default:
220 n_log(LOG_ERR, "unknown type %d", peek_item->v_type);
221 stack_pop_i32(stack, &pop_status);
222 break;
223 }
224 }
225
226 n_log(LOG_INFO, "stack_is_empty after pops: %d", stack_is_empty(stack));
227 delete_stack(&stack);
228
229 exit(0);
230}
static void usage(void)
void process_args(int argc, char **argv)
Definition ex_common.c:47
int main(void)
int getoptret
Definition ex_fluid.c:60
int log_level
Definition ex_fluid.c:61
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_DEBUG
debug-level messages
Definition n_log.h:83
#define LOG_ERR
error conditions
Definition n_log.h:75
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:120
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:79
#define LOG_NULL
no log output
Definition n_log.h:45
#define LOG_INFO
informational
Definition n_log.h:81
size_t tail
position of tail
Definition n_stack.h:125
uint8_t v_type
type of the item
Definition n_stack.h:111
double stack_pop_d(STACK *stack, uint8_t *status)
helper to pop a double
Definition n_stack.c:546
bool stack_pop_b(STACK *stack, uint8_t *status)
helper to pop a bool
Definition n_stack.c:203
#define STACK_ITEM_UINT32
v_type value for a uint32_t
Definition n_stack.h:49
STACK_ITEM * stack_peek(STACK *stack, size_t position)
peek in the stack without removing the stack item
Definition n_stack.c:92
int8_t stack_pop_i8(STACK *stack, uint8_t *status)
helper to pop a int8_t
Definition n_stack.c:350
#define STACK_ITEM_INT8
v_type value for a int8_t
Definition n_stack.h:47
char stack_pop_c(STACK *stack, uint8_t *status)
helper to pop a char
Definition n_stack.c:252
bool stack_is_empty(const STACK *stack)
test if the stack is empty
Definition n_stack.c:81
float stack_pop_f(STACK *stack, uint8_t *status)
helper to pop a float
Definition n_stack.c:497
#define STACK_ITEM_PTR
v_type value for a void *pointer
Definition n_stack.h:61
uint8_t stack_pop_ui8(STACK *stack, uint8_t *status)
helper to pop a uint8_t
Definition n_stack.c:301
bool stack_is_full(const STACK *stack)
test if the stack is full
Definition n_stack.c:71
#define STACK_ITEM_INT32
v_type value for a int32_t
Definition n_stack.h:51
#define STACK_ITEM_CHAR
v_type value for a char
Definition n_stack.h:43
#define STACK_ITEM_OK
code for a successfully retrieved item
Definition n_stack.h:72
uint32_t stack_pop_ui32(STACK *stack, uint8_t *status)
helper to pop a uint32_t
Definition n_stack.c:399
#define STACK_ITEM_BOOL
v_type value for a bool
Definition n_stack.h:41
#define STACK_IS_UNDEFINED
code for a NULL stack state
Definition n_stack.h:68
#define STACK_ITEM_UINT8
v_type value for a uint8_t
Definition n_stack.h:45
int32_t stack_pop_i32(STACK *stack, uint8_t *status)
helper to pop a int32_t
Definition n_stack.c:448
#define stack_push(__STACK, __VAL)
Definition n_stack.h:181
#define STACK_ITEM_DOUBLE
v_type value for a double
Definition n_stack.h:59
bool delete_stack(STACK **stack)
delete a STACK *stack
Definition n_stack.c:58
#define STACK_ITEM_FLOAT
v_type value for a float
Definition n_stack.h:57
STACK * new_stack(size_t size)
allocate a new STACK
Definition n_stack.c:34
void * stack_pop_p(STACK *stack, uint8_t *status)
helper to pop a pointer
Definition n_stack.c:597
STACK structure.
Definition n_stack.h:117
structure of a STACK item
Definition n_stack.h:103
Common headers and low-level functions & define.
Generic log system.
Stack header definitions.
N_STR and string function declaration.