Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_stack.c

Nilorea Library stack API.

Nilorea Library stack API

Author
Castagnier Mickael
Version
1.0
Date
17/06/2024
/*
* Nilorea Library
* Copyright (C) 2005-2026 Castagnier Mickael
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include "nilorea/n_log.h"
#include "nilorea/n_str.h"
void usage(void) {
fprintf(stderr,
" -v version\n"
" -V log level: LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_DEBUG\n"
" -h help\n");
}
void process_args(int argc, char** argv) {
int getoptret = 0,
log_level = LOG_DEBUG; /* default log level */
/* Arguments optionnels */
/* -v version
* -V log level
* -h help
*/
while ((getoptret = getopt(argc, argv, "hvV:")) != EOF) {
switch (getoptret) {
case 'v':
fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
exit(1);
case 'V':
if (!strcmp("LOG_NULL", optarg))
else if (!strcmp("LOG_NOTICE", optarg))
else if (!strcmp("LOG_INFO", optarg))
else if (!strcmp("LOG_ERR", optarg))
else if (!strcmp("LOG_DEBUG", optarg))
else {
fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
exit(-1);
}
break;
default:
case '?': {
if (optopt == 'V') {
fprintf(stderr, "\n Missing log level\n");
} else if (optopt == 'p') {
fprintf(stderr, "\n Missing port\n");
} else if (optopt != 's') {
fprintf(stderr, "\n Unknow missing option %c\n", optopt);
}
usage();
exit(1);
}
case 'h': {
usage();
exit(1);
}
}
}
} /* void process_args( ... ) */
int main(int argc, char** argv) {
/* processing args and set log_level */
process_args(argc, argv);
STACK* stack = new_stack(16);
n_log(LOG_INFO, "created stack of 16 elements at %p", stack);
for (int it = 0; it < 20; it++) {
int32_t nb = rand() % 10;
bool btest = rand() % 2;
stack_push(stack, nb);
stack_push(stack, btest);
}
for (int it = 0; it < 20; it++) {
STACK_ITEM* item = NULL;
item = stack_peek(stack, stack->tail);
if (item) {
uint8_t status = STACK_IS_UNDEFINED;
switch (item->v_type) {
bool bval = stack_pop_b(stack, &status);
n_log(LOG_INFO, "got bool: %d", bval);
status = STACK_ITEM_OK;
} break;
int32_t val = stack_pop_i32(stack, &status);
n_log(LOG_INFO, "got int32_t: %d", val);
status = STACK_ITEM_OK;
} break;
default:
n_log(LOG_ERR, "uknown type %d", item->v_type);
break;
}
if (status != STACK_ITEM_OK) {
n_log(LOG_ERR, "error popping value ! status: %d", status);
}
}
}
delete_stack(&stack);
/* test additional type variants and stack_is_full, stack_is_empty */
stack = new_stack(8);
n_log(LOG_INFO, "stack_is_empty on new stack: %d", stack_is_empty(stack));
/* push various types */
char cval = 'A';
stack_push(stack, cval);
n_log(LOG_INFO, "pushed char: %c", cval);
double dval = 3.14159;
stack_push(stack, dval);
n_log(LOG_INFO, "pushed double: %f", dval);
float fval = 2.71f;
stack_push(stack, fval);
n_log(LOG_INFO, "pushed float: %f", fval);
uint8_t u8val = 255;
stack_push(stack, u8val);
n_log(LOG_INFO, "pushed uint8: %u", u8val);
int8_t i8val = -42;
stack_push(stack, i8val);
n_log(LOG_INFO, "pushed int8: %d", i8val);
uint32_t u32val = 123456;
stack_push(stack, u32val);
n_log(LOG_INFO, "pushed uint32: %u", u32val);
int data_value = 999;
void* pval = &data_value;
stack_push(stack, pval);
n_log(LOG_INFO, "pushed pointer: %p", pval);
int32_t i32val = -789;
stack_push(stack, i32val);
n_log(LOG_INFO, "pushed int32: %d", i32val);
n_log(LOG_INFO, "stack_is_full: %d", stack_is_full(stack));
/* pop them back in reverse order */
uint8_t pop_status = STACK_IS_UNDEFINED;
STACK_ITEM* peek_item = NULL;
while ((peek_item = stack_peek(stack, stack->tail)) != NULL) {
pop_status = STACK_IS_UNDEFINED;
switch (peek_item->v_type) {
char cv = stack_pop_c(stack, &pop_status);
n_log(LOG_INFO, "popped char: %c", cv);
} break;
double dv = stack_pop_d(stack, &pop_status);
n_log(LOG_INFO, "popped double: %f", dv);
} break;
float fv = stack_pop_f(stack, &pop_status);
n_log(LOG_INFO, "popped float: %f", fv);
} break;
uint8_t uv = stack_pop_ui8(stack, &pop_status);
n_log(LOG_INFO, "popped uint8: %u", uv);
} break;
int8_t iv = stack_pop_i8(stack, &pop_status);
n_log(LOG_INFO, "popped int8: %d", iv);
} break;
uint32_t uv = stack_pop_ui32(stack, &pop_status);
n_log(LOG_INFO, "popped uint32: %u", uv);
} break;
int32_t iv = stack_pop_i32(stack, &pop_status);
n_log(LOG_INFO, "popped int32: %d", iv);
} break;
void* pv = stack_pop_p(stack, &pop_status);
n_log(LOG_INFO, "popped pointer: %p (value=%d)", pv, pv ? *(int*)pv : 0);
} break;
default:
n_log(LOG_ERR, "unknown type %d", peek_item->v_type);
stack_pop_i32(stack, &pop_status);
break;
}
}
n_log(LOG_INFO, "stack_is_empty after pops: %d", stack_is_empty(stack));
delete_stack(&stack);
exit(0);
}
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.