fix swap profile
[vg.git] / vg_platform.h
index 52c26c0933fc67ecabb60b6bb2e5a772b6d50257..e8b288e9731afd4034d922ff010a5ed3e4054700 100644 (file)
@@ -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"
 
@@ -39,27 +33,51 @@ struct vg_achievement
 #define vg_list_size( A ) (sizeof(A)/sizeof(A[0]))
 #define VG_MUST_USE_RESULT __attribute__((warn_unused_result))
 
+#include <stdio.h>
+#include <string.h>
+#include <stdarg.h>
+#include <ctype.h>
+#include <math.h>
+#include <assert.h>
+#include <setjmp.h>
+#include <sys/time.h>
+#include <math.h>
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+
+#include "vg_string.h"
+
 enum strncpy_behaviour{
    k_strncpy_always_add_null = 0,
-   k_strncpy_allow_cutoff = 1
+   k_strncpy_allow_cutoff = 1,
+   k_strncpy_overflow_fatal = 2
 };
 
-VG_STATIC void vg_strncpy( const char *src, char *dst, u32 len,
-                           enum strncpy_behaviour behaviour )
+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; i<len; i++ ){
       dst[i] = src[i];
 
-      if( !src[i] )
-         break;
-
-      if( (behaviour == k_strncpy_always_add_null) && (i == len-1) )
-         dst[i] = '\0';
+      if( !src[i] ) return i;
+
+      if( i == len-1 ){
+         if( behaviour == k_strncpy_always_add_null ){
+            dst[i] = '\0';
+            return i;
+         }
+         else if( behaviour == k_strncpy_overflow_fatal ){
+            vg_fatal_error( "Strncpy dest exceeded buffer length\n" );
+         }
+      }
    }
+
+   return 0;
 }
 
-VG_STATIC u32 vg_strdjb2( const char *str )
-{
+static u32 vg_strdjb2( const char *str ){
    u32 hash = 5381, c;
 
    while( (c = *str++) )
@@ -68,13 +86,18 @@ VG_STATIC u32 vg_strdjb2( const char *str )
    return hash;
 }
 
-#include <stdio.h>
-#include <dirent.h>
-#include <string.h>
-#include <stdarg.h>
-#include <ctype.h>
-#include <math.h>
-#include <assert.h>
+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))