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-05 22:55:01 +00:00
|
|
|
void* lovrFilesystemRead(const char* path, int* bytesRead) {
|
2016-11-02 03:27:15 +00:00
|
|
|
|
2016-11-05 22:55:01 +00:00
|
|
|
// Open file
|
|
|
|
PHYSFS_file* handle = PHYSFS_openRead(path);
|
2016-11-02 03:27:15 +00:00
|
|
|
if (!handle) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-11-05 22:55:01 +00:00
|
|
|
// Get file size
|
|
|
|
int size = PHYSFS_fileLength(handle);
|
|
|
|
if (size < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate buffer
|
|
|
|
void* data = malloc(size);
|
|
|
|
if (!data) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform read
|
|
|
|
*bytesRead = PHYSFS_read(handle, data, 1, size);
|
|
|
|
PHYSFS_close(handle);
|
2016-11-02 03:27:15 +00:00
|
|
|
|
2016-11-05 22:55:01 +00:00
|
|
|
// Make sure we got everything
|
|
|
|
if (*bytesRead != size) {
|
|
|
|
free(data);
|
2016-11-02 03:27:15 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2016-11-01 00:14:31 +00:00
|
|
|
int lovrFilesystemSetSource(const char* source) {
|
2016-11-05 23:15:04 +00:00
|
|
|
return PHYSFS_mount(source, NULL, 0);
|
2016-11-01 00:14:31 +00:00
|
|
|
}
|
2016-11-05 23:13:51 +00:00
|
|
|
|
|
|
|
int lovrFilesystemWrite(const char* path, const char* contents, int size) {
|
|
|
|
|
|
|
|
// Open file
|
|
|
|
PHYSFS_file* handle = PHYSFS_openWrite(path);
|
|
|
|
if (!handle) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform write
|
|
|
|
int bytesWritten = PHYSFS_write(handle, contents, 1, size);
|
|
|
|
PHYSFS_close(handle);
|
|
|
|
return bytesWritten;
|
|
|
|
}
|