breakhack/src/io_util.c

37 lines
866 B
C
Raw Normal View History

#include <physfs.h>
#include "physfsrwops.h"
#include "io_util.h"
#include "util.h"
2018-02-23 19:32:01 +01:00
static void
file_error(const char *path)
{
PHYSFS_ErrorCode code = PHYSFS_getLastErrorCode();
fatal("Unable to open file %s: (%d) %s", path, code, PHYSFS_getErrorByCode(code));
}
SDL_RWops *
io_load_rwops(const char *path)
{
2018-02-23 19:32:01 +01:00
if (!PHYSFS_exists(path))
file_error(path);
return PHYSFSRWOPS_openRead(path);
}
2018-02-23 19:32:01 +01:00
void
io_load_file_buffer(char **dest, unsigned long *len, const char *filepath)
2018-02-23 19:32:01 +01:00
{
if (!PHYSFS_exists(filepath))
file_error(filepath);
PHYSFS_File *file = PHYSFS_openRead(filepath);
PHYSFS_sint64 size = (unsigned long) PHYSFS_fileLength(file);
char *buffer = ec_malloc(sizeof(char) * (unsigned long) (size + 1));
PHYSFS_readBytes(file, buffer, (PHYSFS_uint32) size);
buffer[size] = '\0';
2018-02-23 19:32:01 +01:00
PHYSFS_close(file);
*len = (unsigned long) size;
*dest = buffer;
}