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
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include "nilorea/n_log.h"
#include "nilorea/n_str.h"
#include "nilorea/n_stack.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( !strncmp( "LOG_NULL", optarg, 5 ) )
{
log_level = LOG_NULL ;
}
else
{
if( !strncmp( "LOG_NOTICE", optarg, 6 ) )
{
log_level = LOG_NOTICE;
}
else
{
if( !strncmp( "LOG_INFO", optarg, 7 ) )
{
log_level = LOG_INFO;
}
else
{
if( !strncmp( "LOG_ERR", optarg, 5 ) )
{
log_level = LOG_ERR;
}
else
{
if( !strncmp( "LOG_DEBUG", optarg, 5 ) )
{
log_level = LOG_DEBUG;
}
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 );
}
}
}
set_log_level( log_level );
} /* 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()%1 ;
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 );
exit( 0 );
}
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:74
#define LOG_DEBUG
debug-level messages
Definition n_log.h:66
#define LOG_ERR
error conditions
Definition n_log.h:58
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:97
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:62
#define LOG_NULL
no log output
Definition n_log.h:27
#define LOG_INFO
informational
Definition n_log.h:64
bool stack_pop_b(STACK *stack, uint8_t *status)
helper to pop a bool
Definition n_stack.c:197
STACK_ITEM * stack_peek(STACK *stack, size_t position)
peek in the stack with un stacking the stack item
Definition n_stack.c:72
#define STACK_ITEM_INT32
v_type value for a int32_t
Definition n_stack.h:34
#define STACK_ITEM_OK
code for a successfully retrieved item
Definition n_stack.h:55
#define STACK_ITEM_BOOL
v_type value for a bool
Definition n_stack.h:24
#define STACK_IS_UNDEFINED
code for a NULL stack state
Definition n_stack.h:51
int32_t stack_pop_i32(STACK *stack, uint8_t *status)
helper to pop a int32_t
Definition n_stack.c:440
#define stack_push(__STACK, __VAL)
stack push helper with automatic value selection
Definition n_stack.h:135
bool delete_stack(STACK **stack)
delete a STACK *stack
Definition n_stack.c:38
STACK * new_stack(size_t size)
allocate a new STACK
Definition n_stack.c:17
STACK structure.
Definition n_stack.h:104
structure of a STACK item
Definition n_stack.h:88
Common headers and low-level hugly functions & define.
Generic log system.
N_STR and string function declaration.