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 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
28#include <stdio.h>
29#include <errno.h>
30#include <string.h>
31#include <sys/types.h>
32
33#include "nilorea/n_common.h"
34#include "nilorea/n_log.h"
35#include "nilorea/n_str.h"
36#include "nilorea/n_stack.h"
37
38void usage(void) {
39 fprintf(stderr,
40 " -v version\n"
41 " -V log level: LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_DEBUG\n"
42 " -h help\n");
43}
44
45void process_args(int argc, char** argv) {
46 int getoptret = 0,
47 log_level = LOG_DEBUG; /* default log level */
48
49 /* Arguments optionnels */
50 /* -v version
51 * -V log level
52 * -h help
53 */
54 while ((getoptret = getopt(argc, argv, "hvV:")) != EOF) {
55 switch (getoptret) {
56 case 'v':
57 fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
58 exit(1);
59 case 'V':
60 if (!strcmp("LOG_NULL", optarg))
62 else if (!strcmp("LOG_NOTICE", optarg))
64 else if (!strcmp("LOG_INFO", optarg))
66 else if (!strcmp("LOG_ERR", optarg))
68 else if (!strcmp("LOG_DEBUG", optarg))
70 else {
71 fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
72 exit(-1);
73 }
74 break;
75 default:
76 case '?': {
77 if (optopt == 'V') {
78 fprintf(stderr, "\n Missing log level\n");
79 } else if (optopt == 'p') {
80 fprintf(stderr, "\n Missing port\n");
81 } else if (optopt != 's') {
82 fprintf(stderr, "\n Unknow missing option %c\n", optopt);
83 }
84 usage();
85 exit(1);
86 }
87 case 'h': {
88 usage();
89 exit(1);
90 }
91 }
92 }
94} /* void process_args( ... ) */
95
96int main(int argc, char** argv) {
98
99 /* processing args and set log_level */
100 process_args(argc, argv);
101
102 STACK* stack = new_stack(16);
103 n_log(LOG_INFO, "created stack of 16 elements at %p", stack);
104
105 for (int it = 0; it < 20; it++) {
106 int32_t nb = rand() % 10;
107 bool btest = rand() % 2;
108
109 stack_push(stack, nb);
110 stack_push(stack, btest);
111 }
112
113 for (int it = 0; it < 20; it++) {
114 STACK_ITEM* item = NULL;
115 item = stack_peek(stack, stack->tail);
116 if (item) {
117 uint8_t status = STACK_IS_UNDEFINED;
118 switch (item->v_type) {
119 case STACK_ITEM_BOOL: {
120 bool bval = stack_pop_b(stack, &status);
121 n_log(LOG_INFO, "got bool: %d", bval);
122 status = STACK_ITEM_OK;
123 } break;
124 case STACK_ITEM_INT32: {
125 int32_t val = stack_pop_i32(stack, &status);
126 n_log(LOG_INFO, "got int32_t: %d", val);
127 status = STACK_ITEM_OK;
128 } break;
129 default:
130 n_log(LOG_ERR, "uknown type %d", item->v_type);
131 break;
132 }
133 if (status != STACK_ITEM_OK) {
134 n_log(LOG_ERR, "error popping value ! status: %d", status);
135 }
136 }
137 }
138
139 delete_stack(&stack);
140
141 /* test additional type variants and stack_is_full, stack_is_empty */
142 stack = new_stack(8);
143
144 n_log(LOG_INFO, "stack_is_empty on new stack: %d", stack_is_empty(stack));
145
146 /* push various types */
147 char cval = 'A';
148 stack_push(stack, cval);
149 n_log(LOG_INFO, "pushed char: %c", cval);
150
151 double dval = 3.14159;
152 stack_push(stack, dval);
153 n_log(LOG_INFO, "pushed double: %f", dval);
154
155 float fval = 2.71f;
156 stack_push(stack, fval);
157 n_log(LOG_INFO, "pushed float: %f", fval);
158
159 uint8_t u8val = 255;
160 stack_push(stack, u8val);
161 n_log(LOG_INFO, "pushed uint8: %u", u8val);
162
163 int8_t i8val = -42;
164 stack_push(stack, i8val);
165 n_log(LOG_INFO, "pushed int8: %d", i8val);
166
167 uint32_t u32val = 123456;
168 stack_push(stack, u32val);
169 n_log(LOG_INFO, "pushed uint32: %u", u32val);
170
171 int data_value = 999;
172 void* pval = &data_value;
173 stack_push(stack, pval);
174 n_log(LOG_INFO, "pushed pointer: %p", pval);
175
176 int32_t i32val = -789;
177 stack_push(stack, i32val);
178 n_log(LOG_INFO, "pushed int32: %d", i32val);
179
180 n_log(LOG_INFO, "stack_is_full: %d", stack_is_full(stack));
181
182 /* pop them back in reverse order */
183 uint8_t pop_status = STACK_IS_UNDEFINED;
184 STACK_ITEM* peek_item = NULL;
185 while ((peek_item = stack_peek(stack, stack->tail)) != NULL) {
186 pop_status = STACK_IS_UNDEFINED;
187 switch (peek_item->v_type) {
188 case STACK_ITEM_CHAR: {
189 char cv = stack_pop_c(stack, &pop_status);
190 n_log(LOG_INFO, "popped char: %c", cv);
191 } break;
192 case STACK_ITEM_DOUBLE: {
193 double dv = stack_pop_d(stack, &pop_status);
194 n_log(LOG_INFO, "popped double: %f", dv);
195 } break;
196 case STACK_ITEM_FLOAT: {
197 float fv = stack_pop_f(stack, &pop_status);
198 n_log(LOG_INFO, "popped float: %f", fv);
199 } break;
200 case STACK_ITEM_UINT8: {
201 uint8_t uv = stack_pop_ui8(stack, &pop_status);
202 n_log(LOG_INFO, "popped uint8: %u", uv);
203 } break;
204 case STACK_ITEM_INT8: {
205 int8_t iv = stack_pop_i8(stack, &pop_status);
206 n_log(LOG_INFO, "popped int8: %d", iv);
207 } break;
208 case STACK_ITEM_UINT32: {
209 uint32_t uv = stack_pop_ui32(stack, &pop_status);
210 n_log(LOG_INFO, "popped uint32: %u", uv);
211 } break;
212 case STACK_ITEM_INT32: {
213 int32_t iv = stack_pop_i32(stack, &pop_status);
214 n_log(LOG_INFO, "popped int32: %d", iv);
215 } break;
216 case STACK_ITEM_PTR: {
217 void* pv = stack_pop_p(stack, &pop_status);
218 n_log(LOG_INFO, "popped pointer: %p (value=%d)", pv, pv ? *(int*)pv : 0);
219 } break;
220 default:
221 n_log(LOG_ERR, "unknown type %d", peek_item->v_type);
222 stack_pop_i32(stack, &pop_status);
223 break;
224 }
225 }
226
227 n_log(LOG_INFO, "stack_is_empty after pops: %d", stack_is_empty(stack));
228 delete_stack(&stack);
229
230 exit(0);
231}
static void usage(void)
int main(void)
void process_args(int argc, char **argv)
Definition ex_common.c:48
int getoptret
Definition ex_fluid.c:59
int log_level
Definition ex_fluid.c:60
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:89
#define LOG_DEBUG
debug-level messages
Definition n_log.h:84
#define LOG_ERR
error conditions
Definition n_log.h:76
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:121
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:80
#define LOG_NULL
no log output
Definition n_log.h:46
#define LOG_INFO
informational
Definition n_log.h:82
size_t tail
position of tail
Definition n_stack.h:126
uint8_t v_type
type of the item
Definition n_stack.h:112
double stack_pop_d(STACK *stack, uint8_t *status)
helper to pop a double
Definition n_stack.c:547
bool stack_pop_b(STACK *stack, uint8_t *status)
helper to pop a bool
Definition n_stack.c:204
#define STACK_ITEM_UINT32
v_type value for a uint32_t
Definition n_stack.h:50
STACK_ITEM * stack_peek(STACK *stack, size_t position)
peek in the stack without removing the stack item
Definition n_stack.c:93
int8_t stack_pop_i8(STACK *stack, uint8_t *status)
helper to pop a int8_t
Definition n_stack.c:351
#define STACK_ITEM_INT8
v_type value for a int8_t
Definition n_stack.h:48
char stack_pop_c(STACK *stack, uint8_t *status)
helper to pop a char
Definition n_stack.c:253
bool stack_is_empty(const STACK *stack)
test if the stack is empty
Definition n_stack.c:82
float stack_pop_f(STACK *stack, uint8_t *status)
helper to pop a float
Definition n_stack.c:498
#define STACK_ITEM_PTR
v_type value for a void *pointer
Definition n_stack.h:62
uint8_t stack_pop_ui8(STACK *stack, uint8_t *status)
helper to pop a uint8_t
Definition n_stack.c:302
bool stack_is_full(const STACK *stack)
test if the stack is full
Definition n_stack.c:72
#define STACK_ITEM_INT32
v_type value for a int32_t
Definition n_stack.h:52
#define STACK_ITEM_CHAR
v_type value for a char
Definition n_stack.h:44
#define STACK_ITEM_OK
code for a successfully retrieved item
Definition n_stack.h:73
uint32_t stack_pop_ui32(STACK *stack, uint8_t *status)
helper to pop a uint32_t
Definition n_stack.c:400
#define STACK_ITEM_BOOL
v_type value for a bool
Definition n_stack.h:42
#define STACK_IS_UNDEFINED
code for a NULL stack state
Definition n_stack.h:69
#define STACK_ITEM_UINT8
v_type value for a uint8_t
Definition n_stack.h:46
int32_t stack_pop_i32(STACK *stack, uint8_t *status)
helper to pop a int32_t
Definition n_stack.c:449
#define stack_push(__STACK, __VAL)
Definition n_stack.h:182
#define STACK_ITEM_DOUBLE
v_type value for a double
Definition n_stack.h:60
bool delete_stack(STACK **stack)
delete a STACK *stack
Definition n_stack.c:59
#define STACK_ITEM_FLOAT
v_type value for a float
Definition n_stack.h:58
STACK * new_stack(size_t size)
allocate a new STACK
Definition n_stack.c:35
void * stack_pop_p(STACK *stack, uint8_t *status)
helper to pop a pointer
Definition n_stack.c:598
STACK structure.
Definition n_stack.h:118
structure of a STACK item
Definition n_stack.h:104
Common headers and low-level functions & define.
Generic log system.
Stack header definitions.
N_STR and string function declaration.