一、内容介绍

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

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

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

二、Lua配置文件

配置文件名称为test_read.lua 。

文件内容为:

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

三、解析类头文件

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

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

头文件内容为:

  1. #ifndef _rw_lua_parser_h__
  2. #define _rw_lua_parser_h__
  3.  
  4. #include <string>
  5. using namespace std;
  6. #define luac_c
  7. #define LUA_CORE
  8. #include "lua.h"
  9. #include "lauxlib.h"
  10. #include "lualib.h"
  11. #pragma comment(lib,"lua5.2.3.lib")
  12.  
  13. class lua_parser
  14. {
  15. public:
  16. lua_parser(void)
  17. {
  18. l_state = luaL_newstate();
  19. luaL_openlibs(l_state);
  20. }
  21.  
  22. virtual ~lua_parser(void)
  23. {
  24. lua_close(l_state);
  25. }
  26.  
  27. bool load_file(string str)
  28. {
  29. if(luaL_dofile(l_state, str.c_str())){
  30. return false;
  31. }
  32. return true;
  33. }
  34.  
  35. string load_string(string str)
  36. {
  37. lua_getglobal(l_state, str.c_str());
  38. if (lua_isstring(l_state, -1))
  39. {
  40. return (string)lua_tostring(l_state, -1);
  41. }
  42. return "";
  43. }
  44.  
  45. int load_integer(string str)
  46. {
  47. lua_getglobal(l_state, str.c_str());
  48. if (lua_isnumber(l_state, -1))
  49. {
  50. return (int)lua_tointeger(l_state, -1);
  51. }
  52. return -1;
  53. }
  54.  
  55. double load_double(string str)
  56. {
  57. lua_getglobal(l_state, str.c_str());
  58. if (lua_isnumber(l_state, -1))
  59. {
  60. return (double)lua_tonumber(l_state, -1);
  61. }
  62. return 0.0;
  63. }
  64.  
  65. bool load_boolean(string str)
  66. {
  67. lua_getglobal(l_state, str.c_str());
  68. if (lua_isboolean(l_state, -1))
  69. {
  70. return (bool)(lua_toboolean(l_state, -1) != 0 ? true:false);
  71. }
  72. return false;
  73. }
  74.  
  75. bool load_map(const char* name_,const int number_,string str_[],double array_list[],int type_ = 0)
  76. {
  77. if (number_ <= 0){
  78. return false;
  79. }
  80.  
  81. lua_getglobal(l_state,name_);
  82. if (!lua_istable(l_state,-1)){
  83. std::cout<<"input is not a table"<<std::endl;
  84. goto funcend;
  85. }
  86. if (type_ == 0)
  87. {
  88. for (int i =0;i < number_;i++)
  89. {
  90. lua_getfield(l_state,-1,str_[i].c_str());
  91. array_list[i] = (double)lua_tonumber(l_state,-1);
  92. lua_pop(l_state,1);
  93. }
  94. }
  95. else
  96. {
  97. for (int i =0;i < number_;i++)
  98. {
  99. lua_getfield(l_state,-1,str_[i].c_str());
  100. array_list[i] = (int)lua_tointeger(l_state,-1);
  101. lua_pop(l_state,1);
  102. }
  103. }
  104. funcend:
  105. return true;
  106. }
  107.  
  108. bool load_array(const char* name_,int*& array_)
  109. {
  110. if (NULL == name_ ){
  111. return false;
  112. }
  113.  
  114. lua_getglobal(l_state,name_);
  115. if (!lua_istable(l_state,-1)){
  116. std::cout<<"array is not a table"<<std::endl;
  117. }
  118. int top_index = lua_gettop(l_state);
  119. lua_len(l_state,top_index);
  120. int size_arary = lua_tointeger(l_state, -1);
  121. array_ = new int[size_arary];
  122.  
  123. int i = 0;
  124. lua_pushnil(l_state);
  125. while (lua_next(l_state, top_index))
  126. {
  127. int it_idx = lua_gettop(l_state);
  128. array_[i] = lua_tointeger(l_state, -1);
  129. i++;
  130. lua_pop(l_state, 1);
  131. }
  132. return true;
  133. }
  134.  
  135. bool load_array(const char* name_,double*& array_)
  136. {
  137. if (NULL == name_ ){
  138. return false;
  139. }
  140.  
  141. lua_getglobal(l_state,name_);
  142. if (!lua_istable(l_state,-1)){
  143. std::cout<<"array is not a table"<<std::endl;
  144. }
  145. int top_index = lua_gettop(l_state);
  146. lua_len(l_state,top_index);
  147. int size_arary = lua_tointeger(l_state, -1);
  148. array_ = new double[size_arary];
  149.  
  150. int i = 0;
  151. lua_pushnil(l_state);
  152. while (lua_next(l_state, top_index))
  153. {
  154. int it_idx = lua_gettop(l_state);
  155. array_[i] = lua_tonumber(l_state, -1);
  156. i++;
  157. lua_pop(l_state, 1);
  158. }
  159. return true;
  160. }
  161. private:
  162. lua_State* l_state;
  163. };
  164.  
  165. #endif

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

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

