Last active
February 22, 2016 23:14
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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