diff options
author | deva <deva> | 2006-08-16 13:23:11 +0000 |
---|---|---|
committer | deva <deva> | 2006-08-16 13:23:11 +0000 |
commit | ed45396539b206dfc5857312ac12e9e465d31ed5 (patch) | |
tree | 1a45f70764bbcb816f193e6c8d574ef9e92ffea5 | |
parent | 72acb40ac9a477bdfb3f214b642ae6a7929a1dd9 (diff) |
Added the lua library.
Added LUA and a new configuration class.
Removed the no-longer-used config.h
-rw-r--r-- | lib/config.h | 33 | ||||
-rw-r--r-- | lib/configuration.cc | 28 | ||||
-rw-r--r-- | lib/configuration.h | 29 | ||||
-rw-r--r-- | lib/liblua_wrapper.cc | 153 | ||||
-rw-r--r-- | lib/liblua_wrapper.h | 57 |
5 files changed, 267 insertions, 33 deletions
diff --git a/lib/config.h b/lib/config.h deleted file mode 100644 index e7101c9..0000000 --- a/lib/config.h +++ /dev/null @@ -1,33 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/*************************************************************************** - * config.h - * - * Thu Jul 28 12:46:38 CEST 2005 - * Copyright 2004 Bent Bisballe - * deva@aasimon.org - ****************************************************************************/ - -/* - * This file is part of MIaV. - * - * MIaV is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MIaV is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with MIaV; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - */ - -#ifndef __CONFIG_IS_LOADED__ -#define __CONFIG_IS_LOADED__ - -#include "../config.h" - -#endif/*__CONFIG_IS_LOADED__*/ diff --git a/lib/configuration.cc b/lib/configuration.cc new file mode 100644 index 0000000..dbc0dfd --- /dev/null +++ b/lib/configuration.cc @@ -0,0 +1,28 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * configuration.cc + * + * Tue Aug 15 23:57:13 CEST 2006 + * Copyright 2006 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of MIaV. + * + * MIaV is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MIaV is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with MIaV; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include "configuration.h" + diff --git a/lib/configuration.h b/lib/configuration.h new file mode 100644 index 0000000..d187531 --- /dev/null +++ b/lib/configuration.h @@ -0,0 +1,29 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * configuration.h + * + * Tue Aug 15 23:57:12 CEST 2006 + * Copyright 2006 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of MIaV. + * + * MIaV is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MIaV is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with MIaV; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#ifndef __MIAV_CONFIGURATION_H__ +#define __MIAV_CONFIGURATION_H__ +#endif/*__MIAV_CONFIGURATION_H__*/ diff --git a/lib/liblua_wrapper.cc b/lib/liblua_wrapper.cc new file mode 100644 index 0000000..7f493c5 --- /dev/null +++ b/lib/liblua_wrapper.cc @@ -0,0 +1,153 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * liblua_wrapper.cc + * + * Mon Aug 14 17:36:03 CEST 2006 + * Copyright 2006 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of MIaV. + * + * MIaV is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MIaV is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with MIaV; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include "liblua_wrapper.h" + +#include <stdexcept> + +LibLUAWrapper::LibLUAWrapper() +{ + L = lua_open(); + // luaL_openlibs(L); +} + +LibLUAWrapper::~LibLUAWrapper() +{ + lua_close(L); +} + +int LibLUAWrapper::loadFile(char *fname) +{ + int s; + + s = luaL_loadfile(L, fname); + + switch(s) { + case 0: //no errors; + break; + case LUA_ERRSYNTAX: //syntax error during pre-compilation; + case LUA_ERRMEM: //memory allocation error. + case LUA_ERRFILE: //cannot open/read the file. + error = std::string(lua_tostring(L, lua_gettop(L))); + return 1; + default: + error = std::string("Unknown return value of luaL_loadfile."); + return 1; + } + + s = lua_pcall(L, 0, LUA_MULTRET, 0); + + switch(s) { + case 0: // Success + break; + case LUA_ERRRUN:// a runtime error. + case LUA_ERRMEM:// memory allocation error. For such errors, Lua does not call the error handler function. + case LUA_ERRERR:// error while running the error handler function. + error = std::string(lua_tostring(L, lua_gettop(L))); + return 1; + default: + error = std::string("Unknown return value of lua_pcall."); + return 1; + } + + return 0; +} + +int LibLUAWrapper::getInteger(char *name) +{ + lua_getfield(L, LUA_GLOBALSINDEX, name); + + int top = lua_gettop(L); + + int val = lua_tointeger(L, top); + + lua_remove(L, top); + + return val; +} + +double LibLUAWrapper::getReal(char *name) +{ + lua_getfield(L, LUA_GLOBALSINDEX, name); + + int top = lua_gettop(L); + + double val = lua_tonumber(L, top); + + lua_remove(L, top); + + return val; +} + +bool LibLUAWrapper::getBoolean(char *name) +{ + lua_getfield(L, LUA_GLOBALSINDEX, name); + + int top = lua_gettop(L); + + bool val = lua_toboolean(L, top); + + lua_remove(L, top); + + return val; +} + +std::string LibLUAWrapper::getString(char *name) +{ + lua_getfield(L, LUA_GLOBALSINDEX, name); + + int top = lua_gettop(L); + + std::string val = std::string(lua_tostring(L, top)); + + lua_remove(L, top); + + return val; +} + +std::string LibLUAWrapper::getError() +{ + return error; +} + +#ifdef LUA_TEST + +int main() +{ + LibLUAWrapper lua; + + if(!lua.loadFile("test.lua")) { + printf("a: %f\n", lua.getReal("a")); + printf("b: %d\n", lua.getInteger("b")); + printf("c: %d\n", lua.getBoolean("c")); + printf("fisk: %s\n", lua.getString("fisk").c_str()); + } else { + fprintf(stderr, "LUA load error: %s\n", lua.getError().c_str()); + } + return 0; +} + +#endif diff --git a/lib/liblua_wrapper.h b/lib/liblua_wrapper.h new file mode 100644 index 0000000..fed15c1 --- /dev/null +++ b/lib/liblua_wrapper.h @@ -0,0 +1,57 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * liblua_wrapper.h + * + * Mon Aug 14 17:36:03 CEST 2006 + * Copyright 2006 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of MIaV. + * + * MIaV is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MIaV is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with MIaV; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#ifndef __MIAV_LIBLUA_WRAPPER_H__ +#define __MIAV_LIBLUA_WRAPPER_H__ + +// Inludes all lua .h files. +#include <lua.hpp> +#include <string> + +class LibLUAWrapper { +public: + LibLUAWrapper(); + ~LibLUAWrapper(); + + /** + * loadFile reads and parses a lue file. + * @return 0 on success 1 on error + */ + int loadFile(char *fname); + + double getReal(char *name); + int getInteger(char *name); + bool getBoolean(char *name); + std::string getString(char *name); + + std::string getError(); + +private: + lua_State *L; + std::string error; +}; + +#endif/*__MIAV_LIBLUA_WRAPPER_H__*/ |