From ed188d597e727eec8054c050c6da20e6768054db Mon Sep 17 00:00:00 2001 From: anonymous Date: Thu, 20 Jun 2024 16:18:19 +0200 Subject: [PATCH] last up to date commit --- .gitlab-ci.yml | 2 +- Makefile | 24 ++---- examples/ex_stack.c | 161 ++++++++++++++++++++++++++++++++++++++ include/nilorea/n_stack.h | 34 ++++---- src/n_stack.c | 15 ++-- 5 files changed, 191 insertions(+), 45 deletions(-) create mode 100644 examples/ex_stack.c diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b615b07f..54806718 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -43,7 +43,7 @@ build_examples: tests: stage: tests script: - - cd examples ; chmod +Xx valgrind.sh ; ./valgrind.sh + - make examples ; cd examples ; chmod +Xx valgrind.sh ; ./valgrind.sh cache: key: apt-cache paths: diff --git a/Makefile b/Makefile index fc884eb7..00ba9032 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,8 @@ # Makefile for the Nilorea Library # +.PHONY: clean clean-tmp distclean copy-libs download-deps download-and-build main all default release debug + ifeq ($(FORCE_NO_ALLEGRO),) FORCE_NO_ALLEGRO=0 endif @@ -62,14 +64,6 @@ ifeq ($(OS),Windows_NT) LIB_STATIC_EXT=32.a LIB_DYN_EXT=32.dll endif - ifeq ($(MSYSTEM),MINGW32CB) - RM=rm -f - CFLAGS+= -m32 -DARCH32BITS - LIB=-lnilorea32 - EXT=32.exe - LIB_STATIC_EXT=32.a - LIB_DYN_EXT=32.dll - endif ifeq ($(MSYSTEM),MINGW64) RM=rm -f CFLAGS+= -DARCH64BITS @@ -78,14 +72,6 @@ ifeq ($(OS),Windows_NT) LIB_STATIC_EXT=64.a LIB_DYN_EXT=64.dll endif - ifeq ($(MSYSTEM),MINGW64CB) - RM=rm -f - CFLAGS+= -DARCH64BITS - LIB=-lnilorea64 - EXT=64.exe - LIB_STATIC_EXT=64.a - LIB_DYN_EXT=64.dll - endif HDR=$(SRC:%.c=%.h) nilorea.h OBJECTS=$(SRC:%.c=obj/%.o) ifeq "$(shell printf '#include \nint main(){return 0;}' | $(CC) -x c -Wall -O -o $(TMP_OBJ) - > /dev/null 2> /dev/null && echo $$? )" "0" @@ -120,19 +106,19 @@ else LDFLAGS+= -z noexecstack CFLAGS+= -std=c17 CLIBS=-lpthread -lm -lpcre -lz - ifeq "$(shell printf '#include \nint main(){return 0;}' | $(CC) -x c -Wall -O -o $(TMP_OBJ) - > /dev/null 2> /dev/null && echo $$? )" "0" + ifeq "$(shell printf '\#include \nint main(){return 0;}' | $(CC) -x c -Wall -O -o $(TMP_OBJ) - > /dev/null 2> /dev/null && echo $$? )" "0" HAVE_ALLEGRO=1 else HAVE_ALLEGRO=0 endif KAFKA_CFLAGS=-I$(shell realpath ./external/librdkafka/src) - ifeq "$(shell printf '#include \nint main(){return 0;}' | $(CC) $(KAFKA_CFLAGS) -x c -Wall -O -o $(TMP_OBJ) - > /dev/null 2> /dev/null && echo $$? )" "0" + ifeq "$(shell printf '\#include \nint main(){return 0;}' | $(CC) $(KAFKA_CFLAGS) -x c -Wall -O -o $(TMP_OBJ) - > /dev/null 2> /dev/null && echo $$? )" "0" HAVE_KAFKA=1 else HAVE_KAFKA=0 KAFKA_CFLAGS= endif - ifeq "$(shell printf '#include \nint main(){return 0;}' | $(CC) -x c -Wall -O -o $(TMP_OBJ) - > /dev/null 2> /dev/null && echo $$? )" "0" + ifeq "$(shell printf '\#include \nint main(){return 0;}' | $(CC) -x c -Wall -O -o $(TMP_OBJ) - > /dev/null 2> /dev/null && echo $$? )" "0" HAVE_OPENSSL=1 else HAVE_OPENSSL=0 diff --git a/examples/ex_stack.c b/examples/ex_stack.c new file mode 100644 index 00000000..617bf692 --- /dev/null +++ b/examples/ex_stack.c @@ -0,0 +1,161 @@ +/**\example ex_stack.c Nilorea Library stack API + *\author Castagnier Mickael + *\version 1.0 + *\date 17/06/2024 + */ + +#include +#include +#include +#include + +#include "nilorea/n_common.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) +{ + set_log_level( LOG_INFO ); + + /* 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 ); + uint8_t status = STACK_IS_UNDEFINED ; + switch( item -> v_type ) + { + case STACK_ITEM_BOOL: + { + bool bval = stack_pop_b( stack , &status ); + n_log( LOG_INFO , "got bool: %d" , bval ); + } + break; + case STACK_ITEM_INT32: + { + int32_t val = stack_pop_i32( stack , &status ); + n_log( LOG_INFO , "got int32_t: %d" , val ); + } + 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 ); +} diff --git a/include/nilorea/n_stack.h b/include/nilorea/n_stack.h index cfcee7fc..e7266133 100644 --- a/include/nilorea/n_stack.h +++ b/include/nilorea/n_stack.h @@ -115,21 +115,23 @@ extern "C" } STACK ; #ifdef ENV_64BITS -#define stack_push( __STACK , __VAL ) _Generic((__VAL), \ -bool: stack_push_b, \ -char: stack_push_c, \ -uint8_t: stack_push_ui8, \ -int8_t: stack_push_i8, \ -uint32_t: stack_push_ui32, \ -int32_t: stack_push_i32, \ -uint64_t: stack_push_ui64, \ -int64_t: stack_push_i64, \ -float: stack_push_f, \ -double: stack_push_d, \ -void*: stack_push_p, \ -)(__STACK,__VAL) +#define stack_push( __STACK , __VAL ) \ + _Generic((__VAL), \ + bool: stack_push_b, \ + char: stack_push_c, \ + uint8_t: stack_push_ui8, \ + int8_t: stack_push_i8, \ + uint32_t: stack_push_ui32, \ + int32_t: stack_push_i32, \ + uint64_t: stack_push_ui64, \ + int64_t: stack_push_i64, \ + float: stack_push_f, \ + double: stack_push_d, \ + void*: stack_push_p \ + )( __STACK , __VAL ) #else -#define stack_push( __STACK, __VAL ) _Generic((__VAL), \ +#define stack_push( __STACK, __VAL ) \ + _Generic((__VAL), \ bool: stack_push_b, \ char: stack_push_c, \ uint8_t: stack_push_ui8, \ @@ -138,8 +140,8 @@ void*: stack_push_p, \ int32_t: stack_push_i32, \ float: stack_push_f, \ double: stack_push_d, \ - void*: stack_push_p, \ - )(__STACK,__VAL) + void*: stack_push_p \ + )( __STACK , __VAL ) #endif STACK *new_stack( size_t nb_items ); diff --git a/src/n_stack.c b/src/n_stack.c index dba56083..32992642 100644 --- a/src/n_stack.c +++ b/src/n_stack.c @@ -71,16 +71,17 @@ size_t __stack_push( STACK *stack , uint8_t *status ) { (*status) = STACK_IS_UNDEFINED ; __n_assert( stack , return 0 ); + (*status) = STACK_IS_FULL ; - __n_assert( stack_is_full( stack ) ,return 0 ); + if( stack_is_full( stack ) ) + return 0 ; - size_t next_pos = stack -> head + 1 ; - if( next_pos >= stack -> size ) - next_pos = 0 ; + size_t next_pos = ( stack -> head + 1 ) % stack -> size ; // if next_pos == tail, the stack is full if( next_pos == stack -> tail ) { + // status already set to (*status) = STACK_IS_FULL ; return 0 ; } @@ -111,9 +112,7 @@ size_t __stack_pop( STACK *stack , uint8_t *status ) (*status) = STACK_ITEM_OK ; // next is where tail will point to after this read - next_pos = stack -> tail + 1; - if( next_pos >= stack -> size ) - next_pos = 0; + next_pos = (stack -> tail + 1) % stack -> size; // set item status, dec counter, move, return value stack -> stack_array[ stack -> tail ] . is_set = 0 ; @@ -129,11 +128,9 @@ size_t __stack_pop( STACK *stack , uint8_t *status ) bool stack_push_b( STACK *stack , bool b ) { uint8_t status = STACK_ITEM_OK ; - size_t next_pos = __stack_push( stack , &status ); if( status != STACK_ITEM_OK ) return FALSE ; - stack -> stack_array[ stack -> head ] . data . b = b; stack -> stack_array[ stack -> head ] . v_type = STACK_ITEM_BOOL ; stack -> head = next_pos ;