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
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#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)
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.