Permalink
Browse files

File open/save in Lua

  • Loading branch information...
feragon committed Jun 19, 2017
1 parent f5600f7 commit ee9fc84e6cada581385db93e82a331a90b59b6ab
View
@@ -15,7 +15,9 @@ LuaInterface::~LuaInterface() {
void LuaInterface::initLua() {
luaL_openlibs(_L);
luaOpenQtBridge(_L);
lc::LCLua::importLCKernel(_L);
auto lcLua = lc::LCLua(_L);
lcLua.importLCKernel();
LuaIntf::Lua::setGlobal(_L, "luaInterface", this);
View
@@ -20,11 +20,13 @@ void LuaScript::on_luaRun_clicked() {
CadMdiChild* mdiChild = qobject_cast<CadMdiChild*>(activeSubWindow->widget());
auto luaState = LuaIntf::LuaState::newState();
lc::LCLua::addLuaLibs(luaState);
lc::LCLua::importLCKernel(luaState);
lc::LCLua::setDocument(luaState, mdiChild->document());
auto lcLua = lc::LCLua(luaState);
auto out = lc::LCLua::runString(luaState, ui->luaInput->toPlainText().toStdString().c_str());
lcLua.addLuaLibs();
lcLua.importLCKernel();
lcLua.setDocument(mdiChild->document());
auto out = lcLua.runString(ui->luaInput->toPlainText().toStdString().c_str());
_cliCommand->write(QString::fromStdString(out));
}
@@ -1 +1,6 @@
print(LC_interface)
file = openFile("/tmp/a", "r+")
actual = file:read(1)
actual = actual .. "Test"
print(actual)
file:write(actual)
@@ -20,8 +20,8 @@ using namespace lc;
void LCLua::importLCKernel(lua_State* L) {
LuaBinding(L)
void LCLua::importLCKernel() {
LuaBinding(_L)
.beginClass<Color>("Color")
.addConstructor(LUA_ARGS(
double r,
View
@@ -16,35 +16,65 @@ static const luaL_Reg loadedlibs[] = {
{NULL, NULL}
};
LCLua::LCLua() {
LCLua::LCLua(lua_State* L) :
_L(L) {
}
void LCLua::addLuaLibs(lua_State* L) {
void LCLua::addLuaLibs() {
const luaL_Reg *lib;
for (lib = loadedlibs; lib->func; lib++) {
luaL_requiref(L, lib->name, lib->func, 1);
lua_pop(L, 1);
luaL_requiref(_L, lib->name, lib->func, 1);
lua_pop(_L, 1);
}
//Add others non-LC functions
LuaBinding(L)
.addFunction("microtime", &lua_microtime);
LuaBinding(_L)
.addFunction("microtime", &lua_microtime)
.addFunction("openFile", &openFile)
.beginClass<FILE>("FILE")
.addFunction("read", [](FILE* file, const size_t len) {
return read(file, len);
})
.addFunction("write", [](FILE* file, const char* content) {
return write(file, content);
})
.endClass();
}
void LCLua::setDocument(lua_State* L, lc::Document_SPtr document) {
LuaIntf::Lua::setGlobal(L, "document", document);
void LCLua::setDocument(lc::Document_SPtr document) {
LuaIntf::Lua::setGlobal(_L, "document", document);
}
std::string LCLua::runString(lua_State* L, const char* code) {
std::string LCLua::runString(const char* code) {
std::string out;
auto s = luaL_dostring(L, code);
auto s = luaL_dostring(_L, code);
if (s != 0) {
out.append(lua_tostring(L, -1));
lua_pop(L, 1);
out.append(lua_tostring(_L, -1));
lua_pop(_L, 1);
}
return out;
}
FILE* LCLua::openFile(const char* path, const char* mode) {
//TODO: check if the file can be opened
return fopen(path, mode);
}
std::string LCLua::read(FILE* file, const size_t len) {
char buf[len + 1];
size_t n = fread(buf, sizeof(char), len, file);
buf[n] = '\0';
return std::string(buf);
}
void LCLua::write(FILE* file, const char* content) {
fwrite(content, sizeof(char), strlen(content), file);
}
View
@@ -17,15 +17,18 @@ namespace LuaIntf {
namespace lc {
class LCLua {
public:
LCLua();
LCLua(lua_State* L);
static void addLuaLibs(lua_State* L);
static void importLCKernel(lua_State* L);
static void setDocument(lua_State* L, lc::Document_SPtr document);
static std::string runString(lua_State* L, const char* code);
void addLuaLibs();
void importLCKernel();
void setDocument(lc::Document_SPtr document);
std::string runString(const char* code);
static std::string openFile(std::string path);
//TODO: 1 global version with path as param
//TODO: 1 local version with dialog -> subclass LCLua
static FILE* openFile(const char* path, const char* mode);
static std::string read(FILE* file, const size_t len);
static void write(FILE* file, const char* content);
private:
lua_State* _L;
};
}
@@ -31,9 +31,9 @@ void lc::PluginManager::loadPlugins() {
void lc::PluginManager::loadPlugin(const char* file) {
auto state = LuaIntf::LuaState::newState();
LCLua::addLuaLibs(state);
LCLua::importLCKernel(state);
auto lcLua = LCLua(state);
lcLua.addLuaLibs();
lcLua.importLCKernel();
LuaIntf::Lua::setGlobal(state, "LC_interface", _interface);
View
@@ -168,14 +168,15 @@ int main(int argc, char** argv) {
auto luaState = LuaIntf::LuaState::newState();
lc::LCLua::addLuaLibs(luaState);
lc::LCLua::importLCKernel(luaState);
lc::LCLua::setDocument(luaState, _document);
auto lcLua = lc::LCLua(luaState);
lcLua.addLuaLibs();
lcLua.importLCKernel();
lcLua.setDocument(_document);
std::string luaCode = loadFile(fIn);
if (luaCode.size() != 0) {
std::string out = lc::LCLua::runString(luaState, luaCode.c_str());
std::string out = lcLua.runString(luaCode.c_str());
if (out.size() > 0) {
std::cerr << out << std::endl;

0 comments on commit ee9fc84

Please sign in to comment.