Update file io to byte addressable, w/ T
This commit is contained in:
parent
480294da62
commit
4f324da0e9
15
src/fileio.c
15
src/fileio.c
@ -2,19 +2,20 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "fileio.h"
|
#include "fileio.h"
|
||||||
|
#include "global.h"
|
||||||
|
|
||||||
/* Loads a binary file located at filePath to memory, taking up a block of exactly memorySize bytes,
|
/* Loads a binary file located at filePath to memory, taking up a block of exactly memorySize bytes,
|
||||||
and returns the starting address of the data. If memorySize is insufficient to store the entire file,
|
and returns the starting address of the data. If memorySize is insufficient to store the entire file,
|
||||||
an appropriate error is reported. Excess memory is set to 0 bit values. */
|
an appropriate error is reported. Excess memory is set to 0 bit values. */
|
||||||
|
|
||||||
word *fileio_loadBin(const char *filePath, size_t memorySize) {
|
byte *fileio_loadBin(const char *filePath, size_t memorySize) {
|
||||||
FILE *file = fopen(filePath, "rb");
|
FILE *file = fopen(filePath, "rb");
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
fprintf(stderr, "Couldn't open %s!\n", filePath);
|
fprintf(stderr, "Couldn't open %s!\n", filePath);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
word *fileData = malloc(memorySize);
|
byte *fileData = malloc(memorySize);
|
||||||
if (fileData == NULL) {
|
if (fileData == NULL) {
|
||||||
fprintf(stderr, "Ran out of memory attempting to load %s!\n", filePath);
|
fprintf(stderr, "Ran out of memory attempting to load %s!\n", filePath);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
@ -22,10 +23,10 @@ word *fileio_loadBin(const char *filePath, size_t memorySize) {
|
|||||||
|
|
||||||
// Loop while reading from the file yields data. Only terminates if EOF is reached or ERROR occurs.
|
// Loop while reading from the file yields data. Only terminates if EOF is reached or ERROR occurs.
|
||||||
// Explicitly deal with attempting to write too much data to memory block, rather than allow segfault.
|
// Explicitly deal with attempting to write too much data to memory block, rather than allow segfault.
|
||||||
const size_t wordCount = memorySize/sizeof(word);
|
const size_t byteCount = memorySize/sizeof(byte);
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (fread(fileData + i, sizeof(word), 1, file)) {
|
while (fread(fileData + i, sizeof(byte), 1, file)) {
|
||||||
if (i >= wordCount) {
|
if (i >= byteCount) {
|
||||||
fprintf(stderr, "Attempting to load binary %s to memory of smaller size %zu!\n", filePath, memorySize);
|
fprintf(stderr, "Attempting to load binary %s to memory of smaller size %zu!\n", filePath, memorySize);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -40,8 +41,8 @@ word *fileio_loadBin(const char *filePath, size_t memorySize) {
|
|||||||
assert(fclose(file) != EOF);
|
assert(fclose(file) != EOF);
|
||||||
|
|
||||||
// If part of memory block was left uninitialized, initialize it to zero.
|
// If part of memory block was left uninitialized, initialize it to zero.
|
||||||
if (i < wordCount) {
|
if (i < byteCount) {
|
||||||
memset(fileData + i, 0, (wordCount - i) * sizeof(word));
|
memset(fileData + i, 0, (byteCount - i) * sizeof(byte));
|
||||||
}
|
}
|
||||||
return fileData;
|
return fileData;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,5 +5,5 @@
|
|||||||
|
|
||||||
#define EXIT_FAILURE 1
|
#define EXIT_FAILURE 1
|
||||||
|
|
||||||
extern word *fileio_loadBin(const char *filePath, size_t memorySize);
|
extern byte *fileio_loadBin(const char *filePath, size_t memorySize);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user