memory semantics
[vg.git] / vg_io.h
diff --git a/vg_io.h b/vg_io.h
index a97d20a0c6d4c0fcf6a6e9fb50c04460fa07a17a..af1554b3c856d2f9009c4b10f395ab389ac84591 100644 (file)
--- a/vg_io.h
+++ b/vg_io.h
 #include <stdio.h>
 #include <errno.h>
 
+
+#define _TINYDIR_MALLOC(_size) vg_linear_alloc( vg_mem.scratch, _size )
+#define _TINYDIR_FREE(_size) 
+
+#include "submodules/tinydir/tinydir.h"
+#include <sys/stat.h>
+
+/* 
+ * Create directory and silently ignore errors for already exists
+ */
+VG_STATIC int vg_mkdir( const char *path )
+{
+   if( mkdir( path, S_IRWXU|S_IRWXG|S_IWOTH|S_IXOTH ) ){
+      if( errno == EEXIST )
+         return 1;
+
+      vg_error( "Failed to create directory: %s\n", path );
+      return 0;
+   }
+   else{
+      return 1;
+   }
+}
+
 /*
  * File I/O
  */
 
 VG_STATIC void vg_file_print_invalid( FILE *fp )
 {
-   if( feof( fp )) 
-   {
+   if( feof( fp )) {
       vg_error( "mdl_open: header too short\n" );
    }
-   else
-   {
+   else{
       if( ferror( fp ))
          vg_error( "mdl_open: %s\n", strerror(errno) );
       else
@@ -54,11 +76,11 @@ VG_STATIC void *vg_file_read( void *lin_alloc, const char *path, u32 *size )
             else{
                if( ferror( f ) ){
                   fclose(f);
-                  vg_fatal_exit_loop( "read error" );
+                  vg_fatal_error( "read error" );
                }
                else{
                   fclose(f);
-                  vg_fatal_exit_loop( "unknown error codition" );
+                  vg_fatal_error( "unknown error codition" );
                }
             }
          }
@@ -68,11 +90,9 @@ VG_STATIC void *vg_file_read( void *lin_alloc, const char *path, u32 *size )
                fclose( f );
 
       *size = (u32)current;
-      
       return buffer;
        }
-       else
-       {
+       else{
       vg_error( "vg_disk_open_read: %s\n", strerror(errno) );
                return NULL;
        }
@@ -96,19 +116,39 @@ VG_STATIC char *vg_file_read_text( void *lin_alloc, const char *path, u32 *sz )
 }
 
 
-VG_STATIC int vg_asset_write( const char *path, void *data, i64 size )
-{
+VG_STATIC int vg_asset_write( const char *path, void *data, i64 size ){
        FILE *f = fopen( path, "wb" );
-       if( f )
-       {
+       if( f ){
                fwrite( data, size, 1, f );
                fclose( f );
                return 1;
        }
-       else
-       {
+       else{
                return 0;
        }
 }
 
+/* TODO: error handling if read fails */
+VG_STATIC int vg_file_copy( const char *src, const char *dst, void *lin_alloc )
+{
+   vg_info( "vg_file_copy( %s -> %s )\n", src, dst );
+   u32 size;
+   void *data = vg_file_read( lin_alloc, src, &size );
+   return vg_asset_write( dst, data, size );
+}
+
+VG_STATIC const char *vg_path_filename( const char *path )
+{
+   const char *base = path;
+   
+   for( int i=0; i<1024; i++ ){
+      if( path[i] == '\0' ) break;
+      if( path[i] == '/' ){
+         base = path+i+1;
+      }
+   }
+
+   return base;
+}
+
 #endif /* VG_IO_H */