mirror of https://github.com/bjornbytes/lovr.git
luax_loadfile uses core/fs;
This commit is contained in:
parent
d2b0b30faf
commit
6a8048765f
|
@ -1,7 +1,7 @@
|
||||||
#include "api.h"
|
#include "api.h"
|
||||||
#include "filesystem/filesystem.h"
|
#include "filesystem/filesystem.h"
|
||||||
#include "filesystem/file.h"
|
|
||||||
#include "data/blob.h"
|
#include "data/blob.h"
|
||||||
|
#include "core/fs.h"
|
||||||
#include "core/ref.h"
|
#include "core/ref.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -35,22 +35,21 @@ static int pushDirectoryItem(void* userdata, const char* path, const char* filen
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
File file;
|
fs_handle file;
|
||||||
char buffer[4096];
|
char buffer[4096];
|
||||||
} luax_Reader;
|
} luax_Reader;
|
||||||
|
|
||||||
static const char* readCallback(lua_State* L, void* data, size_t* size) {
|
static const char* readCallback(lua_State* L, void* data, size_t* size) {
|
||||||
luax_Reader* reader = data;
|
luax_Reader* reader = data;
|
||||||
*size = lovrFileRead(&reader->file, reader->buffer, sizeof(reader->buffer));
|
*size = sizeof(reader->buffer);
|
||||||
return *size == 0 ? NULL : reader->buffer;
|
return fs_read(reader->file, reader->buffer, size) ? reader->buffer : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int luax_loadfile(lua_State* L, const char* path, const char* debug) {
|
static int luax_loadfile(lua_State* L, const char* path, const char* debug) {
|
||||||
luax_Reader reader;
|
luax_Reader reader;
|
||||||
lovrFileInit(&reader.file, path);
|
lovrAssert(fs_open(path, OPEN_READ, &reader.file), "Could not open file %s", path);
|
||||||
lovrAssert(lovrFileOpen(&reader.file, OPEN_READ), "Could not open file %s", path);
|
|
||||||
int status = lua_load(L, readCallback, &reader, debug);
|
int status = lua_load(L, readCallback, &reader, debug);
|
||||||
lovrFileDestroy(&reader.file);
|
fs_close(reader.file);
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case LUA_ERRMEM: return luaL_error(L, "Memory allocation error: %s", lua_tostring(L, -1));
|
case LUA_ERRMEM: return luaL_error(L, "Memory allocation error: %s", lua_tostring(L, -1));
|
||||||
case LUA_ERRSYNTAX: return luaL_error(L, "Syntax error: %s", lua_tostring(L, -1));
|
case LUA_ERRSYNTAX: return luaL_error(L, "Syntax error: %s", lua_tostring(L, -1));
|
||||||
|
|
Loading…
Reference in New Issue