X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=vg_platform.h;h=30751f2817ec0dde038948a89d2b68182d65d5f3;hb=76d234b7dc5e6500e8a54009b367e7620f11ef97;hp=319f7a9abd1892e2fb4b8d03860484aa4c5725a0;hpb=76f30a0f4aef82c10b4affbeab3f0271936ce7ca;p=vg.git diff --git a/vg_platform.h b/vg_platform.h index 319f7a9..30751f2 100644 --- a/vg_platform.h +++ b/vg_platform.h @@ -29,34 +29,118 @@ struct vg_achievement const char *name; }; -#ifndef VG_STATIC -#define VG_STATIC static -#endif - #define vg_static_assert _Static_assert #define vg_list_size( A ) (sizeof(A)/sizeof(A[0])) #define VG_MUST_USE_RESULT __attribute__((warn_unused_result)) -VG_STATIC void vg_strncpy( const char *src, char *dst, u32 len ) -{ - for( u32 i=0; i -#include #include #include #include #include #include +#include +#include +#include +#include +#include + +enum strncpy_behaviour{ + k_strncpy_always_add_null = 0, + k_strncpy_allow_cutoff = 1, + k_strncpy_overflow_fatal = 2 +}; + +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; +} + +static void vg_strcat( vg_str *str, const char *append ) +{ + if( !append ) return; + for( u32 i=0; str->i < str->len; i++, str->i ++ ){ + str->buffer[ str->i ] = append[i]; + + if( append[i] == '\0' ) return; + } +} + +static int vg_strgood( vg_str *str ) +{ + if( str->i == str->len ){ + if( str->buffer[str->i -1] == '\0' ) return 1; + else return 0; + } + else return 1; +} + +static char *vg_strch( vg_str *str, char c ) +{ + char *ptr = NULL; + for( u32 i=0; ii; i++ ){ + if( str->buffer[i] == c ) + ptr = str->buffer+i; + } + + return ptr; +} + +static u32 vg_strdjb2( const char *str ) +{ + u32 hash = 5381, c; + + while( (c = *str++) ) + hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ + + 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