四、测试代码

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

测试代码:

  1. int main()
  2. {
  3. //lua_State * L = luaL_newstate();
  4. //luaL_openlibs(L);
  5. // read config file
  6. /*int w = 0 , h = 0;*/
  7. //load_ui_conf(L,"test_read.lua",&w, & h);
  8. //lua_close(L);
  9.  
  10. int w = 0 , h = 0;
  11. // number
  12. lua_parser _lua_parser;
  13. _lua_parser.load_file("test_read.lua");
  14. w = _lua_parser.load_integer("width");
  15. h = _lua_parser.load_double("height");
  16. cout<<"width = "<<w<< ",height ="<<h<<endl;
  17. cout<<"get as number is ok"<<endl;
  18.  
  19. // string
  20. string title = _lua_parser.load_string("title");
  21. cout<<"the content is :" <<title<<endl;
  22. cout<<"get as string is ok"<<endl;
  23.  
  24. // map
  25. char* name_ = "array";
  26. int number_ = 3;
  27. string str_[3]= {"r","g","b"};
  28. double array_list_[3];
  29. _lua_parser.load_map(name_,number_,str_,array_list_);
  30. cout<<"get as map is ok"<<endl;
  31.  
  32. // array _1
  33. double* array_list_int = NULL;
  34. _lua_parser.load_array("array_1d", array_list_int);
  35. cout<<"get as array is ok"<<endl;
  36. if(NULL != array_list_int)
  37. {
  38. delete []array_list_int;
  39. array_list_int = NULL;
  40. }
  41. // add table and array
  42.  
  43. return 0;
  44. }

五、测试结果

如图:

六、参考:


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

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

非常感谢!

七、附上测试代码:

  1. //#define luac_c
  2. //#define LUA_CORE
  3. //
  4. //#include "lua.h"
  5. //#include "lauxlib.h"
  6. //#include "lualib.h"
  7. //#pragma comment(lib,"lua5.2.3.lib")
  8.  
  9. // global
  10. void error(lua_State * L ,const char * fmt,...)
  11. {
  12. va_list argp;
  13. va_start(argp,fmt);
  14. vfprintf(stderr,fmt,argp);
  15. va_end(argp);
  16. lua_close(L);
  17. exit(EXIT_FAILURE);
  18. }
  19.  
  20. // read config file
  21. void load_ui_conf(lua_State * L,const char * file_name,int * w,int * h)
  22. {
  23. if(luaL_loadfile(L,file_name) || lua_pcall(L,0,0,0))
  24. {
  25. error(L,"can not run config file: %s",lua_tostring(L,-1));
  26. }
  27.  
  28. lua_getglobal(L,"width");
  29. if(! lua_isnumber(L,-1))
  30. {
  31. error(L,"height should be a number");
  32. }
  33. *w = lua_tointeger(L,-1);
  34.  
  35. lua_getglobal(L,"height");
  36. if(! lua_isnumber(L,-1))
  37. {
  38. error(L,"width should be a number");
  39. }
  40. *h = lua_tointeger(L,-1);
  41. //
  42. //lua_getglobal(L,"array");
  43. //if (!lua_istable(L,-1)){
  44. // std::cout<<"array is not a table"<<std::endl;
  45. //}
  46. //int top_index = lua_gettop(L);
  47. //lua_len(L,top_index);
  48. //int size_arar = lua_tointeger(L, -1);
  49. //int *array_re = new int[size_arar];
  50.  
  51. lua_getglobal(L,"array");
  52. if (!lua_istable(L,-1)){
  53. std::cout<<"array is not a table"<<std::endl;
  54. }
  55. //int t_idx = lua_gettop(L);
  56. //lua_len(L,t_idx);
  57. //int size_arary = lua_tointeger(L, -1);
  58. int *array_re = new int[3];
  59.  
  60. lua_getfield(L,-1,"r");
  61. array_re[0] = (int)lua_tonumber(L,-1);
  62. lua_pop(L,1);
  63.  
  64. lua_getfield(L,-1,"g");
  65. array_re[1] = (int)lua_tonumber(L,-1);
  66. lua_pop(L,1);
  67. lua_getfield(L,-1,"b");
  68. array_re[2] = (int)lua_tonumber(L,-1);
  69. lua_pop(L,1);
  70.  
  71. //////
  72. //lua_getglobal(L,"array_1d");
  73. //if (!lua_istable(L,-1)){
  74. // std::cout<<"array is not a table"<<std::endl;
  75. //}
  76. //int t_idx = lua_gettop(L);
  77. //lua_len(L,t_idx);
  78. //int size_arary = lua_tointeger(L, -1);
  79. //int *array_test = new int[size_arary];
  80.  
  81. //int i = 0;
  82. //lua_pushnil(L);
  83. //while (lua_next(L, t_idx))
  84. //{
  85. // int it_idx = lua_gettop(L);
  86. // printf("%d\n", lua_tointeger(L, -1));
  87. // array_test[i] = lua_tointeger(L, -1);
  88. // i++;
  89. // lua_pop(L, 1);
  90. //}
  91. ////
  92. //printf("============================\n");
  93. //
  94. //lua_getglobal(L,"array_2d");
  95. //if (!lua_istable(L,-1)){
  96. // std::cout<<"array is not a table"<<std::endl;
  97. //}
  98. //int t_idx_2 = lua_gettop(L);
  99. //lua_len(L,t_idx_2);
  100. //int size_arary_2d = lua_tointeger(L, -1);
  101. // int **array_test_2d = new int*[size_arary_2d];
  102.  
  103. //i = 0;
  104. //lua_pushnil(L);
  105. //while (lua_next(L, t_idx_2))
  106. //{
  107. // int it_idx = lua_gettop(L);
  108. // lua_len(L,it_idx);
  109. // int len = lua_tointeger(L, -1);
  110. // array_test_2d[i] = new int[len];
  111. // int j = 0;
  112. // lua_pushnil(L);
  113. //
  114. // while(lua_next(L, it_idx ))
  115. // {
  116. // printf("%d\n", lua_tointeger(L, -1));
  117. // array_test_2d[i][j] = lua_tointeger(L, -1);
  118. // lua_pop(L, 1);
  119. // j++;
  120. // }
  121. // printf("------------\n");
  122. // i++;
  123. // //lua_pop(L, 1);
  124. //}
  125. return;
  126. }
  1. int main()
  2. {
  3. //lua_State * L = luaL_newstate();
  4. //luaL_openlibs(L);
  5. // read config file
  6. /*int w = 0 , h = 0;*/
  7. //load_ui_conf(L,"test_read.lua",&w, & h);
  8. //lua_close(L);
  9. return 0;
  10. }

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

