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
内容还是算清晰的吧!基本代码自解释。所以不说了。
但是头文件还是需要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配置基础类的更多相关文章
- springboot 读取 yml 配置的几种方式
前言:在springboot 项目中一般默认的配置文件是application.properties,但是实际项目中我们一般会使用application.yml 文件,下面就介绍一下在springbo ...
- DOS下读取PCI配置空间信息的汇编程序(通过IOCF8/IOCFC)
汇编程序编写的读取PCI配置空间信息的代码(通过IOCF8/IOCFC): ;------------------------------------------------ ;功能: 读取PCI 配 ...
- Spring Boot 之注解@Component @ConfigurationProperties(prefix = "sms") 使用@ConfigurationProperties读取yml配置
从spring-boot开始,已经支持yml文件形式的配置,@ConfigurationProperties的大致作用就是通过它可以把properties或者yml配置直接转成对象 @Componen ...
- 每日笔记---使用@ConfigurationProperties读取yml配置
每日笔记---使用@ConfigurationProperties读取yml配置 参考地址 https://www.cnblogs.com/mycs-home/p/8352140.html 1.添加 ...
- python 读取consul配置
自动化通过rcp client调用远端服务接口时,都需要将远端测试服务ip.端口记录在配置文件. 但由于,服务发布或重启会导致ip.端口变动. 以下将通过python-consul 自动去读取cons ...
- Springboot(二)-application.yml默认的配置项以及读取自定义配置
写在前面 ===== spring-boot 版本:2.0.0.RELEASE ===== 读取自定义配置 1.配置文件:sys.properties supply.place=云南 supply.c ...
- golang 读取 ini配置信息
package main //BY: 29295842@qq.com//这个有一定问题 如果配置信息里有中文就不行//[Server] ;MYSQL配置//Server=localhost ...
- 表单配置项写法,表单写成JSON数组套对象,一行是一个数组单位,一列是一个对象单位,然后再写一个公共组件读取这个配置,循环加载slot,外层载入slot的自定义部分,比如input select等,这种写法就是把组件嵌套改为配置方式
表单配置项写法,表单写成JSON数组套对象,一行是一个数组单位,一列是一个对象单位,然后再写一个公共组件读取这个配置,循环加载slot,外层载入slot的自定义部分,比如input select等,这 ...
- 使用Viper读取Nacos配置(开源)
使用Viper读取Nacos配置(开源) 一.前言 目前Viper支持的Remote远程读取配置如 etcd, consul:目前还没有对Nacos进行支持,本文中将开源一个Nacos的Viper支持 ...
随机推荐
- Linux系统下到哪儿寻找硬件错误
Linux系统下到哪儿寻找硬件错误 当linux系统出现故障的时候,作为管理员首先要定位错误,现在linux有许多工具都能帮助用户寻找错误,要学会利用他们确定问题.这些工具包括dmesg. ...
- C#导出EXCEL(DataTable导出EXCEL)
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.I ...
- ATcoderARC100D Equal Cut
ARC100 D - Equal Cut Description: 给出长度为n的序列A,把这个序列分成连续的四段,最小化极差. \(4≤n≤2×10^5,4≤n≤2×10^5\) Solution: ...
- MyBatis学习总结(11)——MyBatis动态Sql语句
MyBatis中对数据库的操作,有时要带一些条件,因此动态SQL语句非常有必要,下面就主要来讲讲几个常用的动态SQL语句的语法 MyBatis中用于实现动态SQL的元素主要有: if choose(w ...
- PatentTips - Control register access virtualization performance improvement
BACKGROUND OF THE INVENTION A conventional virtual-machine monitor (VMM) typically runs on a compute ...
- POJ 3461 Oulipo KMP算法题解
本题就是给出非常多对字符串,然后问一个字符串在另外一个字符串出现的次数. 就是所谓的Strstr函数啦. Leetcode有这道差点儿一模一样的题目. 使用KMP算法加速.算法高手必会的算法了. 另外 ...
- 制作 wordpress 博客静态化到本地
wget 克隆 wordpress 博客镜像 wget -e robots=off -w 1 -xq -np -nH -pk -m -t 1 -P "./wordpress.org" ...
- Android学习笔记进阶15之Shader渲染
Android提供的Shader类主要是渲染图像以及一些几何图形. Shader有几个直接子类: BitmapShader : 主要用来渲染图像 LinearGradient :用来进行线性渲 ...
- Android连接socket服务器上传下载多个文件
android连接socket服务器上传下载多个文件1.socket服务端SocketServer.java public class SocketServer { ;// 端口号,必须与客户端一致 ...
- Large Division (大数求余)
Given two integers, a and b, you should check whether a is divisible by b or not. We know that an in ...