Skip to content

Commit

Permalink
last up to date commit
Browse files Browse the repository at this point in the history
  • Loading branch information
anonymous committed Jun 20, 2024
1 parent ddfdfba commit ed188d5
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
24 changes: 5 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 <allegro5/allegro.h>\nint main(){return 0;}' | $(CC) -x c -Wall -O -o $(TMP_OBJ) - > /dev/null 2> /dev/null && echo $$? )" "0"
Expand Down Expand Up @@ -120,19 +106,19 @@ else
LDFLAGS+= -z noexecstack
CFLAGS+= -std=c17
CLIBS=-lpthread -lm -lpcre -lz
ifeq "$(shell printf '#include <allegro5/allegro.h>\nint main(){return 0;}' | $(CC) -x c -Wall -O -o $(TMP_OBJ) - > /dev/null 2> /dev/null && echo $$? )" "0"
ifeq "$(shell printf '\#include <allegro5/allegro.h>\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 <rdkafka.h>\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 <rdkafka.h>\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 <openssl/ssl.h>\nint main(){return 0;}' | $(CC) -x c -Wall -O -o $(TMP_OBJ) - > /dev/null 2> /dev/null && echo $$? )" "0"
ifeq "$(shell printf '\#include <openssl/ssl.h>\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
Expand Down
161 changes: 161 additions & 0 deletions examples/ex_stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/**\example ex_stack.c 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_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 );
}
34 changes: 18 additions & 16 deletions include/nilorea/n_stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand All @@ -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 );
Expand Down
15 changes: 6 additions & 9 deletions src/n_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;
}

Expand Down Expand Up @@ -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 ;
Expand All @@ -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 ;
Expand Down

0 comments on commit ed188d5

Please sign in to comment.