mirror of https://github.com/bjornbytes/lovr.git
lovr.filesystem.read;
This commit is contained in:
parent
9743660d5a
commit
0c0429b8d9
|
@ -39,21 +39,36 @@ int lovrFilesystemIsFile(const char* path) {
|
|||
return lovrFilesystemExists(path) && !lovrFilesystemIsDirectory(path);
|
||||
}
|
||||
|
||||
char* lovrFilesystemRead(const char* path, int* size) {
|
||||
PHYSFS_file* handle = PHYSFS_openRead(path);
|
||||
void* lovrFilesystemRead(const char* path, int* bytesRead) {
|
||||
|
||||
// Open file
|
||||
PHYSFS_file* handle = PHYSFS_openRead(path);
|
||||
if (!handle) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int length = PHYSFS_fileLength(handle);
|
||||
|
||||
if (length < 0) {
|
||||
// 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);
|
||||
|
||||
// Make sure we got everything
|
||||
if (*bytesRead != size) {
|
||||
free(data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char* data = malloc(length * sizeof(char));
|
||||
*size = PHYSFS_read(handle, data, sizeof(char), length);
|
||||
return data;
|
||||
}
|
||||
|
||||
|
|
|
@ -4,5 +4,5 @@ int lovrFilesystemExists(const char* path);
|
|||
const char* lovrFilesystemGetExecutablePath();
|
||||
int lovrFilesystemIsDirectory(const char* path);
|
||||
int lovrFilesystemIsFile(const char* path);
|
||||
char* lovrFilesystemRead(const char* path, int* size);
|
||||
void* lovrFilesystemRead(const char* path, int* bytesRead);
|
||||
int lovrFilesystemSetSource(const char* source);
|
||||
|
|
|
@ -31,7 +31,7 @@ static int filesystemLoader(lua_State* L) {
|
|||
|
||||
if (lovrFilesystemIsFile(filename)) {
|
||||
int size;
|
||||
char* data = lovrFilesystemRead(filename, &size);
|
||||
void* data = lovrFilesystemRead(filename, &size);
|
||||
|
||||
if (data) {
|
||||
if (!luaL_loadbuffer(L, data, size, filename)) {
|
||||
|
@ -53,6 +53,7 @@ const luaL_Reg lovrFilesystem[] = {
|
|||
{ "getExecutablePath", l_lovrFilesystemGetExecutablePath },
|
||||
{ "isDirectory", l_lovrFilesystemIsDirectory },
|
||||
{ "isFile", l_lovrFilesystemIsFile },
|
||||
{ "read", l_lovrFilesystemRead },
|
||||
{ "setSource", l_lovrFilesystemSetSource },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
@ -110,6 +111,18 @@ int l_lovrFilesystemIsFile(lua_State* L) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
int l_lovrFilesystemRead(lua_State* L) {
|
||||
const char* path = luaL_checkstring(L, 1);
|
||||
int size;
|
||||
char* contents = lovrFilesystemRead(path, &size);
|
||||
if (!contents) {
|
||||
return luaL_error(L, "Could not read file '%s'", path);
|
||||
}
|
||||
|
||||
lua_pushlstring(L, contents, size);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_lovrFilesystemSetSource(lua_State* L) {
|
||||
const char* source = lua_tostring(L, 1);
|
||||
|
||||
|
|
|
@ -9,4 +9,5 @@ int l_lovrFilesystemExists(lua_State* L);
|
|||
int l_lovrFilesystemGetExecutablePath(lua_State* L);
|
||||
int l_lovrFilesystemIsDirectory(lua_State* L);
|
||||
int l_lovrFilesystemIsFile(lua_State* L);
|
||||
int l_lovrFilesystemRead(lua_State* L);
|
||||
int l_lovrFilesystemSetSource(lua_State* L);
|
||||
|
|
Loading…
Reference in New Issue