How to use C ++ to read the Lua configuration file

Source: Internet
Author: User

UseC ++ReadLua configuration fileThe implementation case is the content to be introduced in this article, mainly for understanding and learningLuaOfConfiguration FileThe content of this article is mainly implemented by Code. For details, see this article.

 
 
  1. // LuaEx. h file
  2.  
  3. # Pragma once
  4. # Include <Windows. h>
  5. Extern "C"
  6. {
  7. # Include "lua/lua. h"
  8. # Include "lua/lualib. h"
  9. # Include "lua/lauxlib. h"
  10. };
  11.  
  12. Class LuaEx
  13. {
  14. Public:
  15. LuaEx (void );
  16. ~ LuaEx (void );
  17. Bool LoadFile (LPCSTR str); // load the lua File
  18. LPSTR LoadString (LPCSTR str); // read the string
  19. Int LoadInteger (LPCSTR str); // read integer
  20. Double LoadDouble (LPCSTR str); // read floating point type
  21. Bool LoadBoolean (LPCSTR str); // read Boolean
  22.  
  23. Private:
  24. Lua_State * L; // lua pointer
  25. };
  26.  
  27. // LuaEx. cpp File
  28.  
  29. # Include ". \ luaex. h"
  30. # Pragma comment (lib, ". \ lua. lib ")
  31.  
  32. LuaEx: LuaEx (void)
  33. {
  34. L = lua_open ();
  35. LuaL_openlibs (L );
  36. }
  37.  
  38. LuaEx ::~ LuaEx (void)
  39. {
  40. Lua_close (L );
  41. }
  42.  
  43. Bool LuaEx: LoadFile (LPCSTR str)
  44. {
  45. If (luaL_dofile (L, str ))
  46. {
  47. Return false;
  48. }
  49. Return true;
  50. }
  51.  
  52. LPSTR LuaEx: LoadString (LPCSTR str)
  53. {
  54. Lua_getglobal (L, str );
  55. If (lua_isstring (L,-1 ))
  56. {
  57. Return (LPSTR) lua_tostring (L,-1 );
  58. }
  59. Return NULL;
  60. }
  61.  
  62. Int LuaEx: LoadInteger (LPCSTR str)
  63. {
  64. Lua_getglobal (L, str );
  65. If (lua_isnumber (L,-1 ))
  66. {
  67. Return (int) lua_tointeger (L,-1 );
  68. }
  69. Return NULL;
  70. }
  71.  
  72. Double LuaEx: LoadDouble (LPCSTR str)
  73. {
  74. Lua_getglobal (L, str );
  75. If (lua_isnumber (L,-1 ))
  76. {
  77. Return (double) lua_tonumber (L,-1 );
  78. }
  79. Return 0.0;
  80. }
  81.  
  82. Bool LuaEx: LoadBoolean (LPCSTR str)
  83. {
  84. Lua_getglobal (L, str );
  85. If (lua_isboolean (L,-1 ))
  86. {
  87. Return (bool) lua_toboolean (L,-1 );
  88. }
  89. Return false;
  90. }

Instantiate a LuaEx class where you want to use the configuration file.

Call LoadFile to load the file. The parameter is the file path. The file format can be as follows:

 
 
  1. Title = "game"
  2. Width = 640
  3. Height = 480
  4. IsWindowed = true;
  5. UseSound = false;
  6. HideMouse = false;

The end of the semicolon can be added or not added. It is to write a lua script, but only contains the variable and does not contain the method.

Then you can read the content. For example

 
 
  1. LoadString ("title"); // indicates the value of the variable named title.

The parameters of all functions in this class are strings.

Summary: AboutC ++ReadLua configuration fileThe implementation case has been introduced. I hope this article will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.