2013年春节过后,愿大家都节日快乐,同时为还活着的程序猿们祈祷,当然死了的也看不到这个文章啦**
先开个小小的玩笑,下面开始正题。
介绍下Lua做为c++程序的配置文件的用法
大家可能习惯了用xml作为配置文件,其实很多时候我们也可以使用脚本作为程序的配置文件,这样程序既可以添加入脚本
又可以使用配置文件,作为一种脚本语言做配置文件自然可以,用lua的好处有几个方面,
1.是它天生和c++程序结合非常好。
2.这样可以减少整个工程的编译次数(所有配置文件共同的好处)
3.容易进行跨平台的移植
4.lua的解释器非常小,不会使整个工程的体积变得臃肿
最常用的方式就是将要配置的东西放入table中。下面的代码是从table中读取数据的方法!如果有更好的可以提供出来一起分享一下!
最后附上读取配置文件的常用源代码:
#ifndef READLUAVALUE_H
#define READLUAVALUE_H
#include <string>
#include <lua.hpp>
using namespace std;
class LuaReadTable
{
public:
LuaReadTable(){}
~LuaReadTable(){}
void ReadTableFromKey_Int(lua_State *L, const char* lpszTableName, const char* lpszTableItem,int &int_value);
void ReadTableFromIndex_Int(lua_State *L, const char* lpszTableName, int index,int &int_value);
void ReadTableFromKey_String(lua_State *L, const char* lpszTableName, const char* lpszTableItem,string &string_value);
void ReadTableFromIndex_String(lua_State *L, const char* lpszTableName, int index,string &string_value);
void ReadTableFromKey_Double(lua_State *L, const char* lpszTableName, const char* lpszTableItem,double &double_value);
void ReadTableFromIndex_Double(lua_State *L, const char* lpszTableName, int index,double &double_value);
void ReadTableFromKey_UnsignedLong(lua_State *L, const char* lpszTableName, const char* lpszTableItem,unsigned long &unsigned_long_value);
void ReadTableFromIndex_UnsignedLong(lua_State *L, const char* lpszTableName, int index,unsigned long &unsigned_long_value);
void WriteTableFromKey_Int(lua_State *L, const char* lpszTableName, const char* lpszTableItem, int int_value);
void WriteTableFromIndex_Int(lua_State *L, const char* lpszTableName, int index, int int_value);
void WriteTableFromKey_String(lua_State *L, const char* lpszTableName, const char* lpszTableItem, string string_value);
void WriteTableFromIndex_String(lua_State *L, const char* lpszTableName, int index, string string_value);
void WriteTableFromKey_Double(lua_State *L, const char* lpszTableName, const char* lpszTableItem, double double_value);
void WriteTableFromIndex_Double(lua_State *L, const char* lpszTableName, int index, double double_value);
void WriteTableFromKey_UnsignedLong(lua_State *L, const char* lpszTableName, const char* lpszTableItem, unsigned long unsigned_long_value);
void WriteTableFromIndex_UnsignedLong(lua_State *L, const char* lpszTableName, int index, unsigned long unsigned_long_value);
void GetTableLenth(lua_State *L, const char* lpszTableName,int &length);
};
#endif // !READLUAVALUE_H
#include "ReadAndWriteLuaTableValue.h"
void LuaReadTable::ReadTableFromKey_Int(lua_State *L, const char* lpszTableName, const char* lpszTableItem,int &int_value)
{
lua_getglobal(L, lpszTableName);
lua_pushstring(L, lpszTableItem);
lua_gettable(L, -2);
//printf("%s.%s=%d\n", lpszTableName, lpszTableItem, (int)lua_tonumber(L, -1));
int_value = (int)lua_tonumber(L, -1);
lua_pop(L, 2);
}
void LuaReadTable::ReadTableFromIndex_Int(lua_State *L, const char* lpszTableName, int index,int &int_value)
{
lua_getglobal(L, lpszTableName);
lua_rawgeti(L, -1, index);
int_value = (int)lua_tonumber(L, -1);
lua_pop(L, 2);
}
void LuaReadTable::ReadTableFromKey_String(lua_State *L, const char* lpszTableName, const char* lpszTableItem,string &string_value)
{
lua_getglobal(L, lpszTableName);
lua_pushstring(L, lpszTableItem);
lua_gettable(L, -2);
string_value = lua_tostring(L, -1);
lua_pop(L, 2);
}
void LuaReadTable::ReadTableFromIndex_String(lua_State *L, const char* lpszTableName, int index,string &string_value)
{
lua_getglobal(L, lpszTableName);
lua_getglobal(L, lpszTableName);
lua_rawgeti(L, -1, index);
string_value = lua_tostring(L, -1);
lua_pop(L, 2);
}
void LuaReadTable::ReadTableFromKey_Double(lua_State *L, const char* lpszTableName, const char* lpszTableItem,double &double_value)
{
lua_getglobal(L, lpszTableName);
lua_pushstring(L, lpszTableItem);
lua_gettable(L, -2);
double_value = (double)lua_tonumber(L, -1);
lua_pop(L, 2);
}
void LuaReadTable::ReadTableFromIndex_Double(lua_State *L, const char* lpszTableName, int index,double &double_value)
{
lua_getglobal(L, lpszTableName);
lua_rawgeti(L, -1, index);
double_value = (double)lua_tonumber(L, -1);
lua_pop(L, 2);
}
void LuaReadTable::ReadTableFromKey_UnsignedLong(lua_State *L, const char* lpszTableName, const char* lpszTableItem,unsigned long &unsigned_long_value)
{
lua_getglobal(L, lpszTableName);
lua_pushstring(L, lpszTableItem);
lua_gettable(L, -2);
unsigned_long_value = (unsigned long)lua_tonumber(L, -1);
lua_pop(L, 2);
}
void LuaReadTable::ReadTableFromIndex_UnsignedLong(lua_State *L, const char* lpszTableName, int index,unsigned long &unsigned_long_value)
{
lua_getglobal(L, lpszTableName);
lua_rawgeti(L, -1, index);
unsigned_long_value = (unsigned long)lua_tonumber(L, -1);
lua_pop(L, 2);
}
void LuaReadTable::WriteTableFromKey_Int(lua_State *L, const char* lpszTableName, const char* lpszTableItem, int int_value)
{
lua_getglobal(L, lpszTableName);
lua_pushnumber(L, int_value);
lua_setfield(L, -2, lpszTableItem);
lua_pop(L, 1);
}
void LuaReadTable::WriteTableFromIndex_Int(lua_State *L, const char* lpszTableName, int index, int int_value)
{
lua_getglobal(L, lpszTableName);
lua_pushnumber(L, int_value);
lua_rawseti(L, -2, index);
lua_pop(L, 1);
}
void LuaReadTable::WriteTableFromKey_String(lua_State *L, const char* lpszTableName, const char* lpszTableItem, string string_value)
{
lua_getglobal(L, lpszTableName);
lua_pushstring(L, string_value.c_str());
lua_setfield(L, -2, lpszTableItem);
lua_pop(L, 1);
}
void LuaReadTable::WriteTableFromIndex_String(lua_State *L, const char* lpszTableName, int index, string string_value)
{
lua_getglobal(L, lpszTableName);
lua_pushstring(L, string_value.c_str());
lua_rawseti(L, -2, index);
lua_pop(L, 1);
}
void LuaReadTable::WriteTableFromKey_Double(lua_State *L, const char* lpszTableName, const char* lpszTableItem, double double_value)
{
lua_getglobal(L, lpszTableName);
lua_pushnumber(L, double_value);
lua_setfield(L, -2, lpszTableItem);
lua_pop(L, 1);
}
void LuaReadTable::WriteTableFromIndex_Double(lua_State *L, const char* lpszTableName, int index, double double_value)
{
lua_getglobal(L, lpszTableName);
lua_pushnumber(L, double_value);
lua_rawseti(L, -2, index);
lua_pop(L, 1);
}
void LuaReadTable::WriteTableFromKey_UnsignedLong(lua_State *L, const char* lpszTableName, const char* lpszTableItem, unsigned long unsigned_long_value)
{
lua_getglobal(L, lpszTableName);
lua_pushnumber(L, unsigned_long_value);
lua_setfield(L, -2, lpszTableItem);
lua_pop(L, 1);
}
void LuaReadTable::WriteTableFromIndex_UnsignedLong(lua_State *L, const char* lpszTableName, int index, unsigned long unsigned_long_value)
{
lua_getglobal(L, lpszTableName);
lua_pushnumber(L, unsigned_long_value);
lua_rawseti(L, -2, index);
lua_pop(L, 1);
}
void LuaReadTable::GetTableLenth(lua_State *L, const char* lpszTableName,int &length)
{
lua_getglobal(L, lpszTableName);
length=lua_rawlen(L,-1);
lua_pop(L, 1);
}