better build program
[vg.git] / vg_platform.h
index c7e178b93df5dbb8a891e52e30d09da31bc8c843..9366095cc702e3026a16c74efc38b8e30f9750b2 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,12 +33,27 @@ 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>
+
 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 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; i<len; i++ ){
@@ -52,9 +61,14 @@ VG_STATIC u32 vg_strncpy( const char *src, char *dst, u32 len,
 
       if( !src[i] ) return i;
 
-      if( (behaviour == k_strncpy_always_add_null) && (i == len-1) ){
-         dst[i] = '\0';
-         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" );
+         }
       }
    }
 
@@ -62,41 +76,181 @@ VG_STATIC u32 vg_strncpy( const char *src, char *dst, u32 len,
 }
 
 typedef struct vg_str vg_str;
+typedef struct vg_str_dynamic vg_str_dynamic;
+
 struct vg_str{
    char *buffer;
-   u32 i, len;
+   i32 i,   /* -1: error condition. otherwise, current cursor position */
+       len; /* -1: dynamically allocated. otherwise, buffer length */
 };
 
-VG_STATIC void vg_strnull( vg_str *str, char *buffer, u32 len )
-{
+struct vg_str_dynamic {
+   i32 len;
+};
+
+/* 
+ * Returns the current storage size of the string
+ */
+static i32 vg_str_storage( vg_str *str ){
+   if( str->len == -1 ){
+      if( str->buffer ){
+         vg_str_dynamic *arr = (vg_str_dynamic *)str->buffer;
+         return (arr-1)->len;
+      }
+      else return 0;
+   }
+   else return str->len;
+}
+
+/*
+ * Reset string. If len is -1 (dynamically allocated), buffer must be either 
+ * NULL or be acquired from malloc or realloc
+ */
+static void vg_strnull( vg_str *str, char *buffer, i32 len ){
    str->buffer = buffer;
+   if( 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_strfree( vg_str *str ){
+   if( str->len == -1 ){
+      if( str->buffer ){
+         vg_str_dynamic *arr = (vg_str_dynamic *)str->buffer;
+         free( arr-1 );
 
-      if( append[i] == '\0' ) return;
+         str->buffer = NULL;
+         str->i = 0;
+      }
    }
 }
 
-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;
+/*
+ * Double the size of the dynamically allocated string. If unallocated, alloc of
+ * 16 bytes minimum.
+ */
+static i32 vg_str_dynamic_grow( vg_str *str ){
+   if( str->buffer ){
+      vg_str_dynamic *hdr = ((vg_str_dynamic *)str->buffer) - 1;
+      i32 total = (hdr->len + sizeof(vg_str_dynamic)) * 2;
+      hdr = realloc( hdr, total );
+      hdr->len = total - sizeof(vg_str_dynamic);
+      str->buffer = (char *)(hdr+1);
+      return hdr->len;
    }
-   else{
-      str->buffer[ str->i ++ ] = '\0';
-      return 1;
+   else {
+      vg_str_dynamic *hdr = malloc(16);
+      hdr->len = 16-sizeof(vg_str_dynamic);
+      str->buffer = (char *)(hdr+1);
+      str->buffer[0] = '\0';
+      return hdr->len;
    }
 }
 
-VG_STATIC u32 vg_strdjb2( const char *str )
-{
+/*
+ * Append null terminated string to vg_str 
+ */
+static void vg_strcat( vg_str *str, const char *append ){
+   if( !append || (str->i == -1) ) return;
+
+   i32 max = vg_str_storage( str ),
+       i   = 0;
+
+append:
+   if( str->i == max ){
+      if( str->len == -1 )
+         max = vg_str_dynamic_grow( str );
+      else{
+         str->i = -1;
+         str->buffer[ max-1 ] = '\0';
+         return;
+      }
+   }
+
+   char c = append[ i ++ ];
+   str->buffer[ str->i ] = c;
+
+   if( c == '\0' )
+      return;
+
+   str->i ++;
+   goto append;
+}
+
+/* 
+ * Append character to vg_str 
+ */
+static void vg_strcatch( vg_str *str, char c ){
+   vg_strcat( str, (char[]){ c, '\0' } );
+}
+
+/* 
+ * FIXME: Negative numbers
+ */
+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; j<i; j ++ )
+         reverse[j] = temp[ i-1-j ];
+      reverse[i] = '\0';
+
+      vg_strcat( str, reverse );
+   }
+   else
+      vg_strcat( str, "0" );
+}
+
+static void vg_strcati32r( vg_str *str, i32 value, i32 n, char alt ){
+   char temp[32];
+   i32 i=0;
+   while( value ){
+      if( i>=n ) 
+         break;
+
+      temp[ n-1 - (i ++) ] = '0' + (value % 10);
+      value /= 10;
+   }
+
+   for( ;i<n; i ++ )
+      temp[ n-1 - i ] = alt;
+
+   temp[n]='\0';
+   vg_strcat( str, temp );
+}
+
+/*
+ * Returns 1 if string did not overflow while building
+ */
+static int vg_strgood( vg_str *str ){
+   if( str->i == -1 ) return 0;
+   else return 1;
+}
+
+/*
+ * Returns pointer to last instance of character
+ */
+static char *vg_strch( vg_str *str, char c ){
+   char *ptr = NULL;
+   for( i32 i=0; i<str->i; 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++) )
@@ -105,13 +259,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))