BOOL W32LoadROM(ROM_T *rom)
{
rom->file_handle = fopen(rom->filename, "rb");
if (!rom->file_handle)
{
log_error("Couldn't open %s", rom->filename);
return FALSE;
}
// Above opens file and if it fails prints a error message. log_error(const char *, ...) is m own function. ignore it.
else
{
size_t ret; // size_t is unsigned int
struct stat sbuf; // struct stat is what fstat needs
fstat(fileno(rom->file_handle), &sbuf); // get file statistics of file rom->file_handle
rom->filesize = sbuf.st_size; // get file's size and puts it to rom->filesize
rom->filebuffer = (u8 *)malloc(rom->filesize); // allocates memory for filebuffer
ret = fread(rom->filebuffer, 1, rom->filesize, rom->file_handle); // reads rom->filesize amount of bytes from rom->file_handle and puts them in rom->filebuffer
fclose(rom->file_handle); // closes rom->file_handle ;)
if (ret != rom->filesize) // if ret (amoun of bytes read by fread) is different from rom->filesize it prints an error message
{
log_error("fread() returned wrong size!");
return FALSE;
}
}
return TRUE;
}