Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_stack.h
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
26#ifndef __N_STACK_HEADER
27#define __N_STACK_HEADER
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
38#include "nilorea/n_common.h"
39
41#define STACK_ITEM_BOOL 1
43#define STACK_ITEM_CHAR 2
45#define STACK_ITEM_UINT8 3
47#define STACK_ITEM_INT8 4
49#define STACK_ITEM_UINT32 5
51#define STACK_ITEM_INT32 6
53#define STACK_ITEM_UINT64 7
55#define STACK_ITEM_INT64 8
57#define STACK_ITEM_FLOAT 9
59#define STACK_ITEM_DOUBLE 10
61#define STACK_ITEM_PTR 11
62
64#define STACK_IS_FULL 0
66#define STACK_IS_EMPTY 1
68#define STACK_IS_UNDEFINED 2
70#define STACK_ITEM_WRONG_TYPE 3
72#define STACK_ITEM_OK 4
73
77 bool b;
79 char c;
81 uint8_t ui8;
83 int8_t i8;
85 uint32_t ui32;
87 int32_t i32;
88#ifdef ENV_64BITS
90 uint64_t ui64;
92 int64_t i64;
93#endif
95 float f;
97 double d;
99 void* p;
100};
101
103typedef struct STACK_ITEM {
105 bool is_set;
111 uint8_t v_type;
113 uint16_t p_type;
114} STACK_ITEM;
115
117typedef struct STACK {
121 size_t size;
123 size_t head;
125 size_t tail;
127 size_t nb_items;
128} STACK;
129
130/* stack_push_p_default is declared below with the other push functions.
131 * It wraps stack_push_p with a default p_type of 0 so the _Generic macro
132 * can dispatch void* with only 2 arguments.
133 * Use stack_push_p() directly when a custom p_type is needed. */
134
135#if defined(__sun) && defined(__SVR4)
136/* Solaris: avoid _Generic conflicts due to type compatibility */
137#ifdef ENV_64BITS
138#define stack_push(__STACK, __VAL) \
139 _Generic((__VAL), \
140 bool: stack_push_b, \
141 char: stack_push_c, \
142 signed char: stack_push_i8, /* replaces int8_t */ \
143 unsigned char: stack_push_ui8, /* replaces uint8_t */ \
144 uint32_t: stack_push_ui32, \
145 int32_t: stack_push_i32, \
146 uint64_t: stack_push_ui64, \
147 int64_t: stack_push_i64, \
148 float: stack_push_f, \
149 double: stack_push_d, \
150 void*: stack_push_p_default)(__STACK, __VAL)
151#else
152#define stack_push(__STACK, __VAL) \
153 _Generic((__VAL), \
154 bool: stack_push_b, \
155 char: stack_push_c, \
156 signed char: stack_push_i8, \
157 unsigned char: stack_push_ui8, \
158 uint32_t: stack_push_ui32, \
159 int32_t: stack_push_i32, \
160 float: stack_push_f, \
161 double: stack_push_d, \
162 void*: stack_push_p_default)(__STACK, __VAL)
163#endif
164#else
165/* Default version for Linux, Windows, etc. */
166#ifdef ENV_64BITS
167#define stack_push(__STACK, __VAL) \
168 _Generic((__VAL), \
169 bool: stack_push_b, \
170 char: stack_push_c, \
171 uint8_t: stack_push_ui8, \
172 int8_t: stack_push_i8, \
173 uint32_t: stack_push_ui32, \
174 int32_t: stack_push_i32, \
175 uint64_t: stack_push_ui64, \
176 int64_t: stack_push_i64, \
177 float: stack_push_f, \
178 double: stack_push_d, \
179 void*: stack_push_p_default)(__STACK, __VAL)
180#else
181#define stack_push(__STACK, __VAL) \
182 _Generic((__VAL), \
183 bool: stack_push_b, \
184 char: stack_push_c, \
185 uint8_t: stack_push_ui8, \
186 int8_t: stack_push_i8, \
187 uint32_t: stack_push_ui32, \
188 int32_t: stack_push_i32, \
189 float: stack_push_f, \
190 double: stack_push_d, \
191 void*: stack_push_p_default)(__STACK, __VAL)
192#endif
193#endif
194
196STACK* new_stack(size_t nb_items);
198bool delete_stack(STACK** stack);
200bool stack_is_full(const STACK* stack);
202bool stack_is_empty(const STACK* stack);
204STACK_ITEM* stack_peek(STACK* stack, size_t position);
205
207bool stack_push_b(STACK* stack, bool b);
209bool stack_push_c(STACK* stack, char c);
211bool stack_push_ui8(STACK* stack, uint8_t ui8);
213bool stack_push_i8(STACK* stack, int8_t i8);
215bool stack_push_ui32(STACK* stack, uint32_t ui32);
217bool stack_push_i32(STACK* stack, int32_t i32);
219bool stack_push_f(STACK* stack, float f);
221bool stack_push_d(STACK* stack, double d);
223bool stack_push_p(STACK* stack, void* p, uint16_t p_type);
225bool stack_push_p_default(STACK* stack, void* p);
226
228bool stack_pop_b(STACK* stack, uint8_t* status);
230char stack_pop_c(STACK* stack, uint8_t* status);
232uint8_t stack_pop_ui8(STACK* stack, uint8_t* status);
234int8_t stack_pop_i8(STACK* stack, uint8_t* status);
236uint32_t stack_pop_ui32(STACK* stack, uint8_t* status);
238int32_t stack_pop_i32(STACK* stack, uint8_t* status);
240float stack_pop_f(STACK* stack, uint8_t* status);
242double stack_pop_d(STACK* stack, uint8_t* status);
244void* stack_pop_p(STACK* stack, uint8_t* status);
245
246#ifdef ENV_64BITS
248bool stack_push_ui64(STACK* stack, uint64_t ui64_t);
250bool stack_push_i64(STACK* stack, int64_t i64);
252uint64_t stack_pop_ui64(STACK* stack, uint8_t* status);
254int64_t stack_pop_i64(STACK* stack, uint8_t* status);
255#endif
256
261#ifdef __cplusplus
262}
263#endif
264
265#endif
size_t tail
position of tail
Definition n_stack.h:125
bool is_empty
is item empty ?
Definition n_stack.h:107
size_t head
position of head
Definition n_stack.h:123
size_t nb_items
number of item inside stack
Definition n_stack.h:127
STACK_ITEM * stack_array
STACK_ITEM array.
Definition n_stack.h:119
int8_t i8
int 8
Definition n_stack.h:83
uint16_t p_type
if v_type is STACK_ITEM_PTR, user defined pointer type
Definition n_stack.h:113
union STACK_DATA data
union of different types
Definition n_stack.h:109
void * p
pointer
Definition n_stack.h:99
double d
double
Definition n_stack.h:97
size_t size
Size of array.
Definition n_stack.h:121
float f
float
Definition n_stack.h:95
char c
single character
Definition n_stack.h:79
int32_t i32
int 32
Definition n_stack.h:87
uint8_t v_type
type of the item
Definition n_stack.h:111
bool b
boolean
Definition n_stack.h:77
bool is_set
is item set ?
Definition n_stack.h:105
uint32_t ui32
unsigned int 32
Definition n_stack.h:85
uint8_t ui8
unsigned int 8
Definition n_stack.h:81
bool stack_push_f(STACK *stack, float f)
push a float onto the stack
Definition n_stack.c:477
double stack_pop_d(STACK *stack, uint8_t *status)
pop a double from the stack
Definition n_stack.c:546
bool stack_pop_b(STACK *stack, uint8_t *status)
pop a bool from the stack
Definition n_stack.c:203
bool stack_push_d(STACK *stack, double d)
push a double onto the stack
Definition n_stack.c:526
bool stack_push_ui8(STACK *stack, uint8_t ui8)
push a uint8_t onto the stack
Definition n_stack.c:281
STACK_ITEM * stack_peek(STACK *stack, size_t position)
peek at an item at given position without removing it
Definition n_stack.c:92
bool stack_push_ui32(STACK *stack, uint32_t ui32)
push a uint32_t onto the stack
Definition n_stack.c:379
int8_t stack_pop_i8(STACK *stack, uint8_t *status)
pop an int8_t from the stack
Definition n_stack.c:350
char stack_pop_c(STACK *stack, uint8_t *status)
pop a char from the stack
Definition n_stack.c:252
bool stack_is_empty(const STACK *stack)
check if the stack is empty
Definition n_stack.c:81
float stack_pop_f(STACK *stack, uint8_t *status)
pop a float from the stack
Definition n_stack.c:497
uint8_t stack_pop_ui8(STACK *stack, uint8_t *status)
pop a uint8_t from the stack
Definition n_stack.c:301
bool stack_push_c(STACK *stack, char c)
push a char onto the stack
Definition n_stack.c:232
bool stack_is_full(const STACK *stack)
check if the stack is full
Definition n_stack.c:71
uint32_t stack_pop_ui32(STACK *stack, uint8_t *status)
pop a uint32_t from the stack
Definition n_stack.c:399
bool stack_push_b(STACK *stack, bool b)
push a bool onto the stack
Definition n_stack.c:185
bool stack_push_i32(STACK *stack, int32_t i32)
push an int32_t onto the stack
Definition n_stack.c:428
int32_t stack_pop_i32(STACK *stack, uint8_t *status)
pop an int32_t from the stack
Definition n_stack.c:448
bool delete_stack(STACK **stack)
delete a stack and free its memory
Definition n_stack.c:58
bool stack_push_i8(STACK *stack, int8_t i8)
push an int8_t onto the stack
Definition n_stack.c:330
STACK * new_stack(size_t nb_items)
allocate a new stack
Definition n_stack.c:34
bool stack_push_p(STACK *stack, void *p, uint16_t p_type)
push a pointer onto the stack with a custom type
Definition n_stack.c:576
void * stack_pop_p(STACK *stack, uint8_t *status)
pop a pointer from the stack
Definition n_stack.c:597
bool stack_push_p_default(STACK *stack, void *p)
push a pointer onto the stack with default type
Definition n_stack.c:626
STACK structure.
Definition n_stack.h:117
structure of a STACK item
Definition n_stack.h:103
structure of a STACK_ITEM data
Definition n_stack.h:75
Common headers and low-level functions & define.