X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=vg_platform.h;h=b22f989ee608af389956056ca3c433357e389d96;hb=3b841cc427adddeeb5b624d254587a45622d506d;hp=b04ce672b9afa8d0803dec16dffb7f5bcbc48c77;hpb=e490893eb0e952a5e0672faeba7250e053e4175e;p=vg.git diff --git a/vg_platform.h b/vg_platform.h index b04ce67..b22f989 100644 --- a/vg_platform.h +++ b/vg_platform.h @@ -1,12 +1,6 @@ #ifndef VG_PLATFORM_H #define VG_PLATFORM_H -#ifdef VG_RELEASE - #define VG_STATIC static -#else - #define VG_STATIC -#endif - //#include "vg.h" #include "vg_stdint.h" @@ -40,12 +34,16 @@ struct vg_achievement #define VG_MUST_USE_RESULT __attribute__((warn_unused_result)) #include -#include #include #include #include #include #include +#include +#include +#include +#include +#include enum strncpy_behaviour{ k_strncpy_always_add_null = 0, @@ -53,8 +51,8 @@ enum strncpy_behaviour{ k_strncpy_overflow_fatal = 2 }; -VG_STATIC void vg_fatal_error( const char *fmt, ... ); -VG_STATIC u32 vg_strncpy( const char *src, char *dst, u32 len, +static void vg_fatal_error( const char *fmt, ... ); +static u32 vg_strncpy( const char *src, char *dst, u32 len, enum strncpy_behaviour behaviour ) { for( u32 i=0; ibuffer = buffer; str->buffer[0] = '\0'; str->i = 0; str->len = len; + + assert(len); } -VG_STATIC void vg_strcat( vg_str *str, const char *append ) -{ - for( u32 i=0; str->i < str->len; i++, str->i ++ ){ - str->buffer[ str->i ] = append[i]; +static void vg_strcat( vg_str *str, const char *append ){ + if( !append ) return; + if( str->i == -1 ) return; + + for( u32 i=0; str->i < str->len; str->i ++, i ++ ){ + str->buffer[ str->i ] = append[ i ]; - if( append[i] == '\0' ) return; + if( append[ i ] == '\0' ) + return; } + + /* overflow */ + str->buffer[ str->i ] = '\0'; + str->i = -1; } -VG_STATIC int vg_strgood( vg_str *str ) -{ - if( str->i == str->len ){ - if( str->buffer[str->i -1] == '\0' ) return 1; - else return 0; +static void vg_strcati32( vg_str *str, i32 value ){ + if( value ){ + char temp[32]; + int i=0; + while( value && (i<31) ){ + temp[ i ++ ] = '0' + (value % 10); + value /= 10; + } + + char reverse[32]; + for( int j=0; ji == -1 ) return 0; else return 1; } -VG_STATIC char *vg_strch( vg_str *str, char c ) -{ +static char *vg_strch( vg_str *str, char c ){ char *ptr = NULL; - for( u32 i=0; ii; i++ ){ + for( i32 i=0; ii; i++ ){ if( str->buffer[i] == c ) ptr = str->buffer+i; } @@ -119,8 +140,7 @@ VG_STATIC char *vg_strch( vg_str *str, char c ) return ptr; } -VG_STATIC u32 vg_strdjb2( const char *str ) -{ +static u32 vg_strdjb2( const char *str ){ u32 hash = 5381, c; while( (c = *str++) ) @@ -129,6 +149,19 @@ VG_STATIC u32 vg_strdjb2( const char *str ) return hash; } +static int vg_strdjb2_eq( const char *s1, u32 h1, + const char *s2, u32 h2 ) +{ + if( h1 == h2 ){ + if(!strcmp(s1, s2)) return 1; + else return 0; + } else return 0; +} + +#define VG_STRDJB2_EQ( CS1, S2, H2 ) \ + vg_strdjb2_eq( CS1, vg_strdjb2(CS1), S2, H2 ) + + #define VG_MIN( A, B ) ((A)<(B)?(A):(B)) #define VG_MAX( A, B ) ((A)>(B)?(A):(B)) #endif