一、内容介绍

把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

完毕!

若有问题,请随时联系!

非常感谢!

c++读取lua配置基础类的更多相关文章

  1. springboot 读取 yml 配置的几种方式

    前言:在springboot 项目中一般默认的配置文件是application.properties,但是实际项目中我们一般会使用application.yml 文件,下面就介绍一下在springbo ...

  2. DOS下读取PCI配置空间信息的汇编程序(通过IOCF8/IOCFC)

    汇编程序编写的读取PCI配置空间信息的代码(通过IOCF8/IOCFC): ;------------------------------------------------ ;功能: 读取PCI 配 ...

  3. Spring Boot 之注解@Component @ConfigurationProperties(prefix = "sms") 使用@ConfigurationProperties读取yml配置

    从spring-boot开始,已经支持yml文件形式的配置,@ConfigurationProperties的大致作用就是通过它可以把properties或者yml配置直接转成对象 @Componen ...

  4. 每日笔记---使用@ConfigurationProperties读取yml配置

    每日笔记---使用@ConfigurationProperties读取yml配置 参考地址  https://www.cnblogs.com/mycs-home/p/8352140.html 1.添加 ...

  5. python 读取consul配置

    自动化通过rcp client调用远端服务接口时,都需要将远端测试服务ip.端口记录在配置文件. 但由于,服务发布或重启会导致ip.端口变动. 以下将通过python-consul 自动去读取cons ...

  6. Springboot(二)-application.yml默认的配置项以及读取自定义配置

    写在前面 ===== spring-boot 版本:2.0.0.RELEASE ===== 读取自定义配置 1.配置文件:sys.properties supply.place=云南 supply.c ...

  7. golang 读取 ini配置信息

      package main //BY: 29295842@qq.com//这个有一定问题   如果配置信息里有中文就不行//[Server] ;MYSQL配置//Server=localhost   ...

  8. 表单配置项写法,表单写成JSON数组套对象,一行是一个数组单位,一列是一个对象单位,然后再写一个公共组件读取这个配置,循环加载slot,外层载入slot的自定义部分,比如input select等,这种写法就是把组件嵌套改为配置方式

    表单配置项写法,表单写成JSON数组套对象,一行是一个数组单位,一列是一个对象单位,然后再写一个公共组件读取这个配置,循环加载slot,外层载入slot的自定义部分,比如input select等,这 ...

  9. 使用Viper读取Nacos配置(开源)

    使用Viper读取Nacos配置(开源) 一.前言 目前Viper支持的Remote远程读取配置如 etcd, consul:目前还没有对Nacos进行支持,本文中将开源一个Nacos的Viper支持 ...

随机推荐

  1. vue绑定内联样式

    v-bind:style 的对象语法十分直观--看着非常像 CSS ,其实它是一个 JavaScript 对象. CSS 属性名可以用驼峰式(camelCase)或短横分隔命名(kebab-case) ...

  2. hadoop集群环境配置成功与否查看方法

    1.Hadoop启动jobhistoryserver来实现web查看作业的历史运行情况,由于在启动hdfs和Yarn进程之后,jobhistoryserver进程并没有启动,需要手动启动jobhist ...

  3. Linux下截图技巧

           在需要Linux显示图片的场合,最普通的方法,会考虑用数码相,或是用Vmware,或VPc来抓拍,这样以来会比较麻烦,Linux也自带了些工具例如Gimp,ksnapshot这里我介绍一 ...

  4. Codefroces Educational Round 27 845G Shortest Path Problem?

    Shortest Path Problem? You are given an undirected graph with weighted edges. The length of some pat ...

  5. 湖南省第八届大学生计算机程序设计竞赛(A,B,C,E,F,I,J)

    A 三家人 Description 有三户人家共拥有一座花园,每户人家的太太均需帮忙整理花园.A 太太工作了5 天,B 太太则工作了4 天,才将花园整理完毕.C 太太因为正身怀六甲无法加入她们的行列, ...

  6. 【Swing】一点基础操作

    之前实训的老师不推荐swing就没有学...然而学校老师又是另一种态度...加上学长作比赛用swing...学一下吧 1.将窗体放在中间 jdk1.4之后setLocationRelativeTo(o ...

  7. Mysql学习总结(13)——使用JDBC处理MySQL大数据

    一.基本概念 大数据也称之为LOB(Large Objects),LOB又分为:clob和blob,clob用于存储大文本,blob用于存储二进制数据,例如图像.声音.二进制文等. 在实际开发中,有时 ...

  8. wpf--------------datagrid全选反选 多选进行删除操作 前后台

    前台绑定 <DataGrid.Columns> <DataGridTemplateColumn > <DataGridTemplateColumn.HeaderTempl ...

  9. [ReasonML] Named & optional params

    // ::country is named param // ::country=?: which make it optional // because we make ::country=? op ...

  10. python实现获取文件列表中每一个文件keyword

    功能描写叙述: 获取某个路径下的全部文件,提取出每一个文件里出现频率最高的前300个字.保存在数据库其中. 前提.你须要配置好nltk #!/usr/bin/python #coding=utf-8 ...