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.
- // LuaEx. h file
-
- # Pragma once
- # Include <Windows. h>
- Extern "C"
- {
- # Include "lua/lua. h"
- # Include "lua/lualib. h"
- # Include "lua/lauxlib. h"
- };
-
- Class LuaEx
- {
- Public:
- LuaEx (void );
- ~ LuaEx (void );
- Bool LoadFile (LPCSTR str); // load the lua File
- LPSTR LoadString (LPCSTR str); // read the string
- Int LoadInteger (LPCSTR str); // read integer
- Double LoadDouble (LPCSTR str); // read floating point type
- Bool LoadBoolean (LPCSTR str); // read Boolean
-
- Private:
- Lua_State * L; // lua pointer
- };
-
- // LuaEx. cpp File
-
- # Include ". \ luaex. h"
- # Pragma comment (lib, ". \ lua. lib ")
-
- LuaEx: LuaEx (void)
- {
- L = lua_open ();
- LuaL_openlibs (L );
- }
-
- LuaEx ::~ LuaEx (void)
- {
- Lua_close (L );
- }
-
- Bool LuaEx: LoadFile (LPCSTR str)
- {
- If (luaL_dofile (L, str ))
- {
- Return false;
- }
- Return true;
- }
-
- LPSTR LuaEx: LoadString (LPCSTR str)
- {
- Lua_getglobal (L, str );
- If (lua_isstring (L,-1 ))
- {
- Return (LPSTR) lua_tostring (L,-1 );
- }
- Return NULL;
- }
-
- Int LuaEx: LoadInteger (LPCSTR str)
- {
- Lua_getglobal (L, str );
- If (lua_isnumber (L,-1 ))
- {
- Return (int) lua_tointeger (L,-1 );
- }
- Return NULL;
- }
-
- Double LuaEx: LoadDouble (LPCSTR str)
- {
- Lua_getglobal (L, str );
- If (lua_isnumber (L,-1 ))
- {
- Return (double) lua_tonumber (L,-1 );
- }
- Return 0.0;
- }
-
- Bool LuaEx: LoadBoolean (LPCSTR str)
- {
- Lua_getglobal (L, str );
- If (lua_isboolean (L,-1 ))
- {
- Return (bool) lua_toboolean (L,-1 );
- }
- Return false;
- }
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:
- Title = "game"
- Width = 640
- Height = 480
- IsWindowed = true;
- UseSound = false;
- 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
- 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!