lovr/src/filesystem/filesystem.c

64 lines
1.2 KiB
C
Raw Normal View History

2016-11-01 00:14:31 +00:00
#include "filesystem.h"
#include "../util.h"
#include <physfs.h>
#ifdef APPLE
#include <mach-o/dyld.h>
#endif
void lovrFilesystemInit(const char* arg0) {
if (!PHYSFS_init(arg0)) {
error("Could not initialize filesystem: %s", PHYSFS_getLastError());
}
}
void lovrFilesystemDestroy() {
PHYSFS_deinit();
}
int lovrFilesystemExists(const char* path) {
return PHYSFS_exists(path);
}
const char* lovrFilesystemGetExecutablePath() {
#ifdef APPLE
char path[1024];
uint32_t size = sizeof(path);
if (_NSGetExecutablePath(path, &size) == 0) {
return path;
}
#endif
return NULL;
}
2016-11-01 01:35:00 +00:00
int lovrFilesystemIsDirectory(const char* path) {
return PHYSFS_isDirectory(path);
}
int lovrFilesystemIsFile(const char* path) {
return lovrFilesystemExists(path) && !lovrFilesystemIsDirectory(path);
}
2016-11-02 03:27:15 +00:00
char* lovrFilesystemRead(const char* path, int* size) {
PHYSFS_file* handle = PHYSFS_openRead(path);
if (!handle) {
return NULL;
}
int length = PHYSFS_fileLength(handle);
if (length < 0) {
return NULL;
}
char* data = malloc(length * sizeof(char));
*size = PHYSFS_read(handle, data, sizeof(char), length);
return data;
}
2016-11-01 00:14:31 +00:00
int lovrFilesystemSetSource(const char* source) {
2016-11-02 03:27:15 +00:00
int res = PHYSFS_mount(source, NULL, 0);
return res;
2016-11-01 00:14:31 +00:00
}