8#ifndef __NILOREA_COMMONS__
9#define __NILOREA_COMMONS__
35#if defined( _WIN32 ) || defined( _WIN64 )
40#define WEXITSTATUS(w) (((w) >> 8) & 0xff)
43#define WIFEXITED(w) (((w) & 0xff) == 0)
49#if defined(__GNUC__) && __GNUC__ >= 7
50#define FALL_THROUGH __attribute__ ((fallthrough))
56#if defined( _WIN32 ) || defined( _WIN64 )
64#if !defined( ENV_32BITS ) || !defined( ENV_64BITS )
65#if defined( __GNUC__ )
66#if defined( __x86_64__ ) || defined( __ppc64__ )
67#define __ENVBITS __ENV_64BITS
69#define __ENVBITS __ENV_32BITS
75#define BYTEORDER_LITTLE_ENDIAN 0
77#define BYTEORDER_BIG_ENDIAN 1
80#ifndef BYTEORDER_ENDIAN
82#if defined(__BYTE_ORDER__)
83#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
84#define BYTEORDER_ENDIAN BYTEORDER_LITTLE_ENDIAN
85#elif (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
86#define BYTEORDER_ENDIAN BYTEORDER_BIG_ENDIAN
88#define BYTEORDER_ENDIAN BYTEORDER_LITTLE_ENDIAN
89#warning "Unknown machine byteorder endianness detected. User needs to define BYTEORDER_ENDIAN."
90#warning "Setting default to BYTEORDER_LITTLE_ENDIAN"
93#elif defined(__GLIBC__)
95#if (__BYTE_ORDER == __LITTLE_ENDIAN)
96#define BYTEORDER_ENDIAN BYTEORDER_LITTLE_ENDIAN
97#elif (__BYTE_ORDER == __BIG_ENDIAN)
98#define BYTEORDER_ENDIAN BYTEORDER_BIG_ENDIAN
100#define BYTEORDER_ENDIAN BYTEORDER_LITTLE_ENDIAN
101#warning "Unknown machine byteorder endianness detected. User needs to define BYTEORDER_ENDIAN."
102#warning "Setting default to BYTEORDER_LITTLE_ENDIAN"
105#elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
106#define BYTEORDER_ENDIAN BYTEORDER_LITTLE_ENDIAN
107#elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
108#define BYTEORDER_ENDIAN BYTEORDER_BIG_ENDIAN
110#elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || defined(__powerpc__) || defined(__ppc__) || defined(__hpux) || defined(__hppa) || defined(_MIPSEB) || defined(_POWER) || defined(__s390__)
111#define BYTEORDER_ENDIAN BYTEORDER_BIG_ENDIAN
112#elif defined(__i386__) || defined(__alpha__) || defined(__ia64) || defined(__ia64__) || defined(_M_IX86) || defined(_M_IA64) || defined(_M_ALPHA) || defined(__amd64) || defined(__amd64__) || defined(_M_AMD64) || defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || defined(__bfin__)
113#define BYTEORDER_ENDIAN BYTEORDER_LITTLE_ENDIAN
114#elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
115#define BYTEORDER_ENDIAN BYTEORDER_LITTLE_ENDIAN
117#define BYTEORDER_ENDIAN BYTEORDER_LITTLE_ENDIAN
118#warning "Unknown machine byteorder endianness detected. User needs to define BYTEORDER_ENDIAN."
119#warning "Setting default to BYTEORDER_LITTLE_ENDIAN"
123#if defined( __windows__ )
126 typedef unsigned int uint;
128 typedef unsigned long ulong;
130 typedef unsigned short ushort;
132 typedef unsigned char uchar;
137#define FORCE_INLINE __forceinline
138#elif defined( __linux__ ) || defined( __windows__ )
139#define FORCE_INLINE inline __attribute__((always_inline))
141#define FORCE_INLINE __attribute__((always_inline))
172#define _str( __PTR ) ((__PTR)?(__PTR):"NULL")
174#define _strp( __PTR ) ((__PTR)?(__PTR):NULL)
176#define _strw( __PTR ) ((__PTR)?(__PTR):" ")
178#define _nstr( __PTR ) ((__PTR&&__PTR->data)?(__PTR->data):"NULL")
180#define _nstrp( __PTR ) ((__PTR&&__PTR->data)?(__PTR->data):NULL)
183#define Malloc( __ptr , __struct , __size ) \
185 int __n_errno = 0 ; \
186 int64_t __byte_size = __size ; \
187 if( __byte_size <= 0 ) __byte_size = 16; \
189 __ptr = ( __struct *)calloc( __byte_size , sizeof( __struct ) ); \
193 n_log( LOG_ERR , "( %s *)calloc( %ld , sizeof( %s ) ) %s at line %d of %s \n", #__ptr , __size , #__struct , (__n_errno==0)?"malloc error":strerror( __n_errno ) , __LINE__ , __FILE__); \
198#define Alloca( __ptr , __size ) \
200 int __n_errno = 0 ; \
201 int64_t __byte_size = __size ; \
202 if( __byte_size <= 0 ) __byte_size = 16 ; \
204 __ptr = alloca( __byte_size ); \
208 n_log( LOG_ERR , "%s=alloca( %d ) %s at line %d of %s \n", #__ptr , __size , (__n_errno==0)?"alloca error":strerror( __n_errno ) , __LINE__ , __FILE__); \
212 memset( __ptr , 0 , __byte_size ); \
217#define Realloc( __ptr, __struct , __size ) \
219 int __n_errno = 0 ; \
220 int64_t __byte_size = __size * sizeof( __struct ); \
221 if( __byte_size <= 0 ) __byte_size = 16 ; \
223 void *__new_ptr = ( __struct *)realloc( __ptr , __byte_size ); \
224 __n_errno = errno ; \
227 n_log( LOG_ERR , "( %s *)realloc( %s * sizeof( %d ) ) %s at line %d of %s \n", #__ptr , #__struct , __size , (__n_errno==0)?"realloc error":strerror( __n_errno ) , __LINE__ , __FILE__);\
231 __ptr = __new_ptr ; \
236#define Reallocz( __ptr, __struct , __old_size , __size ) \
238 int __n_errno = 0 ; \
239 size_t __byte_size = __size * sizeof( __struct ); \
240 if( __byte_size <= 0 ) __byte_size = 16 ; \
242 void *__new_ptr = ( __struct *)realloc( __ptr , __byte_size ); \
243 __n_errno = errno ; \
246 n_log( LOG_ERR , "( %s *)realloc( %s * sizeof( %d ) ) %s at line %d of %s \n", #__ptr , #__struct , __size , (__n_errno==0)?"realloc error":strerror( __n_errno ) , __LINE__ , __FILE__);\
250 __ptr = __new_ptr ; \
251 if( __byte_size > __old_size ) memset( ( __ptr + __old_size ) , 0 , __byte_size - __old_size ); \
256#define Free( __ptr ) \
264 n_log( LOG_DEBUG , "Free( %s ) already done or NULL at line %d of %s \n", #__ptr , __LINE__ , __FILE__ );\
268#define FreeNoLog( __ptr )\
276#define __n_assert( __ptr , __ret ) \
279 n_log( LOG_DEBUG , "if( !(%s) ) assert at line %d of %s" , #__ptr , __LINE__ , __FILE__ ); \
284#define CALL_RETRY(retvar, expression) do { \
285 retvar = (expression); \
286} while (retvar == -1 && errno == EINTR);
289#define next_odd( __val ) ( (__val)%2 == 0 ) ? (__val) : ( __val + 1 )
292#define next_even( __val ) ( (__val)%2 == 0 ) ? (__val + 1) : ( __val )
296#define init_error_check() \
297 static int ___error__check_flag = FALSE ;
303#define ifzero if( 0 ==
306#define iffalse if( FALSE ==
309#define iftrue if( TRUE ==
312#define checkerror() if( ___error__check_flag == TRUE ) \
313{ n_log( LOG_ERR , "checkerror return false at line %d of %s" , __LINE__ , __FILE__ ); \
318#define endif ){ ___error__check_flag = TRUE ; n_log( LOG_ERR , "First err was at line %d of %s" , __LINE__ , __FILE__ );}
322(___error__check_flag == TRUE)
325#define equal_if( __a , __cond , __b ) if( (__a) __cond (__b) ){ __a = __b ; }
330#define RWLOCK_LOGLEVEL LOG_DEBUG
333#define RWLOCK_LOGLEVEL LOG_NULL
337#define init_lock( __rwlock_mutex ) \
339 pthread_rwlockattr_t __attr ; \
340 pthread_rwlockattr_init( &__attr ); \
344 n_log( RWLOCK_LOGLEVEL , "init_lock %s" , #__rwlock_mutex ); \
345 __rwlock_mutex = (pthread_rwlock_t)PTHREAD_RWLOCK_INITIALIZER ; \
346 __ret = pthread_rwlock_init( &(__rwlock_mutex) , &__attr ); \
349 n_log( LOG_ERR , "Error %s while initializing %s at %s:%s:%d" , strerror( __ret ) , #__rwlock_mutex , __FILE__ , __func__ , __LINE__ ); \
351 pthread_rwlockattr_destroy( &__attr ); \
357#define read_lock( __rwlock_mutex ) \
362 n_log( RWLOCK_LOGLEVEL , "read lock %s" , #__rwlock_mutex ); \
363 __ret = pthread_rwlock_rdlock( &(__rwlock_mutex) ); \
366 n_log( LOG_ERR , "Error %s while read locking %s at %s:%s:%d" , strerror( __ret ) , #__rwlock_mutex , __FILE__ , __func__ , __LINE__ ); \
373#define write_lock( __rwlock_mutex ) \
378 n_log( RWLOCK_LOGLEVEL , "write lock %s" , #__rwlock_mutex ); \
379 __ret = pthread_rwlock_wrlock( &(__rwlock_mutex) ); \
382 n_log( LOG_ERR , "Error %s while write locking %s at %s:%s:%d" , strerror( __ret ) , #__rwlock_mutex , __FILE__ , __func__ , __LINE__ ); \
389#define unlock( __rwlock_mutex ) \
394 n_log( RWLOCK_LOGLEVEL , "unlock lock %s" , #__rwlock_mutex ); \
395 __ret = pthread_rwlock_unlock( &(__rwlock_mutex) ); \
398 n_log( LOG_ERR , "Error %s while unlocking %s at %s:%s:%d" , strerror( __ret ) , #__rwlock_mutex , __FILE__ , __func__ , __LINE__ ); \
404#define rw_lock_destroy( __rwlock_mutex ) \
409 n_log( RWLOCK_LOGLEVEL , "destroy lock %s" , #__rwlock_mutex ); \
410 __ret = pthread_rwlock_destroy( &(__rwlock_mutex) ); \
413 n_log( LOG_ERR , "Error %s while destroying %s at %s:%s:%d" , strerror( __ret ) , #__rwlock_mutex , __FILE__ , __func__ , __LINE__ ); \
428#define STOPWANTED 1002
435#define randomize() { srand((unsigned)time(NULL)); rand(); }
439#define MIN(a,b) (((a)<(b))?(a):(b))
443#define MAX(a,b) (((a)>(b))?(a):(b))
448#define CONCAT_BUILDER(a, b) a ## b
450#define CONCAT(a, b) CONCAT_BUILDER(a, b)
452#if( BYTEORDER_ENDIAN == BYTEORDER_LITTLE_ENDIAN )
454# define htonll(x) (x)
456# define ntohll(x) (x)
459# define htonll(x) ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
461# define ntohll(x) ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
465 void n_abort(
char const *format, ... );
479 int n_popen(
char *cmd,
int read_buf_size,
void **nstr_output,
int *ret );
484 pid_t
system_nb(
const char * command,
int * infp,
int * outfp);
char * get_prog_name(void)
get current program name
void N_HIDE_STR(char *buf,...)
store a hidden version of a string
int get_computer_name(char *computer_name, size_t len)
abort program with a text
pid_t system_nb(const char *command, int *infp, int *outfp)
Non blocking system call.
char * n_get_file_extension(char path[])
get extension of path+filename
int n_popen(char *cmd, int read_buf_size, void **nstr_output, int *ret)
launch a command abd return output and status
char * get_prog_dir(void)
get current program running directory
void n_abort(char const *format,...)
abort program with a text
int n_daemonize(void)
Daemonize program.
int file_exist(const char *filename)
test if file exist and if it's readable
Macro to build enums and their tostring counterparts, a reduced version of https://github....