一、内容介绍

把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

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

但是头文件还是需要lua的lib和dll库的。这些是基本的。

四、测试代码

测试代码主要一个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\n", lua_tointeger(L, -1));
// array_test[i] = lua_tointeger(L, -1);
// i++;
// lua_pop(L, 1);
//}
////
//printf("============================\n");
//
//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\n", lua_tointeger(L, -1));
// array_test_2d[i][j] = lua_tointeger(L, -1);
// lua_pop(L, 1);
// j++;
// }
// printf("------------\n");
// 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

完毕!

若有问题,请随时联系!

非常感谢!

最新文章

  1. HTTP 错误 404.3 – Not Found 由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。
  2. AFNetworking的理解
  3. c# winForm 等待窗体的实现
  4. UVA 1362 Exploring Pyramids 区间DP
  5. schtasks命令遇见ERROR: The request is not supported.
  6. codeforces #309 div1 D
  7. CAS原理
  8. Eclipse搭建Android开发环境(安装ADT,Android4.4.2)(转)
  9. C#中调用Windows API时的数据类型对应关系
  10. Java学习记录第一章
  11. sql with as 用法(转载)
  12. 在laravel环境下将图片存入MongoDB数据库
  13. 3.4 自动测试初步《精通ASP.NET MVC 5》
  14. (8)Microsoft office Word 2013版本操作入门_制作传单海报
  15. 使用glusterfs 作为 kubernetes PersistentVolume PersistentVolumeClaim 持久化仓库,高可用Rabbitmq,高可用mysql,高可用redis
  16. php 多线程
  17. UML类图中的六种关系(物理设计阶段)
  18. 【转】IE内嵌google chrome frame解决浏览器兼容问题
  19. 微信小程序wxml文件中调用自定义函数
  20. Cache Algorithms

热门文章

  1. Get Started with Git and Team Services
  2. myBatis通过逗号分隔字符串,foreach
  3. 商业模式(二):P2P网贷平台,利差和服务费为主的金融玩法
  4. 怎样使用 OneAPM 监控微软 Azure Cloud Service ?
  5. 《Java设计模式》之桥接模式
  6. css大会站点顶部的一个特效
  7. [NOIP2013]车站分级 解题报告
  8. git -处理分支合并
  9. IIS特殊字符设置
  10. Shelled-out Commands In Golang