zoukankan      html  css  js  c++  java
  • c++读取lua配置基础类

    一、内容介绍

    lua作为配置文件,里面的参数值的获取,在他人基础上做了修改,并且补充了一维数组的处理方式。

    若有不足之处请多多指教。

    对于二维数组,没有成功。希望大家继续补充和修改,非常感谢!

    二、Lua配置文件

    配置文件名称为test_read.lua 

    文件内容为:

    width = 10
    height = 3
    title = "this is a test"
    array = {r = 2,g = 3,b = 4}
    array_1d = {2,5,26,8}
    array_2d = {{2,5},{15,18},{25,26,28},{0,5,4}}


    三、解析类头文件

    对于lua单个对象解析,我在其他人基础上进一步的完善了类。

    做成了一个头文件,文件名称为lua_parser.h,类名称为:lua_parser.

    头文件内容为:

    #ifndef _rw_lua_parser_h__
    #define  _rw_lua_parser_h__
    
    #include <string>
    using namespace std;
    #define luac_c
    #define LUA_CORE
    #include "lua.h"
    #include "lauxlib.h"
    #include "lualib.h"
    #pragma  comment(lib,"lua5.2.3.lib")
    
    class lua_parser
    {
    public:
    	lua_parser(void)  
    	{  
    		l_state = luaL_newstate();  
    		luaL_openlibs(l_state);  
    	}  
    
    	virtual ~lua_parser(void)  
    	{  
    		lua_close(l_state);  
    	}  
    
    	bool load_file(string str)  
    	{  
    		if(luaL_dofile(l_state, str.c_str())){
    			return false;  
    		}
    		return true;  
    	}  
    
    	string load_string(string str)  
    	{  
    		lua_getglobal(l_state, str.c_str());
    		if (lua_isstring(l_state, -1))  
    		{
    			return (string)lua_tostring(l_state, -1);
    		}  
    		return "";
    	}
    
    	int load_integer(string str)  
    	{  
    		lua_getglobal(l_state, str.c_str());
    		if (lua_isnumber(l_state, -1))  
    		{  
    			return (int)lua_tointeger(l_state, -1);  
    		}
    		return -1;  
    	}  
    
    	double load_double(string str)  
    	{  
    		lua_getglobal(l_state, str.c_str());
    		if (lua_isnumber(l_state, -1))  
    		{  
    			return (double)lua_tonumber(l_state, -1);  
    		}
    		return 0.0;  
    	}  
    
    	bool load_boolean(string str)  
    	{  
    		lua_getglobal(l_state, str.c_str());
    		if (lua_isboolean(l_state, -1))
    		{  
    			return (bool)(lua_toboolean(l_state, -1) != 0 ? true:false);
    		}  
    		return false;  
    	}
    
    	bool load_map(const char* name_,const int number_,string str_[],double array_list[],int type_ = 0)
    	{
    		if (number_ <= 0){
    			return false;
    		}
    
    		lua_getglobal(l_state,name_);
    		if (!lua_istable(l_state,-1)){
    			std::cout<<"input is not a table"<<std::endl;
    			goto funcend;
    		}
    		if (type_ == 0)
    		{
    			for (int i =0;i < number_;i++)
    			{
    				lua_getfield(l_state,-1,str_[i].c_str());
    				array_list[i] = (double)lua_tonumber(l_state,-1);
    				lua_pop(l_state,1);
    			}
    		}
    		else
    		{
    			for (int i =0;i < number_;i++)
    			{
    				lua_getfield(l_state,-1,str_[i].c_str());
    				array_list[i] = (int)lua_tointeger(l_state,-1);
    				lua_pop(l_state,1);
    			}
    		}
    funcend:
    		return true;
    	}
    
    	bool load_array(const char* name_,int*& array_)
    	{
    		if (NULL == name_ ){
    			return false;
    		}
    
    		lua_getglobal(l_state,name_);
    		if (!lua_istable(l_state,-1)){
    			std::cout<<"array is not a table"<<std::endl;
    		}
    		int top_index = lua_gettop(l_state);
    		lua_len(l_state,top_index);
    		int size_arary = lua_tointeger(l_state, -1);
    		array_ = new int[size_arary];
    
    		int i = 0;
    		lua_pushnil(l_state);
    		while (lua_next(l_state, top_index))
    		{
    			int	it_idx = lua_gettop(l_state);
    			array_[i] = lua_tointeger(l_state, -1);
    			i++;
    			lua_pop(l_state, 1);
    		}
    		return true;
    	}
    
    	bool load_array(const char* name_,double*& array_)
    	{
    		if (NULL == name_ ){
    			return false;
    		}
    
    		lua_getglobal(l_state,name_);
    		if (!lua_istable(l_state,-1)){
    			std::cout<<"array is not a table"<<std::endl;
    		}
    		int top_index = lua_gettop(l_state);
    		lua_len(l_state,top_index);
    		int size_arary = lua_tointeger(l_state, -1);
    		array_ = new double[size_arary];
    
    		int i = 0;
    		lua_pushnil(l_state);
    		while (lua_next(l_state, top_index))
    		{
    			int	it_idx = lua_gettop(l_state);
    			array_[i] = lua_tonumber(l_state, -1);
    			i++;
    			lua_pop(l_state, 1);
    		}
    		return true;
    	}
    private:
    		lua_State* l_state;
    };
    
    #endif
    

    内容还是算清晰的吧!基本代码自解释。所以不说了。

    但是头文件还是需要lualibdll库的。这些是基本的。

    四、测试代码

    测试代码主要一个main函数。

    测试代码:

    int main()
    {
    	//lua_State * L = luaL_newstate();
    	//luaL_openlibs(L);
    	// read config file
    	/*int w = 0 , h = 0;*/
    	//load_ui_conf(L,"test_read.lua",&w, & h);
    	//lua_close(L);
    
    	int w = 0 , h = 0;
    	// number 
    	lua_parser _lua_parser;
    	_lua_parser.load_file("test_read.lua");
    	w = _lua_parser.load_integer("width");
    	h = _lua_parser.load_double("height");
    	cout<<"width = "<<w<< ",height ="<<h<<endl;
    	cout<<"get as number is ok"<<endl;
    
    	// string
    	string title = _lua_parser.load_string("title");
    	cout<<"the content is :" <<title<<endl;
    	cout<<"get as string is ok"<<endl;
    
    	// map
    	char* name_ = "array";
    	int number_ = 3;
    	string str_[3]= {"r","g","b"};
    	double array_list_[3];
    	_lua_parser.load_map(name_,number_,str_,array_list_);
    	cout<<"get as map is ok"<<endl;
    
    	// array _1
    	double* array_list_int = NULL;
    	_lua_parser.load_array("array_1d", array_list_int);
    	cout<<"get as array is ok"<<endl;
    	if(NULL != array_list_int)
    	{
    		delete []array_list_int;
    		array_list_int = NULL;
    	}
    	// add table and array
    	
    	return 0;
    }



    五、测试结果

     如图:

    六、参考:


    http://www.360doc.com/content/11/1225/12/1317564_174843428.shtml

    http://mobile.51cto.com/iphone-287727.htm


    非常感谢! 

    七、附上测试代码:

    //#define luac_c
    //#define LUA_CORE
    //
    //#include "lua.h"
    //#include "lauxlib.h"
    //#include "lualib.h"
    //#pragma  comment(lib,"lua5.2.3.lib")
    
    // global 
    void error(lua_State * L ,const char * fmt,...)
    {
    	va_list argp;
    	va_start(argp,fmt);
    	vfprintf(stderr,fmt,argp);
    	va_end(argp);
    	lua_close(L);
    	exit(EXIT_FAILURE);
    }
    
    // read config file
    void load_ui_conf(lua_State * L,const char * file_name,int * w,int * h)
    {
    	if(luaL_loadfile(L,file_name) || lua_pcall(L,0,0,0))
    	{
    		error(L,"can not run config file: %s",lua_tostring(L,-1));
    	}
    
    	lua_getglobal(L,"width");
    	if(! lua_isnumber(L,-1))
    	{
    		error(L,"height should be a number");
    	}
    	*w = lua_tointeger(L,-1);
    
    	lua_getglobal(L,"height");
    	if(! lua_isnumber(L,-1))
    	{
    		error(L,"width should be a number");
    	}
    	*h = lua_tointeger(L,-1);
    	//
    	//lua_getglobal(L,"array");
    	//if (!lua_istable(L,-1)){
    	//	std::cout<<"array is not a table"<<std::endl;
    	//}
    	//int top_index = lua_gettop(L);
    	//lua_len(L,top_index);
    	//int size_arar = lua_tointeger(L, -1);
    	//int *array_re = new int[size_arar];
    
    
    	lua_getglobal(L,"array");
    	if (!lua_istable(L,-1)){
    		std::cout<<"array is not a table"<<std::endl;
    	}
    	//int t_idx = lua_gettop(L);
    	//lua_len(L,t_idx);
    	//int size_arary = lua_tointeger(L, -1);
    	int *array_re = new int[3];
    	
    	lua_getfield(L,-1,"r");
    	array_re[0] = (int)lua_tonumber(L,-1);
    	lua_pop(L,1);
    
    	lua_getfield(L,-1,"g");
    	array_re[1] = (int)lua_tonumber(L,-1);
    	lua_pop(L,1);
    	lua_getfield(L,-1,"b");
    	array_re[2] = (int)lua_tonumber(L,-1);
    	lua_pop(L,1);
    
    	//////
    	//lua_getglobal(L,"array_1d");
    	//if (!lua_istable(L,-1)){
    	//	std::cout<<"array is not a table"<<std::endl;
    	//}
    	//int t_idx = lua_gettop(L);
    	//lua_len(L,t_idx);
    	//int size_arary = lua_tointeger(L, -1);
    	//int *array_test = new int[size_arary];
    
    	//int i = 0;
    	//lua_pushnil(L);
    	//while (lua_next(L, t_idx))
    	//{
    	//	int	it_idx = lua_gettop(L);
    	//	printf("%d
    ", lua_tointeger(L, -1));
    	//	array_test[i] = lua_tointeger(L, -1);
    	//	i++;
    	//	lua_pop(L, 1);
    	//}
    	////
    	//printf("============================
    ");
    	//
    	//lua_getglobal(L,"array_2d");
    	//if (!lua_istable(L,-1)){
    	//	std::cout<<"array is not a table"<<std::endl;
    	//}
    	//int t_idx_2 = lua_gettop(L);
    	//lua_len(L,t_idx_2);
    	//int size_arary_2d = lua_tointeger(L, -1);
     //	int **array_test_2d = new int*[size_arary_2d];
    
    	//i = 0;
    	//lua_pushnil(L);
    	//while (lua_next(L, t_idx_2))
    	//{
    	//	int	it_idx = lua_gettop(L);
    	//	lua_len(L,it_idx);
    	//	int len = lua_tointeger(L, -1);
    	//	array_test_2d[i] = new int[len];
    	//	int j = 0;
    	//	lua_pushnil(L);
    	//	
    	//	while(lua_next(L, it_idx ))
    	//	{
    	//		printf("%d
    ", lua_tointeger(L, -1));
    	//		array_test_2d[i][j] = lua_tointeger(L, -1);
    	//		lua_pop(L, 1);
    	//		j++;
    	//	}
    	//	printf("------------
    ");
    	//	i++;
    	//	//lua_pop(L, 1);
    	//}
    	return;
    }
    


    int main()
    {
    	//lua_State * L = luaL_newstate();
    	//luaL_openlibs(L);
    	// read config file
    	/*int w = 0 , h = 0;*/
    	//load_ui_conf(L,"test_read.lua",&w, & h);
    	//lua_close(L);
           return 0;
    }


    其中注释掉的内容,有二维数组的处理,目前我还没有测试成功。也请高手指导。

    全部代码免分下载:c++读取lua配置类  http://download.csdn.net/detail/cartzhang/7374271 


    完毕!

     

    若有问题,请随时联系!

    非常感谢!

     

     


  • 相关阅读:
    BZOJ 3064 Tyvj 1518 CPU监控
    JS 省市区级联 修改地址操作时的默认选中方法
    《deetom》项目开发历程<五> PHP邮件
    input type = "image" 造成提交表单
    三元表达式
    二进制计算题
    关于 原码 反码 补码 位运算
    JS 返回
    google 二位码API
    ECLIPSE 集成 PHP开发环境
  • 原文地址:https://www.cnblogs.com/qitian1/p/6461980.html
Copyright © 2011-2022 走看看