bad char
[vg.git] / vg_io.h
diff --git a/vg_io.h b/vg_io.h
index 640ceaea12dd0397320c9cb143f1527b02641460..fefb6bfa1af487c141896a1f1723790f2c69cfaa 100644 (file)
--- a/vg_io.h
+++ b/vg_io.h
-/* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
+/* Copyright (C) 2021-2024 Harry Godden (hgn) - All Rights Reserved */
 
-#ifndef VG_IO_H
-#define VG_IO_H
-
-#include "vg_stdint.h"
+#pragma once
 #include "vg_platform.h"
 #include "vg_log.h"
 #include "vg_mem.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>
-VG_STATIC int vg_mkdir( const char *path )
-{
-   if( mkdir( path, S_IRWXU|S_IRWXG|S_IWOTH|S_IXOTH ) ){
-      vg_error( "Failed to create directory: %s\n", path );
-      return 0;
-   }
-   else{
-      return 1;
-   }
-}
+typedef struct vg_dir vg_dir;
+#ifndef _WIN32
+ #include <dirent.h>
+ struct vg_dir{
+   DIR *h;
+   struct dirent *data;
+   u32 index;
+ };
+#else
+ #include <windows.h>
+ #include <fileapi.h>
+ struct vg_dir{
+   HANDLE h;
+   WIN32_FIND_DATA data;
+   u32 index;
+ };
+#endif
+
+enum vg_entry_type{
+   k_vg_entry_type_unknown,
+   k_vg_entry_type_file,
+   k_vg_entry_type_dir
+};
+
+int vg_dir_open( vg_dir *dir, const char *name );
+const char *vg_dir_entry_name( vg_dir *dir );
+int vg_dirskip( vg_dir *dir );
+int vg_dir_next_entry( vg_dir *dir );
+enum vg_entry_type vg_dir_entry_type( vg_dir *dir );
+void vg_dir_close( vg_dir *dir );
+void vg_file_print_invalid( FILE *fp );
 
 /*
  * File I/O
  */
 
-#define VG_FILE_IO_CHUNK_SIZE 1024*256
-
-VG_STATIC void vg_file_print_invalid( FILE *fp )
-{
-   if( feof( fp )) {
-      vg_error( "mdl_open: header too short\n" );
-   }
-   else{
-      if( ferror( fp ))
-         vg_error( "mdl_open: %s\n", strerror(errno) );
-      else
-         vg_error( "mdl_open: unkown failure\n" );
-
-   }
-}
-
 /* read entire binary file */
-VG_STATIC void *vg_file_read( void *lin_alloc, const char *path, u32 *size )
-{
-       FILE *f = fopen( path, "rb" );
-       if( f ){
-      void *buffer = vg_linear_alloc( lin_alloc, 0 );
-      u64 current = 0;
-
-      /* read in chunks */
-      for( u32 i=0; 1; i++ ){
-         buffer = vg_linear_extend( lin_alloc, buffer, VG_FILE_IO_CHUNK_SIZE );
-
-         u64 l = fread( buffer + current, 1, VG_FILE_IO_CHUNK_SIZE, f );
-         current += l;
-
-         if( l != VG_FILE_IO_CHUNK_SIZE ){
-            if( feof( f ) ){
-               break;
-            }
-            else{
-               if( ferror( f ) ){
-                  fclose(f);
-                  vg_fatal_error( "read error" );
-               }
-               else{
-                  fclose(f);
-                  vg_fatal_error( "unknown error codition" );
-               }
-            }
-         }
-      }
-
-      buffer = vg_linear_resize( lin_alloc, buffer, vg_align8(current) );
-               fclose( f );
-
-      *size = (u32)current;
-      return buffer;
-       }
-       else{
-      vg_error( "vg_disk_open_read: %s\n", strerror(errno) );
-               return NULL;
-       }
-}
+void *vg_file_read( void *lin_alloc, const char *path, u32 *size );
 
 /* read entire file and append a null on the end */
-VG_STATIC char *vg_file_read_text( void *lin_alloc, const char *path, u32 *sz )
-{
-   u32 size;
-   char *str = vg_file_read( lin_alloc, path, &size );
-
-   if( !str )
-      return NULL;
-
-   /* include null terminator */
-   str = vg_linear_extend( lin_alloc, str, 1 );
-   str[ size ] = '\0';
-   *sz = size+1;
-
-   return str;
-}
-
-
-VG_STATIC int vg_asset_write( const char *path, void *data, i64 size ){
-       FILE *f = fopen( path, "wb" );
-       if( f ){
-               fwrite( data, size, 1, f );
-               fclose( f );
-               return 1;
-       }
-       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;
-}
+char *vg_file_read_text( void *lin_alloc, const char *path, u32 *sz );
 
-#endif /* VG_IO_H */
+int vg_asset_write( const char *path, void *data, i64 size );
+int vg_file_copy( const char *src, const char *dst, void *lin_alloc );
+const char *vg_path_filename( const char *path );