[NEW] File handling functions

This commit is contained in:
2022-09-13 17:18:13 +02:00
parent fd3eeded52
commit 2063485240
3 changed files with 167 additions and 10 deletions

76
lua.cpp
View File

@@ -513,6 +513,67 @@ extern "C" {
lua_pushinteger(L, strlen(str));
return 1;
}
static int cpp_fopen(lua_State *L) {
const char* str = luaL_checkstring(L, 1);
uint8_t mode = luaL_optinteger(L, 2, 0);
fopen(str, mode);
return 0;
}
static int cpp_fclose(lua_State *L) {
fclose();
return 0;
}
static int cpp_fwritei(lua_State *L) {
int value = luaL_checkinteger(L, 1);
fwritei(value);
return 0;
}
static int cpp_fwrited(lua_State *L) {
float value = luaL_checknumber(L, 1);
fwrited(value);
return 0;
}
static int cpp_fwrites(lua_State *L) {
const char* str = luaL_checkstring(L, 1);
fwrites(str);
return 0;
}
static int cpp_fwriteb(lua_State *L) {
bool value = lua_toboolean(L, 1);
fwriteb(value);
return 0;
}
static int cpp_fwriteln(lua_State *L) {
void fwriteln();
return 0;
}
static int cpp_freadi(lua_State *L) {
lua_pushinteger(L,freadi());
return 1;
}
static int cpp_freadd(lua_State *L) {
lua_pushnumber(L,freadd());
return 1;
}
static int cpp_freads(lua_State *L) {
lua_pushstring(L,freads());
return 1;
}
static int cpp_freadb(lua_State *L) {
lua_pushboolean(L,freadb());
return 1;
}
}
lua_State *L;
@@ -602,6 +663,18 @@ void lua_init() {
lua_pushcfunction(L,cpp_ascii); lua_setglobal(L, "ascii");
lua_pushcfunction(L,cpp_strlen); lua_setglobal(L, "strlen");
lua_pushcfunction(L,cpp_fopen); lua_setglobal(L, "fopen");
lua_pushcfunction(L,cpp_fclose); lua_setglobal(L, "fclose");
lua_pushcfunction(L,cpp_fwritei); lua_setglobal(L, "fwritei");
lua_pushcfunction(L,cpp_fwrited); lua_setglobal(L, "fwrited");
lua_pushcfunction(L,cpp_fwrites); lua_setglobal(L, "fwrites");
lua_pushcfunction(L,cpp_fwriteb); lua_setglobal(L, "fwriteb");
lua_pushcfunction(L,cpp_fwriteln); lua_setglobal(L, "fwriteln");
lua_pushcfunction(L,cpp_freadi); lua_setglobal(L, "freadi");
lua_pushcfunction(L,cpp_freadd); lua_setglobal(L, "freadd");
lua_pushcfunction(L,cpp_freads); lua_setglobal(L, "freads");
lua_pushcfunction(L,cpp_freadb); lua_setglobal(L, "freadb");
lua_pushinteger(L, 0); lua_setglobal(L, "KEY_UNKNOWN");
lua_pushinteger(L, 4); lua_setglobal(L, "KEY_A");
lua_pushinteger(L, 5); lua_setglobal(L, "KEY_B");
@@ -710,6 +783,9 @@ void lua_init() {
lua_pushinteger(L, 230); lua_setglobal(L, "KEY_RALT");
lua_pushinteger(L, 231); lua_setglobal(L, "KEY_RGUI");
lua_pushinteger(L, 0); lua_setglobal(L, "FILE_READ");
lua_pushinteger(L, 1); lua_setglobal(L, "FILE_WRITE");
if (lua_pcall(L,0, LUA_MULTRET, 0)) {
debug("runtime error");
debug(lua_tostring(L, -1));