Kohi Game Engine
filesystem.h
Go to the documentation of this file.
1 
13 #pragma once
14 
15 #include "defines.h"
16 
18 typedef struct file_handle {
20  void* handle;
24 
26 typedef enum file_modes {
30  FILE_MODE_WRITE = 0x2
32 
41 #define CLOSE_IF_FAILED(func, handle) \
42  if (!func) { \
43  KERROR("File operation failed."); \
44  filesystem_close(handle); \
45  return false; \
46  }
47 
53 KAPI b8 filesystem_exists(const char* path);
54 
63 KAPI b8 filesystem_open(const char* path, file_modes mode, b8 binary, file_handle* out_handle);
64 
70 
78 KAPI b8 filesystem_size(file_handle* handle, u64* out_size);
79 
88 KAPI b8 filesystem_read_line(file_handle* handle, u64 max_length, char** line_buf, u64* out_line_length);
89 
96 KAPI b8 filesystem_write_line(file_handle* handle, const char* text);
97 
107 KAPI b8 filesystem_read(file_handle* handle, u64 data_size, void* out_data, u64* out_bytes_read);
108 
116 KAPI b8 filesystem_read_all_bytes(file_handle* handle, u8* out_bytes, u64* out_bytes_read);
117 
125 KAPI b8 filesystem_read_all_text(file_handle* handle, char* out_text, u64* out_bytes_read);
126 
135 KAPI b8 filesystem_write(file_handle* handle, u64 data_size, const void* data, u64* out_bytes_written);
This file contains global type definitions which are used throughout the entire engine and applicatio...
#define KAPI
Import/export qualifier.
Definition: defines.h:177
_Bool b8
8-bit boolean type
Definition: defines.h:58
unsigned long long u64
Unsigned 64-bit integer.
Definition: defines.h:28
unsigned char u8
Unsigned 8-bit integer.
Definition: defines.h:19
KAPI b8 filesystem_size(file_handle *handle, u64 *out_size)
Attempts to read the size of the file to which handle is attached.
KAPI void filesystem_close(file_handle *handle)
Closes the provided handle to a file.
KAPI b8 filesystem_read_all_text(file_handle *handle, char *out_text, u64 *out_bytes_read)
Reads all characters of data into out_text.
KAPI b8 filesystem_read_line(file_handle *handle, u64 max_length, char **line_buf, u64 *out_line_length)
Reads up to a newline or EOF.
KAPI b8 filesystem_exists(const char *path)
Checks if a file with the given path exists.
file_modes
File open modes. Can be combined.
Definition: filesystem.h:26
@ FILE_MODE_WRITE
Definition: filesystem.h:30
@ FILE_MODE_READ
Definition: filesystem.h:28
KAPI b8 filesystem_read_all_bytes(file_handle *handle, u8 *out_bytes, u64 *out_bytes_read)
Reads all bytes of data into out_bytes.
struct file_handle file_handle
Holds a handle to a file.
KAPI b8 filesystem_open(const char *path, file_modes mode, b8 binary, file_handle *out_handle)
Attempt to open file located at path.
KAPI b8 filesystem_write(file_handle *handle, u64 data_size, const void *data, u64 *out_bytes_written)
Writes provided data to the file.
KAPI b8 filesystem_write_line(file_handle *handle, const char *text)
Writes text to the provided file, appending a ' ' afterward.
KAPI b8 filesystem_read(file_handle *handle, u64 data_size, void *out_data, u64 *out_bytes_read)
Reads up to data_size bytes of data into out_bytes_read. Allocates *out_data, which must be freed by ...
Holds a handle to a file.
Definition: filesystem.h:18
void * handle
Opaque handle to internal file handle.
Definition: filesystem.h:20
b8 is_valid
Indicates if this handle is valid.
Definition: filesystem.h:22