Skip to content

Instantly share code, notes, and snippets.

@karl-zylinski
Last active February 22, 2016 23:14
Show Gist options
  • Save karl-zylinski/a6de16d6b0ddc5c17e22 to your computer and use it in GitHub Desktop.
Save karl-zylinski/a6de16d6b0ddc5c17e22 to your computer and use it in GitHub Desktop.
Using C++11 initializer lists in C-style code to make pretty minimalistic error reporting
struct File
{
uint8* data;
uint32 size;
};
struct LoadedFile
{
bool valid;
File file;
};
LoadedFile load_file(Allocator* alloc, const char* filename)
{
FILE* file_handle = fopen(filename, "r");
if (file_handle == nullptr)
return {false};
fseek(file_handle, 0, SEEK_END);
uint32 filesize = ftell(file_handle);
fseek(file_handle, 0, SEEK_SET);
if (filesize == 0)
return {false};
uint8* data = (uint8*)alloc->alloc(uint32(filesize));
if (data == nullptr)
return {false};
fread(data, 1, filesize, file_handle);
fclose(file_handle);
File file = {};
file.data = data;
file.size = filesize;
return {true, file};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment