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支持 ...
随机推荐
- 洛谷 P1358 扑克牌
P1358 扑克牌 题目描述 组合数学是数学的重要组成部分,是一门研究离散对象的科学,它主要研究满足一定条件的组态(也称组合模型)的存在.计数以及构造等方面的问题.组合数学的主要内容有组合计数.组合设 ...
- [Anuglar & NgRx] StoreRouterConnectingModule
Always treat Router as the source of truth When we use Ngrx, we can see that we will use a "Sto ...
- Python-根据成绩分析是否继续深造
案例:该数据集的是一个关于每个学生成绩的数据集,接下来我们对该数据集进行分析,判断学生是否适合继续深造 数据集特征展示 GRE 成绩 (290 to 340) TOEFL 成绩(92 to 120) ...
- Spark MLlib架构解析(含分类算法、回归算法、聚类算法和协同过滤)
Spark MLlib架构解析 MLlib的底层基础解析 MLlib的算法库分析 分类算法 回归算法 聚类算法 协同过滤 MLlib的实用程序分析 从架构图可以看出MLlib主要包含三个部分: 底层基 ...
- Trie&可持久化Trie
WARNING:以下代码未经测试,若发现错误,欢迎指出qwq~ Trie树(字典树) 一种简单的数据结构,可存储大量字符串,可在$O(len)$的时间内完成插入,删除,查找等操作. 下面是一个简单的例 ...
- 开发板Ping不通虚拟机和主机
Ubuntu 16.04 win7 笔记本连接学校的无线网 开发板S3c2440与笔记本仅通过COM连接 问题描述: 设置了桥接,主机与虚拟机IP在同一网段后,主机与虚拟机可以Ping,但是 ...
- 洛谷 P1724 东风早谷苗
洛谷 P1724 东风早谷苗 题目描述 在幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆,当然有了与以往不同的功能了,那就是它能够自动行走, ...
- javascript中函数声明、变量声明以及变量赋值之间的关系与影响
javascript中函数声明.变量声明以及变量赋值之间的关系与影响 函数声明.变量声明以及变量赋值之间有以下几点共识: 1.所有的全局变量都是window的属性 2.函数声明被提升到范围作用域的顶端 ...
- 洛谷 P1916 小书童——蚂蚁大战
P1916 小书童——蚂蚁大战 题目背景 小A在你的帮助下,开始“刷题”,他在小书童里发现了一款叫“蚂蚁大战”(又称蛋糕保卫战)的游戏.(你懂得) 题目描述 游戏中会出现n只蚂蚁,分别有a1,a2…… ...
- Java String对象的经典问题
先来看一个样例,代码例如以下: public class Test { public static void main(String[] args) { Strin ...