2018-02-22 12:36:24 +01:00
|
|
|
#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));
|
|
|
|
}
|
|
|
|
|
2018-02-22 12:36:24 +01:00
|
|
|
SDL_RWops *
|
|
|
|
io_load_rwops(const char *path)
|
|
|
|
{
|
2018-02-23 19:32:01 +01:00
|
|
|
if (!PHYSFS_exists(path))
|
|
|
|
file_error(path);
|
2018-02-22 12:36:24 +01:00
|
|
|
|
|
|
|
return PHYSFSRWOPS_openRead(path);
|
2018-02-22 13:02:40 +01:00
|
|
|
}
|
2018-02-23 19:32:01 +01:00
|
|
|
|
|
|
|
void
|
2018-03-20 21:54:57 +01:00
|
|
|
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);
|
2018-03-20 21:54:57 +01:00
|
|
|
char *buffer = ec_malloc(sizeof(char) * (unsigned long) (size + 1));
|
2018-02-24 00:06:45 +01:00
|
|
|
PHYSFS_readBytes(file, buffer, (PHYSFS_uint32) size);
|
2018-03-20 21:54:57 +01:00
|
|
|
buffer[size] = '\0';
|
2018-02-23 19:32:01 +01:00
|
|
|
PHYSFS_close(file);
|
|
|
|
|
|
|
|
*len = (unsigned long) size;
|
|
|
|
*dest = buffer;
|
|
|
|
}
|