全部代码免分下载: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. 洛谷 P1358 扑克牌

    P1358 扑克牌 题目描述 组合数学是数学的重要组成部分,是一门研究离散对象的科学,它主要研究满足一定条件的组态(也称组合模型)的存在.计数以及构造等方面的问题.组合数学的主要内容有组合计数.组合设 ...

  2. [Anuglar & NgRx] StoreRouterConnectingModule

    Always treat Router as the source of truth When we use Ngrx, we can see that we will use a "Sto ...

  3. Python-根据成绩分析是否继续深造

    案例:该数据集的是一个关于每个学生成绩的数据集,接下来我们对该数据集进行分析,判断学生是否适合继续深造 数据集特征展示 GRE 成绩 (290 to 340) TOEFL 成绩(92 to 120) ...

  4. Spark MLlib架构解析(含分类算法、回归算法、聚类算法和协同过滤)

    Spark MLlib架构解析 MLlib的底层基础解析 MLlib的算法库分析 分类算法 回归算法 聚类算法 协同过滤 MLlib的实用程序分析 从架构图可以看出MLlib主要包含三个部分: 底层基 ...

  5. Trie&可持久化Trie

    WARNING:以下代码未经测试,若发现错误,欢迎指出qwq~ Trie树(字典树) 一种简单的数据结构,可存储大量字符串,可在$O(len)$的时间内完成插入,删除,查找等操作. 下面是一个简单的例 ...

  6. 开发板Ping不通虚拟机和主机

    Ubuntu 16.04      win7 笔记本连接学校的无线网 开发板S3c2440与笔记本仅通过COM连接 问题描述: 设置了桥接,主机与虚拟机IP在同一网段后,主机与虚拟机可以Ping,但是 ...

  7. 洛谷 P1724 东风早谷苗

    洛谷 P1724 东风早谷苗 题目描述 在幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆,当然有了与以往不同的功能了,那就是它能够自动行走, ...

  8. javascript中函数声明、变量声明以及变量赋值之间的关系与影响

    javascript中函数声明.变量声明以及变量赋值之间的关系与影响 函数声明.变量声明以及变量赋值之间有以下几点共识: 1.所有的全局变量都是window的属性 2.函数声明被提升到范围作用域的顶端 ...

  9. 洛谷 P1916 小书童——蚂蚁大战

    P1916 小书童——蚂蚁大战 题目背景 小A在你的帮助下,开始“刷题”,他在小书童里发现了一款叫“蚂蚁大战”(又称蛋糕保卫战)的游戏.(你懂得) 题目描述 游戏中会出现n只蚂蚁,分别有a1,a2…… ...

  10. Java String对象的经典问题

     先来看一个样例,代码例如以下:  public class Test {       public static void main(String[] args) {           Strin ...